Due to continuing issues with VOP_READ/VOP_WRITE ops being called without
[dragonfly.git] / sys / vfs / 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: src/sys/ntfs/ntfs_vnops.c,v 1.9.2.4 2002/08/06 19:35:18 semenu Exp $
39  * $DragonFly: src/sys/vfs/ntfs/ntfs_vnops.c,v 1.29 2006/04/07 06:38:33 dillon Exp $
40  *
41  */
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/time.h>
47 #include <sys/types.h>
48 #include <sys/stat.h>
49 #include <sys/vnode.h>
50 #include <sys/mount.h>
51 #include <sys/proc.h>
52 #include <sys/namei.h>
53 #include <sys/malloc.h>
54 #include <sys/buf.h>
55 #include <sys/dirent.h>
56 #include <machine/limits.h>
57
58 #include <vm/vm.h>
59 #include <vm/vm_param.h>
60 #if defined(__NetBSD__)
61 #include <vm/vm_prot.h>
62 #endif
63 #include <vm/vm_page.h>
64 #include <vm/vm_object.h>
65 #include <vm/vm_pager.h>
66 #if defined(__DragonFly__)
67 #include <vm/vnode_pager.h>
68 #endif
69 #include <vm/vm_extern.h>
70
71 #include <sys/sysctl.h>
72
73 /*#define NTFS_DEBUG 1*/
74 #include "ntfs.h"
75 #include "ntfs_inode.h"
76 #include "ntfs_subr.h"
77 #if defined(__NetBSD__)
78 #include <miscfs/specfs/specdev.h>
79 #include <miscfs/genfs/genfs.h>
80 #endif
81
82 #include <sys/unistd.h> /* for pathconf(2) constants */
83
84 static int      ntfs_read (struct vop_read_args *);
85 static int      ntfs_write (struct vop_write_args *ap);
86 static int      ntfs_getattr (struct vop_getattr_args *ap);
87 static int      ntfs_inactive (struct vop_inactive_args *ap);
88 static int      ntfs_print (struct vop_print_args *ap);
89 static int      ntfs_reclaim (struct vop_reclaim_args *ap);
90 static int      ntfs_strategy (struct vop_strategy_args *ap);
91 static int      ntfs_access (struct vop_access_args *ap);
92 static int      ntfs_open (struct vop_open_args *ap);
93 static int      ntfs_close (struct vop_close_args *ap);
94 static int      ntfs_readdir (struct vop_readdir_args *ap);
95 static int      ntfs_lookup (struct vop_old_lookup_args *ap);
96 static int      ntfs_bmap (struct vop_bmap_args *ap);
97 #if defined(__DragonFly__)
98 static int      ntfs_getpages (struct vop_getpages_args *ap);
99 static int      ntfs_putpages (struct vop_putpages_args *);
100 static int      ntfs_fsync (struct vop_fsync_args *ap);
101 #else
102 static int      ntfs_bypass (struct vop_generic_args *);
103 #endif
104 static int      ntfs_pathconf (void *);
105
106 int     ntfs_prtactive = 1;     /* 1 => print out reclaim of active vnodes */
107
108 #if defined(__DragonFly__)
109 int
110 ntfs_getpages(struct vop_getpages_args *ap)
111 {
112         return vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
113                 ap->a_reqpage);
114 }
115
116 int
117 ntfs_putpages(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  * ntfs_bmap(struct vnode *a_vp, off_t a_loffset, struct vnode **a_vpp,
128  *           daddr_t *a_doffsetp, int *a_runp, int *a_runb)
129  */
130 int
131 ntfs_bmap(struct vop_bmap_args *ap)
132 {
133         dprintf(("ntfs_bmap: vn: %p, blk: %d\n", ap->a_vp,(u_int32_t)ap->a_bn));
134         if (ap->a_vpp != NULL)
135                 *ap->a_vpp = ap->a_vp;
136         if (ap->a_doffsetp != NULL)
137                 *ap->a_doffsetp = ap->a_loffset;
138         if (ap->a_runp != NULL)
139                 *ap->a_runp = 0;
140 #if !defined(__NetBSD__)
141         if (ap->a_runb != NULL)
142                 *ap->a_runb = 0;
143 #endif
144         return (0);
145 }
146
147 /*
148  * ntfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
149  *           struct ucred *a_cred)
150  */
151 static int
152 ntfs_read(struct vop_read_args *ap)
153 {
154         struct vnode *vp = ap->a_vp;
155         struct fnode *fp = VTOF(vp);
156         struct ntnode *ip = FTONT(fp);
157         struct uio *uio = ap->a_uio;
158         struct ntfsmount *ntmp = ip->i_mp;
159         struct buf *bp;
160         daddr_t cn;
161         int resid, off, toread;
162         int error;
163
164         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));
165
166         dprintf(("ntfs_read: filesize: %d",(u_int32_t)fp->f_size));
167
168         /* don't allow reading after end of file */
169         if (uio->uio_offset > fp->f_size)
170                 return (0);
171
172         resid = min(uio->uio_resid, fp->f_size - uio->uio_offset);
173
174         dprintf((", resid: %d\n", resid));
175
176         error = 0;
177         while (resid) {
178                 cn = ntfs_btocn(uio->uio_offset);
179                 off = ntfs_btocnoff(uio->uio_offset);
180
181                 toread = min(off + resid, ntfs_cntob(1));
182
183                 error = bread(vp, ntfs_cntodoff(cn), ntfs_cntob(1), &bp);
184                 if (error) {
185                         brelse(bp);
186                         break;
187                 }
188
189                 error = uiomove(bp->b_data + off, toread - off, uio);
190                 if(error) {
191                         brelse(bp);
192                         break;
193                 }
194                 brelse(bp);
195
196                 resid -= toread - off;
197         }
198
199         return (error);
200 }
201
202 #if !defined(__DragonFly__)
203
204 /*
205  * ntfs_bypass(struct vnodeop_desc *a_desc, ...)
206  */
207 static int
208 ntfs_bypass(struct vop_generic_args *ap)
209 {
210         int error = ENOTTY;
211         dprintf(("ntfs_bypass: %s\n", ap->a_desc->vdesc_name));
212         return (error);
213 }
214
215 #endif
216
217 /*
218  * ntfs_getattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred,
219  *              struct thread *a_td)
220  */
221 static int
222 ntfs_getattr(struct vop_getattr_args *ap)
223 {
224         struct vnode *vp = ap->a_vp;
225         struct fnode *fp = VTOF(vp);
226         struct ntnode *ip = FTONT(fp);
227         struct vattr *vap = ap->a_vap;
228
229         dprintf(("ntfs_getattr: %d, flags: %d\n",ip->i_number,ip->i_flag));
230
231 #if defined(__DragonFly__)
232         vap->va_fsid = dev2udev(ip->i_dev);
233 #else /* NetBSD */
234         vap->va_fsid = ip->i_dev;
235 #endif
236         vap->va_fileid = ip->i_number;
237         vap->va_mode = ip->i_mp->ntm_mode;
238         vap->va_nlink = ip->i_nlink;
239         vap->va_uid = ip->i_mp->ntm_uid;
240         vap->va_gid = ip->i_mp->ntm_gid;
241         vap->va_rdev = 0;                               /* XXX UNODEV ? */
242         vap->va_size = fp->f_size;
243         vap->va_bytes = fp->f_allocated;
244         vap->va_atime = ntfs_nttimetounix(fp->f_times.t_access);
245         vap->va_mtime = ntfs_nttimetounix(fp->f_times.t_write);
246         vap->va_ctime = ntfs_nttimetounix(fp->f_times.t_create);
247         vap->va_flags = ip->i_flag;
248         vap->va_gen = 0;
249         vap->va_blocksize = ip->i_mp->ntm_spc * ip->i_mp->ntm_bps;
250         vap->va_type = vp->v_type;
251         vap->va_filerev = 0;
252         return (0);
253 }
254
255
256 /*
257  * Last reference to an ntnode.  If necessary, write or delete it.
258  *
259  * ntfs_inactive(struct vnode *a_vp)
260  */
261 int
262 ntfs_inactive(struct vop_inactive_args *ap)
263 {
264         struct vnode *vp = ap->a_vp;
265 #ifdef NTFS_DEBUG
266         struct ntnode *ip = VTONT(vp);
267 #endif
268
269         dprintf(("ntfs_inactive: vnode: %p, ntnode: %d\n", vp, ip->i_number));
270
271         if (ntfs_prtactive && vp->v_usecount != 0)
272                 vprint("ntfs_inactive: pushing active", vp);
273
274         /*
275          * XXX since we don't support any filesystem changes
276          * right now, nothing more needs to be done
277          */
278         return (0);
279 }
280
281 /*
282  * Reclaim an fnode/ntnode so that it can be used for other purposes.
283  *
284  * ntfs_reclaim(struct vnode *a_vp)
285  */
286 int
287 ntfs_reclaim(struct vop_reclaim_args *ap)
288 {
289         struct vnode *vp = ap->a_vp;
290         struct fnode *fp = VTOF(vp);
291         struct ntnode *ip = FTONT(fp);
292         int error;
293
294         dprintf(("ntfs_reclaim: vnode: %p, ntnode: %d\n", vp, ip->i_number));
295
296         if (ntfs_prtactive && vp->v_usecount != 0)
297                 vprint("ntfs_reclaim: pushing active", vp);
298
299         if ((error = ntfs_ntget(ip)) != 0)
300                 return (error);
301         
302         ntfs_frele(fp);
303         ntfs_ntput(ip);
304         vp->v_data = NULL;
305
306         return (0);
307 }
308
309 /*
310  * ntfs_print(struct vnode *a_vp)
311  */
312 static int
313 ntfs_print(struct vop_print_args *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  * ntfs_strategy(struct vnode *a_vp, struct bio *a_bio)
323  */
324 int
325 ntfs_strategy(struct vop_strategy_args *ap)
326 {
327         struct bio *bio = ap->a_bio;
328         struct buf *bp = bio->bio_buf;
329         struct vnode *vp = ap->a_vp;
330         struct fnode *fp = VTOF(vp);
331         struct ntnode *ip = FTONT(fp);
332         struct ntfsmount *ntmp = ip->i_mp;
333         int error;
334
335         dprintf(("ntfs_strategy: loffset: %lld, doffset: %lld\n",
336                 bp->b_loffset, bio->bio_offset));
337
338         dprintf(("strategy: bcount: %d flags: 0x%lx\n", 
339                 (u_int32_t)bp->b_bcount,bp->b_flags));
340
341         if (bp->b_flags & B_READ) {
342                 u_int32_t toread;
343
344                 if (bio->bio_offset >= fp->f_size) {
345                         clrbuf(bp);
346                         error = 0;
347                 } else {
348                         toread = min(bp->b_bcount,
349                                  fp->f_size - bio->bio_offset);
350                         dprintf(("ntfs_strategy: toread: %d, fsize: %d\n",
351                                 toread,(u_int32_t)fp->f_size));
352
353                         error = ntfs_readattr(ntmp, ip, fp->f_attrtype,
354                                 fp->f_attrname, bio->bio_offset,
355                                 toread, bp->b_data, NULL);
356
357                         if (error) {
358                                 printf("ntfs_strategy: ntfs_readattr failed\n");
359                                 bp->b_error = error;
360                                 bp->b_flags |= B_ERROR;
361                         }
362
363                         bzero(bp->b_data + toread, bp->b_bcount - toread);
364                 }
365         } else {
366                 size_t tmp;
367                 u_int32_t towrite;
368
369                 if (bio->bio_offset + bp->b_bcount >= fp->f_size) {
370                         printf("ntfs_strategy: CAN'T EXTEND FILE\n");
371                         bp->b_error = error = EFBIG;
372                         bp->b_flags |= B_ERROR;
373                 } else {
374                         towrite = min(bp->b_bcount,
375                                       fp->f_size - bio->bio_offset);
376                         dprintf(("ntfs_strategy: towrite: %d, fsize: %d\n",
377                                 towrite,(u_int32_t)fp->f_size));
378
379                         error = ntfs_writeattr_plain(ntmp, ip, fp->f_attrtype,  
380                                 fp->f_attrname, bio->bio_offset,towrite,
381                                 bp->b_data, &tmp, NULL);
382
383                         if (error) {
384                                 printf("ntfs_strategy: ntfs_writeattr fail\n");
385                                 bp->b_error = error;
386                                 bp->b_flags |= B_ERROR;
387                         }
388                 }
389         }
390         biodone(bio);
391         return (error);
392 }
393
394 /*
395  * ntfs_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
396  *            struct ucred *a_cred)
397  */
398 static int
399 ntfs_write(struct vop_write_args *ap)
400 {
401         struct vnode *vp = ap->a_vp;
402         struct fnode *fp = VTOF(vp);
403         struct ntnode *ip = FTONT(fp);
404         struct uio *uio = ap->a_uio;
405         struct ntfsmount *ntmp = ip->i_mp;
406         u_int64_t towrite;
407         size_t written;
408         int error;
409
410         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));
411         dprintf(("ntfs_write: filesize: %d",(u_int32_t)fp->f_size));
412
413         if (uio->uio_resid + uio->uio_offset > fp->f_size) {
414                 printf("ntfs_write: CAN'T WRITE BEYOND END OF FILE\n");
415                 return (EFBIG);
416         }
417
418         towrite = min(uio->uio_resid, fp->f_size - uio->uio_offset);
419
420         dprintf((", towrite: %d\n",(u_int32_t)towrite));
421
422         error = ntfs_writeattr_plain(ntmp, ip, fp->f_attrtype,
423                 fp->f_attrname, uio->uio_offset, towrite, NULL, &written, uio);
424 #ifdef NTFS_DEBUG
425         if (error)
426                 printf("ntfs_write: ntfs_writeattr failed: %d\n", error);
427 #endif
428
429         return (error);
430 }
431
432 /*
433  * ntfs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
434  *              struct thread *a_td)
435  */
436 int
437 ntfs_access(struct vop_access_args *ap)
438 {
439         struct vnode *vp = ap->a_vp;
440         struct ntnode *ip = VTONT(vp);
441         struct ucred *cred = ap->a_cred;
442         mode_t mask, mode = ap->a_mode;
443         gid_t *gp;
444         int i;
445 #ifdef QUOTA
446         int error;
447 #endif
448
449         dprintf(("ntfs_access: %d\n",ip->i_number));
450
451         /*
452          * Disallow write attempts on read-only file systems;
453          * unless the file is a socket, fifo, or a block or
454          * character device resident on the file system.
455          */
456         if (mode & VWRITE) {
457                 switch ((int)vp->v_type) {
458                 case VDIR:
459                 case VLNK:
460                 case VREG:
461                         if (vp->v_mount->mnt_flag & MNT_RDONLY)
462                                 return (EROFS);
463 #ifdef QUOTA
464                         if (error = getinoquota(ip))
465                                 return (error);
466 #endif
467                         break;
468                 }
469         }
470
471         /* Otherwise, user id 0 always gets access. */
472         if (cred->cr_uid == 0)
473                 return (0);
474
475         mask = 0;
476
477         /* Otherwise, check the owner. */
478         if (cred->cr_uid == ip->i_mp->ntm_uid) {
479                 if (mode & VEXEC)
480                         mask |= S_IXUSR;
481                 if (mode & VREAD)
482                         mask |= S_IRUSR;
483                 if (mode & VWRITE)
484                         mask |= S_IWUSR;
485                 return ((ip->i_mp->ntm_mode & mask) == mask ? 0 : EACCES);
486         }
487
488         /* Otherwise, check the groups. */
489         for (i = 0, gp = cred->cr_groups; i < cred->cr_ngroups; i++, gp++)
490                 if (ip->i_mp->ntm_gid == *gp) {
491                         if (mode & VEXEC)
492                                 mask |= S_IXGRP;
493                         if (mode & VREAD)
494                                 mask |= S_IRGRP;
495                         if (mode & VWRITE)
496                                 mask |= S_IWGRP;
497                         return ((ip->i_mp->ntm_mode&mask) == mask ? 0 : EACCES);
498                 }
499
500         /* Otherwise, check everyone else. */
501         if (mode & VEXEC)
502                 mask |= S_IXOTH;
503         if (mode & VREAD)
504                 mask |= S_IROTH;
505         if (mode & VWRITE)
506                 mask |= S_IWOTH;
507         return ((ip->i_mp->ntm_mode & mask) == mask ? 0 : EACCES);
508 }
509
510 /*
511  * Open called.
512  *
513  * Nothing to do.
514  *
515  * ntfs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
516  *           struct thread *a_td)
517  */
518 /* ARGSUSED */
519 static int
520 ntfs_open(struct vop_open_args *ap)
521 {
522         return (vop_stdopen(ap));
523 }
524
525 /*
526  * Close called.
527  *
528  * Update the times on the inode.
529  *
530  * ntfs_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred,
531  *            struct thread *a_td)
532  */
533 /* ARGSUSED */
534 static int
535 ntfs_close(struct vop_close_args *ap)
536 {
537 #if NTFS_DEBUG
538         struct vnode *vp = ap->a_vp;
539         struct ntnode *ip = VTONT(vp);
540
541         printf("ntfs_close: %d\n",ip->i_number);
542 #endif
543
544         return (vop_stdclose(ap));
545 }
546
547 /*
548  * ntfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred,
549  *              int *a_ncookies, u_int **cookies)
550  */
551 int
552 ntfs_readdir(struct vop_readdir_args *ap)
553 {
554         struct vnode *vp = ap->a_vp;
555         struct fnode *fp = VTOF(vp);
556         struct ntnode *ip = FTONT(fp);
557         struct uio *uio = ap->a_uio;
558         struct ntfsmount *ntmp = ip->i_mp;
559         int i, error = 0;
560         u_int32_t faked = 0, num, off;
561         int ncookies = 0;
562         char convname[NTFS_MAXFILENAME + 1];
563
564         dprintf(("ntfs_readdir %d off: %d resid: %d\n",ip->i_number,(u_int32_t)uio->uio_offset,uio->uio_resid));
565
566         if (uio->uio_offset < 0 || uio->uio_offset > INT_MAX)
567                 return (EINVAL);
568
569         /*
570          * uio->uio_offset carries the number of the entry
571          * where we should start returning dirents.
572          *
573          * We fake up "." if we're not reading the FS root
574          * and we always fake up "..".
575          *
576          * off contains the entry we are starting at,
577          * num increments while we are reading.
578          */
579
580         off = num = uio->uio_offset;
581         faked = (ip->i_number == NTFS_ROOTINO) ? 1 : 2;
582
583         /* Simulate . in every dir except ROOT */
584         if (ip->i_number != NTFS_ROOTINO && num == 0) {
585                 if (vop_write_dirent(&error, uio, ip->i_number,
586                     DT_DIR, 1, "."))
587                         return (0);
588                 if (error)
589                         return (error);
590
591                 num++;
592                 ncookies++;
593         }
594
595         /* Simulate .. in every dir including ROOT */
596         if (num == faked - 1) {
597                 /* XXX NTFS_ROOTINO seems to be wrong here */
598                 if (vop_write_dirent(&error, uio, NTFS_ROOTINO,
599                     DT_DIR, 2, ".."))
600                         goto readdone;
601                 if(error)
602                         return (error);
603
604                 num++;
605                 ncookies++;
606         }
607
608         for (;;) {
609                 struct attr_indexentry *iep;
610
611                 /*
612                  * num is the number of the entry we will return,
613                  * but ntfs_ntreaddir takes the entry number of the
614                  * ntfs directory listing, so subtract the faked
615                  * . and .. entries.
616                  */
617                 error = ntfs_ntreaddir(ntmp, fp, num - faked, &iep);
618
619                 if(error)
620                         return (error);
621
622                 if( NULL == iep )
623                         break;
624
625                 for (; !(iep->ie_flag & NTFS_IEFLAG_LAST);
626                         iep = NTFS_NEXTREC(iep, struct attr_indexentry *))
627                 {
628                         if(!ntfs_isnamepermitted(ntmp,iep))
629                                 continue;
630
631                         for (i=0; i < iep->ie_fnamelen; i++)
632                                 convname[i] = NTFS_U28(iep->ie_fname[i]);
633                         convname[i] = '\0';
634
635                         if (vop_write_dirent(&error, uio, iep->ie_number,
636                             (iep->ie_fflag & NTFS_FFLAG_DIR) ? DT_DIR : DT_REG,
637                             iep->ie_fnamelen, convname))
638                                 goto readdone;
639
640                         dprintf(("ntfs_readdir: elem: %d, fname:[%s] type: %d, "
641                                  "flag: %d, %s\n",
642                                  ncookies, convname, iep->ie_fnametype,
643                                  iep->ie_flag,
644                                  (iep->ie_fflag & NTFS_FFLAG_DIR) ?
645                                         "dir" : "reg"));
646
647                         if(error)
648                                 return (error);
649
650                         ncookies++;
651                         num++;
652                 }
653         }
654
655 readdone:
656         uio->uio_offset = num;
657
658         dprintf(("ntfs_readdir: %d entries (%d bytes) read\n",
659                 ncookies,(u_int)(uio->uio_offset - off)));
660         dprintf(("ntfs_readdir: off: %d resid: %d\n",
661                 (u_int32_t)uio->uio_offset,uio->uio_resid));
662
663         if (!error && ap->a_ncookies != NULL) {
664 #if defined(__DragonFly__)
665                 u_long *cookies;
666                 u_long *cookiep;
667 #else /* defined(__NetBSD__) */
668                 off_t *cookies;
669                 off_t *cookiep;
670 #endif
671
672                 ddprintf(("ntfs_readdir: %d cookies\n",ncookies));
673                 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
674                         panic("ntfs_readdir: unexpected uio from NFS server");
675 #if defined(__DragonFly__)
676                 MALLOC(cookies, u_long *, ncookies * sizeof(u_long),
677                        M_TEMP, M_WAITOK);
678 #else /* defined(__NetBSD__) */
679                 MALLOC(cookies, off_t *, ncookies * sizeof(off_t),
680                        M_TEMP, M_WAITOK);
681 #endif
682                 cookiep = cookies;
683                 while (off < num)
684                         *cookiep++ = ++off;
685
686                 *ap->a_ncookies = ncookies;
687                 *ap->a_cookies = cookies;
688         }
689 /*
690         if (ap->a_eofflag)
691             *ap->a_eofflag = VTONT(ap->a_vp)->i_size <= uio->uio_offset;
692 */
693         return (error);
694 }
695
696 /*
697  * ntfs_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
698  *              struct componentname *a_cnp)
699  */
700 int
701 ntfs_lookup(struct vop_old_lookup_args *ap)
702 {
703         struct vnode *dvp = ap->a_dvp;
704         struct ntnode *dip = VTONT(dvp);
705         struct ntfsmount *ntmp = dip->i_mp;
706         struct componentname *cnp = ap->a_cnp;
707         int error;
708         int lockparent = cnp->cn_flags & CNP_LOCKPARENT;
709 #if NTFS_DEBUG
710         int wantparent = cnp->cn_flags & (CNP_LOCKPARENT | CNP_WANTPARENT);
711 #endif
712         dprintf(("ntfs_lookup: \"%.*s\" (%ld bytes) in %d, lp: %d, wp: %d \n",
713                 (int)cnp->cn_namelen, cnp->cn_nameptr, cnp->cn_namelen,
714                 dip->i_number, lockparent, wantparent));
715
716         *ap->a_vpp = NULL;
717
718         if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
719                 dprintf(("ntfs_lookup: faking . directory in %d\n",
720                         dip->i_number));
721
722                 vref(dvp);
723                 *ap->a_vpp = dvp;
724                 error = 0;
725         } else if (cnp->cn_flags & CNP_ISDOTDOT) {
726                 struct ntvattr *vap;
727
728                 dprintf(("ntfs_lookup: faking .. directory in %d\n",
729                          dip->i_number));
730
731                 error = ntfs_ntvattrget(ntmp, dip, NTFS_A_NAME, NULL, 0, &vap);
732                 if(error)
733                         return (error);
734
735                 VOP__UNLOCK(dvp,0,cnp->cn_td);
736                 cnp->cn_flags |= CNP_PDIRUNLOCK;
737
738                 dprintf(("ntfs_lookup: parentdir: %d\n",
739                          vap->va_a_name->n_pnumber));
740                 error = VFS_VGET(ntmp->ntm_mountp,
741                                  vap->va_a_name->n_pnumber,ap->a_vpp); 
742                 ntfs_ntvattrrele(vap);
743                 if (error) {
744                         if (VN_LOCK(dvp,LK_EXCLUSIVE|LK_RETRY,cnp->cn_td)==0)
745                                 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
746                         return (error);
747                 }
748
749                 if (lockparent) {
750                         error = VN_LOCK(dvp, LK_EXCLUSIVE, cnp->cn_td);
751                         if (error) {
752                                 vput(*ap->a_vpp);
753                                 *ap->a_vpp = NULL;
754                                 return (error);
755                         }
756                         cnp->cn_flags &= ~CNP_PDIRUNLOCK;
757                 }
758         } else {
759                 error = ntfs_ntlookupfile(ntmp, dvp, cnp, ap->a_vpp);
760                 if (error) {
761                         dprintf(("ntfs_ntlookupfile: returned %d\n", error));
762                         return (error);
763                 }
764
765                 dprintf(("ntfs_lookup: found ino: %d\n", 
766                         VTONT(*ap->a_vpp)->i_number));
767
768                 if (!lockparent)
769                         VOP__UNLOCK(dvp, 0, cnp->cn_td);
770         }
771         return (error);
772 }
773
774 #if defined(__DragonFly__)
775 /*
776  * Flush the blocks of a file to disk.
777  *
778  * This function is worthless for vnodes that represent directories. Maybe we
779  * could just do a sync if they try an fsync on a directory file.
780  *
781  * ntfs_fsync(struct vnode *a_vp, struct ucred *a_cred, int a_waitfor,
782  *            struct thread *a_td)
783  */
784 static int
785 ntfs_fsync(struct vop_fsync_args *ap)
786 {
787         return (0);
788 }
789 #endif
790
791 /*
792  * Return POSIX pathconf information applicable to NTFS filesystem
793  */
794 int
795 ntfs_pathconf(void *v)
796 {
797         struct vop_pathconf_args /* {
798                 struct vnode *a_vp;
799                 int a_name;
800                 register_t *a_retval;
801         } */ *ap = v;
802
803         switch (ap->a_name) {
804         case _PC_LINK_MAX:
805                 *ap->a_retval = 1;
806                 return (0);
807         case _PC_NAME_MAX:
808                 *ap->a_retval = NTFS_MAXFILENAME;
809                 return (0);
810         case _PC_PATH_MAX:
811                 *ap->a_retval = PATH_MAX;
812                 return (0);
813         case _PC_CHOWN_RESTRICTED:
814                 *ap->a_retval = 1;
815                 return (0);
816         case _PC_NO_TRUNC:
817                 *ap->a_retval = 0;
818                 return (0);
819 #if defined(__NetBSD__)
820         case _PC_SYNC_IO:
821                 *ap->a_retval = 1;
822                 return (0);
823         case _PC_FILESIZEBITS:
824                 *ap->a_retval = 64;
825                 return (0);
826 #endif
827         default:
828                 return (EINVAL);
829         }
830         /* NOTREACHED */
831 }
832
833 /*
834  * Global vfs data structures
835  */
836 struct vnodeopv_entry_desc ntfs_vnodeop_entries[] = {
837         { &vop_default_desc,    vop_defaultop },
838
839         { &vop_getattr_desc,    (vnodeopv_entry_t)ntfs_getattr },
840         { &vop_inactive_desc,   (vnodeopv_entry_t)ntfs_inactive },
841         { &vop_reclaim_desc,    (vnodeopv_entry_t)ntfs_reclaim },
842         { &vop_print_desc,      (vnodeopv_entry_t)ntfs_print },
843         { &vop_pathconf_desc,   (vnodeopv_entry_t)ntfs_pathconf },
844
845         { &vop_islocked_desc,   (vnodeopv_entry_t)vop_stdislocked },
846         { &vop_unlock_desc,     (vnodeopv_entry_t)vop_stdunlock },
847         { &vop_lock_desc,       (vnodeopv_entry_t)vop_stdlock },
848         { &vop_old_lookup_desc, (vnodeopv_entry_t)ntfs_lookup },
849
850         { &vop_access_desc,     (vnodeopv_entry_t)ntfs_access },
851         { &vop_close_desc,      (vnodeopv_entry_t)ntfs_close },
852         { &vop_open_desc,       (vnodeopv_entry_t)ntfs_open },
853         { &vop_readdir_desc,    (vnodeopv_entry_t)ntfs_readdir },
854         { &vop_fsync_desc,      (vnodeopv_entry_t)ntfs_fsync },
855
856         { &vop_bmap_desc,       (vnodeopv_entry_t)ntfs_bmap },
857         { &vop_getpages_desc,   (vnodeopv_entry_t)ntfs_getpages },
858         { &vop_putpages_desc,   (vnodeopv_entry_t)ntfs_putpages },
859         { &vop_strategy_desc,   (vnodeopv_entry_t)ntfs_strategy },
860         { &vop_bwrite_desc,     (vnodeopv_entry_t)vop_stdbwrite },
861         { &vop_read_desc,       (vnodeopv_entry_t)ntfs_read },
862         { &vop_write_desc,      (vnodeopv_entry_t)ntfs_write },
863
864         { NULL, NULL }
865 };
866