blob: 53cc096e6a1bd67230a8b99645c78d89edd88e65 [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 Heobcafe4e2013-10-01 17:42:00 -040048 struct sysfs_dirent *sd;
49 struct file *file;
Tejun Heo73107cb2007-06-14 03:45:16 +090050 size_t count;
Greg Kroah-Hartman1b18dc22013-08-21 16:28:26 -070051 char *page;
Dave Young52e8c202007-07-26 11:03:54 +000052 struct mutex mutex;
Tejun Heo73107cb2007-06-14 03:45:16 +090053 int event;
Tejun Heo85a4ffa2007-09-20 16:05:12 +090054 struct list_head list;
Tejun Heo73107cb2007-06-14 03:45:16 +090055};
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Tejun Heo375b6112013-10-01 17:41:57 -040057/*
58 * Determine ktype->sysfs_ops for the given sysfs_dirent. This function
59 * must be called while holding an active reference.
60 */
61static const struct sysfs_ops *sysfs_file_ops(struct sysfs_dirent *sd)
62{
63 struct kobject *kobj = sd->s_parent->s_dir.kobj;
64
65 lockdep_assert_held(sd);
66 return kobj->ktype ? kobj->ktype->sysfs_ops : NULL;
67}
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069/**
70 * fill_read_buffer - allocate and fill buffer from object.
71 * @dentry: dentry pointer.
72 * @buffer: data buffer for file.
73 *
74 * Allocate @buffer->page, if it hasn't been already, then call the
Greg Kroah-Hartmanab9bf4b2013-08-21 16:21:17 -070075 * kobject's show() method to fill the buffer with this attribute's
76 * data.
Oliver Neukum82244b12007-01-02 08:48:08 +010077 * This is called only once, on the file's first read unless an error
78 * is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 */
Tejun Heo58282d82013-10-01 17:41:59 -040080static int fill_read_buffer(struct dentry *dentry, struct sysfs_open_file *of)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Tejun Heo0ab66082007-06-14 03:45:16 +090082 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
Tejun Heob1fc3d62007-09-20 16:05:11 +090083 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
Tejun Heo375b6112013-10-01 17:41:57 -040084 const struct sysfs_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 int ret = 0;
86 ssize_t count;
87
Tejun Heo58282d82013-10-01 17:41:59 -040088 if (!of->page)
89 of->page = (char *) get_zeroed_page(GFP_KERNEL);
90 if (!of->page)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 return -ENOMEM;
92
Tejun Heo0ab66082007-06-14 03:45:16 +090093 /* need attr_sd for attr and ops, its parent for kobj */
Eric W. Biedermane72ceb82010-02-11 15:18:38 -080094 if (!sysfs_get_active(attr_sd))
Tejun Heo0ab66082007-06-14 03:45:16 +090095 return -ENODEV;
96
Tejun Heo58282d82013-10-01 17:41:59 -040097 of->event = atomic_read(&attr_sd->s_attr.open->event);
Tejun Heo375b6112013-10-01 17:41:57 -040098
99 ops = sysfs_file_ops(attr_sd);
Tejun Heo58282d82013-10-01 17:41:59 -0400100 count = ops->show(kobj, attr_sd->s_attr.attr, of->page);
Tejun Heo0ab66082007-06-14 03:45:16 +0900101
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800102 sysfs_put_active(attr_sd);
Tejun Heo0ab66082007-06-14 03:45:16 +0900103
Miao Xie8118a852007-11-21 14:55:19 -0800104 /*
105 * The code works fine with PAGE_SIZE return but it's likely to
106 * indicate truncated result or overflow in normal use cases.
107 */
Andrew Morton815d2d52008-03-04 15:09:07 -0800108 if (count >= (ssize_t)PAGE_SIZE) {
109 print_symbol("fill_read_buffer: %s returned bad count\n",
110 (unsigned long)ops->show);
111 /* Try to struggle along */
112 count = PAGE_SIZE - 1;
113 }
Tejun Heoaea585e2013-10-01 17:41:56 -0400114 if (count >= 0)
Tejun Heo58282d82013-10-01 17:41:59 -0400115 of->count = count;
Tejun Heoaea585e2013-10-01 17:41:56 -0400116 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 ret = count;
118 return ret;
119}
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121/**
Greg Kroah-Hartmanab9bf4b2013-08-21 16:21:17 -0700122 * sysfs_read_file - read an attribute.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 * @file: file pointer.
124 * @buf: buffer to fill.
125 * @count: number of bytes to read.
126 * @ppos: starting offset in file.
127 *
128 * Userspace wants to read an attribute file. The attribute descriptor
129 * is in the file's ->d_fsdata. The target object is in the directory's
130 * ->d_fsdata.
131 *
132 * We call fill_read_buffer() to allocate and fill the buffer from the
133 * object's show() method exactly once (if the read is happening from
134 * the beginning of the file). That should fill the entire buffer with
135 * all the data the object has to offer for that attribute.
136 * We then call flush_read_buffer() to copy the buffer to userspace
137 * in the increments specified.
138 */
139
140static ssize_t
141sysfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
142{
Tejun Heo58282d82013-10-01 17:41:59 -0400143 struct sysfs_open_file *of = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 ssize_t retval = 0;
145
Tejun Heo58282d82013-10-01 17:41:59 -0400146 mutex_lock(&of->mutex);
Tejun Heoaea585e2013-10-01 17:41:56 -0400147 /*
148 * Fill on zero offset and the first read so that silly things like
149 * "dd bs=1 skip=N" can work on sysfs files.
150 */
Tejun Heo58282d82013-10-01 17:41:59 -0400151 if (*ppos == 0 || !of->page) {
152 retval = fill_read_buffer(file->f_path.dentry, of);
Alan Sterne7b0d262007-03-15 15:51:28 -0400153 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 goto out;
155 }
Zach Brown5c1fdf42006-10-03 01:16:06 -0700156 pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",
Tejun Heo58282d82013-10-01 17:41:59 -0400157 __func__, count, *ppos, of->page);
158 retval = simple_read_from_buffer(buf, count, ppos, of->page, of->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159out:
Tejun Heo58282d82013-10-01 17:41:59 -0400160 mutex_unlock(&of->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 return retval;
162}
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164/**
Tejun Heo8ef445f2013-10-01 17:42:01 -0400165 * flush_write_buffer - push buffer to kobject
166 * @of: open file
167 * @buf: data buffer for file
168 * @count: number of bytes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 *
Tejun Heo8ef445f2013-10-01 17:42:01 -0400170 * Get the correct pointers for the kobject and the attribute we're dealing
171 * with, then call the store() method for it with @buf.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 */
Tejun Heo8ef445f2013-10-01 17:42:01 -0400173static int flush_write_buffer(struct sysfs_open_file *of, char *buf,
174 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
Tejun Heobcafe4e2013-10-01 17:42:00 -0400176 struct kobject *kobj = of->sd->s_parent->s_dir.kobj;
Tejun Heo375b6112013-10-01 17:41:57 -0400177 const struct sysfs_ops *ops;
Tejun Heo8ef445f2013-10-01 17:42:01 -0400178 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Tejun Heo8ef445f2013-10-01 17:42:01 -0400180 /*
181 * Need @of->sd for attr and ops, its parent for kobj. @of->mutex
182 * nests outside active ref and is just to ensure that the ops
183 * aren't called concurrently for the same open file.
184 */
185 mutex_lock(&of->mutex);
186 if (!sysfs_get_active(of->sd)) {
187 mutex_unlock(&of->mutex);
Tejun Heo0ab66082007-06-14 03:45:16 +0900188 return -ENODEV;
Tejun Heo8ef445f2013-10-01 17:42:01 -0400189 }
Tejun Heo0ab66082007-06-14 03:45:16 +0900190
Tejun Heobcafe4e2013-10-01 17:42:00 -0400191 ops = sysfs_file_ops(of->sd);
Tejun Heo8ef445f2013-10-01 17:42:01 -0400192 rc = ops->store(kobj, of->sd->s_attr.attr, buf, count);
Tejun Heo0ab66082007-06-14 03:45:16 +0900193
Tejun Heobcafe4e2013-10-01 17:42:00 -0400194 sysfs_put_active(of->sd);
Tejun Heo8ef445f2013-10-01 17:42:01 -0400195 mutex_unlock(&of->mutex);
Tejun Heo0ab66082007-06-14 03:45:16 +0900196
197 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200/**
Tejun Heo8ef445f2013-10-01 17:42:01 -0400201 * sysfs_write_file - write an attribute
202 * @file: file pointer
203 * @user_buf: data to write
204 * @count: number of bytes
205 * @ppos: starting offset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 *
Tejun Heo8ef445f2013-10-01 17:42:01 -0400207 * Copy data in from userland and pass it to the matching
208 * sysfs_ops->store() by invoking flush_write_buffer().
209 *
210 * There is no easy way for us to know if userspace is only doing a partial
211 * write, so we don't support them. We expect the entire buffer to come on
212 * the first write. Hint: if you're writing a value, first read the file,
213 * modify only the the value you're changing, then write entire buffer
214 * back.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 */
Tejun Heo8ef445f2013-10-01 17:42:01 -0400216static ssize_t sysfs_write_file(struct file *file, const char __user *user_buf,
Greg Kroah-Hartmanddfd6d02013-08-21 16:33:34 -0700217 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Tejun Heo58282d82013-10-01 17:41:59 -0400219 struct sysfs_open_file *of = file->private_data;
Tejun Heo8ef445f2013-10-01 17:42:01 -0400220 ssize_t len = min_t(size_t, count, PAGE_SIZE - 1);
221 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Tejun Heo8ef445f2013-10-01 17:42:01 -0400223 if (!len)
224 return 0;
225
226 buf = kmalloc(len + 1, GFP_KERNEL);
227 if (!buf)
228 return -ENOMEM;
229
230 if (copy_from_user(buf, user_buf, len)) {
231 len = -EFAULT;
232 goto out_free;
233 }
234 buf[len] = '\0'; /* guarantee string termination */
235
236 len = flush_write_buffer(of, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 if (len > 0)
238 *ppos += len;
Tejun Heo8ef445f2013-10-01 17:42:01 -0400239out_free:
240 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return len;
242}
243
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900244/**
245 * sysfs_get_open_dirent - get or create sysfs_open_dirent
246 * @sd: target sysfs_dirent
Tejun Heo58282d82013-10-01 17:41:59 -0400247 * @of: sysfs_open_file for this instance of open
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900248 *
249 * If @sd->s_attr.open exists, increment its reference count;
Tejun Heo58282d82013-10-01 17:41:59 -0400250 * otherwise, create one. @of is chained to the files list.
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900251 *
252 * LOCKING:
253 * Kernel thread context (may sleep).
254 *
255 * RETURNS:
256 * 0 on success, -errno on failure.
257 */
258static int sysfs_get_open_dirent(struct sysfs_dirent *sd,
Tejun Heo58282d82013-10-01 17:41:59 -0400259 struct sysfs_open_file *of)
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900260{
261 struct sysfs_open_dirent *od, *new_od = NULL;
262
263 retry:
Tejun Heoc75ec762013-10-01 17:41:58 -0400264 mutex_lock(&sysfs_open_file_mutex);
Neil Brown83db93f2009-09-15 16:05:51 -0700265 spin_lock_irq(&sysfs_open_dirent_lock);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900266
267 if (!sd->s_attr.open && new_od) {
268 sd->s_attr.open = new_od;
269 new_od = NULL;
270 }
271
272 od = sd->s_attr.open;
273 if (od) {
274 atomic_inc(&od->refcnt);
Tejun Heo58282d82013-10-01 17:41:59 -0400275 list_add_tail(&of->list, &od->files);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900276 }
277
Neil Brown83db93f2009-09-15 16:05:51 -0700278 spin_unlock_irq(&sysfs_open_dirent_lock);
Tejun Heoc75ec762013-10-01 17:41:58 -0400279 mutex_unlock(&sysfs_open_file_mutex);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900280
281 if (od) {
282 kfree(new_od);
283 return 0;
284 }
285
286 /* not there, initialize a new one and retry */
287 new_od = kmalloc(sizeof(*new_od), GFP_KERNEL);
288 if (!new_od)
289 return -ENOMEM;
290
291 atomic_set(&new_od->refcnt, 0);
Tejun Heoa4e8b912007-09-20 16:05:12 +0900292 atomic_set(&new_od->event, 1);
293 init_waitqueue_head(&new_od->poll);
Tejun Heo58282d82013-10-01 17:41:59 -0400294 INIT_LIST_HEAD(&new_od->files);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900295 goto retry;
296}
297
298/**
299 * sysfs_put_open_dirent - put sysfs_open_dirent
300 * @sd: target sysfs_dirent
Tejun Heo58282d82013-10-01 17:41:59 -0400301 * @of: associated sysfs_open_file
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900302 *
Tejun Heo58282d82013-10-01 17:41:59 -0400303 * Put @sd->s_attr.open and unlink @of from the files list. If
304 * reference count reaches zero, disassociate and free it.
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900305 *
306 * LOCKING:
307 * None.
308 */
309static void sysfs_put_open_dirent(struct sysfs_dirent *sd,
Tejun Heo58282d82013-10-01 17:41:59 -0400310 struct sysfs_open_file *of)
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900311{
312 struct sysfs_open_dirent *od = sd->s_attr.open;
Neil Brown83db93f2009-09-15 16:05:51 -0700313 unsigned long flags;
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900314
Tejun Heoc75ec762013-10-01 17:41:58 -0400315 mutex_lock(&sysfs_open_file_mutex);
Neil Brown83db93f2009-09-15 16:05:51 -0700316 spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900317
Tejun Heo58282d82013-10-01 17:41:59 -0400318 list_del(&of->list);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900319 if (atomic_dec_and_test(&od->refcnt))
320 sd->s_attr.open = NULL;
321 else
322 od = NULL;
323
Neil Brown83db93f2009-09-15 16:05:51 -0700324 spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
Tejun Heoc75ec762013-10-01 17:41:58 -0400325 mutex_unlock(&sysfs_open_file_mutex);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900326
327 kfree(od);
328}
329
Oliver Neukum94bebf42006-12-20 10:52:44 +0100330static int sysfs_open_file(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Tejun Heo3e519032007-06-14 03:45:15 +0900332 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900333 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
Tejun Heo58282d82013-10-01 17:41:59 -0400334 struct sysfs_open_file *of;
Emese Revfy52cf25d2010-01-19 02:58:23 +0100335 const struct sysfs_ops *ops;
Kay Sievers000f2a42007-11-02 13:47:53 +0100336 int error = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Tejun Heo0ab66082007-06-14 03:45:16 +0900338 /* need attr_sd for attr and ops, its parent for kobj */
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800339 if (!sysfs_get_active(attr_sd))
Tejun Heo0ab66082007-06-14 03:45:16 +0900340 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Kay Sievers000f2a42007-11-02 13:47:53 +0100342 /* every kobject with an attribute needs a ktype assigned */
Tejun Heo375b6112013-10-01 17:41:57 -0400343 ops = sysfs_file_ops(attr_sd);
344 if (WARN(!ops, KERN_ERR
345 "missing sysfs attribute operations for kobject: %s\n",
346 kobject_name(kobj)))
Tejun Heo7b595752007-06-14 03:45:17 +0900347 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 /* File needs write support.
Greg Kroah-Hartmanab9bf4b2013-08-21 16:21:17 -0700350 * The inode's perms must say it's ok,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 * and we must have a store method.
352 */
353 if (file->f_mode & FMODE_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (!(inode->i_mode & S_IWUGO) || !ops->store)
Tejun Heo7b595752007-06-14 03:45:17 +0900355 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
357
358 /* File needs read support.
359 * The inode's perms must say it's ok, and we there
360 * must be a show method for it.
361 */
362 if (file->f_mode & FMODE_READ) {
363 if (!(inode->i_mode & S_IRUGO) || !ops->show)
Tejun Heo7b595752007-06-14 03:45:17 +0900364 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 }
366
Tejun Heo58282d82013-10-01 17:41:59 -0400367 /*
368 * No error? Great, allocate a sysfs_open_file for the file, and
369 * store it it in file->private_data for easy access.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 */
Tejun Heo0ab66082007-06-14 03:45:16 +0900371 error = -ENOMEM;
Tejun Heo58282d82013-10-01 17:41:59 -0400372 of = kzalloc(sizeof(struct sysfs_open_file), GFP_KERNEL);
373 if (!of)
Tejun Heo7b595752007-06-14 03:45:17 +0900374 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Tejun Heo58282d82013-10-01 17:41:59 -0400376 mutex_init(&of->mutex);
Tejun Heobcafe4e2013-10-01 17:42:00 -0400377 of->sd = attr_sd;
378 of->file = file;
Tejun Heo58282d82013-10-01 17:41:59 -0400379 file->private_data = of;
Tejun Heo0ab66082007-06-14 03:45:16 +0900380
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900381 /* make sure we have open dirent struct */
Tejun Heo58282d82013-10-01 17:41:59 -0400382 error = sysfs_get_open_dirent(attr_sd, of);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900383 if (error)
384 goto err_free;
385
Tejun Heob05f0542007-09-20 16:05:10 +0900386 /* open succeeded, put active references */
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800387 sysfs_put_active(attr_sd);
Tejun Heo0ab66082007-06-14 03:45:16 +0900388 return 0;
389
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900390 err_free:
Tejun Heo58282d82013-10-01 17:41:59 -0400391 kfree(of);
Tejun Heo7b595752007-06-14 03:45:17 +0900392 err_out:
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800393 sysfs_put_active(attr_sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 return error;
395}
396
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900397static int sysfs_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900399 struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata;
Tejun Heo58282d82013-10-01 17:41:59 -0400400 struct sysfs_open_file *of = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Tejun Heo58282d82013-10-01 17:41:59 -0400402 sysfs_put_open_dirent(sd, of);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900403
Tejun Heo58282d82013-10-01 17:41:59 -0400404 if (of->page)
405 free_page((unsigned long)of->page);
406 kfree(of);
Tejun Heo50ab1a72007-09-20 16:05:10 +0900407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return 0;
409}
410
NeilBrown4508a7a2006-03-20 17:53:53 +1100411/* Sysfs attribute files are pollable. The idea is that you read
412 * the content and then you use 'poll' or 'select' to wait for
413 * the content to change. When the content changes (assuming the
414 * manager for the kobject supports notification), poll will
415 * return POLLERR|POLLPRI, and select will return the fd whether
416 * it is waiting for read, write, or exceptions.
417 * Once poll/select indicates that the value has changed, you
Dan Williams2424b5d2008-04-07 15:35:01 -0700418 * need to close and re-open the file, or seek to 0 and read again.
NeilBrown4508a7a2006-03-20 17:53:53 +1100419 * Reminder: this only works for attributes which actively support
420 * it, and it is not possible to test an attribute from userspace
Rolf Eike Beera93720e2007-08-10 13:51:07 -0700421 * to see if it supports poll (Neither 'poll' nor 'select' return
NeilBrown4508a7a2006-03-20 17:53:53 +1100422 * an appropriate error code). When in doubt, set a suitable timeout value.
423 */
424static unsigned int sysfs_poll(struct file *filp, poll_table *wait)
425{
Tejun Heo58282d82013-10-01 17:41:59 -0400426 struct sysfs_open_file *of = filp->private_data;
Tejun Heo0ab66082007-06-14 03:45:16 +0900427 struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata;
Tejun Heoa4e8b912007-09-20 16:05:12 +0900428 struct sysfs_open_dirent *od = attr_sd->s_attr.open;
Tejun Heo0ab66082007-06-14 03:45:16 +0900429
430 /* need parent for the kobj, grab both */
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800431 if (!sysfs_get_active(attr_sd))
Tejun Heo0ab66082007-06-14 03:45:16 +0900432 goto trigger;
NeilBrown4508a7a2006-03-20 17:53:53 +1100433
Tejun Heoa4e8b912007-09-20 16:05:12 +0900434 poll_wait(filp, &od->poll, wait);
NeilBrown4508a7a2006-03-20 17:53:53 +1100435
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800436 sysfs_put_active(attr_sd);
NeilBrown4508a7a2006-03-20 17:53:53 +1100437
Tejun Heo58282d82013-10-01 17:41:59 -0400438 if (of->event != atomic_read(&od->event))
Tejun Heo0ab66082007-06-14 03:45:16 +0900439 goto trigger;
440
KOSAKI Motohiro1af35572009-04-09 13:53:22 +0900441 return DEFAULT_POLLMASK;
Tejun Heo0ab66082007-06-14 03:45:16 +0900442
443 trigger:
KOSAKI Motohiro1af35572009-04-09 13:53:22 +0900444 return DEFAULT_POLLMASK|POLLERR|POLLPRI;
NeilBrown4508a7a2006-03-20 17:53:53 +1100445}
446
Neil Brownf1282c82008-07-16 08:58:04 +1000447void sysfs_notify_dirent(struct sysfs_dirent *sd)
448{
449 struct sysfs_open_dirent *od;
Neil Brown83db93f2009-09-15 16:05:51 -0700450 unsigned long flags;
Neil Brownf1282c82008-07-16 08:58:04 +1000451
Neil Brown83db93f2009-09-15 16:05:51 -0700452 spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
Neil Brownf1282c82008-07-16 08:58:04 +1000453
Nick Dyerfc60bb82013-06-07 15:45:13 +0100454 if (!WARN_ON(sysfs_type(sd) != SYSFS_KOBJ_ATTR)) {
455 od = sd->s_attr.open;
456 if (od) {
457 atomic_inc(&od->event);
458 wake_up_interruptible(&od->poll);
459 }
Neil Brownf1282c82008-07-16 08:58:04 +1000460 }
461
Neil Brown83db93f2009-09-15 16:05:51 -0700462 spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
Neil Brownf1282c82008-07-16 08:58:04 +1000463}
464EXPORT_SYMBOL_GPL(sysfs_notify_dirent);
465
Trent Piepho8c0e3992008-09-25 16:45:13 -0700466void sysfs_notify(struct kobject *k, const char *dir, const char *attr)
NeilBrown4508a7a2006-03-20 17:53:53 +1100467{
Tejun Heo51225032007-06-14 04:27:25 +0900468 struct sysfs_dirent *sd = k->sd;
NeilBrown4508a7a2006-03-20 17:53:53 +1100469
Tejun Heo51225032007-06-14 04:27:25 +0900470 mutex_lock(&sysfs_mutex);
NeilBrown4508a7a2006-03-20 17:53:53 +1100471
Tejun Heo51225032007-06-14 04:27:25 +0900472 if (sd && dir)
Tejun Heocfec0bc2013-09-11 22:29:09 -0400473 sd = sysfs_find_dirent(sd, dir, NULL);
Tejun Heo51225032007-06-14 04:27:25 +0900474 if (sd && attr)
Tejun Heocfec0bc2013-09-11 22:29:09 -0400475 sd = sysfs_find_dirent(sd, attr, NULL);
Neil Brownf1282c82008-07-16 08:58:04 +1000476 if (sd)
477 sysfs_notify_dirent(sd);
Tejun Heo51225032007-06-14 04:27:25 +0900478
479 mutex_unlock(&sysfs_mutex);
NeilBrown4508a7a2006-03-20 17:53:53 +1100480}
481EXPORT_SYMBOL_GPL(sysfs_notify);
482
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800483const struct file_operations sysfs_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 .read = sysfs_read_file,
485 .write = sysfs_write_file,
486 .llseek = generic_file_llseek,
487 .open = sysfs_open_file,
488 .release = sysfs_release,
NeilBrown4508a7a2006-03-20 17:53:53 +1100489 .poll = sysfs_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490};
491
Tejun Heo58292cbe2013-09-11 22:29:04 -0400492int sysfs_add_file_mode_ns(struct sysfs_dirent *dir_sd,
493 const struct attribute *attr, int type,
494 umode_t amode, const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
James Bottomley0f423892008-03-20 20:47:52 -0500496 umode_t mode = (amode & S_IALLUGO) | S_IFREG;
Tejun Heofb6896d2007-06-14 04:27:24 +0900497 struct sysfs_addrm_cxt acxt;
Tejun Heoa26cd722007-06-14 03:45:14 +0900498 struct sysfs_dirent *sd;
Tejun Heo23dc2792007-08-02 21:38:03 +0900499 int rc;
Tejun Heoa26cd722007-06-14 03:45:14 +0900500
Tejun Heo3e519032007-06-14 03:45:15 +0900501 sd = sysfs_new_dirent(attr->name, mode, type);
Tejun Heo3007e992007-06-14 04:27:23 +0900502 if (!sd)
503 return -ENOMEM;
Eric W. Biederman487505c2011-10-12 21:53:38 +0000504
505 sd->s_ns = ns;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900506 sd->s_attr.attr = (void *)attr;
Eric W. Biedermana2db6842010-02-11 15:20:00 -0800507 sysfs_dirent_init_lockdep(sd);
Tejun Heoa26cd722007-06-14 03:45:14 +0900508
Tejun Heod69ac5a2013-09-18 17:15:35 -0400509 sysfs_addrm_start(&acxt);
510 rc = sysfs_add_one(&acxt, sd, dir_sd);
Tejun Heo23dc2792007-08-02 21:38:03 +0900511 sysfs_addrm_finish(&acxt);
Tejun Heo3007e992007-06-14 04:27:23 +0900512
Tejun Heo23dc2792007-08-02 21:38:03 +0900513 if (rc)
Tejun Heo967e35d2007-07-18 16:38:11 +0900514 sysfs_put(sd);
Tejun Heo3007e992007-06-14 04:27:23 +0900515
Tejun Heo23dc2792007-08-02 21:38:03 +0900516 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517}
518
519
James Bottomley0f423892008-03-20 20:47:52 -0500520int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,
521 int type)
522{
Tejun Heo58292cbe2013-09-11 22:29:04 -0400523 return sysfs_add_file_mode_ns(dir_sd, attr, type, attr->mode, NULL);
James Bottomley0f423892008-03-20 20:47:52 -0500524}
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526/**
Tejun Heo58292cbe2013-09-11 22:29:04 -0400527 * sysfs_create_file_ns - create an attribute file for an object with custom ns
528 * @kobj: object we're creating for
529 * @attr: attribute descriptor
530 * @ns: namespace the new file should belong to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 */
Tejun Heo58292cbe2013-09-11 22:29:04 -0400532int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
533 const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
Tejun Heo608e2662007-06-14 04:27:22 +0900535 BUG_ON(!kobj || !kobj->sd || !attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Tejun Heo58292cbe2013-09-11 22:29:04 -0400537 return sysfs_add_file_mode_ns(kobj->sd, attr, SYSFS_KOBJ_ATTR,
538 attr->mode, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540}
Tejun Heo58292cbe2013-09-11 22:29:04 -0400541EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Andi Kleen1c205ae2010-01-05 12:48:01 +0100543int sysfs_create_files(struct kobject *kobj, const struct attribute **ptr)
544{
545 int err = 0;
546 int i;
547
548 for (i = 0; ptr[i] && !err; i++)
549 err = sysfs_create_file(kobj, ptr[i]);
550 if (err)
551 while (--i >= 0)
552 sysfs_remove_file(kobj, ptr[i]);
553 return err;
554}
Greg Kroah-Hartman1b866752013-08-21 16:17:47 -0700555EXPORT_SYMBOL_GPL(sysfs_create_files);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557/**
Alan Sterndfa87c82007-02-20 15:02:44 -0500558 * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
559 * @kobj: object we're acting for.
560 * @attr: attribute descriptor.
561 * @group: group name.
562 */
563int sysfs_add_file_to_group(struct kobject *kobj,
564 const struct attribute *attr, const char *group)
565{
Tejun Heo608e2662007-06-14 04:27:22 +0900566 struct sysfs_dirent *dir_sd;
Alan Sterndfa87c82007-02-20 15:02:44 -0500567 int error;
568
James Bottomley11f24fb2008-01-02 18:44:05 -0600569 if (group)
Tejun Heo388975c2013-09-11 23:19:13 -0400570 dir_sd = sysfs_get_dirent(kobj->sd, group);
James Bottomley11f24fb2008-01-02 18:44:05 -0600571 else
572 dir_sd = sysfs_get(kobj->sd);
573
Tejun Heo608e2662007-06-14 04:27:22 +0900574 if (!dir_sd)
575 return -ENOENT;
576
577 error = sysfs_add_file(dir_sd, attr, SYSFS_KOBJ_ATTR);
578 sysfs_put(dir_sd);
579
Alan Sterndfa87c82007-02-20 15:02:44 -0500580 return error;
581}
582EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584/**
Kay Sievers31e5abe2005-04-18 21:57:32 -0700585 * sysfs_chmod_file - update the modified mode value on an object attribute.
586 * @kobj: object we're acting for.
587 * @attr: attribute descriptor.
588 * @mode: file permissions.
589 *
590 */
Jean Delvare49c19402010-07-02 16:54:05 +0200591int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
Al Viro48176a92011-07-24 03:40:40 -0400592 umode_t mode)
Kay Sievers31e5abe2005-04-18 21:57:32 -0700593{
Eric W. Biederman06fc0d62009-11-20 16:08:54 -0800594 struct sysfs_dirent *sd;
Maneesh Sonibc062b12005-07-29 12:13:35 -0700595 struct iattr newattrs;
Tejun Heo51225032007-06-14 04:27:25 +0900596 int rc;
Kay Sievers31e5abe2005-04-18 21:57:32 -0700597
Eric W. Biederman06fc0d62009-11-20 16:08:54 -0800598 mutex_lock(&sysfs_mutex);
599
Tejun Heo51225032007-06-14 04:27:25 +0900600 rc = -ENOENT;
Tejun Heocfec0bc2013-09-11 22:29:09 -0400601 sd = sysfs_find_dirent(kobj->sd, attr->name, NULL);
Eric W. Biederman06fc0d62009-11-20 16:08:54 -0800602 if (!sd)
Tejun Heo51225032007-06-14 04:27:25 +0900603 goto out;
604
Eric W. Biederman06fc0d62009-11-20 16:08:54 -0800605 newattrs.ia_mode = (mode & S_IALLUGO) | (sd->s_mode & ~S_IALLUGO);
Eric W. Biederman4c6974f2009-11-07 23:27:02 -0800606 newattrs.ia_valid = ATTR_MODE;
Eric W. Biederman06fc0d62009-11-20 16:08:54 -0800607 rc = sysfs_sd_setattr(sd, &newattrs);
Tejun Heof88123e2007-09-20 16:05:10 +0900608
Tejun Heo51225032007-06-14 04:27:25 +0900609 out:
Eric W. Biederman06fc0d62009-11-20 16:08:54 -0800610 mutex_unlock(&sysfs_mutex);
Tejun Heo51225032007-06-14 04:27:25 +0900611 return rc;
Kay Sievers31e5abe2005-04-18 21:57:32 -0700612}
613EXPORT_SYMBOL_GPL(sysfs_chmod_file);
614
Kay Sievers31e5abe2005-04-18 21:57:32 -0700615/**
Tejun Heo58292cbe2013-09-11 22:29:04 -0400616 * sysfs_remove_file_ns - remove an object attribute with a custom ns tag
617 * @kobj: object we're acting for
618 * @attr: attribute descriptor
619 * @ns: namespace tag of the file to remove
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 *
Tejun Heo58292cbe2013-09-11 22:29:04 -0400621 * Hash the attribute name and namespace tag and kill the victim.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 */
Tejun Heo58292cbe2013-09-11 22:29:04 -0400623void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
624 const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Tejun Heo58292cbe2013-09-11 22:29:04 -0400626 struct sysfs_dirent *dir_sd = kobj->sd;
Eric W. Biederman487505c2011-10-12 21:53:38 +0000627
Tejun Heocfec0bc2013-09-11 22:29:09 -0400628 sysfs_hash_and_remove(dir_sd, attr->name, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
Tejun Heo58292cbe2013-09-11 22:29:04 -0400630EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Greg Kroah-Hartman1b18dc22013-08-21 16:28:26 -0700632void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr)
Andi Kleen1c205ae2010-01-05 12:48:01 +0100633{
634 int i;
635 for (i = 0; ptr[i]; i++)
636 sysfs_remove_file(kobj, ptr[i]);
637}
Greg Kroah-Hartman1b866752013-08-21 16:17:47 -0700638EXPORT_SYMBOL_GPL(sysfs_remove_files);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Alan Sterndfa87c82007-02-20 15:02:44 -0500640/**
641 * sysfs_remove_file_from_group - remove an attribute file from a group.
642 * @kobj: object we're acting for.
643 * @attr: attribute descriptor.
644 * @group: group name.
645 */
646void sysfs_remove_file_from_group(struct kobject *kobj,
647 const struct attribute *attr, const char *group)
648{
Tejun Heo608e2662007-06-14 04:27:22 +0900649 struct sysfs_dirent *dir_sd;
Alan Sterndfa87c82007-02-20 15:02:44 -0500650
James Bottomley11f24fb2008-01-02 18:44:05 -0600651 if (group)
Tejun Heo388975c2013-09-11 23:19:13 -0400652 dir_sd = sysfs_get_dirent(kobj->sd, group);
James Bottomley11f24fb2008-01-02 18:44:05 -0600653 else
654 dir_sd = sysfs_get(kobj->sd);
Tejun Heo608e2662007-06-14 04:27:22 +0900655 if (dir_sd) {
Tejun Heocfec0bc2013-09-11 22:29:09 -0400656 sysfs_hash_and_remove(dir_sd, attr->name, NULL);
Tejun Heo608e2662007-06-14 04:27:22 +0900657 sysfs_put(dir_sd);
Alan Sterndfa87c82007-02-20 15:02:44 -0500658 }
659}
660EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
661
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400662struct sysfs_schedule_callback_struct {
Alex Chiang66942062009-03-13 12:07:36 -0600663 struct list_head workq_list;
664 struct kobject *kobj;
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400665 void (*func)(void *);
666 void *data;
Alan Stern523ded72007-04-26 00:12:04 -0700667 struct module *owner;
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400668 struct work_struct work;
669};
670
Alex Chiangd1102712009-03-25 15:11:36 -0600671static struct workqueue_struct *sysfs_workqueue;
Alex Chiang66942062009-03-13 12:07:36 -0600672static DEFINE_MUTEX(sysfs_workq_mutex);
673static LIST_HEAD(sysfs_workq);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400674static void sysfs_schedule_callback_work(struct work_struct *work)
675{
676 struct sysfs_schedule_callback_struct *ss = container_of(work,
677 struct sysfs_schedule_callback_struct, work);
678
679 (ss->func)(ss->data);
680 kobject_put(ss->kobj);
Alan Stern523ded72007-04-26 00:12:04 -0700681 module_put(ss->owner);
Alex Chiang66942062009-03-13 12:07:36 -0600682 mutex_lock(&sysfs_workq_mutex);
683 list_del(&ss->workq_list);
684 mutex_unlock(&sysfs_workq_mutex);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400685 kfree(ss);
686}
687
688/**
689 * sysfs_schedule_callback - helper to schedule a callback for a kobject
690 * @kobj: object we're acting for.
691 * @func: callback function to invoke later.
692 * @data: argument to pass to @func.
Alan Stern523ded72007-04-26 00:12:04 -0700693 * @owner: module owning the callback code
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400694 *
695 * sysfs attribute methods must not unregister themselves or their parent
696 * kobject (which would amount to the same thing). Attempts to do so will
697 * deadlock, since unregistration is mutually exclusive with driver
698 * callbacks.
699 *
700 * Instead methods can call this routine, which will attempt to allocate
701 * and schedule a workqueue request to call back @func with @data as its
702 * argument in the workqueue's process context. @kobj will be pinned
703 * until @func returns.
704 *
705 * Returns 0 if the request was submitted, -ENOMEM if storage could not
Alex Chiang66942062009-03-13 12:07:36 -0600706 * be allocated, -ENODEV if a reference to @owner isn't available,
707 * -EAGAIN if a callback has already been scheduled for @kobj.
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400708 */
709int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
Alan Stern523ded72007-04-26 00:12:04 -0700710 void *data, struct module *owner)
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400711{
Alex Chiang66942062009-03-13 12:07:36 -0600712 struct sysfs_schedule_callback_struct *ss, *tmp;
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400713
Alan Stern523ded72007-04-26 00:12:04 -0700714 if (!try_module_get(owner))
715 return -ENODEV;
Alex Chiang66942062009-03-13 12:07:36 -0600716
717 mutex_lock(&sysfs_workq_mutex);
718 list_for_each_entry_safe(ss, tmp, &sysfs_workq, workq_list)
719 if (ss->kobj == kobj) {
Alex Chiangd1102712009-03-25 15:11:36 -0600720 module_put(owner);
Alex Chiang66942062009-03-13 12:07:36 -0600721 mutex_unlock(&sysfs_workq_mutex);
722 return -EAGAIN;
723 }
724 mutex_unlock(&sysfs_workq_mutex);
725
Alex Chiangd1102712009-03-25 15:11:36 -0600726 if (sysfs_workqueue == NULL) {
Andrew Morton086a3772009-05-07 12:36:53 -0700727 sysfs_workqueue = create_singlethread_workqueue("sysfsd");
Alex Chiangd1102712009-03-25 15:11:36 -0600728 if (sysfs_workqueue == NULL) {
729 module_put(owner);
730 return -ENOMEM;
731 }
732 }
733
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400734 ss = kmalloc(sizeof(*ss), GFP_KERNEL);
Alan Stern523ded72007-04-26 00:12:04 -0700735 if (!ss) {
736 module_put(owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400737 return -ENOMEM;
Alan Stern523ded72007-04-26 00:12:04 -0700738 }
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400739 kobject_get(kobj);
740 ss->kobj = kobj;
741 ss->func = func;
742 ss->data = data;
Alan Stern523ded72007-04-26 00:12:04 -0700743 ss->owner = owner;
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400744 INIT_WORK(&ss->work, sysfs_schedule_callback_work);
Alex Chiang66942062009-03-13 12:07:36 -0600745 INIT_LIST_HEAD(&ss->workq_list);
746 mutex_lock(&sysfs_workq_mutex);
747 list_add_tail(&ss->workq_list, &sysfs_workq);
748 mutex_unlock(&sysfs_workq_mutex);
Alex Chiangd1102712009-03-25 15:11:36 -0600749 queue_work(sysfs_workqueue, &ss->work);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400750 return 0;
751}
752EXPORT_SYMBOL_GPL(sysfs_schedule_callback);