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