linux/fs/sysfs/inode.c
<<
>>
Prefs
   1/*
   2 * fs/sysfs/inode.c - basic sysfs inode and dentry operations
   3 *
   4 * Copyright (c) 2001-3 Patrick Mochel
   5 * Copyright (c) 2007 SUSE Linux Products GmbH
   6 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
   7 *
   8 * This file is released under the GPLv2.
   9 *
  10 * Please see Documentation/filesystems/sysfs.txt for more information.
  11 */
  12
  13#undef DEBUG 
  14
  15#include <linux/pagemap.h>
  16#include <linux/namei.h>
  17#include <linux/backing-dev.h>
  18#include <linux/capability.h>
  19#include <linux/errno.h>
  20#include <linux/sched.h>
  21#include <linux/slab.h>
  22#include <linux/sysfs.h>
  23#include <linux/xattr.h>
  24#include <linux/security.h>
  25#include "sysfs.h"
  26
  27static const struct address_space_operations sysfs_aops = {
  28        .readpage       = simple_readpage,
  29        .write_begin    = simple_write_begin,
  30        .write_end      = simple_write_end,
  31};
  32
  33static struct backing_dev_info sysfs_backing_dev_info = {
  34        .name           = "sysfs",
  35        .ra_pages       = 0,    /* No readahead */
  36        .capabilities   = BDI_CAP_NO_ACCT_AND_WRITEBACK,
  37};
  38
  39static const struct inode_operations sysfs_inode_operations ={
  40        .permission     = sysfs_permission,
  41        .setattr        = sysfs_setattr,
  42        .getattr        = sysfs_getattr,
  43        .setxattr       = sysfs_setxattr,
  44};
  45
  46int __init sysfs_inode_init(void)
  47{
  48        return bdi_init(&sysfs_backing_dev_info);
  49}
  50
  51static struct sysfs_inode_attrs *sysfs_init_inode_attrs(struct sysfs_dirent *sd)
  52{
  53        struct sysfs_inode_attrs *attrs;
  54        struct iattr *iattrs;
  55
  56        attrs = kzalloc(sizeof(struct sysfs_inode_attrs), GFP_KERNEL);
  57        if (!attrs)
  58                return NULL;
  59        iattrs = &attrs->ia_iattr;
  60
  61        /* assign default attributes */
  62        iattrs->ia_mode = sd->s_mode;
  63        iattrs->ia_uid = GLOBAL_ROOT_UID;
  64        iattrs->ia_gid = GLOBAL_ROOT_GID;
  65        iattrs->ia_atime = iattrs->ia_mtime = iattrs->ia_ctime = CURRENT_TIME;
  66
  67        return attrs;
  68}
  69
  70int sysfs_sd_setattr(struct sysfs_dirent *sd, struct iattr * iattr)
  71{
  72        struct sysfs_inode_attrs *sd_attrs;
  73        struct iattr *iattrs;
  74        unsigned int ia_valid = iattr->ia_valid;
  75
  76        sd_attrs = sd->s_iattr;
  77
  78        if (!sd_attrs) {
  79                /* setting attributes for the first time, allocate now */
  80                sd_attrs = sysfs_init_inode_attrs(sd);
  81                if (!sd_attrs)
  82                        return -ENOMEM;
  83                sd->s_iattr = sd_attrs;
  84        }
  85        /* attributes were changed at least once in past */
  86        iattrs = &sd_attrs->ia_iattr;
  87
  88        if (ia_valid & ATTR_UID)
  89                iattrs->ia_uid = iattr->ia_uid;
  90        if (ia_valid & ATTR_GID)
  91                iattrs->ia_gid = iattr->ia_gid;
  92        if (ia_valid & ATTR_ATIME)
  93                iattrs->ia_atime = iattr->ia_atime;
  94        if (ia_valid & ATTR_MTIME)
  95                iattrs->ia_mtime = iattr->ia_mtime;
  96        if (ia_valid & ATTR_CTIME)
  97                iattrs->ia_ctime = iattr->ia_ctime;
  98        if (ia_valid & ATTR_MODE) {
  99                umode_t mode = iattr->ia_mode;
 100                iattrs->ia_mode = sd->s_mode = mode;
 101        }
 102        return 0;
 103}
 104
 105int sysfs_setattr(struct dentry *dentry, struct iattr *iattr)
 106{
 107        struct inode *inode = dentry->d_inode;
 108        struct sysfs_dirent *sd = dentry->d_fsdata;
 109        int error;
 110
 111        if (!sd)
 112                return -EINVAL;
 113
 114        mutex_lock(&sysfs_mutex);
 115        error = inode_change_ok(inode, iattr);
 116        if (error)
 117                goto out;
 118
 119        error = sysfs_sd_setattr(sd, iattr);
 120        if (error)
 121                goto out;
 122
 123        /* this ignores size changes */
 124        setattr_copy(inode, iattr);
 125
 126out:
 127        mutex_unlock(&sysfs_mutex);
 128        return error;
 129}
 130
 131static int sysfs_sd_setsecdata(struct sysfs_dirent *sd, void **secdata, u32 *secdata_len)
 132{
 133        struct sysfs_inode_attrs *iattrs;
 134        void *old_secdata;
 135        size_t old_secdata_len;
 136
 137        if (!sd->s_iattr) {
 138                sd->s_iattr = sysfs_init_inode_attrs(sd);
 139                if (!sd->s_iattr)
 140                        return -ENOMEM;
 141        }
 142
 143        iattrs = sd->s_iattr;
 144        old_secdata = iattrs->ia_secdata;
 145        old_secdata_len = iattrs->ia_secdata_len;
 146
 147        iattrs->ia_secdata = *secdata;
 148        iattrs->ia_secdata_len = *secdata_len;
 149
 150        *secdata = old_secdata;
 151        *secdata_len = old_secdata_len;
 152        return 0;
 153}
 154
 155int sysfs_setxattr(struct dentry *dentry, const char *name, const void *value,
 156                size_t size, int flags)
 157{
 158        struct sysfs_dirent *sd = dentry->d_fsdata;
 159        void *secdata;
 160        int error;
 161        u32 secdata_len = 0;
 162
 163        if (!sd)
 164                return -EINVAL;
 165
 166        if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)) {
 167                const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
 168                error = security_inode_setsecurity(dentry->d_inode, suffix,
 169                                                value, size, flags);
 170                if (error)
 171                        goto out;
 172                error = security_inode_getsecctx(dentry->d_inode,
 173                                                &secdata, &secdata_len);
 174                if (error)
 175                        goto out;
 176
 177                mutex_lock(&sysfs_mutex);
 178                error = sysfs_sd_setsecdata(sd, &secdata, &secdata_len);
 179                mutex_unlock(&sysfs_mutex);
 180
 181                if (secdata)
 182                        security_release_secctx(secdata, secdata_len);
 183        } else
 184                return -EINVAL;
 185out:
 186        return error;
 187}
 188
 189static inline void set_default_inode_attr(struct inode * inode, umode_t mode)
 190{
 191        inode->i_mode = mode;
 192        inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
 193}
 194
 195static inline void set_inode_attr(struct inode * inode, struct iattr * iattr)
 196{
 197        inode->i_uid = iattr->ia_uid;
 198        inode->i_gid = iattr->ia_gid;
 199        inode->i_atime = iattr->ia_atime;
 200        inode->i_mtime = iattr->ia_mtime;
 201        inode->i_ctime = iattr->ia_ctime;
 202}
 203
 204static void sysfs_refresh_inode(struct sysfs_dirent *sd, struct inode *inode)
 205{
 206        struct sysfs_inode_attrs *iattrs = sd->s_iattr;
 207
 208        inode->i_mode = sd->s_mode;
 209        if (iattrs) {
 210                /* sysfs_dirent has non-default attributes
 211                 * get them from persistent copy in sysfs_dirent
 212                 */
 213                set_inode_attr(inode, &iattrs->ia_iattr);
 214                security_inode_notifysecctx(inode,
 215                                            iattrs->ia_secdata,
 216                                            iattrs->ia_secdata_len);
 217        }
 218
 219        if (sysfs_type(sd) == SYSFS_DIR)
 220                set_nlink(inode, sd->s_dir.subdirs + 2);
 221}
 222
 223int sysfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
 224{
 225        struct sysfs_dirent *sd = dentry->d_fsdata;
 226        struct inode *inode = dentry->d_inode;
 227
 228        mutex_lock(&sysfs_mutex);
 229        sysfs_refresh_inode(sd, inode);
 230        mutex_unlock(&sysfs_mutex);
 231
 232        generic_fillattr(inode, stat);
 233        return 0;
 234}
 235
 236static void sysfs_init_inode(struct sysfs_dirent *sd, struct inode *inode)
 237{
 238        struct bin_attribute *bin_attr;
 239
 240        inode->i_private = sysfs_get(sd);
 241        inode->i_mapping->a_ops = &sysfs_aops;
 242        inode->i_mapping->backing_dev_info = &sysfs_backing_dev_info;
 243        inode->i_op = &sysfs_inode_operations;
 244
 245        set_default_inode_attr(inode, sd->s_mode);
 246        sysfs_refresh_inode(sd, inode);
 247
 248        /* initialize inode according to type */
 249        switch (sysfs_type(sd)) {
 250        case SYSFS_DIR:
 251                inode->i_op = &sysfs_dir_inode_operations;
 252                inode->i_fop = &sysfs_dir_operations;
 253                break;
 254        case SYSFS_KOBJ_ATTR:
 255                inode->i_size = PAGE_SIZE;
 256                inode->i_fop = &sysfs_file_operations;
 257                break;
 258        case SYSFS_KOBJ_BIN_ATTR:
 259                bin_attr = sd->s_bin_attr.bin_attr;
 260                inode->i_size = bin_attr->size;
 261                inode->i_fop = &bin_fops;
 262                break;
 263        case SYSFS_KOBJ_LINK:
 264                inode->i_op = &sysfs_symlink_inode_operations;
 265                break;
 266        default:
 267                BUG();
 268        }
 269
 270        unlock_new_inode(inode);
 271}
 272
 273/**
 274 *      sysfs_get_inode - get inode for sysfs_dirent
 275 *      @sb: super block
 276 *      @sd: sysfs_dirent to allocate inode for
 277 *
 278 *      Get inode for @sd.  If such inode doesn't exist, a new inode
 279 *      is allocated and basics are initialized.  New inode is
 280 *      returned locked.
 281 *
 282 *      LOCKING:
 283 *      Kernel thread context (may sleep).
 284 *
 285 *      RETURNS:
 286 *      Pointer to allocated inode on success, NULL on failure.
 287 */
 288struct inode * sysfs_get_inode(struct super_block *sb, struct sysfs_dirent *sd)
 289{
 290        struct inode *inode;
 291
 292        inode = iget_locked(sb, sd->s_ino);
 293        if (inode && (inode->i_state & I_NEW))
 294                sysfs_init_inode(sd, inode);
 295
 296        return inode;
 297}
 298
 299/*
 300 * The sysfs_dirent serves as both an inode and a directory entry for sysfs.
 301 * To prevent the sysfs inode numbers from being freed prematurely we take a
 302 * reference to sysfs_dirent from the sysfs inode.  A
 303 * super_operations.evict_inode() implementation is needed to drop that
 304 * reference upon inode destruction.
 305 */
 306void sysfs_evict_inode(struct inode *inode)
 307{
 308        struct sysfs_dirent *sd  = inode->i_private;
 309
 310        truncate_inode_pages(&inode->i_data, 0);
 311        clear_inode(inode);
 312        sysfs_put(sd);
 313}
 314
 315int sysfs_hash_and_remove(struct sysfs_dirent *dir_sd, const void *ns, const char *name)
 316{
 317        struct sysfs_addrm_cxt acxt;
 318        struct sysfs_dirent *sd;
 319
 320        if (!dir_sd) {
 321                WARN(1, KERN_WARNING "sysfs: can not remove '%s', no directory\n",
 322                        name);
 323                return -ENOENT;
 324        }
 325
 326        sysfs_addrm_start(&acxt, dir_sd);
 327
 328        sd = sysfs_find_dirent(dir_sd, ns, name);
 329        if (sd)
 330                sysfs_remove_one(&acxt, sd);
 331
 332        sysfs_addrm_finish(&acxt);
 333
 334        if (sd)
 335                return 0;
 336        else
 337                return -ENOENT;
 338}
 339
 340int sysfs_permission(struct inode *inode, int mask)
 341{
 342        struct sysfs_dirent *sd;
 343
 344        if (mask & MAY_NOT_BLOCK)
 345                return -ECHILD;
 346
 347        sd = inode->i_private;
 348
 349        mutex_lock(&sysfs_mutex);
 350        sysfs_refresh_inode(sd, inode);
 351        mutex_unlock(&sysfs_mutex);
 352
 353        return generic_permission(inode, mask);
 354}
 355
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.