The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/fs/ntfs/ntfs_vfsops.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    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: releng/8.0/sys/fs/ntfs/ntfs_vfsops.c 191990 2009-05-11 15:33:26Z attilio $
   29  */
   30 
   31 
   32 #include <sys/param.h>
   33 #include <sys/systm.h>
   34 #include <sys/namei.h>
   35 #include <sys/conf.h>
   36 #include <sys/proc.h>
   37 #include <sys/kernel.h>
   38 #include <sys/vnode.h>
   39 #include <sys/mount.h>
   40 #include <sys/bio.h>
   41 #include <sys/buf.h>
   42 #include <sys/fcntl.h>
   43 #include <sys/malloc.h>
   44 #include <sys/stat.h>
   45 #include <sys/systm.h>
   46 
   47 #include <geom/geom.h>
   48 #include <geom/geom_vfs.h>
   49 
   50 #include <vm/vm.h>
   51 #include <vm/vm_param.h>
   52 #include <vm/vm_page.h>
   53 #include <vm/vm_object.h>
   54 #include <vm/vm_extern.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 static 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 sockaddr;
   70 
   71 static int      ntfs_mountfs(register struct vnode *, struct mount *, 
   72                                   struct thread *);
   73 static int      ntfs_calccfree(struct ntfsmount *ntmp, cn_t *cfreep);
   74 
   75 static vfs_init_t       ntfs_init;
   76 static vfs_uninit_t     ntfs_uninit;
   77 static vfs_vget_t       ntfs_vget;
   78 static vfs_fhtovp_t     ntfs_fhtovp;
   79 static vfs_cmount_t     ntfs_cmount;
   80 static vfs_mount_t      ntfs_mount;
   81 static vfs_root_t       ntfs_root;
   82 static vfs_statfs_t     ntfs_statfs;
   83 static vfs_unmount_t    ntfs_unmount;
   84 
   85 static b_strategy_t     ntfs_bufstrategy;
   86 
   87 /* 
   88  * Buffer operations for NTFS vnodes.
   89  * We punt on VOP_BMAP, so we need to do
   90  * strategy on the file's vnode rather
   91  * than the underlying device's
   92  */
   93 static struct buf_ops ntfs_vnbufops = {
   94         .bop_name     = "NTFS",
   95         .bop_strategy = ntfs_bufstrategy,
   96 };
   97 
   98 static int
   99 ntfs_init (
  100         struct vfsconf *vcp )
  101 {
  102         ntfs_nthashinit();
  103         ntfs_toupper_init();
  104         return 0;
  105 }
  106 
  107 static int
  108 ntfs_uninit (
  109         struct vfsconf *vcp )
  110 {
  111         ntfs_toupper_destroy();
  112         ntfs_nthashdestroy();
  113         return 0;
  114 }
  115 
  116 static int
  117 ntfs_cmount ( 
  118         struct mntarg *ma,
  119         void *data,
  120         int flags)
  121 {
  122         int error;
  123         struct ntfs_args args;
  124 
  125         error = copyin(data, (caddr_t)&args, sizeof args);
  126         if (error)
  127                 return (error);
  128         ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
  129         ma = mount_arg(ma, "export", &args.export, sizeof args.export);
  130         ma = mount_argf(ma, "uid", "%d", args.uid);
  131         ma = mount_argf(ma, "gid", "%d", args.gid);
  132         ma = mount_argf(ma, "mode", "%d", args.mode);
  133         ma = mount_argb(ma, args.flag & NTFS_MFLAG_CASEINS, "nocaseins");
  134         ma = mount_argb(ma, args.flag & NTFS_MFLAG_ALLNAMES, "noallnames");
  135         if (args.flag & NTFS_MFLAG_KICONV) {
  136                 ma = mount_argsu(ma, "cs_ntfs", args.cs_ntfs, 64);
  137                 ma = mount_argsu(ma, "cs_local", args.cs_local, 64);
  138         }
  139 
  140         error = kernel_mount(ma, flags);
  141 
  142         return (error);
  143 }
  144 
  145 static const char *ntfs_opts[] = {
  146         "from", "export", "uid", "gid", "mode", "caseins", "allnames",
  147         "kiconv", "cs_ntfs", "cs_local", NULL
  148 };
  149 
  150 static int
  151 ntfs_mount (struct mount *mp)
  152 {
  153         int             err = 0, error;
  154         struct vnode    *devvp;
  155         struct nameidata ndp;
  156         char *from;
  157 
  158         if (vfs_filteropt(mp->mnt_optnew, ntfs_opts))
  159                 return (EINVAL);
  160 
  161         from = vfs_getopts(mp->mnt_optnew, "from", &error);
  162         if (error)      
  163                 return (error);
  164 
  165         /*
  166          * If updating, check whether changing from read-only to
  167          * read/write.
  168          */
  169         if (mp->mnt_flag & MNT_UPDATE) {
  170                 if (vfs_flagopt(mp->mnt_optnew, "export", NULL, 0)) {
  171                         /* Process export requests in vfs_mount.c */
  172                         goto success;
  173                 } else {
  174                         printf("ntfs_mount(): MNT_UPDATE not supported\n");
  175                         err = EINVAL;
  176                         goto error_1;
  177                 }
  178         }
  179 
  180         /*
  181          * Not an update, or updating the name: look up the name
  182          * and verify that it refers to a sensible block device.
  183          */
  184         NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, from, curthread);
  185         err = namei(&ndp);
  186         if (err) {
  187                 /* can't get devvp!*/
  188                 goto error_1;
  189         }
  190         NDFREE(&ndp, NDF_ONLY_PNBUF);
  191         devvp = ndp.ni_vp;
  192 
  193         if (!vn_isdisk(devvp, &err))  {
  194                 vput(devvp);
  195                 return (err);
  196         }
  197 
  198         if (mp->mnt_flag & MNT_UPDATE) {
  199 #if 0
  200                 /*
  201                  ********************
  202                  * UPDATE
  203                  ********************
  204                  */
  205 
  206                 if (devvp != ntmp->um_devvp)
  207                         err = EINVAL;   /* needs translation */
  208                 vput(devvp);
  209                 if (err)
  210                         return (err);
  211 #endif
  212         } else {
  213                 /*
  214                  ********************
  215                  * NEW MOUNT
  216                  ********************
  217                  */
  218 
  219                 /*
  220                  * Since this is a new mount, we want the names for
  221                  * the device and the mount point copied in.  If an
  222                  * error occurs, the mountpoint is discarded by the
  223                  * upper level code.  Note that vfs_mount() handles
  224                  * copying the mountpoint f_mntonname for us, so we
  225                  * don't have to do it here unless we want to set it
  226                  * to something other than "path" for some rason.
  227                  */
  228                 /* Save "mounted from" info for mount point (NULL pad)*/
  229                 vfs_mountedfrom(mp, from);
  230 
  231                 err = ntfs_mountfs(devvp, mp, curthread);
  232         }
  233         if (err) {
  234                 vrele(devvp);
  235                 return (err);
  236         }
  237 
  238         goto success;
  239 
  240 error_1:        /* no state to back out*/
  241         /* XXX: missing NDFREE(&ndp, ...) */
  242 
  243 success:
  244         return(err);
  245 }
  246 
  247 /*
  248  * Common code for mount and mountroot
  249  */
  250 int
  251 ntfs_mountfs(devvp, mp, td)
  252         register struct vnode *devvp;
  253         struct mount *mp;
  254         struct thread *td;
  255 {
  256         struct buf *bp;
  257         struct ntfsmount *ntmp;
  258         struct cdev *dev = devvp->v_rdev;
  259         int error, ronly, i, v;
  260         struct vnode *vp;
  261         struct g_consumer *cp;
  262         struct g_provider *pp;
  263         char *cs_ntfs, *cs_local;
  264 
  265         ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
  266         DROP_GIANT();
  267         g_topology_lock();
  268 
  269         /*
  270          * XXX: Do not allow more than one consumer to open a device
  271          *      associated with a particular GEOM provider.
  272          *      This disables multiple read-only mounts of a device,
  273          *      but it gets rid of panics in vget() when you try to
  274          *      mount the same device more than once.
  275          */
  276         pp = g_dev_getprovider(devvp->v_rdev);
  277         if ((pp != NULL) && ((pp->acr | pp->acw | pp->ace ) != 0)) 
  278                 error = EPERM;
  279         else 
  280                 error = g_vfs_open(devvp, &cp, "ntfs", ronly ? 0 : 1);
  281 
  282         g_topology_unlock();
  283         PICKUP_GIANT();
  284         VOP_UNLOCK(devvp, 0);
  285         if (error)
  286                 return (error);
  287 
  288         bp = NULL;
  289 
  290         error = bread(devvp, BBLOCK, BBSIZE, NOCRED, &bp);
  291         if (error)
  292                 goto out;
  293         ntmp = malloc( sizeof *ntmp, M_NTFSMNT, M_WAITOK | M_ZERO);
  294         bcopy( bp->b_data, &ntmp->ntm_bootfile, sizeof(struct bootfile) );
  295         /*
  296          * We must not cache the boot block if its size is not exactly
  297          * one cluster in order to avoid confusing the buffer cache when
  298          * the boot file is read later by ntfs_readntvattr_plain(), which
  299          * reads a cluster at a time.
  300          */
  301         if (ntfs_cntob(1) != BBSIZE)
  302                 bp->b_flags |= B_NOCACHE;
  303         brelse( bp );
  304         bp = NULL;
  305 
  306         if (strncmp((const char *)ntmp->ntm_bootfile.bf_sysid, NTFS_BBID, NTFS_BBIDLEN)) {
  307                 error = EINVAL;
  308                 dprintf(("ntfs_mountfs: invalid boot block\n"));
  309                 goto out;
  310         }
  311 
  312         {
  313                 int8_t cpr = ntmp->ntm_mftrecsz;
  314                 if( cpr > 0 )
  315                         ntmp->ntm_bpmftrec = ntmp->ntm_spc * cpr;
  316                 else
  317                         ntmp->ntm_bpmftrec = (1 << (-cpr)) / ntmp->ntm_bps;
  318         }
  319         dprintf(("ntfs_mountfs(): bps: %d, spc: %d, media: %x, mftrecsz: %d (%d sects)\n",
  320                 ntmp->ntm_bps,ntmp->ntm_spc,ntmp->ntm_bootfile.bf_media,
  321                 ntmp->ntm_mftrecsz,ntmp->ntm_bpmftrec));
  322         dprintf(("ntfs_mountfs(): mftcn: 0x%x|0x%x\n",
  323                 (u_int32_t)ntmp->ntm_mftcn,(u_int32_t)ntmp->ntm_mftmirrcn));
  324 
  325         ntmp->ntm_mountp = mp;
  326         ntmp->ntm_devvp = devvp;
  327         if (1 == vfs_scanopt(mp->mnt_optnew, "uid", "%d", &v))
  328                 ntmp->ntm_uid = v;
  329         if (1 == vfs_scanopt(mp->mnt_optnew, "gid", "%d", &v))
  330                 ntmp->ntm_gid = v;
  331         if (1 == vfs_scanopt(mp->mnt_optnew, "mode", "%d", &v))
  332                 ntmp->ntm_mode = v & ACCESSPERMS;
  333         vfs_flagopt(mp->mnt_optnew,
  334             "caseins", &ntmp->ntm_flag, NTFS_MFLAG_CASEINS);
  335         vfs_flagopt(mp->mnt_optnew,
  336             "allnames", &ntmp->ntm_flag, NTFS_MFLAG_ALLNAMES);
  337         ntmp->ntm_cp = cp;
  338         ntmp->ntm_bo = &devvp->v_bufobj;
  339 
  340         cs_local = vfs_getopts(mp->mnt_optnew, "cs_local", &error);
  341         if (error && error != ENOENT)
  342                 goto out;
  343         cs_ntfs = vfs_getopts(mp->mnt_optnew, "cs_ntfs", &error);
  344         if (error && error != ENOENT)
  345                 goto out;
  346         /* Copy in the 8-bit to Unicode conversion table */
  347         /* Initialize Unicode to 8-bit table from 8toU table */
  348         ntfs_82u_init(ntmp, cs_local, cs_ntfs);
  349         if (cs_local != NULL && cs_ntfs != NULL)
  350                 ntfs_u28_init(ntmp, NULL, cs_local, cs_ntfs);
  351         else
  352                 ntfs_u28_init(ntmp, ntmp->ntm_82u, cs_local, cs_ntfs);
  353 
  354         mp->mnt_data = ntmp;
  355 
  356         dprintf(("ntfs_mountfs(): case-%s,%s uid: %d, gid: %d, mode: %o\n",
  357                 (ntmp->ntm_flag & NTFS_MFLAG_CASEINS)?"insens.":"sens.",
  358                 (ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES)?" allnames,":"",
  359                 ntmp->ntm_uid, ntmp->ntm_gid, ntmp->ntm_mode));
  360 
  361         /*
  362          * We read in some system nodes to do not allow 
  363          * reclaim them and to have everytime access to them.
  364          */ 
  365         {
  366                 int pi[3] = { NTFS_MFTINO, NTFS_ROOTINO, NTFS_BITMAPINO };
  367                 for (i=0; i<3; i++) {
  368                         error = VFS_VGET(mp, pi[i], LK_EXCLUSIVE,
  369                                          &(ntmp->ntm_sysvn[pi[i]]));
  370                         if(error)
  371                                 goto out1;
  372                         ntmp->ntm_sysvn[pi[i]]->v_vflag |= VV_SYSTEM;
  373                         VREF(ntmp->ntm_sysvn[pi[i]]);
  374                         vput(ntmp->ntm_sysvn[pi[i]]);
  375                 }
  376         }
  377 
  378         /* read the Unicode lowercase --> uppercase translation table,
  379          * if necessary */
  380         if ((error = ntfs_toupper_use(mp, ntmp)))
  381                 goto out1;
  382 
  383         /*
  384          * Scan $BitMap and count free clusters
  385          */
  386         error = ntfs_calccfree(ntmp, &ntmp->ntm_cfree);
  387         if(error)
  388                 goto out1;
  389 
  390         /*
  391          * Read and translate to internal format attribute
  392          * definition file. 
  393          */
  394         {
  395                 int num,j;
  396                 struct attrdef ad;
  397 
  398                 /* Open $AttrDef */
  399                 error = VFS_VGET(mp, NTFS_ATTRDEFINO, LK_EXCLUSIVE, &vp );
  400                 if(error) 
  401                         goto out1;
  402 
  403                 /* Count valid entries */
  404                 for(num=0;;num++) {
  405                         error = ntfs_readattr(ntmp, VTONT(vp),
  406                                         NTFS_A_DATA, NULL,
  407                                         num * sizeof(ad), sizeof(ad),
  408                                         &ad, NULL);
  409                         if (error)
  410                                 goto out1;
  411                         if (ad.ad_name[0] == 0)
  412                                 break;
  413                 }
  414 
  415                 /* Alloc memory for attribute definitions */
  416                 ntmp->ntm_ad = malloc(num * sizeof(struct ntvattrdef),
  417                         M_NTFSMNT, M_WAITOK);
  418 
  419                 ntmp->ntm_adnum = num;
  420 
  421                 /* Read them and translate */
  422                 for(i=0;i<num;i++){
  423                         error = ntfs_readattr(ntmp, VTONT(vp),
  424                                         NTFS_A_DATA, NULL,
  425                                         i * sizeof(ad), sizeof(ad),
  426                                         &ad, NULL);
  427                         if (error)
  428                                 goto out1;
  429                         j = 0;
  430                         do {
  431                                 ntmp->ntm_ad[i].ad_name[j] = ad.ad_name[j];
  432                         } while(ad.ad_name[j++]);
  433                         ntmp->ntm_ad[i].ad_namelen = j - 1;
  434                         ntmp->ntm_ad[i].ad_type = ad.ad_type;
  435                 }
  436 
  437                 vput(vp);
  438         }
  439 
  440         mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
  441         mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
  442         mp->mnt_maxsymlinklen = 0;
  443         MNT_ILOCK(mp);
  444         mp->mnt_flag |= MNT_LOCAL;
  445         MNT_IUNLOCK(mp);
  446         return (0);
  447 
  448 out1:
  449         for(i=0;i<NTFS_SYSNODESNUM;i++)
  450                 if(ntmp->ntm_sysvn[i]) vrele(ntmp->ntm_sysvn[i]);
  451 
  452         if (vflush(mp, 0, 0, td))
  453                 dprintf(("ntfs_mountfs: vflush failed\n"));
  454 
  455 out:
  456         if (bp)
  457                 brelse(bp);
  458 
  459         DROP_GIANT();
  460         g_topology_lock();
  461         g_vfs_close(cp);
  462         g_topology_unlock();
  463         PICKUP_GIANT();
  464         
  465         return (error);
  466 }
  467 
  468 static int
  469 ntfs_unmount( 
  470         struct mount *mp,
  471         int mntflags)
  472 {
  473         struct thread *td;
  474         struct ntfsmount *ntmp;
  475         int error, flags, i;
  476 
  477         dprintf(("ntfs_unmount: unmounting...\n"));
  478         td = curthread;
  479         ntmp = VFSTONTFS(mp);
  480 
  481         flags = 0;
  482         if(mntflags & MNT_FORCE)
  483                 flags |= FORCECLOSE;
  484 
  485         dprintf(("ntfs_unmount: vflushing...\n"));
  486         error = vflush(mp, 0, flags | SKIPSYSTEM, td);
  487         if (error) {
  488                 printf("ntfs_unmount: vflush failed: %d\n",error);
  489                 return (error);
  490         }
  491 
  492         /* Check if only system vnodes are rest */
  493         for(i=0;i<NTFS_SYSNODESNUM;i++)
  494                  if((ntmp->ntm_sysvn[i]) && 
  495                     (vrefcnt(ntmp->ntm_sysvn[i]) > 1)) return (EBUSY);
  496 
  497         /* Dereference all system vnodes */
  498         for(i=0;i<NTFS_SYSNODESNUM;i++)
  499                  if(ntmp->ntm_sysvn[i]) vrele(ntmp->ntm_sysvn[i]);
  500 
  501         /* vflush system vnodes */
  502         error = vflush(mp, 0, flags, td);
  503         if (error)
  504                 printf("ntfs_unmount: vflush failed(sysnodes): %d\n",error);
  505 
  506         vinvalbuf(ntmp->ntm_devvp, V_SAVE, 0, 0);
  507 
  508         DROP_GIANT();
  509         g_topology_lock();
  510         g_vfs_close(ntmp->ntm_cp);
  511         g_topology_unlock();
  512         PICKUP_GIANT();
  513 
  514         vrele(ntmp->ntm_devvp);
  515 
  516         /* free the toupper table, if this has been last mounted ntfs volume */
  517         ntfs_toupper_unuse();
  518 
  519         dprintf(("ntfs_umount: freeing memory...\n"));
  520         ntfs_u28_uninit(ntmp);
  521         ntfs_82u_uninit(ntmp);
  522         mp->mnt_data = NULL;
  523         MNT_ILOCK(mp);
  524         mp->mnt_flag &= ~MNT_LOCAL;
  525         MNT_IUNLOCK(mp);
  526         free(ntmp->ntm_ad, M_NTFSMNT);
  527         free(ntmp, M_NTFSMNT);
  528         return (error);
  529 }
  530 
  531 static int
  532 ntfs_root(
  533         struct mount *mp,
  534         int flags,
  535         struct vnode **vpp)
  536 {
  537         struct vnode *nvp;
  538         int error = 0;
  539 
  540         dprintf(("ntfs_root(): sysvn: %p\n",
  541                 VFSTONTFS(mp)->ntm_sysvn[NTFS_ROOTINO]));
  542         error = VFS_VGET(mp, (ino_t)NTFS_ROOTINO, LK_EXCLUSIVE, &nvp);
  543         if(error) {
  544                 printf("ntfs_root: VFS_VGET failed: %d\n",error);
  545                 return (error);
  546         }
  547 
  548         *vpp = nvp;
  549         return (0);
  550 }
  551 
  552 static int
  553 ntfs_calccfree(
  554         struct ntfsmount *ntmp,
  555         cn_t *cfreep)
  556 {
  557         struct vnode *vp;
  558         u_int8_t *tmp;
  559         int j, error;
  560         long cfree = 0;
  561         size_t bmsize, i;
  562 
  563         vp = ntmp->ntm_sysvn[NTFS_BITMAPINO];
  564 
  565         bmsize = VTOF(vp)->f_size;
  566 
  567         tmp = malloc(bmsize, M_TEMP, M_WAITOK);
  568 
  569         error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
  570                                0, bmsize, tmp, NULL);
  571         if (error)
  572                 goto out;
  573 
  574         for(i=0;i<bmsize;i++)
  575                 for(j=0;j<8;j++)
  576                         if(~tmp[i] & (1 << j)) cfree++;
  577         *cfreep = cfree;
  578 
  579     out:
  580         free(tmp, M_TEMP);
  581         return(error);
  582 }
  583 
  584 static int
  585 ntfs_statfs(
  586         struct mount *mp,
  587         struct statfs *sbp)
  588 {
  589         struct ntfsmount *ntmp = VFSTONTFS(mp);
  590         u_int64_t mftsize,mftallocated;
  591 
  592         dprintf(("ntfs_statfs():\n"));
  593 
  594         mftsize = VTOF(ntmp->ntm_sysvn[NTFS_MFTINO])->f_size;
  595         mftallocated = VTOF(ntmp->ntm_sysvn[NTFS_MFTINO])->f_allocated;
  596 
  597         sbp->f_type = mp->mnt_vfc->vfc_typenum;
  598         sbp->f_bsize = ntmp->ntm_bps;
  599         sbp->f_iosize = ntmp->ntm_bps * ntmp->ntm_spc;
  600         sbp->f_blocks = ntmp->ntm_bootfile.bf_spv;
  601         sbp->f_bfree = sbp->f_bavail = ntfs_cntobn(ntmp->ntm_cfree);
  602         sbp->f_ffree = sbp->f_bfree / ntmp->ntm_bpmftrec;
  603         sbp->f_files = mftallocated / ntfs_bntob(ntmp->ntm_bpmftrec) +
  604                        sbp->f_ffree;
  605         sbp->f_flags = mp->mnt_flag;
  606 
  607         return (0);
  608 }
  609 
  610 /*ARGSUSED*/
  611 static int
  612 ntfs_fhtovp(
  613         struct mount *mp,
  614         struct fid *fhp,
  615         struct vnode **vpp)
  616 {
  617         struct vnode *nvp;
  618         struct ntfid *ntfhp = (struct ntfid *)fhp;
  619         int error;
  620 
  621         ddprintf(("ntfs_fhtovp(): %d\n", ntfhp->ntfid_ino));
  622 
  623         if ((error = VFS_VGET(mp, ntfhp->ntfid_ino, LK_EXCLUSIVE, &nvp)) != 0) {
  624                 *vpp = NULLVP;
  625                 return (error);
  626         }
  627         /* XXX as unlink/rmdir/mkdir/creat are not currently possible
  628          * with NTFS, we don't need to check anything else for now */
  629         *vpp = nvp;
  630         vnode_create_vobject(nvp, VTOF(nvp)->f_size, curthread);
  631         return (0);
  632 }
  633 
  634 int
  635 ntfs_vgetex(
  636         struct mount *mp,
  637         ino_t ino,
  638         u_int32_t attrtype,
  639         char *attrname,
  640         u_long lkflags,
  641         u_long flags,
  642         struct thread *td,
  643         struct vnode **vpp) 
  644 {
  645         int error;
  646         register struct ntfsmount *ntmp;
  647         struct ntnode *ip;
  648         struct fnode *fp;
  649         struct vnode *vp;
  650         enum vtype f_type;
  651 
  652         dprintf(("ntfs_vgetex: ino: %d, attr: 0x%x:%s, lkf: 0x%lx, f: 0x%lx\n",
  653                 ino, attrtype, attrname?attrname:"", (u_long)lkflags,
  654                 (u_long)flags));
  655 
  656         ntmp = VFSTONTFS(mp);
  657         *vpp = NULL;
  658 
  659         /* Get ntnode */
  660         error = ntfs_ntlookup(ntmp, ino, &ip);
  661         if (error) {
  662                 printf("ntfs_vget: ntfs_ntget failed\n");
  663                 return (error);
  664         }
  665 
  666         /* It may be not initialized fully, so force load it */
  667         if (!(flags & VG_DONTLOADIN) && !(ip->i_flag & IN_LOADED)) {
  668                 error = ntfs_loadntnode(ntmp, ip);
  669                 if(error) {
  670                         printf("ntfs_vget: CAN'T LOAD ATTRIBUTES FOR INO: %d\n",
  671                                ip->i_number);
  672                         ntfs_ntput(ip);
  673                         return (error);
  674                 }
  675         }
  676 
  677         error = ntfs_fget(ntmp, ip, attrtype, attrname, &fp);
  678         if (error) {
  679                 printf("ntfs_vget: ntfs_fget failed\n");
  680                 ntfs_ntput(ip);
  681                 return (error);
  682         }
  683 
  684         f_type = VNON;
  685         if (!(flags & VG_DONTVALIDFN) && !(fp->f_flag & FN_VALID)) {
  686                 if ((ip->i_frflag & NTFS_FRFLAG_DIR) &&
  687                     (fp->f_attrtype == NTFS_A_DATA && fp->f_attrname == NULL)) {
  688                         f_type = VDIR;
  689                 } else if (flags & VG_EXT) {
  690                         f_type = VNON;
  691                         fp->f_size = fp->f_allocated = 0;
  692                 } else {
  693                         f_type = VREG;  
  694 
  695                         error = ntfs_filesize(ntmp, fp, 
  696                                               &fp->f_size, &fp->f_allocated);
  697                         if (error) {
  698                                 ntfs_ntput(ip);
  699                                 return (error);
  700                         }
  701                 }
  702 
  703                 fp->f_flag |= FN_VALID;
  704         }
  705 
  706         if (FTOV(fp)) {
  707                 vget(FTOV(fp), lkflags, td);
  708                 *vpp = FTOV(fp);
  709                 ntfs_ntput(ip);
  710                 return (0);
  711         }
  712 
  713         error = getnewvnode("ntfs", ntmp->ntm_mountp, &ntfs_vnodeops, &vp);
  714         if(error) {
  715                 ntfs_frele(fp);
  716                 ntfs_ntput(ip);
  717                 return (error);
  718         }
  719         /* XXX: Too early for mpsafe fs, lacks vnode lock */
  720         error = insmntque(vp, ntmp->ntm_mountp);
  721         if (error) {
  722                 ntfs_frele(fp);
  723                 ntfs_ntput(ip);
  724                 return (error);
  725         }
  726         dprintf(("ntfs_vget: vnode: %p for ntnode: %d\n", vp,ino));
  727 
  728         fp->f_vp = vp;
  729         vp->v_data = fp;
  730         vp->v_type = f_type;
  731 
  732         vp->v_bufobj.bo_ops = &ntfs_vnbufops;
  733         vp->v_bufobj.bo_private = vp;
  734 
  735         if (ino == NTFS_ROOTINO)
  736                 vp->v_vflag |= VV_ROOT;
  737 
  738         ntfs_ntput(ip);
  739 
  740         if (lkflags & LK_TYPE_MASK) {
  741                 error = vn_lock(vp, lkflags);
  742                 if (error) {
  743                         vput(vp);
  744                         return (error);
  745                 }
  746         }
  747 
  748         *vpp = vp;
  749         return (0);
  750         
  751 }
  752 
  753 static int
  754 ntfs_vget(
  755         struct mount *mp,
  756         ino_t ino,
  757         int lkflags,
  758         struct vnode **vpp) 
  759 {
  760         return ntfs_vgetex(mp, ino, NTFS_A_DATA, NULL, lkflags, 0,
  761             curthread, vpp);
  762 }
  763 
  764 static void
  765 ntfs_bufstrategy(struct bufobj *bo, struct buf *bp)
  766 {
  767         struct vnode *vp;
  768         int rc;
  769 
  770         vp = bo->bo_private;
  771         KASSERT(bo == &vp->v_bufobj, ("BO/VP mismatch: vp %p bo %p != %p",
  772             vp, &vp->v_bufobj, bo));
  773         rc = VOP_STRATEGY(vp, bp);
  774         KASSERT(rc == 0, ("NTFS VOP_STRATEGY failed: bp=%p, "
  775                 "vp=%p, rc=%d", bp, vp, rc));
  776 }
  777 
  778 static struct vfsops ntfs_vfsops = {
  779         .vfs_fhtovp =   ntfs_fhtovp,
  780         .vfs_init =     ntfs_init,
  781         .vfs_cmount =   ntfs_cmount,
  782         .vfs_mount =    ntfs_mount,
  783         .vfs_root =     ntfs_root,
  784         .vfs_statfs =   ntfs_statfs,
  785         .vfs_uninit =   ntfs_uninit,
  786         .vfs_unmount =  ntfs_unmount,
  787         .vfs_vget =     ntfs_vget,
  788 };
  789 VFS_SET(ntfs_vfsops, ntfs, 0);
  790 MODULE_VERSION(ntfs, 1);

Cache object: d0e05b0f90d665250c95990920a9149c


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.