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