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