]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/ntfs/ntfs_vfsops.c
This commit was generated by cvs2svn to compensate for changes in r53654,
[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_page.h>
47 #include <vm/vm_object.h>
48 #include <vm/vm_extern.h>
49
50 /*#define NTFS_DEBUG 1*/
51 #include <ntfs/ntfs.h>
52 #include <ntfs/ntfs_inode.h>
53 #include <ntfs/ntfs_subr.h>
54 #include <ntfs/ntfs_vfsops.h>
55 #include <ntfs/ntfs_ihash.h>
56 #include <ntfs/ntfs_extern.h>
57 #include <ntfs/ntfsmount.h>
58
59 #if defined(__FreeBSD__)
60 MALLOC_DEFINE(M_NTFSMNT, "NTFS mount", "NTFS mount structure");
61 MALLOC_DEFINE(M_NTFSNTNODE,"NTFS ntnode",  "NTFS ntnode information");
62 MALLOC_DEFINE(M_NTFSFNODE,"NTFS fnode",  "NTFS fnode information");
63 MALLOC_DEFINE(M_NTFSDIR,"NTFS dir",  "NTFS dir buffer");
64 #endif
65
66 #if defined(__FreeBSD__)
67 static int      ntfs_mount __P((struct mount *, char *, caddr_t,
68                                 struct nameidata *, struct proc *));
69 #else
70 static int      ntfs_mount __P((struct mount *, const char *, void *,
71                                 struct nameidata *, struct proc *));
72 #endif
73 static int      ntfs_root __P((struct mount *, struct vnode **));
74 static int      ntfs_statfs __P((struct mount *, struct statfs *,
75                                  struct proc *));
76 static int      ntfs_unmount __P((struct mount *, int, struct proc *));
77 static int      ntfs_vget __P((struct mount *mp, ino_t ino,
78                                struct vnode **vpp));
79 static int      ntfs_mountfs __P((register struct vnode *, struct mount *, 
80                                   struct ntfs_args *, struct proc *));
81
82 #if !defined(__FreeBSD__)
83 static int      ntfs_quotactl __P((struct mount *, int, uid_t, caddr_t,
84                                    struct proc *));
85 static int      ntfs_start __P((struct mount *, int, struct proc *));
86 static int      ntfs_sync __P((struct mount *, int, struct ucred *,
87                                struct proc *));
88 static int      ntfs_vptofh __P((struct vnode *, struct fid *));
89 #endif /* !defined(__FreeBSD__) */
90
91 #if defined(__FreeBSD__)
92 static int      ntfs_init __P((struct vfsconf *));
93 #if 0 /* may be implemented at a later date */
94 static int      ntfs_fhtovp __P((struct mount *, struct fid *,
95                                  struct vnode **));
96 static int      ntfs_checkexp __P((struct vnode *, struct mbuf *,
97                                    int *, struct ucred **));
98 #endif /* 0, default ops in FreeBSD */
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         vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
426         error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
427         VOP_UNLOCK(devvp, 0, p);
428         if (error)
429                 return (error);
430
431         bp = NULL;
432
433         error = bread(devvp, BBLOCK, BBSIZE, NOCRED, &bp);
434         if (error)
435                 goto out;
436         ntmp = malloc( sizeof *ntmp, M_NTFSMNT, M_WAITOK );
437         bzero( ntmp, sizeof *ntmp );
438         bcopy( bp->b_data, &ntmp->ntm_bootfile, sizeof(struct bootfile) );
439         brelse( bp );
440         bp = NULL;
441
442         if (strncmp(ntmp->ntm_bootfile.bf_sysid, NTFS_BBID, NTFS_BBIDLEN)) {
443                 error = EINVAL;
444                 printf("ntfs_mountfs: invalid boot block\n");
445                 goto out;
446         }
447
448         {
449                 int8_t cpr = ntmp->ntm_mftrecsz;
450                 if( cpr > 0 )
451                         ntmp->ntm_bpmftrec = ntmp->ntm_spc * cpr;
452                 else
453                         ntmp->ntm_bpmftrec = (1 << (-cpr)) / ntmp->ntm_bps;
454         }
455         dprintf(("ntfs_mountfs(): bps: %d, spc: %d, media: %x, mftrecsz: %d (%d sects)\n",
456                 ntmp->ntm_bps,ntmp->ntm_spc,ntmp->ntm_bootfile.bf_media,
457                 ntmp->ntm_mftrecsz,ntmp->ntm_bpmftrec));
458         dprintf(("ntfs_mountfs(): mftcn: 0x%x|0x%x\n",
459                 (u_int32_t)ntmp->ntm_mftcn,(u_int32_t)ntmp->ntm_mftmirrcn));
460
461         ntmp->ntm_mountp = mp;
462         ntmp->ntm_dev = dev;
463         ntmp->ntm_devvp = devvp;
464         ntmp->ntm_uid = argsp->uid;
465         ntmp->ntm_gid = argsp->gid;
466         ntmp->ntm_mode = argsp->mode;
467         ntmp->ntm_flag = argsp->flag;
468         mp->mnt_data = (qaddr_t)ntmp;
469
470         dprintf(("ntfs_mountfs(): case-%s,%s uid: %d, gid: %d, mode: %o\n",
471                 (ntmp->ntm_flag & NTFS_MFLAG_CASEINS)?"insens.":"sens.",
472                 (ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES)?" allnames,":"",
473                 ntmp->ntm_uid, ntmp->ntm_gid, ntmp->ntm_mode));
474
475         /*
476          * We read in some system nodes to do not allow 
477          * reclaim them and to have everytime access to them.
478          */ 
479         {
480                 int pi[3] = { NTFS_MFTINO, NTFS_ROOTINO, NTFS_BITMAPINO };
481                 for (i=0; i<3; i++) {
482                         error = VFS_VGET(mp, pi[i], &(ntmp->ntm_sysvn[pi[i]]));
483                         if(error)
484                                 goto out1;
485                         ntmp->ntm_sysvn[pi[i]]->v_flag |= VSYSTEM;
486                         VREF(ntmp->ntm_sysvn[pi[i]]);
487                         vput(ntmp->ntm_sysvn[pi[i]]);
488                 }
489         }
490
491         /*
492          * Read in WHOLE lowcase -> upcase translation
493          * file.
494          */
495         MALLOC(ntmp->ntm_upcase, wchar *, 65536 * sizeof(wchar),
496                 M_NTFSMNT, M_WAITOK);
497
498         error = VFS_VGET(mp, NTFS_UPCASEINO, &vp);
499         if(error) 
500                 goto out1;
501         error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
502                         0, 65536*sizeof(wchar), ntmp->ntm_upcase);
503         vput(vp);
504         if(error) 
505                 goto out1;
506
507         /*
508          * Scan $BitMap and count free clusters
509          */
510         error = ntfs_calccfree(ntmp, &ntmp->ntm_cfree);
511         if(error)
512                 goto out1;
513
514         /*
515          * Read and translate to internal format attribute
516          * definition file. 
517          */
518         {
519                 int num,j;
520                 struct attrdef ad;
521
522                 /* Open $AttrDef */
523                 error = VFS_VGET(mp, NTFS_ATTRDEFINO, &vp );
524                 if(error) 
525                         goto out1;
526
527                 /* Count valid entries */
528                 for(num=0;;num++) {
529                         error = ntfs_readattr(ntmp, VTONT(vp),
530                                         NTFS_A_DATA, NULL,
531                                         num * sizeof(ad), sizeof(ad),
532                                         &ad);
533                         if (error)
534                                 goto out1;
535                         if (ad.ad_name[0] == 0)
536                                 break;
537                 }
538
539                 /* Alloc memory for attribute definitions */
540                 MALLOC(ntmp->ntm_ad, struct ntvattrdef *,
541                         num * sizeof(struct ntvattrdef),
542                         M_NTFSMNT, M_WAITOK);
543
544                 ntmp->ntm_adnum = num;
545
546                 /* Read them and translate */
547                 for(i=0;i<num;i++){
548                         error = ntfs_readattr(ntmp, VTONT(vp),
549                                         NTFS_A_DATA, NULL,
550                                         i * sizeof(ad), sizeof(ad),
551                                         &ad);
552                         if (error)
553                                 goto out1;
554                         j = 0;
555                         do {
556                                 ntmp->ntm_ad[i].ad_name[j] = ad.ad_name[j];
557                         } while(ad.ad_name[j++]);
558                         ntmp->ntm_ad[i].ad_namelen = j - 1;
559                         ntmp->ntm_ad[i].ad_type = ad.ad_type;
560                 }
561
562                 vput(vp);
563         }
564
565         mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
566 #if defined(__FreeBSD__)
567         mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
568 #else
569         mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_NTFS);
570 #endif
571         mp->mnt_maxsymlinklen = 0;
572         mp->mnt_flag |= MNT_LOCAL;
573 #if defined(__FreeBSD__)
574         devvp->v_specmountpoint = mp;
575 #else
576         devvp->v_specflags |= SI_MOUNTEDON;
577 #endif
578         return (0);
579
580 out1:
581         for(i=0;i<NTFS_SYSNODESNUM;i++)
582                 if(ntmp->ntm_sysvn[i]) vrele(ntmp->ntm_sysvn[i]);
583
584         if (vflush(mp,NULLVP,0))
585                 printf("ntfs_mountfs: vflush failed\n");
586
587 out:
588 #if defined(__FreeBSD__)
589         devvp->v_specmountpoint = NULL;
590 #else
591         devvp->v_specflags &= ~SI_MOUNTEDON;
592 #endif
593         if (bp)
594                 brelse(bp);
595         (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
596         return (error);
597 }
598
599 #if !defined(__FreeBSD__)
600 static int
601 ntfs_start (
602         struct mount *mp,
603         int flags,
604         struct proc *p )
605 {
606         return (0);
607 }
608 #endif /* !defined(__FreeBSD__) */
609
610 static int
611 ntfs_unmount( 
612         struct mount *mp,
613         int mntflags,
614         struct proc *p)
615 {
616         register struct ntfsmount *ntmp;
617         int error, ronly = 0, flags, i;
618
619         dprintf(("ntfs_unmount: unmounting...\n"));
620         ntmp = VFSTONTFS(mp);
621
622         flags = 0;
623         if(mntflags & MNT_FORCE)
624                 flags |= FORCECLOSE;
625
626         dprintf(("ntfs_unmount: vflushing...\n"));
627         error = vflush(mp,NULLVP,flags | SKIPSYSTEM);
628         if (error) {
629                 printf("ntfs_unmount: vflush failed: %d\n",error);
630                 return (error);
631         }
632
633         /* Check if only system vnodes are rest */
634         for(i=0;i<NTFS_SYSNODESNUM;i++)
635                  if((ntmp->ntm_sysvn[i]) && 
636                     (ntmp->ntm_sysvn[i]->v_usecount > 1)) return (EBUSY);
637
638         /* Derefernce all system vnodes */
639         for(i=0;i<NTFS_SYSNODESNUM;i++)
640                  if(ntmp->ntm_sysvn[i]) vrele(ntmp->ntm_sysvn[i]);
641
642         /* vflush system vnodes */
643         error = vflush(mp,NULLVP,flags);
644         if (error)
645                 printf("ntfs_unmount: vflush failed(sysnodes): %d\n",error);
646
647 #if defined(__FreeBSD__)
648         ntmp->ntm_devvp->v_specmountpoint = NULL;
649 #else
650         ntmp->ntm_devvp->v_specflags &= ~SI_MOUNTEDON;
651 #endif
652
653         vinvalbuf(ntmp->ntm_devvp, V_SAVE, NOCRED, p, 0, 0);
654         error = VOP_CLOSE(ntmp->ntm_devvp, ronly ? FREAD : FREAD|FWRITE,
655                 NOCRED, p);
656
657         vrele(ntmp->ntm_devvp);
658
659         dprintf(("ntfs_umount: freeing memory...\n"));
660         mp->mnt_data = (qaddr_t)0;
661         mp->mnt_flag &= ~MNT_LOCAL;
662         FREE(ntmp->ntm_ad, M_NTFSMNT);
663         FREE(ntmp->ntm_upcase, M_NTFSMNT);
664         FREE(ntmp, M_NTFSMNT);
665         return (error);
666 }
667
668 static int
669 ntfs_root(
670         struct mount *mp,
671         struct vnode **vpp )
672 {
673         struct vnode *nvp;
674         int error = 0;
675
676         dprintf(("ntfs_root(): sysvn: %p\n",
677                 VFSTONTFS(mp)->ntm_sysvn[NTFS_ROOTINO]));
678         error = VFS_VGET(mp, (ino_t)NTFS_ROOTINO, &nvp);
679         if(error) {
680                 printf("ntfs_root: VFS_VGET failed: %d\n",error);
681                 return (error);
682         }
683
684         *vpp = nvp;
685         return (0);
686 }
687
688 #if !defined(__FreeBSD__)
689 static int
690 ntfs_quotactl ( 
691         struct mount *mp,
692         int cmds,
693         uid_t uid,
694         caddr_t arg,
695         struct proc *p)
696 {
697         printf("\nntfs_quotactl():\n");
698         return EOPNOTSUPP;
699 }
700 #endif /* !defined(__FreeBSD__) */
701
702 int
703 ntfs_calccfree(
704         struct ntfsmount *ntmp,
705         cn_t *cfreep)
706 {
707         struct vnode *vp;
708         u_int8_t *tmp;
709         int j, error;
710         long cfree = 0;
711         size_t bmsize, i;
712
713         vp = ntmp->ntm_sysvn[NTFS_BITMAPINO];
714
715         bmsize = VTOF(vp)->f_size;
716
717         MALLOC(tmp, u_int8_t *, bmsize, M_TEMP, M_WAITOK);
718
719         error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
720                                0, bmsize, tmp);
721         if(error) {
722                 FREE(tmp, M_TEMP);
723                 return (error);
724         }
725
726         for(i=0;i<bmsize;i++)
727                 for(j=0;j<8;j++)
728                         if(~tmp[i] & (1 << j)) cfree++;
729
730         FREE(tmp, M_TEMP);
731
732         *cfreep = cfree;
733
734         return(0);
735 }
736
737 static int
738 ntfs_statfs(
739         struct mount *mp,
740         struct statfs *sbp,
741         struct proc *p)
742 {
743         struct ntfsmount *ntmp = VFSTONTFS(mp);
744         u_int64_t mftsize,mftallocated;
745
746         dprintf(("ntfs_statfs():\n"));
747
748         mftsize = VTOF(ntmp->ntm_sysvn[NTFS_MFTINO])->f_size;
749         mftallocated = VTOF(ntmp->ntm_sysvn[NTFS_MFTINO])->f_allocated;
750
751 #if defined(__FreeBSD__)
752         sbp->f_type = mp->mnt_vfc->vfc_typenum;
753 #elif defined(__NetBSD__)
754         sbp->f_type = 0;
755 #else
756         sbp->f_type = MOUNT_NTFS;
757 #endif
758         sbp->f_bsize = ntmp->ntm_bps;
759         sbp->f_iosize = ntmp->ntm_bps * ntmp->ntm_spc;
760         sbp->f_blocks = ntmp->ntm_bootfile.bf_spv;
761         sbp->f_bfree = sbp->f_bavail = ntfs_cntobn(ntmp->ntm_cfree);
762         sbp->f_ffree = sbp->f_bfree / ntmp->ntm_bpmftrec;
763         sbp->f_files = mftallocated / ntfs_bntob(ntmp->ntm_bpmftrec) +
764                        sbp->f_ffree;
765         if (sbp != &mp->mnt_stat) {
766                 bcopy((caddr_t)mp->mnt_stat.f_mntonname,
767                         (caddr_t)&sbp->f_mntonname[0], MNAMELEN);
768                 bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
769                         (caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
770         }
771         sbp->f_flags = mp->mnt_flag;
772         
773         return (0);
774 }
775
776 #if !defined(__FreeBSD__)
777 static int
778 ntfs_sync (
779         struct mount *mp,
780         int waitfor,
781         struct ucred *cred,
782         struct proc *p)
783 {
784         /*dprintf(("ntfs_sync():\n"));*/
785         return (0);
786 }
787 #endif /* !defined(__FreeBSD__) */
788
789 #if !defined(__FreeBSD__)
790 /*ARGSUSED*/
791 static int
792 ntfs_fhtovp(
793 #if defined(__FreeBSD__)
794         struct mount *mp,
795         struct fid *fhp,
796         struct sockaddr *nam,
797         struct vnode **vpp,
798         int *exflagsp,
799         struct ucred **credanonp)
800 #elif defined(__NetBSD__)
801         struct mount *mp,
802         struct fid *fhp,
803         struct vnode **vpp)
804 #else
805         struct mount *mp,
806         struct fid *fhp,
807         struct mbuf *nam,
808         struct vnode **vpp,
809         int *exflagsp,
810         struct ucred **credanonp)
811 #endif
812 {
813         printf("\ntfs_fhtovp():\n");
814         return 0;
815 }
816
817 static int
818 ntfs_vptofh(
819         struct vnode *vp,
820         struct fid *fhp)
821 {
822         printf("ntfs_vptofh():\n");
823         return EOPNOTSUPP;
824 }
825 #endif /* !defined(__FreeBSD__) */
826
827 int
828 ntfs_vgetex(
829         struct mount *mp,
830         ino_t ino,
831         u_int32_t attrtype,
832         char *attrname,
833         u_long lkflags,
834         u_long flags,
835         struct proc *p,
836         struct vnode **vpp) 
837 {
838         int error;
839         register struct ntfsmount *ntmp;
840         struct ntnode *ip;
841         struct fnode *fp;
842         struct vnode *vp;
843
844         dprintf(("ntfs_vgetex: ino: %d, attr: 0x%x:%s, lkf: 0x%x, f: 0x%x\n",
845                 ino, attrtype, attrname?attrname:"", lkflags, flags ));
846
847         ntmp = VFSTONTFS(mp);
848         *vpp = NULL;
849
850         /* Get ntnode */
851         error = ntfs_ntlookup(ntmp, ino, &ip);
852         if (error) {
853                 printf("ntfs_vget: ntfs_ntget failed\n");
854                 return (error);
855         }
856
857         /* It may be not initialized fully, so force load it */
858         if (!(flags & VG_DONTLOADIN) && !(ip->i_flag & IN_LOADED)) {
859                 error = ntfs_loadntnode(ntmp, ip);
860                 if(error) {
861                         printf("ntfs_vget: CAN'T LOAD ATTRIBUTES FOR INO: %d\n",
862                                ip->i_number);
863                         ntfs_ntput(ip);
864                         return (error);
865                 }
866         }
867
868         error = ntfs_fget(ntmp, ip, attrtype, attrname, &fp);
869         if (error) {
870                 printf("ntfs_vget: ntfs_fget failed\n");
871                 ntfs_ntput(ip);
872                 return (error);
873         }
874
875         if (!(flags & VG_DONTVALIDFN) && !(fp->f_flag & FN_VALID)) {
876                 if ((ip->i_frflag & NTFS_FRFLAG_DIR) &&
877                     (fp->f_attrtype == 0x80 && fp->f_attrname == NULL)) {
878                         fp->f_type = VDIR;
879                 } else if(flags & VG_EXT) {
880                         fp->f_type = VNON;
881
882                         fp->f_size =fp->f_allocated = 0;
883                 } else {
884                         fp->f_type = VREG;      
885
886                         error = ntfs_filesize(ntmp, fp, 
887                                               &fp->f_size, &fp->f_allocated);
888                         if (error) {
889                                 ntfs_ntput(ip);
890                                 return (error);
891                         }
892                 }
893
894                 fp->f_flag |= FN_VALID;
895         }
896
897         if (FTOV(fp)) {
898                 VGET(FTOV(fp), lkflags, p);
899                 *vpp = FTOV(fp);
900                 ntfs_ntput(ip);
901                 return (0);
902         }
903
904         error = getnewvnode(VT_NTFS, ntmp->ntm_mountp, ntfs_vnodeop_p, &vp);
905         if(error) {
906                 ntfs_frele(fp);
907                 ntfs_ntput(ip);
908                 return (error);
909         }
910         dprintf(("ntfs_vget: vnode: %p for ntnode: %d\n", vp,ino));
911
912         lockinit(&fp->f_lock, PINOD, "fnode", 0, 0);
913         fp->f_vp = vp;
914         vp->v_data = fp;
915         vp->v_type = fp->f_type;
916
917         if (ino == NTFS_ROOTINO)
918                 vp->v_flag |= VROOT;
919
920         ntfs_ntput(ip);
921
922         if (lkflags & LK_TYPE_MASK) {
923                 error = VN_LOCK(vp, lkflags, p);
924                 if (error) {
925                         vput(vp);
926                         return (error);
927                 }
928         }
929
930         VREF(fp->f_devvp);
931         *vpp = vp;
932         return (0);
933         
934 }
935
936 static int
937 ntfs_vget(
938         struct mount *mp,
939         ino_t ino,
940         struct vnode **vpp) 
941 {
942         return ntfs_vgetex(mp, ino, NTFS_A_DATA, NULL,
943                            LK_EXCLUSIVE, 0, curproc, vpp);
944 }
945
946 #if defined(__FreeBSD__)
947 static struct vfsops ntfs_vfsops = {
948         ntfs_mount,
949         vfs_stdstart,
950         ntfs_unmount,
951         ntfs_root,
952         vfs_stdquotactl,
953         ntfs_statfs,
954         vfs_stdsync,
955         ntfs_vget,
956         vfs_stdfhtovp,
957         vfs_stdcheckexp,
958         vfs_stdvptofh,
959         ntfs_init,
960         NULL
961 };
962 VFS_SET(ntfs_vfsops, ntfs, 0);
963 #elif defined(__NetBSD__)
964 extern struct vnodeopv_desc ntfs_vnodeop_opv_desc;
965
966 struct vnodeopv_desc *ntfs_vnodeopv_descs[] = {
967         &ntfs_vnodeop_opv_desc,
968         NULL,
969 };
970
971 struct vfsops ntfs_vfsops = {
972         MOUNT_NTFS,
973         ntfs_mount,
974         ntfs_start,
975         ntfs_unmount,
976         ntfs_root,
977         ntfs_quotactl,
978         ntfs_statfs,
979         ntfs_sync,
980         ntfs_vget,
981         ntfs_fhtovp,
982         ntfs_vptofh,
983         ntfs_init,
984         ntfs_sysctl,
985         ntfs_mountroot,
986         ntfs_checkexp,
987         ntfs_vnodeopv_descs,
988 };
989 #else
990 static struct vfsops ntfs_vfsops = {
991         ntfs_mount,
992         ntfs_start,
993         ntfs_unmount,
994         ntfs_root,
995         ntfs_quotactl,
996         ntfs_statfs,
997         ntfs_sync,
998         ntfs_vget,
999         ntfs_fhtovp,
1000         ntfs_vptofh,
1001         ntfs_init,
1002 };
1003 VFS_SET(ntfs_vfsops, ntfs, MOUNT_NTFS, 0);
1004 #endif
1005
1006