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