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