blob: c05f9618b2dcadf72becd1b840909f6ece369184 [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;
Tejun Heoa4e8b912007-09-20 16:05:12 +090065 atomic_t event;
66 wait_queue_head_t poll;
Tejun Heo85a4ffa2007-09-20 16:05:12 +090067 struct list_head buffers; /* goes through sysfs_buffer.list */
68};
69
Tejun Heo73107cb2007-06-14 03:45:16 +090070struct sysfs_buffer {
71 size_t count;
72 loff_t pos;
73 char * page;
74 struct sysfs_ops * ops;
Dave Young52e8c202007-07-26 11:03:54 +000075 struct mutex mutex;
Tejun Heo73107cb2007-06-14 03:45:16 +090076 int needs_read_fill;
77 int event;
Tejun Heo85a4ffa2007-09-20 16:05:12 +090078 struct list_head list;
Tejun Heo73107cb2007-06-14 03:45:16 +090079};
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81/**
82 * fill_read_buffer - allocate and fill buffer from object.
83 * @dentry: dentry pointer.
84 * @buffer: data buffer for file.
85 *
86 * Allocate @buffer->page, if it hasn't been already, then call the
87 * kobject's show() method to fill the buffer with this attribute's
88 * data.
Oliver Neukum82244b12007-01-02 08:48:08 +010089 * This is called only once, on the file's first read unless an error
90 * is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 */
92static int fill_read_buffer(struct dentry * dentry, struct sysfs_buffer * buffer)
93{
Tejun Heo0ab66082007-06-14 03:45:16 +090094 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
Tejun Heob1fc3d62007-09-20 16:05:11 +090095 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 struct sysfs_ops * ops = buffer->ops;
97 int ret = 0;
98 ssize_t count;
99
100 if (!buffer->page)
101 buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
102 if (!buffer->page)
103 return -ENOMEM;
104
Tejun Heo0ab66082007-06-14 03:45:16 +0900105 /* need attr_sd for attr and ops, its parent for kobj */
106 if (!sysfs_get_active_two(attr_sd))
107 return -ENODEV;
108
Tejun Heoa4e8b912007-09-20 16:05:12 +0900109 buffer->event = atomic_read(&attr_sd->s_attr.open->event);
Tejun Heob1fc3d62007-09-20 16:05:11 +0900110 count = ops->show(kobj, attr_sd->s_attr.attr, buffer->page);
Tejun Heo0ab66082007-06-14 03:45:16 +0900111
112 sysfs_put_active_two(attr_sd);
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 BUG_ON(count > (ssize_t)PAGE_SIZE);
Oliver Neukum82244b12007-01-02 08:48:08 +0100115 if (count >= 0) {
116 buffer->needs_read_fill = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 buffer->count = count;
Oliver Neukum82244b12007-01-02 08:48:08 +0100118 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 ret = count;
Oliver Neukum82244b12007-01-02 08:48:08 +0100120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 return ret;
122}
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124/**
125 * sysfs_read_file - read an attribute.
126 * @file: file pointer.
127 * @buf: buffer to fill.
128 * @count: number of bytes to read.
129 * @ppos: starting offset in file.
130 *
131 * Userspace wants to read an attribute file. The attribute descriptor
132 * is in the file's ->d_fsdata. The target object is in the directory's
133 * ->d_fsdata.
134 *
135 * We call fill_read_buffer() to allocate and fill the buffer from the
136 * object's show() method exactly once (if the read is happening from
137 * the beginning of the file). That should fill the entire buffer with
138 * all the data the object has to offer for that attribute.
139 * We then call flush_read_buffer() to copy the buffer to userspace
140 * in the increments specified.
141 */
142
143static ssize_t
144sysfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
145{
146 struct sysfs_buffer * buffer = file->private_data;
147 ssize_t retval = 0;
148
Dave Young52e8c202007-07-26 11:03:54 +0000149 mutex_lock(&buffer->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 if (buffer->needs_read_fill) {
Tejun Heo73107cb2007-06-14 03:45:16 +0900151 retval = fill_read_buffer(file->f_path.dentry,buffer);
Alan Sterne7b0d262007-03-15 15:51:28 -0400152 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 goto out;
154 }
Zach Brown5c1fdf42006-10-03 01:16:06 -0700155 pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",
156 __FUNCTION__, count, *ppos, buffer->page);
Akinobu Mita92f4c702007-05-09 02:33:32 -0700157 retval = simple_read_from_buffer(buf, count, ppos, buffer->page,
158 buffer->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159out:
Dave Young52e8c202007-07-26 11:03:54 +0000160 mutex_unlock(&buffer->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 return retval;
162}
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164/**
165 * fill_write_buffer - copy buffer from userspace.
166 * @buffer: data buffer for file.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700167 * @buf: data from user.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 * @count: number of bytes in @userbuf.
169 *
170 * Allocate @buffer->page if it hasn't been already, then
171 * copy the user-supplied buffer into it.
172 */
173
174static int
175fill_write_buffer(struct sysfs_buffer * buffer, const char __user * buf, size_t count)
176{
177 int error;
178
179 if (!buffer->page)
180 buffer->page = (char *)get_zeroed_page(GFP_KERNEL);
181 if (!buffer->page)
182 return -ENOMEM;
183
184 if (count >= PAGE_SIZE)
Greg Kroah-Hartman6e0dd742006-03-31 15:37:06 -0800185 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 error = copy_from_user(buffer->page,buf,count);
187 buffer->needs_read_fill = 1;
Thomas Maier035ed7a2006-10-22 19:17:47 +0200188 /* if buf is assumed to contain a string, terminate it by \0,
189 so e.g. sscanf() can scan the string easily */
190 buffer->page[count] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return error ? -EFAULT : count;
192}
193
194
195/**
196 * flush_write_buffer - push buffer to kobject.
Martin Waitz3d410882005-06-23 22:05:21 -0700197 * @dentry: dentry to the attribute
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 * @buffer: data buffer for file.
Martin Waitz3d410882005-06-23 22:05:21 -0700199 * @count: number of bytes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 *
201 * Get the correct pointers for the kobject and the attribute we're
202 * dealing with, then call the store() method for the attribute,
203 * passing the buffer that we acquired in fill_write_buffer().
204 */
205
Tejun Heo0ab66082007-06-14 03:45:16 +0900206static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207flush_write_buffer(struct dentry * dentry, struct sysfs_buffer * buffer, size_t count)
208{
Tejun Heo3e519032007-06-14 03:45:15 +0900209 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900210 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 struct sysfs_ops * ops = buffer->ops;
Tejun Heo0ab66082007-06-14 03:45:16 +0900212 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Tejun Heo0ab66082007-06-14 03:45:16 +0900214 /* need attr_sd for attr and ops, its parent for kobj */
215 if (!sysfs_get_active_two(attr_sd))
216 return -ENODEV;
217
Tejun Heob1fc3d62007-09-20 16:05:11 +0900218 rc = ops->store(kobj, attr_sd->s_attr.attr, buffer->page, count);
Tejun Heo0ab66082007-06-14 03:45:16 +0900219
220 sysfs_put_active_two(attr_sd);
221
222 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
225
226/**
227 * sysfs_write_file - write an attribute.
228 * @file: file pointer
229 * @buf: data to write
230 * @count: number of bytes
231 * @ppos: starting offset
232 *
233 * Similar to sysfs_read_file(), though working in the opposite direction.
234 * We allocate and fill the data from the user in fill_write_buffer(),
235 * then push it to the kobject in flush_write_buffer().
236 * There is no easy way for us to know if userspace is only doing a partial
237 * write, so we don't support them. We expect the entire buffer to come
238 * on the first write.
239 * Hint: if you're writing a value, first read the file, modify only the
240 * the value you're changing, then write entire buffer back.
241 */
242
243static ssize_t
244sysfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
245{
246 struct sysfs_buffer * buffer = file->private_data;
247 ssize_t len;
248
Dave Young52e8c202007-07-26 11:03:54 +0000249 mutex_lock(&buffer->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 len = fill_write_buffer(buffer, buf, count);
251 if (len > 0)
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800252 len = flush_write_buffer(file->f_path.dentry, buffer, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if (len > 0)
254 *ppos += len;
Dave Young52e8c202007-07-26 11:03:54 +0000255 mutex_unlock(&buffer->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return len;
257}
258
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900259/**
260 * sysfs_get_open_dirent - get or create sysfs_open_dirent
261 * @sd: target sysfs_dirent
262 * @buffer: sysfs_buffer for this instance of open
263 *
264 * If @sd->s_attr.open exists, increment its reference count;
265 * otherwise, create one. @buffer is chained to the buffers
266 * list.
267 *
268 * LOCKING:
269 * Kernel thread context (may sleep).
270 *
271 * RETURNS:
272 * 0 on success, -errno on failure.
273 */
274static int sysfs_get_open_dirent(struct sysfs_dirent *sd,
275 struct sysfs_buffer *buffer)
276{
277 struct sysfs_open_dirent *od, *new_od = NULL;
278
279 retry:
280 spin_lock(&sysfs_open_dirent_lock);
281
282 if (!sd->s_attr.open && new_od) {
283 sd->s_attr.open = new_od;
284 new_od = NULL;
285 }
286
287 od = sd->s_attr.open;
288 if (od) {
289 atomic_inc(&od->refcnt);
290 list_add_tail(&buffer->list, &od->buffers);
291 }
292
293 spin_unlock(&sysfs_open_dirent_lock);
294
295 if (od) {
296 kfree(new_od);
297 return 0;
298 }
299
300 /* not there, initialize a new one and retry */
301 new_od = kmalloc(sizeof(*new_od), GFP_KERNEL);
302 if (!new_od)
303 return -ENOMEM;
304
305 atomic_set(&new_od->refcnt, 0);
Tejun Heoa4e8b912007-09-20 16:05:12 +0900306 atomic_set(&new_od->event, 1);
307 init_waitqueue_head(&new_od->poll);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900308 INIT_LIST_HEAD(&new_od->buffers);
309 goto retry;
310}
311
312/**
313 * sysfs_put_open_dirent - put sysfs_open_dirent
314 * @sd: target sysfs_dirent
315 * @buffer: associated sysfs_buffer
316 *
317 * Put @sd->s_attr.open and unlink @buffer from the buffers list.
318 * If reference count reaches zero, disassociate and free it.
319 *
320 * LOCKING:
321 * None.
322 */
323static void sysfs_put_open_dirent(struct sysfs_dirent *sd,
324 struct sysfs_buffer *buffer)
325{
326 struct sysfs_open_dirent *od = sd->s_attr.open;
327
328 spin_lock(&sysfs_open_dirent_lock);
329
330 list_del(&buffer->list);
331 if (atomic_dec_and_test(&od->refcnt))
332 sd->s_attr.open = NULL;
333 else
334 od = NULL;
335
336 spin_unlock(&sysfs_open_dirent_lock);
337
338 kfree(od);
339}
340
Oliver Neukum94bebf42006-12-20 10:52:44 +0100341static int sysfs_open_file(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
Tejun Heo3e519032007-06-14 03:45:15 +0900343 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900344 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 struct sysfs_buffer * buffer;
346 struct sysfs_ops * ops = NULL;
Tejun Heo0ab66082007-06-14 03:45:16 +0900347 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Tejun Heo0ab66082007-06-14 03:45:16 +0900349 /* need attr_sd for attr and ops, its parent for kobj */
350 if (!sysfs_get_active_two(attr_sd))
351 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 /* if the kobject has no ktype, then we assume that it is a subsystem
354 * itself, and use ops for it.
355 */
356 if (kobj->kset && kobj->kset->ktype)
357 ops = kobj->kset->ktype->sysfs_ops;
358 else if (kobj->ktype)
359 ops = kobj->ktype->sysfs_ops;
360 else
361 ops = &subsys_sysfs_ops;
362
Tejun Heo73107cb2007-06-14 03:45:16 +0900363 error = -EACCES;
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 /* No sysfs operations, either from having no subsystem,
366 * or the subsystem have no operations.
367 */
368 if (!ops)
Tejun Heo7b595752007-06-14 03:45:17 +0900369 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 /* File needs write support.
372 * The inode's perms must say it's ok,
373 * and we must have a store method.
374 */
375 if (file->f_mode & FMODE_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 if (!(inode->i_mode & S_IWUGO) || !ops->store)
Tejun Heo7b595752007-06-14 03:45:17 +0900377 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 }
379
380 /* File needs read support.
381 * The inode's perms must say it's ok, and we there
382 * must be a show method for it.
383 */
384 if (file->f_mode & FMODE_READ) {
385 if (!(inode->i_mode & S_IRUGO) || !ops->show)
Tejun Heo7b595752007-06-14 03:45:17 +0900386 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388
389 /* No error? Great, allocate a buffer for the file, and store it
390 * it in file->private_data for easy access.
391 */
Tejun Heo0ab66082007-06-14 03:45:16 +0900392 error = -ENOMEM;
Eric Sesterhenn58d49282006-02-22 11:18:15 +0100393 buffer = kzalloc(sizeof(struct sysfs_buffer), GFP_KERNEL);
Tejun Heo0ab66082007-06-14 03:45:16 +0900394 if (!buffer)
Tejun Heo7b595752007-06-14 03:45:17 +0900395 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Dave Young52e8c202007-07-26 11:03:54 +0000397 mutex_init(&buffer->mutex);
Tejun Heo0ab66082007-06-14 03:45:16 +0900398 buffer->needs_read_fill = 1;
399 buffer->ops = ops;
Tejun Heo0ab66082007-06-14 03:45:16 +0900400 file->private_data = buffer;
401
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900402 /* make sure we have open dirent struct */
403 error = sysfs_get_open_dirent(attr_sd, buffer);
404 if (error)
405 goto err_free;
406
Tejun Heob05f0542007-09-20 16:05:10 +0900407 /* open succeeded, put active references */
Tejun Heo0ab66082007-06-14 03:45:16 +0900408 sysfs_put_active_two(attr_sd);
Tejun Heo0ab66082007-06-14 03:45:16 +0900409 return 0;
410
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900411 err_free:
412 kfree(buffer);
Tejun Heo7b595752007-06-14 03:45:17 +0900413 err_out:
Tejun Heo0ab66082007-06-14 03:45:16 +0900414 sysfs_put_active_two(attr_sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return error;
416}
417
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900418static int sysfs_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900420 struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata;
Tejun Heo73107cb2007-06-14 03:45:16 +0900421 struct sysfs_buffer *buffer = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900423 sysfs_put_open_dirent(sd, buffer);
424
Tejun Heo50ab1a72007-09-20 16:05:10 +0900425 if (buffer->page)
426 free_page((unsigned long)buffer->page);
427 kfree(buffer);
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return 0;
430}
431
NeilBrown4508a7a2006-03-20 17:53:53 +1100432/* Sysfs attribute files are pollable. The idea is that you read
433 * the content and then you use 'poll' or 'select' to wait for
434 * the content to change. When the content changes (assuming the
435 * manager for the kobject supports notification), poll will
436 * return POLLERR|POLLPRI, and select will return the fd whether
437 * it is waiting for read, write, or exceptions.
438 * Once poll/select indicates that the value has changed, you
439 * need to close and re-open the file, as simply seeking and reading
440 * again will not get new data, or reset the state of 'poll'.
441 * Reminder: this only works for attributes which actively support
442 * it, and it is not possible to test an attribute from userspace
Rolf Eike Beera93720e2007-08-10 13:51:07 -0700443 * to see if it supports poll (Neither 'poll' nor 'select' return
NeilBrown4508a7a2006-03-20 17:53:53 +1100444 * an appropriate error code). When in doubt, set a suitable timeout value.
445 */
446static unsigned int sysfs_poll(struct file *filp, poll_table *wait)
447{
448 struct sysfs_buffer * buffer = filp->private_data;
Tejun Heo0ab66082007-06-14 03:45:16 +0900449 struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata;
Tejun Heoa4e8b912007-09-20 16:05:12 +0900450 struct sysfs_open_dirent *od = attr_sd->s_attr.open;
Tejun Heo0ab66082007-06-14 03:45:16 +0900451
452 /* need parent for the kobj, grab both */
453 if (!sysfs_get_active_two(attr_sd))
454 goto trigger;
NeilBrown4508a7a2006-03-20 17:53:53 +1100455
Tejun Heoa4e8b912007-09-20 16:05:12 +0900456 poll_wait(filp, &od->poll, wait);
NeilBrown4508a7a2006-03-20 17:53:53 +1100457
Tejun Heo0ab66082007-06-14 03:45:16 +0900458 sysfs_put_active_two(attr_sd);
NeilBrown4508a7a2006-03-20 17:53:53 +1100459
Tejun Heoa4e8b912007-09-20 16:05:12 +0900460 if (buffer->event != atomic_read(&od->event))
Tejun Heo0ab66082007-06-14 03:45:16 +0900461 goto trigger;
462
463 return 0;
464
465 trigger:
466 buffer->needs_read_fill = 1;
467 return POLLERR|POLLPRI;
NeilBrown4508a7a2006-03-20 17:53:53 +1100468}
469
Tejun Heo51225032007-06-14 04:27:25 +0900470void sysfs_notify(struct kobject *k, char *dir, char *attr)
NeilBrown4508a7a2006-03-20 17:53:53 +1100471{
Tejun Heo51225032007-06-14 04:27:25 +0900472 struct sysfs_dirent *sd = k->sd;
NeilBrown4508a7a2006-03-20 17:53:53 +1100473
Tejun Heo51225032007-06-14 04:27:25 +0900474 mutex_lock(&sysfs_mutex);
NeilBrown4508a7a2006-03-20 17:53:53 +1100475
Tejun Heo51225032007-06-14 04:27:25 +0900476 if (sd && dir)
477 sd = sysfs_find_dirent(sd, dir);
478 if (sd && attr)
479 sd = sysfs_find_dirent(sd, attr);
480 if (sd) {
Tejun Heoa4e8b912007-09-20 16:05:12 +0900481 struct sysfs_open_dirent *od;
482
483 spin_lock(&sysfs_open_dirent_lock);
484
485 od = sd->s_attr.open;
486 if (od) {
487 atomic_inc(&od->event);
488 wake_up_interruptible(&od->poll);
489 }
490
491 spin_unlock(&sysfs_open_dirent_lock);
NeilBrown4508a7a2006-03-20 17:53:53 +1100492 }
Tejun Heo51225032007-06-14 04:27:25 +0900493
494 mutex_unlock(&sysfs_mutex);
NeilBrown4508a7a2006-03-20 17:53:53 +1100495}
496EXPORT_SYMBOL_GPL(sysfs_notify);
497
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800498const struct file_operations sysfs_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 .read = sysfs_read_file,
500 .write = sysfs_write_file,
501 .llseek = generic_file_llseek,
502 .open = sysfs_open_file,
503 .release = sysfs_release,
NeilBrown4508a7a2006-03-20 17:53:53 +1100504 .poll = sysfs_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505};
506
507
Tejun Heo608e2662007-06-14 04:27:22 +0900508int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,
509 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 umode_t mode = (attr->mode & S_IALLUGO) | S_IFREG;
Tejun Heofb6896d2007-06-14 04:27:24 +0900512 struct sysfs_addrm_cxt acxt;
Tejun Heoa26cd722007-06-14 03:45:14 +0900513 struct sysfs_dirent *sd;
Tejun Heo23dc2792007-08-02 21:38:03 +0900514 int rc;
Tejun Heoa26cd722007-06-14 03:45:14 +0900515
Tejun Heo3e519032007-06-14 03:45:15 +0900516 sd = sysfs_new_dirent(attr->name, mode, type);
Tejun Heo3007e992007-06-14 04:27:23 +0900517 if (!sd)
518 return -ENOMEM;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900519 sd->s_attr.attr = (void *)attr;
Tejun Heoa26cd722007-06-14 03:45:14 +0900520
Tejun Heofb6896d2007-06-14 04:27:24 +0900521 sysfs_addrm_start(&acxt, dir_sd);
Tejun Heo23dc2792007-08-02 21:38:03 +0900522 rc = sysfs_add_one(&acxt, sd);
523 sysfs_addrm_finish(&acxt);
Tejun Heo3007e992007-06-14 04:27:23 +0900524
Tejun Heo23dc2792007-08-02 21:38:03 +0900525 if (rc)
Tejun Heo967e35d2007-07-18 16:38:11 +0900526 sysfs_put(sd);
Tejun Heo3007e992007-06-14 04:27:23 +0900527
Tejun Heo23dc2792007-08-02 21:38:03 +0900528 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
531
532/**
533 * sysfs_create_file - create an attribute file for an object.
534 * @kobj: object we're creating for.
535 * @attr: atrribute descriptor.
536 */
537
538int sysfs_create_file(struct kobject * kobj, const struct attribute * attr)
539{
Tejun Heo608e2662007-06-14 04:27:22 +0900540 BUG_ON(!kobj || !kobj->sd || !attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Tejun Heo608e2662007-06-14 04:27:22 +0900542 return sysfs_add_file(kobj->sd, attr, SYSFS_KOBJ_ATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544}
545
546
547/**
Alan Sterndfa87c82007-02-20 15:02:44 -0500548 * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
549 * @kobj: object we're acting for.
550 * @attr: attribute descriptor.
551 * @group: group name.
552 */
553int sysfs_add_file_to_group(struct kobject *kobj,
554 const struct attribute *attr, const char *group)
555{
Tejun Heo608e2662007-06-14 04:27:22 +0900556 struct sysfs_dirent *dir_sd;
Alan Sterndfa87c82007-02-20 15:02:44 -0500557 int error;
558
Tejun Heo608e2662007-06-14 04:27:22 +0900559 dir_sd = sysfs_get_dirent(kobj->sd, group);
560 if (!dir_sd)
561 return -ENOENT;
562
563 error = sysfs_add_file(dir_sd, attr, SYSFS_KOBJ_ATTR);
564 sysfs_put(dir_sd);
565
Alan Sterndfa87c82007-02-20 15:02:44 -0500566 return error;
567}
568EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570/**
Kay Sievers31e5abe2005-04-18 21:57:32 -0700571 * sysfs_chmod_file - update the modified mode value on an object attribute.
572 * @kobj: object we're acting for.
573 * @attr: attribute descriptor.
574 * @mode: file permissions.
575 *
576 */
577int sysfs_chmod_file(struct kobject *kobj, struct attribute *attr, mode_t mode)
578{
Tejun Heo51225032007-06-14 04:27:25 +0900579 struct sysfs_dirent *victim_sd = NULL;
580 struct dentry *victim = NULL;
Maneesh Sonibc062b12005-07-29 12:13:35 -0700581 struct inode * inode;
582 struct iattr newattrs;
Tejun Heo51225032007-06-14 04:27:25 +0900583 int rc;
Kay Sievers31e5abe2005-04-18 21:57:32 -0700584
Tejun Heo51225032007-06-14 04:27:25 +0900585 rc = -ENOENT;
586 victim_sd = sysfs_get_dirent(kobj->sd, attr->name);
587 if (!victim_sd)
588 goto out;
589
Eric W. Biederman932ea2e2007-08-20 21:36:30 +0900590 mutex_lock(&sysfs_rename_mutex);
Tejun Heo51225032007-06-14 04:27:25 +0900591 victim = sysfs_get_dentry(victim_sd);
Eric W. Biederman932ea2e2007-08-20 21:36:30 +0900592 mutex_unlock(&sysfs_rename_mutex);
Tejun Heo51225032007-06-14 04:27:25 +0900593 if (IS_ERR(victim)) {
594 rc = PTR_ERR(victim);
595 victim = NULL;
596 goto out;
Kay Sievers31e5abe2005-04-18 21:57:32 -0700597 }
Kay Sievers31e5abe2005-04-18 21:57:32 -0700598
Tejun Heo51225032007-06-14 04:27:25 +0900599 inode = victim->d_inode;
Tejun Heof88123e2007-09-20 16:05:10 +0900600
Tejun Heo51225032007-06-14 04:27:25 +0900601 mutex_lock(&inode->i_mutex);
Tejun Heof88123e2007-09-20 16:05:10 +0900602
Tejun Heo51225032007-06-14 04:27:25 +0900603 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
604 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
605 rc = notify_change(victim, &newattrs);
Tejun Heof88123e2007-09-20 16:05:10 +0900606
607 if (rc == 0) {
608 mutex_lock(&sysfs_mutex);
609 victim_sd->s_mode = newattrs.ia_mode;
610 mutex_unlock(&sysfs_mutex);
611 }
612
Tejun Heo51225032007-06-14 04:27:25 +0900613 mutex_unlock(&inode->i_mutex);
614 out:
615 dput(victim);
616 sysfs_put(victim_sd);
617 return rc;
Kay Sievers31e5abe2005-04-18 21:57:32 -0700618}
619EXPORT_SYMBOL_GPL(sysfs_chmod_file);
620
621
622/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 * sysfs_remove_file - remove an object attribute.
624 * @kobj: object we're acting for.
625 * @attr: attribute descriptor.
626 *
627 * Hash the attribute name and kill the victim.
628 */
629
630void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr)
631{
Tejun Heo608e2662007-06-14 04:27:22 +0900632 sysfs_hash_and_remove(kobj->sd, attr->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634
635
Alan Sterndfa87c82007-02-20 15:02:44 -0500636/**
637 * sysfs_remove_file_from_group - remove an attribute file from a group.
638 * @kobj: object we're acting for.
639 * @attr: attribute descriptor.
640 * @group: group name.
641 */
642void sysfs_remove_file_from_group(struct kobject *kobj,
643 const struct attribute *attr, const char *group)
644{
Tejun Heo608e2662007-06-14 04:27:22 +0900645 struct sysfs_dirent *dir_sd;
Alan Sterndfa87c82007-02-20 15:02:44 -0500646
Tejun Heo608e2662007-06-14 04:27:22 +0900647 dir_sd = sysfs_get_dirent(kobj->sd, group);
648 if (dir_sd) {
649 sysfs_hash_and_remove(dir_sd, attr->name);
650 sysfs_put(dir_sd);
Alan Sterndfa87c82007-02-20 15:02:44 -0500651 }
652}
653EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
654
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400655struct sysfs_schedule_callback_struct {
656 struct kobject *kobj;
657 void (*func)(void *);
658 void *data;
Alan Stern523ded72007-04-26 00:12:04 -0700659 struct module *owner;
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400660 struct work_struct work;
661};
662
663static void sysfs_schedule_callback_work(struct work_struct *work)
664{
665 struct sysfs_schedule_callback_struct *ss = container_of(work,
666 struct sysfs_schedule_callback_struct, work);
667
668 (ss->func)(ss->data);
669 kobject_put(ss->kobj);
Alan Stern523ded72007-04-26 00:12:04 -0700670 module_put(ss->owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400671 kfree(ss);
672}
673
674/**
675 * sysfs_schedule_callback - helper to schedule a callback for a kobject
676 * @kobj: object we're acting for.
677 * @func: callback function to invoke later.
678 * @data: argument to pass to @func.
Alan Stern523ded72007-04-26 00:12:04 -0700679 * @owner: module owning the callback code
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400680 *
681 * sysfs attribute methods must not unregister themselves or their parent
682 * kobject (which would amount to the same thing). Attempts to do so will
683 * deadlock, since unregistration is mutually exclusive with driver
684 * callbacks.
685 *
686 * Instead methods can call this routine, which will attempt to allocate
687 * and schedule a workqueue request to call back @func with @data as its
688 * argument in the workqueue's process context. @kobj will be pinned
689 * until @func returns.
690 *
691 * Returns 0 if the request was submitted, -ENOMEM if storage could not
Alan Stern523ded72007-04-26 00:12:04 -0700692 * be allocated, -ENODEV if a reference to @owner isn't available.
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400693 */
694int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
Alan Stern523ded72007-04-26 00:12:04 -0700695 void *data, struct module *owner)
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400696{
697 struct sysfs_schedule_callback_struct *ss;
698
Alan Stern523ded72007-04-26 00:12:04 -0700699 if (!try_module_get(owner))
700 return -ENODEV;
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400701 ss = kmalloc(sizeof(*ss), GFP_KERNEL);
Alan Stern523ded72007-04-26 00:12:04 -0700702 if (!ss) {
703 module_put(owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400704 return -ENOMEM;
Alan Stern523ded72007-04-26 00:12:04 -0700705 }
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400706 kobject_get(kobj);
707 ss->kobj = kobj;
708 ss->func = func;
709 ss->data = data;
Alan Stern523ded72007-04-26 00:12:04 -0700710 ss->owner = owner;
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400711 INIT_WORK(&ss->work, sysfs_schedule_callback_work);
712 schedule_work(&ss->work);
713 return 0;
714}
715EXPORT_SYMBOL_GPL(sysfs_schedule_callback);
716
Alan Sterndfa87c82007-02-20 15:02:44 -0500717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718EXPORT_SYMBOL_GPL(sysfs_create_file);
719EXPORT_SYMBOL_GPL(sysfs_remove_file);