Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * file.c - operations for regular (text) files. |
| 3 | */ |
| 4 | |
| 5 | #include <linux/module.h> |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 6 | #include <linux/fsnotify.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include <linux/kobject.h> |
Christoph Hellwig | 5f45f1a | 2005-06-23 00:09:12 -0700 | [diff] [blame] | 8 | #include <linux/namei.h> |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 9 | #include <linux/poll.h> |
Oliver Neukum | 94bebf4 | 2006-12-20 10:52:44 +0100 | [diff] [blame] | 10 | #include <linux/list.h> |
Dave Young | 52e8c20 | 2007-07-26 11:03:54 +0000 | [diff] [blame] | 11 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <asm/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | |
| 14 | #include "sysfs.h" |
| 15 | |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 16 | #define to_sattr(a) container_of(a,struct subsys_attribute, attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
Martin Waitz | 3d41088 | 2005-06-23 22:05:21 -0700 | [diff] [blame] | 18 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | * Subsystem file operations. |
| 20 | * These operations allow subsystems to have files that can be |
| 21 | * read/written. |
| 22 | */ |
| 23 | static ssize_t |
| 24 | subsys_attr_show(struct kobject * kobj, struct attribute * attr, char * page) |
| 25 | { |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 26 | struct kset *kset = to_kset(kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | struct subsys_attribute * sattr = to_sattr(attr); |
Dmitry Torokhov | c76d0ab | 2005-04-29 01:22:00 -0500 | [diff] [blame] | 28 | ssize_t ret = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
| 30 | if (sattr->show) |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 31 | ret = sattr->show(kset, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | return ret; |
| 33 | } |
| 34 | |
| 35 | static ssize_t |
| 36 | subsys_attr_store(struct kobject * kobj, struct attribute * attr, |
| 37 | const char * page, size_t count) |
| 38 | { |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 39 | struct kset *kset = to_kset(kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | struct subsys_attribute * sattr = to_sattr(attr); |
Dmitry Torokhov | c76d0ab | 2005-04-29 01:22:00 -0500 | [diff] [blame] | 41 | ssize_t ret = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | if (sattr->store) |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 44 | ret = sattr->store(kset, page, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | return ret; |
| 46 | } |
| 47 | |
| 48 | static struct sysfs_ops subsys_sysfs_ops = { |
| 49 | .show = subsys_attr_show, |
| 50 | .store = subsys_attr_store, |
| 51 | }; |
| 52 | |
Tejun Heo | 73107cb | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 53 | struct sysfs_buffer { |
| 54 | size_t count; |
| 55 | loff_t pos; |
| 56 | char * page; |
| 57 | struct sysfs_ops * ops; |
Dave Young | 52e8c20 | 2007-07-26 11:03:54 +0000 | [diff] [blame] | 58 | struct mutex mutex; |
Tejun Heo | 73107cb | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 59 | int needs_read_fill; |
| 60 | int event; |
| 61 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
| 63 | /** |
| 64 | * fill_read_buffer - allocate and fill buffer from object. |
| 65 | * @dentry: dentry pointer. |
| 66 | * @buffer: data buffer for file. |
| 67 | * |
| 68 | * Allocate @buffer->page, if it hasn't been already, then call the |
| 69 | * kobject's show() method to fill the buffer with this attribute's |
| 70 | * data. |
Oliver Neukum | 82244b1 | 2007-01-02 08:48:08 +0100 | [diff] [blame] | 71 | * This is called only once, on the file's first read unless an error |
| 72 | * is returned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | */ |
| 74 | static int fill_read_buffer(struct dentry * dentry, struct sysfs_buffer * buffer) |
| 75 | { |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 76 | struct sysfs_dirent *attr_sd = dentry->d_fsdata; |
| 77 | struct kobject *kobj = attr_sd->s_parent->s_elem.dir.kobj; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | struct sysfs_ops * ops = buffer->ops; |
| 79 | int ret = 0; |
| 80 | ssize_t count; |
| 81 | |
| 82 | if (!buffer->page) |
| 83 | buffer->page = (char *) get_zeroed_page(GFP_KERNEL); |
| 84 | if (!buffer->page) |
| 85 | return -ENOMEM; |
| 86 | |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 87 | /* need attr_sd for attr and ops, its parent for kobj */ |
| 88 | if (!sysfs_get_active_two(attr_sd)) |
| 89 | return -ENODEV; |
| 90 | |
| 91 | buffer->event = atomic_read(&attr_sd->s_event); |
| 92 | count = ops->show(kobj, attr_sd->s_elem.attr.attr, buffer->page); |
| 93 | |
| 94 | sysfs_put_active_two(attr_sd); |
| 95 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | BUG_ON(count > (ssize_t)PAGE_SIZE); |
Oliver Neukum | 82244b1 | 2007-01-02 08:48:08 +0100 | [diff] [blame] | 97 | if (count >= 0) { |
| 98 | buffer->needs_read_fill = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | buffer->count = count; |
Oliver Neukum | 82244b1 | 2007-01-02 08:48:08 +0100 | [diff] [blame] | 100 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | ret = count; |
Oliver Neukum | 82244b1 | 2007-01-02 08:48:08 +0100 | [diff] [blame] | 102 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | return ret; |
| 104 | } |
| 105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | /** |
| 107 | * sysfs_read_file - read an attribute. |
| 108 | * @file: file pointer. |
| 109 | * @buf: buffer to fill. |
| 110 | * @count: number of bytes to read. |
| 111 | * @ppos: starting offset in file. |
| 112 | * |
| 113 | * Userspace wants to read an attribute file. The attribute descriptor |
| 114 | * is in the file's ->d_fsdata. The target object is in the directory's |
| 115 | * ->d_fsdata. |
| 116 | * |
| 117 | * We call fill_read_buffer() to allocate and fill the buffer from the |
| 118 | * object's show() method exactly once (if the read is happening from |
| 119 | * the beginning of the file). That should fill the entire buffer with |
| 120 | * all the data the object has to offer for that attribute. |
| 121 | * We then call flush_read_buffer() to copy the buffer to userspace |
| 122 | * in the increments specified. |
| 123 | */ |
| 124 | |
| 125 | static ssize_t |
| 126 | sysfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos) |
| 127 | { |
| 128 | struct sysfs_buffer * buffer = file->private_data; |
| 129 | ssize_t retval = 0; |
| 130 | |
Dave Young | 52e8c20 | 2007-07-26 11:03:54 +0000 | [diff] [blame] | 131 | mutex_lock(&buffer->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | if (buffer->needs_read_fill) { |
Tejun Heo | 73107cb | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 133 | retval = fill_read_buffer(file->f_path.dentry,buffer); |
Alan Stern | e7b0d26 | 2007-03-15 15:51:28 -0400 | [diff] [blame] | 134 | if (retval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | goto out; |
| 136 | } |
Zach Brown | 5c1fdf4 | 2006-10-03 01:16:06 -0700 | [diff] [blame] | 137 | pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n", |
| 138 | __FUNCTION__, count, *ppos, buffer->page); |
Akinobu Mita | 92f4c70 | 2007-05-09 02:33:32 -0700 | [diff] [blame] | 139 | retval = simple_read_from_buffer(buf, count, ppos, buffer->page, |
| 140 | buffer->count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | out: |
Dave Young | 52e8c20 | 2007-07-26 11:03:54 +0000 | [diff] [blame] | 142 | mutex_unlock(&buffer->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | return retval; |
| 144 | } |
| 145 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | /** |
| 147 | * fill_write_buffer - copy buffer from userspace. |
| 148 | * @buffer: data buffer for file. |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 149 | * @buf: data from user. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | * @count: number of bytes in @userbuf. |
| 151 | * |
| 152 | * Allocate @buffer->page if it hasn't been already, then |
| 153 | * copy the user-supplied buffer into it. |
| 154 | */ |
| 155 | |
| 156 | static int |
| 157 | fill_write_buffer(struct sysfs_buffer * buffer, const char __user * buf, size_t count) |
| 158 | { |
| 159 | int error; |
| 160 | |
| 161 | if (!buffer->page) |
| 162 | buffer->page = (char *)get_zeroed_page(GFP_KERNEL); |
| 163 | if (!buffer->page) |
| 164 | return -ENOMEM; |
| 165 | |
| 166 | if (count >= PAGE_SIZE) |
Greg Kroah-Hartman | 6e0dd74 | 2006-03-31 15:37:06 -0800 | [diff] [blame] | 167 | count = PAGE_SIZE - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | error = copy_from_user(buffer->page,buf,count); |
| 169 | buffer->needs_read_fill = 1; |
Thomas Maier | 035ed7a | 2006-10-22 19:17:47 +0200 | [diff] [blame] | 170 | /* if buf is assumed to contain a string, terminate it by \0, |
| 171 | so e.g. sscanf() can scan the string easily */ |
| 172 | buffer->page[count] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | return error ? -EFAULT : count; |
| 174 | } |
| 175 | |
| 176 | |
| 177 | /** |
| 178 | * flush_write_buffer - push buffer to kobject. |
Martin Waitz | 3d41088 | 2005-06-23 22:05:21 -0700 | [diff] [blame] | 179 | * @dentry: dentry to the attribute |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | * @buffer: data buffer for file. |
Martin Waitz | 3d41088 | 2005-06-23 22:05:21 -0700 | [diff] [blame] | 181 | * @count: number of bytes |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | * |
| 183 | * Get the correct pointers for the kobject and the attribute we're |
| 184 | * dealing with, then call the store() method for the attribute, |
| 185 | * passing the buffer that we acquired in fill_write_buffer(). |
| 186 | */ |
| 187 | |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 188 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | flush_write_buffer(struct dentry * dentry, struct sysfs_buffer * buffer, size_t count) |
| 190 | { |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 191 | struct sysfs_dirent *attr_sd = dentry->d_fsdata; |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 192 | struct kobject *kobj = attr_sd->s_parent->s_elem.dir.kobj; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | struct sysfs_ops * ops = buffer->ops; |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 194 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 196 | /* need attr_sd for attr and ops, its parent for kobj */ |
| 197 | if (!sysfs_get_active_two(attr_sd)) |
| 198 | return -ENODEV; |
| 199 | |
| 200 | rc = ops->store(kobj, attr_sd->s_elem.attr.attr, buffer->page, count); |
| 201 | |
| 202 | sysfs_put_active_two(attr_sd); |
| 203 | |
| 204 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | |
| 208 | /** |
| 209 | * sysfs_write_file - write an attribute. |
| 210 | * @file: file pointer |
| 211 | * @buf: data to write |
| 212 | * @count: number of bytes |
| 213 | * @ppos: starting offset |
| 214 | * |
| 215 | * Similar to sysfs_read_file(), though working in the opposite direction. |
| 216 | * We allocate and fill the data from the user in fill_write_buffer(), |
| 217 | * then push it to the kobject in flush_write_buffer(). |
| 218 | * There is no easy way for us to know if userspace is only doing a partial |
| 219 | * write, so we don't support them. We expect the entire buffer to come |
| 220 | * on the first write. |
| 221 | * Hint: if you're writing a value, first read the file, modify only the |
| 222 | * the value you're changing, then write entire buffer back. |
| 223 | */ |
| 224 | |
| 225 | static ssize_t |
| 226 | sysfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos) |
| 227 | { |
| 228 | struct sysfs_buffer * buffer = file->private_data; |
| 229 | ssize_t len; |
| 230 | |
Dave Young | 52e8c20 | 2007-07-26 11:03:54 +0000 | [diff] [blame] | 231 | mutex_lock(&buffer->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | len = fill_write_buffer(buffer, buf, count); |
| 233 | if (len > 0) |
Josef "Jeff" Sipek | f427f5d | 2006-12-08 02:36:36 -0800 | [diff] [blame] | 234 | len = flush_write_buffer(file->f_path.dentry, buffer, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | if (len > 0) |
| 236 | *ppos += len; |
Dave Young | 52e8c20 | 2007-07-26 11:03:54 +0000 | [diff] [blame] | 237 | mutex_unlock(&buffer->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | return len; |
| 239 | } |
| 240 | |
Oliver Neukum | 94bebf4 | 2006-12-20 10:52:44 +0100 | [diff] [blame] | 241 | static int sysfs_open_file(struct inode *inode, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | { |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 243 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 244 | struct kobject *kobj = attr_sd->s_parent->s_elem.dir.kobj; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | struct sysfs_buffer * buffer; |
| 246 | struct sysfs_ops * ops = NULL; |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 247 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 249 | /* need attr_sd for attr and ops, its parent for kobj */ |
| 250 | if (!sysfs_get_active_two(attr_sd)) |
| 251 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | /* if the kobject has no ktype, then we assume that it is a subsystem |
| 254 | * itself, and use ops for it. |
| 255 | */ |
| 256 | if (kobj->kset && kobj->kset->ktype) |
| 257 | ops = kobj->kset->ktype->sysfs_ops; |
| 258 | else if (kobj->ktype) |
| 259 | ops = kobj->ktype->sysfs_ops; |
| 260 | else |
| 261 | ops = &subsys_sysfs_ops; |
| 262 | |
Tejun Heo | 73107cb | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 263 | error = -EACCES; |
| 264 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | /* No sysfs operations, either from having no subsystem, |
| 266 | * or the subsystem have no operations. |
| 267 | */ |
| 268 | if (!ops) |
Tejun Heo | 7b59575 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 269 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | |
| 271 | /* File needs write support. |
| 272 | * The inode's perms must say it's ok, |
| 273 | * and we must have a store method. |
| 274 | */ |
| 275 | if (file->f_mode & FMODE_WRITE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | if (!(inode->i_mode & S_IWUGO) || !ops->store) |
Tejun Heo | 7b59575 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 277 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | /* File needs read support. |
| 281 | * The inode's perms must say it's ok, and we there |
| 282 | * must be a show method for it. |
| 283 | */ |
| 284 | if (file->f_mode & FMODE_READ) { |
| 285 | if (!(inode->i_mode & S_IRUGO) || !ops->show) |
Tejun Heo | 7b59575 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 286 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | /* No error? Great, allocate a buffer for the file, and store it |
| 290 | * it in file->private_data for easy access. |
| 291 | */ |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 292 | error = -ENOMEM; |
Eric Sesterhenn | 58d4928 | 2006-02-22 11:18:15 +0100 | [diff] [blame] | 293 | buffer = kzalloc(sizeof(struct sysfs_buffer), GFP_KERNEL); |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 294 | if (!buffer) |
Tejun Heo | 7b59575 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 295 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | |
Dave Young | 52e8c20 | 2007-07-26 11:03:54 +0000 | [diff] [blame] | 297 | mutex_init(&buffer->mutex); |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 298 | buffer->needs_read_fill = 1; |
| 299 | buffer->ops = ops; |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 300 | file->private_data = buffer; |
| 301 | |
| 302 | /* open succeeded, put active references and pin attr_sd */ |
| 303 | sysfs_put_active_two(attr_sd); |
| 304 | sysfs_get(attr_sd); |
| 305 | return 0; |
| 306 | |
Tejun Heo | 7b59575 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 307 | err_out: |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 308 | sysfs_put_active_two(attr_sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | return error; |
| 310 | } |
| 311 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | static int sysfs_release(struct inode * inode, struct file * filp) |
| 313 | { |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 314 | struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata; |
Tejun Heo | 73107cb | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 315 | struct sysfs_buffer *buffer = filp->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 317 | sysfs_put(attr_sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | |
| 319 | if (buffer) { |
| 320 | if (buffer->page) |
| 321 | free_page((unsigned long)buffer->page); |
| 322 | kfree(buffer); |
| 323 | } |
| 324 | return 0; |
| 325 | } |
| 326 | |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 327 | /* Sysfs attribute files are pollable. The idea is that you read |
| 328 | * the content and then you use 'poll' or 'select' to wait for |
| 329 | * the content to change. When the content changes (assuming the |
| 330 | * manager for the kobject supports notification), poll will |
| 331 | * return POLLERR|POLLPRI, and select will return the fd whether |
| 332 | * it is waiting for read, write, or exceptions. |
| 333 | * Once poll/select indicates that the value has changed, you |
| 334 | * need to close and re-open the file, as simply seeking and reading |
| 335 | * again will not get new data, or reset the state of 'poll'. |
| 336 | * Reminder: this only works for attributes which actively support |
| 337 | * it, and it is not possible to test an attribute from userspace |
Rolf Eike Beer | a93720e | 2007-08-10 13:51:07 -0700 | [diff] [blame] | 338 | * to see if it supports poll (Neither 'poll' nor 'select' return |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 339 | * an appropriate error code). When in doubt, set a suitable timeout value. |
| 340 | */ |
| 341 | static unsigned int sysfs_poll(struct file *filp, poll_table *wait) |
| 342 | { |
| 343 | struct sysfs_buffer * buffer = filp->private_data; |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 344 | struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata; |
| 345 | struct kobject *kobj = attr_sd->s_parent->s_elem.dir.kobj; |
| 346 | |
| 347 | /* need parent for the kobj, grab both */ |
| 348 | if (!sysfs_get_active_two(attr_sd)) |
| 349 | goto trigger; |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 350 | |
| 351 | poll_wait(filp, &kobj->poll, wait); |
| 352 | |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 353 | sysfs_put_active_two(attr_sd); |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 354 | |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 355 | if (buffer->event != atomic_read(&attr_sd->s_event)) |
| 356 | goto trigger; |
| 357 | |
| 358 | return 0; |
| 359 | |
| 360 | trigger: |
| 361 | buffer->needs_read_fill = 1; |
| 362 | return POLLERR|POLLPRI; |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 363 | } |
| 364 | |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 365 | void sysfs_notify(struct kobject *k, char *dir, char *attr) |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 366 | { |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 367 | struct sysfs_dirent *sd = k->sd; |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 368 | |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 369 | mutex_lock(&sysfs_mutex); |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 370 | |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 371 | if (sd && dir) |
| 372 | sd = sysfs_find_dirent(sd, dir); |
| 373 | if (sd && attr) |
| 374 | sd = sysfs_find_dirent(sd, attr); |
| 375 | if (sd) { |
| 376 | atomic_inc(&sd->s_event); |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 377 | wake_up_interruptible(&k->poll); |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 378 | } |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 379 | |
| 380 | mutex_unlock(&sysfs_mutex); |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 381 | } |
| 382 | EXPORT_SYMBOL_GPL(sysfs_notify); |
| 383 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 384 | const struct file_operations sysfs_file_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | .read = sysfs_read_file, |
| 386 | .write = sysfs_write_file, |
| 387 | .llseek = generic_file_llseek, |
| 388 | .open = sysfs_open_file, |
| 389 | .release = sysfs_release, |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 390 | .poll = sysfs_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | }; |
| 392 | |
| 393 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 394 | int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr, |
| 395 | int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | umode_t mode = (attr->mode & S_IALLUGO) | S_IFREG; |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 398 | struct sysfs_addrm_cxt acxt; |
Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 399 | struct sysfs_dirent *sd; |
Tejun Heo | 23dc279 | 2007-08-02 21:38:03 +0900 | [diff] [blame] | 400 | int rc; |
Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 401 | |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 402 | sd = sysfs_new_dirent(attr->name, mode, type); |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 403 | if (!sd) |
| 404 | return -ENOMEM; |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 405 | sd->s_elem.attr.attr = (void *)attr; |
Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 406 | |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 407 | sysfs_addrm_start(&acxt, dir_sd); |
Tejun Heo | 23dc279 | 2007-08-02 21:38:03 +0900 | [diff] [blame] | 408 | rc = sysfs_add_one(&acxt, sd); |
| 409 | sysfs_addrm_finish(&acxt); |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 410 | |
Tejun Heo | 23dc279 | 2007-08-02 21:38:03 +0900 | [diff] [blame] | 411 | if (rc) |
Tejun Heo | 967e35d | 2007-07-18 16:38:11 +0900 | [diff] [blame] | 412 | sysfs_put(sd); |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 413 | |
Tejun Heo | 23dc279 | 2007-08-02 21:38:03 +0900 | [diff] [blame] | 414 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | |
| 418 | /** |
| 419 | * sysfs_create_file - create an attribute file for an object. |
| 420 | * @kobj: object we're creating for. |
| 421 | * @attr: atrribute descriptor. |
| 422 | */ |
| 423 | |
| 424 | int sysfs_create_file(struct kobject * kobj, const struct attribute * attr) |
| 425 | { |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 426 | BUG_ON(!kobj || !kobj->sd || !attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 428 | return sysfs_add_file(kobj->sd, attr, SYSFS_KOBJ_ATTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | |
| 430 | } |
| 431 | |
| 432 | |
| 433 | /** |
Alan Stern | dfa87c8 | 2007-02-20 15:02:44 -0500 | [diff] [blame] | 434 | * sysfs_add_file_to_group - add an attribute file to a pre-existing group. |
| 435 | * @kobj: object we're acting for. |
| 436 | * @attr: attribute descriptor. |
| 437 | * @group: group name. |
| 438 | */ |
| 439 | int sysfs_add_file_to_group(struct kobject *kobj, |
| 440 | const struct attribute *attr, const char *group) |
| 441 | { |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 442 | struct sysfs_dirent *dir_sd; |
Alan Stern | dfa87c8 | 2007-02-20 15:02:44 -0500 | [diff] [blame] | 443 | int error; |
| 444 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 445 | dir_sd = sysfs_get_dirent(kobj->sd, group); |
| 446 | if (!dir_sd) |
| 447 | return -ENOENT; |
| 448 | |
| 449 | error = sysfs_add_file(dir_sd, attr, SYSFS_KOBJ_ATTR); |
| 450 | sysfs_put(dir_sd); |
| 451 | |
Alan Stern | dfa87c8 | 2007-02-20 15:02:44 -0500 | [diff] [blame] | 452 | return error; |
| 453 | } |
| 454 | EXPORT_SYMBOL_GPL(sysfs_add_file_to_group); |
| 455 | |
| 456 | |
| 457 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | * sysfs_update_file - update the modified timestamp on an object attribute. |
| 459 | * @kobj: object we're acting for. |
| 460 | * @attr: attribute descriptor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | */ |
| 462 | int sysfs_update_file(struct kobject * kobj, const struct attribute * attr) |
| 463 | { |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 464 | struct sysfs_dirent *victim_sd = NULL; |
| 465 | struct dentry *victim = NULL; |
| 466 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 468 | rc = -ENOENT; |
| 469 | victim_sd = sysfs_get_dirent(kobj->sd, attr->name); |
| 470 | if (!victim_sd) |
| 471 | goto out; |
| 472 | |
Eric W. Biederman | 932ea2e | 2007-08-20 21:36:30 +0900 | [diff] [blame] | 473 | mutex_lock(&sysfs_rename_mutex); |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 474 | victim = sysfs_get_dentry(victim_sd); |
Eric W. Biederman | 932ea2e | 2007-08-20 21:36:30 +0900 | [diff] [blame] | 475 | mutex_unlock(&sysfs_rename_mutex); |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 476 | if (IS_ERR(victim)) { |
| 477 | rc = PTR_ERR(victim); |
| 478 | victim = NULL; |
| 479 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 482 | mutex_lock(&victim->d_inode->i_mutex); |
| 483 | victim->d_inode->i_mtime = CURRENT_TIME; |
| 484 | fsnotify_modify(victim); |
| 485 | mutex_unlock(&victim->d_inode->i_mutex); |
| 486 | rc = 0; |
| 487 | out: |
| 488 | dput(victim); |
| 489 | sysfs_put(victim_sd); |
| 490 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | |
| 494 | /** |
Kay Sievers | 31e5abe | 2005-04-18 21:57:32 -0700 | [diff] [blame] | 495 | * sysfs_chmod_file - update the modified mode value on an object attribute. |
| 496 | * @kobj: object we're acting for. |
| 497 | * @attr: attribute descriptor. |
| 498 | * @mode: file permissions. |
| 499 | * |
| 500 | */ |
| 501 | int sysfs_chmod_file(struct kobject *kobj, struct attribute *attr, mode_t mode) |
| 502 | { |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 503 | struct sysfs_dirent *victim_sd = NULL; |
| 504 | struct dentry *victim = NULL; |
Maneesh Soni | bc062b1 | 2005-07-29 12:13:35 -0700 | [diff] [blame] | 505 | struct inode * inode; |
| 506 | struct iattr newattrs; |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 507 | int rc; |
Kay Sievers | 31e5abe | 2005-04-18 21:57:32 -0700 | [diff] [blame] | 508 | |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 509 | rc = -ENOENT; |
| 510 | victim_sd = sysfs_get_dirent(kobj->sd, attr->name); |
| 511 | if (!victim_sd) |
| 512 | goto out; |
| 513 | |
Eric W. Biederman | 932ea2e | 2007-08-20 21:36:30 +0900 | [diff] [blame] | 514 | mutex_lock(&sysfs_rename_mutex); |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 515 | victim = sysfs_get_dentry(victim_sd); |
Eric W. Biederman | 932ea2e | 2007-08-20 21:36:30 +0900 | [diff] [blame] | 516 | mutex_unlock(&sysfs_rename_mutex); |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 517 | if (IS_ERR(victim)) { |
| 518 | rc = PTR_ERR(victim); |
| 519 | victim = NULL; |
| 520 | goto out; |
Kay Sievers | 31e5abe | 2005-04-18 21:57:32 -0700 | [diff] [blame] | 521 | } |
Kay Sievers | 31e5abe | 2005-04-18 21:57:32 -0700 | [diff] [blame] | 522 | |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 523 | inode = victim->d_inode; |
Tejun Heo | f88123e | 2007-09-20 16:05:10 +0900 | [diff] [blame^] | 524 | |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 525 | mutex_lock(&inode->i_mutex); |
Tejun Heo | f88123e | 2007-09-20 16:05:10 +0900 | [diff] [blame^] | 526 | |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 527 | newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO); |
| 528 | newattrs.ia_valid = ATTR_MODE | ATTR_CTIME; |
| 529 | rc = notify_change(victim, &newattrs); |
Tejun Heo | f88123e | 2007-09-20 16:05:10 +0900 | [diff] [blame^] | 530 | |
| 531 | if (rc == 0) { |
| 532 | mutex_lock(&sysfs_mutex); |
| 533 | victim_sd->s_mode = newattrs.ia_mode; |
| 534 | mutex_unlock(&sysfs_mutex); |
| 535 | } |
| 536 | |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 537 | mutex_unlock(&inode->i_mutex); |
| 538 | out: |
| 539 | dput(victim); |
| 540 | sysfs_put(victim_sd); |
| 541 | return rc; |
Kay Sievers | 31e5abe | 2005-04-18 21:57:32 -0700 | [diff] [blame] | 542 | } |
| 543 | EXPORT_SYMBOL_GPL(sysfs_chmod_file); |
| 544 | |
| 545 | |
| 546 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | * sysfs_remove_file - remove an object attribute. |
| 548 | * @kobj: object we're acting for. |
| 549 | * @attr: attribute descriptor. |
| 550 | * |
| 551 | * Hash the attribute name and kill the victim. |
| 552 | */ |
| 553 | |
| 554 | void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr) |
| 555 | { |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 556 | sysfs_hash_and_remove(kobj->sd, attr->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | |
Alan Stern | dfa87c8 | 2007-02-20 15:02:44 -0500 | [diff] [blame] | 560 | /** |
| 561 | * sysfs_remove_file_from_group - remove an attribute file from a group. |
| 562 | * @kobj: object we're acting for. |
| 563 | * @attr: attribute descriptor. |
| 564 | * @group: group name. |
| 565 | */ |
| 566 | void sysfs_remove_file_from_group(struct kobject *kobj, |
| 567 | const struct attribute *attr, const char *group) |
| 568 | { |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 569 | struct sysfs_dirent *dir_sd; |
Alan Stern | dfa87c8 | 2007-02-20 15:02:44 -0500 | [diff] [blame] | 570 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 571 | dir_sd = sysfs_get_dirent(kobj->sd, group); |
| 572 | if (dir_sd) { |
| 573 | sysfs_hash_and_remove(dir_sd, attr->name); |
| 574 | sysfs_put(dir_sd); |
Alan Stern | dfa87c8 | 2007-02-20 15:02:44 -0500 | [diff] [blame] | 575 | } |
| 576 | } |
| 577 | EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group); |
| 578 | |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 579 | struct sysfs_schedule_callback_struct { |
| 580 | struct kobject *kobj; |
| 581 | void (*func)(void *); |
| 582 | void *data; |
Alan Stern | 523ded7 | 2007-04-26 00:12:04 -0700 | [diff] [blame] | 583 | struct module *owner; |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 584 | struct work_struct work; |
| 585 | }; |
| 586 | |
| 587 | static void sysfs_schedule_callback_work(struct work_struct *work) |
| 588 | { |
| 589 | struct sysfs_schedule_callback_struct *ss = container_of(work, |
| 590 | struct sysfs_schedule_callback_struct, work); |
| 591 | |
| 592 | (ss->func)(ss->data); |
| 593 | kobject_put(ss->kobj); |
Alan Stern | 523ded7 | 2007-04-26 00:12:04 -0700 | [diff] [blame] | 594 | module_put(ss->owner); |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 595 | kfree(ss); |
| 596 | } |
| 597 | |
| 598 | /** |
| 599 | * sysfs_schedule_callback - helper to schedule a callback for a kobject |
| 600 | * @kobj: object we're acting for. |
| 601 | * @func: callback function to invoke later. |
| 602 | * @data: argument to pass to @func. |
Alan Stern | 523ded7 | 2007-04-26 00:12:04 -0700 | [diff] [blame] | 603 | * @owner: module owning the callback code |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 604 | * |
| 605 | * sysfs attribute methods must not unregister themselves or their parent |
| 606 | * kobject (which would amount to the same thing). Attempts to do so will |
| 607 | * deadlock, since unregistration is mutually exclusive with driver |
| 608 | * callbacks. |
| 609 | * |
| 610 | * Instead methods can call this routine, which will attempt to allocate |
| 611 | * and schedule a workqueue request to call back @func with @data as its |
| 612 | * argument in the workqueue's process context. @kobj will be pinned |
| 613 | * until @func returns. |
| 614 | * |
| 615 | * Returns 0 if the request was submitted, -ENOMEM if storage could not |
Alan Stern | 523ded7 | 2007-04-26 00:12:04 -0700 | [diff] [blame] | 616 | * be allocated, -ENODEV if a reference to @owner isn't available. |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 617 | */ |
| 618 | int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *), |
Alan Stern | 523ded7 | 2007-04-26 00:12:04 -0700 | [diff] [blame] | 619 | void *data, struct module *owner) |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 620 | { |
| 621 | struct sysfs_schedule_callback_struct *ss; |
| 622 | |
Alan Stern | 523ded7 | 2007-04-26 00:12:04 -0700 | [diff] [blame] | 623 | if (!try_module_get(owner)) |
| 624 | return -ENODEV; |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 625 | ss = kmalloc(sizeof(*ss), GFP_KERNEL); |
Alan Stern | 523ded7 | 2007-04-26 00:12:04 -0700 | [diff] [blame] | 626 | if (!ss) { |
| 627 | module_put(owner); |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 628 | return -ENOMEM; |
Alan Stern | 523ded7 | 2007-04-26 00:12:04 -0700 | [diff] [blame] | 629 | } |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 630 | kobject_get(kobj); |
| 631 | ss->kobj = kobj; |
| 632 | ss->func = func; |
| 633 | ss->data = data; |
Alan Stern | 523ded7 | 2007-04-26 00:12:04 -0700 | [diff] [blame] | 634 | ss->owner = owner; |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 635 | INIT_WORK(&ss->work, sysfs_schedule_callback_work); |
| 636 | schedule_work(&ss->work); |
| 637 | return 0; |
| 638 | } |
| 639 | EXPORT_SYMBOL_GPL(sysfs_schedule_callback); |
| 640 | |
Alan Stern | dfa87c8 | 2007-02-20 15:02:44 -0500 | [diff] [blame] | 641 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | EXPORT_SYMBOL_GPL(sysfs_create_file); |
| 643 | EXPORT_SYMBOL_GPL(sysfs_remove_file); |
| 644 | EXPORT_SYMBOL_GPL(sysfs_update_file); |