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