kobject.h revision 219820
1/*- 2 * Copyright (c) 2010 Isilon Systems, Inc. 3 * Copyright (c) 2010 iX Systems, Inc. 4 * Copyright (c) 2010 Panasas, Inc. 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 unmodified, this list of conditions, and the following 12 * disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28#ifndef _LINUX_KOBJECT_H_ 29#define _LINUX_KOBJECT_H_ 30 31#include <machine/stdarg.h> 32 33#include <linux/kernel.h> 34#include <linux/kref.h> 35#include <linux/slab.h> 36 37struct kobject; 38struct sysctl_oid; 39 40struct kobj_type { 41 void (*release)(struct kobject *kobj); 42 const struct sysfs_ops *sysfs_ops; 43 struct attribute **default_attrs; 44}; 45 46extern struct kobj_type kfree_type; 47 48struct kobject { 49 struct kobject *parent; 50 char *name; 51 struct kref kref; 52 struct kobj_type *ktype; 53 struct list_head entry; 54 struct sysctl_oid *oidp; 55}; 56 57static inline void 58kobject_init(struct kobject *kobj, struct kobj_type *ktype) 59{ 60 61 kref_init(&kobj->kref); 62 INIT_LIST_HEAD(&kobj->entry); 63 kobj->ktype = ktype; 64 kobj->oidp = NULL; 65} 66 67static inline void kobject_put(struct kobject *kobj); 68void kobject_release(struct kref *kref); 69 70static inline void 71kobject_put(struct kobject *kobj) 72{ 73 74 if (kobj) 75 kref_put(&kobj->kref, kobject_release); 76} 77 78static inline struct kobject * 79kobject_get(struct kobject *kobj) 80{ 81 82 if (kobj) 83 kref_get(&kobj->kref); 84 return kobj; 85} 86 87static inline int 88kobject_set_name_vargs(struct kobject *kobj, const char *fmt, va_list args) 89{ 90 char *old; 91 char *name; 92 93 old = kobj->name; 94 95 if (old && !fmt) 96 return 0; 97 98 name = kzalloc(MAXPATHLEN, GFP_KERNEL); 99 if (!name) 100 return -ENOMEM; 101 vsnprintf(name, MAXPATHLEN, fmt, args); 102 kobj->name = name; 103 kfree(old); 104 for (; *name != '\0'; name++) 105 if (*name == '/') 106 *name = '!'; 107 return (0); 108} 109 110int kobject_add(struct kobject *kobj, struct kobject *parent, 111 const char *fmt, ...); 112 113static inline struct kobject * 114kobject_create(void) 115{ 116 struct kobject *kobj; 117 118 kobj = kzalloc(sizeof(*kobj), GFP_KERNEL); 119 if (kobj == NULL) 120 return (NULL); 121 kobject_init(kobj, &kfree_type); 122 123 return (kobj); 124} 125 126static inline struct kobject * 127kobject_create_and_add(const char *name, struct kobject *parent) 128{ 129 struct kobject *kobj; 130 131 kobj = kobject_create(); 132 if (kobj == NULL) 133 return (NULL); 134 if (kobject_add(kobj, parent, "%s", name) == 0) 135 return (kobj); 136 kobject_put(kobj); 137 138 return (NULL); 139} 140 141 142static inline char * 143kobject_name(const struct kobject *kobj) 144{ 145 146 return kobj->name; 147} 148 149int kobject_set_name(struct kobject *kobj, const char *fmt, ...); 150int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype, 151 struct kobject *parent, const char *fmt, ...); 152 153#endif /* _LINUX_KOBJECT_H_ */ 154