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