blob: 9ba492a3d932db914ef886d06a8b0e45d25b8fa4 [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>
Tejun Heo13c589d2013-10-01 17:42:02 -040024#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include "sysfs.h"
27
Tejun Heo85a4ffa2007-09-20 16:05:12 +090028/*
Tejun Heo58282d82013-10-01 17:41:59 -040029 * There's one sysfs_open_file for each open file and one sysfs_open_dirent
Tejun Heoc75ec762013-10-01 17:41:58 -040030 * for each sysfs_dirent with one or more open files.
Tejun Heo85a4ffa2007-09-20 16:05:12 +090031 *
Tejun Heoc75ec762013-10-01 17:41:58 -040032 * sysfs_dirent->s_attr.open points to sysfs_open_dirent. s_attr.open is
33 * protected by sysfs_open_dirent_lock.
34 *
Tejun Heo13c589d2013-10-01 17:42:02 -040035 * filp->private_data points to seq_file whose ->private points to
36 * sysfs_open_file. sysfs_open_files are chained at
Tejun Heo58282d82013-10-01 17:41:59 -040037 * sysfs_open_dirent->files, which is protected by sysfs_open_file_mutex.
Tejun Heo85a4ffa2007-09-20 16:05:12 +090038 */
Jiri Slabyd7b37882007-11-21 14:55:19 -080039static DEFINE_SPINLOCK(sysfs_open_dirent_lock);
Tejun Heoc75ec762013-10-01 17:41:58 -040040static DEFINE_MUTEX(sysfs_open_file_mutex);
Tejun Heo85a4ffa2007-09-20 16:05:12 +090041
42struct sysfs_open_dirent {
43 atomic_t refcnt;
Tejun Heoa4e8b912007-09-20 16:05:12 +090044 atomic_t event;
45 wait_queue_head_t poll;
Tejun Heo58282d82013-10-01 17:41:59 -040046 struct list_head files; /* goes through sysfs_open_file.list */
Tejun Heo85a4ffa2007-09-20 16:05:12 +090047};
48
Tejun Heo58282d82013-10-01 17:41:59 -040049struct sysfs_open_file {
Tejun Heobcafe4e2013-10-01 17:42:00 -040050 struct sysfs_dirent *sd;
51 struct file *file;
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 Heof9b9a622013-10-01 17:42:05 -040057static bool sysfs_is_bin(struct sysfs_dirent *sd)
58{
59 return sysfs_type(sd) == SYSFS_KOBJ_BIN_ATTR;
60}
61
Tejun Heo13c589d2013-10-01 17:42:02 -040062static struct sysfs_open_file *sysfs_of(struct file *file)
63{
64 return ((struct seq_file *)file->private_data)->private;
65}
66
Tejun Heo375b6112013-10-01 17:41:57 -040067/*
68 * Determine ktype->sysfs_ops for the given sysfs_dirent. This function
69 * must be called while holding an active reference.
70 */
71static const struct sysfs_ops *sysfs_file_ops(struct sysfs_dirent *sd)
72{
73 struct kobject *kobj = sd->s_parent->s_dir.kobj;
74
75 lockdep_assert_held(sd);
76 return kobj->ktype ? kobj->ktype->sysfs_ops : NULL;
77}
78
Tejun Heo13c589d2013-10-01 17:42:02 -040079/*
80 * Reads on sysfs are handled through seq_file, which takes care of hairy
81 * details like buffering and seeking. The following function pipes
82 * sysfs_ops->show() result through seq_file.
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 */
Tejun Heo13c589d2013-10-01 17:42:02 -040084static int sysfs_seq_show(struct seq_file *sf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Tejun Heo13c589d2013-10-01 17:42:02 -040086 struct sysfs_open_file *of = sf->private;
87 struct kobject *kobj = of->sd->s_parent->s_dir.kobj;
Tejun Heo375b6112013-10-01 17:41:57 -040088 const struct sysfs_ops *ops;
Tejun Heo13c589d2013-10-01 17:42:02 -040089 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 ssize_t count;
91
Tejun Heo13c589d2013-10-01 17:42:02 -040092 /* acquire buffer and ensure that it's >= PAGE_SIZE */
93 count = seq_get_buf(sf, &buf);
94 if (count < PAGE_SIZE) {
95 seq_commit(sf, -1);
96 return 0;
97 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Tejun Heo13c589d2013-10-01 17:42:02 -040099 /*
100 * Need @of->sd for attr and ops, its parent for kobj. @of->mutex
101 * nests outside active ref and is just to ensure that the ops
102 * aren't called concurrently for the same open file.
103 */
104 mutex_lock(&of->mutex);
105 if (!sysfs_get_active(of->sd)) {
106 mutex_unlock(&of->mutex);
Tejun Heo0ab66082007-06-14 03:45:16 +0900107 return -ENODEV;
Tejun Heo13c589d2013-10-01 17:42:02 -0400108 }
Tejun Heo0ab66082007-06-14 03:45:16 +0900109
Tejun Heo13c589d2013-10-01 17:42:02 -0400110 of->event = atomic_read(&of->sd->s_attr.open->event);
Tejun Heo375b6112013-10-01 17:41:57 -0400111
Tejun Heo13c589d2013-10-01 17:42:02 -0400112 /*
113 * Lookup @ops and invoke show(). Control may reach here via seq
114 * file lseek even if @ops->show() isn't implemented.
115 */
116 ops = sysfs_file_ops(of->sd);
117 if (ops->show)
118 count = ops->show(kobj, of->sd->s_attr.attr, buf);
119 else
120 count = 0;
Tejun Heo0ab66082007-06-14 03:45:16 +0900121
Tejun Heo13c589d2013-10-01 17:42:02 -0400122 sysfs_put_active(of->sd);
123 mutex_unlock(&of->mutex);
124
125 if (count < 0)
126 return count;
Tejun Heo0ab66082007-06-14 03:45:16 +0900127
Miao Xie8118a852007-11-21 14:55:19 -0800128 /*
129 * The code works fine with PAGE_SIZE return but it's likely to
130 * indicate truncated result or overflow in normal use cases.
131 */
Andrew Morton815d2d52008-03-04 15:09:07 -0800132 if (count >= (ssize_t)PAGE_SIZE) {
133 print_symbol("fill_read_buffer: %s returned bad count\n",
134 (unsigned long)ops->show);
135 /* Try to struggle along */
136 count = PAGE_SIZE - 1;
137 }
Tejun Heo13c589d2013-10-01 17:42:02 -0400138 seq_commit(sf, count);
139 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400142/*
143 * Read method for bin files. As reading a bin file can have side-effects,
144 * the exact offset and bytes specified in read(2) call should be passed to
145 * the read callback making it difficult to use seq_file. Implement
146 * simplistic custom buffering for bin files.
147 */
148static ssize_t sysfs_bin_read(struct file *file, char __user *userbuf,
149 size_t bytes, loff_t *off)
150{
151 struct sysfs_open_file *of = sysfs_of(file);
152 struct bin_attribute *battr = of->sd->s_bin_attr.bin_attr;
153 struct kobject *kobj = of->sd->s_parent->s_dir.kobj;
154 int size = file_inode(file)->i_size;
155 int count = min_t(size_t, bytes, PAGE_SIZE);
156 loff_t offs = *off;
157 char *buf;
158
159 if (!bytes)
160 return 0;
161
162 if (size) {
163 if (offs > size)
164 return 0;
165 if (offs + count > size)
166 count = size - offs;
167 }
168
169 buf = kmalloc(count, GFP_KERNEL);
170 if (!buf)
171 return -ENOMEM;
172
173 /* need of->sd for battr, its parent for kobj */
174 mutex_lock(&of->mutex);
175 if (!sysfs_get_active(of->sd)) {
176 count = -ENODEV;
177 mutex_unlock(&of->mutex);
178 goto out_free;
179 }
180
181 if (battr->read)
182 count = battr->read(file, kobj, battr, buf, offs, count);
183 else
184 count = -EIO;
185
186 sysfs_put_active(of->sd);
187 mutex_unlock(&of->mutex);
188
189 if (count < 0)
190 goto out_free;
191
192 if (copy_to_user(userbuf, buf, count)) {
193 count = -EFAULT;
194 goto out_free;
195 }
196
197 pr_debug("offs = %lld, *off = %lld, count = %d\n", offs, *off, count);
198
199 *off = offs + count;
200
201 out_free:
202 kfree(buf);
203 return count;
204}
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206/**
Tejun Heo8ef445f2013-10-01 17:42:01 -0400207 * flush_write_buffer - push buffer to kobject
208 * @of: open file
209 * @buf: data buffer for file
Tejun Heof9b9a622013-10-01 17:42:05 -0400210 * @off: file offset to write to
Tejun Heo8ef445f2013-10-01 17:42:01 -0400211 * @count: number of bytes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 *
Tejun Heo8ef445f2013-10-01 17:42:01 -0400213 * Get the correct pointers for the kobject and the attribute we're dealing
214 * with, then call the store() method for it with @buf.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 */
Tejun Heof9b9a622013-10-01 17:42:05 -0400216static int flush_write_buffer(struct sysfs_open_file *of, char *buf, loff_t off,
Tejun Heo8ef445f2013-10-01 17:42:01 -0400217 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Tejun Heobcafe4e2013-10-01 17:42:00 -0400219 struct kobject *kobj = of->sd->s_parent->s_dir.kobj;
Tejun Heo8ef445f2013-10-01 17:42:01 -0400220 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Tejun Heo8ef445f2013-10-01 17:42:01 -0400222 /*
223 * Need @of->sd for attr and ops, its parent for kobj. @of->mutex
224 * nests outside active ref and is just to ensure that the ops
225 * aren't called concurrently for the same open file.
226 */
227 mutex_lock(&of->mutex);
228 if (!sysfs_get_active(of->sd)) {
229 mutex_unlock(&of->mutex);
Tejun Heo0ab66082007-06-14 03:45:16 +0900230 return -ENODEV;
Tejun Heo8ef445f2013-10-01 17:42:01 -0400231 }
Tejun Heo0ab66082007-06-14 03:45:16 +0900232
Tejun Heof9b9a622013-10-01 17:42:05 -0400233 if (sysfs_is_bin(of->sd)) {
234 struct bin_attribute *battr = of->sd->s_bin_attr.bin_attr;
235
236 rc = -EIO;
237 if (battr->write)
238 rc = battr->write(of->file, kobj, battr, buf, off,
239 count);
240 } else {
241 const struct sysfs_ops *ops = sysfs_file_ops(of->sd);
242
243 rc = ops->store(kobj, of->sd->s_attr.attr, buf, count);
244 }
Tejun Heo0ab66082007-06-14 03:45:16 +0900245
Tejun Heobcafe4e2013-10-01 17:42:00 -0400246 sysfs_put_active(of->sd);
Tejun Heo8ef445f2013-10-01 17:42:01 -0400247 mutex_unlock(&of->mutex);
Tejun Heo0ab66082007-06-14 03:45:16 +0900248
249 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252/**
Tejun Heo8ef445f2013-10-01 17:42:01 -0400253 * sysfs_write_file - write an attribute
254 * @file: file pointer
255 * @user_buf: data to write
256 * @count: number of bytes
257 * @ppos: starting offset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 *
Tejun Heo8ef445f2013-10-01 17:42:01 -0400259 * Copy data in from userland and pass it to the matching
260 * sysfs_ops->store() by invoking flush_write_buffer().
261 *
262 * There is no easy way for us to know if userspace is only doing a partial
263 * write, so we don't support them. We expect the entire buffer to come on
264 * the first write. Hint: if you're writing a value, first read the file,
265 * modify only the the value you're changing, then write entire buffer
266 * back.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 */
Tejun Heo8ef445f2013-10-01 17:42:01 -0400268static ssize_t sysfs_write_file(struct file *file, const char __user *user_buf,
Greg Kroah-Hartmanddfd6d02013-08-21 16:33:34 -0700269 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Tejun Heo13c589d2013-10-01 17:42:02 -0400271 struct sysfs_open_file *of = sysfs_of(file);
Tejun Heof9b9a622013-10-01 17:42:05 -0400272 ssize_t len = min_t(size_t, count, PAGE_SIZE);
Tejun Heo8ef445f2013-10-01 17:42:01 -0400273 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Tejun Heof9b9a622013-10-01 17:42:05 -0400275 if (sysfs_is_bin(of->sd)) {
276 loff_t size = file_inode(file)->i_size;
277
278 if (size <= *ppos)
279 return 0;
280 len = min_t(ssize_t, len, size - *ppos);
281 }
282
Tejun Heo8ef445f2013-10-01 17:42:01 -0400283 if (!len)
284 return 0;
285
286 buf = kmalloc(len + 1, GFP_KERNEL);
287 if (!buf)
288 return -ENOMEM;
289
290 if (copy_from_user(buf, user_buf, len)) {
291 len = -EFAULT;
292 goto out_free;
293 }
294 buf[len] = '\0'; /* guarantee string termination */
295
Tejun Heof9b9a622013-10-01 17:42:05 -0400296 len = flush_write_buffer(of, buf, *ppos, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 if (len > 0)
298 *ppos += len;
Tejun Heo8ef445f2013-10-01 17:42:01 -0400299out_free:
300 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return len;
302}
303
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900304/**
305 * sysfs_get_open_dirent - get or create sysfs_open_dirent
306 * @sd: target sysfs_dirent
Tejun Heo58282d82013-10-01 17:41:59 -0400307 * @of: sysfs_open_file for this instance of open
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900308 *
309 * If @sd->s_attr.open exists, increment its reference count;
Tejun Heo58282d82013-10-01 17:41:59 -0400310 * otherwise, create one. @of is chained to the files list.
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900311 *
312 * LOCKING:
313 * Kernel thread context (may sleep).
314 *
315 * RETURNS:
316 * 0 on success, -errno on failure.
317 */
318static int sysfs_get_open_dirent(struct sysfs_dirent *sd,
Tejun Heo58282d82013-10-01 17:41:59 -0400319 struct sysfs_open_file *of)
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900320{
321 struct sysfs_open_dirent *od, *new_od = NULL;
322
323 retry:
Tejun Heoc75ec762013-10-01 17:41:58 -0400324 mutex_lock(&sysfs_open_file_mutex);
Neil Brown83db93f2009-09-15 16:05:51 -0700325 spin_lock_irq(&sysfs_open_dirent_lock);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900326
327 if (!sd->s_attr.open && new_od) {
328 sd->s_attr.open = new_od;
329 new_od = NULL;
330 }
331
332 od = sd->s_attr.open;
333 if (od) {
334 atomic_inc(&od->refcnt);
Tejun Heo58282d82013-10-01 17:41:59 -0400335 list_add_tail(&of->list, &od->files);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900336 }
337
Neil Brown83db93f2009-09-15 16:05:51 -0700338 spin_unlock_irq(&sysfs_open_dirent_lock);
Tejun Heoc75ec762013-10-01 17:41:58 -0400339 mutex_unlock(&sysfs_open_file_mutex);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900340
341 if (od) {
342 kfree(new_od);
343 return 0;
344 }
345
346 /* not there, initialize a new one and retry */
347 new_od = kmalloc(sizeof(*new_od), GFP_KERNEL);
348 if (!new_od)
349 return -ENOMEM;
350
351 atomic_set(&new_od->refcnt, 0);
Tejun Heoa4e8b912007-09-20 16:05:12 +0900352 atomic_set(&new_od->event, 1);
353 init_waitqueue_head(&new_od->poll);
Tejun Heo58282d82013-10-01 17:41:59 -0400354 INIT_LIST_HEAD(&new_od->files);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900355 goto retry;
356}
357
358/**
359 * sysfs_put_open_dirent - put sysfs_open_dirent
360 * @sd: target sysfs_dirent
Tejun Heo58282d82013-10-01 17:41:59 -0400361 * @of: associated sysfs_open_file
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900362 *
Tejun Heo58282d82013-10-01 17:41:59 -0400363 * Put @sd->s_attr.open and unlink @of from the files list. If
364 * reference count reaches zero, disassociate and free it.
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900365 *
366 * LOCKING:
367 * None.
368 */
369static void sysfs_put_open_dirent(struct sysfs_dirent *sd,
Tejun Heo58282d82013-10-01 17:41:59 -0400370 struct sysfs_open_file *of)
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900371{
372 struct sysfs_open_dirent *od = sd->s_attr.open;
Neil Brown83db93f2009-09-15 16:05:51 -0700373 unsigned long flags;
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900374
Tejun Heoc75ec762013-10-01 17:41:58 -0400375 mutex_lock(&sysfs_open_file_mutex);
Neil Brown83db93f2009-09-15 16:05:51 -0700376 spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900377
Tejun Heo58282d82013-10-01 17:41:59 -0400378 list_del(&of->list);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900379 if (atomic_dec_and_test(&od->refcnt))
380 sd->s_attr.open = NULL;
381 else
382 od = NULL;
383
Neil Brown83db93f2009-09-15 16:05:51 -0700384 spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
Tejun Heoc75ec762013-10-01 17:41:58 -0400385 mutex_unlock(&sysfs_open_file_mutex);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900386
387 kfree(od);
388}
389
Oliver Neukum94bebf42006-12-20 10:52:44 +0100390static int sysfs_open_file(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
Tejun Heo3e519032007-06-14 03:45:15 +0900392 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900393 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
Tejun Heo58282d82013-10-01 17:41:59 -0400394 struct sysfs_open_file *of;
Emese Revfy52cf25d2010-01-19 02:58:23 +0100395 const struct sysfs_ops *ops;
Kay Sievers000f2a42007-11-02 13:47:53 +0100396 int error = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Tejun Heo0ab66082007-06-14 03:45:16 +0900398 /* need attr_sd for attr and ops, its parent for kobj */
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800399 if (!sysfs_get_active(attr_sd))
Tejun Heo0ab66082007-06-14 03:45:16 +0900400 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Kay Sievers000f2a42007-11-02 13:47:53 +0100402 /* every kobject with an attribute needs a ktype assigned */
Tejun Heo375b6112013-10-01 17:41:57 -0400403 ops = sysfs_file_ops(attr_sd);
404 if (WARN(!ops, KERN_ERR
405 "missing sysfs attribute operations for kobject: %s\n",
406 kobject_name(kobj)))
Tejun Heo7b595752007-06-14 03:45:17 +0900407 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 /* File needs write support.
Greg Kroah-Hartmanab9bf4b2013-08-21 16:21:17 -0700410 * The inode's perms must say it's ok,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 * and we must have a store method.
412 */
413 if (file->f_mode & FMODE_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (!(inode->i_mode & S_IWUGO) || !ops->store)
Tejun Heo7b595752007-06-14 03:45:17 +0900415 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
417
418 /* File needs read support.
419 * The inode's perms must say it's ok, and we there
420 * must be a show method for it.
421 */
422 if (file->f_mode & FMODE_READ) {
423 if (!(inode->i_mode & S_IRUGO) || !ops->show)
Tejun Heo7b595752007-06-14 03:45:17 +0900424 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
426
Tejun Heo13c589d2013-10-01 17:42:02 -0400427 /* allocate a sysfs_open_file for the file */
Tejun Heo0ab66082007-06-14 03:45:16 +0900428 error = -ENOMEM;
Tejun Heo58282d82013-10-01 17:41:59 -0400429 of = kzalloc(sizeof(struct sysfs_open_file), GFP_KERNEL);
430 if (!of)
Tejun Heo7b595752007-06-14 03:45:17 +0900431 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Tejun Heo58282d82013-10-01 17:41:59 -0400433 mutex_init(&of->mutex);
Tejun Heobcafe4e2013-10-01 17:42:00 -0400434 of->sd = attr_sd;
435 of->file = file;
Tejun Heo13c589d2013-10-01 17:42:02 -0400436
437 /*
438 * Always instantiate seq_file even if read access is not
439 * implemented or requested. This unifies private data access and
440 * most files are readable anyway.
441 */
442 error = single_open(file, sysfs_seq_show, of);
443 if (error)
444 goto err_free;
445
446 /* seq_file clears PWRITE unconditionally, restore it if WRITE */
447 if (file->f_mode & FMODE_WRITE)
448 file->f_mode |= FMODE_PWRITE;
Tejun Heo0ab66082007-06-14 03:45:16 +0900449
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900450 /* make sure we have open dirent struct */
Tejun Heo58282d82013-10-01 17:41:59 -0400451 error = sysfs_get_open_dirent(attr_sd, of);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900452 if (error)
Tejun Heo13c589d2013-10-01 17:42:02 -0400453 goto err_close;
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900454
Tejun Heob05f0542007-09-20 16:05:10 +0900455 /* open succeeded, put active references */
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800456 sysfs_put_active(attr_sd);
Tejun Heo0ab66082007-06-14 03:45:16 +0900457 return 0;
458
Tejun Heo13c589d2013-10-01 17:42:02 -0400459err_close:
460 single_release(inode, file);
461err_free:
Tejun Heo58282d82013-10-01 17:41:59 -0400462 kfree(of);
Tejun Heo13c589d2013-10-01 17:42:02 -0400463err_out:
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800464 sysfs_put_active(attr_sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return error;
466}
467
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900468static int sysfs_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900470 struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata;
Tejun Heo13c589d2013-10-01 17:42:02 -0400471 struct sysfs_open_file *of = sysfs_of(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Tejun Heo58282d82013-10-01 17:41:59 -0400473 sysfs_put_open_dirent(sd, of);
Tejun Heo13c589d2013-10-01 17:42:02 -0400474 single_release(inode, filp);
Tejun Heo58282d82013-10-01 17:41:59 -0400475 kfree(of);
Tejun Heo50ab1a72007-09-20 16:05:10 +0900476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 return 0;
478}
479
NeilBrown4508a7a2006-03-20 17:53:53 +1100480/* Sysfs attribute files are pollable. The idea is that you read
481 * the content and then you use 'poll' or 'select' to wait for
482 * the content to change. When the content changes (assuming the
483 * manager for the kobject supports notification), poll will
484 * return POLLERR|POLLPRI, and select will return the fd whether
485 * it is waiting for read, write, or exceptions.
486 * Once poll/select indicates that the value has changed, you
Dan Williams2424b5d2008-04-07 15:35:01 -0700487 * need to close and re-open the file, or seek to 0 and read again.
NeilBrown4508a7a2006-03-20 17:53:53 +1100488 * Reminder: this only works for attributes which actively support
489 * it, and it is not possible to test an attribute from userspace
Rolf Eike Beera93720e2007-08-10 13:51:07 -0700490 * to see if it supports poll (Neither 'poll' nor 'select' return
NeilBrown4508a7a2006-03-20 17:53:53 +1100491 * an appropriate error code). When in doubt, set a suitable timeout value.
492 */
493static unsigned int sysfs_poll(struct file *filp, poll_table *wait)
494{
Tejun Heo13c589d2013-10-01 17:42:02 -0400495 struct sysfs_open_file *of = sysfs_of(filp);
Tejun Heo0ab66082007-06-14 03:45:16 +0900496 struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata;
Tejun Heoa4e8b912007-09-20 16:05:12 +0900497 struct sysfs_open_dirent *od = attr_sd->s_attr.open;
Tejun Heo0ab66082007-06-14 03:45:16 +0900498
499 /* need parent for the kobj, grab both */
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800500 if (!sysfs_get_active(attr_sd))
Tejun Heo0ab66082007-06-14 03:45:16 +0900501 goto trigger;
NeilBrown4508a7a2006-03-20 17:53:53 +1100502
Tejun Heoa4e8b912007-09-20 16:05:12 +0900503 poll_wait(filp, &od->poll, wait);
NeilBrown4508a7a2006-03-20 17:53:53 +1100504
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800505 sysfs_put_active(attr_sd);
NeilBrown4508a7a2006-03-20 17:53:53 +1100506
Tejun Heo58282d82013-10-01 17:41:59 -0400507 if (of->event != atomic_read(&od->event))
Tejun Heo0ab66082007-06-14 03:45:16 +0900508 goto trigger;
509
KOSAKI Motohiro1af35572009-04-09 13:53:22 +0900510 return DEFAULT_POLLMASK;
Tejun Heo0ab66082007-06-14 03:45:16 +0900511
512 trigger:
KOSAKI Motohiro1af35572009-04-09 13:53:22 +0900513 return DEFAULT_POLLMASK|POLLERR|POLLPRI;
NeilBrown4508a7a2006-03-20 17:53:53 +1100514}
515
Neil Brownf1282c82008-07-16 08:58:04 +1000516void sysfs_notify_dirent(struct sysfs_dirent *sd)
517{
518 struct sysfs_open_dirent *od;
Neil Brown83db93f2009-09-15 16:05:51 -0700519 unsigned long flags;
Neil Brownf1282c82008-07-16 08:58:04 +1000520
Neil Brown83db93f2009-09-15 16:05:51 -0700521 spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
Neil Brownf1282c82008-07-16 08:58:04 +1000522
Nick Dyerfc60bb82013-06-07 15:45:13 +0100523 if (!WARN_ON(sysfs_type(sd) != SYSFS_KOBJ_ATTR)) {
524 od = sd->s_attr.open;
525 if (od) {
526 atomic_inc(&od->event);
527 wake_up_interruptible(&od->poll);
528 }
Neil Brownf1282c82008-07-16 08:58:04 +1000529 }
530
Neil Brown83db93f2009-09-15 16:05:51 -0700531 spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
Neil Brownf1282c82008-07-16 08:58:04 +1000532}
533EXPORT_SYMBOL_GPL(sysfs_notify_dirent);
534
Trent Piepho8c0e3992008-09-25 16:45:13 -0700535void sysfs_notify(struct kobject *k, const char *dir, const char *attr)
NeilBrown4508a7a2006-03-20 17:53:53 +1100536{
Tejun Heo51225032007-06-14 04:27:25 +0900537 struct sysfs_dirent *sd = k->sd;
NeilBrown4508a7a2006-03-20 17:53:53 +1100538
Tejun Heo51225032007-06-14 04:27:25 +0900539 mutex_lock(&sysfs_mutex);
NeilBrown4508a7a2006-03-20 17:53:53 +1100540
Tejun Heo51225032007-06-14 04:27:25 +0900541 if (sd && dir)
Tejun Heocfec0bc2013-09-11 22:29:09 -0400542 sd = sysfs_find_dirent(sd, dir, NULL);
Tejun Heo51225032007-06-14 04:27:25 +0900543 if (sd && attr)
Tejun Heocfec0bc2013-09-11 22:29:09 -0400544 sd = sysfs_find_dirent(sd, attr, NULL);
Neil Brownf1282c82008-07-16 08:58:04 +1000545 if (sd)
546 sysfs_notify_dirent(sd);
Tejun Heo51225032007-06-14 04:27:25 +0900547
548 mutex_unlock(&sysfs_mutex);
NeilBrown4508a7a2006-03-20 17:53:53 +1100549}
550EXPORT_SYMBOL_GPL(sysfs_notify);
551
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800552const struct file_operations sysfs_file_operations = {
Tejun Heo13c589d2013-10-01 17:42:02 -0400553 .read = seq_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 .write = sysfs_write_file,
Tejun Heo13c589d2013-10-01 17:42:02 -0400555 .llseek = seq_lseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 .open = sysfs_open_file,
557 .release = sysfs_release,
NeilBrown4508a7a2006-03-20 17:53:53 +1100558 .poll = sysfs_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559};
560
Tejun Heof9b9a622013-10-01 17:42:05 -0400561const struct file_operations sysfs_bin_operations = {
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400562 .read = sysfs_bin_read,
Tejun Heof9b9a622013-10-01 17:42:05 -0400563 .write = sysfs_write_file,
564 .llseek = generic_file_llseek,
565};
566
Tejun Heo58292cbe2013-09-11 22:29:04 -0400567int sysfs_add_file_mode_ns(struct sysfs_dirent *dir_sd,
568 const struct attribute *attr, int type,
569 umode_t amode, const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
James Bottomley0f423892008-03-20 20:47:52 -0500571 umode_t mode = (amode & S_IALLUGO) | S_IFREG;
Tejun Heofb6896d2007-06-14 04:27:24 +0900572 struct sysfs_addrm_cxt acxt;
Tejun Heoa26cd722007-06-14 03:45:14 +0900573 struct sysfs_dirent *sd;
Tejun Heo23dc2792007-08-02 21:38:03 +0900574 int rc;
Tejun Heoa26cd722007-06-14 03:45:14 +0900575
Tejun Heo3e519032007-06-14 03:45:15 +0900576 sd = sysfs_new_dirent(attr->name, mode, type);
Tejun Heo3007e992007-06-14 04:27:23 +0900577 if (!sd)
578 return -ENOMEM;
Eric W. Biederman487505c2011-10-12 21:53:38 +0000579
580 sd->s_ns = ns;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900581 sd->s_attr.attr = (void *)attr;
Eric W. Biedermana2db6842010-02-11 15:20:00 -0800582 sysfs_dirent_init_lockdep(sd);
Tejun Heoa26cd722007-06-14 03:45:14 +0900583
Tejun Heod69ac5a2013-09-18 17:15:35 -0400584 sysfs_addrm_start(&acxt);
585 rc = sysfs_add_one(&acxt, sd, dir_sd);
Tejun Heo23dc2792007-08-02 21:38:03 +0900586 sysfs_addrm_finish(&acxt);
Tejun Heo3007e992007-06-14 04:27:23 +0900587
Tejun Heo23dc2792007-08-02 21:38:03 +0900588 if (rc)
Tejun Heo967e35d2007-07-18 16:38:11 +0900589 sysfs_put(sd);
Tejun Heo3007e992007-06-14 04:27:23 +0900590
Tejun Heo23dc2792007-08-02 21:38:03 +0900591 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
593
594
James Bottomley0f423892008-03-20 20:47:52 -0500595int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,
596 int type)
597{
Tejun Heo58292cbe2013-09-11 22:29:04 -0400598 return sysfs_add_file_mode_ns(dir_sd, attr, type, attr->mode, NULL);
James Bottomley0f423892008-03-20 20:47:52 -0500599}
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601/**
Tejun Heo58292cbe2013-09-11 22:29:04 -0400602 * sysfs_create_file_ns - create an attribute file for an object with custom ns
603 * @kobj: object we're creating for
604 * @attr: attribute descriptor
605 * @ns: namespace the new file should belong to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 */
Tejun Heo58292cbe2013-09-11 22:29:04 -0400607int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
608 const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Tejun Heo608e2662007-06-14 04:27:22 +0900610 BUG_ON(!kobj || !kobj->sd || !attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Tejun Heo58292cbe2013-09-11 22:29:04 -0400612 return sysfs_add_file_mode_ns(kobj->sd, attr, SYSFS_KOBJ_ATTR,
613 attr->mode, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615}
Tejun Heo58292cbe2013-09-11 22:29:04 -0400616EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Andi Kleen1c205ae2010-01-05 12:48:01 +0100618int sysfs_create_files(struct kobject *kobj, const struct attribute **ptr)
619{
620 int err = 0;
621 int i;
622
623 for (i = 0; ptr[i] && !err; i++)
624 err = sysfs_create_file(kobj, ptr[i]);
625 if (err)
626 while (--i >= 0)
627 sysfs_remove_file(kobj, ptr[i]);
628 return err;
629}
Greg Kroah-Hartman1b866752013-08-21 16:17:47 -0700630EXPORT_SYMBOL_GPL(sysfs_create_files);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632/**
Alan Sterndfa87c82007-02-20 15:02:44 -0500633 * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
634 * @kobj: object we're acting for.
635 * @attr: attribute descriptor.
636 * @group: group name.
637 */
638int sysfs_add_file_to_group(struct kobject *kobj,
639 const struct attribute *attr, const char *group)
640{
Tejun Heo608e2662007-06-14 04:27:22 +0900641 struct sysfs_dirent *dir_sd;
Alan Sterndfa87c82007-02-20 15:02:44 -0500642 int error;
643
James Bottomley11f24fb2008-01-02 18:44:05 -0600644 if (group)
Tejun Heo388975c2013-09-11 23:19:13 -0400645 dir_sd = sysfs_get_dirent(kobj->sd, group);
James Bottomley11f24fb2008-01-02 18:44:05 -0600646 else
647 dir_sd = sysfs_get(kobj->sd);
648
Tejun Heo608e2662007-06-14 04:27:22 +0900649 if (!dir_sd)
650 return -ENOENT;
651
652 error = sysfs_add_file(dir_sd, attr, SYSFS_KOBJ_ATTR);
653 sysfs_put(dir_sd);
654
Alan Sterndfa87c82007-02-20 15:02:44 -0500655 return error;
656}
657EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659/**
Kay Sievers31e5abe2005-04-18 21:57:32 -0700660 * sysfs_chmod_file - update the modified mode value on an object attribute.
661 * @kobj: object we're acting for.
662 * @attr: attribute descriptor.
663 * @mode: file permissions.
664 *
665 */
Jean Delvare49c19402010-07-02 16:54:05 +0200666int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
Al Viro48176a92011-07-24 03:40:40 -0400667 umode_t mode)
Kay Sievers31e5abe2005-04-18 21:57:32 -0700668{
Eric W. Biederman06fc0d62009-11-20 16:08:54 -0800669 struct sysfs_dirent *sd;
Maneesh Sonibc062b12005-07-29 12:13:35 -0700670 struct iattr newattrs;
Tejun Heo51225032007-06-14 04:27:25 +0900671 int rc;
Kay Sievers31e5abe2005-04-18 21:57:32 -0700672
Eric W. Biederman06fc0d62009-11-20 16:08:54 -0800673 mutex_lock(&sysfs_mutex);
674
Tejun Heo51225032007-06-14 04:27:25 +0900675 rc = -ENOENT;
Tejun Heocfec0bc2013-09-11 22:29:09 -0400676 sd = sysfs_find_dirent(kobj->sd, attr->name, NULL);
Eric W. Biederman06fc0d62009-11-20 16:08:54 -0800677 if (!sd)
Tejun Heo51225032007-06-14 04:27:25 +0900678 goto out;
679
Eric W. Biederman06fc0d62009-11-20 16:08:54 -0800680 newattrs.ia_mode = (mode & S_IALLUGO) | (sd->s_mode & ~S_IALLUGO);
Eric W. Biederman4c6974f2009-11-07 23:27:02 -0800681 newattrs.ia_valid = ATTR_MODE;
Eric W. Biederman06fc0d62009-11-20 16:08:54 -0800682 rc = sysfs_sd_setattr(sd, &newattrs);
Tejun Heof88123e2007-09-20 16:05:10 +0900683
Tejun Heo51225032007-06-14 04:27:25 +0900684 out:
Eric W. Biederman06fc0d62009-11-20 16:08:54 -0800685 mutex_unlock(&sysfs_mutex);
Tejun Heo51225032007-06-14 04:27:25 +0900686 return rc;
Kay Sievers31e5abe2005-04-18 21:57:32 -0700687}
688EXPORT_SYMBOL_GPL(sysfs_chmod_file);
689
Kay Sievers31e5abe2005-04-18 21:57:32 -0700690/**
Tejun Heo58292cbe2013-09-11 22:29:04 -0400691 * sysfs_remove_file_ns - remove an object attribute with a custom ns tag
692 * @kobj: object we're acting for
693 * @attr: attribute descriptor
694 * @ns: namespace tag of the file to remove
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 *
Tejun Heo58292cbe2013-09-11 22:29:04 -0400696 * Hash the attribute name and namespace tag and kill the victim.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 */
Tejun Heo58292cbe2013-09-11 22:29:04 -0400698void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
699 const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Tejun Heo58292cbe2013-09-11 22:29:04 -0400701 struct sysfs_dirent *dir_sd = kobj->sd;
Eric W. Biederman487505c2011-10-12 21:53:38 +0000702
Tejun Heocfec0bc2013-09-11 22:29:09 -0400703 sysfs_hash_and_remove(dir_sd, attr->name, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
Tejun Heo58292cbe2013-09-11 22:29:04 -0400705EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Greg Kroah-Hartman1b18dc22013-08-21 16:28:26 -0700707void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr)
Andi Kleen1c205ae2010-01-05 12:48:01 +0100708{
709 int i;
710 for (i = 0; ptr[i]; i++)
711 sysfs_remove_file(kobj, ptr[i]);
712}
Greg Kroah-Hartman1b866752013-08-21 16:17:47 -0700713EXPORT_SYMBOL_GPL(sysfs_remove_files);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Alan Sterndfa87c82007-02-20 15:02:44 -0500715/**
716 * sysfs_remove_file_from_group - remove an attribute file from a group.
717 * @kobj: object we're acting for.
718 * @attr: attribute descriptor.
719 * @group: group name.
720 */
721void sysfs_remove_file_from_group(struct kobject *kobj,
722 const struct attribute *attr, const char *group)
723{
Tejun Heo608e2662007-06-14 04:27:22 +0900724 struct sysfs_dirent *dir_sd;
Alan Sterndfa87c82007-02-20 15:02:44 -0500725
James Bottomley11f24fb2008-01-02 18:44:05 -0600726 if (group)
Tejun Heo388975c2013-09-11 23:19:13 -0400727 dir_sd = sysfs_get_dirent(kobj->sd, group);
James Bottomley11f24fb2008-01-02 18:44:05 -0600728 else
729 dir_sd = sysfs_get(kobj->sd);
Tejun Heo608e2662007-06-14 04:27:22 +0900730 if (dir_sd) {
Tejun Heocfec0bc2013-09-11 22:29:09 -0400731 sysfs_hash_and_remove(dir_sd, attr->name, NULL);
Tejun Heo608e2662007-06-14 04:27:22 +0900732 sysfs_put(dir_sd);
Alan Sterndfa87c82007-02-20 15:02:44 -0500733 }
734}
735EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
736
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400737struct sysfs_schedule_callback_struct {
Alex Chiang66942062009-03-13 12:07:36 -0600738 struct list_head workq_list;
739 struct kobject *kobj;
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400740 void (*func)(void *);
741 void *data;
Alan Stern523ded72007-04-26 00:12:04 -0700742 struct module *owner;
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400743 struct work_struct work;
744};
745
Alex Chiangd1102712009-03-25 15:11:36 -0600746static struct workqueue_struct *sysfs_workqueue;
Alex Chiang66942062009-03-13 12:07:36 -0600747static DEFINE_MUTEX(sysfs_workq_mutex);
748static LIST_HEAD(sysfs_workq);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400749static void sysfs_schedule_callback_work(struct work_struct *work)
750{
751 struct sysfs_schedule_callback_struct *ss = container_of(work,
752 struct sysfs_schedule_callback_struct, work);
753
754 (ss->func)(ss->data);
755 kobject_put(ss->kobj);
Alan Stern523ded72007-04-26 00:12:04 -0700756 module_put(ss->owner);
Alex Chiang66942062009-03-13 12:07:36 -0600757 mutex_lock(&sysfs_workq_mutex);
758 list_del(&ss->workq_list);
759 mutex_unlock(&sysfs_workq_mutex);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400760 kfree(ss);
761}
762
763/**
764 * sysfs_schedule_callback - helper to schedule a callback for a kobject
765 * @kobj: object we're acting for.
766 * @func: callback function to invoke later.
767 * @data: argument to pass to @func.
Alan Stern523ded72007-04-26 00:12:04 -0700768 * @owner: module owning the callback code
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400769 *
770 * sysfs attribute methods must not unregister themselves or their parent
771 * kobject (which would amount to the same thing). Attempts to do so will
772 * deadlock, since unregistration is mutually exclusive with driver
773 * callbacks.
774 *
775 * Instead methods can call this routine, which will attempt to allocate
776 * and schedule a workqueue request to call back @func with @data as its
777 * argument in the workqueue's process context. @kobj will be pinned
778 * until @func returns.
779 *
780 * Returns 0 if the request was submitted, -ENOMEM if storage could not
Alex Chiang66942062009-03-13 12:07:36 -0600781 * be allocated, -ENODEV if a reference to @owner isn't available,
782 * -EAGAIN if a callback has already been scheduled for @kobj.
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400783 */
784int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
Alan Stern523ded72007-04-26 00:12:04 -0700785 void *data, struct module *owner)
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400786{
Alex Chiang66942062009-03-13 12:07:36 -0600787 struct sysfs_schedule_callback_struct *ss, *tmp;
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400788
Alan Stern523ded72007-04-26 00:12:04 -0700789 if (!try_module_get(owner))
790 return -ENODEV;
Alex Chiang66942062009-03-13 12:07:36 -0600791
792 mutex_lock(&sysfs_workq_mutex);
793 list_for_each_entry_safe(ss, tmp, &sysfs_workq, workq_list)
794 if (ss->kobj == kobj) {
Alex Chiangd1102712009-03-25 15:11:36 -0600795 module_put(owner);
Alex Chiang66942062009-03-13 12:07:36 -0600796 mutex_unlock(&sysfs_workq_mutex);
797 return -EAGAIN;
798 }
799 mutex_unlock(&sysfs_workq_mutex);
800
Alex Chiangd1102712009-03-25 15:11:36 -0600801 if (sysfs_workqueue == NULL) {
Andrew Morton086a3772009-05-07 12:36:53 -0700802 sysfs_workqueue = create_singlethread_workqueue("sysfsd");
Alex Chiangd1102712009-03-25 15:11:36 -0600803 if (sysfs_workqueue == NULL) {
804 module_put(owner);
805 return -ENOMEM;
806 }
807 }
808
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400809 ss = kmalloc(sizeof(*ss), GFP_KERNEL);
Alan Stern523ded72007-04-26 00:12:04 -0700810 if (!ss) {
811 module_put(owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400812 return -ENOMEM;
Alan Stern523ded72007-04-26 00:12:04 -0700813 }
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400814 kobject_get(kobj);
815 ss->kobj = kobj;
816 ss->func = func;
817 ss->data = data;
Alan Stern523ded72007-04-26 00:12:04 -0700818 ss->owner = owner;
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400819 INIT_WORK(&ss->work, sysfs_schedule_callback_work);
Alex Chiang66942062009-03-13 12:07:36 -0600820 INIT_LIST_HEAD(&ss->workq_list);
821 mutex_lock(&sysfs_workq_mutex);
822 list_add_tail(&ss->workq_list, &sysfs_workq);
823 mutex_unlock(&sysfs_workq_mutex);
Alex Chiangd1102712009-03-25 15:11:36 -0600824 queue_work(sysfs_workqueue, &ss->work);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400825 return 0;
826}
827EXPORT_SYMBOL_GPL(sysfs_schedule_callback);