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