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