]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/fs/ntfs/ntfs_vfsops.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / fs / 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/bio.h>
41 #include <sys/buf.h>
42 #include <sys/fcntl.h>
43 #include <sys/malloc.h>
44 #include <sys/stat.h>
45 #include <sys/systm.h>
46
47 #include <geom/geom.h>
48 #include <geom/geom_vfs.h>
49
50 #include <vm/vm.h>
51 #include <vm/vm_param.h>
52 #include <vm/vm_page.h>
53 #include <vm/vm_object.h>
54 #include <vm/vm_extern.h>
55
56 /*#define NTFS_DEBUG 1*/
57 #include <fs/ntfs/ntfs.h>
58 #include <fs/ntfs/ntfs_inode.h>
59 #include <fs/ntfs/ntfs_subr.h>
60 #include <fs/ntfs/ntfs_vfsops.h>
61 #include <fs/ntfs/ntfs_ihash.h>
62 #include <fs/ntfs/ntfsmount.h>
63
64 static MALLOC_DEFINE(M_NTFSMNT, "ntfs_mount", "NTFS mount structure");
65 MALLOC_DEFINE(M_NTFSNTNODE,"ntfs_ntnode",  "NTFS ntnode information");
66 MALLOC_DEFINE(M_NTFSFNODE,"ntfs_fnode",  "NTFS fnode information");
67 MALLOC_DEFINE(M_NTFSDIR,"ntfs_dir",  "NTFS dir buffer");
68
69 struct sockaddr;
70
71 static int      ntfs_mountfs(register struct vnode *, struct mount *, 
72                                   struct thread *);
73 static int      ntfs_calccfree(struct ntfsmount *ntmp, cn_t *cfreep);
74
75 static vfs_init_t       ntfs_init;
76 static vfs_uninit_t     ntfs_uninit;
77 static vfs_vget_t       ntfs_vget;
78 static vfs_fhtovp_t     ntfs_fhtovp;
79 static vfs_cmount_t     ntfs_cmount;
80 static vfs_mount_t      ntfs_mount;
81 static vfs_root_t       ntfs_root;
82 static vfs_statfs_t     ntfs_statfs;
83 static vfs_unmount_t    ntfs_unmount;
84
85 static b_strategy_t     ntfs_bufstrategy;
86
87 /* 
88  * Buffer operations for NTFS vnodes.
89  * We punt on VOP_BMAP, so we need to do
90  * strategy on the file's vnode rather
91  * than the underlying device's
92  */
93 static struct buf_ops ntfs_vnbufops = {
94         .bop_name     = "NTFS",
95         .bop_strategy = ntfs_bufstrategy,
96 };
97
98 static int
99 ntfs_init (
100         struct vfsconf *vcp )
101 {
102         ntfs_nthashinit();
103         ntfs_toupper_init();
104         return 0;
105 }
106
107 static int
108 ntfs_uninit (
109         struct vfsconf *vcp )
110 {
111         ntfs_toupper_destroy();
112         ntfs_nthashdestroy();
113         return 0;
114 }
115
116 static int
117 ntfs_cmount ( 
118         struct mntarg *ma,
119         void *data,
120         int flags,
121         struct thread *td )
122 {
123         int error;
124         struct ntfs_args args;
125
126         error = copyin(data, (caddr_t)&args, sizeof args);
127         if (error)
128                 return (error);
129         ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
130         ma = mount_arg(ma, "export", &args.export, sizeof args.export);
131         ma = mount_argf(ma, "uid", "%d", args.uid);
132         ma = mount_argf(ma, "gid", "%d", args.gid);
133         ma = mount_argf(ma, "mode", "%d", args.mode);
134         ma = mount_argb(ma, args.flag & NTFS_MFLAG_CASEINS, "nocaseins");
135         ma = mount_argb(ma, args.flag & NTFS_MFLAG_ALLNAMES, "noallnames");
136         if (args.flag & NTFS_MFLAG_KICONV) {
137                 ma = mount_argsu(ma, "cs_ntfs", args.cs_ntfs, 64);
138                 ma = mount_argsu(ma, "cs_local", args.cs_local, 64);
139         }
140
141         error = kernel_mount(ma, flags);
142
143         return (error);
144 }
145
146 static const char *ntfs_opts[] = {
147         "from", "export", "uid", "gid", "mode", "caseins", "allnames",
148         "kiconv", "cs_ntfs", "cs_local", NULL
149 };
150
151 static int
152 ntfs_mount ( 
153         struct mount *mp,
154         struct thread *td )
155 {
156         int             err = 0, error;
157         struct vnode    *devvp;
158         struct nameidata ndp;
159         char *from;
160
161         if (vfs_filteropt(mp->mnt_optnew, ntfs_opts))
162                 return (EINVAL);
163
164         from = vfs_getopts(mp->mnt_optnew, "from", &error);
165         if (error)      
166                 return (error);
167
168         /*
169          * If updating, check whether changing from read-only to
170          * read/write.
171          */
172         if (mp->mnt_flag & MNT_UPDATE) {
173                 if (vfs_flagopt(mp->mnt_optnew, "export", NULL, 0)) {
174                         /* Process export requests in vfs_mount.c */
175                         goto success;
176                 } else {
177                         printf("ntfs_mount(): MNT_UPDATE not supported\n");
178                         err = EINVAL;
179                         goto error_1;
180                 }
181         }
182
183         /*
184          * Not an update, or updating the name: look up the name
185          * and verify that it refers to a sensible block device.
186          */
187         NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, from, td);
188         err = namei(&ndp);
189         if (err) {
190                 /* can't get devvp!*/
191                 goto error_1;
192         }
193         NDFREE(&ndp, NDF_ONLY_PNBUF);
194         devvp = ndp.ni_vp;
195
196         if (!vn_isdisk(devvp, &err))  {
197                 vput(devvp);
198                 return (err);
199         }
200
201         if (mp->mnt_flag & MNT_UPDATE) {
202 #if 0
203                 /*
204                  ********************
205                  * UPDATE
206                  ********************
207                  */
208
209                 if (devvp != ntmp->um_devvp)
210                         err = EINVAL;   /* needs translation */
211                 vput(devvp);
212                 if (err)
213                         return (err);
214 #endif
215         } else {
216                 /*
217                  ********************
218                  * NEW MOUNT
219                  ********************
220                  */
221
222                 /*
223                  * Since this is a new mount, we want the names for
224                  * the device and the mount point copied in.  If an
225                  * error occurs, the mountpoint is discarded by the
226                  * upper level code.  Note that vfs_mount() handles
227                  * copying the mountpoint f_mntonname for us, so we
228                  * don't have to do it here unless we want to set it
229                  * to something other than "path" for some rason.
230                  */
231                 /* Save "mounted from" info for mount point (NULL pad)*/
232                 vfs_mountedfrom(mp, from);
233
234                 err = ntfs_mountfs(devvp, mp, td);
235         }
236         if (err) {
237                 vrele(devvp);
238                 return (err);
239         }
240
241         goto success;
242
243 error_1:        /* no state to back out*/
244         /* XXX: missing NDFREE(&ndp, ...) */
245
246 success:
247         return(err);
248 }
249
250 /*
251  * Common code for mount and mountroot
252  */
253 int
254 ntfs_mountfs(devvp, mp, td)
255         register struct vnode *devvp;
256         struct mount *mp;
257         struct thread *td;
258 {
259         struct buf *bp;
260         struct ntfsmount *ntmp;
261         struct cdev *dev = devvp->v_rdev;
262         int error, ronly, i, v;
263         struct vnode *vp;
264         struct g_consumer *cp;
265         struct g_provider *pp;
266         char *cs_ntfs, *cs_local;
267
268         ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
269         DROP_GIANT();
270         g_topology_lock();
271
272         /*
273          * XXX: Do not allow more than one consumer to open a device
274          *      associated with a particular GEOM provider.
275          *      This disables multiple read-only mounts of a device,
276          *      but it gets rid of panics in vget() when you try to
277          *      mount the same device more than once.
278          */
279         pp = g_dev_getprovider(devvp->v_rdev);
280         if ((pp != NULL) && ((pp->acr | pp->acw | pp->ace ) != 0)) 
281                 error = EPERM;
282         else 
283                 error = g_vfs_open(devvp, &cp, "ntfs", ronly ? 0 : 1);
284
285         g_topology_unlock();
286         PICKUP_GIANT();
287         VOP_UNLOCK(devvp, 0, td);
288         if (error)
289                 return (error);
290
291         bp = NULL;
292
293         error = bread(devvp, BBLOCK, BBSIZE, NOCRED, &bp);
294         if (error)
295                 goto out;
296         ntmp = malloc( sizeof *ntmp, M_NTFSMNT, M_WAITOK | M_ZERO);
297         bcopy( bp->b_data, &ntmp->ntm_bootfile, sizeof(struct bootfile) );
298         /*
299          * We must not cache the boot block if its size is not exactly
300          * one cluster in order to avoid confusing the buffer cache when
301          * the boot file is read later by ntfs_readntvattr_plain(), which
302          * reads a cluster at a time.
303          */
304         if (ntfs_cntob(1) != BBSIZE)
305                 bp->b_flags |= B_NOCACHE;
306         brelse( bp );
307         bp = NULL;
308
309         if (strncmp((const char *)ntmp->ntm_bootfile.bf_sysid, NTFS_BBID, NTFS_BBIDLEN)) {
310                 error = EINVAL;
311                 dprintf(("ntfs_mountfs: invalid boot block\n"));
312                 goto out;
313         }
314
315         {
316                 int8_t cpr = ntmp->ntm_mftrecsz;
317                 if( cpr > 0 )
318                         ntmp->ntm_bpmftrec = ntmp->ntm_spc * cpr;
319                 else
320                         ntmp->ntm_bpmftrec = (1 << (-cpr)) / ntmp->ntm_bps;
321         }
322         dprintf(("ntfs_mountfs(): bps: %d, spc: %d, media: %x, mftrecsz: %d (%d sects)\n",
323                 ntmp->ntm_bps,ntmp->ntm_spc,ntmp->ntm_bootfile.bf_media,
324                 ntmp->ntm_mftrecsz,ntmp->ntm_bpmftrec));
325         dprintf(("ntfs_mountfs(): mftcn: 0x%x|0x%x\n",
326                 (u_int32_t)ntmp->ntm_mftcn,(u_int32_t)ntmp->ntm_mftmirrcn));
327
328         ntmp->ntm_mountp = mp;
329         ntmp->ntm_devvp = devvp;
330         if (1 == vfs_scanopt(mp->mnt_optnew, "uid", "%d", &v))
331                 ntmp->ntm_uid = v;
332         if (1 == vfs_scanopt(mp->mnt_optnew, "gid", "%d", &v))
333                 ntmp->ntm_gid = v;
334         if (1 == vfs_scanopt(mp->mnt_optnew, "mode", "%d", &v))
335                 ntmp->ntm_mode = v & ACCESSPERMS;
336         vfs_flagopt(mp->mnt_optnew,
337             "caseins", &ntmp->ntm_flag, NTFS_MFLAG_CASEINS);
338         vfs_flagopt(mp->mnt_optnew,
339             "allnames", &ntmp->ntm_flag, NTFS_MFLAG_ALLNAMES);
340         ntmp->ntm_cp = cp;
341         ntmp->ntm_bo = &devvp->v_bufobj;
342
343         cs_local = vfs_getopts(mp->mnt_optnew, "cs_local", &error);
344         if (error && error != ENOENT)
345                 goto out;
346         cs_ntfs = vfs_getopts(mp->mnt_optnew, "cs_ntfs", &error);
347         if (error && error != ENOENT)
348                 goto out;
349         /* Copy in the 8-bit to Unicode conversion table */
350         /* Initialize Unicode to 8-bit table from 8toU table */
351         ntfs_82u_init(ntmp, cs_local, cs_ntfs);
352         if (cs_local != NULL && cs_ntfs != NULL)
353                 ntfs_u28_init(ntmp, NULL, cs_local, cs_ntfs);
354         else
355                 ntfs_u28_init(ntmp, ntmp->ntm_82u, cs_local, cs_ntfs);
356
357         mp->mnt_data = (qaddr_t)ntmp;
358
359         dprintf(("ntfs_mountfs(): case-%s,%s uid: %d, gid: %d, mode: %o\n",
360                 (ntmp->ntm_flag & NTFS_MFLAG_CASEINS)?"insens.":"sens.",
361                 (ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES)?" allnames,":"",
362                 ntmp->ntm_uid, ntmp->ntm_gid, ntmp->ntm_mode));
363
364         /*
365          * We read in some system nodes to do not allow 
366          * reclaim them and to have everytime access to them.
367          */ 
368         {
369                 int pi[3] = { NTFS_MFTINO, NTFS_ROOTINO, NTFS_BITMAPINO };
370                 for (i=0; i<3; i++) {
371                         error = VFS_VGET(mp, pi[i], LK_EXCLUSIVE,
372                                          &(ntmp->ntm_sysvn[pi[i]]));
373                         if(error)
374                                 goto out1;
375                         ntmp->ntm_sysvn[pi[i]]->v_vflag |= VV_SYSTEM;
376                         VREF(ntmp->ntm_sysvn[pi[i]]);
377                         vput(ntmp->ntm_sysvn[pi[i]]);
378                 }
379         }
380
381         /* read the Unicode lowercase --> uppercase translation table,
382          * if necessary */
383         if ((error = ntfs_toupper_use(mp, ntmp)))
384                 goto out1;
385
386         /*
387          * Scan $BitMap and count free clusters
388          */
389         error = ntfs_calccfree(ntmp, &ntmp->ntm_cfree);
390         if(error)
391                 goto out1;
392
393         /*
394          * Read and translate to internal format attribute
395          * definition file. 
396          */
397         {
398                 int num,j;
399                 struct attrdef ad;
400
401                 /* Open $AttrDef */
402                 error = VFS_VGET(mp, NTFS_ATTRDEFINO, LK_EXCLUSIVE, &vp );
403                 if(error) 
404                         goto out1;
405
406                 /* Count valid entries */
407                 for(num=0;;num++) {
408                         error = ntfs_readattr(ntmp, VTONT(vp),
409                                         NTFS_A_DATA, NULL,
410                                         num * sizeof(ad), sizeof(ad),
411                                         &ad, NULL);
412                         if (error)
413                                 goto out1;
414                         if (ad.ad_name[0] == 0)
415                                 break;
416                 }
417
418                 /* Alloc memory for attribute definitions */
419                 MALLOC(ntmp->ntm_ad, struct ntvattrdef *,
420                         num * sizeof(struct ntvattrdef),
421                         M_NTFSMNT, M_WAITOK);
422
423                 ntmp->ntm_adnum = num;
424
425                 /* Read them and translate */
426                 for(i=0;i<num;i++){
427                         error = ntfs_readattr(ntmp, VTONT(vp),
428                                         NTFS_A_DATA, NULL,
429                                         i * sizeof(ad), sizeof(ad),
430                                         &ad, NULL);
431                         if (error)
432                                 goto out1;
433                         j = 0;
434                         do {
435                                 ntmp->ntm_ad[i].ad_name[j] = ad.ad_name[j];
436                         } while(ad.ad_name[j++]);
437                         ntmp->ntm_ad[i].ad_namelen = j - 1;
438                         ntmp->ntm_ad[i].ad_type = ad.ad_type;
439                 }
440
441                 vput(vp);
442         }
443
444         mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
445         mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
446         mp->mnt_maxsymlinklen = 0;
447         MNT_ILOCK(mp);
448         mp->mnt_flag |= MNT_LOCAL;
449         MNT_IUNLOCK(mp);
450         return (0);
451
452 out1:
453         for(i=0;i<NTFS_SYSNODESNUM;i++)
454                 if(ntmp->ntm_sysvn[i]) vrele(ntmp->ntm_sysvn[i]);
455
456         if (vflush(mp, 0, 0, td))
457                 dprintf(("ntfs_mountfs: vflush failed\n"));
458
459 out:
460         if (bp)
461                 brelse(bp);
462
463         DROP_GIANT();
464         g_topology_lock();
465         g_vfs_close(cp, td);
466         g_topology_unlock();
467         PICKUP_GIANT();
468         
469         return (error);
470 }
471
472 static int
473 ntfs_unmount( 
474         struct mount *mp,
475         int mntflags,
476         struct thread *td)
477 {
478         struct ntfsmount *ntmp;
479         int error, flags, i;
480
481         dprintf(("ntfs_unmount: unmounting...\n"));
482         ntmp = VFSTONTFS(mp);
483
484         flags = 0;
485         if(mntflags & MNT_FORCE)
486                 flags |= FORCECLOSE;
487
488         dprintf(("ntfs_unmount: vflushing...\n"));
489         error = vflush(mp, 0, flags | SKIPSYSTEM, td);
490         if (error) {
491                 printf("ntfs_unmount: vflush failed: %d\n",error);
492                 return (error);
493         }
494
495         /* Check if only system vnodes are rest */
496         for(i=0;i<NTFS_SYSNODESNUM;i++)
497                  if((ntmp->ntm_sysvn[i]) && 
498                     (vrefcnt(ntmp->ntm_sysvn[i]) > 1)) return (EBUSY);
499
500         /* Dereference all system vnodes */
501         for(i=0;i<NTFS_SYSNODESNUM;i++)
502                  if(ntmp->ntm_sysvn[i]) vrele(ntmp->ntm_sysvn[i]);
503
504         /* vflush system vnodes */
505         error = vflush(mp, 0, flags, td);
506         if (error)
507                 printf("ntfs_unmount: vflush failed(sysnodes): %d\n",error);
508
509         vinvalbuf(ntmp->ntm_devvp, V_SAVE, td, 0, 0);
510
511         DROP_GIANT();
512         g_topology_lock();
513         g_vfs_close(ntmp->ntm_cp, td);
514         g_topology_unlock();
515         PICKUP_GIANT();
516
517         vrele(ntmp->ntm_devvp);
518
519         /* free the toupper table, if this has been last mounted ntfs volume */
520         ntfs_toupper_unuse();
521
522         dprintf(("ntfs_umount: freeing memory...\n"));
523         ntfs_u28_uninit(ntmp);
524         ntfs_82u_uninit(ntmp);
525         mp->mnt_data = (qaddr_t)0;
526         MNT_ILOCK(mp);
527         mp->mnt_flag &= ~MNT_LOCAL;
528         MNT_IUNLOCK(mp);
529         FREE(ntmp->ntm_ad, M_NTFSMNT);
530         FREE(ntmp, M_NTFSMNT);
531         return (error);
532 }
533
534 static int
535 ntfs_root(
536         struct mount *mp,
537         int flags,
538         struct vnode **vpp,
539         struct thread *td )
540 {
541         struct vnode *nvp;
542         int error = 0;
543
544         dprintf(("ntfs_root(): sysvn: %p\n",
545                 VFSTONTFS(mp)->ntm_sysvn[NTFS_ROOTINO]));
546         error = VFS_VGET(mp, (ino_t)NTFS_ROOTINO, LK_EXCLUSIVE, &nvp);
547         if(error) {
548                 printf("ntfs_root: VFS_VGET failed: %d\n",error);
549                 return (error);
550         }
551
552         *vpp = nvp;
553         return (0);
554 }
555
556 static int
557 ntfs_calccfree(
558         struct ntfsmount *ntmp,
559         cn_t *cfreep)
560 {
561         struct vnode *vp;
562         u_int8_t *tmp;
563         int j, error;
564         long cfree = 0;
565         size_t bmsize, i;
566
567         vp = ntmp->ntm_sysvn[NTFS_BITMAPINO];
568
569         bmsize = VTOF(vp)->f_size;
570
571         MALLOC(tmp, u_int8_t *, bmsize, M_TEMP, M_WAITOK);
572
573         error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
574                                0, bmsize, tmp, NULL);
575         if (error)
576                 goto out;
577
578         for(i=0;i<bmsize;i++)
579                 for(j=0;j<8;j++)
580                         if(~tmp[i] & (1 << j)) cfree++;
581         *cfreep = cfree;
582
583     out:
584         FREE(tmp, M_TEMP);
585         return(error);
586 }
587
588 static int
589 ntfs_statfs(
590         struct mount *mp,
591         struct statfs *sbp,
592         struct thread *td)
593 {
594         struct ntfsmount *ntmp = VFSTONTFS(mp);
595         u_int64_t mftsize,mftallocated;
596
597         dprintf(("ntfs_statfs():\n"));
598
599         mftsize = VTOF(ntmp->ntm_sysvn[NTFS_MFTINO])->f_size;
600         mftallocated = VTOF(ntmp->ntm_sysvn[NTFS_MFTINO])->f_allocated;
601
602         sbp->f_type = mp->mnt_vfc->vfc_typenum;
603         sbp->f_bsize = ntmp->ntm_bps;
604         sbp->f_iosize = ntmp->ntm_bps * ntmp->ntm_spc;
605         sbp->f_blocks = ntmp->ntm_bootfile.bf_spv;
606         sbp->f_bfree = sbp->f_bavail = ntfs_cntobn(ntmp->ntm_cfree);
607         sbp->f_ffree = sbp->f_bfree / ntmp->ntm_bpmftrec;
608         sbp->f_files = mftallocated / ntfs_bntob(ntmp->ntm_bpmftrec) +
609                        sbp->f_ffree;
610         sbp->f_flags = mp->mnt_flag;
611
612         return (0);
613 }
614
615 /*ARGSUSED*/
616 static int
617 ntfs_fhtovp(
618         struct mount *mp,
619         struct fid *fhp,
620         struct vnode **vpp)
621 {
622         struct vnode *nvp;
623         struct ntfid *ntfhp = (struct ntfid *)fhp;
624         int error;
625
626         ddprintf(("ntfs_fhtovp(): %d\n", ntfhp->ntfid_ino));
627
628         if ((error = VFS_VGET(mp, ntfhp->ntfid_ino, LK_EXCLUSIVE, &nvp)) != 0) {
629                 *vpp = NULLVP;
630                 return (error);
631         }
632         /* XXX as unlink/rmdir/mkdir/creat are not currently possible
633          * with NTFS, we don't need to check anything else for now */
634         *vpp = nvp;
635         vnode_create_vobject(nvp, VTOF(nvp)->f_size, curthread);
636         return (0);
637 }
638
639 int
640 ntfs_vgetex(
641         struct mount *mp,
642         ino_t ino,
643         u_int32_t attrtype,
644         char *attrname,
645         u_long lkflags,
646         u_long flags,
647         struct thread *td,
648         struct vnode **vpp) 
649 {
650         int error;
651         register struct ntfsmount *ntmp;
652         struct ntnode *ip;
653         struct fnode *fp;
654         struct vnode *vp;
655         enum vtype f_type;
656
657         dprintf(("ntfs_vgetex: ino: %d, attr: 0x%x:%s, lkf: 0x%lx, f: 0x%lx\n",
658                 ino, attrtype, attrname?attrname:"", (u_long)lkflags,
659                 (u_long)flags));
660
661         ntmp = VFSTONTFS(mp);
662         *vpp = NULL;
663
664         /* Get ntnode */
665         error = ntfs_ntlookup(ntmp, ino, &ip);
666         if (error) {
667                 printf("ntfs_vget: ntfs_ntget failed\n");
668                 return (error);
669         }
670
671         /* It may be not initialized fully, so force load it */
672         if (!(flags & VG_DONTLOADIN) && !(ip->i_flag & IN_LOADED)) {
673                 error = ntfs_loadntnode(ntmp, ip);
674                 if(error) {
675                         printf("ntfs_vget: CAN'T LOAD ATTRIBUTES FOR INO: %d\n",
676                                ip->i_number);
677                         ntfs_ntput(ip);
678                         return (error);
679                 }
680         }
681
682         error = ntfs_fget(ntmp, ip, attrtype, attrname, &fp);
683         if (error) {
684                 printf("ntfs_vget: ntfs_fget failed\n");
685                 ntfs_ntput(ip);
686                 return (error);
687         }
688
689         f_type = VNON;
690         if (!(flags & VG_DONTVALIDFN) && !(fp->f_flag & FN_VALID)) {
691                 if ((ip->i_frflag & NTFS_FRFLAG_DIR) &&
692                     (fp->f_attrtype == NTFS_A_DATA && fp->f_attrname == NULL)) {
693                         f_type = VDIR;
694                 } else if (flags & VG_EXT) {
695                         f_type = VNON;
696                         fp->f_size = fp->f_allocated = 0;
697                 } else {
698                         f_type = VREG;  
699
700                         error = ntfs_filesize(ntmp, fp, 
701                                               &fp->f_size, &fp->f_allocated);
702                         if (error) {
703                                 ntfs_ntput(ip);
704                                 return (error);
705                         }
706                 }
707
708                 fp->f_flag |= FN_VALID;
709         }
710
711         if (FTOV(fp)) {
712                 vget(FTOV(fp), lkflags, td);
713                 *vpp = FTOV(fp);
714                 ntfs_ntput(ip);
715                 return (0);
716         }
717
718         error = getnewvnode("ntfs", ntmp->ntm_mountp, &ntfs_vnodeops, &vp);
719         if(error) {
720                 ntfs_frele(fp);
721                 ntfs_ntput(ip);
722                 return (error);
723         }
724         /* XXX: Too early for mpsafe fs, lacks vnode lock */
725         error = insmntque(vp, ntmp->ntm_mountp);
726         if (error) {
727                 ntfs_frele(fp);
728                 ntfs_ntput(ip);
729                 return (error);
730         }
731         dprintf(("ntfs_vget: vnode: %p for ntnode: %d\n", vp,ino));
732
733         fp->f_vp = vp;
734         vp->v_data = fp;
735         vp->v_type = f_type;
736
737         vp->v_bufobj.bo_ops = &ntfs_vnbufops;
738         vp->v_bufobj.bo_private = vp;
739
740         if (ino == NTFS_ROOTINO)
741                 vp->v_vflag |= VV_ROOT;
742
743         ntfs_ntput(ip);
744
745         if (lkflags & LK_TYPE_MASK) {
746                 error = vn_lock(vp, lkflags, td);
747                 if (error) {
748                         vput(vp);
749                         return (error);
750                 }
751         }
752
753         *vpp = vp;
754         return (0);
755         
756 }
757
758 static int
759 ntfs_vget(
760         struct mount *mp,
761         ino_t ino,
762         int lkflags,
763         struct vnode **vpp) 
764 {
765         return ntfs_vgetex(mp, ino, NTFS_A_DATA, NULL, lkflags, 0,
766             curthread, vpp);
767 }
768
769 static void
770 ntfs_bufstrategy(struct bufobj *bo, struct buf *bp)
771 {
772         struct vnode *vp;
773         int rc;
774
775         vp = bo->bo_private;
776         KASSERT(bo == &vp->v_bufobj, ("BO/VP mismatch: vp %p bo %p != %p",
777             vp, &vp->v_bufobj, bo));
778         rc = VOP_STRATEGY(vp, bp);
779         KASSERT(rc == 0, ("NTFS VOP_STRATEGY failed: bp=%p, "
780                 "vp=%p, rc=%d", bp, vp, rc));
781 }
782
783 static struct vfsops ntfs_vfsops = {
784         .vfs_fhtovp =   ntfs_fhtovp,
785         .vfs_init =     ntfs_init,
786         .vfs_cmount =   ntfs_cmount,
787         .vfs_mount =    ntfs_mount,
788         .vfs_root =     ntfs_root,
789         .vfs_statfs =   ntfs_statfs,
790         .vfs_uninit =   ntfs_uninit,
791         .vfs_unmount =  ntfs_unmount,
792         .vfs_vget =     ntfs_vget,
793 };
794 VFS_SET(ntfs_vfsops, ntfs, 0);
795 MODULE_VERSION(ntfs, 1);