xref: /dragonfly/sys/vfs/ntfs/ntfs_vfsops.c (revision fc36a10bce8c5678d103e0498db849506d9dac68)
1 /*        $NetBSD: ntfs_vfsops.c,v 1.23 1999/11/15 19:38:14 jdolecek Exp $      */
2 
3 /*-
4  * Copyright (c) 1998, 1999 Semen Ustimenko
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD: src/sys/ntfs/ntfs_subr.c,v 1.7.2.4 2001/10/12 22:08:49 semenu Exp $
29  */
30 
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/conf.h>
35 #include <sys/proc.h>
36 #include <sys/nlookup.h>
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>
43 
44 #include <machine/inttypes.h>
45 
46 #include <vm/vm.h>
47 #include <vm/vm_param.h>
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 
53 #include <sys/buf2.h>
54 
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"
61 
62 extern struct vop_ops ntfs_vnode_vops;
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 struct iconv_functions *ntfs_iconv = NULL;
70 
71 static int          ntfs_root (struct mount *, struct vnode **);
72 static int          ntfs_statfs (struct mount *, struct statfs *,
73                                         struct ucred *cred);
74 static int          ntfs_statvfs (struct mount *, struct statvfs *,
75                                         struct ucred *);
76 static int          ntfs_unmount (struct mount *, int);
77 static int          ntfs_vget (struct mount *mp, struct vnode *dvp,
78                                         ino_t ino, struct vnode **vpp);
79 static int          ntfs_mountfs (struct vnode *, struct mount *,
80                                         struct ntfs_args *, struct ucred *);
81 static int          ntfs_vptofh (struct vnode *, struct fid *);
82 static int          ntfs_fhtovp (struct mount *, struct vnode *rootvp,
83                                         struct fid *, struct vnode **);
84 
85 struct sockaddr;
86 static int          ntfs_mount (struct mount *, char *, caddr_t, struct ucred *);
87 static int          ntfs_init (struct vfsconf *);
88 static int          ntfs_checkexp (struct mount *, struct sockaddr *,
89                                            int *, struct ucred **);
90 /*
91  * Verify a remote client has export rights and return these rights via.
92  * exflagsp and credanonp.
93  */
94 static int
ntfs_checkexp(struct mount * mp,struct sockaddr * nam,int * exflagsp,struct ucred ** credanonp)95 ntfs_checkexp(struct mount *mp,  struct sockaddr *nam,
96                 int *exflagsp, struct ucred **credanonp)
97 {
98           struct netcred *np;
99           struct ntfsmount *ntm = VFSTONTFS(mp);
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 
113 static int
ntfs_init(struct vfsconf * vcp)114 ntfs_init(struct vfsconf *vcp)
115 {
116           ntfs_nthashinit();
117           ntfs_toupper_init();
118           return 0;
119 }
120 
121 static int
ntfs_mount(struct mount * mp,char * path,caddr_t data,struct ucred * cred)122 ntfs_mount(struct mount *mp, char *path, caddr_t data, struct ucred *cred)
123 {
124           size_t              size;
125           int                 error;
126           struct vnode        *devvp;
127           struct ntfs_args args;
128           struct nlookupdata nd;
129           struct vnode *rootvp;
130 
131           error = 0;
132           /*
133            * Use NULL path to flag a root mount
134            */
135           if( path == NULL) {
136                     /*
137                      ***
138                      * Mounting root file system
139                      ***
140                      */
141 
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                      */
154                     if( ( error = ntfs_mountfs(rootvp, mp, &args, cred)) != 0) {
155                               /* fs specific cleanup (if any)*/
156                               goto error_1;
157                     }
158 
159                     goto dostatfs;                /* success*/
160 
161           }
162 
163           /*
164            ***
165            * Mounting non-root file system or updating a file system
166            ***
167            */
168 
169           /* copy in user arguments*/
170           error = copyin(data, (caddr_t)&args, sizeof (struct ntfs_args));
171           if (error)
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...*/
180                     if (args.fspec == NULL) {
181                               /*
182                                * Process export requests.  Jumping to "success"
183                                * will return the vfs_export() error code.
184                                */
185                               struct ntfsmount *ntm = VFSTONTFS(mp);
186                               error = vfs_export(mp, &ntm->ntm_export, &args.export);
187                               goto success;
188                     }
189 
190                     kprintf("ntfs_mount(): MNT_UPDATE not supported\n");
191                     error = EINVAL;
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            */
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)
204                     error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
205           nlookup_done(&nd);
206           if (error)
207                     goto error_1;
208 
209           if (!vn_isdisk(devvp, &error))
210                     goto error_2;
211 
212           if (mp->mnt_flag & MNT_UPDATE) {
213 #if 0
214                     /*
215                      ********************
216                      * UPDATE
217                      ********************
218                      */
219 
220                     if (devvp != ntmp->um_devvp)
221                               error = EINVAL;     /* needs translation */
222                     else
223                               vrele(devvp);
224                     /*
225                      * Update device name only on success
226                      */
227                     if( !error) {
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 
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 
250                     error = ntfs_mountfs(devvp, mp, &args, cred);
251           }
252           if (error) {
253                     goto error_2;
254           }
255 
256 dostatfs:
257           /*
258            * Initialize FS stat information in mount struct; uses
259            * mp->mnt_stat.f_mntfromname.
260            *
261            * This code is common to root and non-root mounts
262            */
263           (void)VFS_STATFS(mp, &mp->mnt_stat, cred);
264 
265           goto success;
266 
267 
268 error_2:  /* error with devvp held*/
269 
270           /* release devvp before failing*/
271           vrele(devvp);
272 
273 error_1:  /* no state to back out*/
274 
275 success:
276           return(error);
277 }
278 
279 /*
280  * Common code for mount and mountroot
281  */
282 int
ntfs_mountfs(struct vnode * devvp,struct mount * mp,struct ntfs_args * argsp,struct ucred * cred)283 ntfs_mountfs(struct vnode *devvp, struct mount *mp, struct ntfs_args *argsp,
284                struct ucred *cred)
285 {
286           struct buf *bp;
287           struct ntfsmount *ntmp;
288           cdev_t dev;
289           int error, ronly, ncount, i;
290           struct vnode *vp;
291           char cs_local[ICONV_CSNMAXLEN];
292           char cs_ntfs[ICONV_CSNMAXLEN];
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);
303           ncount = vcount(devvp);
304 
305           if (devvp->v_object)
306                     ncount -= 1;
307 
308           if (ncount > 1)
309                     return (EBUSY);
310 
311           VN_LOCK(devvp, LK_EXCLUSIVE | LK_RETRY);
312           error = vinvalbuf(devvp, V_SAVE, 0, 0);
313           VOP__UNLOCK(devvp, 0);
314           if (error)
315                     return (error);
316 
317           ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
318           VN_LOCK(devvp, LK_EXCLUSIVE | LK_RETRY);
319           error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, NULL);
320           VOP__UNLOCK(devvp, 0);
321           if (error)
322                     return (error);
323           dev = devvp->v_rdev;
324 
325           bp = NULL;
326 
327           error = bread(devvp, BBLOCK, BBSIZE, &bp);
328           if (error)
329                     goto out;
330           ntmp = kmalloc(sizeof *ntmp, M_NTFSMNT, M_WAITOK | M_ZERO);
331           bcopy( bp->b_data, &ntmp->ntm_bootfile, sizeof(struct bootfile) );
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;
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 
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);
375           } else {
376                     ntfs_82u_init(ntmp, NULL, NULL);
377                     ntfs_u28_init(ntmp, ntmp->ntm_82u, NULL, NULL);
378           }
379 
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 
387           vfs_add_vnodeops(mp, &ntfs_vnode_vops, &mp->mnt_vn_norm_ops);
388 
389           /*
390            * We read in some system nodes to do not allow
391            * reclaim them and to have everytime access to them.
392            */
393           {
394                     int pi[3] = { NTFS_MFTINO, NTFS_ROOTINO, NTFS_BITMAPINO };
395                     for (i=0; i<3; i++) {
396                               error = VFS_VGET(mp, NULL,
397                                                    pi[i], &(ntmp->ntm_sysvn[pi[i]]));
398                               if(error)
399                                         goto out1;
400                               vsetflags(ntmp->ntm_sysvn[pi[i]], VSYSTEM);
401                               vref(ntmp->ntm_sysvn[pi[i]]);
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
420            * definition file.
421            */
422           {
423                     int num,j;
424                     struct attrdef ad;
425 
426                     /* Open $AttrDef */
427                     error = VFS_VGET(mp, NULL, NTFS_ATTRDEFINO, &vp);
428                     if(error)
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 */
444                     ntmp->ntm_ad = kmalloc(num * sizeof(struct ntvattrdef),
445                                                M_NTFSMNT, M_WAITOK);
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 
468           mp->mnt_stat.f_fsid.val[0] = devid_from_dev(dev);
469           mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
470           mp->mnt_maxsymlinklen = 0;
471           mp->mnt_flag |= MNT_LOCAL;
472           dev->si_mountpoint = mp;
473 
474           return (0);
475 
476 out1:
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 
483 out:
484           dev->si_mountpoint = NULL;
485           if (bp)
486                     brelse(bp);
487 
488           vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
489           (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NULL);
490           vn_unlock(devvp);
491 
492           return (error);
493 }
494 
495 static int
ntfs_unmount(struct mount * mp,int mntflags)496 ntfs_unmount(struct mount *mp, int mntflags)
497 {
498           struct ntfsmount *ntmp;
499           int error, ronly, flags, i;
500 
501           dprintf(("ntfs_unmount: unmounting...\n"));
502           ntmp = VFSTONTFS(mp);
503 
504           ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
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) {
512                     kprintf("ntfs_unmount: vflush failed: %d\n",error);
513                     return (error);
514           }
515 
516           /* Check if only system vnodes are left */
517           for(i=0;i<NTFS_SYSNODESNUM;i++)
518                      if((ntmp->ntm_sysvn[i]) &&
519                         (VREFCNT(ntmp->ntm_sysvn[i]) > 1)) return (EBUSY);
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)
528                     kprintf("ntfs_unmount: vflush failed(sysnodes): %d\n",error);
529 
530           /* Check if the type of device node isn't VBAD before
531            * touching v_cdevinfo.  If the device vnode is revoked, the
532            * field is NULL and touching it causes null pointer derefercence.
533            */
534           if (ntmp->ntm_devvp->v_type != VBAD)
535                     ntmp->ntm_devvp->v_rdev->si_mountpoint = NULL;
536 
537           vn_lock(ntmp->ntm_devvp, LK_EXCLUSIVE | LK_RETRY);
538           vinvalbuf(ntmp->ntm_devvp, V_SAVE, 0, 0);
539           error = VOP_CLOSE(ntmp->ntm_devvp, ronly ? FREAD : FREAD|FWRITE, NULL);
540           vn_unlock(ntmp->ntm_devvp);
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;
552           kfree(ntmp->ntm_ad, M_NTFSMNT);
553           kfree(ntmp, M_NTFSMNT);
554           return (error);
555 }
556 
557 static int
ntfs_root(struct mount * mp,struct vnode ** vpp)558 ntfs_root(struct mount *mp, struct vnode **vpp)
559 {
560           struct vnode *nvp;
561           int error = 0;
562 
563           dprintf(("ntfs_root(): sysvn: %p\n",
564                     VFSTONTFS(mp)->ntm_sysvn[NTFS_ROOTINO]));
565           error = VFS_VGET(mp, NULL, (ino_t)NTFS_ROOTINO, &nvp);
566           if(error) {
567                     kprintf("ntfs_root: VFS_VGET failed: %d\n",error);
568                     return (error);
569           }
570 
571           *vpp = nvp;
572           return (0);
573 }
574 
575 int
ntfs_calccfree(struct ntfsmount * ntmp,cn_t * cfreep)576 ntfs_calccfree(struct ntfsmount *ntmp, cn_t *cfreep)
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 
588           tmp = kmalloc(bmsize, M_TEMP, M_WAITOK);
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:
601           kfree(tmp, M_TEMP);
602           return(error);
603 }
604 
605 static int
ntfs_statfs(struct mount * mp,struct statfs * sbp,struct ucred * cred)606 ntfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
607 {
608           struct ntfsmount *ntmp = VFSTONTFS(mp);
609           u_int64_t mftallocated;
610 
611           dprintf(("ntfs_statfs():\n"));
612 
613           mftallocated = VTOF(ntmp->ntm_sysvn[NTFS_MFTINO])->f_allocated;
614 
615           sbp->f_type = mp->mnt_vfc->vfc_typenum;
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) {
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;
628 
629           return (0);
630 }
631 
632 static int
ntfs_statvfs(struct mount * mp,struct statvfs * sbp,struct ucred * cred)633 ntfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
634 {
635           struct ntfsmount *ntmp = VFSTONTFS(mp);
636           u_int64_t mftallocated;
637 
638           dprintf(("ntfs_statvfs():\n"));
639 
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;
649 
650           return (0);
651 }
652 
653 /*ARGSUSED*/
654 static int
ntfs_fhtovp(struct mount * mp,struct vnode * rootvp,struct fid * fhp,struct vnode ** vpp)655 ntfs_fhtovp(struct mount *mp, struct vnode *rootvp,
656               struct fid *fhp, struct vnode **vpp)
657 {
658           struct vnode *nvp;
659           struct ntfid *ntfhp = (struct ntfid *)fhp;
660           int error;
661 
662           ddprintf(("ntfs_fhtovp(): %ju\n", ntfhp->ntfid_ino));
663 
664           if ((error = VFS_VGET(mp, NULL, ntfhp->ntfid_ino, &nvp)) != 0) {
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 
675 static int
ntfs_vptofh(struct vnode * vp,struct fid * fhp)676 ntfs_vptofh(struct vnode *vp, struct fid *fhp)
677 {
678           struct ntnode *ntp;
679           struct ntfid *ntfhp;
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 
691 int
ntfs_vgetex(struct mount * mp,ino_t ino,u_int32_t attrtype,char * attrname,u_long lkflags,u_long flags,struct thread * td,struct vnode ** vpp)692 ntfs_vgetex(struct mount *mp, ino_t ino, u_int32_t attrtype, char *attrname,
693               u_long lkflags, u_long flags, struct thread *td,
694               struct vnode **vpp)
695 {
696           int error;
697           struct ntfsmount *ntmp;
698           struct ntnode *ip;
699           struct fnode *fp;
700           struct vnode *vp;
701           enum vtype f_type;
702 
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));
705 
706           ntmp = VFSTONTFS(mp);
707           *vpp = NULL;
708 
709           /* Get ntnode */
710           error = ntfs_ntlookup(ntmp, ino, &ip);
711           if (error) {
712                     kprintf("ntfs_vget: ntfs_ntget failed\n");
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) {
720                               kprintf("ntfs_vget: CAN'T LOAD ATTRIBUTES FOR INO: %"PRId64"\n",
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) {
729                     kprintf("ntfs_vget: ntfs_fget failed\n");
730                     ntfs_ntput(ip);
731                     return (error);
732           }
733 
734           f_type = VINT;
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) {
740                               f_type = VINT;
741                               fp->f_size = fp->f_allocated = 0;
742                     } else {
743                               f_type = VREG;
744 
745                               error = ntfs_filesize(ntmp, fp,
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)) {
757                     VGET(FTOV(fp), lkflags);
758                     *vpp = FTOV(fp);
759                     ntfs_ntput(ip);
760                     return (0);
761           }
762 
763           error = getnewvnode(VT_NTFS, ntmp->ntm_mountp, &vp, VLKTIMEOUT, 0);
764           if (error) {
765                     ntfs_frele(fp);
766                     ntfs_ntput(ip);
767                     return (error);
768           }
769           dprintf(("ntfs_vget: vnode: %p for ntnode: %ju\n", vp, (uintmax_t)ino));
770 
771           fp->f_vp = vp;
772           vp->v_data = fp;
773           vp->v_type = f_type;
774 
775           if (ino == NTFS_ROOTINO)
776                     vsetflags(vp, VROOT);
777 
778           /*
779            * Normal files use the buffer cache
780            */
781           if (f_type == VREG)
782                     vinitvmio(vp, fp->f_size, PAGE_SIZE, -1);
783 
784           ntfs_ntput(ip);
785 
786           KKASSERT(lkflags & LK_TYPE_MASK);
787           /* XXX leave vnode locked exclusively from getnewvnode */
788           vx_downgrade(vp);
789           *vpp = vp;
790           return (0);
791 }
792 
793 static int
ntfs_vget(struct mount * mp,struct vnode * dvp,ino_t ino,struct vnode ** vpp)794 ntfs_vget(struct mount *mp, struct vnode *dvp, ino_t ino, struct vnode **vpp)
795 {
796           return ntfs_vgetex(mp, ino, NTFS_A_DATA, NULL,
797                               LK_EXCLUSIVE | LK_RETRY, 0, curthread, vpp);
798 }
799 
800 static struct vfsops ntfs_vfsops = {
801           .vfs_flags =                  0,
802           .vfs_mount =        ntfs_mount,
803           .vfs_unmount =      ntfs_unmount,
804           .vfs_root =         ntfs_root,
805           .vfs_statfs =       ntfs_statfs,
806           .vfs_statvfs =                ntfs_statvfs,
807           .vfs_vget =         ntfs_vget,
808           .vfs_fhtovp =       ntfs_fhtovp,
809           .vfs_checkexp =     ntfs_checkexp,
810           .vfs_vptofh =       ntfs_vptofh,
811           .vfs_init =         ntfs_init,
812           .vfs_uninit =       ntfs_nthash_uninit /* see ntfs_ihash.c */
813 };
814 VFS_SET(ntfs_vfsops, ntfs, 0);
815 MODULE_VERSION(ntfs, 1);
816