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