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