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