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