blob: d673d9b5d33f8ba17ec0bf7cb0ece118b90964d8 [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>
Robert Love0eeca282005-07-12 17:06:03 -04006#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/kobject.h>
Christoph Hellwig5f45f1a2005-06-23 00:09:12 -07008#include <linux/namei.h>
NeilBrown4508a7a2006-03-20 17:53:53 +11009#include <linux/poll.h>
Oliver Neukum94bebf42006-12-20 10:52:44 +010010#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <asm/uaccess.h>
12#include <asm/semaphore.h>
13
14#include "sysfs.h"
15
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -070016#define to_sattr(a) container_of(a,struct subsys_attribute, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Martin Waitz3d410882005-06-23 22:05:21 -070018/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * Subsystem file operations.
20 * These operations allow subsystems to have files that can be
21 * read/written.
22 */
23static ssize_t
24subsys_attr_show(struct kobject * kobj, struct attribute * attr, char * page)
25{
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -070026 struct kset *kset = to_kset(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 struct subsys_attribute * sattr = to_sattr(attr);
Dmitry Torokhovc76d0ab2005-04-29 01:22:00 -050028 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30 if (sattr->show)
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -070031 ret = sattr->show(kset, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 return ret;
33}
34
35static ssize_t
36subsys_attr_store(struct kobject * kobj, struct attribute * attr,
37 const char * page, size_t count)
38{
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -070039 struct kset *kset = to_kset(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 struct subsys_attribute * sattr = to_sattr(attr);
Dmitry Torokhovc76d0ab2005-04-29 01:22:00 -050041 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43 if (sattr->store)
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -070044 ret = sattr->store(kset, page, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 return ret;
46}
47
48static struct sysfs_ops subsys_sysfs_ops = {
49 .show = subsys_attr_show,
50 .store = subsys_attr_store,
51};
52
Tejun Heo73107cb2007-06-14 03:45:16 +090053struct sysfs_buffer {
54 size_t count;
55 loff_t pos;
56 char * page;
57 struct sysfs_ops * ops;
58 struct semaphore sem;
59 int needs_read_fill;
60 int event;
61};
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63/**
64 * fill_read_buffer - allocate and fill buffer from object.
65 * @dentry: dentry pointer.
66 * @buffer: data buffer for file.
67 *
68 * Allocate @buffer->page, if it hasn't been already, then call the
69 * kobject's show() method to fill the buffer with this attribute's
70 * data.
Oliver Neukum82244b12007-01-02 08:48:08 +010071 * This is called only once, on the file's first read unless an error
72 * is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 */
74static int fill_read_buffer(struct dentry * dentry, struct sysfs_buffer * buffer)
75{
Tejun Heo0ab66082007-06-14 03:45:16 +090076 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
77 struct kobject *kobj = attr_sd->s_parent->s_elem.dir.kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 struct sysfs_ops * ops = buffer->ops;
79 int ret = 0;
80 ssize_t count;
81
82 if (!buffer->page)
83 buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
84 if (!buffer->page)
85 return -ENOMEM;
86
Tejun Heo0ab66082007-06-14 03:45:16 +090087 /* need attr_sd for attr and ops, its parent for kobj */
88 if (!sysfs_get_active_two(attr_sd))
89 return -ENODEV;
90
91 buffer->event = atomic_read(&attr_sd->s_event);
92 count = ops->show(kobj, attr_sd->s_elem.attr.attr, buffer->page);
93
94 sysfs_put_active_two(attr_sd);
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 BUG_ON(count > (ssize_t)PAGE_SIZE);
Oliver Neukum82244b12007-01-02 08:48:08 +010097 if (count >= 0) {
98 buffer->needs_read_fill = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 buffer->count = count;
Oliver Neukum82244b12007-01-02 08:48:08 +0100100 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 ret = count;
Oliver Neukum82244b12007-01-02 08:48:08 +0100102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return ret;
104}
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106/**
107 * sysfs_read_file - read an attribute.
108 * @file: file pointer.
109 * @buf: buffer to fill.
110 * @count: number of bytes to read.
111 * @ppos: starting offset in file.
112 *
113 * Userspace wants to read an attribute file. The attribute descriptor
114 * is in the file's ->d_fsdata. The target object is in the directory's
115 * ->d_fsdata.
116 *
117 * We call fill_read_buffer() to allocate and fill the buffer from the
118 * object's show() method exactly once (if the read is happening from
119 * the beginning of the file). That should fill the entire buffer with
120 * all the data the object has to offer for that attribute.
121 * We then call flush_read_buffer() to copy the buffer to userspace
122 * in the increments specified.
123 */
124
125static ssize_t
126sysfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
127{
128 struct sysfs_buffer * buffer = file->private_data;
129 ssize_t retval = 0;
130
131 down(&buffer->sem);
132 if (buffer->needs_read_fill) {
Tejun Heo73107cb2007-06-14 03:45:16 +0900133 retval = fill_read_buffer(file->f_path.dentry,buffer);
Alan Sterne7b0d262007-03-15 15:51:28 -0400134 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 goto out;
136 }
Zach Brown5c1fdf42006-10-03 01:16:06 -0700137 pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",
138 __FUNCTION__, count, *ppos, buffer->page);
Akinobu Mita92f4c702007-05-09 02:33:32 -0700139 retval = simple_read_from_buffer(buf, count, ppos, buffer->page,
140 buffer->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141out:
142 up(&buffer->sem);
143 return retval;
144}
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146/**
147 * fill_write_buffer - copy buffer from userspace.
148 * @buffer: data buffer for file.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700149 * @buf: data from user.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 * @count: number of bytes in @userbuf.
151 *
152 * Allocate @buffer->page if it hasn't been already, then
153 * copy the user-supplied buffer into it.
154 */
155
156static int
157fill_write_buffer(struct sysfs_buffer * buffer, const char __user * buf, size_t count)
158{
159 int error;
160
161 if (!buffer->page)
162 buffer->page = (char *)get_zeroed_page(GFP_KERNEL);
163 if (!buffer->page)
164 return -ENOMEM;
165
166 if (count >= PAGE_SIZE)
Greg Kroah-Hartman6e0dd742006-03-31 15:37:06 -0800167 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 error = copy_from_user(buffer->page,buf,count);
169 buffer->needs_read_fill = 1;
Thomas Maier035ed7a2006-10-22 19:17:47 +0200170 /* if buf is assumed to contain a string, terminate it by \0,
171 so e.g. sscanf() can scan the string easily */
172 buffer->page[count] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return error ? -EFAULT : count;
174}
175
176
177/**
178 * flush_write_buffer - push buffer to kobject.
Martin Waitz3d410882005-06-23 22:05:21 -0700179 * @dentry: dentry to the attribute
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 * @buffer: data buffer for file.
Martin Waitz3d410882005-06-23 22:05:21 -0700181 * @count: number of bytes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 *
183 * Get the correct pointers for the kobject and the attribute we're
184 * dealing with, then call the store() method for the attribute,
185 * passing the buffer that we acquired in fill_write_buffer().
186 */
187
Tejun Heo0ab66082007-06-14 03:45:16 +0900188static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189flush_write_buffer(struct dentry * dentry, struct sysfs_buffer * buffer, size_t count)
190{
Tejun Heo3e519032007-06-14 03:45:15 +0900191 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
Tejun Heo0ab66082007-06-14 03:45:16 +0900192 struct kobject *kobj = attr_sd->s_parent->s_elem.dir.kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 struct sysfs_ops * ops = buffer->ops;
Tejun Heo0ab66082007-06-14 03:45:16 +0900194 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Tejun Heo0ab66082007-06-14 03:45:16 +0900196 /* need attr_sd for attr and ops, its parent for kobj */
197 if (!sysfs_get_active_two(attr_sd))
198 return -ENODEV;
199
200 rc = ops->store(kobj, attr_sd->s_elem.attr.attr, buffer->page, count);
201
202 sysfs_put_active_two(attr_sd);
203
204 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
207
208/**
209 * sysfs_write_file - write an attribute.
210 * @file: file pointer
211 * @buf: data to write
212 * @count: number of bytes
213 * @ppos: starting offset
214 *
215 * Similar to sysfs_read_file(), though working in the opposite direction.
216 * We allocate and fill the data from the user in fill_write_buffer(),
217 * then push it to the kobject in flush_write_buffer().
218 * There is no easy way for us to know if userspace is only doing a partial
219 * write, so we don't support them. We expect the entire buffer to come
220 * on the first write.
221 * Hint: if you're writing a value, first read the file, modify only the
222 * the value you're changing, then write entire buffer back.
223 */
224
225static ssize_t
226sysfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
227{
228 struct sysfs_buffer * buffer = file->private_data;
229 ssize_t len;
230
231 down(&buffer->sem);
232 len = fill_write_buffer(buffer, buf, count);
233 if (len > 0)
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800234 len = flush_write_buffer(file->f_path.dentry, buffer, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 if (len > 0)
236 *ppos += len;
237 up(&buffer->sem);
238 return len;
239}
240
Oliver Neukum94bebf42006-12-20 10:52:44 +0100241static int sysfs_open_file(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Tejun Heo3e519032007-06-14 03:45:15 +0900243 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
244 struct attribute *attr = attr_sd->s_elem.attr.attr;
Tejun Heo0ab66082007-06-14 03:45:16 +0900245 struct kobject *kobj = attr_sd->s_parent->s_elem.dir.kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 struct sysfs_buffer * buffer;
247 struct sysfs_ops * ops = NULL;
Tejun Heo0ab66082007-06-14 03:45:16 +0900248 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Tejun Heo0ab66082007-06-14 03:45:16 +0900250 /* need attr_sd for attr and ops, its parent for kobj */
251 if (!sysfs_get_active_two(attr_sd))
252 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Tejun Heo0ab66082007-06-14 03:45:16 +0900254 /* Grab the module reference for this attribute */
255 error = -ENODEV;
256 if (!try_module_get(attr->owner))
257 goto err_sput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 /* if the kobject has no ktype, then we assume that it is a subsystem
260 * itself, and use ops for it.
261 */
262 if (kobj->kset && kobj->kset->ktype)
263 ops = kobj->kset->ktype->sysfs_ops;
264 else if (kobj->ktype)
265 ops = kobj->ktype->sysfs_ops;
266 else
267 ops = &subsys_sysfs_ops;
268
Tejun Heo73107cb2007-06-14 03:45:16 +0900269 error = -EACCES;
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 /* No sysfs operations, either from having no subsystem,
272 * or the subsystem have no operations.
273 */
274 if (!ops)
Tejun Heo0ab66082007-06-14 03:45:16 +0900275 goto err_mput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 /* File needs write support.
278 * The inode's perms must say it's ok,
279 * and we must have a store method.
280 */
281 if (file->f_mode & FMODE_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (!(inode->i_mode & S_IWUGO) || !ops->store)
Tejun Heo0ab66082007-06-14 03:45:16 +0900283 goto err_mput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
285
286 /* File needs read support.
287 * The inode's perms must say it's ok, and we there
288 * must be a show method for it.
289 */
290 if (file->f_mode & FMODE_READ) {
291 if (!(inode->i_mode & S_IRUGO) || !ops->show)
Tejun Heo0ab66082007-06-14 03:45:16 +0900292 goto err_mput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
294
295 /* No error? Great, allocate a buffer for the file, and store it
296 * it in file->private_data for easy access.
297 */
Tejun Heo0ab66082007-06-14 03:45:16 +0900298 error = -ENOMEM;
Eric Sesterhenn58d49282006-02-22 11:18:15 +0100299 buffer = kzalloc(sizeof(struct sysfs_buffer), GFP_KERNEL);
Tejun Heo0ab66082007-06-14 03:45:16 +0900300 if (!buffer)
301 goto err_mput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Tejun Heo0ab66082007-06-14 03:45:16 +0900303 init_MUTEX(&buffer->sem);
304 buffer->needs_read_fill = 1;
305 buffer->ops = ops;
Tejun Heo0ab66082007-06-14 03:45:16 +0900306 file->private_data = buffer;
307
308 /* open succeeded, put active references and pin attr_sd */
309 sysfs_put_active_two(attr_sd);
310 sysfs_get(attr_sd);
311 return 0;
312
313 err_mput:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 module_put(attr->owner);
Tejun Heo0ab66082007-06-14 03:45:16 +0900315 err_sput:
316 sysfs_put_active_two(attr_sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return error;
318}
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320static int sysfs_release(struct inode * inode, struct file * filp)
321{
Tejun Heo3e519032007-06-14 03:45:15 +0900322 struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata;
323 struct attribute *attr = attr_sd->s_elem.attr.attr;
Tejun Heo73107cb2007-06-14 03:45:16 +0900324 struct sysfs_buffer *buffer = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Tejun Heo0ab66082007-06-14 03:45:16 +0900326 sysfs_put(attr_sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 /* After this point, attr should not be accessed. */
Tejun Heo3e519032007-06-14 03:45:15 +0900328 module_put(attr->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 if (buffer) {
331 if (buffer->page)
332 free_page((unsigned long)buffer->page);
333 kfree(buffer);
334 }
335 return 0;
336}
337
NeilBrown4508a7a2006-03-20 17:53:53 +1100338/* Sysfs attribute files are pollable. The idea is that you read
339 * the content and then you use 'poll' or 'select' to wait for
340 * the content to change. When the content changes (assuming the
341 * manager for the kobject supports notification), poll will
342 * return POLLERR|POLLPRI, and select will return the fd whether
343 * it is waiting for read, write, or exceptions.
344 * Once poll/select indicates that the value has changed, you
345 * need to close and re-open the file, as simply seeking and reading
346 * again will not get new data, or reset the state of 'poll'.
347 * Reminder: this only works for attributes which actively support
348 * it, and it is not possible to test an attribute from userspace
349 * to see if it supports poll (Nether 'poll' or 'select' return
350 * an appropriate error code). When in doubt, set a suitable timeout value.
351 */
352static unsigned int sysfs_poll(struct file *filp, poll_table *wait)
353{
354 struct sysfs_buffer * buffer = filp->private_data;
Tejun Heo0ab66082007-06-14 03:45:16 +0900355 struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata;
356 struct kobject *kobj = attr_sd->s_parent->s_elem.dir.kobj;
357
358 /* need parent for the kobj, grab both */
359 if (!sysfs_get_active_two(attr_sd))
360 goto trigger;
NeilBrown4508a7a2006-03-20 17:53:53 +1100361
362 poll_wait(filp, &kobj->poll, wait);
363
Tejun Heo0ab66082007-06-14 03:45:16 +0900364 sysfs_put_active_two(attr_sd);
NeilBrown4508a7a2006-03-20 17:53:53 +1100365
Tejun Heo0ab66082007-06-14 03:45:16 +0900366 if (buffer->event != atomic_read(&attr_sd->s_event))
367 goto trigger;
368
369 return 0;
370
371 trigger:
372 buffer->needs_read_fill = 1;
373 return POLLERR|POLLPRI;
NeilBrown4508a7a2006-03-20 17:53:53 +1100374}
375
376
377static struct dentry *step_down(struct dentry *dir, const char * name)
378{
379 struct dentry * de;
380
381 if (dir == NULL || dir->d_inode == NULL)
382 return NULL;
383
384 mutex_lock(&dir->d_inode->i_mutex);
385 de = lookup_one_len(name, dir, strlen(name));
386 mutex_unlock(&dir->d_inode->i_mutex);
387 dput(dir);
388 if (IS_ERR(de))
389 return NULL;
390 if (de->d_inode == NULL) {
391 dput(de);
392 return NULL;
393 }
394 return de;
395}
396
397void sysfs_notify(struct kobject * k, char *dir, char *attr)
398{
399 struct dentry *de = k->dentry;
400 if (de)
401 dget(de);
402 if (de && dir)
403 de = step_down(de, dir);
404 if (de && attr)
405 de = step_down(de, attr);
406 if (de) {
407 struct sysfs_dirent * sd = de->d_fsdata;
408 if (sd)
409 atomic_inc(&sd->s_event);
410 wake_up_interruptible(&k->poll);
411 dput(de);
412 }
413}
414EXPORT_SYMBOL_GPL(sysfs_notify);
415
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800416const struct file_operations sysfs_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 .read = sysfs_read_file,
418 .write = sysfs_write_file,
419 .llseek = generic_file_llseek,
420 .open = sysfs_open_file,
421 .release = sysfs_release,
NeilBrown4508a7a2006-03-20 17:53:53 +1100422 .poll = sysfs_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423};
424
425
426int sysfs_add_file(struct dentry * dir, const struct attribute * attr, int type)
427{
428 struct sysfs_dirent * parent_sd = dir->d_fsdata;
429 umode_t mode = (attr->mode & S_IALLUGO) | S_IFREG;
Tejun Heoa26cd722007-06-14 03:45:14 +0900430 struct sysfs_dirent *sd;
431 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800433 mutex_lock(&dir->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Tejun Heoa26cd722007-06-14 03:45:14 +0900435 if (sysfs_dirent_exist(parent_sd, attr->name)) {
436 error = -EEXIST;
437 goto out_unlock;
438 }
439
Tejun Heo3e519032007-06-14 03:45:15 +0900440 sd = sysfs_new_dirent(attr->name, mode, type);
Tejun Heoa26cd722007-06-14 03:45:14 +0900441 if (!sd) {
442 error = -ENOMEM;
443 goto out_unlock;
444 }
Tejun Heo3e519032007-06-14 03:45:15 +0900445 sd->s_elem.attr.attr = (void *)attr;
Tejun Heoa26cd722007-06-14 03:45:14 +0900446 sysfs_attach_dirent(sd, parent_sd, NULL);
447
448 out_unlock:
449 mutex_unlock(&dir->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 return error;
451}
452
453
454/**
455 * sysfs_create_file - create an attribute file for an object.
456 * @kobj: object we're creating for.
457 * @attr: atrribute descriptor.
458 */
459
460int sysfs_create_file(struct kobject * kobj, const struct attribute * attr)
461{
462 BUG_ON(!kobj || !kobj->dentry || !attr);
463
464 return sysfs_add_file(kobj->dentry, attr, SYSFS_KOBJ_ATTR);
465
466}
467
468
469/**
Alan Sterndfa87c82007-02-20 15:02:44 -0500470 * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
471 * @kobj: object we're acting for.
472 * @attr: attribute descriptor.
473 * @group: group name.
474 */
475int sysfs_add_file_to_group(struct kobject *kobj,
476 const struct attribute *attr, const char *group)
477{
478 struct dentry *dir;
479 int error;
480
481 dir = lookup_one_len(group, kobj->dentry, strlen(group));
482 if (IS_ERR(dir))
483 error = PTR_ERR(dir);
484 else {
485 error = sysfs_add_file(dir, attr, SYSFS_KOBJ_ATTR);
486 dput(dir);
487 }
488 return error;
489}
490EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
491
492
493/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 * sysfs_update_file - update the modified timestamp on an object attribute.
495 * @kobj: object we're acting for.
496 * @attr: attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 */
498int sysfs_update_file(struct kobject * kobj, const struct attribute * attr)
499{
500 struct dentry * dir = kobj->dentry;
501 struct dentry * victim;
502 int res = -ENOENT;
503
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800504 mutex_lock(&dir->d_inode->i_mutex);
Christoph Hellwig5f45f1a2005-06-23 00:09:12 -0700505 victim = lookup_one_len(attr->name, dir, strlen(attr->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if (!IS_ERR(victim)) {
507 /* make sure dentry is really there */
508 if (victim->d_inode &&
509 (victim->d_parent->d_inode == dir->d_inode)) {
510 victim->d_inode->i_mtime = CURRENT_TIME;
Robert Love0eeca282005-07-12 17:06:03 -0400511 fsnotify_modify(victim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 res = 0;
513 } else
514 d_drop(victim);
515
516 /**
Hidetoshi Seto97a50182006-09-20 16:49:02 +0900517 * Drop the reference acquired from lookup_one_len() above.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 */
519 dput(victim);
520 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800521 mutex_unlock(&dir->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 return res;
524}
525
526
527/**
Kay Sievers31e5abe2005-04-18 21:57:32 -0700528 * sysfs_chmod_file - update the modified mode value on an object attribute.
529 * @kobj: object we're acting for.
530 * @attr: attribute descriptor.
531 * @mode: file permissions.
532 *
533 */
534int sysfs_chmod_file(struct kobject *kobj, struct attribute *attr, mode_t mode)
535{
536 struct dentry *dir = kobj->dentry;
537 struct dentry *victim;
Maneesh Sonibc062b12005-07-29 12:13:35 -0700538 struct inode * inode;
539 struct iattr newattrs;
Kay Sievers31e5abe2005-04-18 21:57:32 -0700540 int res = -ENOENT;
541
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800542 mutex_lock(&dir->d_inode->i_mutex);
Christoph Hellwig5f45f1a2005-06-23 00:09:12 -0700543 victim = lookup_one_len(attr->name, dir, strlen(attr->name));
Kay Sievers31e5abe2005-04-18 21:57:32 -0700544 if (!IS_ERR(victim)) {
545 if (victim->d_inode &&
546 (victim->d_parent->d_inode == dir->d_inode)) {
Maneesh Sonibc062b12005-07-29 12:13:35 -0700547 inode = victim->d_inode;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800548 mutex_lock(&inode->i_mutex);
Maneesh Sonibc062b12005-07-29 12:13:35 -0700549 newattrs.ia_mode = (mode & S_IALLUGO) |
550 (inode->i_mode & ~S_IALLUGO);
551 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
552 res = notify_change(victim, &newattrs);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800553 mutex_unlock(&inode->i_mutex);
Kay Sievers31e5abe2005-04-18 21:57:32 -0700554 }
Maneesh Sonibc062b12005-07-29 12:13:35 -0700555 dput(victim);
Kay Sievers31e5abe2005-04-18 21:57:32 -0700556 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800557 mutex_unlock(&dir->d_inode->i_mutex);
Kay Sievers31e5abe2005-04-18 21:57:32 -0700558
559 return res;
560}
561EXPORT_SYMBOL_GPL(sysfs_chmod_file);
562
563
564/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 * sysfs_remove_file - remove an object attribute.
566 * @kobj: object we're acting for.
567 * @attr: attribute descriptor.
568 *
569 * Hash the attribute name and kill the victim.
570 */
571
572void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr)
573{
Oliver Neukum94bebf42006-12-20 10:52:44 +0100574 sysfs_hash_and_remove(kobj->dentry, attr->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
577
Alan Sterndfa87c82007-02-20 15:02:44 -0500578/**
579 * sysfs_remove_file_from_group - remove an attribute file from a group.
580 * @kobj: object we're acting for.
581 * @attr: attribute descriptor.
582 * @group: group name.
583 */
584void sysfs_remove_file_from_group(struct kobject *kobj,
585 const struct attribute *attr, const char *group)
586{
587 struct dentry *dir;
588
589 dir = lookup_one_len(group, kobj->dentry, strlen(group));
590 if (!IS_ERR(dir)) {
591 sysfs_hash_and_remove(dir, attr->name);
592 dput(dir);
593 }
594}
595EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
596
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400597struct sysfs_schedule_callback_struct {
598 struct kobject *kobj;
599 void (*func)(void *);
600 void *data;
Alan Stern523ded72007-04-26 00:12:04 -0700601 struct module *owner;
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400602 struct work_struct work;
603};
604
605static void sysfs_schedule_callback_work(struct work_struct *work)
606{
607 struct sysfs_schedule_callback_struct *ss = container_of(work,
608 struct sysfs_schedule_callback_struct, work);
609
610 (ss->func)(ss->data);
611 kobject_put(ss->kobj);
Alan Stern523ded72007-04-26 00:12:04 -0700612 module_put(ss->owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400613 kfree(ss);
614}
615
616/**
617 * sysfs_schedule_callback - helper to schedule a callback for a kobject
618 * @kobj: object we're acting for.
619 * @func: callback function to invoke later.
620 * @data: argument to pass to @func.
Alan Stern523ded72007-04-26 00:12:04 -0700621 * @owner: module owning the callback code
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400622 *
623 * sysfs attribute methods must not unregister themselves or their parent
624 * kobject (which would amount to the same thing). Attempts to do so will
625 * deadlock, since unregistration is mutually exclusive with driver
626 * callbacks.
627 *
628 * Instead methods can call this routine, which will attempt to allocate
629 * and schedule a workqueue request to call back @func with @data as its
630 * argument in the workqueue's process context. @kobj will be pinned
631 * until @func returns.
632 *
633 * Returns 0 if the request was submitted, -ENOMEM if storage could not
Alan Stern523ded72007-04-26 00:12:04 -0700634 * be allocated, -ENODEV if a reference to @owner isn't available.
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400635 */
636int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
Alan Stern523ded72007-04-26 00:12:04 -0700637 void *data, struct module *owner)
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400638{
639 struct sysfs_schedule_callback_struct *ss;
640
Alan Stern523ded72007-04-26 00:12:04 -0700641 if (!try_module_get(owner))
642 return -ENODEV;
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400643 ss = kmalloc(sizeof(*ss), GFP_KERNEL);
Alan Stern523ded72007-04-26 00:12:04 -0700644 if (!ss) {
645 module_put(owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400646 return -ENOMEM;
Alan Stern523ded72007-04-26 00:12:04 -0700647 }
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400648 kobject_get(kobj);
649 ss->kobj = kobj;
650 ss->func = func;
651 ss->data = data;
Alan Stern523ded72007-04-26 00:12:04 -0700652 ss->owner = owner;
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400653 INIT_WORK(&ss->work, sysfs_schedule_callback_work);
654 schedule_work(&ss->work);
655 return 0;
656}
657EXPORT_SYMBOL_GPL(sysfs_schedule_callback);
658
Alan Sterndfa87c82007-02-20 15:02:44 -0500659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660EXPORT_SYMBOL_GPL(sysfs_create_file);
661EXPORT_SYMBOL_GPL(sysfs_remove_file);
662EXPORT_SYMBOL_GPL(sysfs_update_file);