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