1219820Sjeff/*- 2219820Sjeff * Copyright (c) 2010 Isilon Systems, Inc. 3219820Sjeff * Copyright (c) 2010 iX Systems, Inc. 4219820Sjeff * Copyright (c) 2010 Panasas, Inc. 5219820Sjeff * All rights reserved. 6219820Sjeff * 7219820Sjeff * Redistribution and use in source and binary forms, with or without 8219820Sjeff * modification, are permitted provided that the following conditions 9219820Sjeff * are met: 10219820Sjeff * 1. Redistributions of source code must retain the above copyright 11219820Sjeff * notice unmodified, this list of conditions, and the following 12219820Sjeff * disclaimer. 13219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright 14219820Sjeff * notice, this list of conditions and the following disclaimer in the 15219820Sjeff * documentation and/or other materials provided with the distribution. 16219820Sjeff * 17219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18219820Sjeff * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19219820Sjeff * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20219820Sjeff * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21219820Sjeff * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22219820Sjeff * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23219820Sjeff * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24219820Sjeff * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25219820Sjeff * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26219820Sjeff * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27219820Sjeff */ 28219820Sjeff#ifndef _LINUX_KOBJECT_H_ 29219820Sjeff#define _LINUX_KOBJECT_H_ 30219820Sjeff 31219820Sjeff#include <machine/stdarg.h> 32219820Sjeff 33219820Sjeff#include <linux/kernel.h> 34219820Sjeff#include <linux/kref.h> 35219820Sjeff#include <linux/slab.h> 36219820Sjeff 37219820Sjeffstruct kobject; 38219820Sjeffstruct sysctl_oid; 39219820Sjeff 40219820Sjeffstruct kobj_type { 41219820Sjeff void (*release)(struct kobject *kobj); 42219820Sjeff const struct sysfs_ops *sysfs_ops; 43219820Sjeff struct attribute **default_attrs; 44219820Sjeff}; 45219820Sjeff 46219820Sjeffextern struct kobj_type kfree_type; 47219820Sjeff 48219820Sjeffstruct kobject { 49219820Sjeff struct kobject *parent; 50219820Sjeff char *name; 51219820Sjeff struct kref kref; 52219820Sjeff struct kobj_type *ktype; 53219820Sjeff struct list_head entry; 54219820Sjeff struct sysctl_oid *oidp; 55219820Sjeff}; 56219820Sjeff 57219820Sjeffstatic inline void 58219820Sjeffkobject_init(struct kobject *kobj, struct kobj_type *ktype) 59219820Sjeff{ 60219820Sjeff 61219820Sjeff kref_init(&kobj->kref); 62219820Sjeff INIT_LIST_HEAD(&kobj->entry); 63219820Sjeff kobj->ktype = ktype; 64219820Sjeff kobj->oidp = NULL; 65219820Sjeff} 66219820Sjeff 67219820Sjeffstatic inline void kobject_put(struct kobject *kobj); 68219820Sjeffvoid kobject_release(struct kref *kref); 69219820Sjeff 70219820Sjeffstatic inline void 71219820Sjeffkobject_put(struct kobject *kobj) 72219820Sjeff{ 73219820Sjeff 74219820Sjeff if (kobj) 75219820Sjeff kref_put(&kobj->kref, kobject_release); 76219820Sjeff} 77219820Sjeff 78219820Sjeffstatic inline struct kobject * 79219820Sjeffkobject_get(struct kobject *kobj) 80219820Sjeff{ 81219820Sjeff 82219820Sjeff if (kobj) 83219820Sjeff kref_get(&kobj->kref); 84219820Sjeff return kobj; 85219820Sjeff} 86219820Sjeff 87219820Sjeffstatic inline int 88219820Sjeffkobject_set_name_vargs(struct kobject *kobj, const char *fmt, va_list args) 89219820Sjeff{ 90219820Sjeff char *old; 91219820Sjeff char *name; 92219820Sjeff 93219820Sjeff old = kobj->name; 94219820Sjeff 95219820Sjeff if (old && !fmt) 96219820Sjeff return 0; 97219820Sjeff 98219820Sjeff name = kzalloc(MAXPATHLEN, GFP_KERNEL); 99219820Sjeff if (!name) 100219820Sjeff return -ENOMEM; 101219820Sjeff vsnprintf(name, MAXPATHLEN, fmt, args); 102219820Sjeff kobj->name = name; 103219820Sjeff kfree(old); 104219820Sjeff for (; *name != '\0'; name++) 105219820Sjeff if (*name == '/') 106219820Sjeff *name = '!'; 107219820Sjeff return (0); 108219820Sjeff} 109219820Sjeff 110219820Sjeffint kobject_add(struct kobject *kobj, struct kobject *parent, 111219820Sjeff const char *fmt, ...); 112219820Sjeff 113219820Sjeffstatic inline struct kobject * 114219820Sjeffkobject_create(void) 115219820Sjeff{ 116219820Sjeff struct kobject *kobj; 117219820Sjeff 118219820Sjeff kobj = kzalloc(sizeof(*kobj), GFP_KERNEL); 119219820Sjeff if (kobj == NULL) 120219820Sjeff return (NULL); 121219820Sjeff kobject_init(kobj, &kfree_type); 122219820Sjeff 123219820Sjeff return (kobj); 124219820Sjeff} 125219820Sjeff 126219820Sjeffstatic inline struct kobject * 127219820Sjeffkobject_create_and_add(const char *name, struct kobject *parent) 128219820Sjeff{ 129219820Sjeff struct kobject *kobj; 130219820Sjeff 131219820Sjeff kobj = kobject_create(); 132219820Sjeff if (kobj == NULL) 133219820Sjeff return (NULL); 134219820Sjeff if (kobject_add(kobj, parent, "%s", name) == 0) 135219820Sjeff return (kobj); 136219820Sjeff kobject_put(kobj); 137219820Sjeff 138219820Sjeff return (NULL); 139219820Sjeff} 140219820Sjeff 141219820Sjeff 142219820Sjeffstatic inline char * 143219820Sjeffkobject_name(const struct kobject *kobj) 144219820Sjeff{ 145219820Sjeff 146219820Sjeff return kobj->name; 147219820Sjeff} 148219820Sjeff 149219820Sjeffint kobject_set_name(struct kobject *kobj, const char *fmt, ...); 150219820Sjeffint kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype, 151219820Sjeff struct kobject *parent, const char *fmt, ...); 152219820Sjeff 153219820Sjeff#endif /* _LINUX_KOBJECT_H_ */ 154