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