1 /* $NetBSD: ntfs_vnops.c,v 1.27 2005/12/11 12:24:29 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * John Heidemann of the UCLA Ficus project. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * Id: ntfs_vnops.c,v 1.5 1999/05/12 09:43:06 semenu Exp 35 * 36 */ 37 38 #include <sys/cdefs.h> 39 __KERNEL_RCSID(0, "$NetBSD: ntfs_vnops.c,v 1.27 2005/12/11 12:24:29 christos Exp $"); 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/kernel.h> 44 #include <sys/time.h> 45 #include <sys/stat.h> 46 #include <sys/vnode.h> 47 #include <sys/mount.h> 48 #include <sys/namei.h> 49 #include <sys/malloc.h> 50 #include <sys/buf.h> 51 #include <sys/dirent.h> 52 53 #if !defined(__NetBSD__) 54 #include <vm/vm.h> 55 #endif 56 57 #if defined(__FreeBSD__) 58 #include <vm/vnode_pager.h> 59 #endif 60 61 #include <sys/sysctl.h> 62 63 64 #include <fs/ntfs/ntfs.h> 65 #include <fs/ntfs/ntfs_inode.h> 66 #include <fs/ntfs/ntfs_subr.h> 67 #include <miscfs/specfs/specdev.h> 68 #include <miscfs/genfs/genfs.h> 69 70 #include <sys/unistd.h> /* for pathconf(2) constants */ 71 72 static int ntfs_bypass(struct vop_generic_args *ap); 73 static int ntfs_read(struct vop_read_args *); 74 static int ntfs_write(struct vop_write_args *ap); 75 static int ntfs_getattr(struct vop_getattr_args *ap); 76 static int ntfs_inactive(struct vop_inactive_args *ap); 77 static int ntfs_print(struct vop_print_args *ap); 78 static int ntfs_reclaim(struct vop_reclaim_args *ap); 79 static int ntfs_strategy(struct vop_strategy_args *ap); 80 static int ntfs_access(struct vop_access_args *ap); 81 static int ntfs_open(struct vop_open_args *ap); 82 static int ntfs_close(struct vop_close_args *ap); 83 static int ntfs_readdir(struct vop_readdir_args *ap); 84 static int ntfs_lookup(struct vop_lookup_args *ap); 85 static int ntfs_bmap(struct vop_bmap_args *ap); 86 #if defined(__FreeBSD__) 87 static int ntfs_getpages(struct vop_getpages_args *ap); 88 static int ntfs_putpages(struct vop_putpages_args *); 89 #endif 90 static int ntfs_fsync(struct vop_fsync_args *ap); 91 static int ntfs_pathconf(void *); 92 93 extern int prtactive; 94 95 #if defined(__FreeBSD__) 96 int 97 ntfs_getpages(ap) 98 struct vop_getpages_args *ap; 99 { 100 return vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count, 101 ap->a_reqpage); 102 } 103 104 int 105 ntfs_putpages(ap) 106 struct vop_putpages_args *ap; 107 { 108 return vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count, 109 ap->a_sync, ap->a_rtvals); 110 } 111 #endif 112 113 /* 114 * This is a noop, simply returning what one has been given. 115 */ 116 int 117 ntfs_bmap(ap) 118 struct vop_bmap_args /* { 119 struct vnode *a_vp; 120 daddr_t a_bn; 121 struct vnode **a_vpp; 122 daddr_t *a_bnp; 123 int *a_runp; 124 int *a_runb; 125 } */ *ap; 126 { 127 dprintf(("ntfs_bmap: vn: %p, blk: %d\n", ap->a_vp,(u_int32_t)ap->a_bn)); 128 if (ap->a_vpp != NULL) 129 *ap->a_vpp = ap->a_vp; 130 if (ap->a_bnp != NULL) 131 *ap->a_bnp = ap->a_bn; 132 if (ap->a_runp != NULL) 133 *ap->a_runp = 0; 134 #if !defined(__NetBSD__) 135 if (ap->a_runb != NULL) 136 *ap->a_runb = 0; 137 #endif 138 return (0); 139 } 140 141 static int 142 ntfs_read(ap) 143 struct vop_read_args /* { 144 struct vnode *a_vp; 145 struct uio *a_uio; 146 int a_ioflag; 147 struct ucred *a_cred; 148 } */ *ap; 149 { 150 struct vnode *vp = ap->a_vp; 151 struct fnode *fp = VTOF(vp); 152 struct ntnode *ip = FTONT(fp); 153 struct uio *uio = ap->a_uio; 154 struct ntfsmount *ntmp = ip->i_mp; 155 u_int64_t toread; 156 int error; 157 158 dprintf(("ntfs_read: ino: %llu, off: %qd resid: %qd, segflg: %d\n", 159 (unsigned long long)ip->i_number, (long long)uio->uio_offset, 160 (long long)uio->uio_resid, uio->uio_segflg)); 161 162 dprintf(("ntfs_read: filesize: %qu",(long long)fp->f_size)); 163 164 /* don't allow reading after end of file */ 165 if (uio->uio_offset > fp->f_size) 166 toread = 0; 167 else 168 toread = MIN(uio->uio_resid, fp->f_size - uio->uio_offset ); 169 170 dprintf((", toread: %qu\n",(long long)toread)); 171 172 if (toread == 0) 173 return (0); 174 175 error = ntfs_readattr(ntmp, ip, fp->f_attrtype, 176 fp->f_attrname, uio->uio_offset, toread, NULL, uio); 177 if (error) { 178 printf("ntfs_read: ntfs_readattr failed: %d\n",error); 179 return (error); 180 } 181 182 return (0); 183 } 184 185 static int 186 ntfs_bypass(ap) 187 struct vop_generic_args /* { 188 struct vnodeop_desc *a_desc; 189 <other random data follows, presumably> 190 } */ *ap; 191 { 192 int error = ENOTTY; 193 dprintf(("ntfs_bypass: %s\n", ap->a_desc->vdesc_name)); 194 return (error); 195 } 196 197 198 static int 199 ntfs_getattr(ap) 200 struct vop_getattr_args /* { 201 struct vnode *a_vp; 202 struct vattr *a_vap; 203 struct ucred *a_cred; 204 struct lwp *a_l; 205 } */ *ap; 206 { 207 struct vnode *vp = ap->a_vp; 208 struct fnode *fp = VTOF(vp); 209 struct ntnode *ip = FTONT(fp); 210 struct vattr *vap = ap->a_vap; 211 212 dprintf(("ntfs_getattr: %llu, flags: %d\n", 213 (unsigned long long)ip->i_number, ip->i_flag)); 214 215 #if defined(__FreeBSD__) 216 vap->va_fsid = dev2udev(ip->i_dev); 217 #else /* NetBSD */ 218 vap->va_fsid = ip->i_dev; 219 #endif 220 vap->va_fileid = ip->i_number; 221 vap->va_mode = ip->i_mp->ntm_mode; 222 vap->va_nlink = ip->i_nlink; 223 vap->va_uid = ip->i_mp->ntm_uid; 224 vap->va_gid = ip->i_mp->ntm_gid; 225 vap->va_rdev = 0; /* XXX UNODEV ? */ 226 vap->va_size = fp->f_size; 227 vap->va_bytes = fp->f_allocated; 228 vap->va_atime = ntfs_nttimetounix(fp->f_times.t_access); 229 vap->va_mtime = ntfs_nttimetounix(fp->f_times.t_write); 230 vap->va_ctime = ntfs_nttimetounix(fp->f_times.t_create); 231 vap->va_flags = ip->i_flag; 232 vap->va_gen = 0; 233 vap->va_blocksize = ip->i_mp->ntm_spc * ip->i_mp->ntm_bps; 234 vap->va_type = vp->v_type; 235 vap->va_filerev = 0; 236 return (0); 237 } 238 239 240 /* 241 * Last reference to an ntnode. If necessary, write or delete it. 242 */ 243 int 244 ntfs_inactive(ap) 245 struct vop_inactive_args /* { 246 struct vnode *a_vp; 247 } */ *ap; 248 { 249 struct vnode *vp = ap->a_vp; 250 #ifdef NTFS_DEBUG 251 struct ntnode *ip = VTONT(vp); 252 #endif 253 254 dprintf(("ntfs_inactive: vnode: %p, ntnode: %llu\n", vp, 255 (unsigned long long)ip->i_number)); 256 257 if (prtactive && vp->v_usecount != 0) 258 vprint("ntfs_inactive: pushing active", vp); 259 260 VOP_UNLOCK(vp, 0); 261 262 /* XXX since we don't support any filesystem changes 263 * right now, nothing more needs to be done 264 */ 265 return (0); 266 } 267 268 /* 269 * Reclaim an fnode/ntnode so that it can be used for other purposes. 270 */ 271 int 272 ntfs_reclaim(ap) 273 struct vop_reclaim_args /* { 274 struct vnode *a_vp; 275 } */ *ap; 276 { 277 struct vnode *vp = ap->a_vp; 278 struct fnode *fp = VTOF(vp); 279 struct ntnode *ip = FTONT(fp); 280 int error; 281 282 dprintf(("ntfs_reclaim: vnode: %p, ntnode: %llu\n", vp, 283 (unsigned long long)ip->i_number)); 284 285 if (prtactive && vp->v_usecount != 0) 286 vprint("ntfs_reclaim: pushing active", vp); 287 288 if ((error = ntfs_ntget(ip)) != 0) 289 return (error); 290 291 /* Purge old data structures associated with the inode. */ 292 cache_purge(vp); 293 if (ip->i_devvp) { 294 vrele(ip->i_devvp); 295 ip->i_devvp = NULL; 296 } 297 298 ntfs_frele(fp); 299 ntfs_ntput(ip); 300 vp->v_data = NULL; 301 302 return (0); 303 } 304 305 static int 306 ntfs_print(ap) 307 struct vop_print_args /* { 308 struct vnode *a_vp; 309 } */ *ap; 310 { 311 struct ntnode *ip = VTONT(ap->a_vp); 312 313 printf("tag VT_NTFS, ino %llu, flag %#x, usecount %d, nlink %ld\n", 314 (unsigned long long)ip->i_number, ip->i_flag, ip->i_usecount, 315 ip->i_nlink); 316 printf(" "); 317 lockmgr_printinfo(ap->a_vp->v_vnlock); 318 printf("\n"); 319 return (0); 320 } 321 322 /* 323 * Calculate the logical to physical mapping if not done already, 324 * then call the device strategy routine. 325 */ 326 int 327 ntfs_strategy(ap) 328 struct vop_strategy_args /* { 329 struct vnode *a_vp; 330 struct buf *a_bp; 331 } */ *ap; 332 { 333 struct buf *bp = ap->a_bp; 334 struct vnode *vp = ap->a_vp; 335 struct fnode *fp = VTOF(vp); 336 struct ntnode *ip = FTONT(fp); 337 struct ntfsmount *ntmp = ip->i_mp; 338 int error; 339 340 #ifdef __FreeBSD__ 341 dprintf(("ntfs_strategy: offset: %d, blkno: %d, lblkno: %d\n", 342 (u_int32_t)bp->b_offset,(u_int32_t)bp->b_blkno, 343 (u_int32_t)bp->b_lblkno)); 344 #else 345 dprintf(("ntfs_strategy: blkno: %d, lblkno: %d\n", 346 (u_int32_t)bp->b_blkno, 347 (u_int32_t)bp->b_lblkno)); 348 #endif 349 350 dprintf(("strategy: bcount: %u flags: 0x%x\n", 351 (u_int32_t)bp->b_bcount,bp->b_flags)); 352 353 if (bp->b_flags & B_READ) { 354 u_int32_t toread; 355 356 if (ntfs_cntob(bp->b_blkno) >= fp->f_size) { 357 clrbuf(bp); 358 error = 0; 359 } else { 360 toread = MIN(bp->b_bcount, 361 fp->f_size - ntfs_cntob(bp->b_blkno)); 362 dprintf(("ntfs_strategy: toread: %d, fsize: %d\n", 363 toread,(u_int32_t)fp->f_size)); 364 365 error = ntfs_readattr(ntmp, ip, fp->f_attrtype, 366 fp->f_attrname, ntfs_cntob(bp->b_blkno), 367 toread, bp->b_data, NULL); 368 369 if (error) { 370 printf("ntfs_strategy: ntfs_readattr failed\n"); 371 bp->b_error = error; 372 bp->b_flags |= B_ERROR; 373 } 374 375 bzero(bp->b_data + toread, bp->b_bcount - toread); 376 } 377 } else { 378 size_t tmp; 379 u_int32_t towrite; 380 381 if (ntfs_cntob(bp->b_blkno) + bp->b_bcount >= fp->f_size) { 382 printf("ntfs_strategy: CAN'T EXTEND FILE\n"); 383 bp->b_error = error = EFBIG; 384 bp->b_flags |= B_ERROR; 385 } else { 386 towrite = MIN(bp->b_bcount, 387 fp->f_size - ntfs_cntob(bp->b_blkno)); 388 dprintf(("ntfs_strategy: towrite: %d, fsize: %d\n", 389 towrite,(u_int32_t)fp->f_size)); 390 391 error = ntfs_writeattr_plain(ntmp, ip, fp->f_attrtype, 392 fp->f_attrname, ntfs_cntob(bp->b_blkno),towrite, 393 bp->b_data, &tmp, NULL); 394 395 if (error) { 396 printf("ntfs_strategy: ntfs_writeattr fail\n"); 397 bp->b_error = error; 398 bp->b_flags |= B_ERROR; 399 } 400 } 401 } 402 biodone(bp); 403 return (error); 404 } 405 406 static int 407 ntfs_write(ap) 408 struct vop_write_args /* { 409 struct vnode *a_vp; 410 struct uio *a_uio; 411 int a_ioflag; 412 struct ucred *a_cred; 413 } */ *ap; 414 { 415 struct vnode *vp = ap->a_vp; 416 struct fnode *fp = VTOF(vp); 417 struct ntnode *ip = FTONT(fp); 418 struct uio *uio = ap->a_uio; 419 struct ntfsmount *ntmp = ip->i_mp; 420 u_int64_t towrite; 421 size_t written; 422 int error; 423 424 dprintf(("ntfs_write: ino: %llu, off: %qd resid: %qd, segflg: %d\n", 425 (unsigned long long)ip->i_number, (long long)uio->uio_offset, 426 (long long)uio->uio_resid, uio->uio_segflg)); 427 dprintf(("ntfs_write: filesize: %qu",(long long)fp->f_size)); 428 429 if (uio->uio_resid + uio->uio_offset > fp->f_size) { 430 printf("ntfs_write: CAN'T WRITE BEYOND END OF FILE\n"); 431 return (EFBIG); 432 } 433 434 towrite = MIN(uio->uio_resid, fp->f_size - uio->uio_offset); 435 436 dprintf((", towrite: %qu\n",(long long)towrite)); 437 438 error = ntfs_writeattr_plain(ntmp, ip, fp->f_attrtype, 439 fp->f_attrname, uio->uio_offset, towrite, NULL, &written, uio); 440 #ifdef NTFS_DEBUG 441 if (error) 442 printf("ntfs_write: ntfs_writeattr failed: %d\n", error); 443 #endif 444 445 return (error); 446 } 447 448 int 449 ntfs_access(ap) 450 struct vop_access_args /* { 451 struct vnode *a_vp; 452 int a_mode; 453 struct ucred *a_cred; 454 struct lwp *a_l; 455 } */ *ap; 456 { 457 struct vnode *vp = ap->a_vp; 458 struct ntnode *ip = VTONT(vp); 459 struct ucred *cred = ap->a_cred; 460 mode_t mask, mode = ap->a_mode; 461 gid_t *gp; 462 int i; 463 464 dprintf(("ntfs_access: %llu\n", (unsigned long long)ip->i_number)); 465 466 /* 467 * Disallow write attempts on read-only file systems; 468 * unless the file is a socket, fifo, or a block or 469 * character device resident on the file system. 470 */ 471 if (mode & VWRITE) { 472 switch ((int)vp->v_type) { 473 case VDIR: 474 case VLNK: 475 case VREG: 476 if (vp->v_mount->mnt_flag & MNT_RDONLY) 477 return (EROFS); 478 break; 479 } 480 } 481 482 /* Otherwise, user id 0 always gets access. */ 483 if (cred->cr_uid == 0) 484 return (0); 485 486 mask = 0; 487 488 /* Otherwise, check the owner. */ 489 if (cred->cr_uid == ip->i_mp->ntm_uid) { 490 if (mode & VEXEC) 491 mask |= S_IXUSR; 492 if (mode & VREAD) 493 mask |= S_IRUSR; 494 if (mode & VWRITE) 495 mask |= S_IWUSR; 496 return ((ip->i_mp->ntm_mode & mask) == mask ? 0 : EACCES); 497 } 498 499 /* Otherwise, check the groups. */ 500 for (i = 0, gp = cred->cr_groups; i < cred->cr_ngroups; i++, gp++) 501 if (ip->i_mp->ntm_gid == *gp) { 502 if (mode & VEXEC) 503 mask |= S_IXGRP; 504 if (mode & VREAD) 505 mask |= S_IRGRP; 506 if (mode & VWRITE) 507 mask |= S_IWGRP; 508 return ((ip->i_mp->ntm_mode&mask) == mask ? 0 : EACCES); 509 } 510 511 /* Otherwise, check everyone else. */ 512 if (mode & VEXEC) 513 mask |= S_IXOTH; 514 if (mode & VREAD) 515 mask |= S_IROTH; 516 if (mode & VWRITE) 517 mask |= S_IWOTH; 518 return ((ip->i_mp->ntm_mode & mask) == mask ? 0 : EACCES); 519 } 520 521 /* 522 * Open called. 523 * 524 * Nothing to do. 525 */ 526 /* ARGSUSED */ 527 static int 528 ntfs_open(ap) 529 struct vop_open_args /* { 530 struct vnode *a_vp; 531 int a_mode; 532 struct ucred *a_cred; 533 struct lwp *a_l; 534 } */ *ap; 535 { 536 #ifdef NTFS_DEBUG 537 struct vnode *vp = ap->a_vp; 538 struct ntnode *ip = VTONT(vp); 539 540 printf("ntfs_open: %llu\n", (unsigned long long)ip->i_number); 541 #endif 542 543 /* 544 * Files marked append-only must be opened for appending. 545 */ 546 547 return (0); 548 } 549 550 /* 551 * Close called. 552 * 553 * Update the times on the inode. 554 */ 555 /* ARGSUSED */ 556 static int 557 ntfs_close(ap) 558 struct vop_close_args /* { 559 struct vnode *a_vp; 560 int a_fflag; 561 struct ucred *a_cred; 562 struct lwp *a_l; 563 } */ *ap; 564 { 565 #ifdef NTFS_DEBUG 566 struct vnode *vp = ap->a_vp; 567 struct ntnode *ip = VTONT(vp); 568 569 printf("ntfs_close: %llu\n", (unsigned long long)ip->i_number); 570 #endif 571 572 return (0); 573 } 574 575 int 576 ntfs_readdir(ap) 577 struct vop_readdir_args /* { 578 struct vnode *a_vp; 579 struct uio *a_uio; 580 struct ucred *a_cred; 581 int *a_ncookies; 582 u_int **cookies; 583 } */ *ap; 584 { 585 struct vnode *vp = ap->a_vp; 586 struct fnode *fp = VTOF(vp); 587 struct ntnode *ip = FTONT(fp); 588 struct uio *uio = ap->a_uio; 589 struct ntfsmount *ntmp = ip->i_mp; 590 int i, error = 0; 591 u_int32_t faked = 0, num; 592 int ncookies = 0; 593 struct dirent *cde; 594 off_t off; 595 596 dprintf(("ntfs_readdir %llu off: %qd resid: %qd\n", 597 (unsigned long long)ip->i_number, (long long)uio->uio_offset, 598 (long long)uio->uio_resid)); 599 600 off = uio->uio_offset; 601 602 MALLOC(cde, struct dirent *, sizeof(struct dirent), M_TEMP, M_WAITOK); 603 604 /* Simulate . in every dir except ROOT */ 605 if (ip->i_number != NTFS_ROOTINO 606 && uio->uio_offset < sizeof(struct dirent)) { 607 cde->d_fileno = ip->i_number; 608 cde->d_reclen = sizeof(struct dirent); 609 cde->d_type = DT_DIR; 610 cde->d_namlen = 1; 611 strncpy(cde->d_name, ".", 2); 612 error = uiomove((void *)cde, sizeof(struct dirent), uio); 613 if (error) 614 goto out; 615 616 ncookies++; 617 } 618 619 /* Simulate .. in every dir including ROOT */ 620 if (uio->uio_offset < 2 * sizeof(struct dirent)) { 621 cde->d_fileno = NTFS_ROOTINO; /* XXX */ 622 cde->d_reclen = sizeof(struct dirent); 623 cde->d_type = DT_DIR; 624 cde->d_namlen = 2; 625 strncpy(cde->d_name, "..", 3); 626 627 error = uiomove((void *) cde, sizeof(struct dirent), uio); 628 if (error) 629 goto out; 630 631 ncookies++; 632 } 633 634 faked = (ip->i_number == NTFS_ROOTINO) ? 1 : 2; 635 num = uio->uio_offset / sizeof(struct dirent) - faked; 636 637 while (uio->uio_resid >= sizeof(struct dirent)) { 638 struct attr_indexentry *iep; 639 char *fname; 640 size_t remains; 641 int sz; 642 643 error = ntfs_ntreaddir(ntmp, fp, num, &iep); 644 if (error) 645 goto out; 646 647 if (NULL == iep) 648 break; 649 650 for(; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (uio->uio_resid >= sizeof(struct dirent)); 651 iep = NTFS_NEXTREC(iep, struct attr_indexentry *)) 652 { 653 if(!ntfs_isnamepermitted(ntmp,iep)) 654 continue; 655 656 remains = sizeof(cde->d_name) - 1; 657 fname = cde->d_name; 658 for(i=0; i<iep->ie_fnamelen; i++) { 659 sz = (*ntmp->ntm_wput)(fname, remains, 660 iep->ie_fname[i]); 661 fname += sz; 662 remains -= sz; 663 } 664 *fname = '\0'; 665 dprintf(("ntfs_readdir: elem: %d, fname:[%s] type: %d, flag: %d, ", 666 num, cde->d_name, iep->ie_fnametype, 667 iep->ie_flag)); 668 cde->d_namlen = fname - (char *) cde->d_name; 669 cde->d_fileno = iep->ie_number; 670 cde->d_type = (iep->ie_fflag & NTFS_FFLAG_DIR) ? DT_DIR : DT_REG; 671 cde->d_reclen = sizeof(struct dirent); 672 dprintf(("%s\n", (cde->d_type == DT_DIR) ? "dir":"reg")); 673 674 error = uiomove((void *)cde, sizeof(struct dirent), uio); 675 if (error) 676 goto out; 677 678 ncookies++; 679 num++; 680 } 681 } 682 683 dprintf(("ntfs_readdir: %d entries (%d bytes) read\n", 684 ncookies,(u_int)(uio->uio_offset - off))); 685 dprintf(("ntfs_readdir: off: %qd resid: %qu\n", 686 (long long)uio->uio_offset,(long long)uio->uio_resid)); 687 688 if (!error && ap->a_ncookies != NULL) { 689 struct dirent* dpStart; 690 struct dirent* dp; 691 #if defined(__FreeBSD__) 692 u_long *cookies; 693 u_long *cookiep; 694 #else /* defined(__NetBSD__) */ 695 off_t *cookies; 696 off_t *cookiep; 697 #endif 698 699 dprintf(("ntfs_readdir: %d cookies\n",ncookies)); 700 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) 701 panic("ntfs_readdir: unexpected uio from NFS server"); 702 dpStart = (struct dirent *) 703 ((caddr_t)uio->uio_iov->iov_base - 704 (uio->uio_offset - off)); 705 #if defined(__FreeBSD__) 706 MALLOC(cookies, u_long *, ncookies * sizeof(u_long), 707 M_TEMP, M_WAITOK); 708 #else /* defined(__NetBSD__) */ 709 cookies = malloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK); 710 #endif 711 for (dp = dpStart, cookiep = cookies, i=0; 712 i < ncookies; 713 dp = (struct dirent *)((caddr_t) dp + dp->d_reclen), i++) { 714 off += dp->d_reclen; 715 *cookiep++ = (u_int) off; 716 } 717 *ap->a_ncookies = ncookies; 718 *ap->a_cookies = cookies; 719 } 720 /* 721 if (ap->a_eofflag) 722 *ap->a_eofflag = VTONT(ap->a_vp)->i_size <= uio->uio_offset; 723 */ 724 out: 725 FREE(cde, M_TEMP); 726 return (error); 727 } 728 729 int 730 ntfs_lookup(ap) 731 struct vop_lookup_args /* { 732 struct vnode *a_dvp; 733 struct vnode **a_vpp; 734 struct componentname *a_cnp; 735 } */ *ap; 736 { 737 struct vnode *dvp = ap->a_dvp; 738 struct ntnode *dip = VTONT(dvp); 739 struct ntfsmount *ntmp = dip->i_mp; 740 struct componentname *cnp = ap->a_cnp; 741 struct ucred *cred = cnp->cn_cred; 742 int error; 743 int lockparent = cnp->cn_flags & LOCKPARENT; 744 #ifdef NTFS_DEBUG 745 int wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT); 746 #endif 747 dprintf(("ntfs_lookup: \"%.*s\" (%ld bytes) in %llu, lp: %d, wp: %d \n", 748 (int)cnp->cn_namelen, cnp->cn_nameptr, cnp->cn_namelen, 749 (unsigned long long)dip->i_number, lockparent, wantparent)); 750 751 error = VOP_ACCESS(dvp, VEXEC, cred, cnp->cn_lwp); 752 if(error) 753 return (error); 754 755 if ((cnp->cn_flags & ISLASTCN) && 756 (dvp->v_mount->mnt_flag & MNT_RDONLY) && 757 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) 758 return (EROFS); 759 760 #ifdef __NetBSD__ 761 /* 762 * We now have a segment name to search for, and a directory 763 * to search. 764 * 765 * Before tediously performing a linear scan of the directory, 766 * check the name cache to see if the directory/name pair 767 * we are looking for is known already. 768 */ 769 if ((error = cache_lookup(ap->a_dvp, ap->a_vpp, cnp)) >= 0) 770 return (error); 771 #endif 772 773 if(cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') { 774 dprintf(("ntfs_lookup: faking . directory in %llu\n", 775 (unsigned long long)dip->i_number)); 776 777 VREF(dvp); 778 *ap->a_vpp = dvp; 779 error = 0; 780 } else if (cnp->cn_flags & ISDOTDOT) { 781 struct ntvattr *vap; 782 783 dprintf(("ntfs_lookup: faking .. directory in %llu\n", 784 (unsigned long long)dip->i_number)); 785 786 VOP_UNLOCK(dvp, 0); 787 cnp->cn_flags |= PDIRUNLOCK; 788 789 error = ntfs_ntvattrget(ntmp, dip, NTFS_A_NAME, NULL, 0, &vap); 790 if(error) 791 return (error); 792 793 dprintf(("ntfs_lookup: parentdir: %d\n", 794 vap->va_a_name->n_pnumber)); 795 error = VFS_VGET(ntmp->ntm_mountp, 796 vap->va_a_name->n_pnumber,ap->a_vpp); 797 ntfs_ntvattrrele(vap); 798 if (error) { 799 if (vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY) == 0) 800 cnp->cn_flags &= ~PDIRUNLOCK; 801 return (error); 802 } 803 804 if (lockparent && (cnp->cn_flags & ISLASTCN)) { 805 error = vn_lock(dvp, LK_EXCLUSIVE); 806 if (error) { 807 vput( *(ap->a_vpp) ); 808 return (error); 809 } 810 cnp->cn_flags &= ~PDIRUNLOCK; 811 } 812 } else { 813 error = ntfs_ntlookupfile(ntmp, dvp, cnp, ap->a_vpp); 814 if (error) { 815 dprintf(("ntfs_ntlookupfile: returned %d\n", error)); 816 return (error); 817 } 818 819 dprintf(("ntfs_lookup: found ino: %llu\n", 820 (unsigned long long)VTONT(*ap->a_vpp)->i_number)); 821 822 if(!lockparent || (cnp->cn_flags & ISLASTCN) == 0) { 823 VOP_UNLOCK(dvp, 0); 824 cnp->cn_flags |= PDIRUNLOCK; 825 } 826 } 827 828 if (cnp->cn_flags & MAKEENTRY) 829 cache_enter(dvp, *ap->a_vpp, cnp); 830 831 return (error); 832 } 833 834 /* 835 * Flush the blocks of a file to disk. 836 * 837 * This function is worthless for vnodes that represent directories. Maybe we 838 * could just do a sync if they try an fsync on a directory file. 839 */ 840 static int 841 ntfs_fsync(ap) 842 struct vop_fsync_args /* { 843 struct vnode *a_vp; 844 struct ucred *a_cred; 845 int a_flags; 846 off_t offlo; 847 off_t offhi; 848 struct lwp *a_l; 849 } */ *ap; 850 { 851 852 struct vnode *vp = ap->a_vp; 853 int wait; 854 855 if (ap->a_flags & FSYNC_CACHE) { 856 return EOPNOTSUPP; 857 } 858 859 wait = (ap->a_flags & FSYNC_WAIT) != 0; 860 vflushbuf(vp, wait); 861 862 return 0; 863 } 864 865 /* 866 * Return POSIX pathconf information applicable to NTFS filesystem 867 */ 868 static int 869 ntfs_pathconf(v) 870 void *v; 871 { 872 struct vop_pathconf_args /* { 873 struct vnode *a_vp; 874 int a_name; 875 register_t *a_retval; 876 } */ *ap = v; 877 878 switch (ap->a_name) { 879 case _PC_LINK_MAX: 880 *ap->a_retval = 1; 881 return (0); 882 case _PC_NAME_MAX: 883 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_namemax; 884 return (0); 885 case _PC_PATH_MAX: 886 *ap->a_retval = PATH_MAX; 887 return (0); 888 case _PC_CHOWN_RESTRICTED: 889 *ap->a_retval = 1; 890 return (0); 891 case _PC_NO_TRUNC: 892 *ap->a_retval = 0; 893 return (0); 894 case _PC_SYNC_IO: 895 *ap->a_retval = 1; 896 return (0); 897 case _PC_FILESIZEBITS: 898 *ap->a_retval = 64; 899 return (0); 900 default: 901 return (EINVAL); 902 } 903 /* NOTREACHED */ 904 } 905 906 /* 907 * Global vfs data structures 908 */ 909 vop_t **ntfs_vnodeop_p; 910 #if defined(__FreeBSD__) 911 static 912 struct vnodeopv_entry_desc ntfs_vnodeop_entries[] = { 913 { &vop_default_desc, (vop_t *)ntfs_bypass }, 914 915 { &vop_getattr_desc, (vop_t *)ntfs_getattr }, 916 { &vop_inactive_desc, (vop_t *)ntfs_inactive }, 917 { &vop_reclaim_desc, (vop_t *)ntfs_reclaim }, 918 { &vop_print_desc, (vop_t *)ntfs_print }, 919 { &vop_pathconf_desc, ntfs_pathconf }, 920 921 { &vop_islocked_desc, (vop_t *)vop_stdislocked }, 922 { &vop_unlock_desc, (vop_t *)vop_stdunlock }, 923 { &vop_lock_desc, (vop_t *)vop_stdlock }, 924 { &vop_cachedlookup_desc, (vop_t *)ntfs_lookup }, 925 { &vop_lookup_desc, (vop_t *)vfs_cache_lookup }, 926 927 { &vop_access_desc, (vop_t *)ntfs_access }, 928 { &vop_close_desc, (vop_t *)ntfs_close }, 929 { &vop_open_desc, (vop_t *)ntfs_open }, 930 { &vop_readdir_desc, (vop_t *)ntfs_readdir }, 931 { &vop_fsync_desc, (vop_t *)ntfs_fsync }, 932 933 { &vop_bmap_desc, (vop_t *)ntfs_bmap }, 934 { &vop_getpages_desc, (vop_t *) ntfs_getpages }, 935 { &vop_putpages_desc, (vop_t *) ntfs_putpages }, 936 { &vop_strategy_desc, (vop_t *)ntfs_strategy }, 937 { &vop_bwrite_desc, (vop_t *)vop_stdbwrite }, 938 { &vop_read_desc, (vop_t *)ntfs_read }, 939 { &vop_write_desc, (vop_t *)ntfs_write }, 940 941 { NULL, NULL } 942 }; 943 944 static 945 struct vnodeopv_desc ntfs_vnodeop_opv_desc = 946 { &ntfs_vnodeop_p, ntfs_vnodeop_entries }; 947 948 VNODEOP_SET(ntfs_vnodeop_opv_desc); 949 950 #else /* !FreeBSD */ 951 952 const struct vnodeopv_entry_desc ntfs_vnodeop_entries[] = { 953 { &vop_default_desc, (vop_t *) ntfs_bypass }, 954 { &vop_lookup_desc, (vop_t *) ntfs_lookup }, /* lookup */ 955 { &vop_create_desc, genfs_eopnotsupp }, /* create */ 956 { &vop_mknod_desc, genfs_eopnotsupp }, /* mknod */ 957 { &vop_open_desc, (vop_t *) ntfs_open }, /* open */ 958 { &vop_close_desc,(vop_t *) ntfs_close }, /* close */ 959 { &vop_access_desc, (vop_t *) ntfs_access }, /* access */ 960 { &vop_getattr_desc, (vop_t *) ntfs_getattr }, /* getattr */ 961 { &vop_setattr_desc, genfs_eopnotsupp }, /* setattr */ 962 { &vop_read_desc, (vop_t *) ntfs_read }, /* read */ 963 { &vop_write_desc, (vop_t *) ntfs_write }, /* write */ 964 { &vop_lease_desc, genfs_lease_check }, /* lease */ 965 { &vop_fcntl_desc, genfs_fcntl }, /* fcntl */ 966 { &vop_ioctl_desc, genfs_enoioctl }, /* ioctl */ 967 { &vop_poll_desc, genfs_poll }, /* poll */ 968 { &vop_kqfilter_desc, genfs_kqfilter }, /* kqfilter */ 969 { &vop_revoke_desc, genfs_revoke }, /* revoke */ 970 { &vop_mmap_desc, genfs_mmap }, /* mmap */ 971 { &vop_fsync_desc, (vop_t *) ntfs_fsync }, /* fsync */ 972 { &vop_seek_desc, genfs_seek }, /* seek */ 973 { &vop_remove_desc, genfs_eopnotsupp }, /* remove */ 974 { &vop_link_desc, genfs_eopnotsupp }, /* link */ 975 { &vop_rename_desc, genfs_eopnotsupp }, /* rename */ 976 { &vop_mkdir_desc, genfs_eopnotsupp }, /* mkdir */ 977 { &vop_rmdir_desc, genfs_eopnotsupp }, /* rmdir */ 978 { &vop_symlink_desc, genfs_eopnotsupp }, /* symlink */ 979 { &vop_readdir_desc, (vop_t *) ntfs_readdir }, /* readdir */ 980 { &vop_readlink_desc, genfs_eopnotsupp }, /* readlink */ 981 { &vop_abortop_desc, genfs_abortop }, /* abortop */ 982 { &vop_inactive_desc, (vop_t *) ntfs_inactive }, /* inactive */ 983 { &vop_reclaim_desc, (vop_t *) ntfs_reclaim }, /* reclaim */ 984 { &vop_lock_desc, genfs_lock }, /* lock */ 985 { &vop_unlock_desc, genfs_unlock }, /* unlock */ 986 { &vop_bmap_desc, (vop_t *) ntfs_bmap }, /* bmap */ 987 { &vop_strategy_desc, (vop_t *) ntfs_strategy }, /* strategy */ 988 { &vop_print_desc, (vop_t *) ntfs_print }, /* print */ 989 { &vop_islocked_desc, genfs_islocked }, /* islocked */ 990 { &vop_pathconf_desc, ntfs_pathconf }, /* pathconf */ 991 { &vop_advlock_desc, genfs_nullop }, /* advlock */ 992 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */ 993 { &vop_getpages_desc, genfs_compat_getpages }, /* getpages */ 994 { &vop_putpages_desc, genfs_putpages }, /* putpages */ 995 { NULL, NULL } 996 }; 997 const struct vnodeopv_desc ntfs_vnodeop_opv_desc = 998 { &ntfs_vnodeop_p, ntfs_vnodeop_entries }; 999 1000 #endif 1001