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