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