Merge from vendor branch FILE:
[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.40 2007/05/09 00:53:36 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 (struct vop_pathconf_args *);
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()
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->sd_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_rmajor = VNOVAL;
242         vap->va_rminor = VNOVAL;
243         vap->va_size = fp->f_size;
244         vap->va_bytes = fp->f_allocated;
245         vap->va_atime = ntfs_nttimetounix(fp->f_times.t_access);
246         vap->va_mtime = ntfs_nttimetounix(fp->f_times.t_write);
247         vap->va_ctime = ntfs_nttimetounix(fp->f_times.t_create);
248         vap->va_flags = ip->i_flag;
249         vap->va_gen = 0;
250         vap->va_blocksize = ip->i_mp->ntm_spc * ip->i_mp->ntm_bps;
251         vap->va_type = vp->v_type;
252         vap->va_filerev = 0;
253         return (0);
254 }
255
256
257 /*
258  * Last reference to an ntnode.  If necessary, write or delete it.
259  *
260  * ntfs_inactive(struct vnode *a_vp)
261  */
262 int
263 ntfs_inactive(struct vop_inactive_args *ap)
264 {
265         struct vnode *vp = ap->a_vp;
266 #ifdef NTFS_DEBUG
267         struct ntnode *ip = VTONT(vp);
268 #endif
269
270         dprintf(("ntfs_inactive: vnode: %p, ntnode: %d\n", vp, ip->i_number));
271
272         if (ntfs_prtactive && vp->v_sysref.refcnt > 1)
273                 vprint("ntfs_inactive: pushing active", vp);
274
275         /*
276          * XXX since we don't support any filesystem changes
277          * right now, nothing more needs to be done
278          */
279         return (0);
280 }
281
282 /*
283  * Reclaim an fnode/ntnode so that it can be used for other purposes.
284  *
285  * ntfs_reclaim(struct vnode *a_vp)
286  */
287 int
288 ntfs_reclaim(struct vop_reclaim_args *ap)
289 {
290         struct vnode *vp = ap->a_vp;
291         struct fnode *fp = VTOF(vp);
292         struct ntnode *ip = FTONT(fp);
293         int error;
294
295         dprintf(("ntfs_reclaim: vnode: %p, ntnode: %d\n", vp, ip->i_number));
296
297         if (ntfs_prtactive && vp->v_sysref.refcnt > 1)
298                 vprint("ntfs_reclaim: pushing active", vp);
299
300         if ((error = ntfs_ntget(ip)) != 0)
301                 return (error);
302         
303         ntfs_frele(fp);
304         ntfs_ntput(ip);
305         vp->v_data = NULL;
306
307         return (0);
308 }
309
310 /*
311  * ntfs_print(struct vnode *a_vp)
312  */
313 static int
314 ntfs_print(struct vop_print_args *ap)
315 {
316         return (0);
317 }
318
319 /*
320  * Calculate the logical to physical mapping if not done already,
321  * then call the device strategy routine.
322  *
323  * ntfs_strategy(struct vnode *a_vp, struct bio *a_bio)
324  */
325 int
326 ntfs_strategy(struct vop_strategy_args *ap)
327 {
328         struct bio *bio = ap->a_bio;
329         struct buf *bp = bio->bio_buf;
330         struct vnode *vp = ap->a_vp;
331         struct fnode *fp = VTOF(vp);
332         struct ntnode *ip = FTONT(fp);
333         struct ntfsmount *ntmp = ip->i_mp;
334         u_int32_t toread;
335         u_int32_t towrite;
336         size_t tmp;
337         int error;
338
339         dprintf(("ntfs_strategy: loffset: %lld, doffset: %lld\n",
340                 bp->b_loffset, bio->bio_offset));
341
342         dprintf(("strategy: bcount: %d flags: 0x%lx\n", 
343                 (u_int32_t)bp->b_bcount,bp->b_flags));
344
345         bp->b_error = 0;
346
347         switch(bp->b_cmd) {
348         case BUF_CMD_READ:
349                 if (bio->bio_offset >= fp->f_size) {
350                         clrbuf(bp);
351                         error = 0;
352                 } else {
353                         toread = min(bp->b_bcount,
354                                  fp->f_size - bio->bio_offset);
355                         dprintf(("ntfs_strategy: toread: %d, fsize: %d\n",
356                                 toread,(u_int32_t)fp->f_size));
357
358                         error = ntfs_readattr(ntmp, ip, fp->f_attrtype,
359                                 fp->f_attrname, bio->bio_offset,
360                                 toread, bp->b_data, NULL);
361
362                         if (error) {
363                                 kprintf("ntfs_strategy: ntfs_readattr failed\n");
364                                 bp->b_error = error;
365                                 bp->b_flags |= B_ERROR;
366                         }
367
368                         bzero(bp->b_data + toread, bp->b_bcount - toread);
369                 }
370                 break;
371         case BUF_CMD_WRITE:
372                 if (bio->bio_offset + bp->b_bcount >= fp->f_size) {
373                         kprintf("ntfs_strategy: CAN'T EXTEND FILE\n");
374                         bp->b_error = error = EFBIG;
375                         bp->b_flags |= B_ERROR;
376                 } else {
377                         towrite = min(bp->b_bcount,
378                                       fp->f_size - bio->bio_offset);
379                         dprintf(("ntfs_strategy: towrite: %d, fsize: %d\n",
380                                 towrite,(u_int32_t)fp->f_size));
381
382                         error = ntfs_writeattr_plain(ntmp, ip, fp->f_attrtype,  
383                                 fp->f_attrname, bio->bio_offset,towrite,
384                                 bp->b_data, &tmp, NULL);
385
386                         if (error) {
387                                 kprintf("ntfs_strategy: ntfs_writeattr fail\n");
388                                 bp->b_error = error;
389                                 bp->b_flags |= B_ERROR;
390                         }
391                 }
392                 break;
393         default:
394                 panic("ntfs: bad b_cmd %d\n", bp->b_cmd);
395         }
396         biodone(bio);
397         return (error);
398 }
399
400 /*
401  * ntfs_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
402  *            struct ucred *a_cred)
403  */
404 static int
405 ntfs_write(struct vop_write_args *ap)
406 {
407         struct vnode *vp = ap->a_vp;
408         struct fnode *fp = VTOF(vp);
409         struct ntnode *ip = FTONT(fp);
410         struct uio *uio = ap->a_uio;
411         struct ntfsmount *ntmp = ip->i_mp;
412         u_int64_t towrite;
413         size_t written;
414         int error;
415
416         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));
417         dprintf(("ntfs_write: filesize: %d",(u_int32_t)fp->f_size));
418
419         if (uio->uio_resid + uio->uio_offset > fp->f_size) {
420                 kprintf("ntfs_write: CAN'T WRITE BEYOND END OF FILE\n");
421                 return (EFBIG);
422         }
423
424         towrite = min(uio->uio_resid, fp->f_size - uio->uio_offset);
425
426         dprintf((", towrite: %d\n",(u_int32_t)towrite));
427
428         error = ntfs_writeattr_plain(ntmp, ip, fp->f_attrtype,
429                 fp->f_attrname, uio->uio_offset, towrite, NULL, &written, uio);
430 #ifdef NTFS_DEBUG
431         if (error)
432                 kprintf("ntfs_write: ntfs_writeattr failed: %d\n", error);
433 #endif
434
435         return (error);
436 }
437
438 /*
439  * ntfs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
440  *              struct thread *a_td)
441  */
442 int
443 ntfs_access(struct vop_access_args *ap)
444 {
445         struct vnode *vp = ap->a_vp;
446         struct ntnode *ip = VTONT(vp);
447         struct ucred *cred = ap->a_cred;
448         mode_t mask, mode = ap->a_mode;
449         gid_t *gp;
450         int i;
451 #ifdef QUOTA
452         int error;
453 #endif
454
455         dprintf(("ntfs_access: %d\n",ip->i_number));
456
457         /*
458          * Disallow write attempts on read-only file systems;
459          * unless the file is a socket, fifo, or a block or
460          * character device resident on the file system.
461          */
462         if (mode & VWRITE) {
463                 switch ((int)vp->v_type) {
464                 case VDIR:
465                 case VLNK:
466                 case VREG:
467                         if (vp->v_mount->mnt_flag & MNT_RDONLY)
468                                 return (EROFS);
469 #ifdef QUOTA
470                         if (error = getinoquota(ip))
471                                 return (error);
472 #endif
473                         break;
474                 }
475         }
476
477         /* Otherwise, user id 0 always gets access. */
478         if (cred->cr_uid == 0)
479                 return (0);
480
481         mask = 0;
482
483         /* Otherwise, check the owner. */
484         if (cred->cr_uid == ip->i_mp->ntm_uid) {
485                 if (mode & VEXEC)
486                         mask |= S_IXUSR;
487                 if (mode & VREAD)
488                         mask |= S_IRUSR;
489                 if (mode & VWRITE)
490                         mask |= S_IWUSR;
491                 return ((ip->i_mp->ntm_mode & mask) == mask ? 0 : EACCES);
492         }
493
494         /* Otherwise, check the groups. */
495         for (i = 0, gp = cred->cr_groups; i < cred->cr_ngroups; i++, gp++)
496                 if (ip->i_mp->ntm_gid == *gp) {
497                         if (mode & VEXEC)
498                                 mask |= S_IXGRP;
499                         if (mode & VREAD)
500                                 mask |= S_IRGRP;
501                         if (mode & VWRITE)
502                                 mask |= S_IWGRP;
503                         return ((ip->i_mp->ntm_mode&mask) == mask ? 0 : EACCES);
504                 }
505
506         /* Otherwise, check everyone else. */
507         if (mode & VEXEC)
508                 mask |= S_IXOTH;
509         if (mode & VREAD)
510                 mask |= S_IROTH;
511         if (mode & VWRITE)
512                 mask |= S_IWOTH;
513         return ((ip->i_mp->ntm_mode & mask) == mask ? 0 : EACCES);
514 }
515
516 /*
517  * Open called.
518  *
519  * Nothing to do.
520  *
521  * ntfs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
522  *           struct thread *a_td)
523  */
524 /* ARGSUSED */
525 static int
526 ntfs_open(struct vop_open_args *ap)
527 {
528         return (vop_stdopen(ap));
529 }
530
531 /*
532  * Close called.
533  *
534  * Update the times on the inode.
535  *
536  * ntfs_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred,
537  *            struct thread *a_td)
538  */
539 /* ARGSUSED */
540 static int
541 ntfs_close(struct vop_close_args *ap)
542 {
543 #if NTFS_DEBUG
544         struct vnode *vp = ap->a_vp;
545         struct ntnode *ip = VTONT(vp);
546
547         kprintf("ntfs_close: %d\n",ip->i_number);
548 #endif
549
550         return (vop_stdclose(ap));
551 }
552
553 /*
554  * ntfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred,
555  *              int *a_ncookies, u_int **cookies)
556  */
557 int
558 ntfs_readdir(struct vop_readdir_args *ap)
559 {
560         struct vnode *vp = ap->a_vp;
561         struct fnode *fp = VTOF(vp);
562         struct ntnode *ip = FTONT(fp);
563         struct uio *uio = ap->a_uio;
564         struct ntfsmount *ntmp = ip->i_mp;
565         int i, error = 0;
566         u_int32_t faked = 0, num, off;
567         int ncookies = 0;
568         char convname[NTFS_MAXFILENAME + 1];
569
570         dprintf(("ntfs_readdir %d off: %d resid: %d\n",ip->i_number,(u_int32_t)uio->uio_offset,uio->uio_resid));
571
572         if (uio->uio_offset < 0 || uio->uio_offset > INT_MAX)
573                 return (EINVAL);
574         if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0)
575                 return (error);
576
577         /*
578          * uio->uio_offset carries the number of the entry
579          * where we should start returning dirents.
580          *
581          * We fake up "." if we're not reading the FS root
582          * and we always fake up "..".
583          *
584          * off contains the entry we are starting at,
585          * num increments while we are reading.
586          */
587
588         off = num = uio->uio_offset;
589         faked = (ip->i_number == NTFS_ROOTINO) ? 1 : 2;
590
591         /* Simulate . in every dir except ROOT */
592         if (ip->i_number != NTFS_ROOTINO && num == 0) {
593                 if (vop_write_dirent(&error, uio, ip->i_number,
594                     DT_DIR, 1, "."))
595                         goto done;
596                 if (error)
597                         goto done;
598
599                 num++;
600                 ncookies++;
601         }
602
603         /* Simulate .. in every dir including ROOT */
604         if (num == faked - 1) {
605                 /* XXX NTFS_ROOTINO seems to be wrong here */
606                 if (vop_write_dirent(&error, uio, NTFS_ROOTINO,
607                     DT_DIR, 2, ".."))
608                         goto readdone;
609                 if (error)
610                         goto done;
611
612                 num++;
613                 ncookies++;
614         }
615
616         for (;;) {
617                 struct attr_indexentry *iep;
618
619                 /*
620                  * num is the number of the entry we will return,
621                  * but ntfs_ntreaddir takes the entry number of the
622                  * ntfs directory listing, so subtract the faked
623                  * . and .. entries.
624                  */
625                 error = ntfs_ntreaddir(ntmp, fp, num - faked, &iep);
626
627                 if (error)
628                         goto done;
629
630                 if( NULL == iep )
631                         break;
632
633                 for (; !(iep->ie_flag & NTFS_IEFLAG_LAST);
634                         iep = NTFS_NEXTREC(iep, struct attr_indexentry *))
635                 {
636                         if(!ntfs_isnamepermitted(ntmp,iep))
637                                 continue;
638
639                         for (i=0; i < iep->ie_fnamelen; i++)
640                                 convname[i] = NTFS_U28(iep->ie_fname[i]);
641                         convname[i] = '\0';
642
643                         if (vop_write_dirent(&error, uio, iep->ie_number,
644                             (iep->ie_fflag & NTFS_FFLAG_DIR) ? DT_DIR : DT_REG,
645                             iep->ie_fnamelen, convname))
646                                 goto readdone;
647
648                         dprintf(("ntfs_readdir: elem: %d, fname:[%s] type: %d, "
649                                  "flag: %d, %s\n",
650                                  ncookies, convname, iep->ie_fnametype,
651                                  iep->ie_flag,
652                                  (iep->ie_fflag & NTFS_FFLAG_DIR) ?
653                                         "dir" : "reg"));
654
655                         if (error)
656                                 goto done;
657
658                         ncookies++;
659                         num++;
660                 }
661         }
662
663 readdone:
664         uio->uio_offset = num;
665
666         dprintf(("ntfs_readdir: %d entries (%d bytes) read\n",
667                 ncookies,(u_int)(uio->uio_offset - off)));
668         dprintf(("ntfs_readdir: off: %d resid: %d\n",
669                 (u_int32_t)uio->uio_offset,uio->uio_resid));
670
671         if (!error && ap->a_ncookies != NULL) {
672 #if defined(__DragonFly__)
673                 u_long *cookies;
674                 u_long *cookiep;
675 #else /* defined(__NetBSD__) */
676                 off_t *cookies;
677                 off_t *cookiep;
678 #endif
679
680                 ddprintf(("ntfs_readdir: %d cookies\n",ncookies));
681                 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
682                         panic("ntfs_readdir: unexpected uio from NFS server");
683 #if defined(__DragonFly__)
684                 MALLOC(cookies, u_long *, ncookies * sizeof(u_long),
685                        M_TEMP, M_WAITOK);
686 #else /* defined(__NetBSD__) */
687                 MALLOC(cookies, off_t *, ncookies * sizeof(off_t),
688                        M_TEMP, M_WAITOK);
689 #endif
690                 cookiep = cookies;
691                 while (off < num)
692                         *cookiep++ = ++off;
693
694                 *ap->a_ncookies = ncookies;
695                 *ap->a_cookies = cookies;
696         }
697 /*
698         if (ap->a_eofflag)
699             *ap->a_eofflag = VTONT(vp)->i_size <= uio->uio_offset;
700 */
701 done:
702         vn_unlock(vp);
703         return (error);
704 }
705
706 /*
707  * ntfs_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
708  *              struct componentname *a_cnp)
709  */
710 int
711 ntfs_lookup(struct vop_old_lookup_args *ap)
712 {
713         struct vnode *dvp = ap->a_dvp;
714         struct ntnode *dip = VTONT(dvp);
715         struct ntfsmount *ntmp = dip->i_mp;
716         struct componentname *cnp = ap->a_cnp;
717         int error;
718         int lockparent = cnp->cn_flags & CNP_LOCKPARENT;
719 #if NTFS_DEBUG
720         int wantparent = cnp->cn_flags & (CNP_LOCKPARENT | CNP_WANTPARENT);
721 #endif
722         dprintf(("ntfs_lookup: \"%.*s\" (%ld bytes) in %d, lp: %d, wp: %d \n",
723                 (int)cnp->cn_namelen, cnp->cn_nameptr, cnp->cn_namelen,
724                 dip->i_number, lockparent, wantparent));
725
726         *ap->a_vpp = NULL;
727
728         if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
729                 dprintf(("ntfs_lookup: faking . directory in %d\n",
730                         dip->i_number));
731
732                 vref(dvp);
733                 *ap->a_vpp = dvp;
734                 error = 0;
735         } else if (cnp->cn_flags & CNP_ISDOTDOT) {
736                 struct ntvattr *vap;
737
738                 dprintf(("ntfs_lookup: faking .. directory in %d\n",
739                          dip->i_number));
740
741                 error = ntfs_ntvattrget(ntmp, dip, NTFS_A_NAME, NULL, 0, &vap);
742                 if(error)
743                         return (error);
744
745                 VOP__UNLOCK(dvp, 0);
746                 cnp->cn_flags |= CNP_PDIRUNLOCK;
747
748                 dprintf(("ntfs_lookup: parentdir: %d\n",
749                          vap->va_a_name->n_pnumber));
750                 error = VFS_VGET(ntmp->ntm_mountp,
751                                  vap->va_a_name->n_pnumber,ap->a_vpp); 
752                 ntfs_ntvattrrele(vap);
753                 if (error) {
754                         if (VN_LOCK(dvp, LK_EXCLUSIVE | LK_RETRY) == 0)
755                                 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
756                         return (error);
757                 }
758
759                 if (lockparent) {
760                         error = VN_LOCK(dvp, LK_EXCLUSIVE);
761                         if (error) {
762                                 vput(*ap->a_vpp);
763                                 *ap->a_vpp = NULL;
764                                 return (error);
765                         }
766                         cnp->cn_flags &= ~CNP_PDIRUNLOCK;
767                 }
768         } else {
769                 error = ntfs_ntlookupfile(ntmp, dvp, cnp, ap->a_vpp);
770                 if (error) {
771                         dprintf(("ntfs_ntlookupfile: returned %d\n", error));
772                         return (error);
773                 }
774
775                 dprintf(("ntfs_lookup: found ino: %d\n", 
776                         VTONT(*ap->a_vpp)->i_number));
777
778                 if (!lockparent)
779                         VOP__UNLOCK(dvp, 0);
780         }
781         return (error);
782 }
783
784 #if defined(__DragonFly__)
785 /*
786  * Flush the blocks of a file to disk.
787  *
788  * This function is worthless for vnodes that represent directories. Maybe we
789  * could just do a sync if they try an fsync on a directory file.
790  *
791  * ntfs_fsync(struct vnode *a_vp, struct ucred *a_cred, int a_waitfor,
792  *            struct thread *a_td)
793  */
794 static int
795 ntfs_fsync(struct vop_fsync_args *ap)
796 {
797         return (0);
798 }
799 #endif
800
801 /*
802  * Return POSIX pathconf information applicable to NTFS filesystem
803  */
804 int
805 ntfs_pathconf(struct vop_pathconf_args *ap)
806 {
807         switch (ap->a_name) {
808         case _PC_LINK_MAX:
809                 *ap->a_retval = 1;
810                 return (0);
811         case _PC_NAME_MAX:
812                 *ap->a_retval = NTFS_MAXFILENAME;
813                 return (0);
814         case _PC_PATH_MAX:
815                 *ap->a_retval = PATH_MAX;
816                 return (0);
817         case _PC_CHOWN_RESTRICTED:
818                 *ap->a_retval = 1;
819                 return (0);
820         case _PC_NO_TRUNC:
821                 *ap->a_retval = 0;
822                 return (0);
823 #if defined(__NetBSD__)
824         case _PC_SYNC_IO:
825                 *ap->a_retval = 1;
826                 return (0);
827         case _PC_FILESIZEBITS:
828                 *ap->a_retval = 64;
829                 return (0);
830 #endif
831         default:
832                 return (EINVAL);
833         }
834         /* NOTREACHED */
835 }
836
837 /*
838  * Global vfs data structures
839  */
840 struct vop_ops ntfs_vnode_vops = {
841         .vop_default =          vop_defaultop,
842         .vop_getattr =          ntfs_getattr,
843         .vop_inactive =         ntfs_inactive,
844         .vop_reclaim =          ntfs_reclaim,
845         .vop_print =            ntfs_print,
846         .vop_pathconf =         ntfs_pathconf,
847         .vop_old_lookup =       ntfs_lookup,
848         .vop_access =           ntfs_access,
849         .vop_close =            ntfs_close,
850         .vop_open =             ntfs_open,
851         .vop_readdir =          ntfs_readdir,
852         .vop_fsync =            ntfs_fsync,
853         .vop_bmap =             ntfs_bmap,
854         .vop_getpages =         ntfs_getpages,
855         .vop_putpages =         ntfs_putpages,
856         .vop_strategy =         ntfs_strategy,
857         .vop_read =             ntfs_read,
858         .vop_write =            ntfs_write
859 };
860