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