msdosfs_vnops.c revision 1.98 1 /* $NetBSD: msdosfs_vnops.c,v 1.98 2017/04/26 03:02:48 riastradh 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.98 2017/04/26 03:02:48 riastradh 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 VN_KNOTE(ap->a_dvp, NOTE_WRITE);
155 *ap->a_vpp = DETOV(dep);
156 return (0);
157
158 bad:
159 return (error);
160 }
161
162 int
163 msdosfs_close(void *v)
164 {
165 struct vop_close_args /* {
166 struct vnode *a_vp;
167 int a_fflag;
168 kauth_cred_t a_cred;
169 } */ *ap = v;
170 struct vnode *vp = ap->a_vp;
171 struct denode *dep = VTODE(vp);
172
173 mutex_enter(vp->v_interlock);
174 if (vp->v_usecount > 1)
175 DETIMES(dep, NULL, NULL, NULL, dep->de_pmp->pm_gmtoff);
176 mutex_exit(vp->v_interlock);
177 return (0);
178 }
179
180 static int
181 msdosfs_check_possible(struct vnode *vp, struct denode *dep, mode_t mode)
182 {
183
184 /*
185 * Disallow write attempts on read-only file systems;
186 * unless the file is a socket, fifo, or a block or
187 * character device resident on the file system.
188 */
189 if (mode & VWRITE) {
190 switch (vp->v_type) {
191 case VDIR:
192 case VLNK:
193 case VREG:
194 if (vp->v_mount->mnt_flag & MNT_RDONLY)
195 return (EROFS);
196 default:
197 break;
198 }
199 }
200
201 return 0;
202 }
203
204 static int
205 msdosfs_check_permitted(struct vnode *vp, struct denode *dep, mode_t mode,
206 kauth_cred_t cred)
207 {
208 struct msdosfsmount *pmp = dep->de_pmp;
209 mode_t file_mode;
210
211 if ((dep->de_Attributes & ATTR_READONLY) == 0)
212 file_mode = S_IRWXU|S_IRWXG|S_IRWXO;
213 else
214 file_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
215
216 file_mode &= (vp->v_type == VDIR ? pmp->pm_dirmask : pmp->pm_mask);
217
218 return kauth_authorize_vnode(cred, KAUTH_ACCESS_ACTION(mode,
219 vp->v_type, file_mode), vp, NULL, genfs_can_access(vp->v_type,
220 file_mode, pmp->pm_uid, pmp->pm_gid, mode, cred));
221 }
222
223 int
224 msdosfs_access(void *v)
225 {
226 struct vop_access_args /* {
227 struct vnode *a_vp;
228 int a_mode;
229 kauth_cred_t a_cred;
230 } */ *ap = v;
231 struct vnode *vp = ap->a_vp;
232 struct denode *dep = VTODE(vp);
233 int error;
234
235 error = msdosfs_check_possible(vp, dep, ap->a_mode);
236 if (error)
237 return error;
238
239 error = msdosfs_check_permitted(vp, dep, ap->a_mode, ap->a_cred);
240
241 return error;
242 }
243
244 int
245 msdosfs_getattr(void *v)
246 {
247 struct vop_getattr_args /* {
248 struct vnode *a_vp;
249 struct vattr *a_vap;
250 kauth_cred_t a_cred;
251 } */ *ap = v;
252 struct denode *dep = VTODE(ap->a_vp);
253 struct msdosfsmount *pmp = dep->de_pmp;
254 struct vattr *vap = ap->a_vap;
255 mode_t mode;
256 u_long dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
257 ino_t fileid;
258
259 DETIMES(dep, NULL, NULL, NULL, pmp->pm_gmtoff);
260 vap->va_fsid = dep->de_dev;
261 /*
262 * The following computation of the fileid must be the same as that
263 * used in msdosfs_readdir() to compute d_fileno. If not, pwd
264 * doesn't work.
265 */
266 if (dep->de_Attributes & ATTR_DIRECTORY) {
267 fileid = cntobn(pmp, (ino_t)dep->de_StartCluster) * dirsperblk;
268 if (dep->de_StartCluster == MSDOSFSROOT)
269 fileid = 1;
270 } else {
271 fileid = cntobn(pmp, (ino_t)dep->de_dirclust) * dirsperblk;
272 if (dep->de_dirclust == MSDOSFSROOT)
273 fileid = roottobn(pmp, 0) * dirsperblk;
274 fileid += dep->de_diroffset / sizeof(struct direntry);
275 }
276 vap->va_fileid = fileid;
277 if ((dep->de_Attributes & ATTR_READONLY) == 0)
278 mode = S_IRWXU|S_IRWXG|S_IRWXO;
279 else
280 mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
281 vap->va_mode =
282 mode & (ap->a_vp->v_type == VDIR ? pmp->pm_dirmask : pmp->pm_mask);
283 vap->va_uid = pmp->pm_uid;
284 vap->va_gid = pmp->pm_gid;
285 vap->va_nlink = 1;
286 vap->va_rdev = 0;
287 vap->va_size = ap->a_vp->v_size;
288 dos2unixtime(dep->de_MDate, dep->de_MTime, 0, pmp->pm_gmtoff,
289 &vap->va_mtime);
290 if (dep->de_pmp->pm_flags & MSDOSFSMNT_LONGNAME) {
291 dos2unixtime(dep->de_ADate, 0, 0, pmp->pm_gmtoff,
292 &vap->va_atime);
293 dos2unixtime(dep->de_CDate, dep->de_CTime, dep->de_CHun,
294 pmp->pm_gmtoff, &vap->va_ctime);
295 } else {
296 vap->va_atime = vap->va_mtime;
297 vap->va_ctime = vap->va_mtime;
298 }
299 vap->va_flags = 0;
300 if ((dep->de_Attributes & ATTR_ARCHIVE) == 0) {
301 vap->va_flags |= SF_ARCHIVED;
302 vap->va_mode |= S_ARCH1;
303 }
304 vap->va_gen = 0;
305 vap->va_blocksize = pmp->pm_bpcluster;
306 vap->va_bytes =
307 (dep->de_FileSize + pmp->pm_crbomask) & ~pmp->pm_crbomask;
308 vap->va_type = ap->a_vp->v_type;
309 return (0);
310 }
311
312 int
313 msdosfs_setattr(void *v)
314 {
315 struct vop_setattr_args /* {
316 struct vnode *a_vp;
317 struct vattr *a_vap;
318 kauth_cred_t a_cred;
319 } */ *ap = v;
320 int error = 0, de_changed = 0;
321 struct denode *dep = VTODE(ap->a_vp);
322 struct msdosfsmount *pmp = dep->de_pmp;
323 struct vnode *vp = ap->a_vp;
324 struct vattr *vap = ap->a_vap;
325 kauth_cred_t cred = ap->a_cred;
326
327 #ifdef MSDOSFS_DEBUG
328 printf("msdosfs_setattr(): vp %p, vap %p, cred %p\n",
329 ap->a_vp, vap, cred);
330 #endif
331 /*
332 * Note we silently ignore uid or gid changes.
333 */
334 if ((vap->va_type != VNON) || (vap->va_nlink != (nlink_t)VNOVAL) ||
335 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
336 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
337 (vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL) ||
338 (vap->va_uid != VNOVAL && vap->va_uid != pmp->pm_uid) ||
339 (vap->va_gid != VNOVAL && vap->va_gid != pmp->pm_gid)) {
340 #ifdef MSDOSFS_DEBUG
341 printf("msdosfs_setattr(): returning EINVAL\n");
342 printf(" va_type %d, va_nlink %x, va_fsid %"PRIx64", va_fileid %llx\n",
343 vap->va_type, vap->va_nlink, vap->va_fsid,
344 (unsigned long long)vap->va_fileid);
345 printf(" va_blocksize %lx, va_rdev %"PRIx64", va_bytes %"PRIx64", va_gen %lx\n",
346 vap->va_blocksize, vap->va_rdev, vap->va_bytes, vap->va_gen);
347 #endif
348 return (EINVAL);
349 }
350 /*
351 * Silently ignore attributes modifications on directories.
352 */
353 if (ap->a_vp->v_type == VDIR)
354 return 0;
355
356 if (vap->va_size != VNOVAL) {
357 if (vp->v_mount->mnt_flag & MNT_RDONLY) {
358 error = EROFS;
359 goto bad;
360 }
361 error = detrunc(dep, (u_long)vap->va_size, 0, cred);
362 if (error)
363 goto bad;
364 de_changed = 1;
365 }
366 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
367 if (vp->v_mount->mnt_flag & MNT_RDONLY) {
368 error = EROFS;
369 goto bad;
370 }
371 error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_TIMES,
372 ap->a_vp, NULL, genfs_can_chtimes(ap->a_vp, vap->va_vaflags,
373 pmp->pm_uid, cred));
374 if (error)
375 goto bad;
376 if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95) == 0 &&
377 vap->va_atime.tv_sec != VNOVAL)
378 unix2dostime(&vap->va_atime, pmp->pm_gmtoff, &dep->de_ADate, NULL, NULL);
379 if (vap->va_mtime.tv_sec != VNOVAL)
380 unix2dostime(&vap->va_mtime, pmp->pm_gmtoff, &dep->de_MDate, &dep->de_MTime, NULL);
381 dep->de_Attributes |= ATTR_ARCHIVE;
382 dep->de_flag |= DE_MODIFIED;
383 de_changed = 1;
384 }
385 /*
386 * DOS files only have the ability to have their writability
387 * attribute set, so we use the owner write bit to set the readonly
388 * attribute.
389 */
390 if (vap->va_mode != (mode_t)VNOVAL) {
391 if (vp->v_mount->mnt_flag & MNT_RDONLY) {
392 error = EROFS;
393 goto bad;
394 }
395 error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_FLAGS, vp,
396 NULL, genfs_can_chflags(cred, vp->v_type, pmp->pm_uid, false));
397 if (error)
398 goto bad;
399 /* We ignore the read and execute bits. */
400 if (vap->va_mode & S_IWUSR)
401 dep->de_Attributes &= ~ATTR_READONLY;
402 else
403 dep->de_Attributes |= ATTR_READONLY;
404 dep->de_flag |= DE_MODIFIED;
405 de_changed = 1;
406 }
407 /*
408 * Allow the `archived' bit to be toggled.
409 */
410 if (vap->va_flags != VNOVAL) {
411 if (vp->v_mount->mnt_flag & MNT_RDONLY) {
412 error = EROFS;
413 goto bad;
414 }
415 error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_FLAGS, vp,
416 NULL, genfs_can_chflags(cred, vp->v_type, pmp->pm_uid, false));
417 if (error)
418 goto bad;
419 if (vap->va_flags & SF_ARCHIVED)
420 dep->de_Attributes &= ~ATTR_ARCHIVE;
421 else
422 dep->de_Attributes |= ATTR_ARCHIVE;
423 dep->de_flag |= DE_MODIFIED;
424 de_changed = 1;
425 }
426
427 if (de_changed) {
428 VN_KNOTE(vp, NOTE_ATTRIB);
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_UNMAP_FLAG(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, extended = 0;
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_UNMAP_FLAG(vp));
626 extended = 1;
627 }
628
629 do {
630 oldoff = uio->uio_offset;
631 bytelen = uio->uio_resid;
632
633 error = ubc_uiomove(&vp->v_uobj, uio, bytelen,
634 IO_ADV_DECODE(ioflag), UBC_WRITE | UBC_UNMAP_FLAG(vp));
635 if (error)
636 break;
637
638 /*
639 * flush what we just wrote if necessary.
640 * XXXUBC simplistic async flushing.
641 */
642
643 if (!async && oldoff >> 16 != uio->uio_offset >> 16) {
644 mutex_enter(vp->v_interlock);
645 error = VOP_PUTPAGES(vp, (oldoff >> 16) << 16,
646 (uio->uio_offset >> 16) << 16,
647 PGO_CLEANIT | PGO_LAZY);
648 }
649 } while (error == 0 && uio->uio_resid > 0);
650
651 /* set final size */
652 uvm_vnp_setsize(vp, dep->de_FileSize);
653 if (error == 0 && ioflag & IO_SYNC) {
654 mutex_enter(vp->v_interlock);
655 error = VOP_PUTPAGES(vp, trunc_page(oldoff),
656 round_page(oldoff + bytelen), PGO_CLEANIT | PGO_SYNCIO);
657 }
658 dep->de_flag |= DE_UPDATE;
659
660 /*
661 * If the write failed and they want us to, truncate the file back
662 * to the size it was before the write was attempted.
663 */
664 errexit:
665 if (resid > uio->uio_resid)
666 VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
667 if (error) {
668 detrunc(dep, osize, ioflag & IO_SYNC, NOCRED);
669 uio->uio_offset -= resid - uio->uio_resid;
670 uio->uio_resid = resid;
671 } else if ((ioflag & IO_SYNC) == IO_SYNC)
672 error = deupdat(dep, 1);
673 KASSERT(vp->v_size == dep->de_FileSize);
674 return (error);
675 }
676
677 int
678 msdosfs_update(struct vnode *vp, const struct timespec *acc,
679 const struct timespec *mod, int flags)
680 {
681 struct buf *bp;
682 struct direntry *dirp;
683 struct denode *dep;
684 int error;
685
686 if (vp->v_mount->mnt_flag & MNT_RDONLY)
687 return (0);
688 dep = VTODE(vp);
689 DETIMES(dep, acc, mod, NULL, dep->de_pmp->pm_gmtoff);
690 if ((dep->de_flag & DE_MODIFIED) == 0)
691 return (0);
692 dep->de_flag &= ~DE_MODIFIED;
693 if (dep->de_Attributes & ATTR_DIRECTORY)
694 return (0);
695 if (dep->de_refcnt <= 0)
696 return (0);
697 error = readde(dep, &bp, &dirp);
698 if (error)
699 return (error);
700 DE_EXTERNALIZE(dirp, dep);
701 if (flags & (UPDATE_WAIT|UPDATE_DIROP))
702 return (bwrite(bp));
703 else {
704 bdwrite(bp);
705 return (0);
706 }
707 }
708
709 /*
710 * Flush the blocks of a file to disk.
711 *
712 * This function is worthless for vnodes that represent directories. Maybe we
713 * could just do a sync if they try an fsync on a directory file.
714 */
715 int
716 msdosfs_remove(void *v)
717 {
718 struct vop_remove_v2_args /* {
719 struct vnode *a_dvp;
720 struct vnode *a_vp;
721 struct componentname *a_cnp;
722 } */ *ap = v;
723 struct denode *dep = VTODE(ap->a_vp);
724 struct denode *ddep = VTODE(ap->a_dvp);
725 int error;
726
727 if (ap->a_vp->v_type == VDIR)
728 error = EPERM;
729 else
730 error = removede(ddep, dep);
731 #ifdef MSDOSFS_DEBUG
732 printf("msdosfs_remove(), dep %p, v_usecount %d\n",
733 dep, ap->a_vp->v_usecount);
734 #endif
735 VN_KNOTE(ap->a_vp, NOTE_DELETE);
736 VN_KNOTE(ap->a_dvp, NOTE_WRITE);
737 if (ddep == dep)
738 vrele(ap->a_vp);
739 else
740 vput(ap->a_vp); /* causes msdosfs_inactive() to be called
741 * via vrele() */
742
743 return (error);
744 }
745
746 /*
747 * Renames on files require moving the denode to a new hash queue since the
748 * denode's location is used to compute which hash queue to put the file
749 * in. Unless it is a rename in place. For example "mv a b".
750 *
751 * What follows is the basic algorithm:
752 *
753 * if (file move) {
754 * if (dest file exists) {
755 * remove dest file
756 * }
757 * if (dest and src in same directory) {
758 * rewrite name in existing directory slot
759 * } else {
760 * write new entry in dest directory
761 * update offset and dirclust in denode
762 * move denode to new hash chain
763 * clear old directory entry
764 * }
765 * } else {
766 * directory move
767 * if (dest directory exists) {
768 * if (dest is not empty) {
769 * return ENOTEMPTY
770 * }
771 * remove dest directory
772 * }
773 * if (dest and src in same directory) {
774 * rewrite name in existing entry
775 * } else {
776 * be sure dest is not a child of src directory
777 * write entry in dest directory
778 * update "." and ".." in moved directory
779 * update offset and dirclust in denode
780 * move denode to new hash chain
781 * clear old directory entry for moved directory
782 * }
783 * }
784 *
785 * On entry:
786 * source's parent directory is unlocked
787 * source file or directory is unlocked
788 * destination's parent directory is locked
789 * destination file or directory is locked if it exists
790 *
791 * On exit:
792 * all denodes should be released
793 *
794 * Notes:
795 * I'm not sure how the memory containing the pathnames pointed at by the
796 * componentname structures is freed, there may be some memory bleeding
797 * for each rename done.
798 *
799 * --More-- Notes:
800 * This routine needs help. badly.
801 */
802 int
803 msdosfs_rename(void *v)
804 {
805 struct vop_rename_args /* {
806 struct vnode *a_fdvp;
807 struct vnode *a_fvp;
808 struct componentname *a_fcnp;
809 struct vnode *a_tdvp;
810 struct vnode *a_tvp;
811 struct componentname *a_tcnp;
812 } */ *ap = v;
813 struct vnode *tvp = ap->a_tvp;
814 struct vnode *tdvp = ap->a_tdvp;
815 struct vnode *fvp = ap->a_fvp;
816 struct vnode *fdvp = ap->a_fdvp;
817 struct componentname *tcnp = ap->a_tcnp;
818 struct componentname *fcnp = ap->a_fcnp;
819 struct denode *ip, *xp, *dp, *zp;
820 u_char toname[12], oldname[12];
821 u_long from_diroffset, to_diroffset;
822 u_char to_count;
823 int doingdirectory = 0, newparent = 0;
824 int error;
825 u_long cn;
826 daddr_t bn;
827 struct msdosfsmount *pmp;
828 struct direntry *dotdotp;
829 struct buf *bp;
830
831 pmp = VFSTOMSDOSFS(fdvp->v_mount);
832
833 /*
834 * Check for cross-device rename.
835 */
836 if ((fvp->v_mount != tdvp->v_mount) ||
837 (tvp && (fvp->v_mount != tvp->v_mount))) {
838 error = EXDEV;
839 abortit:
840 VOP_ABORTOP(tdvp, tcnp);
841 if (tdvp == tvp)
842 vrele(tdvp);
843 else
844 vput(tdvp);
845 if (tvp)
846 vput(tvp);
847 VOP_ABORTOP(fdvp, fcnp);
848 vrele(fdvp);
849 vrele(fvp);
850 return (error);
851 }
852
853 /*
854 * If source and dest are the same, do nothing.
855 */
856 if (tvp == fvp) {
857 error = 0;
858 goto abortit;
859 }
860
861 /*
862 * XXX: This can deadlock since we hold tdvp/tvp locked.
863 * But I'm not going to fix it now.
864 */
865 if ((error = vn_lock(fvp, LK_EXCLUSIVE)) != 0)
866 goto abortit;
867 dp = VTODE(fdvp);
868 ip = VTODE(fvp);
869
870 /*
871 * Be sure we are not renaming ".", "..", or an alias of ".". This
872 * leads to a crippled directory tree. It's pretty tough to do a
873 * "ls" or "pwd" with the "." directory entry missing, and "cd .."
874 * doesn't work if the ".." entry is missing.
875 */
876 if (ip->de_Attributes & ATTR_DIRECTORY) {
877 /*
878 * Avoid ".", "..", and aliases of "." for obvious reasons.
879 */
880 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
881 dp == ip ||
882 (fcnp->cn_flags & ISDOTDOT) ||
883 (tcnp->cn_flags & ISDOTDOT) ||
884 (ip->de_flag & DE_RENAME)) {
885 VOP_UNLOCK(fvp);
886 error = EINVAL;
887 goto abortit;
888 }
889 ip->de_flag |= DE_RENAME;
890 doingdirectory++;
891 }
892 VN_KNOTE(fdvp, NOTE_WRITE); /* XXXLUKEM/XXX: right place? */
893
894 /*
895 * When the target exists, both the directory
896 * and target vnodes are returned locked.
897 */
898 dp = VTODE(tdvp);
899 xp = tvp ? VTODE(tvp) : NULL;
900 /*
901 * Remember direntry place to use for destination
902 */
903 to_diroffset = dp->de_fndoffset;
904 to_count = dp->de_fndcnt;
905
906 /*
907 * If ".." must be changed (ie the directory gets a new
908 * parent) then the source directory must not be in the
909 * directory hierarchy above the target, as this would
910 * orphan everything below the source directory. Also
911 * the user must have write permission in the source so
912 * as to be able to change "..". We must repeat the call
913 * to namei, as the parent directory is unlocked by the
914 * call to doscheckpath().
915 */
916 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred);
917 VOP_UNLOCK(fvp);
918 if (VTODE(fdvp)->de_StartCluster != VTODE(tdvp)->de_StartCluster)
919 newparent = 1;
920
921 if (doingdirectory && newparent) {
922 if (error) /* write access check above */
923 goto tdvpbad;
924 if (xp != NULL)
925 vput(tvp);
926 tvp = NULL;
927 /*
928 * doscheckpath() vput()'s tdvp (dp == VTODE(tdvp)),
929 * so we have to get an extra ref to it first, and
930 * because it's been unlocked we need to do a relookup
931 * afterwards in case tvp has changed.
932 */
933 vref(tdvp);
934 if ((error = doscheckpath(ip, dp)) != 0)
935 goto bad;
936 vn_lock(tdvp, LK_EXCLUSIVE | LK_RETRY);
937 if ((error = relookup(tdvp, &tvp, tcnp, 0)) != 0) {
938 VOP_UNLOCK(tdvp);
939 goto bad;
940 }
941 dp = VTODE(tdvp);
942 xp = tvp ? VTODE(tvp) : NULL;
943 }
944
945 if (xp != NULL) {
946 /*
947 * Target must be empty if a directory and have no links
948 * to it. Also, ensure source and target are compatible
949 * (both directories, or both not directories).
950 */
951 if (xp->de_Attributes & ATTR_DIRECTORY) {
952 if (!dosdirempty(xp)) {
953 error = ENOTEMPTY;
954 goto tdvpbad;
955 }
956 if (!doingdirectory) {
957 error = ENOTDIR;
958 goto tdvpbad;
959 }
960 } else if (doingdirectory) {
961 error = EISDIR;
962 goto tdvpbad;
963 }
964 if ((error = removede(dp, xp)) != 0)
965 goto tdvpbad;
966 VN_KNOTE(tdvp, NOTE_WRITE);
967 VN_KNOTE(tvp, NOTE_DELETE);
968 cache_purge(tvp);
969 vput(tvp);
970 tvp = NULL;
971 xp = NULL;
972 }
973
974 /*
975 * Convert the filename in tcnp into a dos filename. We copy this
976 * into the denode and directory entry for the destination
977 * file/directory.
978 */
979 if ((error = uniqdosname(VTODE(tdvp), tcnp, toname)) != 0) {
980 goto abortit;
981 }
982
983 /*
984 * Since from wasn't locked at various places above,
985 * have to do a relookup here.
986 */
987 fcnp->cn_flags &= ~MODMASK;
988 fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
989 VOP_UNLOCK(tdvp);
990 vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY);
991 if ((error = relookup(fdvp, &fvp, fcnp, 0))) {
992 VOP_UNLOCK(fdvp);
993 vrele(ap->a_fvp);
994 vrele(tdvp);
995 return (error);
996 }
997 if (fvp == NULL) {
998 /*
999 * From name has disappeared.
1000 */
1001 if (doingdirectory)
1002 panic("rename: lost dir entry");
1003 vput(fdvp);
1004 vrele(ap->a_fvp);
1005 vrele(tdvp);
1006 return 0;
1007 }
1008 VOP_UNLOCK(fdvp);
1009 xp = VTODE(fvp);
1010 zp = VTODE(fdvp);
1011 from_diroffset = zp->de_fndoffset;
1012
1013 /*
1014 * Ensure that the directory entry still exists and has not
1015 * changed till now. If the source is a file the entry may
1016 * have been unlinked or renamed. In either case there is
1017 * no further work to be done. If the source is a directory
1018 * then it cannot have been rmdir'ed or renamed; this is
1019 * prohibited by the DE_RENAME flag.
1020 */
1021 if (xp != ip) {
1022 if (doingdirectory)
1023 panic("rename: lost dir entry");
1024 vrele(ap->a_fvp);
1025 xp = NULL;
1026 } else {
1027 vrele(fvp);
1028 xp = NULL;
1029
1030 /*
1031 * First write a new entry in the destination
1032 * directory and mark the entry in the source directory
1033 * as deleted. Then move the denode to the correct hash
1034 * chain for its new location in the filesystem. And, if
1035 * we moved a directory, then update its .. entry to point
1036 * to the new parent directory.
1037 */
1038 memcpy(oldname, ip->de_Name, 11);
1039 memcpy(ip->de_Name, toname, 11); /* update denode */
1040 dp->de_fndoffset = to_diroffset;
1041 dp->de_fndcnt = to_count;
1042 error = createde(ip, dp, (struct denode **)0, tcnp);
1043 if (error) {
1044 memcpy(ip->de_Name, oldname, 11);
1045 VOP_UNLOCK(fvp);
1046 goto bad;
1047 }
1048 ip->de_refcnt++;
1049 zp->de_fndoffset = from_diroffset;
1050 if ((error = removede(zp, ip)) != 0) {
1051 /* XXX should really panic here, fs is corrupt */
1052 VOP_UNLOCK(fvp);
1053 goto bad;
1054 }
1055 cache_purge(fvp);
1056 if (!doingdirectory) {
1057 struct denode_key old_key = ip->de_key;
1058 struct denode_key new_key = ip->de_key;
1059
1060 error = pcbmap(dp, de_cluster(pmp, to_diroffset), 0,
1061 &new_key.dk_dirclust, 0);
1062 if (error) {
1063 /* XXX should really panic here, fs is corrupt */
1064 VOP_UNLOCK(fvp);
1065 goto bad;
1066 }
1067 new_key.dk_diroffset = to_diroffset;
1068 if (new_key.dk_dirclust != MSDOSFSROOT)
1069 new_key.dk_diroffset &= pmp->pm_crbomask;
1070 vcache_rekey_enter(pmp->pm_mountp, fvp, &old_key,
1071 sizeof(old_key), &new_key, sizeof(new_key));
1072 ip->de_key = new_key;
1073 vcache_rekey_exit(pmp->pm_mountp, fvp, &old_key,
1074 sizeof(old_key), &ip->de_key, sizeof(ip->de_key));
1075 }
1076 }
1077
1078 /*
1079 * If we moved a directory to a new parent directory, then we must
1080 * fixup the ".." entry in the moved directory.
1081 */
1082 if (doingdirectory && newparent) {
1083 cn = ip->de_StartCluster;
1084 if (cn == MSDOSFSROOT) {
1085 /* this should never happen */
1086 panic("msdosfs_rename: updating .. in root directory?");
1087 } else
1088 bn = cntobn(pmp, cn);
1089 error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn),
1090 pmp->pm_bpcluster, B_MODIFY, &bp);
1091 if (error) {
1092 /* XXX should really panic here, fs is corrupt */
1093 VOP_UNLOCK(fvp);
1094 goto bad;
1095 }
1096 dotdotp = (struct direntry *)bp->b_data + 1;
1097 putushort(dotdotp->deStartCluster, dp->de_StartCluster);
1098 if (FAT32(pmp)) {
1099 putushort(dotdotp->deHighClust,
1100 dp->de_StartCluster >> 16);
1101 } else {
1102 putushort(dotdotp->deHighClust, 0);
1103 }
1104 if ((error = bwrite(bp)) != 0) {
1105 /* XXX should really panic here, fs is corrupt */
1106 VOP_UNLOCK(fvp);
1107 goto bad;
1108 }
1109 }
1110
1111 VN_KNOTE(fvp, NOTE_RENAME);
1112 VOP_UNLOCK(fvp);
1113 bad:
1114 if (tvp)
1115 vput(tvp);
1116 vrele(tdvp);
1117 ip->de_flag &= ~DE_RENAME;
1118 vrele(fdvp);
1119 vrele(fvp);
1120 return (error);
1121
1122 /* XXX: uuuh */
1123 tdvpbad:
1124 VOP_UNLOCK(tdvp);
1125 goto bad;
1126 }
1127
1128 static const struct {
1129 struct direntry dot;
1130 struct direntry dotdot;
1131 } dosdirtemplate = {
1132 { ". ", " ", /* the . entry */
1133 ATTR_DIRECTORY, /* file attribute */
1134 0, /* reserved */
1135 0, { 0, 0 }, { 0, 0 }, /* create time & date */
1136 { 0, 0 }, /* access date */
1137 { 0, 0 }, /* high bits of start cluster */
1138 { 210, 4 }, { 210, 4 }, /* modify time & date */
1139 { 0, 0 }, /* startcluster */
1140 { 0, 0, 0, 0 } /* filesize */
1141 },
1142 { ".. ", " ", /* the .. entry */
1143 ATTR_DIRECTORY, /* file attribute */
1144 0, /* reserved */
1145 0, { 0, 0 }, { 0, 0 }, /* create time & date */
1146 { 0, 0 }, /* access date */
1147 { 0, 0 }, /* high bits of start cluster */
1148 { 210, 4 }, { 210, 4 }, /* modify time & date */
1149 { 0, 0 }, /* startcluster */
1150 { 0, 0, 0, 0 } /* filesize */
1151 }
1152 };
1153
1154 int
1155 msdosfs_mkdir(void *v)
1156 {
1157 struct vop_mkdir_v3_args /* {
1158 struct vnode *a_dvp;
1159 struvt vnode **a_vpp;
1160 struvt componentname *a_cnp;
1161 struct vattr *a_vap;
1162 } */ *ap = v;
1163 struct componentname *cnp = ap->a_cnp;
1164 struct denode ndirent;
1165 struct denode *dep;
1166 struct denode *pdep = VTODE(ap->a_dvp);
1167 int error;
1168 int bn;
1169 u_long newcluster, pcl;
1170 daddr_t lbn;
1171 struct direntry *denp;
1172 struct msdosfsmount *pmp = pdep->de_pmp;
1173 struct buf *bp;
1174 int async = pdep->de_pmp->pm_mountp->mnt_flag & MNT_ASYNC;
1175
1176 /*
1177 * If this is the root directory and there is no space left we
1178 * can't do anything. This is because the root directory can not
1179 * change size.
1180 */
1181 if (pdep->de_StartCluster == MSDOSFSROOT
1182 && pdep->de_fndoffset >= pdep->de_FileSize) {
1183 error = ENOSPC;
1184 goto bad2;
1185 }
1186
1187 /*
1188 * Allocate a cluster to hold the about to be created directory.
1189 */
1190 error = clusteralloc(pmp, 0, 1, &newcluster, NULL);
1191 if (error)
1192 goto bad2;
1193
1194 memset(&ndirent, 0, sizeof(ndirent));
1195 ndirent.de_pmp = pmp;
1196 ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
1197 DETIMES(&ndirent, NULL, NULL, NULL, pmp->pm_gmtoff);
1198
1199 /*
1200 * Now fill the cluster with the "." and ".." entries. And write
1201 * the cluster to disk. This way it is there for the parent
1202 * directory to be pointing at if there were a crash.
1203 */
1204 bn = cntobn(pmp, newcluster);
1205 lbn = de_bn2kb(pmp, bn);
1206 /* always succeeds */
1207 bp = getblk(pmp->pm_devvp, lbn, pmp->pm_bpcluster, 0, 0);
1208 memset(bp->b_data, 0, pmp->pm_bpcluster);
1209 memcpy(bp->b_data, &dosdirtemplate, sizeof dosdirtemplate);
1210 denp = (struct direntry *)bp->b_data;
1211 putushort(denp[0].deStartCluster, newcluster);
1212 putushort(denp[0].deCDate, ndirent.de_CDate);
1213 putushort(denp[0].deCTime, ndirent.de_CTime);
1214 denp[0].deCHundredth = ndirent.de_CHun;
1215 putushort(denp[0].deADate, ndirent.de_ADate);
1216 putushort(denp[0].deMDate, ndirent.de_MDate);
1217 putushort(denp[0].deMTime, ndirent.de_MTime);
1218 pcl = pdep->de_StartCluster;
1219 if (FAT32(pmp) && pcl == pmp->pm_rootdirblk)
1220 pcl = 0;
1221 putushort(denp[1].deStartCluster, pcl);
1222 putushort(denp[1].deCDate, ndirent.de_CDate);
1223 putushort(denp[1].deCTime, ndirent.de_CTime);
1224 denp[1].deCHundredth = ndirent.de_CHun;
1225 putushort(denp[1].deADate, ndirent.de_ADate);
1226 putushort(denp[1].deMDate, ndirent.de_MDate);
1227 putushort(denp[1].deMTime, ndirent.de_MTime);
1228 if (FAT32(pmp)) {
1229 putushort(denp[0].deHighClust, newcluster >> 16);
1230 putushort(denp[1].deHighClust, pdep->de_StartCluster >> 16);
1231 } else {
1232 putushort(denp[0].deHighClust, 0);
1233 putushort(denp[1].deHighClust, 0);
1234 }
1235
1236 if (async)
1237 bdwrite(bp);
1238 else if ((error = bwrite(bp)) != 0)
1239 goto bad;
1240
1241 /*
1242 * Now build up a directory entry pointing to the newly allocated
1243 * cluster. This will be written to an empty slot in the parent
1244 * directory.
1245 */
1246 if ((error = uniqdosname(pdep, cnp, ndirent.de_Name)) != 0)
1247 goto bad;
1248
1249 ndirent.de_Attributes = ATTR_DIRECTORY;
1250 ndirent.de_StartCluster = newcluster;
1251 ndirent.de_FileSize = 0;
1252 ndirent.de_dev = pdep->de_dev;
1253 ndirent.de_devvp = pdep->de_devvp;
1254 if ((error = createde(&ndirent, pdep, &dep, cnp)) != 0)
1255 goto bad;
1256 VN_KNOTE(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
1257 *ap->a_vpp = DETOV(dep);
1258 return (0);
1259
1260 bad:
1261 clusterfree(pmp, newcluster, NULL);
1262 bad2:
1263 return (error);
1264 }
1265
1266 int
1267 msdosfs_rmdir(void *v)
1268 {
1269 struct vop_rmdir_v2_args /* {
1270 struct vnode *a_dvp;
1271 struct vnode *a_vp;
1272 struct componentname *a_cnp;
1273 } */ *ap = v;
1274 struct vnode *vp = ap->a_vp;
1275 struct vnode *dvp = ap->a_dvp;
1276 struct componentname *cnp = ap->a_cnp;
1277 struct denode *ip, *dp;
1278 int error;
1279
1280 ip = VTODE(vp);
1281 dp = VTODE(dvp);
1282 /*
1283 * No rmdir "." please.
1284 */
1285 if (dp == ip) {
1286 vrele(vp);
1287 return (EINVAL);
1288 }
1289 /*
1290 * Verify the directory is empty (and valid).
1291 * (Rmdir ".." won't be valid since
1292 * ".." will contain a reference to
1293 * the current directory and thus be
1294 * non-empty.)
1295 */
1296 error = 0;
1297 if (!dosdirempty(ip) || ip->de_flag & DE_RENAME) {
1298 error = ENOTEMPTY;
1299 goto out;
1300 }
1301 /*
1302 * Delete the entry from the directory. For dos filesystems this
1303 * gets rid of the directory entry on disk, the in memory copy
1304 * still exists but the de_refcnt is <= 0. This prevents it from
1305 * being found by deget(). When the vput() on dep is done we give
1306 * up access and eventually msdosfs_reclaim() will be called which
1307 * will remove it from the denode cache.
1308 */
1309 if ((error = removede(dp, ip)) != 0)
1310 goto out;
1311 /*
1312 * This is where we decrement the link count in the parent
1313 * directory. Since dos filesystems don't do this we just purge
1314 * the name cache and let go of the parent directory denode.
1315 */
1316 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
1317 cache_purge(dvp);
1318 /*
1319 * Truncate the directory that is being deleted.
1320 */
1321 error = detrunc(ip, (u_long)0, IO_SYNC, cnp->cn_cred);
1322 cache_purge(vp);
1323 out:
1324 VN_KNOTE(vp, NOTE_DELETE);
1325 vput(vp);
1326 return (error);
1327 }
1328
1329 int
1330 msdosfs_readdir(void *v)
1331 {
1332 struct vop_readdir_args /* {
1333 struct vnode *a_vp;
1334 struct uio *a_uio;
1335 kauth_cred_t a_cred;
1336 int *a_eofflag;
1337 off_t **a_cookies;
1338 int *a_ncookies;
1339 } */ *ap = v;
1340 int error = 0;
1341 int diff;
1342 long n;
1343 int blsize;
1344 long on;
1345 long lost;
1346 long count;
1347 u_long cn;
1348 ino_t fileno;
1349 u_long dirsperblk;
1350 long bias = 0;
1351 daddr_t bn, lbn;
1352 struct buf *bp;
1353 struct denode *dep = VTODE(ap->a_vp);
1354 struct msdosfsmount *pmp = dep->de_pmp;
1355 struct direntry *dentp;
1356 struct dirent *dirbuf;
1357 struct uio *uio = ap->a_uio;
1358 off_t *cookies = NULL;
1359 int ncookies = 0, nc = 0;
1360 off_t offset, uio_off;
1361 int chksum = -1;
1362 uint16_t namlen;
1363
1364 #ifdef MSDOSFS_DEBUG
1365 printf("msdosfs_readdir(): vp %p, uio %p, cred %p, eofflagp %p\n",
1366 ap->a_vp, uio, ap->a_cred, ap->a_eofflag);
1367 #endif
1368
1369 /*
1370 * msdosfs_readdir() won't operate properly on regular files since
1371 * it does i/o only with the filesystem vnode, and hence can
1372 * retrieve the wrong block from the buffer cache for a plain file.
1373 * So, fail attempts to readdir() on a plain file.
1374 */
1375 if ((dep->de_Attributes & ATTR_DIRECTORY) == 0)
1376 return (ENOTDIR);
1377
1378 /*
1379 * If the user buffer is smaller than the size of one dos directory
1380 * entry or the file offset is not a multiple of the size of a
1381 * directory entry, then we fail the read.
1382 */
1383 count = uio->uio_resid & ~(sizeof(struct direntry) - 1);
1384 offset = uio->uio_offset;
1385 if (count < sizeof(struct direntry) ||
1386 (offset & (sizeof(struct direntry) - 1)))
1387 return (EINVAL);
1388 lost = uio->uio_resid - count;
1389 uio->uio_resid = count;
1390 uio_off = uio->uio_offset;
1391
1392
1393 /* Allocate a temporary dirent buffer. */
1394 dirbuf = malloc(sizeof(struct dirent), M_MSDOSFSTMP, M_WAITOK | M_ZERO);
1395
1396 if (ap->a_ncookies) {
1397 nc = uio->uio_resid / _DIRENT_MINSIZE((struct dirent *)0);
1398 cookies = malloc(nc * sizeof (off_t), M_TEMP, M_WAITOK);
1399 *ap->a_cookies = cookies;
1400 }
1401
1402 dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
1403
1404 /*
1405 * If they are reading from the root directory then, we simulate
1406 * the . and .. entries since these don't exist in the root
1407 * directory. We also set the offset bias to make up for having to
1408 * simulate these entries. By this I mean that at file offset 64 we
1409 * read the first entry in the root directory that lives on disk.
1410 */
1411 if (dep->de_StartCluster == MSDOSFSROOT
1412 || (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk)) {
1413 #if 0
1414 printf("msdosfs_readdir(): going after . or .. in root dir, "
1415 "offset %" PRIu64 "\n", offset);
1416 #endif
1417 bias = 2 * sizeof(struct direntry);
1418 if (offset < bias) {
1419 for (n = (int)offset / sizeof(struct direntry);
1420 n < 2; n++) {
1421 if (FAT32(pmp))
1422 dirbuf->d_fileno = cntobn(pmp,
1423 (ino_t)pmp->pm_rootdirblk)
1424 * dirsperblk;
1425 else
1426 dirbuf->d_fileno = 1;
1427 dirbuf->d_type = DT_DIR;
1428 switch (n) {
1429 case 0:
1430 dirbuf->d_namlen = 1;
1431 strlcpy(dirbuf->d_name, ".",
1432 sizeof(dirbuf->d_name));
1433 break;
1434 case 1:
1435 dirbuf->d_namlen = 2;
1436 strlcpy(dirbuf->d_name, "..",
1437 sizeof(dirbuf->d_name));
1438 break;
1439 }
1440 dirbuf->d_reclen = _DIRENT_SIZE(dirbuf);
1441 if (uio->uio_resid < dirbuf->d_reclen)
1442 goto out;
1443 error = uiomove(dirbuf, dirbuf->d_reclen, uio);
1444 if (error)
1445 goto out;
1446 offset += sizeof(struct direntry);
1447 uio_off = offset;
1448 if (cookies) {
1449 *cookies++ = offset;
1450 ncookies++;
1451 if (ncookies >= nc)
1452 goto out;
1453 }
1454 }
1455 }
1456 }
1457
1458 while (uio->uio_resid > 0) {
1459 lbn = de_cluster(pmp, offset - bias);
1460 on = (offset - bias) & pmp->pm_crbomask;
1461 n = MIN(pmp->pm_bpcluster - on, uio->uio_resid);
1462 diff = dep->de_FileSize - (offset - bias);
1463 if (diff <= 0)
1464 break;
1465 n = MIN(n, diff);
1466 if ((error = pcbmap(dep, lbn, &bn, &cn, &blsize)) != 0)
1467 break;
1468 error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize,
1469 0, &bp);
1470 if (error) {
1471 goto bad;
1472 }
1473 n = MIN(n, blsize - bp->b_resid);
1474
1475 /*
1476 * Convert from dos directory entries to fs-independent
1477 * directory entries.
1478 */
1479 for (dentp = (struct direntry *)((char *)bp->b_data + on);
1480 (char *)dentp < (char *)bp->b_data + on + n;
1481 dentp++, offset += sizeof(struct direntry)) {
1482 #if 0
1483
1484 printf("rd: dentp %08x prev %08x crnt %08x deName %02x attr %02x\n",
1485 dentp, prev, crnt, dentp->deName[0], dentp->deAttributes);
1486 #endif
1487 /*
1488 * If this is an unused entry, we can stop.
1489 */
1490 if (dentp->deName[0] == SLOT_EMPTY) {
1491 brelse(bp, 0);
1492 goto out;
1493 }
1494 /*
1495 * Skip deleted entries.
1496 */
1497 if (dentp->deName[0] == SLOT_DELETED) {
1498 chksum = -1;
1499 continue;
1500 }
1501
1502 /*
1503 * Handle Win95 long directory entries
1504 */
1505 if (dentp->deAttributes == ATTR_WIN95) {
1506 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
1507 continue;
1508 chksum = win2unixfn((struct winentry *)dentp,
1509 dirbuf, chksum, &namlen,
1510 pmp->pm_flags & MSDOSFSMNT_UTF8);
1511 if (chksum != -1)
1512 dirbuf->d_namlen = namlen;
1513 continue;
1514 }
1515
1516 /*
1517 * Skip volume labels
1518 */
1519 if (dentp->deAttributes & ATTR_VOLUME) {
1520 chksum = -1;
1521 continue;
1522 }
1523 /*
1524 * This computation of d_fileno must match
1525 * the computation of va_fileid in
1526 * msdosfs_getattr.
1527 */
1528 if (dentp->deAttributes & ATTR_DIRECTORY) {
1529 fileno = getushort(dentp->deStartCluster);
1530 if (FAT32(pmp))
1531 fileno |= ((ino_t)getushort(dentp->deHighClust)) << 16;
1532 /* if this is the root directory */
1533 if (fileno == MSDOSFSROOT)
1534 if (FAT32(pmp))
1535 fileno = cntobn(pmp,
1536 (ino_t)pmp->pm_rootdirblk)
1537 * dirsperblk;
1538 else
1539 fileno = 1;
1540 else
1541 fileno = cntobn(pmp, fileno) * dirsperblk;
1542 dirbuf->d_fileno = fileno;
1543 dirbuf->d_type = DT_DIR;
1544 } else {
1545 dirbuf->d_fileno =
1546 offset / sizeof(struct direntry);
1547 dirbuf->d_type = DT_REG;
1548 }
1549 if (chksum != winChksum(dentp->deName))
1550 dirbuf->d_namlen = dos2unixfn(dentp->deName,
1551 (u_char *)dirbuf->d_name,
1552 pmp->pm_flags & MSDOSFSMNT_SHORTNAME);
1553 else
1554 dirbuf->d_name[dirbuf->d_namlen] = 0;
1555 namlen = dirbuf->d_namlen;
1556 chksum = -1;
1557 dirbuf->d_reclen = _DIRENT_SIZE(dirbuf);
1558 if (uio->uio_resid < dirbuf->d_reclen) {
1559 brelse(bp, 0);
1560 goto out;
1561 }
1562 error = uiomove(dirbuf, dirbuf->d_reclen, uio);
1563 if (error) {
1564 brelse(bp, 0);
1565 goto out;
1566 }
1567 uio_off = offset + sizeof(struct direntry);
1568 if (cookies) {
1569 *cookies++ = offset + sizeof(struct direntry);
1570 ncookies++;
1571 if (ncookies >= nc) {
1572 brelse(bp, 0);
1573 goto out;
1574 }
1575 }
1576 }
1577 brelse(bp, 0);
1578 }
1579
1580 out:
1581 uio->uio_offset = uio_off;
1582 uio->uio_resid += lost;
1583 if (dep->de_FileSize - (offset - bias) <= 0)
1584 *ap->a_eofflag = 1;
1585 else
1586 *ap->a_eofflag = 0;
1587
1588 if (ap->a_ncookies) {
1589 if (error) {
1590 free(*ap->a_cookies, M_TEMP);
1591 *ap->a_ncookies = 0;
1592 *ap->a_cookies = NULL;
1593 } else
1594 *ap->a_ncookies = ncookies;
1595 }
1596
1597 bad:
1598 free(dirbuf, M_MSDOSFSTMP);
1599 return (error);
1600 }
1601
1602 /*
1603 * vp - address of vnode file the file
1604 * bn - which cluster we are interested in mapping to a filesystem block number.
1605 * vpp - returns the vnode for the block special file holding the filesystem
1606 * containing the file of interest
1607 * bnp - address of where to return the filesystem relative block number
1608 */
1609 int
1610 msdosfs_bmap(void *v)
1611 {
1612 struct vop_bmap_args /* {
1613 struct vnode *a_vp;
1614 daddr_t a_bn;
1615 struct vnode **a_vpp;
1616 daddr_t *a_bnp;
1617 int *a_runp;
1618 } */ *ap = v;
1619 struct denode *dep = VTODE(ap->a_vp);
1620 int run, maxrun;
1621 daddr_t runbn;
1622 int status;
1623
1624 if (ap->a_vpp != NULL)
1625 *ap->a_vpp = dep->de_devvp;
1626 if (ap->a_bnp == NULL)
1627 return (0);
1628 status = pcbmap(dep, ap->a_bn, ap->a_bnp, 0, 0);
1629
1630 /*
1631 * From FreeBSD:
1632 * A little kludgy, but we loop calling pcbmap until we
1633 * reach the end of the contiguous piece, or reach MAXPHYS.
1634 * Since it reduces disk I/Os, the "wasted" CPU is put to
1635 * good use (4 to 5 fold sequential read I/O improvement on USB
1636 * drives).
1637 */
1638 if (ap->a_runp != NULL) {
1639 /* taken from ufs_bmap */
1640 maxrun = ulmin(MAXPHYS / dep->de_pmp->pm_bpcluster - 1,
1641 dep->de_pmp->pm_maxcluster - ap->a_bn);
1642 for (run = 1; run <= maxrun; run++) {
1643 if (pcbmap(dep, ap->a_bn + run, &runbn, NULL, NULL)
1644 != 0 || runbn !=
1645 *ap->a_bnp + de_cn2bn(dep->de_pmp, run))
1646 break;
1647 }
1648 *ap->a_runp = run - 1;
1649 }
1650
1651 /*
1652 * We need to scale *ap->a_bnp by sector_size/DEV_BSIZE
1653 */
1654 *ap->a_bnp = de_bn2kb(dep->de_pmp, *ap->a_bnp);
1655 return status;
1656 }
1657
1658 int
1659 msdosfs_strategy(void *v)
1660 {
1661 struct vop_strategy_args /* {
1662 struct vnode *a_vp;
1663 struct buf *a_bp;
1664 } */ *ap = v;
1665 struct vnode *vp = ap->a_vp;
1666 struct buf *bp = ap->a_bp;
1667 struct denode *dep = VTODE(bp->b_vp);
1668 int error = 0;
1669
1670 if (vp->v_type == VBLK || vp->v_type == VCHR)
1671 panic("msdosfs_strategy: spec");
1672 /*
1673 * If we don't already know the filesystem relative block number
1674 * then get it using pcbmap(). If pcbmap() returns the block
1675 * number as -1 then we've got a hole in the file. DOS filesystems
1676 * don't allow files with holes, so we shouldn't ever see this.
1677 */
1678 if (bp->b_blkno == bp->b_lblkno) {
1679 error = pcbmap(dep, de_bn2cn(dep->de_pmp, bp->b_lblkno),
1680 &bp->b_blkno, 0, 0);
1681 if (error)
1682 bp->b_blkno = -1;
1683 if (bp->b_blkno == -1)
1684 clrbuf(bp);
1685 else
1686 bp->b_blkno = de_bn2kb(dep->de_pmp, bp->b_blkno);
1687 }
1688 if (bp->b_blkno == -1) {
1689 biodone(bp);
1690 return (error);
1691 }
1692
1693 /*
1694 * Read/write the block from/to the disk that contains the desired
1695 * file block.
1696 */
1697
1698 vp = dep->de_devvp;
1699 return (VOP_STRATEGY(vp, bp));
1700 }
1701
1702 int
1703 msdosfs_print(void *v)
1704 {
1705 struct vop_print_args /* {
1706 struct vnode *vp;
1707 } */ *ap = v;
1708 struct denode *dep = VTODE(ap->a_vp);
1709
1710 printf(
1711 "tag VT_MSDOSFS, startcluster %ld, dircluster %ld, diroffset %ld ",
1712 dep->de_StartCluster, dep->de_dirclust, dep->de_diroffset);
1713 printf(" dev %llu, %llu ", (unsigned long long)major(dep->de_dev),
1714 (unsigned long long)minor(dep->de_dev));
1715 printf("\n");
1716 return (0);
1717 }
1718
1719 int
1720 msdosfs_advlock(void *v)
1721 {
1722 struct vop_advlock_args /* {
1723 struct vnode *a_vp;
1724 void *a_id;
1725 int a_op;
1726 struct flock *a_fl;
1727 int a_flags;
1728 } */ *ap = v;
1729 struct denode *dep = VTODE(ap->a_vp);
1730
1731 return lf_advlock(ap, &dep->de_lockf, dep->de_FileSize);
1732 }
1733
1734 int
1735 msdosfs_pathconf(void *v)
1736 {
1737 struct vop_pathconf_args /* {
1738 struct vnode *a_vp;
1739 int a_name;
1740 register_t *a_retval;
1741 } */ *ap = v;
1742
1743 switch (ap->a_name) {
1744 case _PC_LINK_MAX:
1745 *ap->a_retval = 1;
1746 return (0);
1747 case _PC_NAME_MAX:
1748 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_namemax;
1749 return (0);
1750 case _PC_PATH_MAX:
1751 *ap->a_retval = PATH_MAX;
1752 return (0);
1753 case _PC_CHOWN_RESTRICTED:
1754 *ap->a_retval = 1;
1755 return (0);
1756 case _PC_NO_TRUNC:
1757 *ap->a_retval = 1;
1758 return (0);
1759 case _PC_SYNC_IO:
1760 *ap->a_retval = 1;
1761 return (0);
1762 case _PC_FILESIZEBITS:
1763 *ap->a_retval = 32;
1764 return (0);
1765 default:
1766 return (EINVAL);
1767 }
1768 /* NOTREACHED */
1769 }
1770
1771 int
1772 msdosfs_fsync(void *v)
1773 {
1774 struct vop_fsync_args /* {
1775 struct vnode *a_vp;
1776 kauth_cred_t a_cred;
1777 int a_flags;
1778 off_t offlo;
1779 off_t offhi;
1780 } */ *ap = v;
1781 struct vnode *vp = ap->a_vp;
1782 int wait;
1783 int error;
1784
1785 wait = (ap->a_flags & FSYNC_WAIT) != 0;
1786 error = vflushbuf(vp, ap->a_flags);
1787 if (error == 0 && (ap->a_flags & FSYNC_DATAONLY) == 0)
1788 error = msdosfs_update(vp, NULL, NULL, wait ? UPDATE_WAIT : 0);
1789
1790 if (error == 0 && ap->a_flags & FSYNC_CACHE) {
1791 struct denode *dep = VTODE(vp);
1792 struct vnode *devvp = dep->de_devvp;
1793
1794 int l = 0;
1795 error = VOP_IOCTL(devvp, DIOCCACHESYNC, &l, FWRITE,
1796 curlwp->l_cred);
1797 }
1798
1799 return (error);
1800 }
1801
1802 void
1803 msdosfs_detimes(struct denode *dep, const struct timespec *acc,
1804 const struct timespec *mod, const struct timespec *cre, int gmtoff)
1805 {
1806 struct timespec *ts = NULL, tsb;
1807
1808 KASSERT(dep->de_flag & (DE_UPDATE | DE_CREATE | DE_ACCESS));
1809 /* XXX just call getnanotime early and use result if needed? */
1810 dep->de_flag |= DE_MODIFIED;
1811 if (dep->de_flag & DE_UPDATE) {
1812 if (mod == NULL) {
1813 getnanotime(&tsb);
1814 mod = ts = &tsb;
1815 }
1816 unix2dostime(mod, gmtoff, &dep->de_MDate, &dep->de_MTime, NULL);
1817 dep->de_Attributes |= ATTR_ARCHIVE;
1818 }
1819 if ((dep->de_pmp->pm_flags & MSDOSFSMNT_NOWIN95) == 0) {
1820 if (dep->de_flag & DE_ACCESS) {
1821 if (acc == NULL)
1822 acc = ts == NULL ?
1823 (getnanotime(&tsb), ts = &tsb) : ts;
1824 unix2dostime(acc, gmtoff, &dep->de_ADate, NULL, NULL);
1825 }
1826 if (dep->de_flag & DE_CREATE) {
1827 if (cre == NULL)
1828 cre = ts == NULL ?
1829 (getnanotime(&tsb), ts = &tsb) : ts;
1830 unix2dostime(cre, gmtoff, &dep->de_CDate,
1831 &dep->de_CTime, &dep->de_CHun);
1832 }
1833 }
1834
1835 dep->de_flag &= ~(DE_UPDATE | DE_CREATE | DE_ACCESS);
1836 }
1837
1838 /* Global vfs data structures for msdosfs */
1839 int (**msdosfs_vnodeop_p)(void *);
1840 const struct vnodeopv_entry_desc msdosfs_vnodeop_entries[] = {
1841 { &vop_default_desc, vn_default_error },
1842 { &vop_lookup_desc, msdosfs_lookup }, /* lookup */
1843 { &vop_create_desc, msdosfs_create }, /* create */
1844 { &vop_mknod_desc, genfs_eopnotsupp }, /* mknod */
1845 { &vop_open_desc, genfs_nullop }, /* open */
1846 { &vop_close_desc, msdosfs_close }, /* close */
1847 { &vop_access_desc, msdosfs_access }, /* access */
1848 { &vop_getattr_desc, msdosfs_getattr }, /* getattr */
1849 { &vop_setattr_desc, msdosfs_setattr }, /* setattr */
1850 { &vop_read_desc, msdosfs_read }, /* read */
1851 { &vop_write_desc, msdosfs_write }, /* write */
1852 { &vop_fallocate_desc, genfs_eopnotsupp }, /* fallocate */
1853 { &vop_fdiscard_desc, genfs_eopnotsupp }, /* fdiscard */
1854 { &vop_fcntl_desc, genfs_fcntl }, /* fcntl */
1855 { &vop_ioctl_desc, msdosfs_ioctl }, /* ioctl */
1856 { &vop_poll_desc, msdosfs_poll }, /* poll */
1857 { &vop_kqfilter_desc, genfs_kqfilter }, /* kqfilter */
1858 { &vop_revoke_desc, msdosfs_revoke }, /* revoke */
1859 { &vop_mmap_desc, msdosfs_mmap }, /* mmap */
1860 { &vop_fsync_desc, msdosfs_fsync }, /* fsync */
1861 { &vop_seek_desc, msdosfs_seek }, /* seek */
1862 { &vop_remove_desc, msdosfs_remove }, /* remove */
1863 { &vop_link_desc, genfs_eopnotsupp }, /* link */
1864 { &vop_rename_desc, msdosfs_rename }, /* rename */
1865 { &vop_mkdir_desc, msdosfs_mkdir }, /* mkdir */
1866 { &vop_rmdir_desc, msdosfs_rmdir }, /* rmdir */
1867 { &vop_symlink_desc, genfs_eopnotsupp }, /* symlink */
1868 { &vop_readdir_desc, msdosfs_readdir }, /* readdir */
1869 { &vop_readlink_desc, genfs_einval }, /* readlink */
1870 { &vop_abortop_desc, msdosfs_abortop }, /* abortop */
1871 { &vop_inactive_desc, msdosfs_inactive }, /* inactive */
1872 { &vop_reclaim_desc, msdosfs_reclaim }, /* reclaim */
1873 { &vop_lock_desc, genfs_lock }, /* lock */
1874 { &vop_unlock_desc, genfs_unlock }, /* unlock */
1875 { &vop_bmap_desc, msdosfs_bmap }, /* bmap */
1876 { &vop_strategy_desc, msdosfs_strategy }, /* strategy */
1877 { &vop_print_desc, msdosfs_print }, /* print */
1878 { &vop_islocked_desc, genfs_islocked }, /* islocked */
1879 { &vop_pathconf_desc, msdosfs_pathconf }, /* pathconf */
1880 { &vop_advlock_desc, msdosfs_advlock }, /* advlock */
1881 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */
1882 { &vop_getpages_desc, genfs_getpages }, /* getpages */
1883 { &vop_putpages_desc, genfs_putpages }, /* putpages */
1884 { NULL, NULL }
1885 };
1886 const struct vnodeopv_desc msdosfs_vnodeop_opv_desc =
1887 { &msdosfs_vnodeop_p, msdosfs_vnodeop_entries };
1888