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