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