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.3/sys/fs/ntfs/ntfs_vfsops.c 163172 2006-10-09 19:47:17Z tegge $
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
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 char *cs_ntfs, *cs_local;
266
267 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
268 DROP_GIANT();
269 g_topology_lock();
270 error = g_vfs_open(devvp, &cp, "ntfs", ronly ? 0 : 1);
271 g_topology_unlock();
272 PICKUP_GIANT();
273 VOP_UNLOCK(devvp, 0, td);
274 if (error)
275 return (error);
276
277 bp = NULL;
278
279 error = bread(devvp, BBLOCK, BBSIZE, NOCRED, &bp);
280 if (error)
281 goto out;
282 ntmp = malloc( sizeof *ntmp, M_NTFSMNT, M_WAITOK | M_ZERO);
283 bcopy( bp->b_data, &ntmp->ntm_bootfile, sizeof(struct bootfile) );
284 /*
285 * We must not cache the boot block if its size is not exactly
286 * one cluster in order to avoid confusing the buffer cache when
287 * the boot file is read later by ntfs_readntvattr_plain(), which
288 * reads a cluster at a time.
289 */
290 if (ntfs_cntob(1) != BBSIZE)
291 bp->b_flags |= B_NOCACHE;
292 brelse( bp );
293 bp = NULL;
294
295 if (strncmp(ntmp->ntm_bootfile.bf_sysid, NTFS_BBID, NTFS_BBIDLEN)) {
296 error = EINVAL;
297 dprintf(("ntfs_mountfs: invalid boot block\n"));
298 goto out;
299 }
300
301 {
302 int8_t cpr = ntmp->ntm_mftrecsz;
303 if( cpr > 0 )
304 ntmp->ntm_bpmftrec = ntmp->ntm_spc * cpr;
305 else
306 ntmp->ntm_bpmftrec = (1 << (-cpr)) / ntmp->ntm_bps;
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 (1 == vfs_scanopt(mp->mnt_optnew, "uid", "%d", &v))
317 ntmp->ntm_uid = v;
318 if (1 == vfs_scanopt(mp->mnt_optnew, "gid", "%d", &v))
319 ntmp->ntm_gid = v;
320 if (1 == vfs_scanopt(mp->mnt_optnew, "mode", "%d", &v))
321 ntmp->ntm_mode = v;
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)
331 goto out;
332 cs_ntfs = vfs_getopts(mp->mnt_optnew, "cs_ntfs", &error);
333 if (error)
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 = (qaddr_t)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 MALLOC(ntmp->ntm_ad, struct ntvattrdef *,
406 num * sizeof(struct ntvattrdef),
407 M_NTFSMNT, M_WAITOK);
408
409 ntmp->ntm_adnum = num;
410
411 /* Read them and translate */
412 for(i=0;i<num;i++){
413 error = ntfs_readattr(ntmp, VTONT(vp),
414 NTFS_A_DATA, NULL,
415 i * sizeof(ad), sizeof(ad),
416 &ad, NULL);
417 if (error)
418 goto out1;
419 j = 0;
420 do {
421 ntmp->ntm_ad[i].ad_name[j] = ad.ad_name[j];
422 } while(ad.ad_name[j++]);
423 ntmp->ntm_ad[i].ad_namelen = j - 1;
424 ntmp->ntm_ad[i].ad_type = ad.ad_type;
425 }
426
427 vput(vp);
428 }
429
430 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
431 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
432 mp->mnt_maxsymlinklen = 0;
433 MNT_ILOCK(mp);
434 mp->mnt_flag |= MNT_LOCAL;
435 MNT_IUNLOCK(mp);
436 return (0);
437
438 out1:
439 for(i=0;i<NTFS_SYSNODESNUM;i++)
440 if(ntmp->ntm_sysvn[i]) vrele(ntmp->ntm_sysvn[i]);
441
442 if (vflush(mp, 0, 0, td))
443 dprintf(("ntfs_mountfs: vflush failed\n"));
444
445 out:
446 if (bp)
447 brelse(bp);
448
449 DROP_GIANT();
450 g_topology_lock();
451 g_vfs_close(cp, td);
452 g_topology_unlock();
453 PICKUP_GIANT();
454
455 return (error);
456 }
457
458 static int
459 ntfs_unmount(
460 struct mount *mp,
461 int mntflags,
462 struct thread *td)
463 {
464 struct ntfsmount *ntmp;
465 int error, flags, i;
466
467 dprintf(("ntfs_unmount: unmounting...\n"));
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, td, 0, 0);
496
497 DROP_GIANT();
498 g_topology_lock();
499 g_vfs_close(ntmp->ntm_cp, td);
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 = (qaddr_t)0;
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 struct thread *td )
526 {
527 struct vnode *nvp;
528 int error = 0;
529
530 dprintf(("ntfs_root(): sysvn: %p\n",
531 VFSTONTFS(mp)->ntm_sysvn[NTFS_ROOTINO]));
532 error = VFS_VGET(mp, (ino_t)NTFS_ROOTINO, LK_EXCLUSIVE, &nvp);
533 if(error) {
534 printf("ntfs_root: VFS_VGET failed: %d\n",error);
535 return (error);
536 }
537
538 *vpp = nvp;
539 return (0);
540 }
541
542 static int
543 ntfs_calccfree(
544 struct ntfsmount *ntmp,
545 cn_t *cfreep)
546 {
547 struct vnode *vp;
548 u_int8_t *tmp;
549 int j, error;
550 long cfree = 0;
551 size_t bmsize, i;
552
553 vp = ntmp->ntm_sysvn[NTFS_BITMAPINO];
554
555 bmsize = VTOF(vp)->f_size;
556
557 MALLOC(tmp, u_int8_t *, bmsize, M_TEMP, M_WAITOK);
558
559 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
560 0, bmsize, tmp, NULL);
561 if (error)
562 goto out;
563
564 for(i=0;i<bmsize;i++)
565 for(j=0;j<8;j++)
566 if(~tmp[i] & (1 << j)) cfree++;
567 *cfreep = cfree;
568
569 out:
570 FREE(tmp, M_TEMP);
571 return(error);
572 }
573
574 static int
575 ntfs_statfs(
576 struct mount *mp,
577 struct statfs *sbp,
578 struct thread *td)
579 {
580 struct ntfsmount *ntmp = VFSTONTFS(mp);
581 u_int64_t mftsize,mftallocated;
582
583 dprintf(("ntfs_statfs():\n"));
584
585 mftsize = VTOF(ntmp->ntm_sysvn[NTFS_MFTINO])->f_size;
586 mftallocated = VTOF(ntmp->ntm_sysvn[NTFS_MFTINO])->f_allocated;
587
588 sbp->f_type = mp->mnt_vfc->vfc_typenum;
589 sbp->f_bsize = ntmp->ntm_bps;
590 sbp->f_iosize = ntmp->ntm_bps * ntmp->ntm_spc;
591 sbp->f_blocks = ntmp->ntm_bootfile.bf_spv;
592 sbp->f_bfree = sbp->f_bavail = ntfs_cntobn(ntmp->ntm_cfree);
593 sbp->f_ffree = sbp->f_bfree / ntmp->ntm_bpmftrec;
594 sbp->f_files = mftallocated / ntfs_bntob(ntmp->ntm_bpmftrec) +
595 sbp->f_ffree;
596 sbp->f_flags = mp->mnt_flag;
597
598 return (0);
599 }
600
601 /*ARGSUSED*/
602 static int
603 ntfs_fhtovp(
604 struct mount *mp,
605 struct fid *fhp,
606 struct vnode **vpp)
607 {
608 struct vnode *nvp;
609 struct ntfid *ntfhp = (struct ntfid *)fhp;
610 int error;
611
612 ddprintf(("ntfs_fhtovp(): %d\n", ntfhp->ntfid_ino));
613
614 if ((error = VFS_VGET(mp, ntfhp->ntfid_ino, LK_EXCLUSIVE, &nvp)) != 0) {
615 *vpp = NULLVP;
616 return (error);
617 }
618 /* XXX as unlink/rmdir/mkdir/creat are not currently possible
619 * with NTFS, we don't need to check anything else for now */
620 *vpp = nvp;
621 vnode_create_vobject_off(nvp, VTOF(nvp)->f_size, curthread);
622 return (0);
623 }
624
625 static int
626 ntfs_vptofh(
627 struct vnode *vp,
628 struct fid *fhp)
629 {
630 register struct ntnode *ntp;
631 register struct ntfid *ntfhp;
632
633 ddprintf(("ntfs_fhtovp(): %p\n", vp));
634
635 ntp = VTONT(vp);
636 ntfhp = (struct ntfid *)fhp;
637 ntfhp->ntfid_len = sizeof(struct ntfid);
638 ntfhp->ntfid_ino = ntp->i_number;
639 /* ntfhp->ntfid_gen = ntp->i_gen; */
640 return (0);
641 }
642
643 int
644 ntfs_vgetex(
645 struct mount *mp,
646 ino_t ino,
647 u_int32_t attrtype,
648 char *attrname,
649 u_long lkflags,
650 u_long flags,
651 struct thread *td,
652 struct vnode **vpp)
653 {
654 int error;
655 register struct ntfsmount *ntmp;
656 struct ntnode *ip;
657 struct fnode *fp;
658 struct vnode *vp;
659 enum vtype f_type;
660
661 dprintf(("ntfs_vgetex: ino: %d, attr: 0x%x:%s, lkf: 0x%lx, f: 0x%lx\n",
662 ino, attrtype, attrname?attrname:"", (u_long)lkflags,
663 (u_long)flags));
664
665 ntmp = VFSTONTFS(mp);
666 *vpp = NULL;
667
668 /* Get ntnode */
669 error = ntfs_ntlookup(ntmp, ino, &ip);
670 if (error) {
671 printf("ntfs_vget: ntfs_ntget failed\n");
672 return (error);
673 }
674
675 /* It may be not initialized fully, so force load it */
676 if (!(flags & VG_DONTLOADIN) && !(ip->i_flag & IN_LOADED)) {
677 error = ntfs_loadntnode(ntmp, ip);
678 if(error) {
679 printf("ntfs_vget: CAN'T LOAD ATTRIBUTES FOR INO: %d\n",
680 ip->i_number);
681 ntfs_ntput(ip);
682 return (error);
683 }
684 }
685
686 error = ntfs_fget(ntmp, ip, attrtype, attrname, &fp);
687 if (error) {
688 printf("ntfs_vget: ntfs_fget failed\n");
689 ntfs_ntput(ip);
690 return (error);
691 }
692
693 f_type = VNON;
694 if (!(flags & VG_DONTVALIDFN) && !(fp->f_flag & FN_VALID)) {
695 if ((ip->i_frflag & NTFS_FRFLAG_DIR) &&
696 (fp->f_attrtype == NTFS_A_DATA && fp->f_attrname == NULL)) {
697 f_type = VDIR;
698 } else if (flags & VG_EXT) {
699 f_type = VNON;
700 fp->f_size = fp->f_allocated = 0;
701 } else {
702 f_type = VREG;
703
704 error = ntfs_filesize(ntmp, fp,
705 &fp->f_size, &fp->f_allocated);
706 if (error) {
707 ntfs_ntput(ip);
708 return (error);
709 }
710 }
711
712 fp->f_flag |= FN_VALID;
713 }
714
715 if (FTOV(fp)) {
716 vget(FTOV(fp), lkflags, td);
717 *vpp = FTOV(fp);
718 ntfs_ntput(ip);
719 return (0);
720 }
721
722 error = getnewvnode("ntfs", ntmp->ntm_mountp, &ntfs_vnodeops, &vp);
723 if(error) {
724 ntfs_frele(fp);
725 ntfs_ntput(ip);
726 return (error);
727 }
728 dprintf(("ntfs_vget: vnode: %p for ntnode: %d\n", vp,ino));
729
730 fp->f_vp = vp;
731 vp->v_data = fp;
732 vp->v_type = f_type;
733
734 vp->v_bufobj.bo_ops = &ntfs_vnbufops;
735 vp->v_bufobj.bo_private = vp;
736
737 if (ino == NTFS_ROOTINO)
738 vp->v_vflag |= VV_ROOT;
739
740 ntfs_ntput(ip);
741
742 if (lkflags & LK_TYPE_MASK) {
743 error = vn_lock(vp, lkflags, td);
744 if (error) {
745 vput(vp);
746 return (error);
747 }
748 }
749
750 *vpp = vp;
751 return (0);
752
753 }
754
755 static int
756 ntfs_vget(
757 struct mount *mp,
758 ino_t ino,
759 int lkflags,
760 struct vnode **vpp)
761 {
762 return ntfs_vgetex(mp, ino, NTFS_A_DATA, NULL, lkflags, 0,
763 curthread, vpp);
764 }
765
766 static void
767 ntfs_bufstrategy(struct bufobj *bo, struct buf *bp)
768 {
769 struct vnode *vp;
770 int rc;
771
772 vp = bo->bo_private;
773 KASSERT(bo == &vp->v_bufobj, ("BO/VP mismatch: vp %p bo %p != %p",
774 vp, &vp->v_bufobj, bo));
775 rc = VOP_STRATEGY(vp, bp);
776 KASSERT(rc == 0, ("NTFS VOP_STRATEGY failed: bp=%p, "
777 "vp=%p, rc=%d", bp, vp, rc));
778 }
779
780 static struct vfsops ntfs_vfsops = {
781 .vfs_fhtovp = ntfs_fhtovp,
782 .vfs_init = ntfs_init,
783 .vfs_cmount = ntfs_cmount,
784 .vfs_mount = ntfs_mount,
785 .vfs_root = ntfs_root,
786 .vfs_statfs = ntfs_statfs,
787 .vfs_uninit = ntfs_uninit,
788 .vfs_unmount = ntfs_unmount,
789 .vfs_vget = ntfs_vget,
790 .vfs_vptofh = ntfs_vptofh,
791 };
792 VFS_SET(ntfs_vfsops, ntfs, 0);
793 MODULE_VERSION(ntfs, 1);
Cache object: 30e41b0e706ae7d9875c067cc4dc9521
|