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