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