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