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