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