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