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