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