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