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