kobject: two typo fixes
[linux-2.6-block.git] / fs / sysfs / file.c
CommitLineData
1da177e4 1/*
6d66f5cd
TH
2 * 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.
1da177e4
LT
11 */
12
13#include <linux/module.h>
1da177e4 14#include <linux/kobject.h>
5f45f1a7 15#include <linux/namei.h>
4508a7a7 16#include <linux/poll.h>
94bebf4d 17#include <linux/list.h>
52e8c209 18#include <linux/mutex.h>
1da177e4 19#include <asm/uaccess.h>
1da177e4
LT
20
21#include "sysfs.h"
22
823bccfc 23#define to_sattr(a) container_of(a,struct subsys_attribute, attr)
1da177e4 24
3d41088f 25/*
1da177e4
LT
26 * 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{
823bccfc 33 struct kset *kset = to_kset(kobj);
1da177e4 34 struct subsys_attribute * sattr = to_sattr(attr);
c76d0abd 35 ssize_t ret = -EIO;
1da177e4
LT
36
37 if (sattr->show)
823bccfc 38 ret = sattr->show(kset, page);
1da177e4
LT
39 return ret;
40}
41
42static ssize_t
43subsys_attr_store(struct kobject * kobj, struct attribute * attr,
44 const char * page, size_t count)
45{
823bccfc 46 struct kset *kset = to_kset(kobj);
1da177e4 47 struct subsys_attribute * sattr = to_sattr(attr);
c76d0abd 48 ssize_t ret = -EIO;
1da177e4
LT
49
50 if (sattr->store)
823bccfc 51 ret = sattr->store(kset, page, count);
1da177e4
LT
52 return ret;
53}
54
55static struct sysfs_ops subsys_sysfs_ops = {
56 .show = subsys_attr_show,
57 .store = subsys_attr_store,
58};
59
85a4ffad
TH
60/*
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;
a4e8b912
TH
73 atomic_t event;
74 wait_queue_head_t poll;
85a4ffad
TH
75 struct list_head buffers; /* goes through sysfs_buffer.list */
76};
77
73107cb3
TH
78struct sysfs_buffer {
79 size_t count;
80 loff_t pos;
81 char * page;
82 struct sysfs_ops * ops;
52e8c209 83 struct mutex mutex;
73107cb3
TH
84 int needs_read_fill;
85 int event;
85a4ffad 86 struct list_head list;
73107cb3 87};
1da177e4
LT
88
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.
82244b16
ON
97 * This is called only once, on the file's first read unless an error
98 * is returned.
1da177e4
LT
99 */
100static int fill_read_buffer(struct dentry * dentry, struct sysfs_buffer * buffer)
101{
0ab66088 102 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
b1fc3d61 103 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
1da177e4
LT
104 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
0ab66088
TH
113 /* need attr_sd for attr and ops, its parent for kobj */
114 if (!sysfs_get_active_two(attr_sd))
115 return -ENODEV;
116
a4e8b912 117 buffer->event = atomic_read(&attr_sd->s_attr.open->event);
b1fc3d61 118 count = ops->show(kobj, attr_sd->s_attr.attr, buffer->page);
0ab66088
TH
119
120 sysfs_put_active_two(attr_sd);
121
1da177e4 122 BUG_ON(count > (ssize_t)PAGE_SIZE);
82244b16
ON
123 if (count >= 0) {
124 buffer->needs_read_fill = 0;
1da177e4 125 buffer->count = count;
82244b16 126 } else {
1da177e4 127 ret = count;
82244b16 128 }
1da177e4
LT
129 return ret;
130}
131
1da177e4
LT
132/**
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
52e8c209 157 mutex_lock(&buffer->mutex);
1da177e4 158 if (buffer->needs_read_fill) {
73107cb3 159 retval = fill_read_buffer(file->f_path.dentry,buffer);
e7b0d26a 160 if (retval)
1da177e4
LT
161 goto out;
162 }
5c1fdf41
ZB
163 pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",
164 __FUNCTION__, count, *ppos, buffer->page);
92f4c701
AM
165 retval = simple_read_from_buffer(buf, count, ppos, buffer->page,
166 buffer->count);
1da177e4 167out:
52e8c209 168 mutex_unlock(&buffer->mutex);
1da177e4
LT
169 return retval;
170}
171
1da177e4
LT
172/**
173 * fill_write_buffer - copy buffer from userspace.
174 * @buffer: data buffer for file.
67be2dd1 175 * @buf: data from user.
1da177e4
LT
176 * @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)
6e0dd741 193 count = PAGE_SIZE - 1;
1da177e4
LT
194 error = copy_from_user(buffer->page,buf,count);
195 buffer->needs_read_fill = 1;
035ed7a4
TM
196 /* 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;
1da177e4
LT
199 return error ? -EFAULT : count;
200}
201
202
203/**
204 * flush_write_buffer - push buffer to kobject.
3d41088f 205 * @dentry: dentry to the attribute
1da177e4 206 * @buffer: data buffer for file.
3d41088f 207 * @count: number of bytes
1da177e4
LT
208 *
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
0ab66088 214static int
1da177e4
LT
215flush_write_buffer(struct dentry * dentry, struct sysfs_buffer * buffer, size_t count)
216{
3e519038 217 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
b1fc3d61 218 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
1da177e4 219 struct sysfs_ops * ops = buffer->ops;
0ab66088
TH
220 int rc;
221
222 /* need attr_sd for attr and ops, its parent for kobj */
223 if (!sysfs_get_active_two(attr_sd))
224 return -ENODEV;
225
b1fc3d61 226 rc = ops->store(kobj, attr_sd->s_attr.attr, buffer->page, count);
0ab66088
TH
227
228 sysfs_put_active_two(attr_sd);
1da177e4 229
0ab66088 230 return rc;
1da177e4
LT
231}
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
52e8c209 257 mutex_lock(&buffer->mutex);
1da177e4
LT
258 len = fill_write_buffer(buffer, buf, count);
259 if (len > 0)
f427f5d5 260 len = flush_write_buffer(file->f_path.dentry, buffer, len);
1da177e4
LT
261 if (len > 0)
262 *ppos += len;
52e8c209 263 mutex_unlock(&buffer->mutex);
1da177e4
LT
264 return len;
265}
266
85a4ffad
TH
267/**
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);
a4e8b912
TH
314 atomic_set(&new_od->event, 1);
315 init_waitqueue_head(&new_od->poll);
85a4ffad
TH
316 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
94bebf4d 349static int sysfs_open_file(struct inode *inode, struct file *file)
1da177e4 350{
3e519038 351 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
b1fc3d61 352 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
1da177e4
LT
353 struct sysfs_buffer * buffer;
354 struct sysfs_ops * ops = NULL;
0ab66088 355 int error;
1da177e4 356
0ab66088
TH
357 /* need attr_sd for attr and ops, its parent for kobj */
358 if (!sysfs_get_active_two(attr_sd))
359 return -ENODEV;
1da177e4 360
1da177e4
LT
361 /* 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
73107cb3
TH
371 error = -EACCES;
372
1da177e4
LT
373 /* No sysfs operations, either from having no subsystem,
374 * or the subsystem have no operations.
375 */
376 if (!ops)
7b595756 377 goto err_out;
1da177e4
LT
378
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) {
1da177e4 384 if (!(inode->i_mode & S_IWUGO) || !ops->store)
7b595756 385 goto err_out;
1da177e4
LT
386 }
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)
7b595756 394 goto err_out;
1da177e4
LT
395 }
396
397 /* No error? Great, allocate a buffer for the file, and store it
398 * it in file->private_data for easy access.
399 */
0ab66088 400 error = -ENOMEM;
58d49283 401 buffer = kzalloc(sizeof(struct sysfs_buffer), GFP_KERNEL);
0ab66088 402 if (!buffer)
7b595756 403 goto err_out;
1da177e4 404
52e8c209 405 mutex_init(&buffer->mutex);
0ab66088
TH
406 buffer->needs_read_fill = 1;
407 buffer->ops = ops;
0ab66088
TH
408 file->private_data = buffer;
409
85a4ffad
TH
410 /* make sure we have open dirent struct */
411 error = sysfs_get_open_dirent(attr_sd, buffer);
412 if (error)
413 goto err_free;
414
b05f0548 415 /* open succeeded, put active references */
0ab66088 416 sysfs_put_active_two(attr_sd);
0ab66088
TH
417 return 0;
418
85a4ffad
TH
419 err_free:
420 kfree(buffer);
7b595756 421 err_out:
0ab66088 422 sysfs_put_active_two(attr_sd);
1da177e4
LT
423 return error;
424}
425
85a4ffad 426static int sysfs_release(struct inode *inode, struct file *filp)
1da177e4 427{
85a4ffad 428 struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata;
73107cb3 429 struct sysfs_buffer *buffer = filp->private_data;
1da177e4 430
85a4ffad
TH
431 sysfs_put_open_dirent(sd, buffer);
432
50ab1a72
TH
433 if (buffer->page)
434 free_page((unsigned long)buffer->page);
435 kfree(buffer);
436
1da177e4
LT
437 return 0;
438}
439
4508a7a7
N
440/* 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
a93720ee 451 * to see if it supports poll (Neither 'poll' nor 'select' return
4508a7a7
N
452 * 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;
0ab66088 457 struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata;
a4e8b912 458 struct sysfs_open_dirent *od = attr_sd->s_attr.open;
0ab66088
TH
459
460 /* need parent for the kobj, grab both */
461 if (!sysfs_get_active_two(attr_sd))
462 goto trigger;
4508a7a7 463
a4e8b912 464 poll_wait(filp, &od->poll, wait);
4508a7a7 465
0ab66088
TH
466 sysfs_put_active_two(attr_sd);
467
a4e8b912 468 if (buffer->event != atomic_read(&od->event))
0ab66088 469 goto trigger;
4508a7a7 470
0ab66088
TH
471 return 0;
472
473 trigger:
474 buffer->needs_read_fill = 1;
475 return POLLERR|POLLPRI;
4508a7a7
N
476}
477
51225039 478void sysfs_notify(struct kobject *k, char *dir, char *attr)
4508a7a7 479{
51225039 480 struct sysfs_dirent *sd = k->sd;
4508a7a7 481
51225039
TH
482 mutex_lock(&sysfs_mutex);
483
484 if (sd && dir)
485 sd = sysfs_find_dirent(sd, dir);
486 if (sd && attr)
487 sd = sysfs_find_dirent(sd, attr);
488 if (sd) {
a4e8b912
TH
489 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);
4508a7a7 500 }
51225039
TH
501
502 mutex_unlock(&sysfs_mutex);
4508a7a7
N
503}
504EXPORT_SYMBOL_GPL(sysfs_notify);
505
4b6f5d20 506const struct file_operations sysfs_file_operations = {
1da177e4
LT
507 .read = sysfs_read_file,
508 .write = sysfs_write_file,
509 .llseek = generic_file_llseek,
510 .open = sysfs_open_file,
511 .release = sysfs_release,
4508a7a7 512 .poll = sysfs_poll,
1da177e4
LT
513};
514
515
608e266a
TH
516int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,
517 int type)
1da177e4 518{
1da177e4 519 umode_t mode = (attr->mode & S_IALLUGO) | S_IFREG;
fb6896da 520 struct sysfs_addrm_cxt acxt;
a26cd722 521 struct sysfs_dirent *sd;
23dc2799 522 int rc;
1da177e4 523
3007e997
TH
524 sd = sysfs_new_dirent(attr->name, mode, type);
525 if (!sd)
526 return -ENOMEM;
b1fc3d61 527 sd->s_attr.attr = (void *)attr;
1da177e4 528
fb6896da 529 sysfs_addrm_start(&acxt, dir_sd);
23dc2799
TH
530 rc = sysfs_add_one(&acxt, sd);
531 sysfs_addrm_finish(&acxt);
a26cd722 532
23dc2799 533 if (rc)
967e35dc 534 sysfs_put(sd);
3007e997 535
23dc2799 536 return rc;
1da177e4
LT
537}
538
539
540/**
541 * sysfs_create_file - create an attribute file for an object.
542 * @kobj: object we're creating for.
3932bf60 543 * @attr: attribute descriptor.
1da177e4
LT
544 */
545
546int sysfs_create_file(struct kobject * kobj, const struct attribute * attr)
547{
608e266a 548 BUG_ON(!kobj || !kobj->sd || !attr);
1da177e4 549
608e266a 550 return sysfs_add_file(kobj->sd, attr, SYSFS_KOBJ_ATTR);
1da177e4
LT
551
552}
553
554
dfa87c82
AS
555/**
556 * 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{
608e266a 564 struct sysfs_dirent *dir_sd;
dfa87c82
AS
565 int error;
566
608e266a
TH
567 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
dfa87c82
AS
574 return error;
575}
576EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
577
31e5abe9
KS
578/**
579 * 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{
51225039
TH
587 struct sysfs_dirent *victim_sd = NULL;
588 struct dentry *victim = NULL;
bc062b1b
MS
589 struct inode * inode;
590 struct iattr newattrs;
51225039
TH
591 int rc;
592
593 rc = -ENOENT;
594 victim_sd = sysfs_get_dirent(kobj->sd, attr->name);
595 if (!victim_sd)
596 goto out;
597
932ea2e3 598 mutex_lock(&sysfs_rename_mutex);
51225039 599 victim = sysfs_get_dentry(victim_sd);
932ea2e3 600 mutex_unlock(&sysfs_rename_mutex);
51225039
TH
601 if (IS_ERR(victim)) {
602 rc = PTR_ERR(victim);
603 victim = NULL;
604 goto out;
31e5abe9 605 }
31e5abe9 606
51225039 607 inode = victim->d_inode;
f88123ea 608
51225039 609 mutex_lock(&inode->i_mutex);
f88123ea 610
51225039
TH
611 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);
f88123ea
TH
614
615 if (rc == 0) {
616 mutex_lock(&sysfs_mutex);
617 victim_sd->s_mode = newattrs.ia_mode;
618 mutex_unlock(&sysfs_mutex);
619 }
620
51225039
TH
621 mutex_unlock(&inode->i_mutex);
622 out:
623 dput(victim);
624 sysfs_put(victim_sd);
625 return rc;
31e5abe9
KS
626}
627EXPORT_SYMBOL_GPL(sysfs_chmod_file);
628
629
1da177e4
LT
630/**
631 * 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{
608e266a 640 sysfs_hash_and_remove(kobj->sd, attr->name);
1da177e4
LT
641}
642
643
dfa87c82
AS
644/**
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{
608e266a 653 struct sysfs_dirent *dir_sd;
dfa87c82 654
608e266a
TH
655 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);
dfa87c82
AS
659 }
660}
661EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
662
d9a9cdfb
AS
663struct sysfs_schedule_callback_struct {
664 struct kobject *kobj;
665 void (*func)(void *);
666 void *data;
523ded71 667 struct module *owner;
d9a9cdfb
AS
668 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);
523ded71 678 module_put(ss->owner);
d9a9cdfb
AS
679 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.
523ded71 687 * @owner: module owning the callback code
d9a9cdfb
AS
688 *
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
523ded71 700 * be allocated, -ENODEV if a reference to @owner isn't available.
d9a9cdfb
AS
701 */
702int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
523ded71 703 void *data, struct module *owner)
d9a9cdfb
AS
704{
705 struct sysfs_schedule_callback_struct *ss;
706
523ded71
AS
707 if (!try_module_get(owner))
708 return -ENODEV;
d9a9cdfb 709 ss = kmalloc(sizeof(*ss), GFP_KERNEL);
523ded71
AS
710 if (!ss) {
711 module_put(owner);
d9a9cdfb 712 return -ENOMEM;
523ded71 713 }
d9a9cdfb
AS
714 kobject_get(kobj);
715 ss->kobj = kobj;
716 ss->func = func;
717 ss->data = data;
523ded71 718 ss->owner = owner;
d9a9cdfb
AS
719 INIT_WORK(&ss->work, sysfs_schedule_callback_work);
720 schedule_work(&ss->work);
721 return 0;
722}
723EXPORT_SYMBOL_GPL(sysfs_schedule_callback);
724
dfa87c82 725
1da177e4
LT
726EXPORT_SYMBOL_GPL(sysfs_create_file);
727EXPORT_SYMBOL_GPL(sysfs_remove_file);