ntfs_vfsops.c revision 1.99 1 /* $NetBSD: ntfs_vfsops.c,v 1.99 2014/12/28 12:57:44 maxv 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 * Id: ntfs_vfsops.c,v 1.7 1999/05/31 11:28:30 phk Exp
29 */
30
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: ntfs_vfsops.c,v 1.99 2014/12/28 12:57:44 maxv Exp $");
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/namei.h>
37 #include <sys/proc.h>
38 #include <sys/kernel.h>
39 #include <sys/vnode.h>
40 #include <sys/mount.h>
41 #include <sys/buf.h>
42 #include <sys/fcntl.h>
43 #include <sys/malloc.h>
44 #include <sys/sysctl.h>
45 #include <sys/device.h>
46 #include <sys/conf.h>
47 #include <sys/kauth.h>
48 #include <sys/module.h>
49
50 #include <uvm/uvm_extern.h>
51
52 #include <miscfs/genfs/genfs.h>
53 #include <miscfs/specfs/specdev.h>
54
55 #include <fs/ntfs/ntfs.h>
56 #include <fs/ntfs/ntfs_inode.h>
57 #include <fs/ntfs/ntfs_subr.h>
58 #include <fs/ntfs/ntfs_vfsops.h>
59 #include <fs/ntfs/ntfs_ihash.h>
60 #include <fs/ntfs/ntfsmount.h>
61
62 MODULE(MODULE_CLASS_VFS, ntfs, NULL);
63
64 MALLOC_JUSTDEFINE(M_NTFSMNT, "NTFS mount", "NTFS mount structure");
65 MALLOC_JUSTDEFINE(M_NTFSNTNODE,"NTFS ntnode", "NTFS ntnode information");
66 MALLOC_JUSTDEFINE(M_NTFSDIR,"NTFS dir", "NTFS dir buffer");
67
68 static int ntfs_mount(struct mount *, const char *, void *, size_t *);
69 static int ntfs_root(struct mount *, struct vnode **);
70 static int ntfs_start(struct mount *, int);
71 static int ntfs_statvfs(struct mount *, struct statvfs *);
72 static int ntfs_sync(struct mount *, int, kauth_cred_t);
73 static int ntfs_unmount(struct mount *, int);
74 static int ntfs_vget(struct mount *mp, ino_t ino,
75 struct vnode **vpp);
76 static int ntfs_loadvnode(struct mount *, struct vnode *,
77 const void *, size_t, const void **);
78 static int ntfs_mountfs(struct vnode *, struct mount *,
79 struct ntfs_args *, struct lwp *);
80 static int ntfs_vptofh(struct vnode *, struct fid *, size_t *);
81
82 static void ntfs_init(void);
83 static void ntfs_reinit(void);
84 static void ntfs_done(void);
85 static int ntfs_fhtovp(struct mount *, struct fid *,
86 struct vnode **);
87 static int ntfs_mountroot(void);
88
89 static const struct genfs_ops ntfs_genfsops = {
90 .gop_write = genfs_compat_gop_write,
91 };
92
93 static struct sysctllog *ntfs_sysctl_log;
94
95 static int
96 ntfs_mountroot(void)
97 {
98 struct mount *mp;
99 struct lwp *l = curlwp; /* XXX */
100 int error;
101 struct ntfs_args args;
102
103 if (device_class(root_device) != DV_DISK)
104 return (ENODEV);
105
106 if ((error = vfs_rootmountalloc(MOUNT_NTFS, "root_device", &mp))) {
107 vrele(rootvp);
108 return (error);
109 }
110
111 args.flag = 0;
112 args.uid = 0;
113 args.gid = 0;
114 args.mode = 0777;
115
116 if ((error = ntfs_mountfs(rootvp, mp, &args, l)) != 0) {
117 vfs_unbusy(mp, false, NULL);
118 vfs_destroy(mp);
119 return (error);
120 }
121
122 mountlist_append(mp);
123 (void)ntfs_statvfs(mp, &mp->mnt_stat);
124 vfs_unbusy(mp, false, NULL);
125 return (0);
126 }
127
128 static void
129 ntfs_init(void)
130 {
131
132 malloc_type_attach(M_NTFSMNT);
133 malloc_type_attach(M_NTFSNTNODE);
134 malloc_type_attach(M_NTFSDIR);
135 malloc_type_attach(M_NTFSNTVATTR);
136 malloc_type_attach(M_NTFSRDATA);
137 malloc_type_attach(M_NTFSDECOMP);
138 malloc_type_attach(M_NTFSRUN);
139 ntfs_nthashinit();
140 ntfs_toupper_init();
141 }
142
143 static void
144 ntfs_reinit(void)
145 {
146 ntfs_nthashreinit();
147 }
148
149 static void
150 ntfs_done(void)
151 {
152 ntfs_nthashdone();
153 malloc_type_detach(M_NTFSMNT);
154 malloc_type_detach(M_NTFSNTNODE);
155 malloc_type_detach(M_NTFSDIR);
156 malloc_type_detach(M_NTFSNTVATTR);
157 malloc_type_detach(M_NTFSRDATA);
158 malloc_type_detach(M_NTFSDECOMP);
159 malloc_type_detach(M_NTFSRUN);
160 }
161
162 static int
163 ntfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
164 {
165 struct lwp *l = curlwp;
166 int err = 0, flags;
167 struct vnode *devvp;
168 struct ntfs_args *args = data;
169
170 if (args == NULL)
171 return EINVAL;
172 if (*data_len < sizeof *args)
173 return EINVAL;
174
175 if (mp->mnt_flag & MNT_GETARGS) {
176 struct ntfsmount *ntmp = VFSTONTFS(mp);
177 if (ntmp == NULL)
178 return EIO;
179 args->fspec = NULL;
180 args->uid = ntmp->ntm_uid;
181 args->gid = ntmp->ntm_gid;
182 args->mode = ntmp->ntm_mode;
183 args->flag = ntmp->ntm_flag;
184 *data_len = sizeof *args;
185 return 0;
186 }
187 /*
188 ***
189 * Mounting non-root file system or updating a file system
190 ***
191 */
192
193 /*
194 * If updating, check whether changing from read-only to
195 * read/write; if there is no device name, that's all we do.
196 */
197 if (mp->mnt_flag & MNT_UPDATE) {
198 printf("ntfs_mount(): MNT_UPDATE not supported\n");
199 return (EINVAL);
200 }
201
202 /*
203 * Not an update, or updating the name: look up the name
204 * and verify that it refers to a sensible block device.
205 */
206 err = namei_simple_user(args->fspec,
207 NSM_FOLLOW_NOEMULROOT, &devvp);
208 if (err)
209 return (err);
210
211 if (devvp->v_type != VBLK) {
212 err = ENOTBLK;
213 goto fail;
214 }
215 if (bdevsw_lookup(devvp->v_rdev) == NULL) {
216 err = ENXIO;
217 goto fail;
218 }
219 if (mp->mnt_flag & MNT_UPDATE) {
220 #if 0
221 /*
222 ********************
223 * UPDATE
224 ********************
225 */
226
227 if (devvp != ntmp->um_devvp) {
228 err = EINVAL; /* needs translation */
229 goto fail;
230 }
231
232 /*
233 * Update device name only on success
234 */
235 err = set_statvfs_info(NULL, UIO_USERSPACE, args->fspec,
236 UIO_USERSPACE, mp->mnt_op->vfs_name, mp, p);
237 if (err)
238 goto fail;
239
240 vrele(devvp);
241 #endif
242 } else {
243 /*
244 ********************
245 * NEW MOUNT
246 ********************
247 */
248
249 /*
250 * Since this is a new mount, we want the names for
251 * the device and the mount point copied in. If an
252 * error occurs, the mountpoint is discarded by the
253 * upper level code.
254 */
255
256 /* Save "last mounted on" info for mount point (NULL pad)*/
257 err = set_statvfs_info(path, UIO_USERSPACE, args->fspec,
258 UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
259 if (err)
260 goto fail;
261
262 if (mp->mnt_flag & MNT_RDONLY)
263 flags = FREAD;
264 else
265 flags = FREAD|FWRITE;
266 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
267 err = VOP_OPEN(devvp, flags, FSCRED);
268 VOP_UNLOCK(devvp);
269 if (err)
270 goto fail;
271 err = ntfs_mountfs(devvp, mp, args, l);
272 if (err) {
273 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
274 (void)VOP_CLOSE(devvp, flags, NOCRED);
275 VOP_UNLOCK(devvp);
276 goto fail;
277 }
278 }
279
280 /*
281 * Initialize FS stat information in mount struct; uses both
282 * mp->mnt_stat.f_mntonname and mp->mnt_stat.f_mntfromname
283 *
284 * This code is common to root and non-root mounts
285 */
286 (void)VFS_STATVFS(mp, &mp->mnt_stat);
287 return (err);
288
289 fail:
290 vrele(devvp);
291 return (err);
292 }
293
294 /*
295 * Common code for mount and mountroot
296 */
297 int
298 ntfs_mountfs(struct vnode *devvp, struct mount *mp, struct ntfs_args *argsp, struct lwp *l)
299 {
300 struct buf *bp;
301 struct ntfsmount *ntmp;
302 dev_t dev = devvp->v_rdev;
303 int error, i;
304 struct vnode *vp;
305
306 ntmp = NULL;
307
308 /*
309 * Flush out any old buffers remaining from a previous use.
310 */
311 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
312 error = vinvalbuf(devvp, V_SAVE, l->l_cred, l, 0, 0);
313 VOP_UNLOCK(devvp);
314 if (error)
315 return (error);
316
317 bp = NULL;
318
319 error = bread(devvp, BBLOCK, BBSIZE, NOCRED, 0, &bp);
320 if (error)
321 goto out;
322 ntmp = malloc(sizeof(*ntmp), M_NTFSMNT, M_WAITOK|M_ZERO);
323 memcpy(&ntmp->ntm_bootfile, bp->b_data, sizeof(struct bootfile));
324 brelse(bp, 0);
325 bp = NULL;
326
327 /* Sanity checks. XXX: More checks are probably needed. */
328 if (strncmp(ntmp->ntm_bootfile.bf_sysid, NTFS_BBID, NTFS_BBIDLEN)) {
329 error = EINVAL;
330 dprintf(("ntfs_mountfs: invalid boot block\n"));
331 goto out;
332 }
333 if (ntmp->ntm_bps == 0) {
334 error = EINVAL;
335 dprintf(("ntfs_mountfs: invalid bytes per sector\n"));
336 goto out;
337 }
338
339 {
340 int8_t cpr = ntmp->ntm_mftrecsz;
341 if (cpr > 0)
342 ntmp->ntm_bpmftrec = ntmp->ntm_spc * cpr;
343 else
344 ntmp->ntm_bpmftrec = (1 << (-cpr)) / ntmp->ntm_bps;
345 }
346 dprintf(("ntfs_mountfs(): bps: %d, spc: %d, media: %x, mftrecsz: %d (%d sects)\n",
347 ntmp->ntm_bps,ntmp->ntm_spc,ntmp->ntm_bootfile.bf_media,
348 ntmp->ntm_mftrecsz,ntmp->ntm_bpmftrec));
349 dprintf(("ntfs_mountfs(): mftcn: 0x%x|0x%x\n",
350 (u_int32_t)ntmp->ntm_mftcn,(u_int32_t)ntmp->ntm_mftmirrcn));
351
352 ntmp->ntm_mountp = mp;
353 ntmp->ntm_dev = dev;
354 ntmp->ntm_devvp = devvp;
355 ntmp->ntm_uid = argsp->uid;
356 ntmp->ntm_gid = argsp->gid;
357 ntmp->ntm_mode = argsp->mode;
358 ntmp->ntm_flag = argsp->flag;
359 mp->mnt_data = ntmp;
360
361 /* set file name encode/decode hooks XXX utf-8 only for now */
362 ntmp->ntm_wget = ntfs_utf8_wget;
363 ntmp->ntm_wput = ntfs_utf8_wput;
364 ntmp->ntm_wcmp = ntfs_utf8_wcmp;
365
366 dprintf(("ntfs_mountfs(): case-%s,%s uid: %d, gid: %d, mode: %o\n",
367 (ntmp->ntm_flag & NTFS_MFLAG_CASEINS)?"insens.":"sens.",
368 (ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES)?" allnames,":"",
369 ntmp->ntm_uid, ntmp->ntm_gid, ntmp->ntm_mode));
370
371 /*
372 * We read in some system nodes to do not allow
373 * reclaim them and to have everytime access to them.
374 */
375 {
376 int pi[3] = { NTFS_MFTINO, NTFS_ROOTINO, NTFS_BITMAPINO };
377 for (i = 0; i < 3; i++) {
378 error = VFS_VGET(mp, pi[i], &(ntmp->ntm_sysvn[pi[i]]));
379 if (error)
380 goto out1;
381 ntmp->ntm_sysvn[pi[i]]->v_vflag |= VV_SYSTEM;
382 vref(ntmp->ntm_sysvn[pi[i]]);
383 vput(ntmp->ntm_sysvn[pi[i]]);
384 }
385 }
386
387 /* read the Unicode lowercase --> uppercase translation table,
388 * if necessary */
389 if ((error = ntfs_toupper_use(mp, ntmp)))
390 goto out1;
391
392 /*
393 * Scan $BitMap and count free clusters
394 */
395 error = ntfs_calccfree(ntmp, &ntmp->ntm_cfree);
396 if (error)
397 goto out1;
398
399 /*
400 * Read and translate to internal format attribute
401 * definition file.
402 */
403 {
404 int num,j;
405 struct attrdef ad;
406
407 /* Open $AttrDef */
408 error = VFS_VGET(mp, NTFS_ATTRDEFINO, &vp );
409 if (error)
410 goto out1;
411
412 /* Count valid entries */
413 for (num = 0; ; num++) {
414 error = ntfs_readattr(ntmp, VTONT(vp),
415 NTFS_A_DATA, NULL,
416 num * sizeof(ad), sizeof(ad),
417 &ad, NULL);
418 if (error)
419 goto out1;
420 if (ad.ad_name[0] == 0)
421 break;
422 }
423
424 /* Alloc memory for attribute definitions */
425 ntmp->ntm_ad = (struct ntvattrdef *) malloc(
426 num * sizeof(struct ntvattrdef),
427 M_NTFSMNT, M_WAITOK);
428
429 ntmp->ntm_adnum = num;
430
431 /* Read them and translate */
432 for (i = 0; i < num; i++) {
433 error = ntfs_readattr(ntmp, VTONT(vp),
434 NTFS_A_DATA, NULL,
435 i * sizeof(ad), sizeof(ad),
436 &ad, NULL);
437 if (error)
438 goto out1;
439 j = 0;
440 do {
441 ntmp->ntm_ad[i].ad_name[j] = ad.ad_name[j];
442 } while(ad.ad_name[j++]);
443 ntmp->ntm_ad[i].ad_namelen = j - 1;
444 ntmp->ntm_ad[i].ad_type = ad.ad_type;
445 }
446
447 vput(vp);
448 }
449
450 mp->mnt_stat.f_fsidx.__fsid_val[0] = dev;
451 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_NTFS);
452 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
453 mp->mnt_stat.f_namemax = NTFS_MAXFILENAME;
454 mp->mnt_flag |= MNT_LOCAL;
455 spec_node_setmountedfs(devvp, mp);
456 return (0);
457
458 out1:
459 for (i = 0; i < NTFS_SYSNODESNUM; i++)
460 if (ntmp->ntm_sysvn[i])
461 vrele(ntmp->ntm_sysvn[i]);
462
463 if (vflush(mp, NULLVP, 0)) {
464 dprintf(("ntfs_mountfs: vflush failed\n"));
465 }
466 out:
467 spec_node_setmountedfs(devvp, NULL);
468 if (bp)
469 brelse(bp, 0);
470
471 if (error) {
472 if (ntmp) {
473 if (ntmp->ntm_ad)
474 free(ntmp->ntm_ad, M_NTFSMNT);
475 free(ntmp, M_NTFSMNT);
476 }
477 }
478
479 return (error);
480 }
481
482 static int
483 ntfs_start(struct mount *mp, int flags)
484 {
485 return (0);
486 }
487
488 static int
489 ntfs_unmount(struct mount *mp, int mntflags)
490 {
491 struct lwp *l = curlwp;
492 struct ntfsmount *ntmp;
493 int error, ronly = 0, flags, i;
494
495 dprintf(("ntfs_unmount: unmounting...\n"));
496 ntmp = VFSTONTFS(mp);
497
498 flags = 0;
499 if (mntflags & MNT_FORCE)
500 flags |= FORCECLOSE;
501
502 dprintf(("ntfs_unmount: vflushing...\n"));
503 error = vflush(mp, NULLVP, flags | SKIPSYSTEM);
504 if (error) {
505 dprintf(("ntfs_unmount: vflush failed: %d\n",error));
506 return (error);
507 }
508
509 /* Check if only system vnodes are rest */
510 for (i = 0; i < NTFS_SYSNODESNUM; i++)
511 if ((ntmp->ntm_sysvn[i]) &&
512 (ntmp->ntm_sysvn[i]->v_usecount > 1))
513 return (EBUSY);
514
515 /* Dereference all system vnodes */
516 for (i = 0; i < NTFS_SYSNODESNUM; i++)
517 if (ntmp->ntm_sysvn[i])
518 vrele(ntmp->ntm_sysvn[i]);
519
520 /* vflush system vnodes */
521 error = vflush(mp, NULLVP, flags);
522 if (error) {
523 panic("ntfs_unmount: vflush failed(sysnodes): %d\n",error);
524 }
525
526 /* Check if the type of device node isn't VBAD before
527 * touching v_specinfo. If the device vnode is revoked, the
528 * field is NULL and touching it causes null pointer derefercence.
529 */
530 if (ntmp->ntm_devvp->v_type != VBAD)
531 spec_node_setmountedfs(ntmp->ntm_devvp, NULL);
532
533 error = vinvalbuf(ntmp->ntm_devvp, V_SAVE, NOCRED, l, 0, 0);
534 KASSERT(error == 0);
535
536 /* lock the device vnode before calling VOP_CLOSE() */
537 vn_lock(ntmp->ntm_devvp, LK_EXCLUSIVE | LK_RETRY);
538 error = VOP_CLOSE(ntmp->ntm_devvp, ronly ? FREAD : FREAD|FWRITE,
539 NOCRED);
540 KASSERT(error == 0);
541 VOP_UNLOCK(ntmp->ntm_devvp);
542
543 vrele(ntmp->ntm_devvp);
544
545 /* free the toupper table, if this has been last mounted ntfs volume */
546 ntfs_toupper_unuse();
547
548 dprintf(("ntfs_umount: freeing memory...\n"));
549 mp->mnt_data = NULL;
550 mp->mnt_flag &= ~MNT_LOCAL;
551 free(ntmp->ntm_ad, M_NTFSMNT);
552 free(ntmp, M_NTFSMNT);
553 return (0);
554 }
555
556 static int
557 ntfs_root(struct mount *mp, struct vnode **vpp)
558 {
559 struct vnode *nvp;
560 int error = 0;
561
562 dprintf(("ntfs_root(): sysvn: %p\n",
563 VFSTONTFS(mp)->ntm_sysvn[NTFS_ROOTINO]));
564 error = VFS_VGET(mp, (ino_t)NTFS_ROOTINO, &nvp);
565 if (error) {
566 printf("ntfs_root: VFS_VGET failed: %d\n", error);
567 return (error);
568 }
569
570 *vpp = nvp;
571 return (0);
572 }
573
574 int
575 ntfs_calccfree(struct ntfsmount *ntmp, cn_t *cfreep)
576 {
577 struct vnode *vp;
578 u_int8_t *tmp;
579 int j, error;
580 cn_t cfree = 0;
581 size_t bmsize, i;
582
583 vp = ntmp->ntm_sysvn[NTFS_BITMAPINO];
584 bmsize = VTOF(vp)->f_size;
585 tmp = (u_int8_t *) malloc(bmsize, M_TEMP, M_WAITOK);
586
587 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
588 0, bmsize, tmp, NULL);
589 if (error)
590 goto out;
591
592 for (i = 0; i < bmsize; i++)
593 for (j = 0; j < 8; j++)
594 if (~tmp[i] & (1 << j))
595 cfree++;
596 *cfreep = cfree;
597
598 out:
599 free(tmp, M_TEMP);
600 return(error);
601 }
602
603 static int
604 ntfs_statvfs(struct mount *mp, struct statvfs *sbp)
605 {
606 struct ntfsmount *ntmp = VFSTONTFS(mp);
607 u_int64_t mftallocated;
608
609 dprintf(("ntfs_statvfs():\n"));
610
611 mftallocated = VTOF(ntmp->ntm_sysvn[NTFS_MFTINO])->f_allocated;
612
613 sbp->f_bsize = ntmp->ntm_bps;
614 sbp->f_frsize = sbp->f_bsize; /* XXX */
615 sbp->f_iosize = ntmp->ntm_bps * ntmp->ntm_spc;
616 sbp->f_blocks = ntmp->ntm_bootfile.bf_spv;
617 sbp->f_bfree = sbp->f_bavail = ntfs_cntobn(ntmp->ntm_cfree);
618 sbp->f_ffree = sbp->f_favail = sbp->f_bfree / ntmp->ntm_bpmftrec;
619 sbp->f_files = mftallocated / ntfs_bntob(ntmp->ntm_bpmftrec) +
620 sbp->f_ffree;
621 sbp->f_fresvd = sbp->f_bresvd = 0; /* XXX */
622 sbp->f_flag = mp->mnt_flag;
623 copy_statvfs_info(sbp, mp);
624 return (0);
625 }
626
627 static int
628 ntfs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
629 {
630 /*dprintf(("ntfs_sync():\n"));*/
631 return (0);
632 }
633
634 /*ARGSUSED*/
635 static int
636 ntfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
637 {
638 struct ntfid ntfh;
639 int error;
640
641 if (fhp->fid_len != sizeof(struct ntfid))
642 return EINVAL;
643 memcpy(&ntfh, fhp, sizeof(ntfh));
644 ddprintf(("ntfs_fhtovp(): %s: %llu\n", mp->mnt_stat.f_mntonname,
645 (unsigned long long)ntfh.ntfid_ino));
646
647 error = ntfs_vgetex(mp, ntfh.ntfid_ino, ntfh.ntfid_attr, "",
648 LK_EXCLUSIVE, vpp);
649 if (error != 0) {
650 *vpp = NULLVP;
651 return (error);
652 }
653
654 /* XXX as unlink/rmdir/mkdir/creat are not currently possible
655 * with NTFS, we don't need to check anything else for now */
656 return (0);
657 }
658
659 static int
660 ntfs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
661 {
662 struct ntnode *ntp;
663 struct ntfid ntfh;
664 struct fnode *fn;
665
666 if (*fh_size < sizeof(struct ntfid)) {
667 *fh_size = sizeof(struct ntfid);
668 return E2BIG;
669 }
670 *fh_size = sizeof(struct ntfid);
671
672 ddprintf(("ntfs_fhtovp(): %s: %p\n", vp->v_mount->mnt_stat.f_mntonname,
673 vp));
674
675 fn = VTOF(vp);
676 ntp = VTONT(vp);
677 memset(&ntfh, 0, sizeof(ntfh));
678 ntfh.ntfid_len = sizeof(struct ntfid);
679 ntfh.ntfid_ino = ntp->i_number;
680 ntfh.ntfid_attr = fn->f_attrtype;
681 #ifdef notyet
682 ntfh.ntfid_gen = ntp->i_gen;
683 #endif
684 memcpy(fhp, &ntfh, sizeof(ntfh));
685 return (0);
686 }
687
688 static int
689 ntfs_loadvnode(struct mount *mp, struct vnode *vp,
690 const void *key, size_t key_len, const void **new_key)
691 {
692 int error;
693 struct ntvattr *vap;
694 struct ntkey small_key, *ntkey;
695 struct ntfsmount *ntmp;
696 struct ntnode *ip;
697 struct fnode *fp = NULL;
698 enum vtype f_type = VBAD;
699
700 if (key_len <= sizeof(small_key))
701 ntkey = &small_key;
702 else
703 ntkey = kmem_alloc(key_len, KM_SLEEP);
704 memcpy(ntkey, key, key_len);
705
706 dprintf(("ntfs_loadvnode: ino: %llu, attr: 0x%x:%s",
707 (unsigned long long)ntkey->k_ino,
708 ntkey->k_attrtype, ntkey->k_attrname));
709
710 ntmp = VFSTONTFS(mp);
711
712 /* Get ntnode */
713 error = ntfs_ntlookup(ntmp, ntkey->k_ino, &ip);
714 if (error) {
715 printf("ntfs_loadvnode: ntfs_ntget failed\n");
716 goto out;
717 }
718 /* It may be not initialized fully, so force load it */
719 if (!(ip->i_flag & IN_LOADED)) {
720 error = ntfs_loadntnode(ntmp, ip);
721 if (error) {
722 printf("ntfs_loadvnode: CAN'T LOAD ATTRIBUTES FOR INO:"
723 " %llu\n", (unsigned long long)ip->i_number);
724 ntfs_ntput(ip);
725 goto out;
726 }
727 }
728
729 /* Setup fnode */
730 fp = kmem_zalloc(sizeof(*fp), KM_SLEEP);
731 dprintf(("%s: allocating fnode: %p\n", __func__, fp));
732
733 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_NAME, NULL, 0, &vap);
734 if (error) {
735 ntfs_ntput(ip);
736 goto out;
737 }
738 fp->f_fflag = vap->va_a_name->n_flag;
739 fp->f_pnumber = vap->va_a_name->n_pnumber;
740 fp->f_times = vap->va_a_name->n_times;
741 ntfs_ntvattrrele(vap);
742
743 if ((ip->i_frflag & NTFS_FRFLAG_DIR) &&
744 (ntkey->k_attrtype == NTFS_A_DATA &&
745 strcmp(ntkey->k_attrname, "") == 0)) {
746 f_type = VDIR;
747 } else {
748 f_type = VREG;
749 error = ntfs_ntvattrget(ntmp, ip,
750 ntkey->k_attrtype, ntkey->k_attrname, 0, &vap);
751 if (error == 0) {
752 fp->f_size = vap->va_datalen;
753 fp->f_allocated = vap->va_allocated;
754 ntfs_ntvattrrele(vap);
755 } else if (ntkey->k_attrtype == NTFS_A_DATA &&
756 strcmp(ntkey->k_attrname, "") == 0 &&
757 error == ENOENT) {
758 fp->f_size = 0;
759 fp->f_allocated = 0;
760 error = 0;
761 } else
762 goto out;
763 }
764
765 if (key_len <= sizeof(fp->f_smallkey))
766 fp->f_key = &fp->f_smallkey;
767 else
768 fp->f_key = kmem_alloc(key_len, KM_SLEEP);
769 fp->f_ip = ip;
770 fp->f_ino = ip->i_number;
771 strcpy(fp->f_attrname, ntkey->k_attrname);
772 fp->f_attrtype = ntkey->k_attrtype;
773 fp->f_vp = vp;
774 vp->v_data = fp;
775
776 vp->v_tag = VT_NTFS;
777 vp->v_type = f_type;
778 vp->v_op = ntfs_vnodeop_p;
779 ntfs_ntref(ip);
780 vref(ip->i_devvp);
781 genfs_node_init(vp, &ntfs_genfsops);
782
783 if (ip->i_number == NTFS_ROOTINO)
784 vp->v_vflag |= VV_ROOT;
785
786 uvm_vnp_setsize(vp, fp->f_size);
787 ntfs_ntput(ip);
788
789 *new_key = fp->f_key;
790
791 fp = NULL;
792
793 out:
794 if (ntkey != &small_key)
795 kmem_free(ntkey, key_len);
796 if (fp)
797 kmem_free(fp, sizeof(*fp));
798
799 return error;
800 }
801
802 static int
803 ntfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
804 {
805 return ntfs_vgetex(mp, ino, NTFS_A_DATA, "", LK_EXCLUSIVE, vpp);
806 }
807
808 int
809 ntfs_vgetex(struct mount *mp, ino_t ino, u_int32_t attrtype,
810 const char *attrname, u_long lkflags, struct vnode **vpp)
811 {
812 const int attrlen = strlen(attrname);
813 int error;
814 struct ntkey small_key, *ntkey;
815
816 if (NTKEY_SIZE(attrlen) <= sizeof(small_key))
817 ntkey = &small_key;
818 else
819 ntkey = malloc(NTKEY_SIZE(attrlen), M_TEMP, M_WAITOK);
820 ntkey->k_ino = ino;
821 ntkey->k_attrtype = attrtype;
822 strcpy(ntkey->k_attrname, attrname);
823
824 error = vcache_get(mp, ntkey, NTKEY_SIZE(attrlen), vpp);
825 if (error)
826 goto out;
827
828 if ((lkflags & (LK_SHARED | LK_EXCLUSIVE)) != 0) {
829 error = vn_lock(*vpp, lkflags);
830 if (error) {
831 vrele(*vpp);
832 *vpp = NULL;
833 }
834 }
835
836 out:
837 if (ntkey != &small_key)
838 free(ntkey, M_TEMP);
839 return error;
840 }
841
842 extern const struct vnodeopv_desc ntfs_vnodeop_opv_desc;
843
844 const struct vnodeopv_desc * const ntfs_vnodeopv_descs[] = {
845 &ntfs_vnodeop_opv_desc,
846 NULL,
847 };
848
849 struct vfsops ntfs_vfsops = {
850 .vfs_name = MOUNT_NTFS,
851 .vfs_min_mount_data = sizeof (struct ntfs_args),
852 .vfs_mount = ntfs_mount,
853 .vfs_start = ntfs_start,
854 .vfs_unmount = ntfs_unmount,
855 .vfs_root = ntfs_root,
856 .vfs_quotactl = (void *)eopnotsupp,
857 .vfs_statvfs = ntfs_statvfs,
858 .vfs_sync = ntfs_sync,
859 .vfs_vget = ntfs_vget,
860 .vfs_loadvnode = ntfs_loadvnode,
861 .vfs_fhtovp = ntfs_fhtovp,
862 .vfs_vptofh = ntfs_vptofh,
863 .vfs_init = ntfs_init,
864 .vfs_reinit = ntfs_reinit,
865 .vfs_done = ntfs_done,
866 .vfs_mountroot = ntfs_mountroot,
867 .vfs_snapshot = (void *)eopnotsupp,
868 .vfs_extattrctl = vfs_stdextattrctl,
869 .vfs_suspendctl = (void *)eopnotsupp,
870 .vfs_renamelock_enter = genfs_renamelock_enter,
871 .vfs_renamelock_exit = genfs_renamelock_exit,
872 .vfs_fsync = (void *)eopnotsupp,
873 .vfs_opv_descs = ntfs_vnodeopv_descs
874 };
875
876 static int
877 ntfs_modcmd(modcmd_t cmd, void *arg)
878 {
879 int error;
880
881 switch (cmd) {
882 case MODULE_CMD_INIT:
883 error = vfs_attach(&ntfs_vfsops);
884 if (error != 0)
885 break;
886 sysctl_createv(&ntfs_sysctl_log, 0, NULL, NULL,
887 CTLFLAG_PERMANENT,
888 CTLTYPE_NODE, "ntfs",
889 SYSCTL_DESCR("NTFS file system"),
890 NULL, 0, NULL, 0,
891 CTL_VFS, 20, CTL_EOL);
892 /*
893 * XXX the "20" above could be dynamic, thereby eliminating
894 * one more instance of the "number to vfs" mapping problem,
895 * but "20" is the order as taken from sys/mount.h
896 */
897 break;
898 case MODULE_CMD_FINI:
899 error = vfs_detach(&ntfs_vfsops);
900 if (error != 0)
901 break;
902 sysctl_teardown(&ntfs_sysctl_log);
903 break;
904 default:
905 error = ENOTTY;
906 break;
907 }
908
909 return (error);
910 }
911