LCOV - code coverage report
Current view: top level - lkbce/fs/configfs - item.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 6 75 8.0 %
Date: 2017-01-25 Functions: 2 11 18.2 %

          Line data    Source code
       1             : /* -*- mode: c; c-basic-offset: 8; -*-
       2             :  * vim: noexpandtab sw=8 ts=8 sts=0:
       3             :  *
       4             :  * item.c - library routines for handling generic config items
       5             :  *
       6             :  * This program is free software; you can redistribute it and/or
       7             :  * modify it under the terms of the GNU General Public
       8             :  * License as published by the Free Software Foundation; either
       9             :  * version 2 of the License, or (at your option) any later version.
      10             :  *
      11             :  * This program is distributed in the hope that it will be useful,
      12             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14             :  * General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU General Public
      17             :  * License along with this program; if not, write to the
      18             :  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
      19             :  * Boston, MA 021110-1307, USA.
      20             :  *
      21             :  * Based on kobject:
      22             :  *      kobject is Copyright (c) 2002-2003 Patrick Mochel
      23             :  *
      24             :  * configfs Copyright (C) 2005 Oracle.  All rights reserved.
      25             :  *
      26             :  * Please see the file Documentation/filesystems/configfs.txt for
      27             :  * critical information about using the config_item interface.
      28             :  */
      29             : 
      30             : #include <linux/string.h>
      31             : #include <linux/module.h>
      32             : #include <linux/stat.h>
      33             : #include <linux/slab.h>
      34             : 
      35             : #include <linux/configfs.h>
      36             : 
      37             : 
      38             : static inline struct config_item * to_item(struct list_head * entry)
      39             : {
      40           0 :         return container_of(entry,struct config_item,ci_entry);
      41             : }
      42             : 
      43             : /* Evil kernel */
      44             : static void config_item_release(struct kref *kref);
      45             : 
      46             : /**
      47             :  *      config_item_init - initialize item.
      48             :  *      @item:  item in question.
      49             :  */
      50             : void config_item_init(struct config_item * item)
      51             : {
      52           0 :         kref_init(&item->ci_kref);
      53           0 :         INIT_LIST_HEAD(&item->ci_entry);
      54           0 : }
      55             : 
      56             : /**
      57             :  *      config_item_set_name - Set the name of an item
      58             :  *      @item:  item.
      59             :  *      @name:  name.
      60             :  *
      61             :  *      If strlen(name) >= CONFIGFS_ITEM_NAME_LEN, then use a
      62             :  *      dynamically allocated string that @item->ci_name points to.
      63             :  *      Otherwise, use the static @item->ci_namebuf array.
      64             :  */
      65             : int config_item_set_name(struct config_item * item, const char * fmt, ...)
      66             : {
      67           0 :         int error = 0;
      68           0 :         int limit = CONFIGFS_ITEM_NAME_LEN;
      69           0 :         int need;
      70           0 :         va_list args;
      71           0 :         char * name;
      72           0 : 
      73             :         /*
      74             :          * First, try the static array
      75             :          */
      76           0 :         va_start(args,fmt);
      77           0 :         need = vsnprintf(item->ci_namebuf,limit,fmt,args);
      78           0 :         va_end(args);
      79           0 :         if (need < limit)
      80           0 :                 name = item->ci_namebuf;
      81             :         else {
      82             :                 /*
      83             :                  * Need more space? Allocate it and try again
      84             :                  */
      85           0 :                 limit = need + 1;
      86           0 :                 name = kmalloc(limit,GFP_KERNEL);
      87           0 :                 if (!name) {
      88           0 :                         error = -ENOMEM;
      89           0 :                         goto Done;
      90             :                 }
      91           0 :                 va_start(args,fmt);
      92           0 :                 need = vsnprintf(name,limit,fmt,args);
      93           0 :                 va_end(args);
      94             : 
      95             :                 /* Still? Give up. */
      96           0 :                 if (need >= limit) {
      97           0 :                         kfree(name);
      98           0 :                         error = -EFAULT;
      99           0 :                         goto Done;
     100             :                 }
     101             :         }
     102             : 
     103             :         /* Free the old name, if necessary. */
     104           0 :         if (item->ci_name && item->ci_name != item->ci_namebuf)
     105           0 :                 kfree(item->ci_name);
     106             : 
     107             :         /* Now, set the new name */
     108           0 :         item->ci_name = name;
     109           0 :  Done:
     110           0 :         return error;
     111             : }
     112             : 
     113             : EXPORT_SYMBOL(config_item_set_name);
     114             : 
     115             : void config_item_init_type_name(struct config_item *item,
     116             :                                 const char *name,
     117             :                                 struct config_item_type *type)
     118             : {
     119           0 :         config_item_set_name(item, name);
     120           0 :         item->ci_type = type;
     121           0 :         config_item_init(item);
     122           0 : }
     123             : EXPORT_SYMBOL(config_item_init_type_name);
     124             : 
     125             : void config_group_init_type_name(struct config_group *group, const char *name,
     126             :                          struct config_item_type *type)
     127             : {
     128           0 :         config_item_set_name(&group->cg_item, name);
     129           0 :         group->cg_item.ci_type = type;
     130           0 :         config_group_init(group);
     131           0 : }
     132             : EXPORT_SYMBOL(config_group_init_type_name);
     133             : 
     134             : struct config_item * config_item_get(struct config_item * item)
     135             : {
     136          46 :         if (item)
     137          23 :                 kref_get(&item->ci_kref);
     138          23 :         return item;
     139             : }
     140             : 
     141             : static void config_item_cleanup(struct config_item * item)
     142             : {
     143           0 :         struct config_item_type * t = item->ci_type;
     144           0 :         struct config_group * s = item->ci_group;
     145           0 :         struct config_item * parent = item->ci_parent;
     146             : 
     147             :         pr_debug("config_item %s: cleaning up\n",config_item_name(item));
     148           0 :         if (item->ci_name != item->ci_namebuf)
     149           0 :                 kfree(item->ci_name);
     150           0 :         item->ci_name = NULL;
     151           0 :         if (t && t->ct_item_ops && t->ct_item_ops->release)
     152           0 :                 t->ct_item_ops->release(item);
     153           0 :         if (s)
     154           0 :                 config_group_put(s);
     155           0 :         if (parent)
     156           0 :                 config_item_put(parent);
     157           0 : }
     158             : 
     159             : static void config_item_release(struct kref *kref)
     160             : {
     161           0 :         config_item_cleanup(container_of(kref, struct config_item, ci_kref));
     162             : }
     163           0 : 
     164             : /**
     165             :  *      config_item_put - decrement refcount for item.
     166             :  *      @item:  item.
     167             :  *
     168             :  *      Decrement the refcount, and if 0, call config_item_cleanup().
     169             :  */
     170             : void config_item_put(struct config_item * item)
     171             : {
     172          98 :         if (item)
     173          49 :                 kref_put(&item->ci_kref, config_item_release);
     174          49 : }
     175             : 
     176             : /**
     177             :  *      config_group_init - initialize a group for use
     178             :  *      @k:     group
     179             :  */
     180             : void config_group_init(struct config_group *group)
     181             : {
     182           0 :         config_item_init(&group->cg_item);
     183           0 :         INIT_LIST_HEAD(&group->cg_children);
     184           0 : }
     185             : 
     186             : /**
     187             :  *      config_group_find_item - search for item in group.
     188             :  *      @group: group we're looking in.
     189             :  *      @name:  item's name.
     190             :  *
     191             :  *      Iterate over @group->cg_list, looking for a matching config_item.
     192             :  *      If matching item is found take a reference and return the item.
     193             :  *      Caller must have locked group via @group->cg_subsys->su_mtx.
     194             :  */
     195             : struct config_item *config_group_find_item(struct config_group *group,
     196             :                                            const char *name)
     197           0 : {
     198           0 :         struct list_head * entry;
     199           0 :         struct config_item * ret = NULL;
     200           0 : 
     201           0 :         list_for_each(entry,&group->cg_children) {
     202           0 :                 struct config_item * item = to_item(entry);
     203           0 :                 if (config_item_name(item) &&
     204             :                     !strcmp(config_item_name(item), name)) {
     205           0 :                         ret = config_item_get(item);
     206           0 :                         break;
     207           0 :                 }
     208             :         }
     209           0 :         return ret;
     210             : }
     211             : 
     212             : EXPORT_SYMBOL(config_item_init);
     213             : EXPORT_SYMBOL(config_group_init);
     214             : EXPORT_SYMBOL(config_item_get);
     215             : EXPORT_SYMBOL(config_item_put);
     216             : EXPORT_SYMBOL(config_group_find_item);

Generated by: LCOV version 1.10