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