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