blob: 4b55bcf4422ee84a29c42e129070e5ff6f3c5e81 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Tejun Heo6d66f5c2007-09-20 17:31:38 +09002 * fs/sysfs/file.c - sysfs regular (text) file implementation
3 *
4 * Copyright (c) 2001-3 Patrick Mochel
5 * Copyright (c) 2007 SUSE Linux Products GmbH
6 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
7 *
8 * This file is released under the GPLv2.
9 *
10 * Please see Documentation/filesystems/sysfs.txt for more information.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
13#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/kobject.h>
Andrew Morton815d2d52008-03-04 15:09:07 -080015#include <linux/kallsyms.h>
Robert P. J. Dayc6f87732008-03-13 22:41:52 -040016#include <linux/slab.h>
Miklos Szeredi93265d12008-06-16 13:46:47 +020017#include <linux/fsnotify.h>
Christoph Hellwig5f45f1a2005-06-23 00:09:12 -070018#include <linux/namei.h>
NeilBrown4508a7a2006-03-20 17:53:53 +110019#include <linux/poll.h>
Oliver Neukum94bebf42006-12-20 10:52:44 +010020#include <linux/list.h>
Dave Young52e8c202007-07-26 11:03:54 +000021#include <linux/mutex.h>
Andrew Mortonae87221d2007-08-24 16:11:54 -070022#include <linux/limits.h>
Greg Kroah-Hartman060cc742013-08-21 16:34:59 -070023#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include "sysfs.h"
26
Tejun Heo85a4ffa2007-09-20 16:05:12 +090027/*
Tejun Heo58282d82013-10-01 17:41:59 -040028 * There's one sysfs_open_file for each open file and one sysfs_open_dirent
Tejun Heoc75ec762013-10-01 17:41:58 -040029 * for each sysfs_dirent with one or more open files.
Tejun Heo85a4ffa2007-09-20 16:05:12 +090030 *
Tejun Heoc75ec762013-10-01 17:41:58 -040031 * sysfs_dirent->s_attr.open points to sysfs_open_dirent. s_attr.open is
32 * protected by sysfs_open_dirent_lock.
33 *
Tejun Heo58282d82013-10-01 17:41:59 -040034 * filp->private_data points to sysfs_open_file which is chained at
35 * sysfs_open_dirent->files, which is protected by sysfs_open_file_mutex.
Tejun Heo85a4ffa2007-09-20 16:05:12 +090036 */
Jiri Slabyd7b37882007-11-21 14:55:19 -080037static DEFINE_SPINLOCK(sysfs_open_dirent_lock);
Tejun Heoc75ec762013-10-01 17:41:58 -040038static DEFINE_MUTEX(sysfs_open_file_mutex);
Tejun Heo85a4ffa2007-09-20 16:05:12 +090039
40struct sysfs_open_dirent {
41 atomic_t refcnt;
Tejun Heoa4e8b912007-09-20 16:05:12 +090042 atomic_t event;
43 wait_queue_head_t poll;
Tejun Heo58282d82013-10-01 17:41:59 -040044 struct list_head files; /* goes through sysfs_open_file.list */
Tejun Heo85a4ffa2007-09-20 16:05:12 +090045};
46
Tejun Heo58282d82013-10-01 17:41:59 -040047struct sysfs_open_file {
Tejun Heo73107cb2007-06-14 03:45:16 +090048 size_t count;
Greg Kroah-Hartman1b18dc22013-08-21 16:28:26 -070049 char *page;
Dave Young52e8c202007-07-26 11:03:54 +000050 struct mutex mutex;
Tejun Heo73107cb2007-06-14 03:45:16 +090051 int event;
Tejun Heo85a4ffa2007-09-20 16:05:12 +090052 struct list_head list;
Tejun Heo73107cb2007-06-14 03:45:16 +090053};
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Tejun Heo375b6112013-10-01 17:41:57 -040055/*
56 * Determine ktype->sysfs_ops for the given sysfs_dirent. This function
57 * must be called while holding an active reference.
58 */
59static const struct sysfs_ops *sysfs_file_ops(struct sysfs_dirent *sd)
60{
61 struct kobject *kobj = sd->s_parent->s_dir.kobj;
62
63 lockdep_assert_held(sd);
64 return kobj->ktype ? kobj->ktype->sysfs_ops : NULL;
65}
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067/**
68 * fill_read_buffer - allocate and fill buffer from object.
69 * @dentry: dentry pointer.
70 * @buffer: data buffer for file.
71 *
72 * Allocate @buffer->page, if it hasn't been already, then call the
Greg Kroah-Hartmanab9bf4b2013-08-21 16:21:17 -070073 * kobject's show() method to fill the buffer with this attribute's
74 * data.
Oliver Neukum82244b12007-01-02 08:48:08 +010075 * This is called only once, on the file's first read unless an error
76 * is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 */
Tejun Heo58282d82013-10-01 17:41:59 -040078static int fill_read_buffer(struct dentry *dentry, struct sysfs_open_file *of)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Tejun Heo0ab66082007-06-14 03:45:16 +090080 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
Tejun Heob1fc3d62007-09-20 16:05:11 +090081 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
Tejun Heo375b6112013-10-01 17:41:57 -040082 const struct sysfs_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 int ret = 0;
84 ssize_t count;
85
Tejun Heo58282d82013-10-01 17:41:59 -040086 if (!of->page)
87 of->page = (char *) get_zeroed_page(GFP_KERNEL);
88 if (!of->page)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 return -ENOMEM;
90
Tejun Heo0ab66082007-06-14 03:45:16 +090091 /* need attr_sd for attr and ops, its parent for kobj */
Eric W. Biedermane72ceb82010-02-11 15:18:38 -080092 if (!sysfs_get_active(attr_sd))
Tejun Heo0ab66082007-06-14 03:45:16 +090093 return -ENODEV;
94
Tejun Heo58282d82013-10-01 17:41:59 -040095 of->event = atomic_read(&attr_sd->s_attr.open->event);
Tejun Heo375b6112013-10-01 17:41:57 -040096
97 ops = sysfs_file_ops(attr_sd);
Tejun Heo58282d82013-10-01 17:41:59 -040098 count = ops->show(kobj, attr_sd->s_attr.attr, of->page);
Tejun Heo0ab66082007-06-14 03:45:16 +090099
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800100 sysfs_put_active(attr_sd);
Tejun Heo0ab66082007-06-14 03:45:16 +0900101
Miao Xie8118a852007-11-21 14:55:19 -0800102 /*
103 * The code works fine with PAGE_SIZE return but it's likely to
104 * indicate truncated result or overflow in normal use cases.
105 */
Andrew Morton815d2d52008-03-04 15:09:07 -0800106 if (count >= (ssize_t)PAGE_SIZE) {
107 print_symbol("fill_read_buffer: %s returned bad count\n",
108 (unsigned long)ops->show);
109 /* Try to struggle along */
110 count = PAGE_SIZE - 1;
111 }
Tejun Heoaea585e2013-10-01 17:41:56 -0400112 if (count >= 0)
Tejun Heo58282d82013-10-01 17:41:59 -0400113 of->count = count;
Tejun Heoaea585e2013-10-01 17:41:56 -0400114 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 ret = count;
116 return ret;
117}
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119/**
Greg Kroah-Hartmanab9bf4b2013-08-21 16:21:17 -0700120 * sysfs_read_file - read an attribute.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 * @file: file pointer.
122 * @buf: buffer to fill.
123 * @count: number of bytes to read.
124 * @ppos: starting offset in file.
125 *
126 * Userspace wants to read an attribute file. The attribute descriptor
127 * is in the file's ->d_fsdata. The target object is in the directory's
128 * ->d_fsdata.
129 *
130 * We call fill_read_buffer() to allocate and fill the buffer from the
131 * object's show() method exactly once (if the read is happening from
132 * the beginning of the file). That should fill the entire buffer with
133 * all the data the object has to offer for that attribute.
134 * We then call flush_read_buffer() to copy the buffer to userspace
135 * in the increments specified.
136 */
137
138static ssize_t
139sysfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
140{
Tejun Heo58282d82013-10-01 17:41:59 -0400141 struct sysfs_open_file *of = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 ssize_t retval = 0;
143
Tejun Heo58282d82013-10-01 17:41:59 -0400144 mutex_lock(&of->mutex);
Tejun Heoaea585e2013-10-01 17:41:56 -0400145 /*
146 * Fill on zero offset and the first read so that silly things like
147 * "dd bs=1 skip=N" can work on sysfs files.
148 */
Tejun Heo58282d82013-10-01 17:41:59 -0400149 if (*ppos == 0 || !of->page) {
150 retval = fill_read_buffer(file->f_path.dentry, of);
Alan Sterne7b0d262007-03-15 15:51:28 -0400151 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 goto out;
153 }
Zach Brown5c1fdf42006-10-03 01:16:06 -0700154 pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",
Tejun Heo58282d82013-10-01 17:41:59 -0400155 __func__, count, *ppos, of->page);
156 retval = simple_read_from_buffer(buf, count, ppos, of->page, of->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157out:
Tejun Heo58282d82013-10-01 17:41:59 -0400158 mutex_unlock(&of->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 return retval;
160}
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162/**
163 * fill_write_buffer - copy buffer from userspace.
Tejun Heo58282d82013-10-01 17:41:59 -0400164 * @of: open file struct.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700165 * @buf: data from user.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 * @count: number of bytes in @userbuf.
167 *
Tejun Heo58282d82013-10-01 17:41:59 -0400168 * Allocate @of->page if it hasn't been already, then copy the
169 * user-supplied buffer into it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 */
Tejun Heo58282d82013-10-01 17:41:59 -0400171static int fill_write_buffer(struct sysfs_open_file *of,
Greg Kroah-Hartman1b18dc22013-08-21 16:28:26 -0700172 const char __user *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
174 int error;
175
Tejun Heo58282d82013-10-01 17:41:59 -0400176 if (!of->page)
177 of->page = (char *)get_zeroed_page(GFP_KERNEL);
178 if (!of->page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return -ENOMEM;
180
181 if (count >= PAGE_SIZE)
Greg Kroah-Hartman6e0dd742006-03-31 15:37:06 -0800182 count = PAGE_SIZE - 1;
Tejun Heo58282d82013-10-01 17:41:59 -0400183 error = copy_from_user(of->page, buf, count);
Tejun Heoaea585e2013-10-01 17:41:56 -0400184
185 /*
186 * If buf is assumed to contain a string, terminate it by \0, so
187 * e.g. sscanf() can scan the string easily.
188 */
Tejun Heo58282d82013-10-01 17:41:59 -0400189 of->page[count] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return error ? -EFAULT : count;
191}
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193/**
194 * flush_write_buffer - push buffer to kobject.
Martin Waitz3d410882005-06-23 22:05:21 -0700195 * @dentry: dentry to the attribute
Tejun Heo58282d82013-10-01 17:41:59 -0400196 * @of: open file
Martin Waitz3d410882005-06-23 22:05:21 -0700197 * @count: number of bytes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 *
199 * Get the correct pointers for the kobject and the attribute we're
Greg Kroah-Hartmanab9bf4b2013-08-21 16:21:17 -0700200 * dealing with, then call the store() method for the attribute,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 * passing the buffer that we acquired in fill_write_buffer().
202 */
Greg Kroah-Hartmanddfd6d02013-08-21 16:33:34 -0700203static int flush_write_buffer(struct dentry *dentry,
Tejun Heo58282d82013-10-01 17:41:59 -0400204 struct sysfs_open_file *of, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Tejun Heo3e519032007-06-14 03:45:15 +0900206 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900207 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
Tejun Heo375b6112013-10-01 17:41:57 -0400208 const struct sysfs_ops *ops;
Tejun Heo0ab66082007-06-14 03:45:16 +0900209 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Tejun Heo0ab66082007-06-14 03:45:16 +0900211 /* need attr_sd for attr and ops, its parent for kobj */
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800212 if (!sysfs_get_active(attr_sd))
Tejun Heo0ab66082007-06-14 03:45:16 +0900213 return -ENODEV;
214
Tejun Heo375b6112013-10-01 17:41:57 -0400215 ops = sysfs_file_ops(attr_sd);
Tejun Heo58282d82013-10-01 17:41:59 -0400216 rc = ops->store(kobj, attr_sd->s_attr.attr, of->page, count);
Tejun Heo0ab66082007-06-14 03:45:16 +0900217
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800218 sysfs_put_active(attr_sd);
Tejun Heo0ab66082007-06-14 03:45:16 +0900219
220 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223/**
224 * sysfs_write_file - write an attribute.
225 * @file: file pointer
226 * @buf: data to write
227 * @count: number of bytes
228 * @ppos: starting offset
229 *
230 * Similar to sysfs_read_file(), though working in the opposite direction.
231 * We allocate and fill the data from the user in fill_write_buffer(),
232 * then push it to the kobject in flush_write_buffer().
233 * There is no easy way for us to know if userspace is only doing a partial
234 * write, so we don't support them. We expect the entire buffer to come
Greg Kroah-Hartmanab9bf4b2013-08-21 16:21:17 -0700235 * on the first write.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 * Hint: if you're writing a value, first read the file, modify only the
Greg Kroah-Hartmanab9bf4b2013-08-21 16:21:17 -0700237 * the value you're changing, then write entire buffer back.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 */
Greg Kroah-Hartmanddfd6d02013-08-21 16:33:34 -0700239static ssize_t sysfs_write_file(struct file *file, const char __user *buf,
240 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Tejun Heo58282d82013-10-01 17:41:59 -0400242 struct sysfs_open_file *of = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 ssize_t len;
244
Tejun Heo58282d82013-10-01 17:41:59 -0400245 mutex_lock(&of->mutex);
246 len = fill_write_buffer(of, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (len > 0)
Tejun Heo58282d82013-10-01 17:41:59 -0400248 len = flush_write_buffer(file->f_path.dentry, of, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 if (len > 0)
250 *ppos += len;
Tejun Heo58282d82013-10-01 17:41:59 -0400251 mutex_unlock(&of->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return len;
253}
254
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900255/**
256 * sysfs_get_open_dirent - get or create sysfs_open_dirent
257 * @sd: target sysfs_dirent
Tejun Heo58282d82013-10-01 17:41:59 -0400258 * @of: sysfs_open_file for this instance of open
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900259 *
260 * If @sd->s_attr.open exists, increment its reference count;
Tejun Heo58282d82013-10-01 17:41:59 -0400261 * otherwise, create one. @of is chained to the files list.
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900262 *
263 * LOCKING:
264 * Kernel thread context (may sleep).
265 *
266 * RETURNS:
267 * 0 on success, -errno on failure.
268 */
269static int sysfs_get_open_dirent(struct sysfs_dirent *sd,
Tejun Heo58282d82013-10-01 17:41:59 -0400270 struct sysfs_open_file *of)
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900271{
272 struct sysfs_open_dirent *od, *new_od = NULL;
273
274 retry:
Tejun Heoc75ec762013-10-01 17:41:58 -0400275 mutex_lock(&sysfs_open_file_mutex);
Neil Brown83db93f2009-09-15 16:05:51 -0700276 spin_lock_irq(&sysfs_open_dirent_lock);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900277
278 if (!sd->s_attr.open && new_od) {
279 sd->s_attr.open = new_od;
280 new_od = NULL;
281 }
282
283 od = sd->s_attr.open;
284 if (od) {
285 atomic_inc(&od->refcnt);
Tejun Heo58282d82013-10-01 17:41:59 -0400286 list_add_tail(&of->list, &od->files);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900287 }
288
Neil Brown83db93f2009-09-15 16:05:51 -0700289 spin_unlock_irq(&sysfs_open_dirent_lock);
Tejun Heoc75ec762013-10-01 17:41:58 -0400290 mutex_unlock(&sysfs_open_file_mutex);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900291
292 if (od) {
293 kfree(new_od);
294 return 0;
295 }
296
297 /* not there, initialize a new one and retry */
298 new_od = kmalloc(sizeof(*new_od), GFP_KERNEL);
299 if (!new_od)
300 return -ENOMEM;
301
302 atomic_set(&new_od->refcnt, 0);
Tejun Heoa4e8b912007-09-20 16:05:12 +0900303 atomic_set(&new_od->event, 1);
304 init_waitqueue_head(&new_od->poll);
Tejun Heo58282d82013-10-01 17:41:59 -0400305 INIT_LIST_HEAD(&new_od->files);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900306 goto retry;
307}
308
309/**
310 * sysfs_put_open_dirent - put sysfs_open_dirent
311 * @sd: target sysfs_dirent
Tejun Heo58282d82013-10-01 17:41:59 -0400312 * @of: associated sysfs_open_file
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900313 *
Tejun Heo58282d82013-10-01 17:41:59 -0400314 * Put @sd->s_attr.open and unlink @of from the files list. If
315 * reference count reaches zero, disassociate and free it.
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900316 *
317 * LOCKING:
318 * None.
319 */
320static void sysfs_put_open_dirent(struct sysfs_dirent *sd,
Tejun Heo58282d82013-10-01 17:41:59 -0400321 struct sysfs_open_file *of)
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900322{
323 struct sysfs_open_dirent *od = sd->s_attr.open;
Neil Brown83db93f2009-09-15 16:05:51 -0700324 unsigned long flags;
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900325
Tejun Heoc75ec762013-10-01 17:41:58 -0400326 mutex_lock(&sysfs_open_file_mutex);
Neil Brown83db93f2009-09-15 16:05:51 -0700327 spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900328
Tejun Heo58282d82013-10-01 17:41:59 -0400329 list_del(&of->list);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900330 if (atomic_dec_and_test(&od->refcnt))
331 sd->s_attr.open = NULL;
332 else
333 od = NULL;
334
Neil Brown83db93f2009-09-15 16:05:51 -0700335 spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
Tejun Heoc75ec762013-10-01 17:41:58 -0400336 mutex_unlock(&sysfs_open_file_mutex);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900337
338 kfree(od);
339}
340
Oliver Neukum94bebf42006-12-20 10:52:44 +0100341static int sysfs_open_file(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
Tejun Heo3e519032007-06-14 03:45:15 +0900343 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900344 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
Tejun Heo58282d82013-10-01 17:41:59 -0400345 struct sysfs_open_file *of;
Emese Revfy52cf25d2010-01-19 02:58:23 +0100346 const struct sysfs_ops *ops;
Kay Sievers000f2a42007-11-02 13:47:53 +0100347 int error = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Tejun Heo0ab66082007-06-14 03:45:16 +0900349 /* need attr_sd for attr and ops, its parent for kobj */
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800350 if (!sysfs_get_active(attr_sd))
Tejun Heo0ab66082007-06-14 03:45:16 +0900351 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Kay Sievers000f2a42007-11-02 13:47:53 +0100353 /* every kobject with an attribute needs a ktype assigned */
Tejun Heo375b6112013-10-01 17:41:57 -0400354 ops = sysfs_file_ops(attr_sd);
355 if (WARN(!ops, KERN_ERR
356 "missing sysfs attribute operations for kobject: %s\n",
357 kobject_name(kobj)))
Tejun Heo7b595752007-06-14 03:45:17 +0900358 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 /* File needs write support.
Greg Kroah-Hartmanab9bf4b2013-08-21 16:21:17 -0700361 * The inode's perms must say it's ok,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 * and we must have a store method.
363 */
364 if (file->f_mode & FMODE_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 if (!(inode->i_mode & S_IWUGO) || !ops->store)
Tejun Heo7b595752007-06-14 03:45:17 +0900366 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
368
369 /* File needs read support.
370 * The inode's perms must say it's ok, and we there
371 * must be a show method for it.
372 */
373 if (file->f_mode & FMODE_READ) {
374 if (!(inode->i_mode & S_IRUGO) || !ops->show)
Tejun Heo7b595752007-06-14 03:45:17 +0900375 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
377
Tejun Heo58282d82013-10-01 17:41:59 -0400378 /*
379 * No error? Great, allocate a sysfs_open_file for the file, and
380 * store it it in file->private_data for easy access.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 */
Tejun Heo0ab66082007-06-14 03:45:16 +0900382 error = -ENOMEM;
Tejun Heo58282d82013-10-01 17:41:59 -0400383 of = kzalloc(sizeof(struct sysfs_open_file), GFP_KERNEL);
384 if (!of)
Tejun Heo7b595752007-06-14 03:45:17 +0900385 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Tejun Heo58282d82013-10-01 17:41:59 -0400387 mutex_init(&of->mutex);
388 file->private_data = of;
Tejun Heo0ab66082007-06-14 03:45:16 +0900389
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900390 /* make sure we have open dirent struct */
Tejun Heo58282d82013-10-01 17:41:59 -0400391 error = sysfs_get_open_dirent(attr_sd, of);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900392 if (error)
393 goto err_free;
394
Tejun Heob05f0542007-09-20 16:05:10 +0900395 /* open succeeded, put active references */
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800396 sysfs_put_active(attr_sd);
Tejun Heo0ab66082007-06-14 03:45:16 +0900397 return 0;
398
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900399 err_free:
Tejun Heo58282d82013-10-01 17:41:59 -0400400 kfree(of);
Tejun Heo7b595752007-06-14 03:45:17 +0900401 err_out:
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800402 sysfs_put_active(attr_sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 return error;
404}
405
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900406static int sysfs_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900408 struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata;
Tejun Heo58282d82013-10-01 17:41:59 -0400409 struct sysfs_open_file *of = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Tejun Heo58282d82013-10-01 17:41:59 -0400411 sysfs_put_open_dirent(sd, of);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900412
Tejun Heo58282d82013-10-01 17:41:59 -0400413 if (of->page)
414 free_page((unsigned long)of->page);
415 kfree(of);
Tejun Heo50ab1a72007-09-20 16:05:10 +0900416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return 0;
418}
419
NeilBrown4508a7a2006-03-20 17:53:53 +1100420/* Sysfs attribute files are pollable. The idea is that you read
421 * the content and then you use 'poll' or 'select' to wait for
422 * the content to change. When the content changes (assuming the
423 * manager for the kobject supports notification), poll will
424 * return POLLERR|POLLPRI, and select will return the fd whether
425 * it is waiting for read, write, or exceptions.
426 * Once poll/select indicates that the value has changed, you
Dan Williams2424b5d2008-04-07 15:35:01 -0700427 * need to close and re-open the file, or seek to 0 and read again.
NeilBrown4508a7a2006-03-20 17:53:53 +1100428 * Reminder: this only works for attributes which actively support
429 * it, and it is not possible to test an attribute from userspace
Rolf Eike Beera93720e2007-08-10 13:51:07 -0700430 * to see if it supports poll (Neither 'poll' nor 'select' return
NeilBrown4508a7a2006-03-20 17:53:53 +1100431 * an appropriate error code). When in doubt, set a suitable timeout value.
432 */
433static unsigned int sysfs_poll(struct file *filp, poll_table *wait)
434{
Tejun Heo58282d82013-10-01 17:41:59 -0400435 struct sysfs_open_file *of = filp->private_data;
Tejun Heo0ab66082007-06-14 03:45:16 +0900436 struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata;
Tejun Heoa4e8b912007-09-20 16:05:12 +0900437 struct sysfs_open_dirent *od = attr_sd->s_attr.open;
Tejun Heo0ab66082007-06-14 03:45:16 +0900438
439 /* need parent for the kobj, grab both */
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800440 if (!sysfs_get_active(attr_sd))
Tejun Heo0ab66082007-06-14 03:45:16 +0900441 goto trigger;
NeilBrown4508a7a2006-03-20 17:53:53 +1100442
Tejun Heoa4e8b912007-09-20 16:05:12 +0900443 poll_wait(filp, &od->poll, wait);
NeilBrown4508a7a2006-03-20 17:53:53 +1100444
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800445 sysfs_put_active(attr_sd);
NeilBrown4508a7a2006-03-20 17:53:53 +1100446
Tejun Heo58282d82013-10-01 17:41:59 -0400447 if (of->event != atomic_read(&od->event))
Tejun Heo0ab66082007-06-14 03:45:16 +0900448 goto trigger;
449
KOSAKI Motohiro1af35572009-04-09 13:53:22 +0900450 return DEFAULT_POLLMASK;
Tejun Heo0ab66082007-06-14 03:45:16 +0900451
452 trigger:
KOSAKI Motohiro1af35572009-04-09 13:53:22 +0900453 return DEFAULT_POLLMASK|POLLERR|POLLPRI;
NeilBrown4508a7a2006-03-20 17:53:53 +1100454}
455
Neil Brownf1282c82008-07-16 08:58:04 +1000456void sysfs_notify_dirent(struct sysfs_dirent *sd)
457{
458 struct sysfs_open_dirent *od;
Neil Brown83db93f2009-09-15 16:05:51 -0700459 unsigned long flags;
Neil Brownf1282c82008-07-16 08:58:04 +1000460
Neil Brown83db93f2009-09-15 16:05:51 -0700461 spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
Neil Brownf1282c82008-07-16 08:58:04 +1000462
Nick Dyerfc60bb82013-06-07 15:45:13 +0100463 if (!WARN_ON(sysfs_type(sd) != SYSFS_KOBJ_ATTR)) {
464 od = sd->s_attr.open;
465 if (od) {
466 atomic_inc(&od->event);
467 wake_up_interruptible(&od->poll);
468 }
Neil Brownf1282c82008-07-16 08:58:04 +1000469 }
470
Neil Brown83db93f2009-09-15 16:05:51 -0700471 spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
Neil Brownf1282c82008-07-16 08:58:04 +1000472}
473EXPORT_SYMBOL_GPL(sysfs_notify_dirent);
474
Trent Piepho8c0e3992008-09-25 16:45:13 -0700475void sysfs_notify(struct kobject *k, const char *dir, const char *attr)
NeilBrown4508a7a2006-03-20 17:53:53 +1100476{
Tejun Heo51225032007-06-14 04:27:25 +0900477 struct sysfs_dirent *sd = k->sd;
NeilBrown4508a7a2006-03-20 17:53:53 +1100478
Tejun Heo51225032007-06-14 04:27:25 +0900479 mutex_lock(&sysfs_mutex);
NeilBrown4508a7a2006-03-20 17:53:53 +1100480
Tejun Heo51225032007-06-14 04:27:25 +0900481 if (sd && dir)
Tejun Heocfec0bc2013-09-11 22:29:09 -0400482 sd = sysfs_find_dirent(sd, dir, NULL);
Tejun Heo51225032007-06-14 04:27:25 +0900483 if (sd && attr)
Tejun Heocfec0bc2013-09-11 22:29:09 -0400484 sd = sysfs_find_dirent(sd, attr, NULL);
Neil Brownf1282c82008-07-16 08:58:04 +1000485 if (sd)
486 sysfs_notify_dirent(sd);
Tejun Heo51225032007-06-14 04:27:25 +0900487
488 mutex_unlock(&sysfs_mutex);
NeilBrown4508a7a2006-03-20 17:53:53 +1100489}
490EXPORT_SYMBOL_GPL(sysfs_notify);
491
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800492const struct file_operations sysfs_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 .read = sysfs_read_file,
494 .write = sysfs_write_file,
495 .llseek = generic_file_llseek,
496 .open = sysfs_open_file,
497 .release = sysfs_release,
NeilBrown4508a7a2006-03-20 17:53:53 +1100498 .poll = sysfs_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499};
500
Tejun Heo58292cb2013-09-11 22:29:04 -0400501int sysfs_add_file_mode_ns(struct sysfs_dirent *dir_sd,
502 const struct attribute *attr, int type,
503 umode_t amode, const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
James Bottomley0f423892008-03-20 20:47:52 -0500505 umode_t mode = (amode & S_IALLUGO) | S_IFREG;
Tejun Heofb6896d2007-06-14 04:27:24 +0900506 struct sysfs_addrm_cxt acxt;
Tejun Heoa26cd722007-06-14 03:45:14 +0900507 struct sysfs_dirent *sd;
Tejun Heo23dc2792007-08-02 21:38:03 +0900508 int rc;
Tejun Heoa26cd722007-06-14 03:45:14 +0900509
Tejun Heo3e519032007-06-14 03:45:15 +0900510 sd = sysfs_new_dirent(attr->name, mode, type);
Tejun Heo3007e992007-06-14 04:27:23 +0900511 if (!sd)
512 return -ENOMEM;
Eric W. Biederman487505c2011-10-12 21:53:38 +0000513
514 sd->s_ns = ns;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900515 sd->s_attr.attr = (void *)attr;
Eric W. Biedermana2db6842010-02-11 15:20:00 -0800516 sysfs_dirent_init_lockdep(sd);
Tejun Heoa26cd722007-06-14 03:45:14 +0900517
Tejun Heod69ac5a2013-09-18 17:15:35 -0400518 sysfs_addrm_start(&acxt);
519 rc = sysfs_add_one(&acxt, sd, dir_sd);
Tejun Heo23dc2792007-08-02 21:38:03 +0900520 sysfs_addrm_finish(&acxt);
Tejun Heo3007e992007-06-14 04:27:23 +0900521
Tejun Heo23dc2792007-08-02 21:38:03 +0900522 if (rc)
Tejun Heo967e35d2007-07-18 16:38:11 +0900523 sysfs_put(sd);
Tejun Heo3007e992007-06-14 04:27:23 +0900524
Tejun Heo23dc2792007-08-02 21:38:03 +0900525 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526}
527
528
James Bottomley0f423892008-03-20 20:47:52 -0500529int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,
530 int type)
531{
Tejun Heo58292cb2013-09-11 22:29:04 -0400532 return sysfs_add_file_mode_ns(dir_sd, attr, type, attr->mode, NULL);
James Bottomley0f423892008-03-20 20:47:52 -0500533}
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535/**
Tejun Heo58292cb2013-09-11 22:29:04 -0400536 * sysfs_create_file_ns - create an attribute file for an object with custom ns
537 * @kobj: object we're creating for
538 * @attr: attribute descriptor
539 * @ns: namespace the new file should belong to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 */
Tejun Heo58292cb2013-09-11 22:29:04 -0400541int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
542 const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
Tejun Heo608e2662007-06-14 04:27:22 +0900544 BUG_ON(!kobj || !kobj->sd || !attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Tejun Heo58292cb2013-09-11 22:29:04 -0400546 return sysfs_add_file_mode_ns(kobj->sd, attr, SYSFS_KOBJ_ATTR,
547 attr->mode, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549}
Tejun Heo58292cb2013-09-11 22:29:04 -0400550EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Andi Kleen1c205ae2010-01-05 12:48:01 +0100552int sysfs_create_files(struct kobject *kobj, const struct attribute **ptr)
553{
554 int err = 0;
555 int i;
556
557 for (i = 0; ptr[i] && !err; i++)
558 err = sysfs_create_file(kobj, ptr[i]);
559 if (err)
560 while (--i >= 0)
561 sysfs_remove_file(kobj, ptr[i]);
562 return err;
563}
Greg Kroah-Hartman1b866752013-08-21 16:17:47 -0700564EXPORT_SYMBOL_GPL(sysfs_create_files);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566/**
Alan Sterndfa87c82007-02-20 15:02:44 -0500567 * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
568 * @kobj: object we're acting for.
569 * @attr: attribute descriptor.
570 * @group: group name.
571 */
572int sysfs_add_file_to_group(struct kobject *kobj,
573 const struct attribute *attr, const char *group)
574{
Tejun Heo608e2662007-06-14 04:27:22 +0900575 struct sysfs_dirent *dir_sd;
Alan Sterndfa87c82007-02-20 15:02:44 -0500576 int error;
577
James Bottomley11f24fb2008-01-02 18:44:05 -0600578 if (group)
Tejun Heo388975c2013-09-11 23:19:13 -0400579 dir_sd = sysfs_get_dirent(kobj->sd, group);
James Bottomley11f24fb2008-01-02 18:44:05 -0600580 else
581 dir_sd = sysfs_get(kobj->sd);
582
Tejun Heo608e2662007-06-14 04:27:22 +0900583 if (!dir_sd)
584 return -ENOENT;
585
586 error = sysfs_add_file(dir_sd, attr, SYSFS_KOBJ_ATTR);
587 sysfs_put(dir_sd);
588
Alan Sterndfa87c82007-02-20 15:02:44 -0500589 return error;
590}
591EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593/**
Kay Sievers31e5abe2005-04-18 21:57:32 -0700594 * sysfs_chmod_file - update the modified mode value on an object attribute.
595 * @kobj: object we're acting for.
596 * @attr: attribute descriptor.
597 * @mode: file permissions.
598 *
599 */
Jean Delvare49c19402010-07-02 16:54:05 +0200600int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
Al Viro48176a92011-07-24 03:40:40 -0400601 umode_t mode)
Kay Sievers31e5abe2005-04-18 21:57:32 -0700602{
Eric W. Biederman06fc0d62009-11-20 16:08:54 -0800603 struct sysfs_dirent *sd;
Maneesh Sonibc062b12005-07-29 12:13:35 -0700604 struct iattr newattrs;
Tejun Heo51225032007-06-14 04:27:25 +0900605 int rc;
Kay Sievers31e5abe2005-04-18 21:57:32 -0700606
Eric W. Biederman06fc0d62009-11-20 16:08:54 -0800607 mutex_lock(&sysfs_mutex);
608
Tejun Heo51225032007-06-14 04:27:25 +0900609 rc = -ENOENT;
Tejun Heocfec0bc2013-09-11 22:29:09 -0400610 sd = sysfs_find_dirent(kobj->sd, attr->name, NULL);
Eric W. Biederman06fc0d62009-11-20 16:08:54 -0800611 if (!sd)
Tejun Heo51225032007-06-14 04:27:25 +0900612 goto out;
613
Eric W. Biederman06fc0d62009-11-20 16:08:54 -0800614 newattrs.ia_mode = (mode & S_IALLUGO) | (sd->s_mode & ~S_IALLUGO);
Eric W. Biederman4c6974f2009-11-07 23:27:02 -0800615 newattrs.ia_valid = ATTR_MODE;
Eric W. Biederman06fc0d62009-11-20 16:08:54 -0800616 rc = sysfs_sd_setattr(sd, &newattrs);
Tejun Heof88123e2007-09-20 16:05:10 +0900617
Tejun Heo51225032007-06-14 04:27:25 +0900618 out:
Eric W. Biederman06fc0d62009-11-20 16:08:54 -0800619 mutex_unlock(&sysfs_mutex);
Tejun Heo51225032007-06-14 04:27:25 +0900620 return rc;
Kay Sievers31e5abe2005-04-18 21:57:32 -0700621}
622EXPORT_SYMBOL_GPL(sysfs_chmod_file);
623
Kay Sievers31e5abe2005-04-18 21:57:32 -0700624/**
Tejun Heo58292cb2013-09-11 22:29:04 -0400625 * sysfs_remove_file_ns - remove an object attribute with a custom ns tag
626 * @kobj: object we're acting for
627 * @attr: attribute descriptor
628 * @ns: namespace tag of the file to remove
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 *
Tejun Heo58292cb2013-09-11 22:29:04 -0400630 * Hash the attribute name and namespace tag and kill the victim.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 */
Tejun Heo58292cb2013-09-11 22:29:04 -0400632void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
633 const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Tejun Heo58292cb2013-09-11 22:29:04 -0400635 struct sysfs_dirent *dir_sd = kobj->sd;
Eric W. Biederman487505c2011-10-12 21:53:38 +0000636
Tejun Heocfec0bc2013-09-11 22:29:09 -0400637 sysfs_hash_and_remove(dir_sd, attr->name, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638}
Tejun Heo58292cb2013-09-11 22:29:04 -0400639EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Greg Kroah-Hartman1b18dc22013-08-21 16:28:26 -0700641void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr)
Andi Kleen1c205ae2010-01-05 12:48:01 +0100642{
643 int i;
644 for (i = 0; ptr[i]; i++)
645 sysfs_remove_file(kobj, ptr[i]);
646}
Greg Kroah-Hartman1b866752013-08-21 16:17:47 -0700647EXPORT_SYMBOL_GPL(sysfs_remove_files);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Alan Sterndfa87c82007-02-20 15:02:44 -0500649/**
650 * sysfs_remove_file_from_group - remove an attribute file from a group.
651 * @kobj: object we're acting for.
652 * @attr: attribute descriptor.
653 * @group: group name.
654 */
655void sysfs_remove_file_from_group(struct kobject *kobj,
656 const struct attribute *attr, const char *group)
657{
Tejun Heo608e2662007-06-14 04:27:22 +0900658 struct sysfs_dirent *dir_sd;
Alan Sterndfa87c82007-02-20 15:02:44 -0500659
James Bottomley11f24fb2008-01-02 18:44:05 -0600660 if (group)
Tejun Heo388975c2013-09-11 23:19:13 -0400661 dir_sd = sysfs_get_dirent(kobj->sd, group);
James Bottomley11f24fb2008-01-02 18:44:05 -0600662 else
663 dir_sd = sysfs_get(kobj->sd);
Tejun Heo608e2662007-06-14 04:27:22 +0900664 if (dir_sd) {
Tejun Heocfec0bc2013-09-11 22:29:09 -0400665 sysfs_hash_and_remove(dir_sd, attr->name, NULL);
Tejun Heo608e2662007-06-14 04:27:22 +0900666 sysfs_put(dir_sd);
Alan Sterndfa87c82007-02-20 15:02:44 -0500667 }
668}
669EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
670
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400671struct sysfs_schedule_callback_struct {
Alex Chiang66942062009-03-13 12:07:36 -0600672 struct list_head workq_list;
673 struct kobject *kobj;
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400674 void (*func)(void *);
675 void *data;
Alan Stern523ded72007-04-26 00:12:04 -0700676 struct module *owner;
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400677 struct work_struct work;
678};
679
Alex Chiangd1102712009-03-25 15:11:36 -0600680static struct workqueue_struct *sysfs_workqueue;
Alex Chiang66942062009-03-13 12:07:36 -0600681static DEFINE_MUTEX(sysfs_workq_mutex);
682static LIST_HEAD(sysfs_workq);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400683static void sysfs_schedule_callback_work(struct work_struct *work)
684{
685 struct sysfs_schedule_callback_struct *ss = container_of(work,
686 struct sysfs_schedule_callback_struct, work);
687
688 (ss->func)(ss->data);
689 kobject_put(ss->kobj);
Alan Stern523ded72007-04-26 00:12:04 -0700690 module_put(ss->owner);
Alex Chiang66942062009-03-13 12:07:36 -0600691 mutex_lock(&sysfs_workq_mutex);
692 list_del(&ss->workq_list);
693 mutex_unlock(&sysfs_workq_mutex);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400694 kfree(ss);
695}
696
697/**
698 * sysfs_schedule_callback - helper to schedule a callback for a kobject
699 * @kobj: object we're acting for.
700 * @func: callback function to invoke later.
701 * @data: argument to pass to @func.
Alan Stern523ded72007-04-26 00:12:04 -0700702 * @owner: module owning the callback code
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400703 *
704 * sysfs attribute methods must not unregister themselves or their parent
705 * kobject (which would amount to the same thing). Attempts to do so will
706 * deadlock, since unregistration is mutually exclusive with driver
707 * callbacks.
708 *
709 * Instead methods can call this routine, which will attempt to allocate
710 * and schedule a workqueue request to call back @func with @data as its
711 * argument in the workqueue's process context. @kobj will be pinned
712 * until @func returns.
713 *
714 * Returns 0 if the request was submitted, -ENOMEM if storage could not
Alex Chiang66942062009-03-13 12:07:36 -0600715 * be allocated, -ENODEV if a reference to @owner isn't available,
716 * -EAGAIN if a callback has already been scheduled for @kobj.
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400717 */
718int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
Alan Stern523ded72007-04-26 00:12:04 -0700719 void *data, struct module *owner)
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400720{
Alex Chiang66942062009-03-13 12:07:36 -0600721 struct sysfs_schedule_callback_struct *ss, *tmp;
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400722
Alan Stern523ded72007-04-26 00:12:04 -0700723 if (!try_module_get(owner))
724 return -ENODEV;
Alex Chiang66942062009-03-13 12:07:36 -0600725
726 mutex_lock(&sysfs_workq_mutex);
727 list_for_each_entry_safe(ss, tmp, &sysfs_workq, workq_list)
728 if (ss->kobj == kobj) {
Alex Chiangd1102712009-03-25 15:11:36 -0600729 module_put(owner);
Alex Chiang66942062009-03-13 12:07:36 -0600730 mutex_unlock(&sysfs_workq_mutex);
731 return -EAGAIN;
732 }
733 mutex_unlock(&sysfs_workq_mutex);
734
Alex Chiangd1102712009-03-25 15:11:36 -0600735 if (sysfs_workqueue == NULL) {
Andrew Morton086a3772009-05-07 12:36:53 -0700736 sysfs_workqueue = create_singlethread_workqueue("sysfsd");
Alex Chiangd1102712009-03-25 15:11:36 -0600737 if (sysfs_workqueue == NULL) {
738 module_put(owner);
739 return -ENOMEM;
740 }
741 }
742
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400743 ss = kmalloc(sizeof(*ss), GFP_KERNEL);
Alan Stern523ded72007-04-26 00:12:04 -0700744 if (!ss) {
745 module_put(owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400746 return -ENOMEM;
Alan Stern523ded72007-04-26 00:12:04 -0700747 }
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400748 kobject_get(kobj);
749 ss->kobj = kobj;
750 ss->func = func;
751 ss->data = data;
Alan Stern523ded72007-04-26 00:12:04 -0700752 ss->owner = owner;
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400753 INIT_WORK(&ss->work, sysfs_schedule_callback_work);
Alex Chiang66942062009-03-13 12:07:36 -0600754 INIT_LIST_HEAD(&ss->workq_list);
755 mutex_lock(&sysfs_workq_mutex);
756 list_add_tail(&ss->workq_list, &sysfs_workq);
757 mutex_unlock(&sysfs_workq_mutex);
Alex Chiangd1102712009-03-25 15:11:36 -0600758 queue_work(sysfs_workqueue, &ss->work);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400759 return 0;
760}
761EXPORT_SYMBOL_GPL(sysfs_schedule_callback);