kobject: update the copyrights
[deliverable/linux.git] / fs / sysfs / file.c
CommitLineData
1da177e4
LT
1/*
2 * file.c - operations for regular (text) files.
3 */
4
5#include <linux/module.h>
1da177e4 6#include <linux/kobject.h>
5f45f1a7 7#include <linux/namei.h>
4508a7a7 8#include <linux/poll.h>
94bebf4d 9#include <linux/list.h>
52e8c209 10#include <linux/mutex.h>
1da177e4 11#include <asm/uaccess.h>
1da177e4
LT
12
13#include "sysfs.h"
14
823bccfc 15#define to_sattr(a) container_of(a,struct subsys_attribute, attr)
1da177e4 16
3d41088f 17/*
1da177e4
LT
18 * 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{
823bccfc 25 struct kset *kset = to_kset(kobj);
1da177e4 26 struct subsys_attribute * sattr = to_sattr(attr);
c76d0abd 27 ssize_t ret = -EIO;
1da177e4
LT
28
29 if (sattr->show)
823bccfc 30 ret = sattr->show(kset, page);
1da177e4
LT
31 return ret;
32}
33
34static ssize_t
35subsys_attr_store(struct kobject * kobj, struct attribute * attr,
36 const char * page, size_t count)
37{
823bccfc 38 struct kset *kset = to_kset(kobj);
1da177e4 39 struct subsys_attribute * sattr = to_sattr(attr);
c76d0abd 40 ssize_t ret = -EIO;
1da177e4
LT
41
42 if (sattr->store)
823bccfc 43 ret = sattr->store(kset, page, count);
1da177e4
LT
44 return ret;
45}
46
47static struct sysfs_ops subsys_sysfs_ops = {
48 .show = subsys_attr_show,
49 .store = subsys_attr_store,
50};
51
85a4ffad
TH
52/*
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;
a4e8b912
TH
65 atomic_t event;
66 wait_queue_head_t poll;
85a4ffad
TH
67 struct list_head buffers; /* goes through sysfs_buffer.list */
68};
69
73107cb3
TH
70struct sysfs_buffer {
71 size_t count;
72 loff_t pos;
73 char * page;
74 struct sysfs_ops * ops;
52e8c209 75 struct mutex mutex;
73107cb3
TH
76 int needs_read_fill;
77 int event;
85a4ffad 78 struct list_head list;
73107cb3 79};
1da177e4
LT
80
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.
82244b16
ON
89 * This is called only once, on the file's first read unless an error
90 * is returned.
1da177e4
LT
91 */
92static int fill_read_buffer(struct dentry * dentry, struct sysfs_buffer * buffer)
93{
0ab66088 94 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
b1fc3d61 95 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
1da177e4
LT
96 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
0ab66088
TH
105 /* need attr_sd for attr and ops, its parent for kobj */
106 if (!sysfs_get_active_two(attr_sd))
107 return -ENODEV;
108
a4e8b912 109 buffer->event = atomic_read(&attr_sd->s_attr.open->event);
b1fc3d61 110 count = ops->show(kobj, attr_sd->s_attr.attr, buffer->page);
0ab66088
TH
111
112 sysfs_put_active_two(attr_sd);
113
1da177e4 114 BUG_ON(count > (ssize_t)PAGE_SIZE);
82244b16
ON
115 if (count >= 0) {
116 buffer->needs_read_fill = 0;
1da177e4 117 buffer->count = count;
82244b16 118 } else {
1da177e4 119 ret = count;
82244b16 120 }
1da177e4
LT
121 return ret;
122}
123
1da177e4
LT
124/**
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
52e8c209 149 mutex_lock(&buffer->mutex);
1da177e4 150 if (buffer->needs_read_fill) {
73107cb3 151 retval = fill_read_buffer(file->f_path.dentry,buffer);
e7b0d26a 152 if (retval)
1da177e4
LT
153 goto out;
154 }
5c1fdf41
ZB
155 pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",
156 __FUNCTION__, count, *ppos, buffer->page);
92f4c701
AM
157 retval = simple_read_from_buffer(buf, count, ppos, buffer->page,
158 buffer->count);
1da177e4 159out:
52e8c209 160 mutex_unlock(&buffer->mutex);
1da177e4
LT
161 return retval;
162}
163
1da177e4
LT
164/**
165 * fill_write_buffer - copy buffer from userspace.
166 * @buffer: data buffer for file.
67be2dd1 167 * @buf: data from user.
1da177e4
LT
168 * @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)
6e0dd741 185 count = PAGE_SIZE - 1;
1da177e4
LT
186 error = copy_from_user(buffer->page,buf,count);
187 buffer->needs_read_fill = 1;
035ed7a4
TM
188 /* 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;
1da177e4
LT
191 return error ? -EFAULT : count;
192}
193
194
195/**
196 * flush_write_buffer - push buffer to kobject.
3d41088f 197 * @dentry: dentry to the attribute
1da177e4 198 * @buffer: data buffer for file.
3d41088f 199 * @count: number of bytes
1da177e4
LT
200 *
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
0ab66088 206static int
1da177e4
LT
207flush_write_buffer(struct dentry * dentry, struct sysfs_buffer * buffer, size_t count)
208{
3e519038 209 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
b1fc3d61 210 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
1da177e4 211 struct sysfs_ops * ops = buffer->ops;
0ab66088
TH
212 int rc;
213
214 /* need attr_sd for attr and ops, its parent for kobj */
215 if (!sysfs_get_active_two(attr_sd))
216 return -ENODEV;
217
b1fc3d61 218 rc = ops->store(kobj, attr_sd->s_attr.attr, buffer->page, count);
0ab66088
TH
219
220 sysfs_put_active_two(attr_sd);
1da177e4 221
0ab66088 222 return rc;
1da177e4
LT
223}
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
52e8c209 249 mutex_lock(&buffer->mutex);
1da177e4
LT
250 len = fill_write_buffer(buffer, buf, count);
251 if (len > 0)
f427f5d5 252 len = flush_write_buffer(file->f_path.dentry, buffer, len);
1da177e4
LT
253 if (len > 0)
254 *ppos += len;
52e8c209 255 mutex_unlock(&buffer->mutex);
1da177e4
LT
256 return len;
257}
258
85a4ffad
TH
259/**
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);
a4e8b912
TH
306 atomic_set(&new_od->event, 1);
307 init_waitqueue_head(&new_od->poll);
85a4ffad
TH
308 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
94bebf4d 341static int sysfs_open_file(struct inode *inode, struct file *file)
1da177e4 342{
3e519038 343 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
b1fc3d61 344 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
1da177e4
LT
345 struct sysfs_buffer * buffer;
346 struct sysfs_ops * ops = NULL;
0ab66088 347 int error;
1da177e4 348
0ab66088
TH
349 /* need attr_sd for attr and ops, its parent for kobj */
350 if (!sysfs_get_active_two(attr_sd))
351 return -ENODEV;
1da177e4 352
1da177e4
LT
353 /* 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
73107cb3
TH
363 error = -EACCES;
364
1da177e4
LT
365 /* No sysfs operations, either from having no subsystem,
366 * or the subsystem have no operations.
367 */
368 if (!ops)
7b595756 369 goto err_out;
1da177e4
LT
370
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) {
1da177e4 376 if (!(inode->i_mode & S_IWUGO) || !ops->store)
7b595756 377 goto err_out;
1da177e4
LT
378 }
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)
7b595756 386 goto err_out;
1da177e4
LT
387 }
388
389 /* No error? Great, allocate a buffer for the file, and store it
390 * it in file->private_data for easy access.
391 */
0ab66088 392 error = -ENOMEM;
58d49283 393 buffer = kzalloc(sizeof(struct sysfs_buffer), GFP_KERNEL);
0ab66088 394 if (!buffer)
7b595756 395 goto err_out;
1da177e4 396
52e8c209 397 mutex_init(&buffer->mutex);
0ab66088
TH
398 buffer->needs_read_fill = 1;
399 buffer->ops = ops;
0ab66088
TH
400 file->private_data = buffer;
401
85a4ffad
TH
402 /* make sure we have open dirent struct */
403 error = sysfs_get_open_dirent(attr_sd, buffer);
404 if (error)
405 goto err_free;
406
b05f0548 407 /* open succeeded, put active references */
0ab66088 408 sysfs_put_active_two(attr_sd);
0ab66088
TH
409 return 0;
410
85a4ffad
TH
411 err_free:
412 kfree(buffer);
7b595756 413 err_out:
0ab66088 414 sysfs_put_active_two(attr_sd);
1da177e4
LT
415 return error;
416}
417
85a4ffad 418static int sysfs_release(struct inode *inode, struct file *filp)
1da177e4 419{
85a4ffad 420 struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata;
73107cb3 421 struct sysfs_buffer *buffer = filp->private_data;
1da177e4 422
85a4ffad
TH
423 sysfs_put_open_dirent(sd, buffer);
424
50ab1a72
TH
425 if (buffer->page)
426 free_page((unsigned long)buffer->page);
427 kfree(buffer);
428
1da177e4
LT
429 return 0;
430}
431
4508a7a7
N
432/* 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
a93720ee 443 * to see if it supports poll (Neither 'poll' nor 'select' return
4508a7a7
N
444 * 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;
0ab66088 449 struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata;
a4e8b912 450 struct sysfs_open_dirent *od = attr_sd->s_attr.open;
0ab66088
TH
451
452 /* need parent for the kobj, grab both */
453 if (!sysfs_get_active_two(attr_sd))
454 goto trigger;
4508a7a7 455
a4e8b912 456 poll_wait(filp, &od->poll, wait);
4508a7a7 457
0ab66088
TH
458 sysfs_put_active_two(attr_sd);
459
a4e8b912 460 if (buffer->event != atomic_read(&od->event))
0ab66088 461 goto trigger;
4508a7a7 462
0ab66088
TH
463 return 0;
464
465 trigger:
466 buffer->needs_read_fill = 1;
467 return POLLERR|POLLPRI;
4508a7a7
N
468}
469
51225039 470void sysfs_notify(struct kobject *k, char *dir, char *attr)
4508a7a7 471{
51225039 472 struct sysfs_dirent *sd = k->sd;
4508a7a7 473
51225039
TH
474 mutex_lock(&sysfs_mutex);
475
476 if (sd && dir)
477 sd = sysfs_find_dirent(sd, dir);
478 if (sd && attr)
479 sd = sysfs_find_dirent(sd, attr);
480 if (sd) {
a4e8b912
TH
481 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);
4508a7a7 492 }
51225039
TH
493
494 mutex_unlock(&sysfs_mutex);
4508a7a7
N
495}
496EXPORT_SYMBOL_GPL(sysfs_notify);
497
4b6f5d20 498const struct file_operations sysfs_file_operations = {
1da177e4
LT
499 .read = sysfs_read_file,
500 .write = sysfs_write_file,
501 .llseek = generic_file_llseek,
502 .open = sysfs_open_file,
503 .release = sysfs_release,
4508a7a7 504 .poll = sysfs_poll,
1da177e4
LT
505};
506
507
608e266a
TH
508int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,
509 int type)
1da177e4 510{
1da177e4 511 umode_t mode = (attr->mode & S_IALLUGO) | S_IFREG;
fb6896da 512 struct sysfs_addrm_cxt acxt;
a26cd722 513 struct sysfs_dirent *sd;
23dc2799 514 int rc;
1da177e4 515
3007e997
TH
516 sd = sysfs_new_dirent(attr->name, mode, type);
517 if (!sd)
518 return -ENOMEM;
b1fc3d61 519 sd->s_attr.attr = (void *)attr;
1da177e4 520
fb6896da 521 sysfs_addrm_start(&acxt, dir_sd);
23dc2799
TH
522 rc = sysfs_add_one(&acxt, sd);
523 sysfs_addrm_finish(&acxt);
a26cd722 524
23dc2799 525 if (rc)
967e35dc 526 sysfs_put(sd);
3007e997 527
23dc2799 528 return rc;
1da177e4
LT
529}
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{
608e266a 540 BUG_ON(!kobj || !kobj->sd || !attr);
1da177e4 541
608e266a 542 return sysfs_add_file(kobj->sd, attr, SYSFS_KOBJ_ATTR);
1da177e4
LT
543
544}
545
546
dfa87c82
AS
547/**
548 * 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{
608e266a 556 struct sysfs_dirent *dir_sd;
dfa87c82
AS
557 int error;
558
608e266a
TH
559 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
dfa87c82
AS
566 return error;
567}
568EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
569
31e5abe9
KS
570/**
571 * 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{
51225039
TH
579 struct sysfs_dirent *victim_sd = NULL;
580 struct dentry *victim = NULL;
bc062b1b
MS
581 struct inode * inode;
582 struct iattr newattrs;
51225039
TH
583 int rc;
584
585 rc = -ENOENT;
586 victim_sd = sysfs_get_dirent(kobj->sd, attr->name);
587 if (!victim_sd)
588 goto out;
589
932ea2e3 590 mutex_lock(&sysfs_rename_mutex);
51225039 591 victim = sysfs_get_dentry(victim_sd);
932ea2e3 592 mutex_unlock(&sysfs_rename_mutex);
51225039
TH
593 if (IS_ERR(victim)) {
594 rc = PTR_ERR(victim);
595 victim = NULL;
596 goto out;
31e5abe9 597 }
31e5abe9 598
51225039 599 inode = victim->d_inode;
f88123ea 600
51225039 601 mutex_lock(&inode->i_mutex);
f88123ea 602
51225039
TH
603 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);
f88123ea
TH
606
607 if (rc == 0) {
608 mutex_lock(&sysfs_mutex);
609 victim_sd->s_mode = newattrs.ia_mode;
610 mutex_unlock(&sysfs_mutex);
611 }
612
51225039
TH
613 mutex_unlock(&inode->i_mutex);
614 out:
615 dput(victim);
616 sysfs_put(victim_sd);
617 return rc;
31e5abe9
KS
618}
619EXPORT_SYMBOL_GPL(sysfs_chmod_file);
620
621
1da177e4
LT
622/**
623 * 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{
608e266a 632 sysfs_hash_and_remove(kobj->sd, attr->name);
1da177e4
LT
633}
634
635
dfa87c82
AS
636/**
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{
608e266a 645 struct sysfs_dirent *dir_sd;
dfa87c82 646
608e266a
TH
647 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);
dfa87c82
AS
651 }
652}
653EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
654
d9a9cdfb
AS
655struct sysfs_schedule_callback_struct {
656 struct kobject *kobj;
657 void (*func)(void *);
658 void *data;
523ded71 659 struct module *owner;
d9a9cdfb
AS
660 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);
523ded71 670 module_put(ss->owner);
d9a9cdfb
AS
671 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.
523ded71 679 * @owner: module owning the callback code
d9a9cdfb
AS
680 *
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
523ded71 692 * be allocated, -ENODEV if a reference to @owner isn't available.
d9a9cdfb
AS
693 */
694int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
523ded71 695 void *data, struct module *owner)
d9a9cdfb
AS
696{
697 struct sysfs_schedule_callback_struct *ss;
698
523ded71
AS
699 if (!try_module_get(owner))
700 return -ENODEV;
d9a9cdfb 701 ss = kmalloc(sizeof(*ss), GFP_KERNEL);
523ded71
AS
702 if (!ss) {
703 module_put(owner);
d9a9cdfb 704 return -ENOMEM;
523ded71 705 }
d9a9cdfb
AS
706 kobject_get(kobj);
707 ss->kobj = kobj;
708 ss->func = func;
709 ss->data = data;
523ded71 710 ss->owner = owner;
d9a9cdfb
AS
711 INIT_WORK(&ss->work, sysfs_schedule_callback_work);
712 schedule_work(&ss->work);
713 return 0;
714}
715EXPORT_SYMBOL_GPL(sysfs_schedule_callback);
716
dfa87c82 717
1da177e4
LT
718EXPORT_SYMBOL_GPL(sysfs_create_file);
719EXPORT_SYMBOL_GPL(sysfs_remove_file);
This page took 0.314528 seconds and 5 git commands to generate.