ntfs_vfsops.c revision 1.53 1 /* $NetBSD: ntfs_vfsops.c,v 1.53 2007/07/23 11:27:46 pooka 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.53 2007/07/23 11:27:46 pooka 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
49 #if defined(__NetBSD__)
50 #include <uvm/uvm_extern.h>
51 #else
52 #include <vm/vm.h>
53 #endif
54
55 #include <miscfs/specfs/specdev.h>
56
57 #include <fs/ntfs/ntfs.h>
58 #include <fs/ntfs/ntfs_inode.h>
59 #include <fs/ntfs/ntfs_subr.h>
60 #include <fs/ntfs/ntfs_vfsops.h>
61 #include <fs/ntfs/ntfs_ihash.h>
62 #include <fs/ntfs/ntfsmount.h>
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_NTFSFNODE,"NTFS fnode", "NTFS fnode information");
67 MALLOC_JUSTDEFINE(M_NTFSDIR,"NTFS dir", "NTFS dir buffer");
68
69 #if defined(__FreeBSD__)
70 static int ntfs_mount(struct mount *, char *, void *,
71 struct nameidata *, struct proc *);
72 #else
73 static int ntfs_mount(struct mount *, const char *, void *, size_t *,
74 struct nameidata *, struct lwp *);
75 #endif
76 static int ntfs_quotactl(struct mount *, int, uid_t, void *,
77 struct lwp *);
78 static int ntfs_root(struct mount *, struct vnode **);
79 static int ntfs_start(struct mount *, int, struct lwp *);
80 static int ntfs_statvfs(struct mount *, struct statvfs *,
81 struct lwp *);
82 static int ntfs_sync(struct mount *, int, kauth_cred_t,
83 struct lwp *);
84 static int ntfs_unmount(struct mount *, int, struct lwp *);
85 static int ntfs_vget(struct mount *mp, ino_t ino,
86 struct vnode **vpp);
87 static int ntfs_mountfs(struct vnode *, struct mount *,
88 struct ntfs_args *, struct lwp *);
89 static int ntfs_vptofh(struct vnode *, struct fid *, size_t *);
90
91 #if defined(__FreeBSD__)
92 static int ntfs_init(struct vfsconf *);
93 static int ntfs_fhtovp(struct mount *, struct fid *,
94 struct sockaddr *, struct vnode **,
95 int *, struct ucred **);
96 #elif defined(__NetBSD__)
97 static void ntfs_init(void);
98 static void ntfs_reinit(void);
99 static void ntfs_done(void);
100 static int ntfs_fhtovp(struct mount *, struct fid *,
101 struct vnode **);
102 static int ntfs_mountroot(void);
103 #else
104 static int ntfs_init(void);
105 static int ntfs_fhtovp(struct mount *, struct fid *,
106 struct mbuf *, struct vnode **,
107 int *, kauth_cred_t *);
108 #endif
109
110 static const struct genfs_ops ntfs_genfsops = {
111 .gop_write = genfs_compat_gop_write,
112 };
113
114 #ifdef __NetBSD__
115
116 SYSCTL_SETUP(sysctl_vfs_ntfs_setup, "sysctl vfs.ntfs subtree setup")
117 {
118
119 sysctl_createv(clog, 0, NULL, NULL,
120 CTLFLAG_PERMANENT,
121 CTLTYPE_NODE, "vfs", NULL,
122 NULL, 0, NULL, 0,
123 CTL_VFS, CTL_EOL);
124 sysctl_createv(clog, 0, NULL, NULL,
125 CTLFLAG_PERMANENT,
126 CTLTYPE_NODE, "ntfs",
127 SYSCTL_DESCR("NTFS file system"),
128 NULL, 0, NULL, 0,
129 CTL_VFS, 20, CTL_EOL);
130 /*
131 * XXX the "20" above could be dynamic, thereby eliminating
132 * one more instance of the "number to vfs" mapping problem,
133 * but "20" is the order as taken from sys/mount.h
134 */
135 }
136
137 static int
138 ntfs_mountroot()
139 {
140 struct mount *mp;
141 struct lwp *l = curlwp; /* XXX */
142 int error;
143 struct ntfs_args args;
144
145 if (device_class(root_device) != DV_DISK)
146 return (ENODEV);
147
148 if ((error = vfs_rootmountalloc(MOUNT_NTFS, "root_device", &mp))) {
149 vrele(rootvp);
150 return (error);
151 }
152
153 args.flag = 0;
154 args.uid = 0;
155 args.gid = 0;
156 args.mode = 0777;
157
158 if ((error = ntfs_mountfs(rootvp, mp, &args, l)) != 0) {
159 mp->mnt_op->vfs_refcount--;
160 vfs_unbusy(mp);
161 free(mp, M_MOUNT);
162 return (error);
163 }
164
165 simple_lock(&mountlist_slock);
166 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
167 simple_unlock(&mountlist_slock);
168 (void)ntfs_statvfs(mp, &mp->mnt_stat, l);
169 vfs_unbusy(mp);
170 return (0);
171 }
172
173 static void
174 ntfs_init()
175 {
176
177 malloc_type_attach(M_NTFSMNT);
178 malloc_type_attach(M_NTFSNTNODE);
179 malloc_type_attach(M_NTFSFNODE);
180 malloc_type_attach(M_NTFSDIR);
181 malloc_type_attach(M_NTFSNTHASH);
182 malloc_type_attach(M_NTFSNTVATTR);
183 malloc_type_attach(M_NTFSRDATA);
184 malloc_type_attach(M_NTFSDECOMP);
185 malloc_type_attach(M_NTFSRUN);
186 ntfs_nthashinit();
187 ntfs_toupper_init();
188 }
189
190 static void
191 ntfs_reinit()
192 {
193 ntfs_nthashreinit();
194 }
195
196 static void
197 ntfs_done()
198 {
199 ntfs_nthashdone();
200 malloc_type_detach(M_NTFSMNT);
201 malloc_type_detach(M_NTFSNTNODE);
202 malloc_type_detach(M_NTFSFNODE);
203 malloc_type_detach(M_NTFSDIR);
204 malloc_type_detach(M_NTFSNTHASH);
205 malloc_type_detach(M_NTFSNTVATTR);
206 malloc_type_detach(M_NTFSRDATA);
207 malloc_type_detach(M_NTFSDECOMP);
208 malloc_type_detach(M_NTFSRUN);
209 }
210
211 #elif defined(__FreeBSD__)
212
213 static int
214 ntfs_init (
215 struct vfsconf *vcp )
216 {
217 ntfs_nthashinit();
218 ntfs_toupper_init();
219 return 0;
220 }
221
222 #endif /* NetBSD */
223
224 static int
225 ntfs_mount (
226 struct mount *mp,
227 #if defined(__FreeBSD__)
228 char *path,
229 void *data,
230 #else
231 const char *path,
232 void *data,
233 size_t *data_len,
234 #endif
235 struct nameidata *ndp,
236 #if defined(__FreeBSD__)
237 struct proc *p )
238 #else
239 struct lwp *l )
240 #endif
241 {
242 int err = 0, flags;
243 struct vnode *devvp;
244 #if defined(__FreeBSD__)
245 struct ntfs_args *args = args_buf;
246 #else
247 struct ntfs_args *args = data;
248
249 if (*data_len < sizeof *args)
250 return EINVAL;
251 #endif
252
253 if (mp->mnt_flag & MNT_GETARGS) {
254 struct ntfsmount *ntmp = VFSTONTFS(mp);
255 if (ntmp == NULL)
256 return EIO;
257 args->fspec = NULL;
258 args->uid = ntmp->ntm_uid;
259 args->gid = ntmp->ntm_gid;
260 args->mode = ntmp->ntm_mode;
261 args->flag = ntmp->ntm_flag;
262 #if defined(__FreeBSD__)
263 return copyout(args, data, sizeof(args));
264 #else
265 *data_len = sizeof *args;
266 return 0;
267 #endif
268 }
269 /*
270 ***
271 * Mounting non-root file system or updating a file system
272 ***
273 */
274
275 #if defined(__FreeBSD__)
276 /* copy in user arguments*/
277 err = copyin(data, args, sizeof (struct ntfs_args));
278 if (err)
279 return (err); /* can't get arguments*/
280 #endif
281
282 /*
283 * If updating, check whether changing from read-only to
284 * read/write; if there is no device name, that's all we do.
285 */
286 if (mp->mnt_flag & MNT_UPDATE) {
287 printf("ntfs_mount(): MNT_UPDATE not supported\n");
288 return (EINVAL);
289 }
290
291 /*
292 * Not an update, or updating the name: look up the name
293 * and verify that it refers to a sensible block device.
294 */
295 #ifdef __FreeBSD__
296 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args->fspec, p);
297 #else
298 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args->fspec, l);
299 #endif
300 err = namei(ndp);
301 if (err) {
302 /* can't get devvp!*/
303 return (err);
304 }
305
306 devvp = ndp->ni_vp;
307
308 if (devvp->v_type != VBLK) {
309 err = ENOTBLK;
310 goto fail;
311 }
312 #ifdef __FreeBSD__
313 if (bdevsw(devvp->v_rdev) == NULL) {
314 #else
315 if (bdevsw_lookup(devvp->v_rdev) == NULL) {
316 #endif
317 err = ENXIO;
318 goto fail;
319 }
320 if (mp->mnt_flag & MNT_UPDATE) {
321 #if 0
322 /*
323 ********************
324 * UPDATE
325 ********************
326 */
327
328 if (devvp != ntmp->um_devvp) {
329 err = EINVAL; /* needs translation */
330 goto fail;
331 }
332
333 /*
334 * Update device name only on success
335 */
336 err = set_statvfs_info(NULL, UIO_USERSPACE, args->fspec,
337 UIO_USERSPACE, mp->mnt_op->vfs_name, mp, p);
338 if (err)
339 goto fail;
340
341 vrele(devvp);
342 #endif
343 } else {
344 /*
345 ********************
346 * NEW MOUNT
347 ********************
348 */
349
350 /*
351 * Since this is a new mount, we want the names for
352 * the device and the mount point copied in. If an
353 * error occurs, the mountpoint is discarded by the
354 * upper level code.
355 */
356
357 /* Save "last mounted on" info for mount point (NULL pad)*/
358 err = set_statvfs_info(path, UIO_USERSPACE, args->fspec,
359 UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
360 if (err)
361 goto fail;
362
363 /*
364 * Disallow multiple mounts of the same device.
365 * Disallow mounting of a device that is currently in use
366 * (except for root, which might share swap device for
367 * miniroot).
368 */
369 err = vfs_mountedon(devvp);
370 if (err)
371 goto fail;
372 if (vcount(devvp) > 1 && devvp != rootvp) {
373 err = EBUSY;
374 goto fail;
375 }
376 if (mp->mnt_flag & MNT_RDONLY)
377 flags = FREAD;
378 else
379 flags = FREAD|FWRITE;
380 err = VOP_OPEN(devvp, flags, FSCRED, l);
381 if (err)
382 goto fail;
383 err = ntfs_mountfs(devvp, mp, args, l);
384 if (err) {
385 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
386 (void)VOP_CLOSE(devvp, flags, NOCRED, l);
387 VOP_UNLOCK(devvp, 0);
388 goto fail;
389 }
390 }
391
392 #ifdef __FreeBSD__
393 dostatvfs:
394 #endif
395 /*
396 * Initialize FS stat information in mount struct; uses both
397 * mp->mnt_stat.f_mntonname and mp->mnt_stat.f_mntfromname
398 *
399 * This code is common to root and non-root mounts
400 */
401 (void)VFS_STATVFS(mp, &mp->mnt_stat, l);
402 return (err);
403
404 fail:
405 vrele(devvp);
406 return (err);
407 }
408
409 /*
410 * Common code for mount and mountroot
411 */
412 int
413 ntfs_mountfs(devvp, mp, argsp, l)
414 struct vnode *devvp;
415 struct mount *mp;
416 struct ntfs_args *argsp;
417 struct lwp *l;
418 {
419 struct buf *bp;
420 struct ntfsmount *ntmp;
421 dev_t dev = devvp->v_rdev;
422 int error, ronly, i;
423 struct vnode *vp;
424
425 ntmp = NULL;
426
427 /*
428 * Flush out any old buffers remaining from a previous use.
429 */
430 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
431 error = vinvalbuf(devvp, V_SAVE, l->l_cred, l, 0, 0);
432 VOP_UNLOCK(devvp, 0);
433 if (error)
434 return (error);
435
436 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
437
438 bp = NULL;
439
440 error = bread(devvp, BBLOCK, BBSIZE, NOCRED, &bp);
441 if (error)
442 goto out;
443 ntmp = malloc( sizeof *ntmp, M_NTFSMNT, M_WAITOK );
444 bzero( ntmp, sizeof *ntmp );
445 bcopy( bp->b_data, &ntmp->ntm_bootfile, sizeof(struct bootfile) );
446 brelse( bp );
447 bp = NULL;
448
449 if (strncmp(ntmp->ntm_bootfile.bf_sysid, NTFS_BBID, NTFS_BBIDLEN)) {
450 error = EINVAL;
451 dprintf(("ntfs_mountfs: invalid boot block\n"));
452 goto out;
453 }
454
455 {
456 int8_t cpr = ntmp->ntm_mftrecsz;
457 if( cpr > 0 )
458 ntmp->ntm_bpmftrec = ntmp->ntm_spc * cpr;
459 else
460 ntmp->ntm_bpmftrec = (1 << (-cpr)) / ntmp->ntm_bps;
461 }
462 dprintf(("ntfs_mountfs(): bps: %d, spc: %d, media: %x, mftrecsz: %d (%d sects)\n",
463 ntmp->ntm_bps,ntmp->ntm_spc,ntmp->ntm_bootfile.bf_media,
464 ntmp->ntm_mftrecsz,ntmp->ntm_bpmftrec));
465 dprintf(("ntfs_mountfs(): mftcn: 0x%x|0x%x\n",
466 (u_int32_t)ntmp->ntm_mftcn,(u_int32_t)ntmp->ntm_mftmirrcn));
467
468 ntmp->ntm_mountp = mp;
469 ntmp->ntm_dev = dev;
470 ntmp->ntm_devvp = devvp;
471 ntmp->ntm_uid = argsp->uid;
472 ntmp->ntm_gid = argsp->gid;
473 ntmp->ntm_mode = argsp->mode;
474 ntmp->ntm_flag = argsp->flag;
475 mp->mnt_data = ntmp;
476
477 /* set file name encode/decode hooks XXX utf-8 only for now */
478 ntmp->ntm_wget = ntfs_utf8_wget;
479 ntmp->ntm_wput = ntfs_utf8_wput;
480 ntmp->ntm_wcmp = ntfs_utf8_wcmp;
481
482 dprintf(("ntfs_mountfs(): case-%s,%s uid: %d, gid: %d, mode: %o\n",
483 (ntmp->ntm_flag & NTFS_MFLAG_CASEINS)?"insens.":"sens.",
484 (ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES)?" allnames,":"",
485 ntmp->ntm_uid, ntmp->ntm_gid, ntmp->ntm_mode));
486
487 /*
488 * We read in some system nodes to do not allow
489 * reclaim them and to have everytime access to them.
490 */
491 {
492 int pi[3] = { NTFS_MFTINO, NTFS_ROOTINO, NTFS_BITMAPINO };
493 for (i=0; i<3; i++) {
494 error = VFS_VGET(mp, pi[i], &(ntmp->ntm_sysvn[pi[i]]));
495 if(error)
496 goto out1;
497 ntmp->ntm_sysvn[pi[i]]->v_flag |= VSYSTEM;
498 VREF(ntmp->ntm_sysvn[pi[i]]);
499 vput(ntmp->ntm_sysvn[pi[i]]);
500 }
501 }
502
503 /* read the Unicode lowercase --> uppercase translation table,
504 * if necessary */
505 if ((error = ntfs_toupper_use(mp, ntmp)))
506 goto out1;
507
508 /*
509 * Scan $BitMap and count free clusters
510 */
511 error = ntfs_calccfree(ntmp, &ntmp->ntm_cfree);
512 if(error)
513 goto out1;
514
515 /*
516 * Read and translate to internal format attribute
517 * definition file.
518 */
519 {
520 int num,j;
521 struct attrdef ad;
522
523 /* Open $AttrDef */
524 error = VFS_VGET(mp, NTFS_ATTRDEFINO, &vp );
525 if(error)
526 goto out1;
527
528 /* Count valid entries */
529 for(num=0;;num++) {
530 error = ntfs_readattr(ntmp, VTONT(vp),
531 NTFS_A_DATA, NULL,
532 num * sizeof(ad), sizeof(ad),
533 &ad, NULL);
534 if (error)
535 goto out1;
536 if (ad.ad_name[0] == 0)
537 break;
538 }
539
540 /* Alloc memory for attribute definitions */
541 ntmp->ntm_ad = (struct ntvattrdef *) malloc(
542 num * sizeof(struct ntvattrdef),
543 M_NTFSMNT, M_WAITOK);
544
545 ntmp->ntm_adnum = num;
546
547 /* Read them and translate */
548 for(i=0;i<num;i++){
549 error = ntfs_readattr(ntmp, VTONT(vp),
550 NTFS_A_DATA, NULL,
551 i * sizeof(ad), sizeof(ad),
552 &ad, NULL);
553 if (error)
554 goto out1;
555 j = 0;
556 do {
557 ntmp->ntm_ad[i].ad_name[j] = ad.ad_name[j];
558 } while(ad.ad_name[j++]);
559 ntmp->ntm_ad[i].ad_namelen = j - 1;
560 ntmp->ntm_ad[i].ad_type = ad.ad_type;
561 }
562
563 vput(vp);
564 }
565
566 #if defined(__FreeBSD__)
567 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
568 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
569 #else
570 mp->mnt_stat.f_fsidx.__fsid_val[0] = dev;
571 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_NTFS);
572 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
573 mp->mnt_stat.f_namemax = NTFS_MAXFILENAME;
574 #endif
575 mp->mnt_flag |= MNT_LOCAL;
576 devvp->v_specmountpoint = mp;
577 return (0);
578
579 out1:
580 for(i=0;i<NTFS_SYSNODESNUM;i++)
581 if(ntmp->ntm_sysvn[i]) vrele(ntmp->ntm_sysvn[i]);
582
583 if (vflush(mp,NULLVP,0)) {
584 dprintf(("ntfs_mountfs: vflush failed\n"));
585 }
586 out:
587 devvp->v_specmountpoint = NULL;
588 if (bp)
589 brelse(bp);
590
591 if (error) {
592 if (ntmp) {
593 if (ntmp->ntm_ad)
594 free(ntmp->ntm_ad, M_NTFSMNT);
595 free(ntmp, M_NTFSMNT);
596 }
597 }
598
599 return (error);
600 }
601
602 static int
603 ntfs_start (
604 struct mount *mp,
605 int flags,
606 struct lwp *l)
607 {
608 return (0);
609 }
610
611 static int
612 ntfs_unmount(
613 struct mount *mp,
614 int mntflags,
615 struct lwp *l)
616 {
617 struct ntfsmount *ntmp;
618 int error, ronly = 0, flags, i;
619
620 dprintf(("ntfs_unmount: unmounting...\n"));
621 ntmp = VFSTONTFS(mp);
622
623 flags = 0;
624 if(mntflags & MNT_FORCE)
625 flags |= FORCECLOSE;
626
627 dprintf(("ntfs_unmount: vflushing...\n"));
628 error = vflush(mp,NULLVP,flags | SKIPSYSTEM);
629 if (error) {
630 dprintf(("ntfs_unmount: vflush failed: %d\n",error));
631 return (error);
632 }
633
634 /* Check if only system vnodes are rest */
635 for(i=0;i<NTFS_SYSNODESNUM;i++)
636 if((ntmp->ntm_sysvn[i]) &&
637 (ntmp->ntm_sysvn[i]->v_usecount > 1)) return (EBUSY);
638
639 /* Dereference all system vnodes */
640 for(i=0;i<NTFS_SYSNODESNUM;i++)
641 if(ntmp->ntm_sysvn[i]) vrele(ntmp->ntm_sysvn[i]);
642
643 /* vflush system vnodes */
644 error = vflush(mp,NULLVP,flags);
645 if (error) {
646 /* XXX should this be panic() ? */
647 printf("ntfs_unmount: vflush failed(sysnodes): %d\n",error);
648 }
649
650 /* Check if the type of device node isn't VBAD before
651 * touching v_specinfo. If the device vnode is revoked, the
652 * field is NULL and touching it causes null pointer derefercence.
653 */
654 if (ntmp->ntm_devvp->v_type != VBAD)
655 ntmp->ntm_devvp->v_specmountpoint = NULL;
656
657 vinvalbuf(ntmp->ntm_devvp, V_SAVE, NOCRED, l, 0, 0);
658
659 /* lock the device vnode before calling VOP_CLOSE() */
660 vn_lock(ntmp->ntm_devvp, LK_EXCLUSIVE | LK_RETRY);
661 error = VOP_CLOSE(ntmp->ntm_devvp, ronly ? FREAD : FREAD|FWRITE,
662 NOCRED, l);
663 VOP_UNLOCK(ntmp->ntm_devvp, 0);
664
665 vrele(ntmp->ntm_devvp);
666
667 /* free the toupper table, if this has been last mounted ntfs volume */
668 ntfs_toupper_unuse();
669
670 dprintf(("ntfs_umount: freeing memory...\n"));
671 mp->mnt_data = NULL;
672 mp->mnt_flag &= ~MNT_LOCAL;
673 free(ntmp->ntm_ad, M_NTFSMNT);
674 FREE(ntmp, M_NTFSMNT);
675 return (error);
676 }
677
678 static int
679 ntfs_root(
680 struct mount *mp,
681 struct vnode **vpp)
682 {
683 struct vnode *nvp;
684 int error = 0;
685
686 dprintf(("ntfs_root(): sysvn: %p\n",
687 VFSTONTFS(mp)->ntm_sysvn[NTFS_ROOTINO]));
688 error = VFS_VGET(mp, (ino_t)NTFS_ROOTINO, &nvp);
689 if(error) {
690 printf("ntfs_root: VFS_VGET failed: %d\n",error);
691 return (error);
692 }
693
694 *vpp = nvp;
695 return (0);
696 }
697
698 /*
699 * Do operations associated with quotas, not supported
700 */
701 /* ARGSUSED */
702 static int
703 ntfs_quotactl (
704 struct mount *mp,
705 int cmds,
706 uid_t uid,
707 void *arg,
708 struct lwp *l)
709 {
710
711 return EOPNOTSUPP;
712 }
713
714 int
715 ntfs_calccfree(
716 struct ntfsmount *ntmp,
717 cn_t *cfreep)
718 {
719 struct vnode *vp;
720 u_int8_t *tmp;
721 int j, error;
722 cn_t cfree = 0;
723 size_t bmsize, i;
724
725 vp = ntmp->ntm_sysvn[NTFS_BITMAPINO];
726
727 bmsize = VTOF(vp)->f_size;
728
729 tmp = (u_int8_t *) malloc(bmsize, M_TEMP, M_WAITOK);
730
731 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
732 0, bmsize, tmp, NULL);
733 if (error)
734 goto out;
735
736 for(i=0;i<bmsize;i++)
737 for(j=0;j<8;j++)
738 if(~tmp[i] & (1 << j)) cfree++;
739 *cfreep = cfree;
740
741 out:
742 free(tmp, M_TEMP);
743 return(error);
744 }
745
746 static int
747 ntfs_statvfs(
748 struct mount *mp,
749 struct statvfs *sbp,
750 struct lwp *l)
751 {
752 struct ntfsmount *ntmp = VFSTONTFS(mp);
753 u_int64_t mftallocated;
754
755 dprintf(("ntfs_statvfs():\n"));
756
757 mftallocated = VTOF(ntmp->ntm_sysvn[NTFS_MFTINO])->f_allocated;
758
759 #if defined(__FreeBSD__)
760 sbp->f_type = mp->mnt_vfc->vfc_typenum;
761 #endif
762 sbp->f_bsize = ntmp->ntm_bps;
763 sbp->f_frsize = sbp->f_bsize; /* XXX */
764 sbp->f_iosize = ntmp->ntm_bps * ntmp->ntm_spc;
765 sbp->f_blocks = ntmp->ntm_bootfile.bf_spv;
766 sbp->f_bfree = sbp->f_bavail = ntfs_cntobn(ntmp->ntm_cfree);
767 sbp->f_ffree = sbp->f_favail = sbp->f_bfree / ntmp->ntm_bpmftrec;
768 sbp->f_files = mftallocated / ntfs_bntob(ntmp->ntm_bpmftrec) +
769 sbp->f_ffree;
770 sbp->f_fresvd = sbp->f_bresvd = 0; /* XXX */
771 sbp->f_flag = mp->mnt_flag;
772 copy_statvfs_info(sbp, mp);
773 return (0);
774 }
775
776 static int
777 ntfs_sync (
778 struct mount *mp,
779 int waitfor,
780 kauth_cred_t cred,
781 struct lwp *l)
782 {
783 /*dprintf(("ntfs_sync():\n"));*/
784 return (0);
785 }
786
787 /*ARGSUSED*/
788 static int
789 ntfs_fhtovp(
790 #if defined(__FreeBSD__)
791 struct mount *mp,
792 struct fid *fhp,
793 struct sockaddr *nam,
794 struct vnode **vpp,
795 int *exflagsp,
796 struct ucred **credanonp)
797 #elif defined(__NetBSD__)
798 struct mount *mp,
799 struct fid *fhp,
800 struct vnode **vpp)
801 #else
802 struct mount *mp,
803 struct fid *fhp,
804 struct mbuf *nam,
805 struct vnode **vpp,
806 int *exflagsp,
807 struct ucred **credanonp)
808 #endif
809 {
810 struct ntfid ntfh;
811 int error;
812
813 if (fhp->fid_len != sizeof(struct ntfid))
814 return EINVAL;
815 memcpy(&ntfh, fhp, sizeof(ntfh));
816 ddprintf(("ntfs_fhtovp(): %s: %llu\n", mp->mnt_stat.f_mntonname,
817 (unsigned long long)ntfh.ntfid_ino));
818
819 error = ntfs_vgetex(mp, ntfh.ntfid_ino, ntfh.ntfid_attr, NULL,
820 LK_EXCLUSIVE | LK_RETRY, 0, vpp);
821 if (error != 0) {
822 *vpp = NULLVP;
823 return (error);
824 }
825
826 /* XXX as unlink/rmdir/mkdir/creat are not currently possible
827 * with NTFS, we don't need to check anything else for now */
828 return (0);
829 }
830
831 static int
832 ntfs_vptofh(
833 struct vnode *vp,
834 struct fid *fhp,
835 size_t *fh_size)
836 {
837 struct ntnode *ntp;
838 struct ntfid ntfh;
839 struct fnode *fn;
840
841 if (*fh_size < sizeof(struct ntfid)) {
842 *fh_size = sizeof(struct ntfid);
843 return E2BIG;
844 }
845 *fh_size = sizeof(struct ntfid);
846
847 ddprintf(("ntfs_fhtovp(): %s: %p\n", vp->v_mount->mnt_stat.f_mntonname,
848 vp));
849
850 fn = VTOF(vp);
851 ntp = VTONT(vp);
852 memset(&ntfh, 0, sizeof(ntfh));
853 ntfh.ntfid_len = sizeof(struct ntfid);
854 ntfh.ntfid_ino = ntp->i_number;
855 ntfh.ntfid_attr = fn->f_attrtype;
856 #ifdef notyet
857 ntfh.ntfid_gen = ntp->i_gen;
858 #endif
859 memcpy(fhp, &ntfh, sizeof(ntfh));
860 return (0);
861 }
862
863 int
864 ntfs_vgetex(
865 struct mount *mp,
866 ino_t ino,
867 u_int32_t attrtype,
868 char *attrname,
869 u_long lkflags,
870 u_long flags,
871 struct vnode **vpp)
872 {
873 int error;
874 struct ntfsmount *ntmp;
875 struct ntnode *ip;
876 struct fnode *fp;
877 struct vnode *vp;
878 enum vtype f_type = VBAD;
879
880 dprintf(("ntfs_vgetex: ino: %llu, attr: 0x%x:%s, lkf: 0x%lx, f:"
881 " 0x%lx\n", (unsigned long long)ino, attrtype,
882 attrname ? attrname : "", (u_long)lkflags, (u_long)flags));
883
884 ntmp = VFSTONTFS(mp);
885 *vpp = NULL;
886
887 /* Get ntnode */
888 error = ntfs_ntlookup(ntmp, ino, &ip);
889 if (error) {
890 printf("ntfs_vget: ntfs_ntget failed\n");
891 return (error);
892 }
893
894 /* It may be not initialized fully, so force load it */
895 if (!(flags & VG_DONTLOADIN) && !(ip->i_flag & IN_LOADED)) {
896 error = ntfs_loadntnode(ntmp, ip);
897 if(error) {
898 printf("ntfs_vget: CAN'T LOAD ATTRIBUTES FOR INO:"
899 " %llu\n", (unsigned long long)ip->i_number);
900 ntfs_ntput(ip);
901 return (error);
902 }
903 }
904
905 error = ntfs_fget(ntmp, ip, attrtype, attrname, &fp);
906 if (error) {
907 printf("ntfs_vget: ntfs_fget failed\n");
908 ntfs_ntput(ip);
909 return (error);
910 }
911
912 if (!(flags & VG_DONTVALIDFN) && !(fp->f_flag & FN_VALID)) {
913 if ((ip->i_frflag & NTFS_FRFLAG_DIR) &&
914 (fp->f_attrtype == NTFS_A_DATA && fp->f_attrname == NULL)) {
915 f_type = VDIR;
916 } else if (flags & VG_EXT) {
917 f_type = VNON;
918 fp->f_size = fp->f_allocated = 0;
919 } else {
920 f_type = VREG;
921
922 error = ntfs_filesize(ntmp, fp,
923 &fp->f_size, &fp->f_allocated);
924 if (error) {
925 ntfs_ntput(ip);
926 return (error);
927 }
928 }
929
930 fp->f_flag |= FN_VALID;
931 }
932
933 /*
934 * We may be calling vget() now. To avoid potential deadlock, we need
935 * to release ntnode lock, since due to locking order vnode
936 * lock has to be acquired first.
937 * ntfs_fget() bumped ntnode usecount, so ntnode won't be recycled
938 * prematurely.
939 */
940 ntfs_ntput(ip);
941
942 if (FTOV(fp)) {
943 /* vget() returns error if the vnode has been recycled */
944 if (vget(FTOV(fp), lkflags) == 0) {
945 *vpp = FTOV(fp);
946 return (0);
947 }
948 }
949
950 error = getnewvnode(VT_NTFS, ntmp->ntm_mountp, ntfs_vnodeop_p, &vp);
951 if(error) {
952 ntfs_frele(fp);
953 ntfs_ntput(ip);
954 return (error);
955 }
956 dprintf(("ntfs_vget: vnode: %p for ntnode: %llu\n", vp,
957 (unsigned long long)ino));
958
959 #ifdef __FreeBSD__
960 lockinit(&fp->f_lock, PINOD, "fnode", 0, 0);
961 #endif
962 fp->f_vp = vp;
963 vp->v_data = fp;
964 if (f_type != VBAD)
965 vp->v_type = f_type;
966
967 if (ino == NTFS_ROOTINO)
968 vp->v_flag |= VROOT;
969
970 if (lkflags & LK_TYPE_MASK) {
971 error = vn_lock(vp, lkflags);
972 if (error) {
973 vput(vp);
974 return (error);
975 }
976 }
977
978 genfs_node_init(vp, &ntfs_genfsops);
979 uvm_vnp_setsize(vp, 0); /* XXX notused */
980 VREF(ip->i_devvp);
981 *vpp = vp;
982 return (0);
983 }
984
985 static int
986 ntfs_vget(
987 struct mount *mp,
988 ino_t ino,
989 struct vnode **vpp)
990 {
991 return ntfs_vgetex(mp, ino, NTFS_A_DATA, NULL,
992 LK_EXCLUSIVE | LK_RETRY, 0, vpp);
993 }
994
995 #if defined(__FreeBSD__)
996 static struct vfsops ntfs_vfsops = {
997 ntfs_mount,
998 ntfs_start,
999 ntfs_unmount,
1000 ntfs_root,
1001 ntfs_quotactl,
1002 ntfs_statvfs,
1003 ntfs_sync,
1004 ntfs_vget,
1005 ntfs_fhtovp,
1006 ntfs_vptofh,
1007 ntfs_init,
1008 NULL
1009 };
1010 VFS_SET(ntfs_vfsops, ntfs, 0);
1011 #elif defined(__NetBSD__)
1012 extern const struct vnodeopv_desc ntfs_vnodeop_opv_desc;
1013
1014 const struct vnodeopv_desc * const ntfs_vnodeopv_descs[] = {
1015 &ntfs_vnodeop_opv_desc,
1016 NULL,
1017 };
1018
1019 struct vfsops ntfs_vfsops = {
1020 MOUNT_NTFS,
1021 #if !defined(__FreeBSD__)
1022 sizeof (struct ntfs_args),
1023 #endif
1024 ntfs_mount,
1025 ntfs_start,
1026 ntfs_unmount,
1027 ntfs_root,
1028 ntfs_quotactl,
1029 ntfs_statvfs,
1030 ntfs_sync,
1031 ntfs_vget,
1032 ntfs_fhtovp,
1033 ntfs_vptofh,
1034 ntfs_init,
1035 ntfs_reinit,
1036 ntfs_done,
1037 ntfs_mountroot,
1038 (int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
1039 vfs_stdextattrctl,
1040 vfs_stdsuspendctl,
1041 ntfs_vnodeopv_descs,
1042 0,
1043 { NULL, NULL },
1044 };
1045 VFS_ATTACH(ntfs_vfsops);
1046 #endif
1047