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: releng/6.1/sys/fs/ntfs/ntfs_vfsops.c 155853 2006-02-20 00:53:15Z yar $
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/systm.h>
45
46 #include <geom/geom.h>
47 #include <geom/geom_vfs.h>
48
49 #include <vm/vm.h>
50 #include <vm/vm_param.h>
51 #include <vm/vm_page.h>
52 #include <vm/vm_object.h>
53 #include <vm/vm_extern.h>
54
55 /*#define NTFS_DEBUG 1*/
56 #include <fs/ntfs/ntfs.h>
57 #include <fs/ntfs/ntfs_inode.h>
58 #include <fs/ntfs/ntfs_subr.h>
59 #include <fs/ntfs/ntfs_vfsops.h>
60 #include <fs/ntfs/ntfs_ihash.h>
61 #include <fs/ntfs/ntfsmount.h>
62
63 static MALLOC_DEFINE(M_NTFSMNT, "NTFS mount", "NTFS mount structure");
64 MALLOC_DEFINE(M_NTFSNTNODE,"NTFS ntnode", "NTFS ntnode information");
65 MALLOC_DEFINE(M_NTFSFNODE,"NTFS fnode", "NTFS fnode information");
66 MALLOC_DEFINE(M_NTFSDIR,"NTFS dir", "NTFS dir buffer");
67
68 struct sockaddr;
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 static vfs_vptofh_t ntfs_vptofh;
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 struct export_args export;
161
162 if (vfs_filteropt(mp->mnt_optnew, ntfs_opts))
163 return (EINVAL);
164
165 from = vfs_getopts(mp->mnt_optnew, "from", &error);
166 if (error)
167 return (error);
168
169 /*
170 * If updating, check whether changing from read-only to
171 * read/write.
172 */
173 if (mp->mnt_flag & MNT_UPDATE) {
174 error = vfs_copyopt(mp->mnt_optnew, "export",
175 &export, sizeof export);
176 if ((error == 0) && export.ex_flags != 0) {
177 /*
178 * Process export requests. Jumping to "success"
179 * will return the vfs_export() error code.
180 */
181 err = vfs_export(mp, &export);
182 goto success;
183 }
184
185 printf("ntfs_mount(): MNT_UPDATE not supported\n");
186 err = EINVAL;
187 goto error_1;
188 }
189
190 /*
191 * Not an update, or updating the name: look up the name
192 * and verify that it refers to a sensible block device.
193 */
194 NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, from, td);
195 err = namei(&ndp);
196 if (err) {
197 /* can't get devvp!*/
198 goto error_1;
199 }
200 NDFREE(&ndp, NDF_ONLY_PNBUF);
201 devvp = ndp.ni_vp;
202
203 if (!vn_isdisk(devvp, &err)) {
204 vput(devvp);
205 return (err);
206 }
207
208 if (mp->mnt_flag & MNT_UPDATE) {
209 #if 0
210 /*
211 ********************
212 * UPDATE
213 ********************
214 */
215
216 if (devvp != ntmp->um_devvp)
217 err = EINVAL; /* needs translation */
218 vput(devvp);
219 if (err)
220 return (err);
221 #endif
222 } else {
223 /*
224 ********************
225 * NEW MOUNT
226 ********************
227 */
228
229 /*
230 * Since this is a new mount, we want the names for
231 * the device and the mount point copied in. If an
232 * error occurs, the mountpoint is discarded by the
233 * upper level code. Note that vfs_mount() handles
234 * copying the mountpoint f_mntonname for us, so we
235 * don't have to do it here unless we want to set it
236 * to something other than "path" for some rason.
237 */
238 /* Save "mounted from" info for mount point (NULL pad)*/
239 vfs_mountedfrom(mp, from);
240
241 err = ntfs_mountfs(devvp, mp, td);
242 }
243 if (err) {
244 vrele(devvp);
245 return (err);
246 }
247
248 goto success;
249
250 error_1: /* no state to back out*/
251 /* XXX: missing NDFREE(&ndp, ...) */
252
253 success:
254 return(err);
255 }
256
257 /*
258 * Common code for mount and mountroot
259 */
260 int
261 ntfs_mountfs(devvp, mp, td)
262 register struct vnode *devvp;
263 struct mount *mp;
264 struct thread *td;
265 {
266 struct buf *bp;
267 struct ntfsmount *ntmp;
268 struct cdev *dev = devvp->v_rdev;
269 int error, ronly, i, v;
270 struct vnode *vp;
271 struct g_consumer *cp;
272 char *cs_ntfs, *cs_local;
273
274 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
275 DROP_GIANT();
276 g_topology_lock();
277 error = g_vfs_open(devvp, &cp, "ntfs", ronly ? 0 : 1);
278 g_topology_unlock();
279 PICKUP_GIANT();
280 VOP_UNLOCK(devvp, 0, td);
281 if (error)
282 return (error);
283
284 bp = NULL;
285
286 error = bread(devvp, BBLOCK, BBSIZE, NOCRED, &bp);
287 if (error)
288 goto out;
289 ntmp = malloc( sizeof *ntmp, M_NTFSMNT, M_WAITOK | M_ZERO);
290 bcopy( bp->b_data, &ntmp->ntm_bootfile, sizeof(struct bootfile) );
291 /*
292 * We must not cache the boot block if its size is not exactly
293 * one cluster in order to avoid confusing the buffer cache when
294 * the boot file is read later by ntfs_readntvattr_plain(), which
295 * reads a cluster at a time.
296 */
297 if (ntfs_cntob(1) != BBSIZE)
298 bp->b_flags |= B_NOCACHE;
299 brelse( bp );
300 bp = NULL;
301
302 if (strncmp(ntmp->ntm_bootfile.bf_sysid, NTFS_BBID, NTFS_BBIDLEN)) {
303 error = EINVAL;
304 dprintf(("ntfs_mountfs: invalid boot block\n"));
305 goto out;
306 }
307
308 {
309 int8_t cpr = ntmp->ntm_mftrecsz;
310 if( cpr > 0 )
311 ntmp->ntm_bpmftrec = ntmp->ntm_spc * cpr;
312 else
313 ntmp->ntm_bpmftrec = (1 << (-cpr)) / ntmp->ntm_bps;
314 }
315 dprintf(("ntfs_mountfs(): bps: %d, spc: %d, media: %x, mftrecsz: %d (%d sects)\n",
316 ntmp->ntm_bps,ntmp->ntm_spc,ntmp->ntm_bootfile.bf_media,
317 ntmp->ntm_mftrecsz,ntmp->ntm_bpmftrec));
318 dprintf(("ntfs_mountfs(): mftcn: 0x%x|0x%x\n",
319 (u_int32_t)ntmp->ntm_mftcn,(u_int32_t)ntmp->ntm_mftmirrcn));
320
321 ntmp->ntm_mountp = mp;
322 ntmp->ntm_devvp = devvp;
323 if (1 == vfs_scanopt(mp->mnt_optnew, "uid", "%d", &v))
324 ntmp->ntm_uid = v;
325 if (1 == vfs_scanopt(mp->mnt_optnew, "gid", "%d", &v))
326 ntmp->ntm_gid = v;
327 if (1 == vfs_scanopt(mp->mnt_optnew, "mode", "%d", &v))
328 ntmp->ntm_mode = v;
329 vfs_flagopt(mp->mnt_optnew,
330 "caseins", &ntmp->ntm_flag, NTFS_MFLAG_CASEINS);
331 vfs_flagopt(mp->mnt_optnew,
332 "allnames", &ntmp->ntm_flag, NTFS_MFLAG_ALLNAMES);
333 ntmp->ntm_cp = cp;
334 ntmp->ntm_bo = &devvp->v_bufobj;
335
336 cs_local = vfs_getopts(mp->mnt_optnew, "cs_local", &error);
337 if (error)
338 goto out;
339 cs_ntfs = vfs_getopts(mp->mnt_optnew, "cs_ntfs", &error);
340 if (error)
341 goto out;
342 /* Copy in the 8-bit to Unicode conversion table */
343 /* Initialize Unicode to 8-bit table from 8toU table */
344 ntfs_82u_init(ntmp, cs_local, cs_ntfs);
345 if (cs_local != NULL && cs_ntfs != NULL)
346 ntfs_u28_init(ntmp, NULL, cs_local, cs_ntfs);
347 else
348 ntfs_u28_init(ntmp, ntmp->ntm_82u, cs_local, cs_ntfs);
349
350 mp->mnt_data = (qaddr_t)ntmp;
351
352 dprintf(("ntfs_mountfs(): case-%s,%s uid: %d, gid: %d, mode: %o\n",
353 (ntmp->ntm_flag & NTFS_MFLAG_CASEINS)?"insens.":"sens.",
354 (ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES)?" allnames,":"",
355 ntmp->ntm_uid, ntmp->ntm_gid, ntmp->ntm_mode));
356
357 /*
358 * We read in some system nodes to do not allow
359 * reclaim them and to have everytime access to them.
360 */
361 {
362 int pi[3] = { NTFS_MFTINO, NTFS_ROOTINO, NTFS_BITMAPINO };
363 for (i=0; i<3; i++) {
364 error = VFS_VGET(mp, pi[i], LK_EXCLUSIVE,
365 &(ntmp->ntm_sysvn[pi[i]]));
366 if(error)
367 goto out1;
368 ntmp->ntm_sysvn[pi[i]]->v_vflag |= VV_SYSTEM;
369 VREF(ntmp->ntm_sysvn[pi[i]]);
370 vput(ntmp->ntm_sysvn[pi[i]]);
371 }
372 }
373
374 /* read the Unicode lowercase --> uppercase translation table,
375 * if necessary */
376 if ((error = ntfs_toupper_use(mp, ntmp)))
377 goto out1;
378
379 /*
380 * Scan $BitMap and count free clusters
381 */
382 error = ntfs_calccfree(ntmp, &ntmp->ntm_cfree);
383 if(error)
384 goto out1;
385
386 /*
387 * Read and translate to internal format attribute
388 * definition file.
389 */
390 {
391 int num,j;
392 struct attrdef ad;
393
394 /* Open $AttrDef */
395 error = VFS_VGET(mp, NTFS_ATTRDEFINO, LK_EXCLUSIVE, &vp );
396 if(error)
397 goto out1;
398
399 /* Count valid entries */
400 for(num=0;;num++) {
401 error = ntfs_readattr(ntmp, VTONT(vp),
402 NTFS_A_DATA, NULL,
403 num * sizeof(ad), sizeof(ad),
404 &ad, NULL);
405 if (error)
406 goto out1;
407 if (ad.ad_name[0] == 0)
408 break;
409 }
410
411 /* Alloc memory for attribute definitions */
412 MALLOC(ntmp->ntm_ad, struct ntvattrdef *,
413 num * sizeof(struct ntvattrdef),
414 M_NTFSMNT, M_WAITOK);
415
416 ntmp->ntm_adnum = num;
417
418 /* Read them and translate */
419 for(i=0;i<num;i++){
420 error = ntfs_readattr(ntmp, VTONT(vp),
421 NTFS_A_DATA, NULL,
422 i * sizeof(ad), sizeof(ad),
423 &ad, NULL);
424 if (error)
425 goto out1;
426 j = 0;
427 do {
428 ntmp->ntm_ad[i].ad_name[j] = ad.ad_name[j];
429 } while(ad.ad_name[j++]);
430 ntmp->ntm_ad[i].ad_namelen = j - 1;
431 ntmp->ntm_ad[i].ad_type = ad.ad_type;
432 }
433
434 vput(vp);
435 }
436
437 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
438 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
439 mp->mnt_maxsymlinklen = 0;
440 mp->mnt_flag |= MNT_LOCAL;
441 return (0);
442
443 out1:
444 for(i=0;i<NTFS_SYSNODESNUM;i++)
445 if(ntmp->ntm_sysvn[i]) vrele(ntmp->ntm_sysvn[i]);
446
447 if (vflush(mp, 0, 0, td))
448 dprintf(("ntfs_mountfs: vflush failed\n"));
449
450 out:
451 if (bp)
452 brelse(bp);
453
454 DROP_GIANT();
455 g_topology_lock();
456 g_vfs_close(cp, td);
457 g_topology_unlock();
458 PICKUP_GIANT();
459
460 return (error);
461 }
462
463 static int
464 ntfs_unmount(
465 struct mount *mp,
466 int mntflags,
467 struct thread *td)
468 {
469 struct ntfsmount *ntmp;
470 int error, flags, i;
471
472 dprintf(("ntfs_unmount: unmounting...\n"));
473 ntmp = VFSTONTFS(mp);
474
475 flags = 0;
476 if(mntflags & MNT_FORCE)
477 flags |= FORCECLOSE;
478
479 dprintf(("ntfs_unmount: vflushing...\n"));
480 error = vflush(mp, 0, flags | SKIPSYSTEM, td);
481 if (error) {
482 printf("ntfs_unmount: vflush failed: %d\n",error);
483 return (error);
484 }
485
486 /* Check if only system vnodes are rest */
487 for(i=0;i<NTFS_SYSNODESNUM;i++)
488 if((ntmp->ntm_sysvn[i]) &&
489 (vrefcnt(ntmp->ntm_sysvn[i]) > 1)) return (EBUSY);
490
491 /* Dereference all system vnodes */
492 for(i=0;i<NTFS_SYSNODESNUM;i++)
493 if(ntmp->ntm_sysvn[i]) vrele(ntmp->ntm_sysvn[i]);
494
495 /* vflush system vnodes */
496 error = vflush(mp, 0, flags, td);
497 if (error)
498 printf("ntfs_unmount: vflush failed(sysnodes): %d\n",error);
499
500 vinvalbuf(ntmp->ntm_devvp, V_SAVE, td, 0, 0);
501
502 DROP_GIANT();
503 g_topology_lock();
504 g_vfs_close(ntmp->ntm_cp, td);
505 g_topology_unlock();
506 PICKUP_GIANT();
507
508 vrele(ntmp->ntm_devvp);
509
510 /* free the toupper table, if this has been last mounted ntfs volume */
511 ntfs_toupper_unuse();
512
513 dprintf(("ntfs_umount: freeing memory...\n"));
514 ntfs_u28_uninit(ntmp);
515 ntfs_82u_uninit(ntmp);
516 mp->mnt_data = (qaddr_t)0;
517 mp->mnt_flag &= ~MNT_LOCAL;
518 FREE(ntmp->ntm_ad, M_NTFSMNT);
519 FREE(ntmp, M_NTFSMNT);
520 return (error);
521 }
522
523 static int
524 ntfs_root(
525 struct mount *mp,
526 int flags,
527 struct vnode **vpp,
528 struct thread *td )
529 {
530 struct vnode *nvp;
531 int error = 0;
532
533 dprintf(("ntfs_root(): sysvn: %p\n",
534 VFSTONTFS(mp)->ntm_sysvn[NTFS_ROOTINO]));
535 error = VFS_VGET(mp, (ino_t)NTFS_ROOTINO, LK_EXCLUSIVE, &nvp);
536 if(error) {
537 printf("ntfs_root: VFS_VGET failed: %d\n",error);
538 return (error);
539 }
540
541 *vpp = nvp;
542 return (0);
543 }
544
545 static int
546 ntfs_calccfree(
547 struct ntfsmount *ntmp,
548 cn_t *cfreep)
549 {
550 struct vnode *vp;
551 u_int8_t *tmp;
552 int j, error;
553 long cfree = 0;
554 size_t bmsize, i;
555
556 vp = ntmp->ntm_sysvn[NTFS_BITMAPINO];
557
558 bmsize = VTOF(vp)->f_size;
559
560 MALLOC(tmp, u_int8_t *, bmsize, M_TEMP, M_WAITOK);
561
562 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
563 0, bmsize, tmp, NULL);
564 if (error)
565 goto out;
566
567 for(i=0;i<bmsize;i++)
568 for(j=0;j<8;j++)
569 if(~tmp[i] & (1 << j)) cfree++;
570 *cfreep = cfree;
571
572 out:
573 FREE(tmp, M_TEMP);
574 return(error);
575 }
576
577 static int
578 ntfs_statfs(
579 struct mount *mp,
580 struct statfs *sbp,
581 struct thread *td)
582 {
583 struct ntfsmount *ntmp = VFSTONTFS(mp);
584 u_int64_t mftsize,mftallocated;
585
586 dprintf(("ntfs_statfs():\n"));
587
588 mftsize = VTOF(ntmp->ntm_sysvn[NTFS_MFTINO])->f_size;
589 mftallocated = VTOF(ntmp->ntm_sysvn[NTFS_MFTINO])->f_allocated;
590
591 sbp->f_type = mp->mnt_vfc->vfc_typenum;
592 sbp->f_bsize = ntmp->ntm_bps;
593 sbp->f_iosize = ntmp->ntm_bps * ntmp->ntm_spc;
594 sbp->f_blocks = ntmp->ntm_bootfile.bf_spv;
595 sbp->f_bfree = sbp->f_bavail = ntfs_cntobn(ntmp->ntm_cfree);
596 sbp->f_ffree = sbp->f_bfree / ntmp->ntm_bpmftrec;
597 sbp->f_files = mftallocated / ntfs_bntob(ntmp->ntm_bpmftrec) +
598 sbp->f_ffree;
599 sbp->f_flags = mp->mnt_flag;
600
601 return (0);
602 }
603
604 /*ARGSUSED*/
605 static int
606 ntfs_fhtovp(
607 struct mount *mp,
608 struct fid *fhp,
609 struct vnode **vpp)
610 {
611 struct vnode *nvp;
612 struct ntfid *ntfhp = (struct ntfid *)fhp;
613 int error;
614
615 ddprintf(("ntfs_fhtovp(): %d\n", ntfhp->ntfid_ino));
616
617 if ((error = VFS_VGET(mp, ntfhp->ntfid_ino, LK_EXCLUSIVE, &nvp)) != 0) {
618 *vpp = NULLVP;
619 return (error);
620 }
621 /* XXX as unlink/rmdir/mkdir/creat are not currently possible
622 * with NTFS, we don't need to check anything else for now */
623 *vpp = nvp;
624 vnode_create_vobject_off(nvp, VTOF(nvp)->f_size, curthread);
625 return (0);
626 }
627
628 static int
629 ntfs_vptofh(
630 struct vnode *vp,
631 struct fid *fhp)
632 {
633 register struct ntnode *ntp;
634 register struct ntfid *ntfhp;
635
636 ddprintf(("ntfs_fhtovp(): %p\n", vp));
637
638 ntp = VTONT(vp);
639 ntfhp = (struct ntfid *)fhp;
640 ntfhp->ntfid_len = sizeof(struct ntfid);
641 ntfhp->ntfid_ino = ntp->i_number;
642 /* ntfhp->ntfid_gen = ntp->i_gen; */
643 return (0);
644 }
645
646 int
647 ntfs_vgetex(
648 struct mount *mp,
649 ino_t ino,
650 u_int32_t attrtype,
651 char *attrname,
652 u_long lkflags,
653 u_long flags,
654 struct thread *td,
655 struct vnode **vpp)
656 {
657 int error;
658 register struct ntfsmount *ntmp;
659 struct ntnode *ip;
660 struct fnode *fp;
661 struct vnode *vp;
662 enum vtype f_type;
663
664 dprintf(("ntfs_vgetex: ino: %d, attr: 0x%x:%s, lkf: 0x%lx, f: 0x%lx\n",
665 ino, attrtype, attrname?attrname:"", (u_long)lkflags,
666 (u_long)flags));
667
668 ntmp = VFSTONTFS(mp);
669 *vpp = NULL;
670
671 /* Get ntnode */
672 error = ntfs_ntlookup(ntmp, ino, &ip);
673 if (error) {
674 printf("ntfs_vget: ntfs_ntget failed\n");
675 return (error);
676 }
677
678 /* It may be not initialized fully, so force load it */
679 if (!(flags & VG_DONTLOADIN) && !(ip->i_flag & IN_LOADED)) {
680 error = ntfs_loadntnode(ntmp, ip);
681 if(error) {
682 printf("ntfs_vget: CAN'T LOAD ATTRIBUTES FOR INO: %d\n",
683 ip->i_number);
684 ntfs_ntput(ip);
685 return (error);
686 }
687 }
688
689 error = ntfs_fget(ntmp, ip, attrtype, attrname, &fp);
690 if (error) {
691 printf("ntfs_vget: ntfs_fget failed\n");
692 ntfs_ntput(ip);
693 return (error);
694 }
695
696 f_type = VNON;
697 if (!(flags & VG_DONTVALIDFN) && !(fp->f_flag & FN_VALID)) {
698 if ((ip->i_frflag & NTFS_FRFLAG_DIR) &&
699 (fp->f_attrtype == NTFS_A_DATA && fp->f_attrname == NULL)) {
700 f_type = VDIR;
701 } else if (flags & VG_EXT) {
702 f_type = VNON;
703 fp->f_size = fp->f_allocated = 0;
704 } else {
705 f_type = VREG;
706
707 error = ntfs_filesize(ntmp, fp,
708 &fp->f_size, &fp->f_allocated);
709 if (error) {
710 ntfs_ntput(ip);
711 return (error);
712 }
713 }
714
715 fp->f_flag |= FN_VALID;
716 }
717
718 if (FTOV(fp)) {
719 vget(FTOV(fp), lkflags, td);
720 *vpp = FTOV(fp);
721 ntfs_ntput(ip);
722 return (0);
723 }
724
725 error = getnewvnode("ntfs", ntmp->ntm_mountp, &ntfs_vnodeops, &vp);
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 .vfs_vptofh = ntfs_vptofh,
794 };
795 VFS_SET(ntfs_vfsops, ntfs, 0);
796 MODULE_VERSION(ntfs, 1);
Cache object: 1a194876188d18ee8d28fba37b49e8a6
|