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