LCOV - code coverage report
Current view: top level - drivers/base - devtmpfs.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 43 174 24.7 %
Date: 2014-02-18 Functions: 3 14 21.4 %
Branches: 12 83 14.5 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * devtmpfs - kernel-maintained tmpfs-based /dev
       3                 :            :  *
       4                 :            :  * Copyright (C) 2009, Kay Sievers <kay.sievers@vrfy.org>
       5                 :            :  *
       6                 :            :  * During bootup, before any driver core device is registered,
       7                 :            :  * devtmpfs, a tmpfs-based filesystem is created. Every driver-core
       8                 :            :  * device which requests a device node, will add a node in this
       9                 :            :  * filesystem.
      10                 :            :  * By default, all devices are named after the name of the device,
      11                 :            :  * owned by root and have a default mode of 0600. Subsystems can
      12                 :            :  * overwrite the default setting if needed.
      13                 :            :  */
      14                 :            : 
      15                 :            : #include <linux/kernel.h>
      16                 :            : #include <linux/syscalls.h>
      17                 :            : #include <linux/mount.h>
      18                 :            : #include <linux/device.h>
      19                 :            : #include <linux/genhd.h>
      20                 :            : #include <linux/namei.h>
      21                 :            : #include <linux/fs.h>
      22                 :            : #include <linux/shmem_fs.h>
      23                 :            : #include <linux/ramfs.h>
      24                 :            : #include <linux/sched.h>
      25                 :            : #include <linux/slab.h>
      26                 :            : #include <linux/kthread.h>
      27                 :            : #include "base.h"
      28                 :            : 
      29                 :            : static struct task_struct *thread;
      30                 :            : 
      31                 :            : #if defined CONFIG_DEVTMPFS_MOUNT
      32                 :            : static int mount_dev = 1;
      33                 :            : #else
      34                 :            : static int mount_dev;
      35                 :            : #endif
      36                 :            : 
      37                 :            : static DEFINE_SPINLOCK(req_lock);
      38                 :            : 
      39                 :            : static struct req {
      40                 :            :         struct req *next;
      41                 :            :         struct completion done;
      42                 :            :         int err;
      43                 :            :         const char *name;
      44                 :            :         umode_t mode;   /* 0 => delete */
      45                 :            :         kuid_t uid;
      46                 :            :         kgid_t gid;
      47                 :            :         struct device *dev;
      48                 :            : } *requests;
      49                 :            : 
      50                 :          0 : static int __init mount_param(char *str)
      51                 :            : {
      52                 :          0 :         mount_dev = simple_strtoul(str, NULL, 0);
      53                 :          0 :         return 1;
      54                 :            : }
      55                 :            : __setup("devtmpfs.mount=", mount_param);
      56                 :            : 
      57                 :          0 : static struct dentry *dev_mount(struct file_system_type *fs_type, int flags,
      58                 :            :                       const char *dev_name, void *data)
      59                 :            : {
      60                 :            : #ifdef CONFIG_TMPFS
      61                 :          0 :         return mount_single(fs_type, flags, data, shmem_fill_super);
      62                 :            : #else
      63                 :            :         return mount_single(fs_type, flags, data, ramfs_fill_super);
      64                 :            : #endif
      65                 :            : }
      66                 :            : 
      67                 :            : static struct file_system_type dev_fs_type = {
      68                 :            :         .name = "devtmpfs",
      69                 :            :         .mount = dev_mount,
      70                 :            :         .kill_sb = kill_litter_super,
      71                 :            : };
      72                 :            : 
      73                 :            : #ifdef CONFIG_BLOCK
      74                 :            : static inline int is_blockdev(struct device *dev)
      75                 :            : {
      76                 :            :         return dev->class == &block_class;
      77                 :            : }
      78                 :            : #else
      79                 :            : static inline int is_blockdev(struct device *dev) { return 0; }
      80                 :            : #endif
      81                 :            : 
      82                 :          0 : int devtmpfs_create_node(struct device *dev)
      83                 :            : {
      84                 :        112 :         const char *tmp = NULL;
      85                 :            :         struct req req;
      86                 :            : 
      87         [ +  - ]:        112 :         if (!thread)
      88                 :            :                 return 0;
      89                 :            : 
      90                 :        112 :         req.mode = 0;
      91                 :        112 :         req.uid = GLOBAL_ROOT_UID;
      92                 :        112 :         req.gid = GLOBAL_ROOT_GID;
      93                 :        112 :         req.name = device_get_devnode(dev, &req.mode, &req.uid, &req.gid, &tmp);
      94            [ + ]:        112 :         if (!req.name)
      95                 :            :                 return -ENOMEM;
      96                 :            : 
      97         [ +  - ]:        224 :         if (req.mode == 0)
      98                 :        112 :                 req.mode = 0600;
      99         [ -  + ]:        112 :         if (is_blockdev(dev))
     100                 :          0 :                 req.mode |= S_IFBLK;
     101                 :            :         else
     102                 :        112 :                 req.mode |= S_IFCHR;
     103                 :            : 
     104                 :        112 :         req.dev = dev;
     105                 :            : 
     106                 :            :         init_completion(&req.done);
     107                 :            : 
     108                 :            :         spin_lock(&req_lock);
     109                 :        112 :         req.next = requests;
     110                 :        112 :         requests = &req;
     111                 :            :         spin_unlock(&req_lock);
     112                 :            : 
     113                 :        112 :         wake_up_process(thread);
     114                 :        112 :         wait_for_completion(&req.done);
     115                 :            : 
     116                 :        112 :         kfree(tmp);
     117                 :            : 
     118                 :        112 :         return req.err;
     119                 :            : }
     120                 :            : 
     121                 :          0 : int devtmpfs_delete_node(struct device *dev)
     122                 :            : {
     123                 :          0 :         const char *tmp = NULL;
     124                 :            :         struct req req;
     125                 :            : 
     126         [ #  # ]:          0 :         if (!thread)
     127                 :            :                 return 0;
     128                 :            : 
     129                 :          0 :         req.name = device_get_devnode(dev, NULL, NULL, NULL, &tmp);
     130         [ #  # ]:          0 :         if (!req.name)
     131                 :            :                 return -ENOMEM;
     132                 :            : 
     133                 :          0 :         req.mode = 0;
     134                 :          0 :         req.dev = dev;
     135                 :            : 
     136                 :            :         init_completion(&req.done);
     137                 :            : 
     138                 :            :         spin_lock(&req_lock);
     139                 :          0 :         req.next = requests;
     140                 :          0 :         requests = &req;
     141                 :            :         spin_unlock(&req_lock);
     142                 :            : 
     143                 :          0 :         wake_up_process(thread);
     144                 :          0 :         wait_for_completion(&req.done);
     145                 :            : 
     146                 :          0 :         kfree(tmp);
     147                 :          0 :         return req.err;
     148                 :            : }
     149                 :            : 
     150                 :          0 : static int dev_mkdir(const char *name, umode_t mode)
     151                 :            : {
     152                 :            :         struct dentry *dentry;
     153                 :            :         struct path path;
     154                 :            :         int err;
     155                 :            : 
     156                 :          0 :         dentry = kern_path_create(AT_FDCWD, name, &path, LOOKUP_DIRECTORY);
     157         [ #  # ]:          0 :         if (IS_ERR(dentry))
     158                 :          0 :                 return PTR_ERR(dentry);
     159                 :            : 
     160                 :          0 :         err = vfs_mkdir(path.dentry->d_inode, dentry, mode);
     161         [ #  # ]:          0 :         if (!err)
     162                 :            :                 /* mark as kernel-created inode */
     163                 :          0 :                 dentry->d_inode->i_private = &thread;
     164                 :          0 :         done_path_create(&path, dentry);
     165                 :          0 :         return err;
     166                 :            : }
     167                 :            : 
     168                 :          0 : static int create_path(const char *nodepath)
     169                 :            : {
     170                 :            :         char *path;
     171                 :            :         char *s;
     172                 :            :         int err = 0;
     173                 :            : 
     174                 :            :         /* parent directories do not exist, create them */
     175                 :          0 :         path = kstrdup(nodepath, GFP_KERNEL);
     176         [ #  # ]:          0 :         if (!path)
     177                 :            :                 return -ENOMEM;
     178                 :            : 
     179                 :            :         s = path;
     180                 :            :         for (;;) {
     181                 :          0 :                 s = strchr(s, '/');
     182         [ #  # ]:          0 :                 if (!s)
     183                 :            :                         break;
     184                 :          0 :                 s[0] = '\0';
     185                 :          0 :                 err = dev_mkdir(path, 0755);
     186         [ #  # ]:          0 :                 if (err && err != -EEXIST)
     187                 :            :                         break;
     188                 :          0 :                 s[0] = '/';
     189                 :          0 :                 s++;
     190                 :          0 :         }
     191                 :          0 :         kfree(path);
     192                 :          0 :         return err;
     193                 :            : }
     194                 :            : 
     195                 :          0 : static int handle_create(const char *nodename, umode_t mode, kuid_t uid,
     196                 :            :                          kgid_t gid, struct device *dev)
     197                 :            : {
     198                 :            :         struct dentry *dentry;
     199                 :            :         struct path path;
     200                 :            :         int err;
     201                 :            : 
     202                 :        112 :         dentry = kern_path_create(AT_FDCWD, nodename, &path, 0);
     203         [ -  + ]:        112 :         if (dentry == ERR_PTR(-ENOENT)) {
     204                 :          0 :                 create_path(nodename);
     205                 :          0 :                 dentry = kern_path_create(AT_FDCWD, nodename, &path, 0);
     206                 :            :         }
     207         [ -  + ]:        112 :         if (IS_ERR(dentry))
     208                 :            :                 return PTR_ERR(dentry);
     209                 :            : 
     210                 :        112 :         err = vfs_mknod(path.dentry->d_inode, dentry, mode, dev->devt);
     211         [ +  - ]:        112 :         if (!err) {
     212                 :            :                 struct iattr newattrs;
     213                 :            : 
     214                 :        112 :                 newattrs.ia_mode = mode;
     215                 :        112 :                 newattrs.ia_uid = uid;
     216                 :        112 :                 newattrs.ia_gid = gid;
     217                 :        112 :                 newattrs.ia_valid = ATTR_MODE|ATTR_UID|ATTR_GID;
     218                 :        112 :                 mutex_lock(&dentry->d_inode->i_mutex);
     219                 :        112 :                 notify_change(dentry, &newattrs, NULL);
     220                 :        112 :                 mutex_unlock(&dentry->d_inode->i_mutex);
     221                 :            : 
     222                 :            :                 /* mark as kernel-created inode */
     223                 :        112 :                 dentry->d_inode->i_private = &thread;
     224                 :            :         }
     225                 :        112 :         done_path_create(&path, dentry);
     226                 :            :         return err;
     227                 :            : }
     228                 :            : 
     229                 :          0 : static int dev_rmdir(const char *name)
     230                 :            : {
     231                 :            :         struct path parent;
     232                 :            :         struct dentry *dentry;
     233                 :            :         int err;
     234                 :            : 
     235                 :          0 :         dentry = kern_path_locked(name, &parent);
     236         [ #  # ]:          0 :         if (IS_ERR(dentry))
     237                 :          0 :                 return PTR_ERR(dentry);
     238         [ #  # ]:          0 :         if (dentry->d_inode) {
     239         [ #  # ]:          0 :                 if (dentry->d_inode->i_private == &thread)
     240                 :          0 :                         err = vfs_rmdir(parent.dentry->d_inode, dentry);
     241                 :            :                 else
     242                 :            :                         err = -EPERM;
     243                 :            :         } else {
     244                 :            :                 err = -ENOENT;
     245                 :            :         }
     246                 :          0 :         dput(dentry);
     247                 :          0 :         mutex_unlock(&parent.dentry->d_inode->i_mutex);
     248                 :          0 :         path_put(&parent);
     249                 :          0 :         return err;
     250                 :            : }
     251                 :            : 
     252                 :          0 : static int delete_path(const char *nodepath)
     253                 :            : {
     254                 :            :         const char *path;
     255                 :            :         int err = 0;
     256                 :            : 
     257                 :          0 :         path = kstrdup(nodepath, GFP_KERNEL);
     258         [ #  # ]:          0 :         if (!path)
     259                 :            :                 return -ENOMEM;
     260                 :            : 
     261                 :            :         for (;;) {
     262                 :            :                 char *base;
     263                 :            : 
     264                 :          0 :                 base = strrchr(path, '/');
     265         [ #  # ]:          0 :                 if (!base)
     266                 :            :                         break;
     267                 :          0 :                 base[0] = '\0';
     268                 :          0 :                 err = dev_rmdir(path);
     269         [ #  # ]:          0 :                 if (err)
     270                 :            :                         break;
     271                 :            :         }
     272                 :            : 
     273                 :          0 :         kfree(path);
     274                 :          0 :         return err;
     275                 :            : }
     276                 :            : 
     277                 :          0 : static int dev_mynode(struct device *dev, struct inode *inode, struct kstat *stat)
     278                 :            : {
     279                 :            :         /* did we create it */
     280         [ #  # ]:          0 :         if (inode->i_private != &thread)
     281                 :            :                 return 0;
     282                 :            : 
     283                 :            :         /* does the dev_t match */
     284         [ #  # ]:          0 :         if (is_blockdev(dev)) {
     285         [ #  # ]:          0 :                 if (!S_ISBLK(stat->mode))
     286                 :            :                         return 0;
     287                 :            :         } else {
     288         [ #  # ]:          0 :                 if (!S_ISCHR(stat->mode))
     289                 :            :                         return 0;
     290                 :            :         }
     291         [ #  # ]:          0 :         if (stat->rdev != dev->devt)
     292                 :            :                 return 0;
     293                 :            : 
     294                 :            :         /* ours */
     295                 :            :         return 1;
     296                 :            : }
     297                 :            : 
     298                 :          0 : static int handle_remove(const char *nodename, struct device *dev)
     299                 :            : {
     300                 :            :         struct path parent;
     301                 :            :         struct dentry *dentry;
     302                 :            :         int deleted = 1;
     303                 :            :         int err;
     304                 :            : 
     305                 :          0 :         dentry = kern_path_locked(nodename, &parent);
     306         [ #  # ]:          0 :         if (IS_ERR(dentry))
     307                 :          0 :                 return PTR_ERR(dentry);
     308                 :            : 
     309         [ #  # ]:          0 :         if (dentry->d_inode) {
     310                 :            :                 struct kstat stat;
     311                 :          0 :                 struct path p = {.mnt = parent.mnt, .dentry = dentry};
     312                 :          0 :                 err = vfs_getattr(&p, &stat);
     313 [ #  # ][ #  # ]:          0 :                 if (!err && dev_mynode(dev, dentry->d_inode, &stat)) {
     314                 :            :                         struct iattr newattrs;
     315                 :            :                         /*
     316                 :            :                          * before unlinking this node, reset permissions
     317                 :            :                          * of possible references like hardlinks
     318                 :            :                          */
     319                 :          0 :                         newattrs.ia_uid = GLOBAL_ROOT_UID;
     320                 :          0 :                         newattrs.ia_gid = GLOBAL_ROOT_GID;
     321                 :          0 :                         newattrs.ia_mode = stat.mode & ~0777;
     322                 :          0 :                         newattrs.ia_valid =
     323                 :            :                                 ATTR_UID|ATTR_GID|ATTR_MODE;
     324                 :          0 :                         mutex_lock(&dentry->d_inode->i_mutex);
     325                 :          0 :                         notify_change(dentry, &newattrs, NULL);
     326                 :          0 :                         mutex_unlock(&dentry->d_inode->i_mutex);
     327                 :          0 :                         err = vfs_unlink(parent.dentry->d_inode, dentry, NULL);
     328                 :            :                         if (!err || err == -ENOENT)
     329                 :            :                                 deleted = 1;
     330                 :            :                 }
     331                 :            :         } else {
     332                 :            :                 err = -ENOENT;
     333                 :            :         }
     334                 :          0 :         dput(dentry);
     335                 :          0 :         mutex_unlock(&parent.dentry->d_inode->i_mutex);
     336                 :            : 
     337                 :          0 :         path_put(&parent);
     338         [ #  # ]:          0 :         if (deleted && strchr(nodename, '/'))
     339                 :          0 :                 delete_path(nodename);
     340                 :          0 :         return err;
     341                 :            : }
     342                 :            : 
     343                 :            : /*
     344                 :            :  * If configured, or requested by the commandline, devtmpfs will be
     345                 :            :  * auto-mounted after the kernel mounted the root filesystem.
     346                 :            :  */
     347                 :          0 : int devtmpfs_mount(const char *mntdir)
     348                 :            : {
     349                 :            :         int err;
     350                 :            : 
     351         [ #  # ]:          0 :         if (!mount_dev)
     352                 :            :                 return 0;
     353                 :            : 
     354         [ #  # ]:          0 :         if (!thread)
     355                 :            :                 return 0;
     356                 :            : 
     357                 :          0 :         err = sys_mount("devtmpfs", (char *)mntdir, "devtmpfs", MS_SILENT, NULL);
     358         [ #  # ]:          0 :         if (err)
     359                 :          0 :                 printk(KERN_INFO "devtmpfs: error mounting %i\n", err);
     360                 :            :         else
     361                 :          0 :                 printk(KERN_INFO "devtmpfs: mounted\n");
     362                 :          0 :         return err;
     363                 :            : }
     364                 :            : 
     365                 :            : static DECLARE_COMPLETION(setup_done);
     366                 :            : 
     367                 :          0 : static int handle(const char *name, umode_t mode, kuid_t uid, kgid_t gid,
     368                 :            :                   struct device *dev)
     369                 :            : {
     370         [ +  - ]:        112 :         if (mode)
     371                 :        112 :                 return handle_create(name, mode, uid, gid, dev);
     372                 :            :         else
     373                 :          0 :                 return handle_remove(name, dev);
     374                 :            : }
     375                 :            : 
     376                 :          0 : static int devtmpfsd(void *p)
     377                 :            : {
     378                 :          0 :         char options[] = "mode=0755";
     379                 :            :         int *err = p;
     380                 :          0 :         *err = sys_unshare(CLONE_NEWNS);
     381         [ #  # ]:          0 :         if (*err)
     382                 :            :                 goto out;
     383                 :          0 :         *err = sys_mount("devtmpfs", "/", "devtmpfs", MS_SILENT, options);
     384         [ #  # ]:          0 :         if (*err)
     385                 :            :                 goto out;
     386                 :          0 :         sys_chdir("/.."); /* will traverse into overmounted root */
     387                 :          0 :         sys_chroot(".");
     388                 :          0 :         complete(&setup_done);
     389                 :            :         while (1) {
     390                 :            :                 spin_lock(&req_lock);
     391         [ +  + ]:        224 :                 while (requests) {
     392                 :            :                         struct req *req = requests;
     393                 :        112 :                         requests = NULL;
     394                 :            :                         spin_unlock(&req_lock);
     395         [ +  + ]:        224 :                         while (req) {
     396                 :        112 :                                 struct req *next = req->next;
     397                 :        112 :                                 req->err = handle(req->name, req->mode,
     398                 :            :                                                   req->uid, req->gid, req->dev);
     399                 :        112 :                                 complete(&req->done);
     400                 :            :                                 req = next;
     401                 :            :                         }
     402                 :            :                         spin_lock(&req_lock);
     403                 :            :                 }
     404                 :        112 :                 __set_current_state(TASK_INTERRUPTIBLE);
     405                 :            :                 spin_unlock(&req_lock);
     406                 :        112 :                 schedule();
     407                 :        112 :         }
     408                 :            :         return 0;
     409                 :            : out:
     410                 :          0 :         complete(&setup_done);
     411                 :          0 :         return *err;
     412                 :            : }
     413                 :            : 
     414                 :            : /*
     415                 :            :  * Create devtmpfs instance, driver-core devices will add their device
     416                 :            :  * nodes here.
     417                 :            :  */
     418                 :          0 : int __init devtmpfs_init(void)
     419                 :            : {
     420                 :          0 :         int err = register_filesystem(&dev_fs_type);
     421         [ #  # ]:          0 :         if (err) {
     422                 :          0 :                 printk(KERN_ERR "devtmpfs: unable to register devtmpfs "
     423                 :            :                        "type %i\n", err);
     424                 :          0 :                 return err;
     425                 :            :         }
     426                 :            : 
     427         [ #  # ]:          0 :         thread = kthread_run(devtmpfsd, &err, "kdevtmpfs");
     428         [ #  # ]:          0 :         if (!IS_ERR(thread)) {
     429                 :          0 :                 wait_for_completion(&setup_done);
     430                 :            :         } else {
     431                 :          0 :                 err = PTR_ERR(thread);
     432                 :          0 :                 thread = NULL;
     433                 :            :         }
     434                 :            : 
     435         [ #  # ]:          0 :         if (err) {
     436                 :          0 :                 printk(KERN_ERR "devtmpfs: unable to create devtmpfs %i\n", err);
     437                 :          0 :                 unregister_filesystem(&dev_fs_type);
     438                 :          0 :                 return err;
     439                 :            :         }
     440                 :            : 
     441                 :          0 :         printk(KERN_INFO "devtmpfs: initialized\n");
     442                 :          0 :         return 0;
     443                 :            : }

Generated by: LCOV version 1.9