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