darwin-xnu/bsd/miscfs/devfs/devfs_vfsops.c
<<
>>
Prefs
   1/*
   2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
   3 *
   4 * @APPLE_LICENSE_HEADER_START@
   5 * 
   6 * The contents of this file constitute Original Code as defined in and
   7 * are subject to the Apple Public Source License Version 1.1 (the
   8 * "License").  You may not use this file except in compliance with the
   9 * License.  Please obtain a copy of the License at
  10 * http://www.apple.com/publicsource and read it before using this file.
  11 * 
  12 * This Original Code and all software distributed under the License are
  13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
  17 * License for the specific language governing rights and limitations
  18 * under the License.
  19 * 
  20 * @APPLE_LICENSE_HEADER_END@
  21 */
  22/*-
  23 * Copyright 1997,1998 Julian Elischer.  All rights reserved.
  24 * julian@freebsd.org
  25 * 
  26 * Redistribution and use in source and binary forms, with or without
  27 * modification, are permitted provided that the following conditions are
  28 * met:
  29 *  1. Redistributions of source code must retain the above copyright
  30 *     notice, this list of conditions and the following disclaimer.
  31 *  2. Redistributions in binary form must reproduce the above copyright notice,
  32 *     this list of conditions and the following disclaimer in the documentation
  33 *     and/or other materials provided with the distribution.
  34 * 
  35 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ``AS IS'' AND ANY EXPRESS
  36 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  37 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38 * DISCLAIMED.  IN NO EVENT SHALL THE HOLDER OR CONTRIBUTORS BE LIABLE FOR
  39 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  41 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  42 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  45 * SUCH DAMAGE.
  46 * 
  47 * devfs_vfsops.c
  48 *
  49 */
  50/*
  51 * HISTORY
  52 *  Dieter Siegmund (dieter@apple.com) Wed Jul 14 13:37:59 PDT 1999
  53 *  - modified devfs_statfs() to use devfs_stats to calculate the
  54 *    amount of memory used by devfs
  55 */
  56
  57
  58#include <sys/param.h>
  59#include <sys/systm.h>
  60#include <sys/kernel.h>
  61#include <sys/vnode_internal.h>
  62#include <sys/proc.h>
  63#include <sys/kauth.h>
  64#include <sys/mount_internal.h>
  65#include <sys/malloc.h>
  66
  67#include "devfs.h"
  68#include "devfsdefs.h"
  69
  70static int devfs_statfs( struct mount *mp, struct vfsstatfs *sbp, vfs_context_t context);
  71static int devfs_vfs_getattr(mount_t mp, struct vfs_attr *fsap, vfs_context_t context);
  72
  73static struct vfstable * devfs_vfsp = 0;
  74
  75
  76/*-
  77 * Called from the generic VFS startups.
  78 * This is the second stage of DEVFS initialisation.
  79 * The probed devices have already been loaded and the 
  80 * basic structure of the DEVFS created.
  81 * We take the oportunity to mount the hidden DEVFS layer, so that
  82 * devices from devfs get sync'd.
  83 */
  84static int
  85devfs_init(struct vfsconf *vfsp)
  86{
  87    devfs_vfsp = (struct vfstable *)vfsp; /* remember this for devfs_kernel_mount below */
  88
  89    if (devfs_sinit())
  90        return (ENOTSUP);
  91    devfs_make_node(makedev(0, 0), DEVFS_CHAR, 
  92                    UID_ROOT, GID_WHEEL, 0622, "console");
  93    devfs_make_node(makedev(2, 0), DEVFS_CHAR, 
  94                    UID_ROOT, GID_WHEEL, 0666, "tty");
  95    devfs_make_node(makedev(3, 0), DEVFS_CHAR, 
  96                    UID_ROOT, GID_KMEM, 0640, "mem");
  97    devfs_make_node(makedev(3, 1), DEVFS_CHAR, 
  98                    UID_ROOT, GID_KMEM, 0640, "kmem");
  99    devfs_make_node(makedev(3, 2), DEVFS_CHAR, 
 100                    UID_ROOT, GID_WHEEL, 0666, "null");
 101    devfs_make_node(makedev(3, 3), DEVFS_CHAR, 
 102                    UID_ROOT, GID_WHEEL, 0666, "zero");
 103    devfs_make_node(makedev(6, 0), DEVFS_CHAR, 
 104                    UID_ROOT, GID_WHEEL, 0600, "klog");
 105    return 0;
 106}
 107
 108/*-
 109 *  mp   - pointer to 'mount' structure
 110 *  path - addr in user space of mount point (ie /usr or whatever)
 111 *  data - addr in user space of mount params including the
 112 *         name of the block special file to treat as a filesystem.
 113 *         (NOT USED)
 114 *  ndp  - namei data pointer (NOT USED)
 115 *  p    - proc pointer
 116 * devfs is special in that it doesn't require any device to be mounted..
 117 * It makes up its data as it goes along.
 118 * it must be mounted during single user.. until it is, only std{in/out/err}
 119 * and the root filesystem are available.
 120 */
 121/*proto*/
 122int
 123devfs_mount(struct mount *mp, __unused vnode_t devvp, __unused user_addr_t data, vfs_context_t context)
 124{
 125        struct devfsmount *devfs_mp_p;  /* devfs specific mount info */
 126        int error;
 127
 128        /*-
 129         *  If they just want to update, we don't need to do anything.
 130         */
 131        if (mp->mnt_flag & MNT_UPDATE)
 132        {
 133                return 0;
 134        }
 135
 136        /* Advisory locking should be handled at the VFS layer */
 137        vfs_setlocklocal(mp);
 138
 139        /*-
 140         *  Well, it's not an update, it's a real mount request.
 141         *  Time to get dirty.
 142         * HERE we should check to see if we are already mounted here.
 143         */
 144
 145        MALLOC(devfs_mp_p, struct devfsmount *, sizeof(struct devfsmount),
 146               M_DEVFSMNT, M_WAITOK);
 147        if (devfs_mp_p == NULL)
 148                return (ENOMEM);
 149        bzero(devfs_mp_p,sizeof(*devfs_mp_p));
 150        devfs_mp_p->mount = mp;
 151
 152        /*-
 153         *  Fill out some fields
 154         */
 155        mp->mnt_data = (qaddr_t)devfs_mp_p;
 156        mp->mnt_vfsstat.f_fsid.val[0] = (int32_t)(void *)devfs_mp_p;
 157        mp->mnt_vfsstat.f_fsid.val[1] = vfs_typenum(mp);
 158        mp->mnt_flag |= MNT_LOCAL;
 159
 160        DEVFS_LOCK();
 161        error = dev_dup_plane(devfs_mp_p);
 162        DEVFS_UNLOCK();
 163
 164        if (error) {
 165                mp->mnt_data = (qaddr_t)0;
 166                FREE((caddr_t)devfs_mp_p, M_DEVFSMNT);
 167                return (error);
 168        } else
 169                DEVFS_INCR_MOUNTS();
 170
 171        /*-
 172         *  Copy in the name of the directory the filesystem
 173         *  is to be mounted on.
 174         *  And we clear the remainder of the character strings
 175         *  to be tidy.
 176         */
 177        
 178        bzero(mp->mnt_vfsstat.f_mntfromname, MAXPATHLEN);
 179        bcopy("devfs",mp->mnt_vfsstat.f_mntfromname, 5);
 180        (void)devfs_statfs(mp, &mp->mnt_vfsstat, context);
 181
 182        return 0;
 183}
 184
 185
 186static int
 187devfs_start(__unused struct mount *mp, __unused int flags, __unused vfs_context_t context)
 188{
 189        return 0;
 190}
 191
 192/*-
 193 *  Unmount the filesystem described by mp.
 194 */
 195static int
 196devfs_unmount( struct mount *mp, int mntflags, __unused vfs_context_t context)
 197{
 198        struct devfsmount *devfs_mp_p = (struct devfsmount *)mp->mnt_data;
 199        int flags = 0;
 200        int force = 0;
 201        int error;
 202        
 203        if (mntflags & MNT_FORCE) {
 204                flags |= FORCECLOSE;
 205                force = 1;
 206        }
 207        error = vflush(mp, NULLVP, flags);
 208        if (error && !force)
 209                return error;
 210
 211        DEVFS_LOCK();
 212        devfs_free_plane(devfs_mp_p);
 213        DEVFS_UNLOCK();
 214
 215        DEVFS_DECR_MOUNTS();
 216
 217        FREE((caddr_t)devfs_mp_p, M_DEVFSMNT);
 218        mp->mnt_data = (qaddr_t)0;
 219        mp->mnt_flag &= ~MNT_LOCAL;
 220
 221        return 0;
 222}
 223
 224/* return the address of the root vnode  in *vpp */
 225static int
 226devfs_root(struct mount *mp, struct vnode **vpp, vfs_context_t context)
 227{
 228        struct devfsmount *devfs_mp_p = (struct devfsmount *)(mp->mnt_data);
 229        int error;
 230
 231        DEVFS_LOCK();
 232        error = devfs_dntovn(devfs_mp_p->plane_root->de_dnp, vpp, context->vc_proc);
 233        DEVFS_UNLOCK();
 234
 235        return error;
 236}
 237
 238static int
 239devfs_statfs( struct mount *mp, struct vfsstatfs *sbp, __unused vfs_context_t context)
 240{
 241        struct devfsmount *devfs_mp_p = (struct devfsmount *)mp->mnt_data;
 242
 243        /*-
 244         *  Fill in the stat block.
 245         */
 246        //sbp->f_type   = mp->mnt_vfsstat.f_type;
 247        sbp->f_flags  = 0;              /* XXX */
 248        sbp->f_bsize  = 512;
 249        sbp->f_iosize = 512;
 250        sbp->f_blocks = (devfs_stats.mounts * sizeof(struct devfsmount)
 251                         + devfs_stats.nodes * sizeof(devnode_t)
 252                         + devfs_stats.entries * sizeof(devdirent_t)
 253                         + devfs_stats.stringspace
 254                         ) / sbp->f_bsize;
 255        sbp->f_bfree  = 0;
 256        sbp->f_bavail = 0;
 257        sbp->f_files  = devfs_stats.nodes;
 258        sbp->f_ffree  = 0;
 259        sbp->f_fsid.val[0] = (int32_t)(void *)devfs_mp_p;
 260        sbp->f_fsid.val[1] = vfs_typenum(mp);
 261
 262        return 0;
 263}
 264
 265static int
 266devfs_vfs_getattr(mount_t mp, struct vfs_attr *fsap, vfs_context_t context)
 267{
 268        VFSATTR_RETURN(fsap, f_objcount, devfs_stats.nodes);
 269        VFSATTR_RETURN(fsap, f_maxobjcount, devfs_stats.nodes);
 270        VFSATTR_RETURN(fsap, f_bsize, 512);
 271        VFSATTR_RETURN(fsap, f_iosize, 512);
 272        if (VFSATTR_IS_ACTIVE(fsap, f_blocks) || VFSATTR_IS_ACTIVE(fsap, f_bused)) {
 273                fsap->f_blocks = (devfs_stats.mounts * sizeof(struct devfsmount)
 274                         + devfs_stats.nodes * sizeof(devnode_t)
 275                         + devfs_stats.entries * sizeof(devdirent_t)
 276                         + devfs_stats.stringspace
 277                         ) / fsap->f_bsize;
 278                fsap->f_bused = fsap->f_blocks;
 279                VFSATTR_SET_SUPPORTED(fsap, f_blocks);
 280                VFSATTR_SET_SUPPORTED(fsap, f_bused);
 281        }
 282        VFSATTR_RETURN(fsap, f_bfree, 0);
 283        VFSATTR_RETURN(fsap, f_bavail, 0);
 284        VFSATTR_RETURN(fsap, f_files, devfs_stats.nodes);
 285        VFSATTR_RETURN(fsap, f_ffree, 0);
 286        VFSATTR_RETURN(fsap, f_fssubtype, 0);
 287        
 288        return 0;
 289}
 290
 291static int
 292devfs_sync(__unused struct mount *mp, __unused int waitfor, __unused vfs_context_t context)
 293{
 294    return (0);
 295}
 296
 297
 298static int
 299devfs_vget(__unused struct mount *mp, __unused ino64_t ino, __unused struct vnode **vpp, __unused vfs_context_t context)
 300{
 301        return ENOTSUP;
 302}
 303
 304/*************************************************************
 305 * The concept of exporting a kernel generated devfs is stupid
 306 * So don't handle filehandles
 307 */
 308
 309static int
 310devfs_fhtovp (__unused struct mount *mp, __unused int fhlen, __unused unsigned char *fhp, __unused struct vnode **vpp, __unused vfs_context_t context)
 311{
 312        return (EINVAL);
 313}
 314
 315
 316static int
 317devfs_vptofh (__unused struct vnode *vp, __unused int *fhlenp, __unused unsigned char *fhp, __unused vfs_context_t context)
 318{
 319        return (EINVAL);
 320}
 321
 322static int
 323devfs_sysctl(__unused int *name, __unused u_int namelen, __unused user_addr_t oldp, 
 324             __unused size_t *oldlenp, __unused user_addr_t newp, 
 325             __unused size_t newlen, __unused vfs_context_t context)
 326{
 327    return (ENOTSUP);
 328}
 329
 330#include <sys/namei.h>
 331
 332/*
 333 * Function: devfs_kernel_mount
 334 * Purpose:
 335 *   Mount devfs at the given mount point from within the kernel.
 336 */
 337int
 338devfs_kernel_mount(char * mntname)
 339{
 340        struct mount *mp;
 341        int error;
 342        struct nameidata nd;
 343        struct vnode  * vp;
 344        struct vfs_context context;
 345
 346        if (devfs_vfsp == NULL) {
 347            printf("devfs_kernel_mount: devfs_vfsp is NULL\n");
 348            return (EINVAL);
 349        }
 350        context.vc_proc = current_proc();
 351        context.vc_ucred = kauth_cred_get();
 352
 353        /*
 354         * Get vnode to be covered
 355         */
 356        NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE32,
 357            CAST_USER_ADDR_T(mntname), &context);
 358        if ((error = namei(&nd))) {
 359            printf("devfs_kernel_mount: failed to find directory '%s', %d", 
 360                   mntname, error);
 361            return (error);
 362        }
 363        nameidone(&nd);
 364        vp = nd.ni_vp;
 365
 366        if ((error = VNOP_FSYNC(vp, MNT_WAIT, &context))) {
 367            printf("devfs_kernel_mount: vnop_fsync failed: %d\n", error);
 368            vnode_put(vp);
 369            return (error);
 370        }
 371        if ((error = buf_invalidateblks(vp, BUF_WRITE_DATA, 0, 0))) {
 372            printf("devfs_kernel_mount: buf_invalidateblks failed: %d\n", error);
 373            vnode_put(vp);
 374            return (error);
 375        }
 376        if (vnode_isdir(vp) == 0) {
 377            printf("devfs_kernel_mount: '%s' is not a directory\n", mntname);
 378            vnode_put(vp);
 379            return (ENOTDIR);
 380        }
 381        if ((vnode_mountedhere(vp))) {
 382            vnode_put(vp);
 383            return (EBUSY);
 384        }
 385
 386        /*
 387         * Allocate and initialize the filesystem.
 388         */
 389        MALLOC_ZONE(mp, struct mount *, (u_long)sizeof(struct mount),
 390                M_MOUNT, M_WAITOK);
 391        bzero((char *)mp, (u_long)sizeof(struct mount));
 392
 393        /* Initialize the default IO constraints */
 394        mp->mnt_maxreadcnt = mp->mnt_maxwritecnt = MAXPHYS;
 395        mp->mnt_segreadcnt = mp->mnt_segwritecnt = 32;
 396
 397        mount_lock_init(mp);
 398        TAILQ_INIT(&mp->mnt_vnodelist);
 399        TAILQ_INIT(&mp->mnt_workerqueue);
 400        TAILQ_INIT(&mp->mnt_newvnodes);
 401
 402        (void)vfs_busy(mp, LK_NOWAIT);
 403        mp->mnt_op = devfs_vfsp->vfc_vfsops;
 404        mp->mnt_vtable = devfs_vfsp;
 405        devfs_vfsp->vfc_refcount++;
 406        devfs_vfsp->vfc_threadsafe = TRUE;
 407        devfs_vfsp->vfc_64bitready = TRUE;
 408        mp->mnt_flag = 0;
 409        mp->mnt_flag |= devfs_vfsp->vfc_flags & MNT_VISFLAGMASK;
 410        strncpy(mp->mnt_vfsstat.f_fstypename, devfs_vfsp->vfc_name, MFSTYPENAMELEN);
 411        vp->v_mountedhere = mp;
 412        mp->mnt_vnodecovered = vp;
 413        mp->mnt_vfsstat.f_owner = kauth_cred_getuid(kauth_cred_get());
 414        (void) copystr(mntname, mp->mnt_vfsstat.f_mntonname, MAXPATHLEN - 1, 0);
 415
 416        error = devfs_mount(mp, NULL, NULL, &context);
 417
 418        if (error) {
 419            printf("devfs_kernel_mount: mount %s failed: %d", mntname, error);
 420            mp->mnt_vtable->vfc_refcount--;
 421
 422            vfs_unbusy(mp);
 423
 424            mount_lock_destroy(mp);
 425            FREE_ZONE(mp, sizeof (struct mount), M_MOUNT);
 426            vnode_put(vp);
 427            return (error);
 428        }
 429        vnode_ref(vp);
 430        vnode_put(vp);
 431        vfs_unbusy(mp);
 432        mount_list_add(mp);
 433        return (0);
 434}
 435
 436struct vfsops devfs_vfsops = {
 437        devfs_mount,
 438        devfs_start,
 439        devfs_unmount,
 440        devfs_root,
 441        NULL,                           /* quotactl */
 442        devfs_vfs_getattr,
 443        devfs_sync,
 444        devfs_vget,
 445        devfs_fhtovp,
 446        devfs_vptofh,
 447        devfs_init,
 448        devfs_sysctl
 449};
 450
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.