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