blob: d3be1e7fb48b6d3ad4ccb7ceccf1b1fbf637767e [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
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -070023#define to_sattr(a) container_of(a,struct subsys_attribute, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Martin Waitz3d410882005-06-23 22:05:21 -070025/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 * Subsystem file operations.
27 * These operations allow subsystems to have files that can be
28 * read/written.
29 */
30static ssize_t
31subsys_attr_show(struct kobject * kobj, struct attribute * attr, char * page)
32{
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -070033 struct kset *kset = to_kset(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 struct subsys_attribute * sattr = to_sattr(attr);
Dmitry Torokhovc76d0ab2005-04-29 01:22:00 -050035 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37 if (sattr->show)
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -070038 ret = sattr->show(kset, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 return ret;
40}
41
42static ssize_t
43subsys_attr_store(struct kobject * kobj, struct attribute * attr,
44 const char * page, size_t count)
45{
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -070046 struct kset *kset = to_kset(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 struct subsys_attribute * sattr = to_sattr(attr);
Dmitry Torokhovc76d0ab2005-04-29 01:22:00 -050048 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50 if (sattr->store)
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -070051 ret = sattr->store(kset, page, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 return ret;
53}
54
55static struct sysfs_ops subsys_sysfs_ops = {
56 .show = subsys_attr_show,
57 .store = subsys_attr_store,
58};
59
Tejun Heo85a4ffa2007-09-20 16:05:12 +090060/*
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 */
69static spinlock_t sysfs_open_dirent_lock = SPIN_LOCK_UNLOCKED;
70
71struct sysfs_open_dirent {
72 atomic_t refcnt;
Tejun Heoa4e8b912007-09-20 16:05:12 +090073 atomic_t event;
74 wait_queue_head_t poll;
Tejun Heo85a4ffa2007-09-20 16:05:12 +090075 struct list_head buffers; /* goes through sysfs_buffer.list */
76};
77
Tejun Heo73107cb2007-06-14 03:45:16 +090078struct sysfs_buffer {
79 size_t count;
80 loff_t pos;
81 char * page;
82 struct sysfs_ops * ops;
Dave Young52e8c202007-07-26 11:03:54 +000083 struct mutex mutex;
Tejun Heo73107cb2007-06-14 03:45:16 +090084 int needs_read_fill;
85 int event;
Tejun Heo85a4ffa2007-09-20 16:05:12 +090086 struct list_head list;
Tejun Heo73107cb2007-06-14 03:45:16 +090087};
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
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 Neukum82244b12007-01-02 08:48:08 +010097 * This is called only once, on the file's first read unless an error
98 * is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 */
100static int fill_read_buffer(struct dentry * dentry, struct sysfs_buffer * buffer)
101{
Tejun Heo0ab66082007-06-14 03:45:16 +0900102 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900103 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 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 Heo0ab66082007-06-14 03:45:16 +0900113 /* need attr_sd for attr and ops, its parent for kobj */
114 if (!sysfs_get_active_two(attr_sd))
115 return -ENODEV;
116
Tejun Heoa4e8b912007-09-20 16:05:12 +0900117 buffer->event = atomic_read(&attr_sd->s_attr.open->event);
Tejun Heob1fc3d62007-09-20 16:05:11 +0900118 count = ops->show(kobj, attr_sd->s_attr.attr, buffer->page);
Tejun Heo0ab66082007-06-14 03:45:16 +0900119
120 sysfs_put_active_two(attr_sd);
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 BUG_ON(count > (ssize_t)PAGE_SIZE);
Oliver Neukum82244b12007-01-02 08:48:08 +0100123 if (count >= 0) {
124 buffer->needs_read_fill = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 buffer->count = count;
Oliver Neukum82244b12007-01-02 08:48:08 +0100126 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 ret = count;
Oliver Neukum82244b12007-01-02 08:48:08 +0100128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 return ret;
130}
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132/**
133 * sysfs_read_file - read an attribute.
134 * @file: file pointer.
135 * @buf: buffer to fill.
136 * @count: number of bytes to read.
137 * @ppos: starting offset in file.
138 *
139 * Userspace wants to read an attribute file. The attribute descriptor
140 * is in the file's ->d_fsdata. The target object is in the directory's
141 * ->d_fsdata.
142 *
143 * We call fill_read_buffer() to allocate and fill the buffer from the
144 * object's show() method exactly once (if the read is happening from
145 * the beginning of the file). That should fill the entire buffer with
146 * all the data the object has to offer for that attribute.
147 * We then call flush_read_buffer() to copy the buffer to userspace
148 * in the increments specified.
149 */
150
151static ssize_t
152sysfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
153{
154 struct sysfs_buffer * buffer = file->private_data;
155 ssize_t retval = 0;
156
Dave Young52e8c202007-07-26 11:03:54 +0000157 mutex_lock(&buffer->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 if (buffer->needs_read_fill) {
Tejun Heo73107cb2007-06-14 03:45:16 +0900159 retval = fill_read_buffer(file->f_path.dentry,buffer);
Alan Sterne7b0d262007-03-15 15:51:28 -0400160 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 goto out;
162 }
Zach Brown5c1fdf42006-10-03 01:16:06 -0700163 pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",
164 __FUNCTION__, count, *ppos, buffer->page);
Akinobu Mita92f4c702007-05-09 02:33:32 -0700165 retval = simple_read_from_buffer(buf, count, ppos, buffer->page,
166 buffer->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167out:
Dave Young52e8c202007-07-26 11:03:54 +0000168 mutex_unlock(&buffer->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 return retval;
170}
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172/**
173 * fill_write_buffer - copy buffer from userspace.
174 * @buffer: data buffer for file.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700175 * @buf: data from user.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 * @count: number of bytes in @userbuf.
177 *
178 * Allocate @buffer->page if it hasn't been already, then
179 * copy the user-supplied buffer into it.
180 */
181
182static int
183fill_write_buffer(struct sysfs_buffer * buffer, const char __user * buf, size_t count)
184{
185 int error;
186
187 if (!buffer->page)
188 buffer->page = (char *)get_zeroed_page(GFP_KERNEL);
189 if (!buffer->page)
190 return -ENOMEM;
191
192 if (count >= PAGE_SIZE)
Greg Kroah-Hartman6e0dd742006-03-31 15:37:06 -0800193 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 error = copy_from_user(buffer->page,buf,count);
195 buffer->needs_read_fill = 1;
Thomas Maier035ed7a2006-10-22 19:17:47 +0200196 /* if buf is assumed to contain a string, terminate it by \0,
197 so e.g. sscanf() can scan the string easily */
198 buffer->page[count] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return error ? -EFAULT : count;
200}
201
202
203/**
204 * flush_write_buffer - push buffer to kobject.
Martin Waitz3d410882005-06-23 22:05:21 -0700205 * @dentry: dentry to the attribute
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 * @buffer: data buffer for file.
Martin Waitz3d410882005-06-23 22:05:21 -0700207 * @count: number of bytes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 *
209 * Get the correct pointers for the kobject and the attribute we're
210 * dealing with, then call the store() method for the attribute,
211 * passing the buffer that we acquired in fill_write_buffer().
212 */
213
Tejun Heo0ab66082007-06-14 03:45:16 +0900214static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215flush_write_buffer(struct dentry * dentry, struct sysfs_buffer * buffer, size_t count)
216{
Tejun Heo3e519032007-06-14 03:45:15 +0900217 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900218 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 struct sysfs_ops * ops = buffer->ops;
Tejun Heo0ab66082007-06-14 03:45:16 +0900220 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Tejun Heo0ab66082007-06-14 03:45:16 +0900222 /* need attr_sd for attr and ops, its parent for kobj */
223 if (!sysfs_get_active_two(attr_sd))
224 return -ENODEV;
225
Tejun Heob1fc3d62007-09-20 16:05:11 +0900226 rc = ops->store(kobj, attr_sd->s_attr.attr, buffer->page, count);
Tejun Heo0ab66082007-06-14 03:45:16 +0900227
228 sysfs_put_active_two(attr_sd);
229
230 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
232
233
234/**
235 * sysfs_write_file - write an attribute.
236 * @file: file pointer
237 * @buf: data to write
238 * @count: number of bytes
239 * @ppos: starting offset
240 *
241 * Similar to sysfs_read_file(), though working in the opposite direction.
242 * We allocate and fill the data from the user in fill_write_buffer(),
243 * then push it to the kobject in flush_write_buffer().
244 * There is no easy way for us to know if userspace is only doing a partial
245 * write, so we don't support them. We expect the entire buffer to come
246 * on the first write.
247 * Hint: if you're writing a value, first read the file, modify only the
248 * the value you're changing, then write entire buffer back.
249 */
250
251static ssize_t
252sysfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
253{
254 struct sysfs_buffer * buffer = file->private_data;
255 ssize_t len;
256
Dave Young52e8c202007-07-26 11:03:54 +0000257 mutex_lock(&buffer->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 len = fill_write_buffer(buffer, buf, count);
259 if (len > 0)
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800260 len = flush_write_buffer(file->f_path.dentry, buffer, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (len > 0)
262 *ppos += len;
Dave Young52e8c202007-07-26 11:03:54 +0000263 mutex_unlock(&buffer->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return len;
265}
266
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900267/**
268 * sysfs_get_open_dirent - get or create sysfs_open_dirent
269 * @sd: target sysfs_dirent
270 * @buffer: sysfs_buffer for this instance of open
271 *
272 * If @sd->s_attr.open exists, increment its reference count;
273 * otherwise, create one. @buffer is chained to the buffers
274 * list.
275 *
276 * LOCKING:
277 * Kernel thread context (may sleep).
278 *
279 * RETURNS:
280 * 0 on success, -errno on failure.
281 */
282static int sysfs_get_open_dirent(struct sysfs_dirent *sd,
283 struct sysfs_buffer *buffer)
284{
285 struct sysfs_open_dirent *od, *new_od = NULL;
286
287 retry:
288 spin_lock(&sysfs_open_dirent_lock);
289
290 if (!sd->s_attr.open && new_od) {
291 sd->s_attr.open = new_od;
292 new_od = NULL;
293 }
294
295 od = sd->s_attr.open;
296 if (od) {
297 atomic_inc(&od->refcnt);
298 list_add_tail(&buffer->list, &od->buffers);
299 }
300
301 spin_unlock(&sysfs_open_dirent_lock);
302
303 if (od) {
304 kfree(new_od);
305 return 0;
306 }
307
308 /* not there, initialize a new one and retry */
309 new_od = kmalloc(sizeof(*new_od), GFP_KERNEL);
310 if (!new_od)
311 return -ENOMEM;
312
313 atomic_set(&new_od->refcnt, 0);
Tejun Heoa4e8b912007-09-20 16:05:12 +0900314 atomic_set(&new_od->event, 1);
315 init_waitqueue_head(&new_od->poll);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900316 INIT_LIST_HEAD(&new_od->buffers);
317 goto retry;
318}
319
320/**
321 * sysfs_put_open_dirent - put sysfs_open_dirent
322 * @sd: target sysfs_dirent
323 * @buffer: associated sysfs_buffer
324 *
325 * Put @sd->s_attr.open and unlink @buffer from the buffers list.
326 * If reference count reaches zero, disassociate and free it.
327 *
328 * LOCKING:
329 * None.
330 */
331static void sysfs_put_open_dirent(struct sysfs_dirent *sd,
332 struct sysfs_buffer *buffer)
333{
334 struct sysfs_open_dirent *od = sd->s_attr.open;
335
336 spin_lock(&sysfs_open_dirent_lock);
337
338 list_del(&buffer->list);
339 if (atomic_dec_and_test(&od->refcnt))
340 sd->s_attr.open = NULL;
341 else
342 od = NULL;
343
344 spin_unlock(&sysfs_open_dirent_lock);
345
346 kfree(od);
347}
348
Oliver Neukum94bebf42006-12-20 10:52:44 +0100349static int sysfs_open_file(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
Tejun Heo3e519032007-06-14 03:45:15 +0900351 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900352 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 struct sysfs_buffer * buffer;
354 struct sysfs_ops * ops = NULL;
Tejun Heo0ab66082007-06-14 03:45:16 +0900355 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Tejun Heo0ab66082007-06-14 03:45:16 +0900357 /* need attr_sd for attr and ops, its parent for kobj */
358 if (!sysfs_get_active_two(attr_sd))
359 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 /* if the kobject has no ktype, then we assume that it is a subsystem
362 * itself, and use ops for it.
363 */
364 if (kobj->kset && kobj->kset->ktype)
365 ops = kobj->kset->ktype->sysfs_ops;
366 else if (kobj->ktype)
367 ops = kobj->ktype->sysfs_ops;
368 else
369 ops = &subsys_sysfs_ops;
370
Tejun Heo73107cb2007-06-14 03:45:16 +0900371 error = -EACCES;
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 /* No sysfs operations, either from having no subsystem,
374 * or the subsystem have no operations.
375 */
376 if (!ops)
Tejun Heo7b595752007-06-14 03:45:17 +0900377 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 /* File needs write support.
380 * The inode's perms must say it's ok,
381 * and we must have a store method.
382 */
383 if (file->f_mode & FMODE_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (!(inode->i_mode & S_IWUGO) || !ops->store)
Tejun Heo7b595752007-06-14 03:45:17 +0900385 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
387
388 /* File needs read support.
389 * The inode's perms must say it's ok, and we there
390 * must be a show method for it.
391 */
392 if (file->f_mode & FMODE_READ) {
393 if (!(inode->i_mode & S_IRUGO) || !ops->show)
Tejun Heo7b595752007-06-14 03:45:17 +0900394 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
396
397 /* No error? Great, allocate a buffer for the file, and store it
398 * it in file->private_data for easy access.
399 */
Tejun Heo0ab66082007-06-14 03:45:16 +0900400 error = -ENOMEM;
Eric Sesterhenn58d49282006-02-22 11:18:15 +0100401 buffer = kzalloc(sizeof(struct sysfs_buffer), GFP_KERNEL);
Tejun Heo0ab66082007-06-14 03:45:16 +0900402 if (!buffer)
Tejun Heo7b595752007-06-14 03:45:17 +0900403 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Dave Young52e8c202007-07-26 11:03:54 +0000405 mutex_init(&buffer->mutex);
Tejun Heo0ab66082007-06-14 03:45:16 +0900406 buffer->needs_read_fill = 1;
407 buffer->ops = ops;
Tejun Heo0ab66082007-06-14 03:45:16 +0900408 file->private_data = buffer;
409
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900410 /* make sure we have open dirent struct */
411 error = sysfs_get_open_dirent(attr_sd, buffer);
412 if (error)
413 goto err_free;
414
Tejun Heob05f0542007-09-20 16:05:10 +0900415 /* open succeeded, put active references */
Tejun Heo0ab66082007-06-14 03:45:16 +0900416 sysfs_put_active_two(attr_sd);
Tejun Heo0ab66082007-06-14 03:45:16 +0900417 return 0;
418
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900419 err_free:
420 kfree(buffer);
Tejun Heo7b595752007-06-14 03:45:17 +0900421 err_out:
Tejun Heo0ab66082007-06-14 03:45:16 +0900422 sysfs_put_active_two(attr_sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return error;
424}
425
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900426static int sysfs_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900428 struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata;
Tejun Heo73107cb2007-06-14 03:45:16 +0900429 struct sysfs_buffer *buffer = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900431 sysfs_put_open_dirent(sd, buffer);
432
Tejun Heo50ab1a72007-09-20 16:05:10 +0900433 if (buffer->page)
434 free_page((unsigned long)buffer->page);
435 kfree(buffer);
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return 0;
438}
439
NeilBrown4508a7a2006-03-20 17:53:53 +1100440/* Sysfs attribute files are pollable. The idea is that you read
441 * the content and then you use 'poll' or 'select' to wait for
442 * the content to change. When the content changes (assuming the
443 * manager for the kobject supports notification), poll will
444 * return POLLERR|POLLPRI, and select will return the fd whether
445 * it is waiting for read, write, or exceptions.
446 * Once poll/select indicates that the value has changed, you
447 * need to close and re-open the file, as simply seeking and reading
448 * again will not get new data, or reset the state of 'poll'.
449 * Reminder: this only works for attributes which actively support
450 * it, and it is not possible to test an attribute from userspace
Rolf Eike Beera93720e2007-08-10 13:51:07 -0700451 * to see if it supports poll (Neither 'poll' nor 'select' return
NeilBrown4508a7a2006-03-20 17:53:53 +1100452 * an appropriate error code). When in doubt, set a suitable timeout value.
453 */
454static unsigned int sysfs_poll(struct file *filp, poll_table *wait)
455{
456 struct sysfs_buffer * buffer = filp->private_data;
Tejun Heo0ab66082007-06-14 03:45:16 +0900457 struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata;
Tejun Heoa4e8b912007-09-20 16:05:12 +0900458 struct sysfs_open_dirent *od = attr_sd->s_attr.open;
Tejun Heo0ab66082007-06-14 03:45:16 +0900459
460 /* need parent for the kobj, grab both */
461 if (!sysfs_get_active_two(attr_sd))
462 goto trigger;
NeilBrown4508a7a2006-03-20 17:53:53 +1100463
Tejun Heoa4e8b912007-09-20 16:05:12 +0900464 poll_wait(filp, &od->poll, wait);
NeilBrown4508a7a2006-03-20 17:53:53 +1100465
Tejun Heo0ab66082007-06-14 03:45:16 +0900466 sysfs_put_active_two(attr_sd);
NeilBrown4508a7a2006-03-20 17:53:53 +1100467
Tejun Heoa4e8b912007-09-20 16:05:12 +0900468 if (buffer->event != atomic_read(&od->event))
Tejun Heo0ab66082007-06-14 03:45:16 +0900469 goto trigger;
470
471 return 0;
472
473 trigger:
474 buffer->needs_read_fill = 1;
475 return POLLERR|POLLPRI;
NeilBrown4508a7a2006-03-20 17:53:53 +1100476}
477
Tejun Heo51225032007-06-14 04:27:25 +0900478void sysfs_notify(struct kobject *k, char *dir, char *attr)
NeilBrown4508a7a2006-03-20 17:53:53 +1100479{
Tejun Heo51225032007-06-14 04:27:25 +0900480 struct sysfs_dirent *sd = k->sd;
NeilBrown4508a7a2006-03-20 17:53:53 +1100481
Tejun Heo51225032007-06-14 04:27:25 +0900482 mutex_lock(&sysfs_mutex);
NeilBrown4508a7a2006-03-20 17:53:53 +1100483
Tejun Heo51225032007-06-14 04:27:25 +0900484 if (sd && dir)
485 sd = sysfs_find_dirent(sd, dir);
486 if (sd && attr)
487 sd = sysfs_find_dirent(sd, attr);
488 if (sd) {
Tejun Heoa4e8b912007-09-20 16:05:12 +0900489 struct sysfs_open_dirent *od;
490
491 spin_lock(&sysfs_open_dirent_lock);
492
493 od = sd->s_attr.open;
494 if (od) {
495 atomic_inc(&od->event);
496 wake_up_interruptible(&od->poll);
497 }
498
499 spin_unlock(&sysfs_open_dirent_lock);
NeilBrown4508a7a2006-03-20 17:53:53 +1100500 }
Tejun Heo51225032007-06-14 04:27:25 +0900501
502 mutex_unlock(&sysfs_mutex);
NeilBrown4508a7a2006-03-20 17:53:53 +1100503}
504EXPORT_SYMBOL_GPL(sysfs_notify);
505
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800506const struct file_operations sysfs_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 .read = sysfs_read_file,
508 .write = sysfs_write_file,
509 .llseek = generic_file_llseek,
510 .open = sysfs_open_file,
511 .release = sysfs_release,
NeilBrown4508a7a2006-03-20 17:53:53 +1100512 .poll = sysfs_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513};
514
515
Tejun Heo608e2662007-06-14 04:27:22 +0900516int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,
517 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 umode_t mode = (attr->mode & S_IALLUGO) | S_IFREG;
Tejun Heofb6896d2007-06-14 04:27:24 +0900520 struct sysfs_addrm_cxt acxt;
Tejun Heoa26cd722007-06-14 03:45:14 +0900521 struct sysfs_dirent *sd;
Tejun Heo23dc2792007-08-02 21:38:03 +0900522 int rc;
Tejun Heoa26cd722007-06-14 03:45:14 +0900523
Tejun Heo3e519032007-06-14 03:45:15 +0900524 sd = sysfs_new_dirent(attr->name, mode, type);
Tejun Heo3007e992007-06-14 04:27:23 +0900525 if (!sd)
526 return -ENOMEM;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900527 sd->s_attr.attr = (void *)attr;
Tejun Heoa26cd722007-06-14 03:45:14 +0900528
Tejun Heofb6896d2007-06-14 04:27:24 +0900529 sysfs_addrm_start(&acxt, dir_sd);
Tejun Heo23dc2792007-08-02 21:38:03 +0900530 rc = sysfs_add_one(&acxt, sd);
531 sysfs_addrm_finish(&acxt);
Tejun Heo3007e992007-06-14 04:27:23 +0900532
Tejun Heo23dc2792007-08-02 21:38:03 +0900533 if (rc)
Tejun Heo967e35d2007-07-18 16:38:11 +0900534 sysfs_put(sd);
Tejun Heo3007e992007-06-14 04:27:23 +0900535
Tejun Heo23dc2792007-08-02 21:38:03 +0900536 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
539
540/**
541 * sysfs_create_file - create an attribute file for an object.
542 * @kobj: object we're creating for.
543 * @attr: atrribute descriptor.
544 */
545
546int sysfs_create_file(struct kobject * kobj, const struct attribute * attr)
547{
Tejun Heo608e2662007-06-14 04:27:22 +0900548 BUG_ON(!kobj || !kobj->sd || !attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Tejun Heo608e2662007-06-14 04:27:22 +0900550 return sysfs_add_file(kobj->sd, attr, SYSFS_KOBJ_ATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552}
553
554
555/**
Alan Sterndfa87c82007-02-20 15:02:44 -0500556 * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
557 * @kobj: object we're acting for.
558 * @attr: attribute descriptor.
559 * @group: group name.
560 */
561int sysfs_add_file_to_group(struct kobject *kobj,
562 const struct attribute *attr, const char *group)
563{
Tejun Heo608e2662007-06-14 04:27:22 +0900564 struct sysfs_dirent *dir_sd;
Alan Sterndfa87c82007-02-20 15:02:44 -0500565 int error;
566
Tejun Heo608e2662007-06-14 04:27:22 +0900567 dir_sd = sysfs_get_dirent(kobj->sd, group);
568 if (!dir_sd)
569 return -ENOENT;
570
571 error = sysfs_add_file(dir_sd, attr, SYSFS_KOBJ_ATTR);
572 sysfs_put(dir_sd);
573
Alan Sterndfa87c82007-02-20 15:02:44 -0500574 return error;
575}
576EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578/**
Kay Sievers31e5abe2005-04-18 21:57:32 -0700579 * sysfs_chmod_file - update the modified mode value on an object attribute.
580 * @kobj: object we're acting for.
581 * @attr: attribute descriptor.
582 * @mode: file permissions.
583 *
584 */
585int sysfs_chmod_file(struct kobject *kobj, struct attribute *attr, mode_t mode)
586{
Tejun Heo51225032007-06-14 04:27:25 +0900587 struct sysfs_dirent *victim_sd = NULL;
588 struct dentry *victim = NULL;
Maneesh Sonibc062b12005-07-29 12:13:35 -0700589 struct inode * inode;
590 struct iattr newattrs;
Tejun Heo51225032007-06-14 04:27:25 +0900591 int rc;
Kay Sievers31e5abe2005-04-18 21:57:32 -0700592
Tejun Heo51225032007-06-14 04:27:25 +0900593 rc = -ENOENT;
594 victim_sd = sysfs_get_dirent(kobj->sd, attr->name);
595 if (!victim_sd)
596 goto out;
597
Eric W. Biederman932ea2e2007-08-20 21:36:30 +0900598 mutex_lock(&sysfs_rename_mutex);
Tejun Heo51225032007-06-14 04:27:25 +0900599 victim = sysfs_get_dentry(victim_sd);
Eric W. Biederman932ea2e2007-08-20 21:36:30 +0900600 mutex_unlock(&sysfs_rename_mutex);
Tejun Heo51225032007-06-14 04:27:25 +0900601 if (IS_ERR(victim)) {
602 rc = PTR_ERR(victim);
603 victim = NULL;
604 goto out;
Kay Sievers31e5abe2005-04-18 21:57:32 -0700605 }
Kay Sievers31e5abe2005-04-18 21:57:32 -0700606
Tejun Heo51225032007-06-14 04:27:25 +0900607 inode = victim->d_inode;
Tejun Heof88123e2007-09-20 16:05:10 +0900608
Tejun Heo51225032007-06-14 04:27:25 +0900609 mutex_lock(&inode->i_mutex);
Tejun Heof88123e2007-09-20 16:05:10 +0900610
Tejun Heo51225032007-06-14 04:27:25 +0900611 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
612 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
613 rc = notify_change(victim, &newattrs);
Tejun Heof88123e2007-09-20 16:05:10 +0900614
615 if (rc == 0) {
616 mutex_lock(&sysfs_mutex);
617 victim_sd->s_mode = newattrs.ia_mode;
618 mutex_unlock(&sysfs_mutex);
619 }
620
Tejun Heo51225032007-06-14 04:27:25 +0900621 mutex_unlock(&inode->i_mutex);
622 out:
623 dput(victim);
624 sysfs_put(victim_sd);
625 return rc;
Kay Sievers31e5abe2005-04-18 21:57:32 -0700626}
627EXPORT_SYMBOL_GPL(sysfs_chmod_file);
628
629
630/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 * sysfs_remove_file - remove an object attribute.
632 * @kobj: object we're acting for.
633 * @attr: attribute descriptor.
634 *
635 * Hash the attribute name and kill the victim.
636 */
637
638void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr)
639{
Tejun Heo608e2662007-06-14 04:27:22 +0900640 sysfs_hash_and_remove(kobj->sd, attr->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
643
Alan Sterndfa87c82007-02-20 15:02:44 -0500644/**
645 * sysfs_remove_file_from_group - remove an attribute file from a group.
646 * @kobj: object we're acting for.
647 * @attr: attribute descriptor.
648 * @group: group name.
649 */
650void sysfs_remove_file_from_group(struct kobject *kobj,
651 const struct attribute *attr, const char *group)
652{
Tejun Heo608e2662007-06-14 04:27:22 +0900653 struct sysfs_dirent *dir_sd;
Alan Sterndfa87c82007-02-20 15:02:44 -0500654
Tejun Heo608e2662007-06-14 04:27:22 +0900655 dir_sd = sysfs_get_dirent(kobj->sd, group);
656 if (dir_sd) {
657 sysfs_hash_and_remove(dir_sd, attr->name);
658 sysfs_put(dir_sd);
Alan Sterndfa87c82007-02-20 15:02:44 -0500659 }
660}
661EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
662
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400663struct sysfs_schedule_callback_struct {
664 struct kobject *kobj;
665 void (*func)(void *);
666 void *data;
Alan Stern523ded72007-04-26 00:12:04 -0700667 struct module *owner;
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400668 struct work_struct work;
669};
670
671static void sysfs_schedule_callback_work(struct work_struct *work)
672{
673 struct sysfs_schedule_callback_struct *ss = container_of(work,
674 struct sysfs_schedule_callback_struct, work);
675
676 (ss->func)(ss->data);
677 kobject_put(ss->kobj);
Alan Stern523ded72007-04-26 00:12:04 -0700678 module_put(ss->owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400679 kfree(ss);
680}
681
682/**
683 * sysfs_schedule_callback - helper to schedule a callback for a kobject
684 * @kobj: object we're acting for.
685 * @func: callback function to invoke later.
686 * @data: argument to pass to @func.
Alan Stern523ded72007-04-26 00:12:04 -0700687 * @owner: module owning the callback code
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400688 *
689 * sysfs attribute methods must not unregister themselves or their parent
690 * kobject (which would amount to the same thing). Attempts to do so will
691 * deadlock, since unregistration is mutually exclusive with driver
692 * callbacks.
693 *
694 * Instead methods can call this routine, which will attempt to allocate
695 * and schedule a workqueue request to call back @func with @data as its
696 * argument in the workqueue's process context. @kobj will be pinned
697 * until @func returns.
698 *
699 * Returns 0 if the request was submitted, -ENOMEM if storage could not
Alan Stern523ded72007-04-26 00:12:04 -0700700 * be allocated, -ENODEV if a reference to @owner isn't available.
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400701 */
702int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
Alan Stern523ded72007-04-26 00:12:04 -0700703 void *data, struct module *owner)
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400704{
705 struct sysfs_schedule_callback_struct *ss;
706
Alan Stern523ded72007-04-26 00:12:04 -0700707 if (!try_module_get(owner))
708 return -ENODEV;
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400709 ss = kmalloc(sizeof(*ss), GFP_KERNEL);
Alan Stern523ded72007-04-26 00:12:04 -0700710 if (!ss) {
711 module_put(owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400712 return -ENOMEM;
Alan Stern523ded72007-04-26 00:12:04 -0700713 }
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400714 kobject_get(kobj);
715 ss->kobj = kobj;
716 ss->func = func;
717 ss->data = data;
Alan Stern523ded72007-04-26 00:12:04 -0700718 ss->owner = owner;
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400719 INIT_WORK(&ss->work, sysfs_schedule_callback_work);
720 schedule_work(&ss->work);
721 return 0;
722}
723EXPORT_SYMBOL_GPL(sysfs_schedule_callback);
724
Alan Sterndfa87c82007-02-20 15:02:44 -0500725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726EXPORT_SYMBOL_GPL(sysfs_create_file);
727EXPORT_SYMBOL_GPL(sysfs_remove_file);