blob: b13ba94cf8ac66d9f2138b1d422784990c8919d5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * file.c - operations for regular (text) files.
3 */
4
5#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/kobject.h>
Christoph Hellwig5f45f1a2005-06-23 00:09:12 -07007#include <linux/namei.h>
NeilBrown4508a7a2006-03-20 17:53:53 +11008#include <linux/poll.h>
Oliver Neukum94bebf42006-12-20 10:52:44 +01009#include <linux/list.h>
Dave Young52e8c202007-07-26 11:03:54 +000010#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
13#include "sysfs.h"
14
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -070015#define to_sattr(a) container_of(a,struct subsys_attribute, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Martin Waitz3d410882005-06-23 22:05:21 -070017/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * Subsystem file operations.
19 * These operations allow subsystems to have files that can be
20 * read/written.
21 */
22static ssize_t
23subsys_attr_show(struct kobject * kobj, struct attribute * attr, char * page)
24{
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -070025 struct kset *kset = to_kset(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 struct subsys_attribute * sattr = to_sattr(attr);
Dmitry Torokhovc76d0ab2005-04-29 01:22:00 -050027 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29 if (sattr->show)
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -070030 ret = sattr->show(kset, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 return ret;
32}
33
34static ssize_t
35subsys_attr_store(struct kobject * kobj, struct attribute * attr,
36 const char * page, size_t count)
37{
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -070038 struct kset *kset = to_kset(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 struct subsys_attribute * sattr = to_sattr(attr);
Dmitry Torokhovc76d0ab2005-04-29 01:22:00 -050040 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42 if (sattr->store)
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -070043 ret = sattr->store(kset, page, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 return ret;
45}
46
47static struct sysfs_ops subsys_sysfs_ops = {
48 .show = subsys_attr_show,
49 .store = subsys_attr_store,
50};
51
Tejun Heo85a4ffa2007-09-20 16:05:12 +090052/*
53 * There's one sysfs_buffer for each open file and one
54 * sysfs_open_dirent for each sysfs_dirent with one or more open
55 * files.
56 *
57 * filp->private_data points to sysfs_buffer and
58 * sysfs_dirent->s_attr.open points to sysfs_open_dirent. s_attr.open
59 * is protected by sysfs_open_dirent_lock.
60 */
61static spinlock_t sysfs_open_dirent_lock = SPIN_LOCK_UNLOCKED;
62
63struct sysfs_open_dirent {
64 atomic_t refcnt;
65 struct list_head buffers; /* goes through sysfs_buffer.list */
66};
67
Tejun Heo73107cb2007-06-14 03:45:16 +090068struct sysfs_buffer {
69 size_t count;
70 loff_t pos;
71 char * page;
72 struct sysfs_ops * ops;
Dave Young52e8c202007-07-26 11:03:54 +000073 struct mutex mutex;
Tejun Heo73107cb2007-06-14 03:45:16 +090074 int needs_read_fill;
75 int event;
Tejun Heo85a4ffa2007-09-20 16:05:12 +090076 struct list_head list;
Tejun Heo73107cb2007-06-14 03:45:16 +090077};
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79/**
80 * fill_read_buffer - allocate and fill buffer from object.
81 * @dentry: dentry pointer.
82 * @buffer: data buffer for file.
83 *
84 * Allocate @buffer->page, if it hasn't been already, then call the
85 * kobject's show() method to fill the buffer with this attribute's
86 * data.
Oliver Neukum82244b12007-01-02 08:48:08 +010087 * This is called only once, on the file's first read unless an error
88 * is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 */
90static int fill_read_buffer(struct dentry * dentry, struct sysfs_buffer * buffer)
91{
Tejun Heo0ab66082007-06-14 03:45:16 +090092 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
Tejun Heob1fc3d62007-09-20 16:05:11 +090093 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 struct sysfs_ops * ops = buffer->ops;
95 int ret = 0;
96 ssize_t count;
97
98 if (!buffer->page)
99 buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
100 if (!buffer->page)
101 return -ENOMEM;
102
Tejun Heo0ab66082007-06-14 03:45:16 +0900103 /* need attr_sd for attr and ops, its parent for kobj */
104 if (!sysfs_get_active_two(attr_sd))
105 return -ENODEV;
106
107 buffer->event = atomic_read(&attr_sd->s_event);
Tejun Heob1fc3d62007-09-20 16:05:11 +0900108 count = ops->show(kobj, attr_sd->s_attr.attr, buffer->page);
Tejun Heo0ab66082007-06-14 03:45:16 +0900109
110 sysfs_put_active_two(attr_sd);
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 BUG_ON(count > (ssize_t)PAGE_SIZE);
Oliver Neukum82244b12007-01-02 08:48:08 +0100113 if (count >= 0) {
114 buffer->needs_read_fill = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 buffer->count = count;
Oliver Neukum82244b12007-01-02 08:48:08 +0100116 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 ret = count;
Oliver Neukum82244b12007-01-02 08:48:08 +0100118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 return ret;
120}
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122/**
123 * sysfs_read_file - read an attribute.
124 * @file: file pointer.
125 * @buf: buffer to fill.
126 * @count: number of bytes to read.
127 * @ppos: starting offset in file.
128 *
129 * Userspace wants to read an attribute file. The attribute descriptor
130 * is in the file's ->d_fsdata. The target object is in the directory's
131 * ->d_fsdata.
132 *
133 * We call fill_read_buffer() to allocate and fill the buffer from the
134 * object's show() method exactly once (if the read is happening from
135 * the beginning of the file). That should fill the entire buffer with
136 * all the data the object has to offer for that attribute.
137 * We then call flush_read_buffer() to copy the buffer to userspace
138 * in the increments specified.
139 */
140
141static ssize_t
142sysfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
143{
144 struct sysfs_buffer * buffer = file->private_data;
145 ssize_t retval = 0;
146
Dave Young52e8c202007-07-26 11:03:54 +0000147 mutex_lock(&buffer->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if (buffer->needs_read_fill) {
Tejun Heo73107cb2007-06-14 03:45:16 +0900149 retval = fill_read_buffer(file->f_path.dentry,buffer);
Alan Sterne7b0d262007-03-15 15:51:28 -0400150 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 goto out;
152 }
Zach Brown5c1fdf42006-10-03 01:16:06 -0700153 pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",
154 __FUNCTION__, count, *ppos, buffer->page);
Akinobu Mita92f4c702007-05-09 02:33:32 -0700155 retval = simple_read_from_buffer(buf, count, ppos, buffer->page,
156 buffer->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157out:
Dave Young52e8c202007-07-26 11:03:54 +0000158 mutex_unlock(&buffer->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 return retval;
160}
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162/**
163 * fill_write_buffer - copy buffer from userspace.
164 * @buffer: data buffer for file.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700165 * @buf: data from user.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 * @count: number of bytes in @userbuf.
167 *
168 * Allocate @buffer->page if it hasn't been already, then
169 * copy the user-supplied buffer into it.
170 */
171
172static int
173fill_write_buffer(struct sysfs_buffer * buffer, const char __user * buf, size_t count)
174{
175 int error;
176
177 if (!buffer->page)
178 buffer->page = (char *)get_zeroed_page(GFP_KERNEL);
179 if (!buffer->page)
180 return -ENOMEM;
181
182 if (count >= PAGE_SIZE)
Greg Kroah-Hartman6e0dd742006-03-31 15:37:06 -0800183 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 error = copy_from_user(buffer->page,buf,count);
185 buffer->needs_read_fill = 1;
Thomas Maier035ed7a2006-10-22 19:17:47 +0200186 /* if buf is assumed to contain a string, terminate it by \0,
187 so e.g. sscanf() can scan the string easily */
188 buffer->page[count] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return error ? -EFAULT : count;
190}
191
192
193/**
194 * flush_write_buffer - push buffer to kobject.
Martin Waitz3d410882005-06-23 22:05:21 -0700195 * @dentry: dentry to the attribute
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 * @buffer: data buffer for file.
Martin Waitz3d410882005-06-23 22:05:21 -0700197 * @count: number of bytes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 *
199 * Get the correct pointers for the kobject and the attribute we're
200 * dealing with, then call the store() method for the attribute,
201 * passing the buffer that we acquired in fill_write_buffer().
202 */
203
Tejun Heo0ab66082007-06-14 03:45:16 +0900204static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205flush_write_buffer(struct dentry * dentry, struct sysfs_buffer * buffer, size_t count)
206{
Tejun Heo3e519032007-06-14 03:45:15 +0900207 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900208 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 struct sysfs_ops * ops = buffer->ops;
Tejun Heo0ab66082007-06-14 03:45:16 +0900210 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Tejun Heo0ab66082007-06-14 03:45:16 +0900212 /* need attr_sd for attr and ops, its parent for kobj */
213 if (!sysfs_get_active_two(attr_sd))
214 return -ENODEV;
215
Tejun Heob1fc3d62007-09-20 16:05:11 +0900216 rc = ops->store(kobj, attr_sd->s_attr.attr, buffer->page, count);
Tejun Heo0ab66082007-06-14 03:45:16 +0900217
218 sysfs_put_active_two(attr_sd);
219
220 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
223
224/**
225 * sysfs_write_file - write an attribute.
226 * @file: file pointer
227 * @buf: data to write
228 * @count: number of bytes
229 * @ppos: starting offset
230 *
231 * Similar to sysfs_read_file(), though working in the opposite direction.
232 * We allocate and fill the data from the user in fill_write_buffer(),
233 * then push it to the kobject in flush_write_buffer().
234 * There is no easy way for us to know if userspace is only doing a partial
235 * write, so we don't support them. We expect the entire buffer to come
236 * on the first write.
237 * Hint: if you're writing a value, first read the file, modify only the
238 * the value you're changing, then write entire buffer back.
239 */
240
241static ssize_t
242sysfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
243{
244 struct sysfs_buffer * buffer = file->private_data;
245 ssize_t len;
246
Dave Young52e8c202007-07-26 11:03:54 +0000247 mutex_lock(&buffer->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 len = fill_write_buffer(buffer, buf, count);
249 if (len > 0)
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800250 len = flush_write_buffer(file->f_path.dentry, buffer, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 if (len > 0)
252 *ppos += len;
Dave Young52e8c202007-07-26 11:03:54 +0000253 mutex_unlock(&buffer->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return len;
255}
256
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900257/**
258 * sysfs_get_open_dirent - get or create sysfs_open_dirent
259 * @sd: target sysfs_dirent
260 * @buffer: sysfs_buffer for this instance of open
261 *
262 * If @sd->s_attr.open exists, increment its reference count;
263 * otherwise, create one. @buffer is chained to the buffers
264 * list.
265 *
266 * LOCKING:
267 * Kernel thread context (may sleep).
268 *
269 * RETURNS:
270 * 0 on success, -errno on failure.
271 */
272static int sysfs_get_open_dirent(struct sysfs_dirent *sd,
273 struct sysfs_buffer *buffer)
274{
275 struct sysfs_open_dirent *od, *new_od = NULL;
276
277 retry:
278 spin_lock(&sysfs_open_dirent_lock);
279
280 if (!sd->s_attr.open && new_od) {
281 sd->s_attr.open = new_od;
282 new_od = NULL;
283 }
284
285 od = sd->s_attr.open;
286 if (od) {
287 atomic_inc(&od->refcnt);
288 list_add_tail(&buffer->list, &od->buffers);
289 }
290
291 spin_unlock(&sysfs_open_dirent_lock);
292
293 if (od) {
294 kfree(new_od);
295 return 0;
296 }
297
298 /* not there, initialize a new one and retry */
299 new_od = kmalloc(sizeof(*new_od), GFP_KERNEL);
300 if (!new_od)
301 return -ENOMEM;
302
303 atomic_set(&new_od->refcnt, 0);
304 INIT_LIST_HEAD(&new_od->buffers);
305 goto retry;
306}
307
308/**
309 * sysfs_put_open_dirent - put sysfs_open_dirent
310 * @sd: target sysfs_dirent
311 * @buffer: associated sysfs_buffer
312 *
313 * Put @sd->s_attr.open and unlink @buffer from the buffers list.
314 * If reference count reaches zero, disassociate and free it.
315 *
316 * LOCKING:
317 * None.
318 */
319static void sysfs_put_open_dirent(struct sysfs_dirent *sd,
320 struct sysfs_buffer *buffer)
321{
322 struct sysfs_open_dirent *od = sd->s_attr.open;
323
324 spin_lock(&sysfs_open_dirent_lock);
325
326 list_del(&buffer->list);
327 if (atomic_dec_and_test(&od->refcnt))
328 sd->s_attr.open = NULL;
329 else
330 od = NULL;
331
332 spin_unlock(&sysfs_open_dirent_lock);
333
334 kfree(od);
335}
336
Oliver Neukum94bebf42006-12-20 10:52:44 +0100337static int sysfs_open_file(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Tejun Heo3e519032007-06-14 03:45:15 +0900339 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900340 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 struct sysfs_buffer * buffer;
342 struct sysfs_ops * ops = NULL;
Tejun Heo0ab66082007-06-14 03:45:16 +0900343 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Tejun Heo0ab66082007-06-14 03:45:16 +0900345 /* need attr_sd for attr and ops, its parent for kobj */
346 if (!sysfs_get_active_two(attr_sd))
347 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 /* if the kobject has no ktype, then we assume that it is a subsystem
350 * itself, and use ops for it.
351 */
352 if (kobj->kset && kobj->kset->ktype)
353 ops = kobj->kset->ktype->sysfs_ops;
354 else if (kobj->ktype)
355 ops = kobj->ktype->sysfs_ops;
356 else
357 ops = &subsys_sysfs_ops;
358
Tejun Heo73107cb2007-06-14 03:45:16 +0900359 error = -EACCES;
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 /* No sysfs operations, either from having no subsystem,
362 * or the subsystem have no operations.
363 */
364 if (!ops)
Tejun Heo7b595752007-06-14 03:45:17 +0900365 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 /* File needs write support.
368 * The inode's perms must say it's ok,
369 * and we must have a store method.
370 */
371 if (file->f_mode & FMODE_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (!(inode->i_mode & S_IWUGO) || !ops->store)
Tejun Heo7b595752007-06-14 03:45:17 +0900373 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
375
376 /* File needs read support.
377 * The inode's perms must say it's ok, and we there
378 * must be a show method for it.
379 */
380 if (file->f_mode & FMODE_READ) {
381 if (!(inode->i_mode & S_IRUGO) || !ops->show)
Tejun Heo7b595752007-06-14 03:45:17 +0900382 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
384
385 /* No error? Great, allocate a buffer for the file, and store it
386 * it in file->private_data for easy access.
387 */
Tejun Heo0ab66082007-06-14 03:45:16 +0900388 error = -ENOMEM;
Eric Sesterhenn58d49282006-02-22 11:18:15 +0100389 buffer = kzalloc(sizeof(struct sysfs_buffer), GFP_KERNEL);
Tejun Heo0ab66082007-06-14 03:45:16 +0900390 if (!buffer)
Tejun Heo7b595752007-06-14 03:45:17 +0900391 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Dave Young52e8c202007-07-26 11:03:54 +0000393 mutex_init(&buffer->mutex);
Tejun Heo0ab66082007-06-14 03:45:16 +0900394 buffer->needs_read_fill = 1;
395 buffer->ops = ops;
Tejun Heo0ab66082007-06-14 03:45:16 +0900396 file->private_data = buffer;
397
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900398 /* make sure we have open dirent struct */
399 error = sysfs_get_open_dirent(attr_sd, buffer);
400 if (error)
401 goto err_free;
402
Tejun Heob05f0542007-09-20 16:05:10 +0900403 /* open succeeded, put active references */
Tejun Heo0ab66082007-06-14 03:45:16 +0900404 sysfs_put_active_two(attr_sd);
Tejun Heo0ab66082007-06-14 03:45:16 +0900405 return 0;
406
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900407 err_free:
408 kfree(buffer);
Tejun Heo7b595752007-06-14 03:45:17 +0900409 err_out:
Tejun Heo0ab66082007-06-14 03:45:16 +0900410 sysfs_put_active_two(attr_sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 return error;
412}
413
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900414static int sysfs_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900416 struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata;
Tejun Heo73107cb2007-06-14 03:45:16 +0900417 struct sysfs_buffer *buffer = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900419 sysfs_put_open_dirent(sd, buffer);
420
Tejun Heo50ab1a72007-09-20 16:05:10 +0900421 if (buffer->page)
422 free_page((unsigned long)buffer->page);
423 kfree(buffer);
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return 0;
426}
427
NeilBrown4508a7a2006-03-20 17:53:53 +1100428/* Sysfs attribute files are pollable. The idea is that you read
429 * the content and then you use 'poll' or 'select' to wait for
430 * the content to change. When the content changes (assuming the
431 * manager for the kobject supports notification), poll will
432 * return POLLERR|POLLPRI, and select will return the fd whether
433 * it is waiting for read, write, or exceptions.
434 * Once poll/select indicates that the value has changed, you
435 * need to close and re-open the file, as simply seeking and reading
436 * again will not get new data, or reset the state of 'poll'.
437 * Reminder: this only works for attributes which actively support
438 * it, and it is not possible to test an attribute from userspace
Rolf Eike Beera93720e2007-08-10 13:51:07 -0700439 * to see if it supports poll (Neither 'poll' nor 'select' return
NeilBrown4508a7a2006-03-20 17:53:53 +1100440 * an appropriate error code). When in doubt, set a suitable timeout value.
441 */
442static unsigned int sysfs_poll(struct file *filp, poll_table *wait)
443{
444 struct sysfs_buffer * buffer = filp->private_data;
Tejun Heo0ab66082007-06-14 03:45:16 +0900445 struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900446 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
Tejun Heo0ab66082007-06-14 03:45:16 +0900447
448 /* need parent for the kobj, grab both */
449 if (!sysfs_get_active_two(attr_sd))
450 goto trigger;
NeilBrown4508a7a2006-03-20 17:53:53 +1100451
452 poll_wait(filp, &kobj->poll, wait);
453
Tejun Heo0ab66082007-06-14 03:45:16 +0900454 sysfs_put_active_two(attr_sd);
NeilBrown4508a7a2006-03-20 17:53:53 +1100455
Tejun Heo0ab66082007-06-14 03:45:16 +0900456 if (buffer->event != atomic_read(&attr_sd->s_event))
457 goto trigger;
458
459 return 0;
460
461 trigger:
462 buffer->needs_read_fill = 1;
463 return POLLERR|POLLPRI;
NeilBrown4508a7a2006-03-20 17:53:53 +1100464}
465
Tejun Heo51225032007-06-14 04:27:25 +0900466void sysfs_notify(struct kobject *k, char *dir, char *attr)
NeilBrown4508a7a2006-03-20 17:53:53 +1100467{
Tejun Heo51225032007-06-14 04:27:25 +0900468 struct sysfs_dirent *sd = k->sd;
NeilBrown4508a7a2006-03-20 17:53:53 +1100469
Tejun Heo51225032007-06-14 04:27:25 +0900470 mutex_lock(&sysfs_mutex);
NeilBrown4508a7a2006-03-20 17:53:53 +1100471
Tejun Heo51225032007-06-14 04:27:25 +0900472 if (sd && dir)
473 sd = sysfs_find_dirent(sd, dir);
474 if (sd && attr)
475 sd = sysfs_find_dirent(sd, attr);
476 if (sd) {
477 atomic_inc(&sd->s_event);
NeilBrown4508a7a2006-03-20 17:53:53 +1100478 wake_up_interruptible(&k->poll);
NeilBrown4508a7a2006-03-20 17:53:53 +1100479 }
Tejun Heo51225032007-06-14 04:27:25 +0900480
481 mutex_unlock(&sysfs_mutex);
NeilBrown4508a7a2006-03-20 17:53:53 +1100482}
483EXPORT_SYMBOL_GPL(sysfs_notify);
484
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800485const struct file_operations sysfs_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 .read = sysfs_read_file,
487 .write = sysfs_write_file,
488 .llseek = generic_file_llseek,
489 .open = sysfs_open_file,
490 .release = sysfs_release,
NeilBrown4508a7a2006-03-20 17:53:53 +1100491 .poll = sysfs_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492};
493
494
Tejun Heo608e2662007-06-14 04:27:22 +0900495int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,
496 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 umode_t mode = (attr->mode & S_IALLUGO) | S_IFREG;
Tejun Heofb6896d2007-06-14 04:27:24 +0900499 struct sysfs_addrm_cxt acxt;
Tejun Heoa26cd722007-06-14 03:45:14 +0900500 struct sysfs_dirent *sd;
Tejun Heo23dc2792007-08-02 21:38:03 +0900501 int rc;
Tejun Heoa26cd722007-06-14 03:45:14 +0900502
Tejun Heo3e519032007-06-14 03:45:15 +0900503 sd = sysfs_new_dirent(attr->name, mode, type);
Tejun Heo3007e992007-06-14 04:27:23 +0900504 if (!sd)
505 return -ENOMEM;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900506 sd->s_attr.attr = (void *)attr;
Tejun Heoa26cd722007-06-14 03:45:14 +0900507
Tejun Heofb6896d2007-06-14 04:27:24 +0900508 sysfs_addrm_start(&acxt, dir_sd);
Tejun Heo23dc2792007-08-02 21:38:03 +0900509 rc = sysfs_add_one(&acxt, sd);
510 sysfs_addrm_finish(&acxt);
Tejun Heo3007e992007-06-14 04:27:23 +0900511
Tejun Heo23dc2792007-08-02 21:38:03 +0900512 if (rc)
Tejun Heo967e35d2007-07-18 16:38:11 +0900513 sysfs_put(sd);
Tejun Heo3007e992007-06-14 04:27:23 +0900514
Tejun Heo23dc2792007-08-02 21:38:03 +0900515 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516}
517
518
519/**
520 * sysfs_create_file - create an attribute file for an object.
521 * @kobj: object we're creating for.
522 * @attr: atrribute descriptor.
523 */
524
525int sysfs_create_file(struct kobject * kobj, const struct attribute * attr)
526{
Tejun Heo608e2662007-06-14 04:27:22 +0900527 BUG_ON(!kobj || !kobj->sd || !attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Tejun Heo608e2662007-06-14 04:27:22 +0900529 return sysfs_add_file(kobj->sd, attr, SYSFS_KOBJ_ATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531}
532
533
534/**
Alan Sterndfa87c82007-02-20 15:02:44 -0500535 * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
536 * @kobj: object we're acting for.
537 * @attr: attribute descriptor.
538 * @group: group name.
539 */
540int sysfs_add_file_to_group(struct kobject *kobj,
541 const struct attribute *attr, const char *group)
542{
Tejun Heo608e2662007-06-14 04:27:22 +0900543 struct sysfs_dirent *dir_sd;
Alan Sterndfa87c82007-02-20 15:02:44 -0500544 int error;
545
Tejun Heo608e2662007-06-14 04:27:22 +0900546 dir_sd = sysfs_get_dirent(kobj->sd, group);
547 if (!dir_sd)
548 return -ENOENT;
549
550 error = sysfs_add_file(dir_sd, attr, SYSFS_KOBJ_ATTR);
551 sysfs_put(dir_sd);
552
Alan Sterndfa87c82007-02-20 15:02:44 -0500553 return error;
554}
555EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557/**
Kay Sievers31e5abe2005-04-18 21:57:32 -0700558 * sysfs_chmod_file - update the modified mode value on an object attribute.
559 * @kobj: object we're acting for.
560 * @attr: attribute descriptor.
561 * @mode: file permissions.
562 *
563 */
564int sysfs_chmod_file(struct kobject *kobj, struct attribute *attr, mode_t mode)
565{
Tejun Heo51225032007-06-14 04:27:25 +0900566 struct sysfs_dirent *victim_sd = NULL;
567 struct dentry *victim = NULL;
Maneesh Sonibc062b12005-07-29 12:13:35 -0700568 struct inode * inode;
569 struct iattr newattrs;
Tejun Heo51225032007-06-14 04:27:25 +0900570 int rc;
Kay Sievers31e5abe2005-04-18 21:57:32 -0700571
Tejun Heo51225032007-06-14 04:27:25 +0900572 rc = -ENOENT;
573 victim_sd = sysfs_get_dirent(kobj->sd, attr->name);
574 if (!victim_sd)
575 goto out;
576
Eric W. Biederman932ea2e2007-08-20 21:36:30 +0900577 mutex_lock(&sysfs_rename_mutex);
Tejun Heo51225032007-06-14 04:27:25 +0900578 victim = sysfs_get_dentry(victim_sd);
Eric W. Biederman932ea2e2007-08-20 21:36:30 +0900579 mutex_unlock(&sysfs_rename_mutex);
Tejun Heo51225032007-06-14 04:27:25 +0900580 if (IS_ERR(victim)) {
581 rc = PTR_ERR(victim);
582 victim = NULL;
583 goto out;
Kay Sievers31e5abe2005-04-18 21:57:32 -0700584 }
Kay Sievers31e5abe2005-04-18 21:57:32 -0700585
Tejun Heo51225032007-06-14 04:27:25 +0900586 inode = victim->d_inode;
Tejun Heof88123e2007-09-20 16:05:10 +0900587
Tejun Heo51225032007-06-14 04:27:25 +0900588 mutex_lock(&inode->i_mutex);
Tejun Heof88123e2007-09-20 16:05:10 +0900589
Tejun Heo51225032007-06-14 04:27:25 +0900590 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
591 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
592 rc = notify_change(victim, &newattrs);
Tejun Heof88123e2007-09-20 16:05:10 +0900593
594 if (rc == 0) {
595 mutex_lock(&sysfs_mutex);
596 victim_sd->s_mode = newattrs.ia_mode;
597 mutex_unlock(&sysfs_mutex);
598 }
599
Tejun Heo51225032007-06-14 04:27:25 +0900600 mutex_unlock(&inode->i_mutex);
601 out:
602 dput(victim);
603 sysfs_put(victim_sd);
604 return rc;
Kay Sievers31e5abe2005-04-18 21:57:32 -0700605}
606EXPORT_SYMBOL_GPL(sysfs_chmod_file);
607
608
609/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 * sysfs_remove_file - remove an object attribute.
611 * @kobj: object we're acting for.
612 * @attr: attribute descriptor.
613 *
614 * Hash the attribute name and kill the victim.
615 */
616
617void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr)
618{
Tejun Heo608e2662007-06-14 04:27:22 +0900619 sysfs_hash_and_remove(kobj->sd, attr->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
621
622
Alan Sterndfa87c82007-02-20 15:02:44 -0500623/**
624 * sysfs_remove_file_from_group - remove an attribute file from a group.
625 * @kobj: object we're acting for.
626 * @attr: attribute descriptor.
627 * @group: group name.
628 */
629void sysfs_remove_file_from_group(struct kobject *kobj,
630 const struct attribute *attr, const char *group)
631{
Tejun Heo608e2662007-06-14 04:27:22 +0900632 struct sysfs_dirent *dir_sd;
Alan Sterndfa87c82007-02-20 15:02:44 -0500633
Tejun Heo608e2662007-06-14 04:27:22 +0900634 dir_sd = sysfs_get_dirent(kobj->sd, group);
635 if (dir_sd) {
636 sysfs_hash_and_remove(dir_sd, attr->name);
637 sysfs_put(dir_sd);
Alan Sterndfa87c82007-02-20 15:02:44 -0500638 }
639}
640EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
641
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400642struct sysfs_schedule_callback_struct {
643 struct kobject *kobj;
644 void (*func)(void *);
645 void *data;
Alan Stern523ded72007-04-26 00:12:04 -0700646 struct module *owner;
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400647 struct work_struct work;
648};
649
650static void sysfs_schedule_callback_work(struct work_struct *work)
651{
652 struct sysfs_schedule_callback_struct *ss = container_of(work,
653 struct sysfs_schedule_callback_struct, work);
654
655 (ss->func)(ss->data);
656 kobject_put(ss->kobj);
Alan Stern523ded72007-04-26 00:12:04 -0700657 module_put(ss->owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400658 kfree(ss);
659}
660
661/**
662 * sysfs_schedule_callback - helper to schedule a callback for a kobject
663 * @kobj: object we're acting for.
664 * @func: callback function to invoke later.
665 * @data: argument to pass to @func.
Alan Stern523ded72007-04-26 00:12:04 -0700666 * @owner: module owning the callback code
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400667 *
668 * sysfs attribute methods must not unregister themselves or their parent
669 * kobject (which would amount to the same thing). Attempts to do so will
670 * deadlock, since unregistration is mutually exclusive with driver
671 * callbacks.
672 *
673 * Instead methods can call this routine, which will attempt to allocate
674 * and schedule a workqueue request to call back @func with @data as its
675 * argument in the workqueue's process context. @kobj will be pinned
676 * until @func returns.
677 *
678 * Returns 0 if the request was submitted, -ENOMEM if storage could not
Alan Stern523ded72007-04-26 00:12:04 -0700679 * be allocated, -ENODEV if a reference to @owner isn't available.
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400680 */
681int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
Alan Stern523ded72007-04-26 00:12:04 -0700682 void *data, struct module *owner)
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400683{
684 struct sysfs_schedule_callback_struct *ss;
685
Alan Stern523ded72007-04-26 00:12:04 -0700686 if (!try_module_get(owner))
687 return -ENODEV;
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400688 ss = kmalloc(sizeof(*ss), GFP_KERNEL);
Alan Stern523ded72007-04-26 00:12:04 -0700689 if (!ss) {
690 module_put(owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400691 return -ENOMEM;
Alan Stern523ded72007-04-26 00:12:04 -0700692 }
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400693 kobject_get(kobj);
694 ss->kobj = kobj;
695 ss->func = func;
696 ss->data = data;
Alan Stern523ded72007-04-26 00:12:04 -0700697 ss->owner = owner;
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400698 INIT_WORK(&ss->work, sysfs_schedule_callback_work);
699 schedule_work(&ss->work);
700 return 0;
701}
702EXPORT_SYMBOL_GPL(sysfs_schedule_callback);
703
Alan Sterndfa87c82007-02-20 15:02:44 -0500704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705EXPORT_SYMBOL_GPL(sysfs_create_file);
706EXPORT_SYMBOL_GPL(sysfs_remove_file);