Merge from vendor branch NTPD:
[dragonfly.git] / sys / vfs / ntfs / ntfs_vfsops.c
1 /*      $NetBSD: ntfs_vfsops.c,v 1.23 1999/11/15 19:38:14 jdolecek Exp $        */
2
3 /*-
4  * Copyright (c) 1998, 1999 Semen Ustimenko
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD: src/sys/ntfs/ntfs_vfsops.c,v 1.20.2.5 2001/12/25 01:44:45 dillon Exp $
29  * $DragonFly: src/sys/vfs/ntfs/ntfs_vfsops.c,v 1.23 2004/10/12 19:21:02 dillon Exp $
30  */
31
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/conf.h>
36 #include <sys/proc.h>
37 #include <sys/namei.h>
38 #include <sys/kernel.h>
39 #include <sys/vnode.h>
40 #include <sys/mount.h>
41 #include <sys/buf.h>
42 #include <sys/fcntl.h>
43 #include <sys/malloc.h>
44 #include <sys/systm.h>
45 #if defined(__NetBSD__)
46 #include <sys/device.h>
47 #endif
48
49 #include <vm/vm.h>
50 #include <vm/vm_param.h>
51 #if defined(__NetBSD__)
52 #include <vm/vm_prot.h>
53 #endif
54 #include <vm/vm_page.h>
55 #include <vm/vm_object.h>
56 #include <vm/vm_extern.h>
57 #include <vm/vm_zone.h>
58
59 #if defined(__NetBSD__)
60 #include <miscfs/specfs/specdev.h>
61 #endif
62
63 /*#define NTFS_DEBUG 1*/
64 #include "ntfs.h"
65 #include "ntfs_inode.h"
66 #include "ntfs_subr.h"
67 #include "ntfs_vfsops.h"
68 #include "ntfs_ihash.h"
69 #include "ntfsmount.h"
70
71 extern struct vnodeopv_entry_desc ntfs_vnodeop_entries[];
72
73 #if defined(__DragonFly__)
74 MALLOC_DEFINE(M_NTFSMNT, "NTFS mount", "NTFS mount structure");
75 MALLOC_DEFINE(M_NTFSNTNODE,"NTFS ntnode",  "NTFS ntnode information");
76 MALLOC_DEFINE(M_NTFSFNODE,"NTFS fnode",  "NTFS fnode information");
77 MALLOC_DEFINE(M_NTFSDIR,"NTFS dir",  "NTFS dir buffer");
78 #endif
79
80 static int      ntfs_root (struct mount *, struct vnode **);
81 static int      ntfs_statfs (struct mount *, struct statfs *,
82                                  struct thread *);
83 static int      ntfs_unmount (struct mount *, int, struct thread *);
84 static int      ntfs_vget (struct mount *mp, ino_t ino,
85                                struct vnode **vpp);
86 static int      ntfs_mountfs (struct vnode *, struct mount *, 
87                                   struct ntfs_args *, struct thread *);
88 static int      ntfs_vptofh (struct vnode *, struct fid *);
89 static int      ntfs_fhtovp (struct mount *, struct fid *,
90                                  struct vnode **);
91
92 #if !defined (__DragonFly__)
93 static int      ntfs_quotactl (struct mount *, int, uid_t, caddr_t,
94                                    struct thread *);
95 static int      ntfs_start (struct mount *, int, struct thread *);
96 static int      ntfs_sync (struct mount *, int, struct ucred *,
97                                struct thread *);
98 #endif
99
100 #if defined(__DragonFly__)
101 struct sockaddr;
102 static int      ntfs_mount (struct mount *, char *, caddr_t, struct thread *);
103 static int      ntfs_init (struct vfsconf *);
104 static int      ntfs_checkexp (struct mount *, struct sockaddr *,
105                                    int *, struct ucred **);
106 #elif defined(__NetBSD__)
107 static int      ntfs_mount (struct mount *, const char *, void *,
108                                 struct nameidata *, struct thread *);
109 static void     ntfs_init (void);
110 static int      ntfs_mountroot (void);
111 static int      ntfs_sysctl (int *, u_int, void *, size_t *, void *,
112                                  size_t, struct thread *);
113 static int      ntfs_checkexp (struct mount *, struct mbuf *,
114                                    int *, struct ucred **);
115 #endif
116
117 /*
118  * Verify a remote client has export rights and return these rights via.
119  * exflagsp and credanonp.
120  */
121 static int
122 ntfs_checkexp(struct mount *mp,
123 #if defined(__DragonFly__)
124               struct sockaddr *nam,
125 #else /* defined(__NetBSD__) */
126               struct mbuf *nam,
127 #endif
128               int *exflagsp, struct ucred **credanonp)
129 {
130         struct netcred *np;
131         struct ntfsmount *ntm = VFSTONTFS(mp);
132
133         /*
134          * Get the export permission structure for this <mp, client> tuple.
135          */
136         np = vfs_export_lookup(mp, &ntm->ntm_export, nam);
137         if (np == NULL)
138                 return (EACCES);
139
140         *exflagsp = np->netc_exflags;
141         *credanonp = &np->netc_anon;
142         return (0);
143 }
144
145 #if defined(__NetBSD__)
146 /*ARGSUSED*/
147 static int
148 ntfs_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
149             size_t newlen, struct thread *td)
150 {
151         return (EINVAL);
152 }
153
154 static int
155 ntfs_mountroot(void)
156 {
157         struct mount *mp;
158         struct vnode *rootvp;
159         struct thread *td = curthread;  /* XXX */
160         struct ntfs_args args;
161         lwkt_tokref ilock;
162         int error;
163
164         if (root_device->dv_class != DV_DISK)
165                 return (ENODEV);
166
167         /*
168          * Get vnodes for rootdev.
169          */
170         if (bdevvp(rootdev, &rootvp))
171                 panic("ntfs_mountroot: can't setup rootvp");
172
173         if ((error = vfs_rootmountalloc(MOUNT_NTFS, "root_device", &mp))) {
174                 vrele(rootvp);
175                 return (error);
176         }
177
178         args.flag = 0;
179         args.uid = 0;
180         args.gid = 0;
181         args.mode = 0777;
182
183         if ((error = ntfs_mountfs(rootvp, mp, &args, td)) != 0) {
184                 mp->mnt_op->vfs_refcount--;
185                 vfs_unbusy(mp);
186                 free(mp, M_MOUNT);
187                 vrele(rootvp);
188                 return (error);
189         }
190
191         lwkt_gettoken(&ilock, &mountlist_token);
192         CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
193         lwkt_reltoken(&ilock);
194         (void)ntfs_statfs(mp, &mp->mnt_stat, td);
195         vfs_unbusy(mp);
196         return (0);
197 }
198
199 static void
200 ntfs_init(void)
201 {
202         ntfs_nthashinit();
203         ntfs_toupper_init();
204 }
205
206 #elif defined(__DragonFly__)
207
208 static int
209 ntfs_init(struct vfsconf *vcp)
210 {
211         ntfs_nthashinit();
212         ntfs_toupper_init();
213         return 0;
214 }
215
216 #endif /* NetBSD */
217
218 static int
219 ntfs_mount(struct mount *mp,
220 #if defined(__DragonFly__)
221            char *path, caddr_t data,
222 #else
223            const char *path, void *data,
224 #endif
225            struct thread *td)
226 {
227         size_t          size;
228         int             err = 0;
229         struct vnode    *devvp;
230         struct ntfs_args args;
231         struct nameidata nd;
232         struct vnode *rootvp;
233
234 #ifdef __DragonFly__
235         /*
236          * Use NULL path to flag a root mount
237          */
238         if( path == NULL) {
239                 /*
240                  ***
241                  * Mounting root file system
242                  ***
243                  */
244         
245                 /* Get vnode for root device*/
246                 if( bdevvp( rootdev, &rootvp))
247                         panic("ffs_mountroot: can't setup bdevvp for root");
248
249                 /*
250                  * FS specific handling
251                  */
252                 mp->mnt_flag |= MNT_RDONLY;     /* XXX globally applicable?*/
253
254                 /*
255                  * Attempt mount
256                  */
257                 if( ( err = ntfs_mountfs(rootvp, mp, &args, td)) != 0) {
258                         /* fs specific cleanup (if any)*/
259                         goto error_1;
260                 }
261
262                 goto dostatfs;          /* success*/
263
264         }
265 #endif /* FreeBSD */
266
267         /*
268          ***
269          * Mounting non-root file system or updating a file system
270          ***
271          */
272
273         /* copy in user arguments*/
274         err = copyin(data, (caddr_t)&args, sizeof (struct ntfs_args));
275         if (err)
276                 goto error_1;           /* can't get arguments*/
277
278         /*
279          * If updating, check whether changing from read-only to
280          * read/write; if there is no device name, that's all we do.
281          */
282         if (mp->mnt_flag & MNT_UPDATE) {
283                 /* if not updating name...*/
284                 if (args.fspec == 0) {
285                         /*
286                          * Process export requests.  Jumping to "success"
287                          * will return the vfs_export() error code.
288                          */
289                         struct ntfsmount *ntm = VFSTONTFS(mp);
290                         err = vfs_export(mp, &ntm->ntm_export, &args.export);
291                         goto success;
292                 }
293
294                 printf("ntfs_mount(): MNT_UPDATE not supported\n");
295                 err = EINVAL;
296                 goto error_1;
297         }
298
299         /*
300          * Not an update, or updating the name: look up the name
301          * and verify that it refers to a sensible block device.
302          */
303         NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, args.fspec, td);
304         err = namei(&nd);
305         if (err) {
306                 /* can't get devvp!*/
307                 goto error_1;
308         }
309         NDFREE(&nd, NDF_ONLY_PNBUF);
310         devvp = nd.ni_vp;
311
312 #if defined(__DragonFly__)
313         if (!vn_isdisk(devvp, &err)) 
314                 goto error_2;
315 #else
316         if (devvp->v_type != VBLK) {
317                 err = ENOTBLK;
318                 goto error_2;
319         }
320         if (umajor(devvp->v_udev) >= nblkdev) {
321                 err = ENXIO;
322                 goto error_2;
323         }
324 #endif
325         if (mp->mnt_flag & MNT_UPDATE) {
326 #if 0
327                 /*
328                  ********************
329                  * UPDATE
330                  ********************
331                  */
332
333                 if (devvp != ntmp->um_devvp)
334                         err = EINVAL;   /* needs translation */
335                 else
336                         vrele(devvp);
337                 /*
338                  * Update device name only on success
339                  */
340                 if( !err) {
341                         /* Save "mounted from" info for mount point (NULL pad)*/
342                         copyinstr(      args.fspec,
343                                         mp->mnt_stat.f_mntfromname,
344                                         MNAMELEN - 1,
345                                         &size);
346                         bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
347                 }
348 #endif
349         } else {
350                 /*
351                  ********************
352                  * NEW MOUNT
353                  ********************
354                  */
355
356                 /*
357                  * Since this is a new mount, we want the names for
358                  * the device and the mount point copied in.  If an
359                  * error occurs,  the mountpoint is discarded by the
360                  * upper level code.
361                  */
362                 /* Save "last mounted on" info for mount point (NULL pad)*/
363                 copyinstr(      path,                           /* mount point*/
364                                 mp->mnt_stat.f_mntonname,       /* save area*/
365                                 MNAMELEN - 1,                   /* max size*/
366                                 &size);                         /* real size*/
367                 bzero( mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
368
369                 /* Save "mounted from" info for mount point (NULL pad)*/
370                 copyinstr(      args.fspec,                     /* device name*/
371                                 mp->mnt_stat.f_mntfromname,     /* save area*/
372                                 MNAMELEN - 1,                   /* max size*/
373                                 &size);                         /* real size*/
374                 bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
375
376                 err = ntfs_mountfs(devvp, mp, &args, td);
377         }
378         if (err) {
379                 goto error_2;
380         }
381
382 #ifdef __DragonFly__
383 dostatfs:
384 #endif
385         /*
386          * Initialize FS stat information in mount struct; uses both
387          * mp->mnt_stat.f_mntonname and mp->mnt_stat.f_mntfromname
388          *
389          * This code is common to root and non-root mounts
390          */
391         (void)VFS_STATFS(mp, &mp->mnt_stat, td);
392
393         goto success;
394
395
396 error_2:        /* error with devvp held*/
397
398         /* release devvp before failing*/
399         vrele(devvp);
400
401 error_1:        /* no state to back out*/
402
403 success:
404         return(err);
405 }
406
407 /*
408  * Common code for mount and mountroot
409  */
410 int
411 ntfs_mountfs(struct vnode *devvp, struct mount *mp, struct ntfs_args *argsp,
412              struct thread *td)
413 {
414         struct buf *bp;
415         struct ntfsmount *ntmp;
416         dev_t dev;
417         int error, ronly, ncount, i;
418         struct vnode *vp;
419         struct ucred *cred;
420
421         KKASSERT(td->td_proc);
422         cred = td->td_proc->p_ucred;
423
424         /*
425          * Disallow multiple mounts of the same device.
426          * Disallow mounting of a device that is currently in use
427          * (except for root, which might share swap device for miniroot).
428          * Flush out any old buffers remaining from a previous use.
429          */
430         error = vfs_mountedon(devvp);
431         if (error)
432                 return (error);
433         ncount = count_udev(devvp->v_udev);
434 #if defined(__DragonFly__)
435         if (devvp->v_object)
436                 ncount -= 1;
437 #endif
438         if (ncount > 1)
439                 return (EBUSY);
440 #if defined(__DragonFly__)
441         VN_LOCK(devvp, LK_EXCLUSIVE | LK_RETRY, td);
442         error = vinvalbuf(devvp, V_SAVE, td, 0, 0);
443         VOP__UNLOCK(devvp, 0, td);
444 #else
445         error = vinvalbuf(devvp, V_SAVE, td, 0, 0);
446 #endif
447         if (error)
448                 return (error);
449
450         ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
451         VN_LOCK(devvp, LK_EXCLUSIVE | LK_RETRY, td);
452         error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, td);
453         VOP__UNLOCK(devvp, 0, td);
454         if (error)
455                 return (error);
456         dev = devvp->v_rdev;
457
458         bp = NULL;
459
460         error = bread(devvp, BBLOCK, BBSIZE, &bp);
461         if (error)
462                 goto out;
463         ntmp = malloc( sizeof *ntmp, M_NTFSMNT, M_WAITOK );
464         bzero( ntmp, sizeof *ntmp );
465         bcopy( bp->b_data, &ntmp->ntm_bootfile, sizeof(struct bootfile) );
466         brelse( bp );
467         bp = NULL;
468
469         if (strncmp(ntmp->ntm_bootfile.bf_sysid, NTFS_BBID, NTFS_BBIDLEN)) {
470                 error = EINVAL;
471                 dprintf(("ntfs_mountfs: invalid boot block\n"));
472                 goto out;
473         }
474
475         {
476                 int8_t cpr = ntmp->ntm_mftrecsz;
477                 if( cpr > 0 )
478                         ntmp->ntm_bpmftrec = ntmp->ntm_spc * cpr;
479                 else
480                         ntmp->ntm_bpmftrec = (1 << (-cpr)) / ntmp->ntm_bps;
481         }
482         dprintf(("ntfs_mountfs(): bps: %d, spc: %d, media: %x, mftrecsz: %d (%d sects)\n",
483                 ntmp->ntm_bps,ntmp->ntm_spc,ntmp->ntm_bootfile.bf_media,
484                 ntmp->ntm_mftrecsz,ntmp->ntm_bpmftrec));
485         dprintf(("ntfs_mountfs(): mftcn: 0x%x|0x%x\n",
486                 (u_int32_t)ntmp->ntm_mftcn,(u_int32_t)ntmp->ntm_mftmirrcn));
487
488         ntmp->ntm_mountp = mp;
489         ntmp->ntm_dev = dev;
490         ntmp->ntm_devvp = devvp;
491         ntmp->ntm_uid = argsp->uid;
492         ntmp->ntm_gid = argsp->gid;
493         ntmp->ntm_mode = argsp->mode;
494         ntmp->ntm_flag = argsp->flag;
495
496         /* Copy in the 8-bit to Unicode conversion table */
497         if (argsp->flag & NTFSMNT_U2WTABLE) {
498                 ntfs_82u_init(ntmp, argsp->u2w);
499         } else {
500                 ntfs_82u_init(ntmp, NULL);
501         }
502
503         /* Initialize Unicode to 8-bit table from 8toU table */
504         ntfs_u28_init(ntmp, ntmp->ntm_82u);
505
506         mp->mnt_data = (qaddr_t)ntmp;
507
508         dprintf(("ntfs_mountfs(): case-%s,%s uid: %d, gid: %d, mode: %o\n",
509                 (ntmp->ntm_flag & NTFS_MFLAG_CASEINS)?"insens.":"sens.",
510                 (ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES)?" allnames,":"",
511                 ntmp->ntm_uid, ntmp->ntm_gid, ntmp->ntm_mode));
512
513         vfs_add_vnodeops(&mp->mnt_vn_ops, ntfs_vnodeop_entries);
514
515         /*
516          * We read in some system nodes to do not allow 
517          * reclaim them and to have everytime access to them.
518          */ 
519         {
520                 int pi[3] = { NTFS_MFTINO, NTFS_ROOTINO, NTFS_BITMAPINO };
521                 for (i=0; i<3; i++) {
522                         error = VFS_VGET(mp, pi[i], &(ntmp->ntm_sysvn[pi[i]]));
523                         if(error)
524                                 goto out1;
525                         ntmp->ntm_sysvn[pi[i]]->v_flag |= VSYSTEM;
526                         vref(ntmp->ntm_sysvn[pi[i]]);
527                         vput(ntmp->ntm_sysvn[pi[i]]);
528                 }
529         }
530
531         /* read the Unicode lowercase --> uppercase translation table,
532          * if necessary */
533         if ((error = ntfs_toupper_use(mp, ntmp)))
534                 goto out1;
535
536         /*
537          * Scan $BitMap and count free clusters
538          */
539         error = ntfs_calccfree(ntmp, &ntmp->ntm_cfree);
540         if(error)
541                 goto out1;
542
543         /*
544          * Read and translate to internal format attribute
545          * definition file. 
546          */
547         {
548                 int num,j;
549                 struct attrdef ad;
550
551                 /* Open $AttrDef */
552                 error = VFS_VGET(mp, NTFS_ATTRDEFINO, &vp );
553                 if(error) 
554                         goto out1;
555
556                 /* Count valid entries */
557                 for(num=0;;num++) {
558                         error = ntfs_readattr(ntmp, VTONT(vp),
559                                         NTFS_A_DATA, NULL,
560                                         num * sizeof(ad), sizeof(ad),
561                                         &ad, NULL);
562                         if (error)
563                                 goto out1;
564                         if (ad.ad_name[0] == 0)
565                                 break;
566                 }
567
568                 /* Alloc memory for attribute definitions */
569                 MALLOC(ntmp->ntm_ad, struct ntvattrdef *,
570                         num * sizeof(struct ntvattrdef),
571                         M_NTFSMNT, M_WAITOK);
572
573                 ntmp->ntm_adnum = num;
574
575                 /* Read them and translate */
576                 for(i=0;i<num;i++){
577                         error = ntfs_readattr(ntmp, VTONT(vp),
578                                         NTFS_A_DATA, NULL,
579                                         i * sizeof(ad), sizeof(ad),
580                                         &ad, NULL);
581                         if (error)
582                                 goto out1;
583                         j = 0;
584                         do {
585                                 ntmp->ntm_ad[i].ad_name[j] = ad.ad_name[j];
586                         } while(ad.ad_name[j++]);
587                         ntmp->ntm_ad[i].ad_namelen = j - 1;
588                         ntmp->ntm_ad[i].ad_type = ad.ad_type;
589                 }
590
591                 vput(vp);
592         }
593
594 #if defined(__DragonFly__)
595         mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
596         mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
597 #else
598         mp->mnt_stat.f_fsid.val[0] = dev;
599         mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_NTFS);
600 #endif
601         mp->mnt_maxsymlinklen = 0;
602         mp->mnt_flag |= MNT_LOCAL;
603         dev->si_mountpoint = mp;
604
605         return (0);
606
607 out1:
608         for(i=0;i<NTFS_SYSNODESNUM;i++)
609                 if(ntmp->ntm_sysvn[i]) vrele(ntmp->ntm_sysvn[i]);
610
611         if (vflush(mp, 0, 0))
612                 dprintf(("ntfs_mountfs: vflush failed\n"));
613
614 out:
615         dev->si_mountpoint = NULL;
616         if (bp)
617                 brelse(bp);
618
619 #if defined __NetBSD__
620         /* lock the device vnode before calling VOP_CLOSE() */
621         VN_LOCK(devvp, LK_EXCLUSIVE | LK_RETRY, td);
622         (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, td);
623         VOP__UNLOCK(devvp, 0, td);
624 #else
625         (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, td);
626 #endif
627         
628         return (error);
629 }
630
631 #if !defined(__DragonFly__)
632 static int
633 ntfs_start(struct mount *mp, int flags, struct thread *td)
634 {
635         return (0);
636 }
637 #endif
638
639 static int
640 ntfs_unmount(struct mount *mp, int mntflags, struct thread *td)
641 {
642         struct ntfsmount *ntmp;
643         int error, ronly = 0, flags, i;
644
645         dprintf(("ntfs_unmount: unmounting...\n"));
646         ntmp = VFSTONTFS(mp);
647
648         flags = 0;
649         if(mntflags & MNT_FORCE)
650                 flags |= FORCECLOSE;
651
652         dprintf(("ntfs_unmount: vflushing...\n"));
653         error = vflush(mp, 0, flags | SKIPSYSTEM);
654         if (error) {
655                 printf("ntfs_unmount: vflush failed: %d\n",error);
656                 return (error);
657         }
658
659         /* Check if only system vnodes are rest */
660         for(i=0;i<NTFS_SYSNODESNUM;i++)
661                  if((ntmp->ntm_sysvn[i]) && 
662                     (ntmp->ntm_sysvn[i]->v_usecount > 1)) return (EBUSY);
663
664         /* Dereference all system vnodes */
665         for(i=0;i<NTFS_SYSNODESNUM;i++)
666                  if(ntmp->ntm_sysvn[i]) vrele(ntmp->ntm_sysvn[i]);
667
668         /* vflush system vnodes */
669         error = vflush(mp, 0, flags);
670         if (error)
671                 printf("ntfs_unmount: vflush failed(sysnodes): %d\n",error);
672
673         /* Check if the type of device node isn't VBAD before
674          * touching v_specinfo.  If the device vnode is revoked, the
675          * field is NULL and touching it causes null pointer derefercence.
676          */
677         if (ntmp->ntm_devvp->v_type != VBAD)
678                 ntmp->ntm_devvp->v_rdev->si_mountpoint = NULL;
679
680         vinvalbuf(ntmp->ntm_devvp, V_SAVE, td, 0, 0);
681
682 #if defined(__NetBSD__)
683         /* lock the device vnode before calling VOP_CLOSE() */
684         VOP_LOCK(ntmp->ntm_devvp, LK_EXCLUSIVE | LK_RETRY);
685         error = VOP_CLOSE(ntmp->ntm_devvp, ronly ? FREAD : FREAD|FWRITE, td);
686         VOP__UNLOCK(ntmp->ntm_devvp, 0, td);
687 #else
688         error = VOP_CLOSE(ntmp->ntm_devvp, ronly ? FREAD : FREAD|FWRITE, td);
689 #endif
690
691         vrele(ntmp->ntm_devvp);
692
693         /* free the toupper table, if this has been last mounted ntfs volume */
694         ntfs_toupper_unuse();
695
696         dprintf(("ntfs_umount: freeing memory...\n"));
697         ntfs_u28_uninit(ntmp);
698         ntfs_82u_uninit(ntmp);
699         mp->mnt_data = (qaddr_t)0;
700         mp->mnt_flag &= ~MNT_LOCAL;
701         FREE(ntmp->ntm_ad, M_NTFSMNT);
702         FREE(ntmp, M_NTFSMNT);
703         return (error);
704 }
705
706 static int
707 ntfs_root(struct mount *mp, struct vnode **vpp)
708 {
709         struct vnode *nvp;
710         int error = 0;
711
712         dprintf(("ntfs_root(): sysvn: %p\n",
713                 VFSTONTFS(mp)->ntm_sysvn[NTFS_ROOTINO]));
714         error = VFS_VGET(mp, (ino_t)NTFS_ROOTINO, &nvp);
715         if(error) {
716                 printf("ntfs_root: VFS_VGET failed: %d\n",error);
717                 return (error);
718         }
719
720         *vpp = nvp;
721         return (0);
722 }
723
724 #if !defined(__DragonFly__)
725 static int
726 ntfs_quotactl(struct mount *mp, int cmds, uid_t uid, caddr_t arg,
727               struct thread *td)
728 {
729         printf("\nntfs_quotactl():\n");
730         return EOPNOTSUPP;
731 }
732 #endif
733
734 int
735 ntfs_calccfree(struct ntfsmount *ntmp, cn_t *cfreep)
736 {
737         struct vnode *vp;
738         u_int8_t *tmp;
739         int j, error;
740         long cfree = 0;
741         size_t bmsize, i;
742
743         vp = ntmp->ntm_sysvn[NTFS_BITMAPINO];
744
745         bmsize = VTOF(vp)->f_size;
746
747         MALLOC(tmp, u_int8_t *, bmsize, M_TEMP, M_WAITOK);
748
749         error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
750                                0, bmsize, tmp, NULL);
751         if (error)
752                 goto out;
753
754         for(i=0;i<bmsize;i++)
755                 for(j=0;j<8;j++)
756                         if(~tmp[i] & (1 << j)) cfree++;
757         *cfreep = cfree;
758
759     out:
760         FREE(tmp, M_TEMP);
761         return(error);
762 }
763
764 static int
765 ntfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
766 {
767         struct ntfsmount *ntmp = VFSTONTFS(mp);
768         u_int64_t mftsize,mftallocated;
769
770         dprintf(("ntfs_statfs():\n"));
771
772         mftsize = VTOF(ntmp->ntm_sysvn[NTFS_MFTINO])->f_size;
773         mftallocated = VTOF(ntmp->ntm_sysvn[NTFS_MFTINO])->f_allocated;
774
775 #if defined(__DragonFly__)
776         sbp->f_type = mp->mnt_vfc->vfc_typenum;
777 #elif defined(__NetBSD__)
778         sbp->f_type = 0;
779 #else
780         sbp->f_type = MOUNT_NTFS;
781 #endif
782         sbp->f_bsize = ntmp->ntm_bps;
783         sbp->f_iosize = ntmp->ntm_bps * ntmp->ntm_spc;
784         sbp->f_blocks = ntmp->ntm_bootfile.bf_spv;
785         sbp->f_bfree = sbp->f_bavail = ntfs_cntobn(ntmp->ntm_cfree);
786         sbp->f_ffree = sbp->f_bfree / ntmp->ntm_bpmftrec;
787         sbp->f_files = mftallocated / ntfs_bntob(ntmp->ntm_bpmftrec) +
788                        sbp->f_ffree;
789         if (sbp != &mp->mnt_stat) {
790                 bcopy((caddr_t)mp->mnt_stat.f_mntonname,
791                         (caddr_t)&sbp->f_mntonname[0], MNAMELEN);
792                 bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
793                         (caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
794         }
795         sbp->f_flags = mp->mnt_flag;
796 #ifdef __NetBSD__
797         strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN);
798 #endif
799         
800         return (0);
801 }
802
803 #if !defined(__DragonFly__)
804 static int
805 ntfs_sync(struct mount *mp, int waitfor, struct ucred *cred, struct thread *td)
806 {
807         /*dprintf(("ntfs_sync():\n"));*/
808         return (0);
809 }
810 #endif
811
812 /*ARGSUSED*/
813 static int
814 ntfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
815 {
816         struct vnode *nvp;
817         struct ntfid *ntfhp = (struct ntfid *)fhp;
818         int error;
819
820         ddprintf(("ntfs_fhtovp(): %d\n", ntfhp->ntfid_ino));
821
822         if ((error = VFS_VGET(mp, ntfhp->ntfid_ino, &nvp)) != 0) {
823                 *vpp = NULLVP;
824                 return (error);
825         }
826         /* XXX as unlink/rmdir/mkdir/creat are not currently possible
827          * with NTFS, we don't need to check anything else for now */
828         *vpp = nvp;
829
830         return (0);
831 }
832
833 static int
834 ntfs_vptofh(struct vnode *vp, struct fid *fhp)
835 {
836         struct ntnode *ntp;
837         struct ntfid *ntfhp;
838
839         ddprintf(("ntfs_fhtovp(): %p\n", vp));
840
841         ntp = VTONT(vp);
842         ntfhp = (struct ntfid *)fhp;
843         ntfhp->ntfid_len = sizeof(struct ntfid);
844         ntfhp->ntfid_ino = ntp->i_number;
845         /* ntfhp->ntfid_gen = ntp->i_gen; */
846         return (0);
847 }
848
849 int
850 ntfs_vgetex(struct mount *mp, ino_t ino, u_int32_t attrtype, char *attrname,
851             u_long lkflags, u_long flags, struct thread *td,
852             struct vnode **vpp) 
853 {
854         int error;
855         struct ntfsmount *ntmp;
856         struct ntnode *ip;
857         struct fnode *fp;
858         struct vnode *vp;
859         enum vtype f_type;
860
861         dprintf(("ntfs_vgetex: ino: %d, attr: 0x%x:%s, lkf: 0x%lx, f: 0x%lx\n",
862                 ino, attrtype, attrname?attrname:"", (u_long)lkflags,
863                 (u_long)flags ));
864
865         ntmp = VFSTONTFS(mp);
866         *vpp = NULL;
867
868         /* Get ntnode */
869         error = ntfs_ntlookup(ntmp, ino, &ip);
870         if (error) {
871                 printf("ntfs_vget: ntfs_ntget failed\n");
872                 return (error);
873         }
874
875         /* It may be not initialized fully, so force load it */
876         if (!(flags & VG_DONTLOADIN) && !(ip->i_flag & IN_LOADED)) {
877                 error = ntfs_loadntnode(ntmp, ip);
878                 if(error) {
879                         printf("ntfs_vget: CAN'T LOAD ATTRIBUTES FOR INO: %d\n",
880                                ip->i_number);
881                         ntfs_ntput(ip);
882                         return (error);
883                 }
884         }
885
886         error = ntfs_fget(ntmp, ip, attrtype, attrname, &fp);
887         if (error) {
888                 printf("ntfs_vget: ntfs_fget failed\n");
889                 ntfs_ntput(ip);
890                 return (error);
891         }
892
893         f_type = VNON;
894         if (!(flags & VG_DONTVALIDFN) && !(fp->f_flag & FN_VALID)) {
895                 if ((ip->i_frflag & NTFS_FRFLAG_DIR) &&
896                     (fp->f_attrtype == NTFS_A_DATA && fp->f_attrname == NULL)) {
897                         f_type = VDIR;
898                 } else if (flags & VG_EXT) {
899                         f_type = VNON;
900                         fp->f_size = fp->f_allocated = 0;
901                 } else {
902                         f_type = VREG;  
903
904                         error = ntfs_filesize(ntmp, fp, 
905                                               &fp->f_size, &fp->f_allocated);
906                         if (error) {
907                                 ntfs_ntput(ip);
908                                 return (error);
909                         }
910                 }
911
912                 fp->f_flag |= FN_VALID;
913         }
914
915         if (FTOV(fp)) {
916                 VGET(FTOV(fp), lkflags, td);
917                 *vpp = FTOV(fp);
918                 ntfs_ntput(ip);
919                 return (0);
920         }
921
922         error = getnewvnode(VT_NTFS, ntmp->ntm_mountp, 
923                             ntmp->ntm_mountp->mnt_vn_ops, &vp,
924                             VLKTIMEOUT, 0);
925         if(error) {
926                 ntfs_frele(fp);
927                 ntfs_ntput(ip);
928                 return (error);
929         }
930         dprintf(("ntfs_vget: vnode: %p for ntnode: %d\n", vp,ino));
931
932         fp->f_vp = vp;
933         vp->v_data = fp;
934         vp->v_type = f_type;
935
936         if (ino == NTFS_ROOTINO)
937                 vp->v_flag |= VROOT;
938
939         ntfs_ntput(ip);
940
941         KKASSERT(lkflags & LK_TYPE_MASK);
942         KKASSERT((lkflags & LK_INTERLOCK) == 0);
943         /* XXX leave vnode locked exclusively from getnewvnode */
944         *vpp = vp;
945         return (0);
946         
947 }
948
949 static int
950 ntfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp) 
951 {
952         return ntfs_vgetex(mp, ino, NTFS_A_DATA, NULL,
953                         LK_EXCLUSIVE | LK_RETRY, 0, curthread, vpp);
954 }
955
956 #if defined(__DragonFly__)
957 static struct vfsops ntfs_vfsops = {
958         ntfs_mount,
959         vfs_stdstart,
960         ntfs_unmount,
961         ntfs_root,
962         vfs_stdquotactl,
963         ntfs_statfs,
964         vfs_stdsync,
965         ntfs_vget,
966         ntfs_fhtovp,
967         ntfs_checkexp,
968         ntfs_vptofh,
969         ntfs_init,
970         ntfs_nthash_uninit, /* see ntfs_ihash.c */
971         vfs_stdextattrctl,
972 };
973 VFS_SET(ntfs_vfsops, ntfs, 0);
974 #elif defined(__NetBSD__)
975 extern struct vnodeopv_desc ntfs_vnodeop_opv_desc;
976
977 struct vnodeopv_desc *ntfs_vnodeopv_descs[] = {
978         &ntfs_vnodeop_opv_desc,
979         NULL,
980 };
981
982 struct vfsops ntfs_vfsops = {
983         MOUNT_NTFS,
984         ntfs_mount,
985         ntfs_start,
986         ntfs_unmount,
987         ntfs_root,
988         ntfs_quotactl,
989         ntfs_statfs,
990         ntfs_sync,
991         ntfs_vget,
992         ntfs_fhtovp,
993         ntfs_vptofh,
994         ntfs_init,
995         ntfs_sysctl,
996         ntfs_mountroot,
997         ntfs_checkexp,
998         ntfs_vnodeopv_descs,
999 };
1000 #else /* !NetBSD && !FreeBSD */
1001 static struct vfsops ntfs_vfsops = {
1002         ntfs_mount,
1003         ntfs_start,
1004         ntfs_unmount,
1005         ntfs_root,
1006         ntfs_quotactl,
1007         ntfs_statfs,
1008         ntfs_sync,
1009         ntfs_vget,
1010         ntfs_fhtovp,
1011         ntfs_vptofh,
1012         ntfs_init,
1013 };
1014 VFS_SET(ntfs_vfsops, ntfs, MOUNT_NTFS, 0);
1015 #endif
1016
1017