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