]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/ntfs/ntfs_vfsops.c
This commit was generated by cvs2svn to compensate for changes in r55839,
[FreeBSD/FreeBSD.git] / sys / 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$
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         mp->mnt_data = (qaddr_t)ntmp;
505
506         dprintf(("ntfs_mountfs(): case-%s,%s uid: %d, gid: %d, mode: %o\n",
507                 (ntmp->ntm_flag & NTFS_MFLAG_CASEINS)?"insens.":"sens.",
508                 (ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES)?" allnames,":"",
509                 ntmp->ntm_uid, ntmp->ntm_gid, ntmp->ntm_mode));
510
511         /*
512          * We read in some system nodes to do not allow 
513          * reclaim them and to have everytime access to them.
514          */ 
515         {
516                 int pi[3] = { NTFS_MFTINO, NTFS_ROOTINO, NTFS_BITMAPINO };
517                 for (i=0; i<3; i++) {
518                         error = VFS_VGET(mp, pi[i], &(ntmp->ntm_sysvn[pi[i]]));
519                         if(error)
520                                 goto out1;
521                         ntmp->ntm_sysvn[pi[i]]->v_flag |= VSYSTEM;
522                         VREF(ntmp->ntm_sysvn[pi[i]]);
523                         vput(ntmp->ntm_sysvn[pi[i]]);
524                 }
525         }
526
527         /* read the Unicode lowercase --> uppercase translation table,
528          * if necessary */
529         if ((error = ntfs_toupper_use(mp, ntmp)))
530                 goto out1;
531
532         /*
533          * Scan $BitMap and count free clusters
534          */
535         error = ntfs_calccfree(ntmp, &ntmp->ntm_cfree);
536         if(error)
537                 goto out1;
538
539         /*
540          * Read and translate to internal format attribute
541          * definition file. 
542          */
543         {
544                 int num,j;
545                 struct attrdef ad;
546
547                 /* Open $AttrDef */
548                 error = VFS_VGET(mp, NTFS_ATTRDEFINO, &vp );
549                 if(error) 
550                         goto out1;
551
552                 /* Count valid entries */
553                 for(num=0;;num++) {
554                         error = ntfs_readattr(ntmp, VTONT(vp),
555                                         NTFS_A_DATA, NULL,
556                                         num * sizeof(ad), sizeof(ad),
557                                         &ad, NULL);
558                         if (error)
559                                 goto out1;
560                         if (ad.ad_name[0] == 0)
561                                 break;
562                 }
563
564                 /* Alloc memory for attribute definitions */
565                 MALLOC(ntmp->ntm_ad, struct ntvattrdef *,
566                         num * sizeof(struct ntvattrdef),
567                         M_NTFSMNT, M_WAITOK);
568
569                 ntmp->ntm_adnum = num;
570
571                 /* Read them and translate */
572                 for(i=0;i<num;i++){
573                         error = ntfs_readattr(ntmp, VTONT(vp),
574                                         NTFS_A_DATA, NULL,
575                                         i * sizeof(ad), sizeof(ad),
576                                         &ad, NULL);
577                         if (error)
578                                 goto out1;
579                         j = 0;
580                         do {
581                                 ntmp->ntm_ad[i].ad_name[j] = ad.ad_name[j];
582                         } while(ad.ad_name[j++]);
583                         ntmp->ntm_ad[i].ad_namelen = j - 1;
584                         ntmp->ntm_ad[i].ad_type = ad.ad_type;
585                 }
586
587                 vput(vp);
588         }
589
590 #if defined(__FreeBSD__)
591         mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
592         mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
593 #else
594         mp->mnt_stat.f_fsid.val[0] = dev;
595         mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_NTFS);
596 #endif
597         mp->mnt_maxsymlinklen = 0;
598         mp->mnt_flag |= MNT_LOCAL;
599         devvp->v_specmountpoint = mp;
600         return (0);
601
602 out1:
603         for(i=0;i<NTFS_SYSNODESNUM;i++)
604                 if(ntmp->ntm_sysvn[i]) vrele(ntmp->ntm_sysvn[i]);
605
606         if (vflush(mp,NULLVP,0))
607                 dprintf(("ntfs_mountfs: vflush failed\n"));
608
609 out:
610         devvp->v_specmountpoint = NULL;
611         if (bp)
612                 brelse(bp);
613
614 #if defined __NetBSD__
615         /* lock the device vnode before calling VOP_CLOSE() */
616         VN_LOCK(devvp, LK_EXCLUSIVE | LK_RETRY, p);
617         (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
618         VOP__UNLOCK(devvp, 0, p);
619 #else
620         (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
621 #endif
622         
623         return (error);
624 }
625
626 #if !defined(__FreeBSD__)
627 static int
628 ntfs_start (
629         struct mount *mp,
630         int flags,
631         struct proc *p )
632 {
633         return (0);
634 }
635 #endif
636
637 static int
638 ntfs_unmount( 
639         struct mount *mp,
640         int mntflags,
641         struct proc *p)
642 {
643         register struct ntfsmount *ntmp;
644         int error, ronly = 0, flags, i;
645
646         dprintf(("ntfs_unmount: unmounting...\n"));
647         ntmp = VFSTONTFS(mp);
648
649         flags = 0;
650         if(mntflags & MNT_FORCE)
651                 flags |= FORCECLOSE;
652
653         dprintf(("ntfs_unmount: vflushing...\n"));
654         error = vflush(mp,NULLVP,flags | SKIPSYSTEM);
655         if (error) {
656                 printf("ntfs_unmount: vflush failed: %d\n",error);
657                 return (error);
658         }
659
660         /* Check if only system vnodes are rest */
661         for(i=0;i<NTFS_SYSNODESNUM;i++)
662                  if((ntmp->ntm_sysvn[i]) && 
663                     (ntmp->ntm_sysvn[i]->v_usecount > 1)) return (EBUSY);
664
665         /* Dereference all system vnodes */
666         for(i=0;i<NTFS_SYSNODESNUM;i++)
667                  if(ntmp->ntm_sysvn[i]) vrele(ntmp->ntm_sysvn[i]);
668
669         /* vflush system vnodes */
670         error = vflush(mp,NULLVP,flags);
671         if (error)
672                 printf("ntfs_unmount: vflush failed(sysnodes): %d\n",error);
673
674         /* Check if the type of device node isn't VBAD before
675          * touching v_specinfo.  If the device vnode is revoked, the
676          * field is NULL and touching it causes null pointer derefercence.
677          */
678         if (ntmp->ntm_devvp->v_type != VBAD)
679                 ntmp->ntm_devvp->v_specmountpoint = NULL;
680
681         vinvalbuf(ntmp->ntm_devvp, V_SAVE, NOCRED, p, 0, 0);
682
683 #if defined(__NetBSD__)
684         /* lock the device vnode before calling VOP_CLOSE() */
685         VOP_LOCK(ntmp->ntm_devvp, LK_EXCLUSIVE | LK_RETRY);
686         error = VOP_CLOSE(ntmp->ntm_devvp, ronly ? FREAD : FREAD|FWRITE,
687                 NOCRED, p);
688         VOP__UNLOCK(ntmp->ntm_devvp, 0, p);
689 #else
690         error = VOP_CLOSE(ntmp->ntm_devvp, ronly ? FREAD : FREAD|FWRITE,
691                 NOCRED, p);
692 #endif
693
694         vrele(ntmp->ntm_devvp);
695
696         /* free the toupper table, if this has been last mounted ntfs volume */
697         ntfs_toupper_unuse();
698
699         dprintf(("ntfs_umount: freeing memory...\n"));
700         mp->mnt_data = (qaddr_t)0;
701         mp->mnt_flag &= ~MNT_LOCAL;
702         FREE(ntmp->ntm_ad, M_NTFSMNT);
703         FREE(ntmp, M_NTFSMNT);
704         return (error);
705 }
706
707 static int
708 ntfs_root(
709         struct mount *mp,
710         struct vnode **vpp )
711 {
712         struct vnode *nvp;
713         int error = 0;
714
715         dprintf(("ntfs_root(): sysvn: %p\n",
716                 VFSTONTFS(mp)->ntm_sysvn[NTFS_ROOTINO]));
717         error = VFS_VGET(mp, (ino_t)NTFS_ROOTINO, &nvp);
718         if(error) {
719                 printf("ntfs_root: VFS_VGET failed: %d\n",error);
720                 return (error);
721         }
722
723         *vpp = nvp;
724         return (0);
725 }
726
727 #if !defined(__FreeBSD__)
728 static int
729 ntfs_quotactl ( 
730         struct mount *mp,
731         int cmds,
732         uid_t uid,
733         caddr_t arg,
734         struct proc *p)
735 {
736         printf("\nntfs_quotactl():\n");
737         return EOPNOTSUPP;
738 }
739 #endif
740
741 int
742 ntfs_calccfree(
743         struct ntfsmount *ntmp,
744         cn_t *cfreep)
745 {
746         struct vnode *vp;
747         u_int8_t *tmp;
748         int j, error;
749         long cfree = 0;
750         size_t bmsize, i;
751
752         vp = ntmp->ntm_sysvn[NTFS_BITMAPINO];
753
754         bmsize = VTOF(vp)->f_size;
755
756         MALLOC(tmp, u_int8_t *, bmsize, M_TEMP, M_WAITOK);
757
758         error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
759                                0, bmsize, tmp, NULL);
760         if (error)
761                 goto out;
762
763         for(i=0;i<bmsize;i++)
764                 for(j=0;j<8;j++)
765                         if(~tmp[i] & (1 << j)) cfree++;
766         *cfreep = cfree;
767
768     out:
769         FREE(tmp, M_TEMP);
770         return(error);
771 }
772
773 static int
774 ntfs_statfs(
775         struct mount *mp,
776         struct statfs *sbp,
777         struct proc *p)
778 {
779         struct ntfsmount *ntmp = VFSTONTFS(mp);
780         u_int64_t mftsize,mftallocated;
781
782         dprintf(("ntfs_statfs():\n"));
783
784         mftsize = VTOF(ntmp->ntm_sysvn[NTFS_MFTINO])->f_size;
785         mftallocated = VTOF(ntmp->ntm_sysvn[NTFS_MFTINO])->f_allocated;
786
787 #if defined(__FreeBSD__)
788         sbp->f_type = mp->mnt_vfc->vfc_typenum;
789 #elif defined(__NetBSD__)
790         sbp->f_type = 0;
791 #else
792         sbp->f_type = MOUNT_NTFS;
793 #endif
794         sbp->f_bsize = ntmp->ntm_bps;
795         sbp->f_iosize = ntmp->ntm_bps * ntmp->ntm_spc;
796         sbp->f_blocks = ntmp->ntm_bootfile.bf_spv;
797         sbp->f_bfree = sbp->f_bavail = ntfs_cntobn(ntmp->ntm_cfree);
798         sbp->f_ffree = sbp->f_bfree / ntmp->ntm_bpmftrec;
799         sbp->f_files = mftallocated / ntfs_bntob(ntmp->ntm_bpmftrec) +
800                        sbp->f_ffree;
801         if (sbp != &mp->mnt_stat) {
802                 bcopy((caddr_t)mp->mnt_stat.f_mntonname,
803                         (caddr_t)&sbp->f_mntonname[0], MNAMELEN);
804                 bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
805                         (caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
806         }
807         sbp->f_flags = mp->mnt_flag;
808 #ifdef __NetBSD__
809         strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN);
810 #endif
811         
812         return (0);
813 }
814
815 #if !defined(__FreeBSD__)
816 static int
817 ntfs_sync (
818         struct mount *mp,
819         int waitfor,
820         struct ucred *cred,
821         struct proc *p)
822 {
823         /*dprintf(("ntfs_sync():\n"));*/
824         return (0);
825 }
826 #endif
827
828 /*ARGSUSED*/
829 static int
830 ntfs_fhtovp(
831         struct mount *mp,
832         struct fid *fhp,
833         struct vnode **vpp)
834 {
835         struct vnode *nvp;
836         struct ntfid *ntfhp = (struct ntfid *)fhp;
837         int error;
838
839         ddprintf(("ntfs_fhtovp(): %s: %d\n", mp->mnt_stat->f_mntonname,
840                 ntfhp->ntfid_ino));
841
842         if ((error = VFS_VGET(mp, ntfhp->ntfid_ino, &nvp)) != 0) {
843                 *vpp = NULLVP;
844                 return (error);
845         }
846         /* XXX as unlink/rmdir/mkdir/creat are not currently possible
847          * with NTFS, we don't need to check anything else for now */
848         *vpp = nvp;
849
850         return (0);
851 }
852
853 static int
854 ntfs_vptofh(
855         struct vnode *vp,
856         struct fid *fhp)
857 {
858         register struct ntnode *ntp;
859         register struct ntfid *ntfhp;
860
861         ddprintf(("ntfs_fhtovp(): %s: %p\n", vp->v_mount->mnt_stat->f_mntonname,
862                 vp));
863
864         ntp = VTONT(vp);
865         ntfhp = (struct ntfid *)fhp;
866         ntfhp->ntfid_len = sizeof(struct ntfid);
867         ntfhp->ntfid_ino = ntp->i_number;
868         /* ntfhp->ntfid_gen = ntp->i_gen; */
869         return (0);
870 }
871
872 int
873 ntfs_vgetex(
874         struct mount *mp,
875         ino_t ino,
876         u_int32_t attrtype,
877         char *attrname,
878         u_long lkflags,
879         u_long flags,
880         struct proc *p,
881         struct vnode **vpp) 
882 {
883         int error;
884         register struct ntfsmount *ntmp;
885         struct ntnode *ip;
886         struct fnode *fp;
887         struct vnode *vp;
888         enum vtype f_type;
889
890         dprintf(("ntfs_vgetex: ino: %d, attr: 0x%x:%s, lkf: 0x%lx, f: 0x%lx\n",
891                 ino, attrtype, attrname?attrname:"", (u_long)lkflags,
892                 (u_long)flags ));
893
894         ntmp = VFSTONTFS(mp);
895         *vpp = NULL;
896
897         /* Get ntnode */
898         error = ntfs_ntlookup(ntmp, ino, &ip);
899         if (error) {
900                 printf("ntfs_vget: ntfs_ntget failed\n");
901                 return (error);
902         }
903
904         /* It may be not initialized fully, so force load it */
905         if (!(flags & VG_DONTLOADIN) && !(ip->i_flag & IN_LOADED)) {
906                 error = ntfs_loadntnode(ntmp, ip);
907                 if(error) {
908                         printf("ntfs_vget: CAN'T LOAD ATTRIBUTES FOR INO: %d\n",
909                                ip->i_number);
910                         ntfs_ntput(ip);
911                         return (error);
912                 }
913         }
914
915         error = ntfs_fget(ntmp, ip, attrtype, attrname, &fp);
916         if (error) {
917                 printf("ntfs_vget: ntfs_fget failed\n");
918                 ntfs_ntput(ip);
919                 return (error);
920         }
921
922         f_type = VNON;
923         if (!(flags & VG_DONTVALIDFN) && !(fp->f_flag & FN_VALID)) {
924                 if ((ip->i_frflag & NTFS_FRFLAG_DIR) &&
925                     (fp->f_attrtype == NTFS_A_DATA && fp->f_attrname == NULL)) {
926                         f_type = VDIR;
927                 } else if (flags & VG_EXT) {
928                         f_type = VNON;
929                         fp->f_size = fp->f_allocated = 0;
930                 } else {
931                         f_type = VREG;  
932
933                         error = ntfs_filesize(ntmp, fp, 
934                                               &fp->f_size, &fp->f_allocated);
935                         if (error) {
936                                 ntfs_ntput(ip);
937                                 return (error);
938                         }
939                 }
940
941                 fp->f_flag |= FN_VALID;
942         }
943
944         if (FTOV(fp)) {
945                 VGET(FTOV(fp), lkflags, p);
946                 *vpp = FTOV(fp);
947                 ntfs_ntput(ip);
948                 return (0);
949         }
950
951         error = getnewvnode(VT_NTFS, ntmp->ntm_mountp, ntfs_vnodeop_p, &vp);
952         if(error) {
953                 ntfs_frele(fp);
954                 ntfs_ntput(ip);
955                 return (error);
956         }
957         dprintf(("ntfs_vget: vnode: %p for ntnode: %d\n", vp,ino));
958
959 #ifdef __FreeBSD__
960         lockinit(&fp->f_lock, PINOD, "fnode", 0, 0);
961 #endif
962         fp->f_vp = vp;
963         vp->v_data = fp;
964         vp->v_type = f_type;
965
966         if (ino == NTFS_ROOTINO)
967                 vp->v_flag |= VROOT;
968
969         ntfs_ntput(ip);
970
971         if (lkflags & LK_TYPE_MASK) {
972                 error = VN_LOCK(vp, lkflags, p);
973                 if (error) {
974                         vput(vp);
975                         return (error);
976                 }
977         }
978
979         VREF(ip->i_devvp);
980         *vpp = vp;
981         return (0);
982         
983 }
984
985 static int
986 ntfs_vget(
987         struct mount *mp,
988         ino_t ino,
989         struct vnode **vpp) 
990 {
991         return ntfs_vgetex(mp, ino, NTFS_A_DATA, NULL,
992                         LK_EXCLUSIVE | LK_RETRY, 0, curproc, vpp);
993 }
994
995 #if defined(__FreeBSD__)
996 static struct vfsops ntfs_vfsops = {
997         ntfs_mount,
998         vfs_stdstart,
999         ntfs_unmount,
1000         ntfs_root,
1001         vfs_stdquotactl,
1002         ntfs_statfs,
1003         vfs_stdsync,
1004         ntfs_vget,
1005         ntfs_fhtovp,
1006         ntfs_checkexp,
1007         ntfs_vptofh,
1008         ntfs_init,
1009         vfs_stduninit,
1010         vfs_stdextattrctl,
1011 };
1012 VFS_SET(ntfs_vfsops, ntfs, 0);
1013 #elif defined(__NetBSD__)
1014 extern struct vnodeopv_desc ntfs_vnodeop_opv_desc;
1015
1016 struct vnodeopv_desc *ntfs_vnodeopv_descs[] = {
1017         &ntfs_vnodeop_opv_desc,
1018         NULL,
1019 };
1020
1021 struct vfsops ntfs_vfsops = {
1022         MOUNT_NTFS,
1023         ntfs_mount,
1024         ntfs_start,
1025         ntfs_unmount,
1026         ntfs_root,
1027         ntfs_quotactl,
1028         ntfs_statfs,
1029         ntfs_sync,
1030         ntfs_vget,
1031         ntfs_fhtovp,
1032         ntfs_vptofh,
1033         ntfs_init,
1034         ntfs_sysctl,
1035         ntfs_mountroot,
1036         ntfs_checkexp,
1037         ntfs_vnodeopv_descs,
1038 };
1039 #else /* !NetBSD && !FreeBSD */
1040 static struct vfsops ntfs_vfsops = {
1041         ntfs_mount,
1042         ntfs_start,
1043         ntfs_unmount,
1044         ntfs_root,
1045         ntfs_quotactl,
1046         ntfs_statfs,
1047         ntfs_sync,
1048         ntfs_vget,
1049         ntfs_fhtovp,
1050         ntfs_vptofh,
1051         ntfs_init,
1052 };
1053 VFS_SET(ntfs_vfsops, ntfs, MOUNT_NTFS, 0);
1054 #endif
1055
1056