blob: 04f6b0ebc889e43e7d7644b2eb3db0dbe4a31431 [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
Oliver Neukum94bebf42006-12-20 10:52:44 +010053/**
54 * add_to_collection - add buffer to a collection
55 * @buffer: buffer to be added
Randy Dunlapf95d8822007-02-10 14:41:56 -080056 * @node: inode of set to add to
Oliver Neukum94bebf42006-12-20 10:52:44 +010057 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Oliver Neukum94bebf42006-12-20 10:52:44 +010059static inline void
60add_to_collection(struct sysfs_buffer *buffer, struct inode *node)
61{
62 struct sysfs_buffer_collection *set = node->i_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Oliver Neukum94bebf42006-12-20 10:52:44 +010064 mutex_lock(&node->i_mutex);
65 list_add(&buffer->associates, &set->associates);
66 mutex_unlock(&node->i_mutex);
67}
68
69static inline void
70remove_from_collection(struct sysfs_buffer *buffer, struct inode *node)
71{
72 mutex_lock(&node->i_mutex);
73 list_del(&buffer->associates);
74 mutex_unlock(&node->i_mutex);
75}
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77/**
78 * fill_read_buffer - allocate and fill buffer from object.
79 * @dentry: dentry pointer.
80 * @buffer: data buffer for file.
81 *
82 * Allocate @buffer->page, if it hasn't been already, then call the
83 * kobject's show() method to fill the buffer with this attribute's
84 * data.
Oliver Neukum82244b12007-01-02 08:48:08 +010085 * This is called only once, on the file's first read unless an error
86 * is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 */
88static int fill_read_buffer(struct dentry * dentry, struct sysfs_buffer * buffer)
89{
NeilBrown4508a7a2006-03-20 17:53:53 +110090 struct sysfs_dirent * sd = dentry->d_fsdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 struct kobject * kobj = to_kobj(dentry->d_parent);
92 struct sysfs_ops * ops = buffer->ops;
93 int ret = 0;
94 ssize_t count;
95
96 if (!buffer->page)
97 buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
98 if (!buffer->page)
99 return -ENOMEM;
100
NeilBrown4508a7a2006-03-20 17:53:53 +1100101 buffer->event = atomic_read(&sd->s_event);
Tejun Heo3e519032007-06-14 03:45:15 +0900102 count = ops->show(kobj, sd->s_elem.attr.attr, buffer->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 BUG_ON(count > (ssize_t)PAGE_SIZE);
Oliver Neukum82244b12007-01-02 08:48:08 +0100104 if (count >= 0) {
105 buffer->needs_read_fill = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 buffer->count = count;
Oliver Neukum82244b12007-01-02 08:48:08 +0100107 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 ret = count;
Oliver Neukum82244b12007-01-02 08:48:08 +0100109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return ret;
111}
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113/**
114 * sysfs_read_file - read an attribute.
115 * @file: file pointer.
116 * @buf: buffer to fill.
117 * @count: number of bytes to read.
118 * @ppos: starting offset in file.
119 *
120 * Userspace wants to read an attribute file. The attribute descriptor
121 * is in the file's ->d_fsdata. The target object is in the directory's
122 * ->d_fsdata.
123 *
124 * We call fill_read_buffer() to allocate and fill the buffer from the
125 * object's show() method exactly once (if the read is happening from
126 * the beginning of the file). That should fill the entire buffer with
127 * all the data the object has to offer for that attribute.
128 * We then call flush_read_buffer() to copy the buffer to userspace
129 * in the increments specified.
130 */
131
132static ssize_t
133sysfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
134{
135 struct sysfs_buffer * buffer = file->private_data;
136 ssize_t retval = 0;
137
138 down(&buffer->sem);
139 if (buffer->needs_read_fill) {
Alan Sterne7b0d262007-03-15 15:51:28 -0400140 if (buffer->orphaned)
141 retval = -ENODEV;
142 else
143 retval = fill_read_buffer(file->f_path.dentry,buffer);
144 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 goto out;
146 }
Zach Brown5c1fdf42006-10-03 01:16:06 -0700147 pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",
148 __FUNCTION__, count, *ppos, buffer->page);
Akinobu Mita92f4c702007-05-09 02:33:32 -0700149 retval = simple_read_from_buffer(buf, count, ppos, buffer->page,
150 buffer->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151out:
152 up(&buffer->sem);
153 return retval;
154}
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156/**
157 * fill_write_buffer - copy buffer from userspace.
158 * @buffer: data buffer for file.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700159 * @buf: data from user.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 * @count: number of bytes in @userbuf.
161 *
162 * Allocate @buffer->page if it hasn't been already, then
163 * copy the user-supplied buffer into it.
164 */
165
166static int
167fill_write_buffer(struct sysfs_buffer * buffer, const char __user * buf, size_t count)
168{
169 int error;
170
171 if (!buffer->page)
172 buffer->page = (char *)get_zeroed_page(GFP_KERNEL);
173 if (!buffer->page)
174 return -ENOMEM;
175
176 if (count >= PAGE_SIZE)
Greg Kroah-Hartman6e0dd742006-03-31 15:37:06 -0800177 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 error = copy_from_user(buffer->page,buf,count);
179 buffer->needs_read_fill = 1;
Thomas Maier035ed7a2006-10-22 19:17:47 +0200180 /* if buf is assumed to contain a string, terminate it by \0,
181 so e.g. sscanf() can scan the string easily */
182 buffer->page[count] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 return error ? -EFAULT : count;
184}
185
186
187/**
188 * flush_write_buffer - push buffer to kobject.
Martin Waitz3d410882005-06-23 22:05:21 -0700189 * @dentry: dentry to the attribute
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 * @buffer: data buffer for file.
Martin Waitz3d410882005-06-23 22:05:21 -0700191 * @count: number of bytes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 *
193 * Get the correct pointers for the kobject and the attribute we're
194 * dealing with, then call the store() method for the attribute,
195 * passing the buffer that we acquired in fill_write_buffer().
196 */
197
198static int
199flush_write_buffer(struct dentry * dentry, struct sysfs_buffer * buffer, size_t count)
200{
Tejun Heo3e519032007-06-14 03:45:15 +0900201 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 struct kobject * kobj = to_kobj(dentry->d_parent);
203 struct sysfs_ops * ops = buffer->ops;
204
Tejun Heo3e519032007-06-14 03:45:15 +0900205 return ops->store(kobj, attr_sd->s_elem.attr.attr, buffer->page, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
208
209/**
210 * sysfs_write_file - write an attribute.
211 * @file: file pointer
212 * @buf: data to write
213 * @count: number of bytes
214 * @ppos: starting offset
215 *
216 * Similar to sysfs_read_file(), though working in the opposite direction.
217 * We allocate and fill the data from the user in fill_write_buffer(),
218 * then push it to the kobject in flush_write_buffer().
219 * There is no easy way for us to know if userspace is only doing a partial
220 * write, so we don't support them. We expect the entire buffer to come
221 * on the first write.
222 * Hint: if you're writing a value, first read the file, modify only the
223 * the value you're changing, then write entire buffer back.
224 */
225
226static ssize_t
227sysfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
228{
229 struct sysfs_buffer * buffer = file->private_data;
230 ssize_t len;
231
232 down(&buffer->sem);
Oliver Neukum94bebf42006-12-20 10:52:44 +0100233 if (buffer->orphaned) {
234 len = -ENODEV;
235 goto out;
236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 len = fill_write_buffer(buffer, buf, count);
238 if (len > 0)
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800239 len = flush_write_buffer(file->f_path.dentry, buffer, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if (len > 0)
241 *ppos += len;
Oliver Neukum94bebf42006-12-20 10:52:44 +0100242out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 up(&buffer->sem);
244 return len;
245}
246
Oliver Neukum94bebf42006-12-20 10:52:44 +0100247static int sysfs_open_file(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800249 struct kobject *kobj = sysfs_get_kobject(file->f_path.dentry->d_parent);
Tejun Heo3e519032007-06-14 03:45:15 +0900250 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
251 struct attribute *attr = attr_sd->s_elem.attr.attr;
Oliver Neukum94bebf42006-12-20 10:52:44 +0100252 struct sysfs_buffer_collection *set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 struct sysfs_buffer * buffer;
254 struct sysfs_ops * ops = NULL;
255 int error = 0;
256
257 if (!kobj || !attr)
258 goto Einval;
259
260 /* Grab the module reference for this attribute if we have one */
261 if (!try_module_get(attr->owner)) {
262 error = -ENODEV;
263 goto Done;
264 }
265
266 /* if the kobject has no ktype, then we assume that it is a subsystem
267 * itself, and use ops for it.
268 */
269 if (kobj->kset && kobj->kset->ktype)
270 ops = kobj->kset->ktype->sysfs_ops;
271 else if (kobj->ktype)
272 ops = kobj->ktype->sysfs_ops;
273 else
274 ops = &subsys_sysfs_ops;
275
276 /* No sysfs operations, either from having no subsystem,
277 * or the subsystem have no operations.
278 */
279 if (!ops)
280 goto Eaccess;
281
Oliver Neukum94bebf42006-12-20 10:52:44 +0100282 /* make sure we have a collection to add our buffers to */
283 mutex_lock(&inode->i_mutex);
284 if (!(set = inode->i_private)) {
285 if (!(set = inode->i_private = kmalloc(sizeof(struct sysfs_buffer_collection), GFP_KERNEL))) {
286 error = -ENOMEM;
287 goto Done;
288 } else {
289 INIT_LIST_HEAD(&set->associates);
290 }
291 }
292 mutex_unlock(&inode->i_mutex);
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 /* File needs write support.
295 * The inode's perms must say it's ok,
296 * and we must have a store method.
297 */
298 if (file->f_mode & FMODE_WRITE) {
299
300 if (!(inode->i_mode & S_IWUGO) || !ops->store)
301 goto Eaccess;
302
303 }
304
305 /* File needs read support.
306 * The inode's perms must say it's ok, and we there
307 * must be a show method for it.
308 */
309 if (file->f_mode & FMODE_READ) {
310 if (!(inode->i_mode & S_IRUGO) || !ops->show)
311 goto Eaccess;
312 }
313
314 /* No error? Great, allocate a buffer for the file, and store it
315 * it in file->private_data for easy access.
316 */
Eric Sesterhenn58d49282006-02-22 11:18:15 +0100317 buffer = kzalloc(sizeof(struct sysfs_buffer), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (buffer) {
Oliver Neukum94bebf42006-12-20 10:52:44 +0100319 INIT_LIST_HEAD(&buffer->associates);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 init_MUTEX(&buffer->sem);
321 buffer->needs_read_fill = 1;
322 buffer->ops = ops;
Oliver Neukum94bebf42006-12-20 10:52:44 +0100323 add_to_collection(buffer, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 file->private_data = buffer;
325 } else
326 error = -ENOMEM;
327 goto Done;
328
329 Einval:
330 error = -EINVAL;
331 goto Done;
332 Eaccess:
333 error = -EACCES;
334 module_put(attr->owner);
335 Done:
Mariusz Kozlowskif7506532007-01-02 13:41:10 +0100336 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 kobject_put(kobj);
338 return error;
339}
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341static int sysfs_release(struct inode * inode, struct file * filp)
342{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800343 struct kobject * kobj = to_kobj(filp->f_path.dentry->d_parent);
Tejun Heo3e519032007-06-14 03:45:15 +0900344 struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata;
345 struct attribute *attr = attr_sd->s_elem.attr.attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 struct sysfs_buffer * buffer = filp->private_data;
347
Oliver Neukum94bebf42006-12-20 10:52:44 +0100348 if (buffer)
349 remove_from_collection(buffer, inode);
Mariusz Kozlowskif7506532007-01-02 13:41:10 +0100350 kobject_put(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 /* After this point, attr should not be accessed. */
Tejun Heo3e519032007-06-14 03:45:15 +0900352 module_put(attr->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 if (buffer) {
355 if (buffer->page)
356 free_page((unsigned long)buffer->page);
357 kfree(buffer);
358 }
359 return 0;
360}
361
NeilBrown4508a7a2006-03-20 17:53:53 +1100362/* Sysfs attribute files are pollable. The idea is that you read
363 * the content and then you use 'poll' or 'select' to wait for
364 * the content to change. When the content changes (assuming the
365 * manager for the kobject supports notification), poll will
366 * return POLLERR|POLLPRI, and select will return the fd whether
367 * it is waiting for read, write, or exceptions.
368 * Once poll/select indicates that the value has changed, you
369 * need to close and re-open the file, as simply seeking and reading
370 * again will not get new data, or reset the state of 'poll'.
371 * Reminder: this only works for attributes which actively support
372 * it, and it is not possible to test an attribute from userspace
373 * to see if it supports poll (Nether 'poll' or 'select' return
374 * an appropriate error code). When in doubt, set a suitable timeout value.
375 */
376static unsigned int sysfs_poll(struct file *filp, poll_table *wait)
377{
378 struct sysfs_buffer * buffer = filp->private_data;
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800379 struct kobject * kobj = to_kobj(filp->f_path.dentry->d_parent);
380 struct sysfs_dirent * sd = filp->f_path.dentry->d_fsdata;
NeilBrown4508a7a2006-03-20 17:53:53 +1100381 int res = 0;
382
383 poll_wait(filp, &kobj->poll, wait);
384
385 if (buffer->event != atomic_read(&sd->s_event)) {
386 res = POLLERR|POLLPRI;
387 buffer->needs_read_fill = 1;
388 }
389
390 return res;
391}
392
393
394static struct dentry *step_down(struct dentry *dir, const char * name)
395{
396 struct dentry * de;
397
398 if (dir == NULL || dir->d_inode == NULL)
399 return NULL;
400
401 mutex_lock(&dir->d_inode->i_mutex);
402 de = lookup_one_len(name, dir, strlen(name));
403 mutex_unlock(&dir->d_inode->i_mutex);
404 dput(dir);
405 if (IS_ERR(de))
406 return NULL;
407 if (de->d_inode == NULL) {
408 dput(de);
409 return NULL;
410 }
411 return de;
412}
413
414void sysfs_notify(struct kobject * k, char *dir, char *attr)
415{
416 struct dentry *de = k->dentry;
417 if (de)
418 dget(de);
419 if (de && dir)
420 de = step_down(de, dir);
421 if (de && attr)
422 de = step_down(de, attr);
423 if (de) {
424 struct sysfs_dirent * sd = de->d_fsdata;
425 if (sd)
426 atomic_inc(&sd->s_event);
427 wake_up_interruptible(&k->poll);
428 dput(de);
429 }
430}
431EXPORT_SYMBOL_GPL(sysfs_notify);
432
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800433const struct file_operations sysfs_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 .read = sysfs_read_file,
435 .write = sysfs_write_file,
436 .llseek = generic_file_llseek,
437 .open = sysfs_open_file,
438 .release = sysfs_release,
NeilBrown4508a7a2006-03-20 17:53:53 +1100439 .poll = sysfs_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440};
441
442
443int sysfs_add_file(struct dentry * dir, const struct attribute * attr, int type)
444{
445 struct sysfs_dirent * parent_sd = dir->d_fsdata;
446 umode_t mode = (attr->mode & S_IALLUGO) | S_IFREG;
Tejun Heoa26cd722007-06-14 03:45:14 +0900447 struct sysfs_dirent *sd;
448 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800450 mutex_lock(&dir->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Tejun Heoa26cd722007-06-14 03:45:14 +0900452 if (sysfs_dirent_exist(parent_sd, attr->name)) {
453 error = -EEXIST;
454 goto out_unlock;
455 }
456
Tejun Heo3e519032007-06-14 03:45:15 +0900457 sd = sysfs_new_dirent(attr->name, mode, type);
Tejun Heoa26cd722007-06-14 03:45:14 +0900458 if (!sd) {
459 error = -ENOMEM;
460 goto out_unlock;
461 }
Tejun Heo3e519032007-06-14 03:45:15 +0900462 sd->s_elem.attr.attr = (void *)attr;
Tejun Heoa26cd722007-06-14 03:45:14 +0900463 sysfs_attach_dirent(sd, parent_sd, NULL);
464
465 out_unlock:
466 mutex_unlock(&dir->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return error;
468}
469
470
471/**
472 * sysfs_create_file - create an attribute file for an object.
473 * @kobj: object we're creating for.
474 * @attr: atrribute descriptor.
475 */
476
477int sysfs_create_file(struct kobject * kobj, const struct attribute * attr)
478{
479 BUG_ON(!kobj || !kobj->dentry || !attr);
480
481 return sysfs_add_file(kobj->dentry, attr, SYSFS_KOBJ_ATTR);
482
483}
484
485
486/**
Alan Sterndfa87c82007-02-20 15:02:44 -0500487 * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
488 * @kobj: object we're acting for.
489 * @attr: attribute descriptor.
490 * @group: group name.
491 */
492int sysfs_add_file_to_group(struct kobject *kobj,
493 const struct attribute *attr, const char *group)
494{
495 struct dentry *dir;
496 int error;
497
498 dir = lookup_one_len(group, kobj->dentry, strlen(group));
499 if (IS_ERR(dir))
500 error = PTR_ERR(dir);
501 else {
502 error = sysfs_add_file(dir, attr, SYSFS_KOBJ_ATTR);
503 dput(dir);
504 }
505 return error;
506}
507EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
508
509
510/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 * sysfs_update_file - update the modified timestamp on an object attribute.
512 * @kobj: object we're acting for.
513 * @attr: attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 */
515int sysfs_update_file(struct kobject * kobj, const struct attribute * attr)
516{
517 struct dentry * dir = kobj->dentry;
518 struct dentry * victim;
519 int res = -ENOENT;
520
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800521 mutex_lock(&dir->d_inode->i_mutex);
Christoph Hellwig5f45f1a2005-06-23 00:09:12 -0700522 victim = lookup_one_len(attr->name, dir, strlen(attr->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (!IS_ERR(victim)) {
524 /* make sure dentry is really there */
525 if (victim->d_inode &&
526 (victim->d_parent->d_inode == dir->d_inode)) {
527 victim->d_inode->i_mtime = CURRENT_TIME;
Robert Love0eeca282005-07-12 17:06:03 -0400528 fsnotify_modify(victim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 res = 0;
530 } else
531 d_drop(victim);
532
533 /**
Hidetoshi Seto97a50182006-09-20 16:49:02 +0900534 * Drop the reference acquired from lookup_one_len() above.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 */
536 dput(victim);
537 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800538 mutex_unlock(&dir->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 return res;
541}
542
543
544/**
Kay Sievers31e5abe2005-04-18 21:57:32 -0700545 * sysfs_chmod_file - update the modified mode value on an object attribute.
546 * @kobj: object we're acting for.
547 * @attr: attribute descriptor.
548 * @mode: file permissions.
549 *
550 */
551int sysfs_chmod_file(struct kobject *kobj, struct attribute *attr, mode_t mode)
552{
553 struct dentry *dir = kobj->dentry;
554 struct dentry *victim;
Maneesh Sonibc062b12005-07-29 12:13:35 -0700555 struct inode * inode;
556 struct iattr newattrs;
Kay Sievers31e5abe2005-04-18 21:57:32 -0700557 int res = -ENOENT;
558
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800559 mutex_lock(&dir->d_inode->i_mutex);
Christoph Hellwig5f45f1a2005-06-23 00:09:12 -0700560 victim = lookup_one_len(attr->name, dir, strlen(attr->name));
Kay Sievers31e5abe2005-04-18 21:57:32 -0700561 if (!IS_ERR(victim)) {
562 if (victim->d_inode &&
563 (victim->d_parent->d_inode == dir->d_inode)) {
Maneesh Sonibc062b12005-07-29 12:13:35 -0700564 inode = victim->d_inode;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800565 mutex_lock(&inode->i_mutex);
Maneesh Sonibc062b12005-07-29 12:13:35 -0700566 newattrs.ia_mode = (mode & S_IALLUGO) |
567 (inode->i_mode & ~S_IALLUGO);
568 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
569 res = notify_change(victim, &newattrs);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800570 mutex_unlock(&inode->i_mutex);
Kay Sievers31e5abe2005-04-18 21:57:32 -0700571 }
Maneesh Sonibc062b12005-07-29 12:13:35 -0700572 dput(victim);
Kay Sievers31e5abe2005-04-18 21:57:32 -0700573 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800574 mutex_unlock(&dir->d_inode->i_mutex);
Kay Sievers31e5abe2005-04-18 21:57:32 -0700575
576 return res;
577}
578EXPORT_SYMBOL_GPL(sysfs_chmod_file);
579
580
581/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 * sysfs_remove_file - remove an object attribute.
583 * @kobj: object we're acting for.
584 * @attr: attribute descriptor.
585 *
586 * Hash the attribute name and kill the victim.
587 */
588
589void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr)
590{
Oliver Neukum94bebf42006-12-20 10:52:44 +0100591 sysfs_hash_and_remove(kobj->dentry, attr->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
593
594
Alan Sterndfa87c82007-02-20 15:02:44 -0500595/**
596 * sysfs_remove_file_from_group - remove an attribute file from a group.
597 * @kobj: object we're acting for.
598 * @attr: attribute descriptor.
599 * @group: group name.
600 */
601void sysfs_remove_file_from_group(struct kobject *kobj,
602 const struct attribute *attr, const char *group)
603{
604 struct dentry *dir;
605
606 dir = lookup_one_len(group, kobj->dentry, strlen(group));
607 if (!IS_ERR(dir)) {
608 sysfs_hash_and_remove(dir, attr->name);
609 dput(dir);
610 }
611}
612EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
613
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400614struct sysfs_schedule_callback_struct {
615 struct kobject *kobj;
616 void (*func)(void *);
617 void *data;
Alan Stern523ded72007-04-26 00:12:04 -0700618 struct module *owner;
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400619 struct work_struct work;
620};
621
622static void sysfs_schedule_callback_work(struct work_struct *work)
623{
624 struct sysfs_schedule_callback_struct *ss = container_of(work,
625 struct sysfs_schedule_callback_struct, work);
626
627 (ss->func)(ss->data);
628 kobject_put(ss->kobj);
Alan Stern523ded72007-04-26 00:12:04 -0700629 module_put(ss->owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400630 kfree(ss);
631}
632
633/**
634 * sysfs_schedule_callback - helper to schedule a callback for a kobject
635 * @kobj: object we're acting for.
636 * @func: callback function to invoke later.
637 * @data: argument to pass to @func.
Alan Stern523ded72007-04-26 00:12:04 -0700638 * @owner: module owning the callback code
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400639 *
640 * sysfs attribute methods must not unregister themselves or their parent
641 * kobject (which would amount to the same thing). Attempts to do so will
642 * deadlock, since unregistration is mutually exclusive with driver
643 * callbacks.
644 *
645 * Instead methods can call this routine, which will attempt to allocate
646 * and schedule a workqueue request to call back @func with @data as its
647 * argument in the workqueue's process context. @kobj will be pinned
648 * until @func returns.
649 *
650 * Returns 0 if the request was submitted, -ENOMEM if storage could not
Alan Stern523ded72007-04-26 00:12:04 -0700651 * be allocated, -ENODEV if a reference to @owner isn't available.
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400652 */
653int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
Alan Stern523ded72007-04-26 00:12:04 -0700654 void *data, struct module *owner)
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400655{
656 struct sysfs_schedule_callback_struct *ss;
657
Alan Stern523ded72007-04-26 00:12:04 -0700658 if (!try_module_get(owner))
659 return -ENODEV;
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400660 ss = kmalloc(sizeof(*ss), GFP_KERNEL);
Alan Stern523ded72007-04-26 00:12:04 -0700661 if (!ss) {
662 module_put(owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400663 return -ENOMEM;
Alan Stern523ded72007-04-26 00:12:04 -0700664 }
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400665 kobject_get(kobj);
666 ss->kobj = kobj;
667 ss->func = func;
668 ss->data = data;
Alan Stern523ded72007-04-26 00:12:04 -0700669 ss->owner = owner;
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400670 INIT_WORK(&ss->work, sysfs_schedule_callback_work);
671 schedule_work(&ss->work);
672 return 0;
673}
674EXPORT_SYMBOL_GPL(sysfs_schedule_callback);
675
Alan Sterndfa87c82007-02-20 15:02:44 -0500676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677EXPORT_SYMBOL_GPL(sysfs_create_file);
678EXPORT_SYMBOL_GPL(sysfs_remove_file);
679EXPORT_SYMBOL_GPL(sysfs_update_file);