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