Add missing bit of the wchar stdio support and hook the whole thing in.
[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 $
43c45e8f 29 * $DragonFly: src/sys/vfs/ntfs/ntfs_vfsops.c,v 1.28 2005/07/26 15:43:36 hmp 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>
fad57d0e 37#include <sys/nlookup.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*/
1f2de5d4
MD
64#include "ntfs.h"
65#include "ntfs_inode.h"
66#include "ntfs_subr.h"
67#include "ntfs_vfsops.h"
68#include "ntfs_ihash.h"
69#include "ntfsmount.h"
984263bc 70
0961aa92
MD
71extern struct vnodeopv_entry_desc ntfs_vnodeop_entries[];
72
08c23b99 73#if defined(__DragonFly__)
984263bc
MD
74MALLOC_DEFINE(M_NTFSMNT, "NTFS mount", "NTFS mount structure");
75MALLOC_DEFINE(M_NTFSNTNODE,"NTFS ntnode", "NTFS ntnode information");
76MALLOC_DEFINE(M_NTFSFNODE,"NTFS fnode", "NTFS fnode information");
77MALLOC_DEFINE(M_NTFSDIR,"NTFS dir", "NTFS dir buffer");
78#endif
79
a6ee311a
RG
80static int ntfs_root (struct mount *, struct vnode **);
81static int ntfs_statfs (struct mount *, struct statfs *,
82 struct thread *);
83static int ntfs_unmount (struct mount *, int, struct thread *);
84static int ntfs_vget (struct mount *mp, ino_t ino,
85 struct vnode **vpp);
86static int ntfs_mountfs (struct vnode *, struct mount *,
87 struct ntfs_args *, struct thread *);
88static int ntfs_vptofh (struct vnode *, struct fid *);
89static int ntfs_fhtovp (struct mount *, struct fid *,
90 struct vnode **);
984263bc 91
08c23b99 92#if !defined (__DragonFly__)
a6ee311a
RG
93static int ntfs_quotactl (struct mount *, int, uid_t, caddr_t,
94 struct thread *);
95static int ntfs_start (struct mount *, int, struct thread *);
96static int ntfs_sync (struct mount *, int, struct ucred *,
97 struct thread *);
984263bc
MD
98#endif
99
08c23b99 100#if defined(__DragonFly__)
984263bc 101struct sockaddr;
21739618 102static int ntfs_mount (struct mount *, char *, caddr_t, struct thread *);
a6ee311a
RG
103static int ntfs_init (struct vfsconf *);
104static int ntfs_checkexp (struct mount *, struct sockaddr *,
105 int *, struct ucred **);
984263bc 106#elif defined(__NetBSD__)
a6ee311a
RG
107static int ntfs_mount (struct mount *, const char *, void *,
108 struct nameidata *, struct thread *);
109static void ntfs_init (void);
110static int ntfs_mountroot (void);
111static int ntfs_sysctl (int *, u_int, void *, size_t *, void *,
112 size_t, struct thread *);
113static int ntfs_checkexp (struct mount *, struct mbuf *,
114 int *, struct ucred **);
984263bc
MD
115#endif
116
117/*
118 * Verify a remote client has export rights and return these rights via.
119 * exflagsp and credanonp.
120 */
121static int
cd0a65a2 122ntfs_checkexp(struct mount *mp,
08c23b99 123#if defined(__DragonFly__)
cd0a65a2 124 struct sockaddr *nam,
984263bc 125#else /* defined(__NetBSD__) */
cd0a65a2 126 struct mbuf *nam,
984263bc 127#endif
cd0a65a2 128 int *exflagsp, struct ucred **credanonp)
984263bc 129{
34ee9825
RG
130 struct netcred *np;
131 struct ntfsmount *ntm = VFSTONTFS(mp);
984263bc
MD
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#if defined(__NetBSD__)
146/*ARGSUSED*/
147static int
cd0a65a2
CP
148ntfs_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
149 size_t newlen, struct thread *td)
984263bc
MD
150{
151 return (EINVAL);
152}
153
154static int
cd0a65a2 155ntfs_mountroot(void)
984263bc
MD
156{
157 struct mount *mp;
21739618 158 struct vnode *rootvp;
dadab5e9 159 struct thread *td = curthread; /* XXX */
984263bc 160 struct ntfs_args args;
41a01a4d
MD
161 lwkt_tokref ilock;
162 int error;
984263bc
MD
163
164 if (root_device->dv_class != DV_DISK)
165 return (ENODEV);
166
167 /*
168 * Get vnodes for rootdev.
169 */
170 if (bdevvp(rootdev, &rootvp))
171 panic("ntfs_mountroot: can't setup rootvp");
172
173 if ((error = vfs_rootmountalloc(MOUNT_NTFS, "root_device", &mp))) {
174 vrele(rootvp);
175 return (error);
176 }
177
178 args.flag = 0;
179 args.uid = 0;
180 args.gid = 0;
181 args.mode = 0777;
182
dadab5e9 183 if ((error = ntfs_mountfs(rootvp, mp, &args, td)) != 0) {
984263bc
MD
184 mp->mnt_op->vfs_refcount--;
185 vfs_unbusy(mp);
186 free(mp, M_MOUNT);
187 vrele(rootvp);
188 return (error);
189 }
861905fb 190 mountlist_insert(mp, MNTINS_LAST);
dadab5e9 191 (void)ntfs_statfs(mp, &mp->mnt_stat, td);
984263bc
MD
192 vfs_unbusy(mp);
193 return (0);
194}
195
196static void
cd0a65a2 197ntfs_init(void)
984263bc
MD
198{
199 ntfs_nthashinit();
200 ntfs_toupper_init();
201}
202
08c23b99 203#elif defined(__DragonFly__)
984263bc
MD
204
205static int
cd0a65a2 206ntfs_init(struct vfsconf *vcp)
984263bc
MD
207{
208 ntfs_nthashinit();
209 ntfs_toupper_init();
210 return 0;
211}
212
213#endif /* NetBSD */
214
215static int
cd0a65a2 216ntfs_mount(struct mount *mp,
08c23b99 217#if defined(__DragonFly__)
cd0a65a2 218 char *path, caddr_t data,
984263bc 219#else
cd0a65a2 220 const char *path, void *data,
984263bc 221#endif
21739618 222 struct thread *td)
cd0a65a2 223{
984263bc 224 size_t size;
fad57d0e 225 int error;
984263bc
MD
226 struct vnode *devvp;
227 struct ntfs_args args;
fad57d0e 228 struct nlookupdata nd;
21739618 229 struct vnode *rootvp;
984263bc 230
fad57d0e 231 error = 0;
08c23b99 232#ifdef __DragonFly__
984263bc
MD
233 /*
234 * Use NULL path to flag a root mount
235 */
236 if( path == NULL) {
237 /*
238 ***
239 * Mounting root file system
240 ***
241 */
242
243 /* Get vnode for root device*/
244 if( bdevvp( rootdev, &rootvp))
245 panic("ffs_mountroot: can't setup bdevvp for root");
246
247 /*
248 * FS specific handling
249 */
250 mp->mnt_flag |= MNT_RDONLY; /* XXX globally applicable?*/
251
252 /*
253 * Attempt mount
254 */
fad57d0e 255 if( ( error = ntfs_mountfs(rootvp, mp, &args, td)) != 0) {
984263bc
MD
256 /* fs specific cleanup (if any)*/
257 goto error_1;
258 }
259
260 goto dostatfs; /* success*/
261
262 }
263#endif /* FreeBSD */
264
265 /*
266 ***
267 * Mounting non-root file system or updating a file system
268 ***
269 */
270
271 /* copy in user arguments*/
fad57d0e
MD
272 error = copyin(data, (caddr_t)&args, sizeof (struct ntfs_args));
273 if (error)
984263bc
MD
274 goto error_1; /* can't get arguments*/
275
276 /*
277 * If updating, check whether changing from read-only to
278 * read/write; if there is no device name, that's all we do.
279 */
280 if (mp->mnt_flag & MNT_UPDATE) {
281 /* if not updating name...*/
282 if (args.fspec == 0) {
283 /*
284 * Process export requests. Jumping to "success"
285 * will return the vfs_export() error code.
286 */
287 struct ntfsmount *ntm = VFSTONTFS(mp);
fad57d0e 288 error = vfs_export(mp, &ntm->ntm_export, &args.export);
984263bc
MD
289 goto success;
290 }
291
292 printf("ntfs_mount(): MNT_UPDATE not supported\n");
fad57d0e 293 error = EINVAL;
984263bc
MD
294 goto error_1;
295 }
296
297 /*
298 * Not an update, or updating the name: look up the name
299 * and verify that it refers to a sensible block device.
300 */
fad57d0e
MD
301 devvp = NULL;
302 error = nlookup_init(&nd, args.fspec, UIO_USERSPACE, NLC_FOLLOW);
303 if (error == 0)
304 error = nlookup(&nd);
305 if (error == 0)
306 error = cache_vref(nd.nl_ncp, nd.nl_cred, &devvp);
307 nlookup_done(&nd);
308 if (error)
984263bc 309 goto error_1;
984263bc 310
08c23b99 311#if defined(__DragonFly__)
fad57d0e 312 if (!vn_isdisk(devvp, &error))
984263bc
MD
313 goto error_2;
314#else
315 if (devvp->v_type != VBLK) {
fad57d0e 316 error = ENOTBLK;
984263bc
MD
317 goto error_2;
318 }
e4c9c0c8 319 if (umajor(devvp->v_udev) >= nblkdev) {
fad57d0e 320 error = ENXIO;
984263bc
MD
321 goto error_2;
322 }
323#endif
324 if (mp->mnt_flag & MNT_UPDATE) {
325#if 0
326 /*
327 ********************
328 * UPDATE
329 ********************
330 */
331
332 if (devvp != ntmp->um_devvp)
fad57d0e 333 error = EINVAL; /* needs translation */
984263bc
MD
334 else
335 vrele(devvp);
336 /*
337 * Update device name only on success
338 */
fad57d0e 339 if( !error) {
984263bc
MD
340 /* Save "mounted from" info for mount point (NULL pad)*/
341 copyinstr( args.fspec,
342 mp->mnt_stat.f_mntfromname,
343 MNAMELEN - 1,
344 &size);
345 bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
346 }
347#endif
348 } else {
349 /*
350 ********************
351 * NEW MOUNT
352 ********************
353 */
354
984263bc
MD
355 /* Save "mounted from" info for mount point (NULL pad)*/
356 copyinstr( args.fspec, /* device name*/
357 mp->mnt_stat.f_mntfromname, /* save area*/
358 MNAMELEN - 1, /* max size*/
359 &size); /* real size*/
360 bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
361
fad57d0e 362 error = ntfs_mountfs(devvp, mp, &args, td);
984263bc 363 }
fad57d0e 364 if (error) {
984263bc
MD
365 goto error_2;
366 }
367
08c23b99 368#ifdef __DragonFly__
984263bc
MD
369dostatfs:
370#endif
371 /*
75ffff0d
JS
372 * Initialize FS stat information in mount struct; uses
373 * mp->mnt_stat.f_mntfromname.
984263bc
MD
374 *
375 * This code is common to root and non-root mounts
376 */
dadab5e9 377 (void)VFS_STATFS(mp, &mp->mnt_stat, td);
984263bc
MD
378
379 goto success;
380
381
382error_2: /* error with devvp held*/
383
384 /* release devvp before failing*/
385 vrele(devvp);
386
387error_1: /* no state to back out*/
388
389success:
fad57d0e 390 return(error);
984263bc
MD
391}
392
393/*
394 * Common code for mount and mountroot
395 */
396int
cd0a65a2
CP
397ntfs_mountfs(struct vnode *devvp, struct mount *mp, struct ntfs_args *argsp,
398 struct thread *td)
984263bc
MD
399{
400 struct buf *bp;
401 struct ntfsmount *ntmp;
e4c9c0c8 402 dev_t dev;
984263bc
MD
403 int error, ronly, ncount, i;
404 struct vnode *vp;
dadab5e9
MD
405 struct ucred *cred;
406
407 KKASSERT(td->td_proc);
408 cred = td->td_proc->p_ucred;
984263bc
MD
409
410 /*
411 * Disallow multiple mounts of the same device.
412 * Disallow mounting of a device that is currently in use
413 * (except for root, which might share swap device for miniroot).
414 * Flush out any old buffers remaining from a previous use.
415 */
416 error = vfs_mountedon(devvp);
417 if (error)
418 return (error);
715f92b6 419 ncount = count_udev(devvp->v_udev);
08c23b99 420#if defined(__DragonFly__)
984263bc
MD
421 if (devvp->v_object)
422 ncount -= 1;
423#endif
21739618 424 if (ncount > 1)
984263bc 425 return (EBUSY);
08c23b99 426#if defined(__DragonFly__)
dadab5e9 427 VN_LOCK(devvp, LK_EXCLUSIVE | LK_RETRY, td);
3b568787 428 error = vinvalbuf(devvp, V_SAVE, td, 0, 0);
dadab5e9 429 VOP__UNLOCK(devvp, 0, td);
984263bc 430#else
3b568787 431 error = vinvalbuf(devvp, V_SAVE, td, 0, 0);
984263bc
MD
432#endif
433 if (error)
434 return (error);
435
436 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
dadab5e9 437 VN_LOCK(devvp, LK_EXCLUSIVE | LK_RETRY, td);
fad57d0e 438 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, NULL, td);
dadab5e9 439 VOP__UNLOCK(devvp, 0, td);
984263bc
MD
440 if (error)
441 return (error);
e4c9c0c8 442 dev = devvp->v_rdev;
984263bc
MD
443
444 bp = NULL;
445
3b568787 446 error = bread(devvp, BBLOCK, BBSIZE, &bp);
984263bc
MD
447 if (error)
448 goto out;
449 ntmp = malloc( sizeof *ntmp, M_NTFSMNT, M_WAITOK );
450 bzero( ntmp, sizeof *ntmp );
451 bcopy( bp->b_data, &ntmp->ntm_bootfile, sizeof(struct bootfile) );
452 brelse( bp );
453 bp = NULL;
454
455 if (strncmp(ntmp->ntm_bootfile.bf_sysid, NTFS_BBID, NTFS_BBIDLEN)) {
456 error = EINVAL;
457 dprintf(("ntfs_mountfs: invalid boot block\n"));
458 goto out;
459 }
460
461 {
462 int8_t cpr = ntmp->ntm_mftrecsz;
463 if( cpr > 0 )
464 ntmp->ntm_bpmftrec = ntmp->ntm_spc * cpr;
465 else
466 ntmp->ntm_bpmftrec = (1 << (-cpr)) / ntmp->ntm_bps;
467 }
468 dprintf(("ntfs_mountfs(): bps: %d, spc: %d, media: %x, mftrecsz: %d (%d sects)\n",
469 ntmp->ntm_bps,ntmp->ntm_spc,ntmp->ntm_bootfile.bf_media,
470 ntmp->ntm_mftrecsz,ntmp->ntm_bpmftrec));
471 dprintf(("ntfs_mountfs(): mftcn: 0x%x|0x%x\n",
472 (u_int32_t)ntmp->ntm_mftcn,(u_int32_t)ntmp->ntm_mftmirrcn));
473
474 ntmp->ntm_mountp = mp;
475 ntmp->ntm_dev = dev;
476 ntmp->ntm_devvp = devvp;
477 ntmp->ntm_uid = argsp->uid;
478 ntmp->ntm_gid = argsp->gid;
479 ntmp->ntm_mode = argsp->mode;
480 ntmp->ntm_flag = argsp->flag;
481
482 /* Copy in the 8-bit to Unicode conversion table */
483 if (argsp->flag & NTFSMNT_U2WTABLE) {
484 ntfs_82u_init(ntmp, argsp->u2w);
485 } else {
486 ntfs_82u_init(ntmp, NULL);
487 }
488
489 /* Initialize Unicode to 8-bit table from 8toU table */
490 ntfs_u28_init(ntmp, ntmp->ntm_82u);
491
492 mp->mnt_data = (qaddr_t)ntmp;
493
494 dprintf(("ntfs_mountfs(): case-%s,%s uid: %d, gid: %d, mode: %o\n",
495 (ntmp->ntm_flag & NTFS_MFLAG_CASEINS)?"insens.":"sens.",
496 (ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES)?" allnames,":"",
497 ntmp->ntm_uid, ntmp->ntm_gid, ntmp->ntm_mode));
498
6ddb7618 499 vfs_add_vnodeops(mp, &mp->mnt_vn_norm_ops, ntfs_vnodeop_entries);
320bc044 500
984263bc
MD
501 /*
502 * We read in some system nodes to do not allow
503 * reclaim them and to have everytime access to them.
504 */
505 {
506 int pi[3] = { NTFS_MFTINO, NTFS_ROOTINO, NTFS_BITMAPINO };
507 for (i=0; i<3; i++) {
508 error = VFS_VGET(mp, pi[i], &(ntmp->ntm_sysvn[pi[i]]));
509 if(error)
510 goto out1;
511 ntmp->ntm_sysvn[pi[i]]->v_flag |= VSYSTEM;
597aea93 512 vref(ntmp->ntm_sysvn[pi[i]]);
984263bc
MD
513 vput(ntmp->ntm_sysvn[pi[i]]);
514 }
515 }
516
517 /* read the Unicode lowercase --> uppercase translation table,
518 * if necessary */
519 if ((error = ntfs_toupper_use(mp, ntmp)))
520 goto out1;
521
522 /*
523 * Scan $BitMap and count free clusters
524 */
525 error = ntfs_calccfree(ntmp, &ntmp->ntm_cfree);
526 if(error)
527 goto out1;
528
529 /*
530 * Read and translate to internal format attribute
531 * definition file.
532 */
533 {
534 int num,j;
535 struct attrdef ad;
536
537 /* Open $AttrDef */
538 error = VFS_VGET(mp, NTFS_ATTRDEFINO, &vp );
539 if(error)
540 goto out1;
541
542 /* Count valid entries */
543 for(num=0;;num++) {
544 error = ntfs_readattr(ntmp, VTONT(vp),
545 NTFS_A_DATA, NULL,
546 num * sizeof(ad), sizeof(ad),
547 &ad, NULL);
548 if (error)
549 goto out1;
550 if (ad.ad_name[0] == 0)
551 break;
552 }
553
554 /* Alloc memory for attribute definitions */
555 MALLOC(ntmp->ntm_ad, struct ntvattrdef *,
556 num * sizeof(struct ntvattrdef),
557 M_NTFSMNT, M_WAITOK);
558
559 ntmp->ntm_adnum = num;
560
561 /* Read them and translate */
562 for(i=0;i<num;i++){
563 error = ntfs_readattr(ntmp, VTONT(vp),
564 NTFS_A_DATA, NULL,
565 i * sizeof(ad), sizeof(ad),
566 &ad, NULL);
567 if (error)
568 goto out1;
569 j = 0;
570 do {
571 ntmp->ntm_ad[i].ad_name[j] = ad.ad_name[j];
572 } while(ad.ad_name[j++]);
573 ntmp->ntm_ad[i].ad_namelen = j - 1;
574 ntmp->ntm_ad[i].ad_type = ad.ad_type;
575 }
576
577 vput(vp);
578 }
579
08c23b99 580#if defined(__DragonFly__)
984263bc
MD
581 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
582 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
583#else
584 mp->mnt_stat.f_fsid.val[0] = dev;
585 mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_NTFS);
586#endif
587 mp->mnt_maxsymlinklen = 0;
588 mp->mnt_flag |= MNT_LOCAL;
e4c9c0c8 589 dev->si_mountpoint = mp;
0961aa92 590
984263bc
MD
591 return (0);
592
593out1:
594 for(i=0;i<NTFS_SYSNODESNUM;i++)
595 if(ntmp->ntm_sysvn[i]) vrele(ntmp->ntm_sysvn[i]);
596
597 if (vflush(mp, 0, 0))
598 dprintf(("ntfs_mountfs: vflush failed\n"));
599
600out:
e4c9c0c8 601 dev->si_mountpoint = NULL;
984263bc
MD
602 if (bp)
603 brelse(bp);
604
605#if defined __NetBSD__
606 /* lock the device vnode before calling VOP_CLOSE() */
dadab5e9 607 VN_LOCK(devvp, LK_EXCLUSIVE | LK_RETRY, td);
3b568787 608 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, td);
dadab5e9 609 VOP__UNLOCK(devvp, 0, td);
984263bc 610#else
3b568787 611 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, td);
984263bc
MD
612#endif
613
614 return (error);
615}
616
08c23b99 617#if !defined(__DragonFly__)
984263bc 618static int
cd0a65a2 619ntfs_start(struct mount *mp, int flags, struct thread *td)
984263bc
MD
620{
621 return (0);
622}
623#endif
624
625static int
cd0a65a2 626ntfs_unmount(struct mount *mp, int mntflags, struct thread *td)
984263bc 627{
34ee9825 628 struct ntfsmount *ntmp;
984263bc
MD
629 int error, ronly = 0, flags, i;
630
631 dprintf(("ntfs_unmount: unmounting...\n"));
632 ntmp = VFSTONTFS(mp);
633
634 flags = 0;
635 if(mntflags & MNT_FORCE)
636 flags |= FORCECLOSE;
637
638 dprintf(("ntfs_unmount: vflushing...\n"));
639 error = vflush(mp, 0, flags | SKIPSYSTEM);
640 if (error) {
641 printf("ntfs_unmount: vflush failed: %d\n",error);
642 return (error);
643 }
644
645 /* Check if only system vnodes are rest */
646 for(i=0;i<NTFS_SYSNODESNUM;i++)
647 if((ntmp->ntm_sysvn[i]) &&
648 (ntmp->ntm_sysvn[i]->v_usecount > 1)) return (EBUSY);
649
650 /* Dereference all system vnodes */
651 for(i=0;i<NTFS_SYSNODESNUM;i++)
652 if(ntmp->ntm_sysvn[i]) vrele(ntmp->ntm_sysvn[i]);
653
654 /* vflush system vnodes */
655 error = vflush(mp, 0, flags);
656 if (error)
657 printf("ntfs_unmount: vflush failed(sysnodes): %d\n",error);
658
659 /* Check if the type of device node isn't VBAD before
660 * touching v_specinfo. If the device vnode is revoked, the
661 * field is NULL and touching it causes null pointer derefercence.
662 */
663 if (ntmp->ntm_devvp->v_type != VBAD)
e4c9c0c8 664 ntmp->ntm_devvp->v_rdev->si_mountpoint = NULL;
984263bc 665
3b568787 666 vinvalbuf(ntmp->ntm_devvp, V_SAVE, td, 0, 0);
984263bc
MD
667
668#if defined(__NetBSD__)
669 /* lock the device vnode before calling VOP_CLOSE() */
670 VOP_LOCK(ntmp->ntm_devvp, LK_EXCLUSIVE | LK_RETRY);
3b568787 671 error = VOP_CLOSE(ntmp->ntm_devvp, ronly ? FREAD : FREAD|FWRITE, td);
dadab5e9 672 VOP__UNLOCK(ntmp->ntm_devvp, 0, td);
984263bc 673#else
3b568787 674 error = VOP_CLOSE(ntmp->ntm_devvp, ronly ? FREAD : FREAD|FWRITE, td);
984263bc
MD
675#endif
676
677 vrele(ntmp->ntm_devvp);
678
679 /* free the toupper table, if this has been last mounted ntfs volume */
680 ntfs_toupper_unuse();
681
682 dprintf(("ntfs_umount: freeing memory...\n"));
683 ntfs_u28_uninit(ntmp);
684 ntfs_82u_uninit(ntmp);
685 mp->mnt_data = (qaddr_t)0;
686 mp->mnt_flag &= ~MNT_LOCAL;
687 FREE(ntmp->ntm_ad, M_NTFSMNT);
688 FREE(ntmp, M_NTFSMNT);
689 return (error);
690}
691
692static int
cd0a65a2 693ntfs_root(struct mount *mp, struct vnode **vpp)
984263bc
MD
694{
695 struct vnode *nvp;
696 int error = 0;
697
698 dprintf(("ntfs_root(): sysvn: %p\n",
699 VFSTONTFS(mp)->ntm_sysvn[NTFS_ROOTINO]));
700 error = VFS_VGET(mp, (ino_t)NTFS_ROOTINO, &nvp);
701 if(error) {
702 printf("ntfs_root: VFS_VGET failed: %d\n",error);
703 return (error);
704 }
705
706 *vpp = nvp;
707 return (0);
708}
709
08c23b99 710#if !defined(__DragonFly__)
984263bc 711static int
cd0a65a2
CP
712ntfs_quotactl(struct mount *mp, int cmds, uid_t uid, caddr_t arg,
713 struct thread *td)
984263bc
MD
714{
715 printf("\nntfs_quotactl():\n");
716 return EOPNOTSUPP;
717}
718#endif
719
720int
cd0a65a2 721ntfs_calccfree(struct ntfsmount *ntmp, cn_t *cfreep)
984263bc
MD
722{
723 struct vnode *vp;
724 u_int8_t *tmp;
725 int j, error;
726 long cfree = 0;
727 size_t bmsize, i;
728
729 vp = ntmp->ntm_sysvn[NTFS_BITMAPINO];
730
731 bmsize = VTOF(vp)->f_size;
732
733 MALLOC(tmp, u_int8_t *, bmsize, M_TEMP, M_WAITOK);
734
735 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
736 0, bmsize, tmp, NULL);
737 if (error)
738 goto out;
739
740 for(i=0;i<bmsize;i++)
741 for(j=0;j<8;j++)
742 if(~tmp[i] & (1 << j)) cfree++;
743 *cfreep = cfree;
744
745 out:
746 FREE(tmp, M_TEMP);
747 return(error);
748}
749
750static int
cd0a65a2 751ntfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
984263bc
MD
752{
753 struct ntfsmount *ntmp = VFSTONTFS(mp);
754 u_int64_t mftsize,mftallocated;
755
756 dprintf(("ntfs_statfs():\n"));
757
758 mftsize = VTOF(ntmp->ntm_sysvn[NTFS_MFTINO])->f_size;
759 mftallocated = VTOF(ntmp->ntm_sysvn[NTFS_MFTINO])->f_allocated;
760
08c23b99 761#if defined(__DragonFly__)
984263bc
MD
762 sbp->f_type = mp->mnt_vfc->vfc_typenum;
763#elif defined(__NetBSD__)
764 sbp->f_type = 0;
765#else
766 sbp->f_type = MOUNT_NTFS;
767#endif
768 sbp->f_bsize = ntmp->ntm_bps;
769 sbp->f_iosize = ntmp->ntm_bps * ntmp->ntm_spc;
770 sbp->f_blocks = ntmp->ntm_bootfile.bf_spv;
771 sbp->f_bfree = sbp->f_bavail = ntfs_cntobn(ntmp->ntm_cfree);
772 sbp->f_ffree = sbp->f_bfree / ntmp->ntm_bpmftrec;
773 sbp->f_files = mftallocated / ntfs_bntob(ntmp->ntm_bpmftrec) +
774 sbp->f_ffree;
775 if (sbp != &mp->mnt_stat) {
984263bc
MD
776 bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
777 (caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
778 }
779 sbp->f_flags = mp->mnt_flag;
780#ifdef __NetBSD__
781 strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN);
782#endif
783
784 return (0);
785}
786
08c23b99 787#if !defined(__DragonFly__)
984263bc 788static int
cd0a65a2 789ntfs_sync(struct mount *mp, int waitfor, struct ucred *cred, struct thread *td)
984263bc
MD
790{
791 /*dprintf(("ntfs_sync():\n"));*/
792 return (0);
793}
794#endif
795
796/*ARGSUSED*/
797static int
cd0a65a2 798ntfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
984263bc
MD
799{
800 struct vnode *nvp;
801 struct ntfid *ntfhp = (struct ntfid *)fhp;
802 int error;
803
804 ddprintf(("ntfs_fhtovp(): %d\n", ntfhp->ntfid_ino));
805
806 if ((error = VFS_VGET(mp, ntfhp->ntfid_ino, &nvp)) != 0) {
807 *vpp = NULLVP;
808 return (error);
809 }
810 /* XXX as unlink/rmdir/mkdir/creat are not currently possible
811 * with NTFS, we don't need to check anything else for now */
812 *vpp = nvp;
813
814 return (0);
815}
816
817static int
cd0a65a2 818ntfs_vptofh(struct vnode *vp, struct fid *fhp)
984263bc 819{
34ee9825
RG
820 struct ntnode *ntp;
821 struct ntfid *ntfhp;
984263bc
MD
822
823 ddprintf(("ntfs_fhtovp(): %p\n", vp));
824
825 ntp = VTONT(vp);
826 ntfhp = (struct ntfid *)fhp;
827 ntfhp->ntfid_len = sizeof(struct ntfid);
828 ntfhp->ntfid_ino = ntp->i_number;
829 /* ntfhp->ntfid_gen = ntp->i_gen; */
830 return (0);
831}
832
833int
cd0a65a2
CP
834ntfs_vgetex(struct mount *mp, ino_t ino, u_int32_t attrtype, char *attrname,
835 u_long lkflags, u_long flags, struct thread *td,
836 struct vnode **vpp)
984263bc
MD
837{
838 int error;
34ee9825 839 struct ntfsmount *ntmp;
984263bc
MD
840 struct ntnode *ip;
841 struct fnode *fp;
842 struct vnode *vp;
843 enum vtype f_type;
844
845 dprintf(("ntfs_vgetex: ino: %d, attr: 0x%x:%s, lkf: 0x%lx, f: 0x%lx\n",
846 ino, attrtype, attrname?attrname:"", (u_long)lkflags,
847 (u_long)flags ));
848
849 ntmp = VFSTONTFS(mp);
850 *vpp = NULL;
851
852 /* Get ntnode */
853 error = ntfs_ntlookup(ntmp, ino, &ip);
854 if (error) {
855 printf("ntfs_vget: ntfs_ntget failed\n");
856 return (error);
857 }
858
859 /* It may be not initialized fully, so force load it */
860 if (!(flags & VG_DONTLOADIN) && !(ip->i_flag & IN_LOADED)) {
861 error = ntfs_loadntnode(ntmp, ip);
862 if(error) {
863 printf("ntfs_vget: CAN'T LOAD ATTRIBUTES FOR INO: %d\n",
864 ip->i_number);
865 ntfs_ntput(ip);
866 return (error);
867 }
868 }
869
870 error = ntfs_fget(ntmp, ip, attrtype, attrname, &fp);
871 if (error) {
872 printf("ntfs_vget: ntfs_fget failed\n");
873 ntfs_ntput(ip);
874 return (error);
875 }
876
877 f_type = VNON;
878 if (!(flags & VG_DONTVALIDFN) && !(fp->f_flag & FN_VALID)) {
879 if ((ip->i_frflag & NTFS_FRFLAG_DIR) &&
880 (fp->f_attrtype == NTFS_A_DATA && fp->f_attrname == NULL)) {
881 f_type = VDIR;
882 } else if (flags & VG_EXT) {
883 f_type = VNON;
884 fp->f_size = fp->f_allocated = 0;
885 } else {
886 f_type = VREG;
887
888 error = ntfs_filesize(ntmp, fp,
889 &fp->f_size, &fp->f_allocated);
890 if (error) {
891 ntfs_ntput(ip);
892 return (error);
893 }
894 }
895
896 fp->f_flag |= FN_VALID;
897 }
898
899 if (FTOV(fp)) {
dadab5e9 900 VGET(FTOV(fp), lkflags, td);
984263bc
MD
901 *vpp = FTOV(fp);
902 ntfs_ntput(ip);
903 return (0);
904 }
905
6ddb7618 906 error = getnewvnode(VT_NTFS, ntmp->ntm_mountp, &vp, VLKTIMEOUT, 0);
984263bc
MD
907 if(error) {
908 ntfs_frele(fp);
909 ntfs_ntput(ip);
910 return (error);
911 }
912 dprintf(("ntfs_vget: vnode: %p for ntnode: %d\n", vp,ino));
913
984263bc
MD
914 fp->f_vp = vp;
915 vp->v_data = fp;
916 vp->v_type = f_type;
917
918 if (ino == NTFS_ROOTINO)
919 vp->v_flag |= VROOT;
920
921 ntfs_ntput(ip);
922
5fd012e0
MD
923 KKASSERT(lkflags & LK_TYPE_MASK);
924 KKASSERT((lkflags & LK_INTERLOCK) == 0);
925 /* XXX leave vnode locked exclusively from getnewvnode */
984263bc
MD
926 *vpp = vp;
927 return (0);
928
929}
930
931static int
cd0a65a2 932ntfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
984263bc
MD
933{
934 return ntfs_vgetex(mp, ino, NTFS_A_DATA, NULL,
dadab5e9 935 LK_EXCLUSIVE | LK_RETRY, 0, curthread, vpp);
984263bc
MD
936}
937
08c23b99 938#if defined(__DragonFly__)
984263bc 939static struct vfsops ntfs_vfsops = {
43c45e8f
HP
940 .vfs_mount = ntfs_mount,
941 .vfs_unmount = ntfs_unmount,
942 .vfs_root = ntfs_root,
943 .vfs_statfs = ntfs_statfs,
944 .vfs_sync = vfs_stdsync,
945 .vfs_vget = ntfs_vget,
946 .vfs_fhtovp = ntfs_fhtovp,
947 .vfs_checkexp = ntfs_checkexp,
948 .vfs_vptofh = ntfs_vptofh,
949 .vfs_init = ntfs_init,
950 .vfs_uninit = ntfs_nthash_uninit /* see ntfs_ihash.c */
984263bc
MD
951};
952VFS_SET(ntfs_vfsops, ntfs, 0);
953#elif defined(__NetBSD__)
954extern struct vnodeopv_desc ntfs_vnodeop_opv_desc;
955
956struct vnodeopv_desc *ntfs_vnodeopv_descs[] = {
957 &ntfs_vnodeop_opv_desc,
958 NULL,
959};
960
961struct vfsops ntfs_vfsops = {
962 MOUNT_NTFS,
963 ntfs_mount,
964 ntfs_start,
965 ntfs_unmount,
966 ntfs_root,
967 ntfs_quotactl,
968 ntfs_statfs,
969 ntfs_sync,
970 ntfs_vget,
971 ntfs_fhtovp,
972 ntfs_vptofh,
973 ntfs_init,
974 ntfs_sysctl,
975 ntfs_mountroot,
976 ntfs_checkexp,
977 ntfs_vnodeopv_descs,
978};
979#else /* !NetBSD && !FreeBSD */
980static struct vfsops ntfs_vfsops = {
981 ntfs_mount,
982 ntfs_start,
983 ntfs_unmount,
984 ntfs_root,
985 ntfs_quotactl,
986 ntfs_statfs,
987 ntfs_sync,
988 ntfs_vget,
989 ntfs_fhtovp,
990 ntfs_vptofh,
991 ntfs_init,
992};
993VFS_SET(ntfs_vfsops, ntfs, MOUNT_NTFS, 0);
994#endif
995
996