blob: 8acf82bba44c60c14f46317448c792abd19ceb52 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Tejun Heo6d66f5c2007-09-20 17:31:38 +09002 * 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 Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
13#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/kobject.h>
Christoph Hellwig5f45f1a2005-06-23 00:09:12 -070015#include <linux/namei.h>
NeilBrown4508a7a2006-03-20 17:53:53 +110016#include <linux/poll.h>
Oliver Neukum94bebf42006-12-20 10:52:44 +010017#include <linux/list.h>
Dave Young52e8c202007-07-26 11:03:54 +000018#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#include "sysfs.h"
22
Tejun Heo85a4ffa2007-09-20 16:05:12 +090023/*
24 * There's one sysfs_buffer for each open file and one
25 * sysfs_open_dirent for each sysfs_dirent with one or more open
26 * files.
27 *
28 * filp->private_data points to sysfs_buffer and
29 * sysfs_dirent->s_attr.open points to sysfs_open_dirent. s_attr.open
30 * is protected by sysfs_open_dirent_lock.
31 */
Jiri Slabyd7b37882007-11-21 14:55:19 -080032static DEFINE_SPINLOCK(sysfs_open_dirent_lock);
Tejun Heo85a4ffa2007-09-20 16:05:12 +090033
34struct sysfs_open_dirent {
35 atomic_t refcnt;
Tejun Heoa4e8b912007-09-20 16:05:12 +090036 atomic_t event;
37 wait_queue_head_t poll;
Tejun Heo85a4ffa2007-09-20 16:05:12 +090038 struct list_head buffers; /* goes through sysfs_buffer.list */
39};
40
Tejun Heo73107cb2007-06-14 03:45:16 +090041struct sysfs_buffer {
42 size_t count;
43 loff_t pos;
44 char * page;
45 struct sysfs_ops * ops;
Dave Young52e8c202007-07-26 11:03:54 +000046 struct mutex mutex;
Tejun Heo73107cb2007-06-14 03:45:16 +090047 int needs_read_fill;
48 int event;
Tejun Heo85a4ffa2007-09-20 16:05:12 +090049 struct list_head list;
Tejun Heo73107cb2007-06-14 03:45:16 +090050};
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52/**
53 * fill_read_buffer - allocate and fill buffer from object.
54 * @dentry: dentry pointer.
55 * @buffer: data buffer for file.
56 *
57 * Allocate @buffer->page, if it hasn't been already, then call the
58 * kobject's show() method to fill the buffer with this attribute's
59 * data.
Oliver Neukum82244b12007-01-02 08:48:08 +010060 * This is called only once, on the file's first read unless an error
61 * is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 */
63static int fill_read_buffer(struct dentry * dentry, struct sysfs_buffer * buffer)
64{
Tejun Heo0ab66082007-06-14 03:45:16 +090065 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
Tejun Heob1fc3d62007-09-20 16:05:11 +090066 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 struct sysfs_ops * ops = buffer->ops;
68 int ret = 0;
69 ssize_t count;
70
71 if (!buffer->page)
72 buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
73 if (!buffer->page)
74 return -ENOMEM;
75
Tejun Heo0ab66082007-06-14 03:45:16 +090076 /* need attr_sd for attr and ops, its parent for kobj */
77 if (!sysfs_get_active_two(attr_sd))
78 return -ENODEV;
79
Tejun Heoa4e8b912007-09-20 16:05:12 +090080 buffer->event = atomic_read(&attr_sd->s_attr.open->event);
Tejun Heob1fc3d62007-09-20 16:05:11 +090081 count = ops->show(kobj, attr_sd->s_attr.attr, buffer->page);
Tejun Heo0ab66082007-06-14 03:45:16 +090082
83 sysfs_put_active_two(attr_sd);
84
Miao Xie8118a852007-11-21 14:55:19 -080085 /*
86 * The code works fine with PAGE_SIZE return but it's likely to
87 * indicate truncated result or overflow in normal use cases.
88 */
89 BUG_ON(count >= (ssize_t)PAGE_SIZE);
Oliver Neukum82244b12007-01-02 08:48:08 +010090 if (count >= 0) {
91 buffer->needs_read_fill = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 buffer->count = count;
Oliver Neukum82244b12007-01-02 08:48:08 +010093 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 ret = count;
Oliver Neukum82244b12007-01-02 08:48:08 +010095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 return ret;
97}
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099/**
100 * sysfs_read_file - read an attribute.
101 * @file: file pointer.
102 * @buf: buffer to fill.
103 * @count: number of bytes to read.
104 * @ppos: starting offset in file.
105 *
106 * Userspace wants to read an attribute file. The attribute descriptor
107 * is in the file's ->d_fsdata. The target object is in the directory's
108 * ->d_fsdata.
109 *
110 * We call fill_read_buffer() to allocate and fill the buffer from the
111 * object's show() method exactly once (if the read is happening from
112 * the beginning of the file). That should fill the entire buffer with
113 * all the data the object has to offer for that attribute.
114 * We then call flush_read_buffer() to copy the buffer to userspace
115 * in the increments specified.
116 */
117
118static ssize_t
119sysfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
120{
121 struct sysfs_buffer * buffer = file->private_data;
122 ssize_t retval = 0;
123
Dave Young52e8c202007-07-26 11:03:54 +0000124 mutex_lock(&buffer->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 if (buffer->needs_read_fill) {
Tejun Heo73107cb2007-06-14 03:45:16 +0900126 retval = fill_read_buffer(file->f_path.dentry,buffer);
Alan Sterne7b0d262007-03-15 15:51:28 -0400127 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 goto out;
129 }
Zach Brown5c1fdf42006-10-03 01:16:06 -0700130 pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",
131 __FUNCTION__, count, *ppos, buffer->page);
Akinobu Mita92f4c702007-05-09 02:33:32 -0700132 retval = simple_read_from_buffer(buf, count, ppos, buffer->page,
133 buffer->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134out:
Dave Young52e8c202007-07-26 11:03:54 +0000135 mutex_unlock(&buffer->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 return retval;
137}
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139/**
140 * fill_write_buffer - copy buffer from userspace.
141 * @buffer: data buffer for file.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700142 * @buf: data from user.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 * @count: number of bytes in @userbuf.
144 *
145 * Allocate @buffer->page if it hasn't been already, then
146 * copy the user-supplied buffer into it.
147 */
148
149static int
150fill_write_buffer(struct sysfs_buffer * buffer, const char __user * buf, size_t count)
151{
152 int error;
153
154 if (!buffer->page)
155 buffer->page = (char *)get_zeroed_page(GFP_KERNEL);
156 if (!buffer->page)
157 return -ENOMEM;
158
159 if (count >= PAGE_SIZE)
Greg Kroah-Hartman6e0dd742006-03-31 15:37:06 -0800160 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 error = copy_from_user(buffer->page,buf,count);
162 buffer->needs_read_fill = 1;
Thomas Maier035ed7a2006-10-22 19:17:47 +0200163 /* if buf is assumed to contain a string, terminate it by \0,
164 so e.g. sscanf() can scan the string easily */
165 buffer->page[count] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 return error ? -EFAULT : count;
167}
168
169
170/**
171 * flush_write_buffer - push buffer to kobject.
Martin Waitz3d410882005-06-23 22:05:21 -0700172 * @dentry: dentry to the attribute
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 * @buffer: data buffer for file.
Martin Waitz3d410882005-06-23 22:05:21 -0700174 * @count: number of bytes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 *
176 * Get the correct pointers for the kobject and the attribute we're
177 * dealing with, then call the store() method for the attribute,
178 * passing the buffer that we acquired in fill_write_buffer().
179 */
180
Tejun Heo0ab66082007-06-14 03:45:16 +0900181static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182flush_write_buffer(struct dentry * dentry, struct sysfs_buffer * buffer, size_t count)
183{
Tejun Heo3e519032007-06-14 03:45:15 +0900184 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900185 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 struct sysfs_ops * ops = buffer->ops;
Tejun Heo0ab66082007-06-14 03:45:16 +0900187 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Tejun Heo0ab66082007-06-14 03:45:16 +0900189 /* need attr_sd for attr and ops, its parent for kobj */
190 if (!sysfs_get_active_two(attr_sd))
191 return -ENODEV;
192
Tejun Heob1fc3d62007-09-20 16:05:11 +0900193 rc = ops->store(kobj, attr_sd->s_attr.attr, buffer->page, count);
Tejun Heo0ab66082007-06-14 03:45:16 +0900194
195 sysfs_put_active_two(attr_sd);
196
197 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
200
201/**
202 * sysfs_write_file - write an attribute.
203 * @file: file pointer
204 * @buf: data to write
205 * @count: number of bytes
206 * @ppos: starting offset
207 *
208 * Similar to sysfs_read_file(), though working in the opposite direction.
209 * We allocate and fill the data from the user in fill_write_buffer(),
210 * then push it to the kobject in flush_write_buffer().
211 * There is no easy way for us to know if userspace is only doing a partial
212 * write, so we don't support them. We expect the entire buffer to come
213 * on the first write.
214 * Hint: if you're writing a value, first read the file, modify only the
215 * the value you're changing, then write entire buffer back.
216 */
217
218static ssize_t
219sysfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
220{
221 struct sysfs_buffer * buffer = file->private_data;
222 ssize_t len;
223
Dave Young52e8c202007-07-26 11:03:54 +0000224 mutex_lock(&buffer->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 len = fill_write_buffer(buffer, buf, count);
226 if (len > 0)
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800227 len = flush_write_buffer(file->f_path.dentry, buffer, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 if (len > 0)
229 *ppos += len;
Dave Young52e8c202007-07-26 11:03:54 +0000230 mutex_unlock(&buffer->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return len;
232}
233
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900234/**
235 * sysfs_get_open_dirent - get or create sysfs_open_dirent
236 * @sd: target sysfs_dirent
237 * @buffer: sysfs_buffer for this instance of open
238 *
239 * If @sd->s_attr.open exists, increment its reference count;
240 * otherwise, create one. @buffer is chained to the buffers
241 * list.
242 *
243 * LOCKING:
244 * Kernel thread context (may sleep).
245 *
246 * RETURNS:
247 * 0 on success, -errno on failure.
248 */
249static int sysfs_get_open_dirent(struct sysfs_dirent *sd,
250 struct sysfs_buffer *buffer)
251{
252 struct sysfs_open_dirent *od, *new_od = NULL;
253
254 retry:
255 spin_lock(&sysfs_open_dirent_lock);
256
257 if (!sd->s_attr.open && new_od) {
258 sd->s_attr.open = new_od;
259 new_od = NULL;
260 }
261
262 od = sd->s_attr.open;
263 if (od) {
264 atomic_inc(&od->refcnt);
265 list_add_tail(&buffer->list, &od->buffers);
266 }
267
268 spin_unlock(&sysfs_open_dirent_lock);
269
270 if (od) {
271 kfree(new_od);
272 return 0;
273 }
274
275 /* not there, initialize a new one and retry */
276 new_od = kmalloc(sizeof(*new_od), GFP_KERNEL);
277 if (!new_od)
278 return -ENOMEM;
279
280 atomic_set(&new_od->refcnt, 0);
Tejun Heoa4e8b912007-09-20 16:05:12 +0900281 atomic_set(&new_od->event, 1);
282 init_waitqueue_head(&new_od->poll);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900283 INIT_LIST_HEAD(&new_od->buffers);
284 goto retry;
285}
286
287/**
288 * sysfs_put_open_dirent - put sysfs_open_dirent
289 * @sd: target sysfs_dirent
290 * @buffer: associated sysfs_buffer
291 *
292 * Put @sd->s_attr.open and unlink @buffer from the buffers list.
293 * If reference count reaches zero, disassociate and free it.
294 *
295 * LOCKING:
296 * None.
297 */
298static void sysfs_put_open_dirent(struct sysfs_dirent *sd,
299 struct sysfs_buffer *buffer)
300{
301 struct sysfs_open_dirent *od = sd->s_attr.open;
302
303 spin_lock(&sysfs_open_dirent_lock);
304
305 list_del(&buffer->list);
306 if (atomic_dec_and_test(&od->refcnt))
307 sd->s_attr.open = NULL;
308 else
309 od = NULL;
310
311 spin_unlock(&sysfs_open_dirent_lock);
312
313 kfree(od);
314}
315
Oliver Neukum94bebf42006-12-20 10:52:44 +0100316static int sysfs_open_file(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Tejun Heo3e519032007-06-14 03:45:15 +0900318 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900319 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
Kay Sievers000f2a42007-11-02 13:47:53 +0100320 struct sysfs_buffer *buffer;
321 struct sysfs_ops *ops;
322 int error = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Tejun Heo0ab66082007-06-14 03:45:16 +0900324 /* need attr_sd for attr and ops, its parent for kobj */
325 if (!sysfs_get_active_two(attr_sd))
326 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Kay Sievers000f2a42007-11-02 13:47:53 +0100328 /* every kobject with an attribute needs a ktype assigned */
329 if (kobj->ktype && kobj->ktype->sysfs_ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 ops = kobj->ktype->sysfs_ops;
Kay Sievers000f2a42007-11-02 13:47:53 +0100331 else {
332 printk(KERN_ERR "missing sysfs attribute operations for "
333 "kobject: %s\n", kobject_name(kobj));
334 WARN_ON(1);
Tejun Heo7b595752007-06-14 03:45:17 +0900335 goto err_out;
Kay Sievers000f2a42007-11-02 13:47:53 +0100336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 /* File needs write support.
339 * The inode's perms must say it's ok,
340 * and we must have a store method.
341 */
342 if (file->f_mode & FMODE_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (!(inode->i_mode & S_IWUGO) || !ops->store)
Tejun Heo7b595752007-06-14 03:45:17 +0900344 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
346
347 /* File needs read support.
348 * The inode's perms must say it's ok, and we there
349 * must be a show method for it.
350 */
351 if (file->f_mode & FMODE_READ) {
352 if (!(inode->i_mode & S_IRUGO) || !ops->show)
Tejun Heo7b595752007-06-14 03:45:17 +0900353 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
355
356 /* No error? Great, allocate a buffer for the file, and store it
357 * it in file->private_data for easy access.
358 */
Tejun Heo0ab66082007-06-14 03:45:16 +0900359 error = -ENOMEM;
Eric Sesterhenn58d49282006-02-22 11:18:15 +0100360 buffer = kzalloc(sizeof(struct sysfs_buffer), GFP_KERNEL);
Tejun Heo0ab66082007-06-14 03:45:16 +0900361 if (!buffer)
Tejun Heo7b595752007-06-14 03:45:17 +0900362 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Dave Young52e8c202007-07-26 11:03:54 +0000364 mutex_init(&buffer->mutex);
Tejun Heo0ab66082007-06-14 03:45:16 +0900365 buffer->needs_read_fill = 1;
366 buffer->ops = ops;
Tejun Heo0ab66082007-06-14 03:45:16 +0900367 file->private_data = buffer;
368
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900369 /* make sure we have open dirent struct */
370 error = sysfs_get_open_dirent(attr_sd, buffer);
371 if (error)
372 goto err_free;
373
Tejun Heob05f0542007-09-20 16:05:10 +0900374 /* open succeeded, put active references */
Tejun Heo0ab66082007-06-14 03:45:16 +0900375 sysfs_put_active_two(attr_sd);
Tejun Heo0ab66082007-06-14 03:45:16 +0900376 return 0;
377
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900378 err_free:
379 kfree(buffer);
Tejun Heo7b595752007-06-14 03:45:17 +0900380 err_out:
Tejun Heo0ab66082007-06-14 03:45:16 +0900381 sysfs_put_active_two(attr_sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return error;
383}
384
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900385static int sysfs_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900387 struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata;
Tejun Heo73107cb2007-06-14 03:45:16 +0900388 struct sysfs_buffer *buffer = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900390 sysfs_put_open_dirent(sd, buffer);
391
Tejun Heo50ab1a72007-09-20 16:05:10 +0900392 if (buffer->page)
393 free_page((unsigned long)buffer->page);
394 kfree(buffer);
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return 0;
397}
398
NeilBrown4508a7a2006-03-20 17:53:53 +1100399/* Sysfs attribute files are pollable. The idea is that you read
400 * the content and then you use 'poll' or 'select' to wait for
401 * the content to change. When the content changes (assuming the
402 * manager for the kobject supports notification), poll will
403 * return POLLERR|POLLPRI, and select will return the fd whether
404 * it is waiting for read, write, or exceptions.
405 * Once poll/select indicates that the value has changed, you
406 * need to close and re-open the file, as simply seeking and reading
407 * again will not get new data, or reset the state of 'poll'.
408 * Reminder: this only works for attributes which actively support
409 * it, and it is not possible to test an attribute from userspace
Rolf Eike Beera93720e2007-08-10 13:51:07 -0700410 * to see if it supports poll (Neither 'poll' nor 'select' return
NeilBrown4508a7a2006-03-20 17:53:53 +1100411 * an appropriate error code). When in doubt, set a suitable timeout value.
412 */
413static unsigned int sysfs_poll(struct file *filp, poll_table *wait)
414{
415 struct sysfs_buffer * buffer = filp->private_data;
Tejun Heo0ab66082007-06-14 03:45:16 +0900416 struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata;
Tejun Heoa4e8b912007-09-20 16:05:12 +0900417 struct sysfs_open_dirent *od = attr_sd->s_attr.open;
Tejun Heo0ab66082007-06-14 03:45:16 +0900418
419 /* need parent for the kobj, grab both */
420 if (!sysfs_get_active_two(attr_sd))
421 goto trigger;
NeilBrown4508a7a2006-03-20 17:53:53 +1100422
Tejun Heoa4e8b912007-09-20 16:05:12 +0900423 poll_wait(filp, &od->poll, wait);
NeilBrown4508a7a2006-03-20 17:53:53 +1100424
Tejun Heo0ab66082007-06-14 03:45:16 +0900425 sysfs_put_active_two(attr_sd);
NeilBrown4508a7a2006-03-20 17:53:53 +1100426
Tejun Heoa4e8b912007-09-20 16:05:12 +0900427 if (buffer->event != atomic_read(&od->event))
Tejun Heo0ab66082007-06-14 03:45:16 +0900428 goto trigger;
429
430 return 0;
431
432 trigger:
433 buffer->needs_read_fill = 1;
434 return POLLERR|POLLPRI;
NeilBrown4508a7a2006-03-20 17:53:53 +1100435}
436
Tejun Heo51225032007-06-14 04:27:25 +0900437void sysfs_notify(struct kobject *k, char *dir, char *attr)
NeilBrown4508a7a2006-03-20 17:53:53 +1100438{
Tejun Heo51225032007-06-14 04:27:25 +0900439 struct sysfs_dirent *sd = k->sd;
NeilBrown4508a7a2006-03-20 17:53:53 +1100440
Tejun Heo51225032007-06-14 04:27:25 +0900441 mutex_lock(&sysfs_mutex);
NeilBrown4508a7a2006-03-20 17:53:53 +1100442
Tejun Heo51225032007-06-14 04:27:25 +0900443 if (sd && dir)
444 sd = sysfs_find_dirent(sd, dir);
445 if (sd && attr)
446 sd = sysfs_find_dirent(sd, attr);
447 if (sd) {
Tejun Heoa4e8b912007-09-20 16:05:12 +0900448 struct sysfs_open_dirent *od;
449
450 spin_lock(&sysfs_open_dirent_lock);
451
452 od = sd->s_attr.open;
453 if (od) {
454 atomic_inc(&od->event);
455 wake_up_interruptible(&od->poll);
456 }
457
458 spin_unlock(&sysfs_open_dirent_lock);
NeilBrown4508a7a2006-03-20 17:53:53 +1100459 }
Tejun Heo51225032007-06-14 04:27:25 +0900460
461 mutex_unlock(&sysfs_mutex);
NeilBrown4508a7a2006-03-20 17:53:53 +1100462}
463EXPORT_SYMBOL_GPL(sysfs_notify);
464
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800465const struct file_operations sysfs_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 .read = sysfs_read_file,
467 .write = sysfs_write_file,
468 .llseek = generic_file_llseek,
469 .open = sysfs_open_file,
470 .release = sysfs_release,
NeilBrown4508a7a2006-03-20 17:53:53 +1100471 .poll = sysfs_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472};
473
474
Tejun Heo608e2662007-06-14 04:27:22 +0900475int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,
476 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 umode_t mode = (attr->mode & S_IALLUGO) | S_IFREG;
Tejun Heofb6896d2007-06-14 04:27:24 +0900479 struct sysfs_addrm_cxt acxt;
Tejun Heoa26cd722007-06-14 03:45:14 +0900480 struct sysfs_dirent *sd;
Tejun Heo23dc2792007-08-02 21:38:03 +0900481 int rc;
Tejun Heoa26cd722007-06-14 03:45:14 +0900482
Tejun Heo3e519032007-06-14 03:45:15 +0900483 sd = sysfs_new_dirent(attr->name, mode, type);
Tejun Heo3007e992007-06-14 04:27:23 +0900484 if (!sd)
485 return -ENOMEM;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900486 sd->s_attr.attr = (void *)attr;
Tejun Heoa26cd722007-06-14 03:45:14 +0900487
Tejun Heofb6896d2007-06-14 04:27:24 +0900488 sysfs_addrm_start(&acxt, dir_sd);
Tejun Heo23dc2792007-08-02 21:38:03 +0900489 rc = sysfs_add_one(&acxt, sd);
490 sysfs_addrm_finish(&acxt);
Tejun Heo3007e992007-06-14 04:27:23 +0900491
Tejun Heo23dc2792007-08-02 21:38:03 +0900492 if (rc)
Tejun Heo967e35d2007-07-18 16:38:11 +0900493 sysfs_put(sd);
Tejun Heo3007e992007-06-14 04:27:23 +0900494
Tejun Heo23dc2792007-08-02 21:38:03 +0900495 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
498
499/**
500 * sysfs_create_file - create an attribute file for an object.
501 * @kobj: object we're creating for.
Chris Malley3932bf62007-10-20 03:14:32 +0200502 * @attr: attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 */
504
505int sysfs_create_file(struct kobject * kobj, const struct attribute * attr)
506{
Tejun Heo608e2662007-06-14 04:27:22 +0900507 BUG_ON(!kobj || !kobj->sd || !attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Tejun Heo608e2662007-06-14 04:27:22 +0900509 return sysfs_add_file(kobj->sd, attr, SYSFS_KOBJ_ATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511}
512
513
514/**
Alan Sterndfa87c82007-02-20 15:02:44 -0500515 * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
516 * @kobj: object we're acting for.
517 * @attr: attribute descriptor.
518 * @group: group name.
519 */
520int sysfs_add_file_to_group(struct kobject *kobj,
521 const struct attribute *attr, const char *group)
522{
Tejun Heo608e2662007-06-14 04:27:22 +0900523 struct sysfs_dirent *dir_sd;
Alan Sterndfa87c82007-02-20 15:02:44 -0500524 int error;
525
Tejun Heo608e2662007-06-14 04:27:22 +0900526 dir_sd = sysfs_get_dirent(kobj->sd, group);
527 if (!dir_sd)
528 return -ENOENT;
529
530 error = sysfs_add_file(dir_sd, attr, SYSFS_KOBJ_ATTR);
531 sysfs_put(dir_sd);
532
Alan Sterndfa87c82007-02-20 15:02:44 -0500533 return error;
534}
535EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537/**
Kay Sievers31e5abe2005-04-18 21:57:32 -0700538 * sysfs_chmod_file - update the modified mode value on an object attribute.
539 * @kobj: object we're acting for.
540 * @attr: attribute descriptor.
541 * @mode: file permissions.
542 *
543 */
544int sysfs_chmod_file(struct kobject *kobj, struct attribute *attr, mode_t mode)
545{
Tejun Heo51225032007-06-14 04:27:25 +0900546 struct sysfs_dirent *victim_sd = NULL;
547 struct dentry *victim = NULL;
Maneesh Sonibc062b12005-07-29 12:13:35 -0700548 struct inode * inode;
549 struct iattr newattrs;
Tejun Heo51225032007-06-14 04:27:25 +0900550 int rc;
Kay Sievers31e5abe2005-04-18 21:57:32 -0700551
Tejun Heo51225032007-06-14 04:27:25 +0900552 rc = -ENOENT;
553 victim_sd = sysfs_get_dirent(kobj->sd, attr->name);
554 if (!victim_sd)
555 goto out;
556
Eric W. Biederman932ea2e2007-08-20 21:36:30 +0900557 mutex_lock(&sysfs_rename_mutex);
Tejun Heo51225032007-06-14 04:27:25 +0900558 victim = sysfs_get_dentry(victim_sd);
Eric W. Biederman932ea2e2007-08-20 21:36:30 +0900559 mutex_unlock(&sysfs_rename_mutex);
Tejun Heo51225032007-06-14 04:27:25 +0900560 if (IS_ERR(victim)) {
561 rc = PTR_ERR(victim);
562 victim = NULL;
563 goto out;
Kay Sievers31e5abe2005-04-18 21:57:32 -0700564 }
Kay Sievers31e5abe2005-04-18 21:57:32 -0700565
Tejun Heo51225032007-06-14 04:27:25 +0900566 inode = victim->d_inode;
Tejun Heof88123e2007-09-20 16:05:10 +0900567
Tejun Heo51225032007-06-14 04:27:25 +0900568 mutex_lock(&inode->i_mutex);
Tejun Heof88123e2007-09-20 16:05:10 +0900569
Tejun Heo51225032007-06-14 04:27:25 +0900570 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
571 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
572 rc = notify_change(victim, &newattrs);
Tejun Heof88123e2007-09-20 16:05:10 +0900573
574 if (rc == 0) {
575 mutex_lock(&sysfs_mutex);
576 victim_sd->s_mode = newattrs.ia_mode;
577 mutex_unlock(&sysfs_mutex);
578 }
579
Tejun Heo51225032007-06-14 04:27:25 +0900580 mutex_unlock(&inode->i_mutex);
581 out:
582 dput(victim);
583 sysfs_put(victim_sd);
584 return rc;
Kay Sievers31e5abe2005-04-18 21:57:32 -0700585}
586EXPORT_SYMBOL_GPL(sysfs_chmod_file);
587
588
589/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 * sysfs_remove_file - remove an object attribute.
591 * @kobj: object we're acting for.
592 * @attr: attribute descriptor.
593 *
594 * Hash the attribute name and kill the victim.
595 */
596
597void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr)
598{
Tejun Heo608e2662007-06-14 04:27:22 +0900599 sysfs_hash_and_remove(kobj->sd, attr->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
601
602
Alan Sterndfa87c82007-02-20 15:02:44 -0500603/**
604 * sysfs_remove_file_from_group - remove an attribute file from a group.
605 * @kobj: object we're acting for.
606 * @attr: attribute descriptor.
607 * @group: group name.
608 */
609void sysfs_remove_file_from_group(struct kobject *kobj,
610 const struct attribute *attr, const char *group)
611{
Tejun Heo608e2662007-06-14 04:27:22 +0900612 struct sysfs_dirent *dir_sd;
Alan Sterndfa87c82007-02-20 15:02:44 -0500613
Tejun Heo608e2662007-06-14 04:27:22 +0900614 dir_sd = sysfs_get_dirent(kobj->sd, group);
615 if (dir_sd) {
616 sysfs_hash_and_remove(dir_sd, attr->name);
617 sysfs_put(dir_sd);
Alan Sterndfa87c82007-02-20 15:02:44 -0500618 }
619}
620EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
621
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400622struct sysfs_schedule_callback_struct {
623 struct kobject *kobj;
624 void (*func)(void *);
625 void *data;
Alan Stern523ded72007-04-26 00:12:04 -0700626 struct module *owner;
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400627 struct work_struct work;
628};
629
630static void sysfs_schedule_callback_work(struct work_struct *work)
631{
632 struct sysfs_schedule_callback_struct *ss = container_of(work,
633 struct sysfs_schedule_callback_struct, work);
634
635 (ss->func)(ss->data);
636 kobject_put(ss->kobj);
Alan Stern523ded72007-04-26 00:12:04 -0700637 module_put(ss->owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400638 kfree(ss);
639}
640
641/**
642 * sysfs_schedule_callback - helper to schedule a callback for a kobject
643 * @kobj: object we're acting for.
644 * @func: callback function to invoke later.
645 * @data: argument to pass to @func.
Alan Stern523ded72007-04-26 00:12:04 -0700646 * @owner: module owning the callback code
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400647 *
648 * sysfs attribute methods must not unregister themselves or their parent
649 * kobject (which would amount to the same thing). Attempts to do so will
650 * deadlock, since unregistration is mutually exclusive with driver
651 * callbacks.
652 *
653 * Instead methods can call this routine, which will attempt to allocate
654 * and schedule a workqueue request to call back @func with @data as its
655 * argument in the workqueue's process context. @kobj will be pinned
656 * until @func returns.
657 *
658 * Returns 0 if the request was submitted, -ENOMEM if storage could not
Alan Stern523ded72007-04-26 00:12:04 -0700659 * be allocated, -ENODEV if a reference to @owner isn't available.
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400660 */
661int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
Alan Stern523ded72007-04-26 00:12:04 -0700662 void *data, struct module *owner)
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400663{
664 struct sysfs_schedule_callback_struct *ss;
665
Alan Stern523ded72007-04-26 00:12:04 -0700666 if (!try_module_get(owner))
667 return -ENODEV;
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400668 ss = kmalloc(sizeof(*ss), GFP_KERNEL);
Alan Stern523ded72007-04-26 00:12:04 -0700669 if (!ss) {
670 module_put(owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400671 return -ENOMEM;
Alan Stern523ded72007-04-26 00:12:04 -0700672 }
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400673 kobject_get(kobj);
674 ss->kobj = kobj;
675 ss->func = func;
676 ss->data = data;
Alan Stern523ded72007-04-26 00:12:04 -0700677 ss->owner = owner;
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400678 INIT_WORK(&ss->work, sysfs_schedule_callback_work);
679 schedule_work(&ss->work);
680 return 0;
681}
682EXPORT_SYMBOL_GPL(sysfs_schedule_callback);
683
Alan Sterndfa87c82007-02-20 15:02:44 -0500684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685EXPORT_SYMBOL_GPL(sysfs_create_file);
686EXPORT_SYMBOL_GPL(sysfs_remove_file);