libc/nsdispatch: Use libc_dlopen().
[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 *
b07e7f2d 28 * $FreeBSD: src/sys/ntfs/ntfs_subr.c,v 1.7.2.4 2001/10/12 22:08:49 semenu Exp $
984263bc
MD
29 */
30
31
32#include <sys/param.h>
33#include <sys/systm.h>
984263bc
MD
34#include <sys/conf.h>
35#include <sys/proc.h>
fad57d0e 36#include <sys/nlookup.h>
984263bc
MD
37#include <sys/kernel.h>
38#include <sys/vnode.h>
39#include <sys/mount.h>
40#include <sys/buf.h>
41#include <sys/fcntl.h>
42#include <sys/malloc.h>
984263bc 43
f91a71dd
JS
44#include <machine/inttypes.h>
45
984263bc
MD
46#include <vm/vm.h>
47#include <vm/vm_param.h>
984263bc
MD
48#include <vm/vm_page.h>
49#include <vm/vm_object.h>
50#include <vm/vm_extern.h>
51#include <vm/vm_zone.h>
52
54341a3b
MD
53#include <sys/buf2.h>
54
1f2de5d4
MD
55#include "ntfs.h"
56#include "ntfs_inode.h"
57#include "ntfs_subr.h"
58#include "ntfs_vfsops.h"
59#include "ntfs_ihash.h"
60#include "ntfsmount.h"
984263bc 61
66a1ddf5 62extern struct vop_ops ntfs_vnode_vops;
0961aa92 63
984263bc
MD
64MALLOC_DEFINE(M_NTFSMNT, "NTFS mount", "NTFS mount structure");
65MALLOC_DEFINE(M_NTFSNTNODE,"NTFS ntnode", "NTFS ntnode information");
66MALLOC_DEFINE(M_NTFSFNODE,"NTFS fnode", "NTFS fnode information");
67MALLOC_DEFINE(M_NTFSDIR,"NTFS dir", "NTFS dir buffer");
984263bc 68
23731001
AP
69struct iconv_functions *ntfs_iconv = NULL;
70
a6ee311a 71static int ntfs_root (struct mount *, struct vnode **);
c5fbda8e
SW
72static int ntfs_statfs (struct mount *, struct statfs *,
73 struct ucred *cred);
74static int ntfs_statvfs (struct mount *, struct statvfs *,
75 struct ucred *);
acde96db 76static int ntfs_unmount (struct mount *, int);
b9b0a6d0
MD
77static int ntfs_vget (struct mount *mp, struct vnode *dvp,
78 ino_t ino, struct vnode **vpp);
a6ee311a 79static int ntfs_mountfs (struct vnode *, struct mount *,
b9b0a6d0 80 struct ntfs_args *, struct ucred *);
a6ee311a 81static int ntfs_vptofh (struct vnode *, struct fid *);
67863d04
MD
82static int ntfs_fhtovp (struct mount *, struct vnode *rootvp,
83 struct fid *, struct vnode **);
984263bc 84
984263bc 85struct sockaddr;
acde96db 86static int ntfs_mount (struct mount *, char *, caddr_t, struct ucred *);
a6ee311a
RG
87static int ntfs_init (struct vfsconf *);
88static int ntfs_checkexp (struct mount *, struct sockaddr *,
89 int *, struct ucred **);
984263bc
MD
90/*
91 * Verify a remote client has export rights and return these rights via.
92 * exflagsp and credanonp.
93 */
94static int
0249ae38 95ntfs_checkexp(struct mount *mp, struct sockaddr *nam,
cd0a65a2 96 int *exflagsp, struct ucred **credanonp)
984263bc 97{
34ee9825
RG
98 struct netcred *np;
99 struct ntfsmount *ntm = VFSTONTFS(mp);
984263bc
MD
100
101 /*
102 * Get the export permission structure for this <mp, client> tuple.
103 */
104 np = vfs_export_lookup(mp, &ntm->ntm_export, nam);
105 if (np == NULL)
106 return (EACCES);
107
108 *exflagsp = np->netc_exflags;
109 *credanonp = &np->netc_anon;
110 return (0);
111}
112
984263bc 113static int
cd0a65a2 114ntfs_init(struct vfsconf *vcp)
984263bc
MD
115{
116 ntfs_nthashinit();
117 ntfs_toupper_init();
118 return 0;
119}
120
984263bc 121static int
acde96db 122ntfs_mount(struct mount *mp, char *path, caddr_t data, struct ucred *cred)
cd0a65a2 123{
984263bc 124 size_t size;
fad57d0e 125 int error;
984263bc
MD
126 struct vnode *devvp;
127 struct ntfs_args args;
fad57d0e 128 struct nlookupdata nd;
21739618 129 struct vnode *rootvp;
984263bc 130
fad57d0e 131 error = 0;
984263bc
MD
132 /*
133 * Use NULL path to flag a root mount
134 */
135 if( path == NULL) {
136 /*
137 ***
138 * Mounting root file system
139 ***
140 */
0249ae38 141
984263bc
MD
142 /* Get vnode for root device*/
143 if( bdevvp( rootdev, &rootvp))
144 panic("ffs_mountroot: can't setup bdevvp for root");
145
146 /*
147 * FS specific handling
148 */
149 mp->mnt_flag |= MNT_RDONLY; /* XXX globally applicable?*/
150
151 /*
152 * Attempt mount
153 */
acde96db 154 if( ( error = ntfs_mountfs(rootvp, mp, &args, cred)) != 0) {
984263bc
MD
155 /* fs specific cleanup (if any)*/
156 goto error_1;
157 }
158
159 goto dostatfs; /* success*/
160
161 }
984263bc
MD
162
163 /*
164 ***
165 * Mounting non-root file system or updating a file system
166 ***
167 */
168
169 /* copy in user arguments*/
fad57d0e
MD
170 error = copyin(data, (caddr_t)&args, sizeof (struct ntfs_args));
171 if (error)
984263bc
MD
172 goto error_1; /* can't get arguments*/
173
174 /*
175 * If updating, check whether changing from read-only to
176 * read/write; if there is no device name, that's all we do.
177 */
178 if (mp->mnt_flag & MNT_UPDATE) {
179 /* if not updating name...*/
d8061892 180 if (args.fspec == NULL) {
984263bc
MD
181 /*
182 * Process export requests. Jumping to "success"
183 * will return the vfs_export() error code.
184 */
185 struct ntfsmount *ntm = VFSTONTFS(mp);
fad57d0e 186 error = vfs_export(mp, &ntm->ntm_export, &args.export);
984263bc
MD
187 goto success;
188 }
189
086c1d7e 190 kprintf("ntfs_mount(): MNT_UPDATE not supported\n");
fad57d0e 191 error = EINVAL;
984263bc
MD
192 goto error_1;
193 }
194
195 /*
196 * Not an update, or updating the name: look up the name
197 * and verify that it refers to a sensible block device.
198 */
fad57d0e
MD
199 devvp = NULL;
200 error = nlookup_init(&nd, args.fspec, UIO_USERSPACE, NLC_FOLLOW);
201 if (error == 0)
202 error = nlookup(&nd);
203 if (error == 0)
28623bf9 204 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
fad57d0e
MD
205 nlookup_done(&nd);
206 if (error)
984263bc 207 goto error_1;
984263bc 208
0249ae38 209 if (!vn_isdisk(devvp, &error))
984263bc 210 goto error_2;
0249ae38 211
984263bc
MD
212 if (mp->mnt_flag & MNT_UPDATE) {
213#if 0
214 /*
215 ********************
216 * UPDATE
217 ********************
218 */
219
220 if (devvp != ntmp->um_devvp)
fad57d0e 221 error = EINVAL; /* needs translation */
984263bc
MD
222 else
223 vrele(devvp);
224 /*
225 * Update device name only on success
226 */
fad57d0e 227 if( !error) {
984263bc
MD
228 /* Save "mounted from" info for mount point (NULL pad)*/
229 copyinstr( args.fspec,
230 mp->mnt_stat.f_mntfromname,
231 MNAMELEN - 1,
232 &size);
233 bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
234 }
235#endif
236 } else {
237 /*
238 ********************
239 * NEW MOUNT
240 ********************
241 */
242
984263bc
MD
243 /* Save "mounted from" info for mount point (NULL pad)*/
244 copyinstr( args.fspec, /* device name*/
245 mp->mnt_stat.f_mntfromname, /* save area*/
246 MNAMELEN - 1, /* max size*/
247 &size); /* real size*/
248 bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
249
acde96db 250 error = ntfs_mountfs(devvp, mp, &args, cred);
984263bc 251 }
fad57d0e 252 if (error) {
984263bc
MD
253 goto error_2;
254 }
255
984263bc 256dostatfs:
984263bc 257 /*
75ffff0d
JS
258 * Initialize FS stat information in mount struct; uses
259 * mp->mnt_stat.f_mntfromname.
984263bc
MD
260 *
261 * This code is common to root and non-root mounts
262 */
acde96db 263 (void)VFS_STATFS(mp, &mp->mnt_stat, cred);
984263bc
MD
264
265 goto success;
266
267
268error_2: /* error with devvp held*/
269
270 /* release devvp before failing*/
271 vrele(devvp);
272
273error_1: /* no state to back out*/
274
275success:
fad57d0e 276 return(error);
984263bc
MD
277}
278
279/*
280 * Common code for mount and mountroot
281 */
282int
cd0a65a2 283ntfs_mountfs(struct vnode *devvp, struct mount *mp, struct ntfs_args *argsp,
acde96db 284 struct ucred *cred)
984263bc
MD
285{
286 struct buf *bp;
287 struct ntfsmount *ntmp;
b13267a5 288 cdev_t dev;
984263bc
MD
289 int error, ronly, ncount, i;
290 struct vnode *vp;
23731001
AP
291 char cs_local[ICONV_CSNMAXLEN];
292 char cs_ntfs[ICONV_CSNMAXLEN];
984263bc
MD
293
294 /*
295 * Disallow multiple mounts of the same device.
296 * Disallow mounting of a device that is currently in use
297 * (except for root, which might share swap device for miniroot).
298 * Flush out any old buffers remaining from a previous use.
299 */
300 error = vfs_mountedon(devvp);
301 if (error)
302 return (error);
8be7edad 303 ncount = vcount(devvp);
0249ae38 304
984263bc
MD
305 if (devvp->v_object)
306 ncount -= 1;
0249ae38 307
21739618 308 if (ncount > 1)
984263bc 309 return (EBUSY);
0249ae38 310
acde96db 311 VN_LOCK(devvp, LK_EXCLUSIVE | LK_RETRY);
87de5057 312 error = vinvalbuf(devvp, V_SAVE, 0, 0);
acde96db 313 VOP__UNLOCK(devvp, 0);
984263bc
MD
314 if (error)
315 return (error);
316
317 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
acde96db 318 VN_LOCK(devvp, LK_EXCLUSIVE | LK_RETRY);
87de5057 319 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, NULL);
acde96db 320 VOP__UNLOCK(devvp, 0);
984263bc
MD
321 if (error)
322 return (error);
e4c9c0c8 323 dev = devvp->v_rdev;
984263bc
MD
324
325 bp = NULL;
326
fa69abee 327 error = bread(devvp, BBLOCK, BBSIZE, &bp);
984263bc
MD
328 if (error)
329 goto out;
e7b4468c 330 ntmp = kmalloc(sizeof *ntmp, M_NTFSMNT, M_WAITOK | M_ZERO);
984263bc 331 bcopy( bp->b_data, &ntmp->ntm_bootfile, sizeof(struct bootfile) );
b07e7f2d
AHJ
332 /*
333 * We must not cache the boot block if its size is not exactly
334 * one cluster in order to avoid confusing the buffer cache when
335 * the boot file is read later by ntfs_readntvattr_plain(), which
336 * reads a cluster at a time.
337 */
338 if (ntfs_cntob(1) != BBSIZE)
339 bp->b_flags |= B_NOCACHE;
984263bc
MD
340 brelse( bp );
341 bp = NULL;
342
343 if (strncmp(ntmp->ntm_bootfile.bf_sysid, NTFS_BBID, NTFS_BBIDLEN)) {
344 error = EINVAL;
345 dprintf(("ntfs_mountfs: invalid boot block\n"));
346 goto out;
347 }
348
349 {
350 int8_t cpr = ntmp->ntm_mftrecsz;
351 if( cpr > 0 )
352 ntmp->ntm_bpmftrec = ntmp->ntm_spc * cpr;
353 else
354 ntmp->ntm_bpmftrec = (1 << (-cpr)) / ntmp->ntm_bps;
355 }
356 dprintf(("ntfs_mountfs(): bps: %d, spc: %d, media: %x, mftrecsz: %d (%d sects)\n",
357 ntmp->ntm_bps,ntmp->ntm_spc,ntmp->ntm_bootfile.bf_media,
358 ntmp->ntm_mftrecsz,ntmp->ntm_bpmftrec));
359 dprintf(("ntfs_mountfs(): mftcn: 0x%x|0x%x\n",
360 (u_int32_t)ntmp->ntm_mftcn,(u_int32_t)ntmp->ntm_mftmirrcn));
361
362 ntmp->ntm_mountp = mp;
363 ntmp->ntm_dev = dev;
364 ntmp->ntm_devvp = devvp;
365 ntmp->ntm_uid = argsp->uid;
366 ntmp->ntm_gid = argsp->gid;
367 ntmp->ntm_mode = argsp->mode;
368 ntmp->ntm_flag = argsp->flag;
369
23731001
AP
370 if (argsp->flag & NTFS_MFLAG_KICONV && ntfs_iconv) {
371 bcopy(argsp->cs_local, cs_local, sizeof(cs_local));
372 bcopy(argsp->cs_ntfs, cs_ntfs, sizeof(cs_ntfs));
373 ntfs_82u_init(ntmp, cs_local, cs_ntfs);
374 ntfs_u28_init(ntmp, NULL, cs_local, cs_ntfs);
984263bc 375 } else {
23731001
AP
376 ntfs_82u_init(ntmp, NULL, NULL);
377 ntfs_u28_init(ntmp, ntmp->ntm_82u, NULL, NULL);
984263bc
MD
378 }
379
984263bc
MD
380 mp->mnt_data = (qaddr_t)ntmp;
381
382 dprintf(("ntfs_mountfs(): case-%s,%s uid: %d, gid: %d, mode: %o\n",
383 (ntmp->ntm_flag & NTFS_MFLAG_CASEINS)?"insens.":"sens.",
384 (ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES)?" allnames,":"",
385 ntmp->ntm_uid, ntmp->ntm_gid, ntmp->ntm_mode));
386
66a1ddf5 387 vfs_add_vnodeops(mp, &ntfs_vnode_vops, &mp->mnt_vn_norm_ops);
320bc044 388
984263bc 389 /*
0249ae38 390 * We read in some system nodes to do not allow
984263bc 391 * reclaim them and to have everytime access to them.
0249ae38 392 */
984263bc
MD
393 {
394 int pi[3] = { NTFS_MFTINO, NTFS_ROOTINO, NTFS_BITMAPINO };
395 for (i=0; i<3; i++) {
b9b0a6d0
MD
396 error = VFS_VGET(mp, NULL,
397 pi[i], &(ntmp->ntm_sysvn[pi[i]]));
984263bc
MD
398 if(error)
399 goto out1;
2247fe02 400 vsetflags(ntmp->ntm_sysvn[pi[i]], VSYSTEM);
597aea93 401 vref(ntmp->ntm_sysvn[pi[i]]);
984263bc
MD
402 vput(ntmp->ntm_sysvn[pi[i]]);
403 }
404 }
405
406 /* read the Unicode lowercase --> uppercase translation table,
407 * if necessary */
408 if ((error = ntfs_toupper_use(mp, ntmp)))
409 goto out1;
410
411 /*
412 * Scan $BitMap and count free clusters
413 */
414 error = ntfs_calccfree(ntmp, &ntmp->ntm_cfree);
415 if(error)
416 goto out1;
417
418 /*
419 * Read and translate to internal format attribute
0249ae38 420 * definition file.
984263bc
MD
421 */
422 {
423 int num,j;
424 struct attrdef ad;
425
426 /* Open $AttrDef */
b9b0a6d0 427 error = VFS_VGET(mp, NULL, NTFS_ATTRDEFINO, &vp);
0249ae38 428 if(error)
984263bc
MD
429 goto out1;
430
431 /* Count valid entries */
432 for(num=0;;num++) {
433 error = ntfs_readattr(ntmp, VTONT(vp),
434 NTFS_A_DATA, NULL,
435 num * sizeof(ad), sizeof(ad),
436 &ad, NULL);
437 if (error)
438 goto out1;
439 if (ad.ad_name[0] == 0)
440 break;
441 }
442
443 /* Alloc memory for attribute definitions */
884717e1
SW
444 ntmp->ntm_ad = kmalloc(num * sizeof(struct ntvattrdef),
445 M_NTFSMNT, M_WAITOK);
984263bc
MD
446
447 ntmp->ntm_adnum = num;
448
449 /* Read them and translate */
450 for(i=0;i<num;i++){
451 error = ntfs_readattr(ntmp, VTONT(vp),
452 NTFS_A_DATA, NULL,
453 i * sizeof(ad), sizeof(ad),
454 &ad, NULL);
455 if (error)
456 goto out1;
457 j = 0;
458 do {
459 ntmp->ntm_ad[i].ad_name[j] = ad.ad_name[j];
460 } while(ad.ad_name[j++]);
461 ntmp->ntm_ad[i].ad_namelen = j - 1;
462 ntmp->ntm_ad[i].ad_type = ad.ad_type;
463 }
464
465 vput(vp);
466 }
467
984263bc
MD
468 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
469 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
984263bc
MD
470 mp->mnt_maxsymlinklen = 0;
471 mp->mnt_flag |= MNT_LOCAL;
e4c9c0c8 472 dev->si_mountpoint = mp;
0961aa92 473
984263bc
MD
474 return (0);
475
476out1:
477 for(i=0;i<NTFS_SYSNODESNUM;i++)
478 if(ntmp->ntm_sysvn[i]) vrele(ntmp->ntm_sysvn[i]);
479
480 if (vflush(mp, 0, 0))
481 dprintf(("ntfs_mountfs: vflush failed\n"));
482
483out:
e4c9c0c8 484 dev->si_mountpoint = NULL;
984263bc
MD
485 if (bp)
486 brelse(bp);
487
12cdc371 488 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
3596743e 489 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NULL);
12cdc371 490 vn_unlock(devvp);
984263bc 491
0249ae38 492 return (error);
984263bc 493}
984263bc
MD
494
495static int
acde96db 496ntfs_unmount(struct mount *mp, int mntflags)
984263bc 497{
34ee9825 498 struct ntfsmount *ntmp;
fb8c9c01 499 int error, ronly, flags, i;
984263bc
MD
500
501 dprintf(("ntfs_unmount: unmounting...\n"));
502 ntmp = VFSTONTFS(mp);
503
fb8c9c01 504 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
984263bc
MD
505 flags = 0;
506 if(mntflags & MNT_FORCE)
507 flags |= FORCECLOSE;
508
509 dprintf(("ntfs_unmount: vflushing...\n"));
510 error = vflush(mp, 0, flags | SKIPSYSTEM);
511 if (error) {
086c1d7e 512 kprintf("ntfs_unmount: vflush failed: %d\n",error);
984263bc
MD
513 return (error);
514 }
515
d8c0625a 516 /* Check if only system vnodes are left */
984263bc 517 for(i=0;i<NTFS_SYSNODESNUM;i++)
0249ae38 518 if((ntmp->ntm_sysvn[i]) &&
ee173d09 519 (VREFCNT(ntmp->ntm_sysvn[i]) > 1)) return (EBUSY);
984263bc
MD
520
521 /* Dereference all system vnodes */
522 for(i=0;i<NTFS_SYSNODESNUM;i++)
523 if(ntmp->ntm_sysvn[i]) vrele(ntmp->ntm_sysvn[i]);
524
525 /* vflush system vnodes */
526 error = vflush(mp, 0, flags);
527 if (error)
086c1d7e 528 kprintf("ntfs_unmount: vflush failed(sysnodes): %d\n",error);
984263bc
MD
529
530 /* Check if the type of device node isn't VBAD before
0de08e6d 531 * touching v_cdevinfo. If the device vnode is revoked, the
984263bc
MD
532 * field is NULL and touching it causes null pointer derefercence.
533 */
534 if (ntmp->ntm_devvp->v_type != VBAD)
e4c9c0c8 535 ntmp->ntm_devvp->v_rdev->si_mountpoint = NULL;
984263bc 536
12cdc371 537 vn_lock(ntmp->ntm_devvp, LK_EXCLUSIVE | LK_RETRY);
87de5057 538 vinvalbuf(ntmp->ntm_devvp, V_SAVE, 0, 0);
3596743e 539 error = VOP_CLOSE(ntmp->ntm_devvp, ronly ? FREAD : FREAD|FWRITE, NULL);
12cdc371 540 vn_unlock(ntmp->ntm_devvp);
984263bc
MD
541
542 vrele(ntmp->ntm_devvp);
543
544 /* free the toupper table, if this has been last mounted ntfs volume */
545 ntfs_toupper_unuse();
546
547 dprintf(("ntfs_umount: freeing memory...\n"));
548 ntfs_u28_uninit(ntmp);
549 ntfs_82u_uninit(ntmp);
550 mp->mnt_data = (qaddr_t)0;
551 mp->mnt_flag &= ~MNT_LOCAL;
884717e1
SW
552 kfree(ntmp->ntm_ad, M_NTFSMNT);
553 kfree(ntmp, M_NTFSMNT);
984263bc
MD
554 return (error);
555}
556
557static int
cd0a65a2 558ntfs_root(struct mount *mp, struct vnode **vpp)
984263bc
MD
559{
560 struct vnode *nvp;
561 int error = 0;
562
563 dprintf(("ntfs_root(): sysvn: %p\n",
564 VFSTONTFS(mp)->ntm_sysvn[NTFS_ROOTINO]));
b9b0a6d0 565 error = VFS_VGET(mp, NULL, (ino_t)NTFS_ROOTINO, &nvp);
984263bc 566 if(error) {
086c1d7e 567 kprintf("ntfs_root: VFS_VGET failed: %d\n",error);
984263bc
MD
568 return (error);
569 }
570
571 *vpp = nvp;
572 return (0);
573}
574
984263bc 575int
cd0a65a2 576ntfs_calccfree(struct ntfsmount *ntmp, cn_t *cfreep)
984263bc
MD
577{
578 struct vnode *vp;
579 u_int8_t *tmp;
580 int j, error;
581 long cfree = 0;
582 size_t bmsize, i;
583
584 vp = ntmp->ntm_sysvn[NTFS_BITMAPINO];
585
586 bmsize = VTOF(vp)->f_size;
587
884717e1 588 tmp = kmalloc(bmsize, M_TEMP, M_WAITOK);
984263bc
MD
589
590 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
591 0, bmsize, tmp, NULL);
592 if (error)
593 goto out;
594
595 for(i=0;i<bmsize;i++)
596 for(j=0;j<8;j++)
597 if(~tmp[i] & (1 << j)) cfree++;
598 *cfreep = cfree;
599
600 out:
884717e1 601 kfree(tmp, M_TEMP);
984263bc
MD
602 return(error);
603}
604
605static int
acde96db 606ntfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
984263bc
MD
607{
608 struct ntfsmount *ntmp = VFSTONTFS(mp);
dfb8a852 609 u_int64_t mftallocated;
984263bc
MD
610
611 dprintf(("ntfs_statfs():\n"));
612
984263bc
MD
613 mftallocated = VTOF(ntmp->ntm_sysvn[NTFS_MFTINO])->f_allocated;
614
984263bc 615 sbp->f_type = mp->mnt_vfc->vfc_typenum;
984263bc
MD
616 sbp->f_bsize = ntmp->ntm_bps;
617 sbp->f_iosize = ntmp->ntm_bps * ntmp->ntm_spc;
618 sbp->f_blocks = ntmp->ntm_bootfile.bf_spv;
619 sbp->f_bfree = sbp->f_bavail = ntfs_cntobn(ntmp->ntm_cfree);
620 sbp->f_ffree = sbp->f_bfree / ntmp->ntm_bpmftrec;
621 sbp->f_files = mftallocated / ntfs_bntob(ntmp->ntm_bpmftrec) +
622 sbp->f_ffree;
623 if (sbp != &mp->mnt_stat) {
984263bc
MD
624 bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
625 (caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
626 }
627 sbp->f_flags = mp->mnt_flag;
0249ae38 628
984263bc
MD
629 return (0);
630}
631
c5fbda8e
SW
632static int
633ntfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
634{
635 struct ntfsmount *ntmp = VFSTONTFS(mp);
dfb8a852 636 u_int64_t mftallocated;
c5fbda8e
SW
637
638 dprintf(("ntfs_statvfs():\n"));
639
c5fbda8e
SW
640 mftallocated = VTOF(ntmp->ntm_sysvn[NTFS_MFTINO])->f_allocated;
641
642 sbp->f_type = mp->mnt_vfc->vfc_typenum;
643 sbp->f_bsize = ntmp->ntm_bps;
644 sbp->f_blocks = ntmp->ntm_bootfile.bf_spv;
645 sbp->f_bfree = sbp->f_bavail = ntmp->ntm_cfree * ntmp->ntm_spc;
646 sbp->f_ffree = sbp->f_bfree / ntmp->ntm_bpmftrec;
647 sbp->f_files = mftallocated / (ntmp->ntm_bpmftrec * ntmp->ntm_bps) +
648 sbp->f_ffree;
c5fbda8e 649
984263bc
MD
650 return (0);
651}
984263bc
MD
652
653/*ARGSUSED*/
654static int
67863d04
MD
655ntfs_fhtovp(struct mount *mp, struct vnode *rootvp,
656 struct fid *fhp, struct vnode **vpp)
984263bc
MD
657{
658 struct vnode *nvp;
659 struct ntfid *ntfhp = (struct ntfid *)fhp;
660 int error;
661
39285282 662 ddprintf(("ntfs_fhtovp(): %ju\n", ntfhp->ntfid_ino));
984263bc 663
b9b0a6d0 664 if ((error = VFS_VGET(mp, NULL, ntfhp->ntfid_ino, &nvp)) != 0) {
984263bc
MD
665 *vpp = NULLVP;
666 return (error);
667 }
668 /* XXX as unlink/rmdir/mkdir/creat are not currently possible
669 * with NTFS, we don't need to check anything else for now */
670 *vpp = nvp;
671
672 return (0);
673}
674
675static int
cd0a65a2 676ntfs_vptofh(struct vnode *vp, struct fid *fhp)
984263bc 677{
34ee9825
RG
678 struct ntnode *ntp;
679 struct ntfid *ntfhp;
984263bc
MD
680
681 ddprintf(("ntfs_fhtovp(): %p\n", vp));
682
683 ntp = VTONT(vp);
684 ntfhp = (struct ntfid *)fhp;
685 ntfhp->ntfid_len = sizeof(struct ntfid);
686 ntfhp->ntfid_ino = ntp->i_number;
687 /* ntfhp->ntfid_gen = ntp->i_gen; */
688 return (0);
689}
690
691int
cd0a65a2
CP
692ntfs_vgetex(struct mount *mp, ino_t ino, u_int32_t attrtype, char *attrname,
693 u_long lkflags, u_long flags, struct thread *td,
0249ae38 694 struct vnode **vpp)
984263bc
MD
695{
696 int error;
34ee9825 697 struct ntfsmount *ntmp;
984263bc
MD
698 struct ntnode *ip;
699 struct fnode *fp;
700 struct vnode *vp;
701 enum vtype f_type;
702
f3696fdb
AHJ
703 dprintf(("ntfs_vgetex: ino: %ju, attr: 0x%x:%s, lkf: 0x%lx, f: 0x%lx\n",
704 (uintmax_t) ino, attrtype, attrname?attrname:"", lkflags, flags));
984263bc
MD
705
706 ntmp = VFSTONTFS(mp);
707 *vpp = NULL;
708
709 /* Get ntnode */
710 error = ntfs_ntlookup(ntmp, ino, &ip);
711 if (error) {
086c1d7e 712 kprintf("ntfs_vget: ntfs_ntget failed\n");
984263bc
MD
713 return (error);
714 }
715
716 /* It may be not initialized fully, so force load it */
717 if (!(flags & VG_DONTLOADIN) && !(ip->i_flag & IN_LOADED)) {
718 error = ntfs_loadntnode(ntmp, ip);
719 if(error) {
086c1d7e 720 kprintf("ntfs_vget: CAN'T LOAD ATTRIBUTES FOR INO: %"PRId64"\n",
984263bc
MD
721 ip->i_number);
722 ntfs_ntput(ip);
723 return (error);
724 }
725 }
726
727 error = ntfs_fget(ntmp, ip, attrtype, attrname, &fp);
728 if (error) {
086c1d7e 729 kprintf("ntfs_vget: ntfs_fget failed\n");
984263bc
MD
730 ntfs_ntput(ip);
731 return (error);
732 }
733
d8c0625a 734 f_type = VINT;
984263bc
MD
735 if (!(flags & VG_DONTVALIDFN) && !(fp->f_flag & FN_VALID)) {
736 if ((ip->i_frflag & NTFS_FRFLAG_DIR) &&
737 (fp->f_attrtype == NTFS_A_DATA && fp->f_attrname == NULL)) {
738 f_type = VDIR;
739 } else if (flags & VG_EXT) {
d8c0625a 740 f_type = VINT;
984263bc
MD
741 fp->f_size = fp->f_allocated = 0;
742 } else {
0249ae38 743 f_type = VREG;
984263bc 744
0249ae38 745 error = ntfs_filesize(ntmp, fp,
984263bc
MD
746 &fp->f_size, &fp->f_allocated);
747 if (error) {
748 ntfs_ntput(ip);
749 return (error);
750 }
751 }
752
753 fp->f_flag |= FN_VALID;
754 }
755
756 if (FTOV(fp)) {
acde96db 757 VGET(FTOV(fp), lkflags);
984263bc
MD
758 *vpp = FTOV(fp);
759 ntfs_ntput(ip);
760 return (0);
761 }
762
6ddb7618 763 error = getnewvnode(VT_NTFS, ntmp->ntm_mountp, &vp, VLKTIMEOUT, 0);
984263bc
MD
764 if(error) {
765 ntfs_frele(fp);
766 ntfs_ntput(ip);
767 return (error);
768 }
f3696fdb 769 dprintf(("ntfs_vget: vnode: %p for ntnode: %ju\n", vp, (uintmax_t)ino));
984263bc 770
984263bc
MD
771 fp->f_vp = vp;
772 vp->v_data = fp;
773 vp->v_type = f_type;
774
775 if (ino == NTFS_ROOTINO)
2247fe02 776 vsetflags(vp, VROOT);
984263bc 777
30365587
MD
778 /*
779 * Normal files use the buffer cache
780 */
781 if (f_type == VREG)
b0d18f7d 782 vinitvmio(vp, fp->f_size, PAGE_SIZE, -1);
30365587 783
984263bc
MD
784 ntfs_ntput(ip);
785
5fd012e0 786 KKASSERT(lkflags & LK_TYPE_MASK);
5fd012e0 787 /* XXX leave vnode locked exclusively from getnewvnode */
984263bc
MD
788 *vpp = vp;
789 return (0);
984263bc
MD
790}
791
792static int
b9b0a6d0 793ntfs_vget(struct mount *mp, struct vnode *dvp, ino_t ino, struct vnode **vpp)
984263bc
MD
794{
795 return ntfs_vgetex(mp, ino, NTFS_A_DATA, NULL,
dadab5e9 796 LK_EXCLUSIVE | LK_RETRY, 0, curthread, vpp);
984263bc
MD
797}
798
984263bc 799static struct vfsops ntfs_vfsops = {
43c45e8f
HP
800 .vfs_mount = ntfs_mount,
801 .vfs_unmount = ntfs_unmount,
802 .vfs_root = ntfs_root,
803 .vfs_statfs = ntfs_statfs,
c5fbda8e 804 .vfs_statvfs = ntfs_statvfs,
43c45e8f
HP
805 .vfs_vget = ntfs_vget,
806 .vfs_fhtovp = ntfs_fhtovp,
807 .vfs_checkexp = ntfs_checkexp,
808 .vfs_vptofh = ntfs_vptofh,
809 .vfs_init = ntfs_init,
810 .vfs_uninit = ntfs_nthash_uninit /* see ntfs_ihash.c */
984263bc
MD
811};
812VFS_SET(ntfs_vfsops, ntfs, 0);
23731001 813MODULE_VERSION(ntfs, 1);