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