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