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