blob: ac77d2be3c312df08fa1b1fc90fd9525cf06aafd [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>
Oliver Neukum94bebf42006-12-20 10:52:44 +010017#include <linux/list.h>
Dave Young52e8c202007-07-26 11:03:54 +000018#include <linux/mutex.h>
Tejun Heo13c589d2013-10-01 17:42:02 -040019#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#include "sysfs.h"
Tejun Heo414985a2013-11-28 14:54:34 -050022#include "../kernfs/kernfs-internal.h"
Tejun Heof6acf8b2013-11-28 14:54:21 -050023
24/*
Tejun Heo375b6112013-10-01 17:41:57 -040025 * Determine ktype->sysfs_ops for the given sysfs_dirent. This function
26 * must be called while holding an active reference.
27 */
28static const struct sysfs_ops *sysfs_file_ops(struct sysfs_dirent *sd)
29{
Tejun Heo7c6e2d32013-11-28 14:54:14 -050030 struct kobject *kobj = sd->s_parent->priv;
Tejun Heo375b6112013-10-01 17:41:57 -040031
Tejun Heo517e64f2013-11-28 14:54:29 -050032 if (sd->s_flags & SYSFS_FLAG_LOCKDEP)
Tejun Heo785a1622013-10-14 09:27:11 -040033 lockdep_assert_held(sd);
Tejun Heo375b6112013-10-01 17:41:57 -040034 return kobj->ktype ? kobj->ktype->sysfs_ops : NULL;
35}
36
Tejun Heo13c589d2013-10-01 17:42:02 -040037/*
38 * Reads on sysfs are handled through seq_file, which takes care of hairy
39 * details like buffering and seeking. The following function pipes
40 * sysfs_ops->show() result through seq_file.
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 */
Tejun Heoc2b19da2013-11-28 14:54:16 -050042static int sysfs_kf_seq_show(struct seq_file *sf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Tejun Heo13c589d2013-10-01 17:42:02 -040044 struct sysfs_open_file *of = sf->private;
Tejun Heo7c6e2d32013-11-28 14:54:14 -050045 struct kobject *kobj = of->sd->s_parent->priv;
Tejun Heoc2b19da2013-11-28 14:54:16 -050046 const struct sysfs_ops *ops = sysfs_file_ops(of->sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 ssize_t count;
Tejun Heoc2b19da2013-11-28 14:54:16 -050048 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Tejun Heo13c589d2013-10-01 17:42:02 -040050 /* acquire buffer and ensure that it's >= PAGE_SIZE */
51 count = seq_get_buf(sf, &buf);
52 if (count < PAGE_SIZE) {
53 seq_commit(sf, -1);
54 return 0;
55 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Tejun Heo13c589d2013-10-01 17:42:02 -040057 /*
Tejun Heoc2b19da2013-11-28 14:54:16 -050058 * Invoke show(). Control may reach here via seq file lseek even
59 * if @ops->show() isn't implemented.
Tejun Heo13c589d2013-10-01 17:42:02 -040060 */
Tejun Heoc2b19da2013-11-28 14:54:16 -050061 if (ops->show) {
Tejun Heo7c6e2d32013-11-28 14:54:14 -050062 count = ops->show(kobj, of->sd->priv, buf);
Tejun Heoc2b19da2013-11-28 14:54:16 -050063 if (count < 0)
64 return count;
65 }
Tejun Heo0ab66082007-06-14 03:45:16 +090066
Miao Xie8118a852007-11-21 14:55:19 -080067 /*
68 * The code works fine with PAGE_SIZE return but it's likely to
69 * indicate truncated result or overflow in normal use cases.
70 */
Andrew Morton815d2d52008-03-04 15:09:07 -080071 if (count >= (ssize_t)PAGE_SIZE) {
72 print_symbol("fill_read_buffer: %s returned bad count\n",
73 (unsigned long)ops->show);
74 /* Try to struggle along */
75 count = PAGE_SIZE - 1;
76 }
Tejun Heo13c589d2013-10-01 17:42:02 -040077 seq_commit(sf, count);
78 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
80
Tejun Heoc2b19da2013-11-28 14:54:16 -050081static ssize_t sysfs_kf_bin_read(struct sysfs_open_file *of, char *buf,
82 size_t count, loff_t pos)
Tejun Heo2f0c6b72013-10-01 17:42:06 -040083{
Tejun Heo7c6e2d32013-11-28 14:54:14 -050084 struct bin_attribute *battr = of->sd->priv;
85 struct kobject *kobj = of->sd->s_parent->priv;
Tejun Heoc2b19da2013-11-28 14:54:16 -050086 loff_t size = file_inode(of->file)->i_size;
Tejun Heo2f0c6b72013-10-01 17:42:06 -040087
Tejun Heoc2b19da2013-11-28 14:54:16 -050088 if (!count)
Tejun Heo2f0c6b72013-10-01 17:42:06 -040089 return 0;
90
91 if (size) {
Tejun Heoc2b19da2013-11-28 14:54:16 -050092 if (pos > size)
Tejun Heo2f0c6b72013-10-01 17:42:06 -040093 return 0;
Tejun Heoc2b19da2013-11-28 14:54:16 -050094 if (pos + count > size)
95 count = size - pos;
Tejun Heo2f0c6b72013-10-01 17:42:06 -040096 }
97
Tejun Heoc2b19da2013-11-28 14:54:16 -050098 if (!battr->read)
99 return -EIO;
100
101 return battr->read(of->file, kobj, battr, buf, pos, count);
102}
103
Tejun Heo50b38ca2013-11-28 14:54:17 -0500104/* kernfs write callback for regular sysfs files */
105static ssize_t sysfs_kf_write(struct sysfs_open_file *of, char *buf,
106 size_t count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Tejun Heo50b38ca2013-11-28 14:54:17 -0500108 const struct sysfs_ops *ops = sysfs_file_ops(of->sd);
Tejun Heo7c6e2d32013-11-28 14:54:14 -0500109 struct kobject *kobj = of->sd->s_parent->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Tejun Heo50b38ca2013-11-28 14:54:17 -0500111 if (!count)
112 return 0;
113
114 return ops->store(kobj, of->sd->priv, buf, count);
115}
116
117/* kernfs write callback for bin sysfs files */
118static ssize_t sysfs_kf_bin_write(struct sysfs_open_file *of, char *buf,
119 size_t count, loff_t pos)
120{
121 struct bin_attribute *battr = of->sd->priv;
122 struct kobject *kobj = of->sd->s_parent->priv;
123 loff_t size = file_inode(of->file)->i_size;
124
125 if (size) {
126 if (size <= pos)
127 return 0;
128 count = min_t(ssize_t, count, size - pos);
Tejun Heo8ef445f2013-10-01 17:42:01 -0400129 }
Tejun Heo50b38ca2013-11-28 14:54:17 -0500130 if (!count)
131 return 0;
Tejun Heo0ab66082007-06-14 03:45:16 +0900132
Tejun Heo50b38ca2013-11-28 14:54:17 -0500133 if (!battr->write)
134 return -EIO;
Tejun Heof9b9a622013-10-01 17:42:05 -0400135
Tejun Heo50b38ca2013-11-28 14:54:17 -0500136 return battr->write(of->file, kobj, battr, buf, pos, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Tejun Heofdbffaa2013-11-28 14:54:18 -0500139static int sysfs_kf_bin_mmap(struct sysfs_open_file *of,
140 struct vm_area_struct *vma)
141{
142 struct bin_attribute *battr = of->sd->priv;
143 struct kobject *kobj = of->sd->s_parent->priv;
144
145 if (!battr->mmap)
146 return -ENODEV;
147
148 return battr->mmap(of->file, kobj, battr, vma);
149}
150
Trent Piepho8c0e3992008-09-25 16:45:13 -0700151void sysfs_notify(struct kobject *k, const char *dir, const char *attr)
NeilBrown4508a7a2006-03-20 17:53:53 +1100152{
Tejun Heo024f6472013-11-28 14:54:27 -0500153 struct sysfs_dirent *sd = k->sd, *tmp;
NeilBrown4508a7a2006-03-20 17:53:53 +1100154
Tejun Heo51225032007-06-14 04:27:25 +0900155 if (sd && dir)
Tejun Heoccf73cf2013-11-28 14:54:30 -0500156 sd = kernfs_find_and_get(sd, dir);
Tejun Heo024f6472013-11-28 14:54:27 -0500157 else
Tejun Heoccf73cf2013-11-28 14:54:30 -0500158 kernfs_get(sd);
Tejun Heo51225032007-06-14 04:27:25 +0900159
Tejun Heo024f6472013-11-28 14:54:27 -0500160 if (sd && attr) {
Tejun Heoccf73cf2013-11-28 14:54:30 -0500161 tmp = kernfs_find_and_get(sd, attr);
162 kernfs_put(sd);
Tejun Heo024f6472013-11-28 14:54:27 -0500163 sd = tmp;
164 }
165
166 if (sd) {
167 kernfs_notify(sd);
Tejun Heoccf73cf2013-11-28 14:54:30 -0500168 kernfs_put(sd);
Tejun Heo024f6472013-11-28 14:54:27 -0500169 }
NeilBrown4508a7a2006-03-20 17:53:53 +1100170}
171EXPORT_SYMBOL_GPL(sysfs_notify);
172
Tejun Heof6acf8b2013-11-28 14:54:21 -0500173static const struct kernfs_ops sysfs_file_kfops_empty = {
174};
175
176static const struct kernfs_ops sysfs_file_kfops_ro = {
177 .seq_show = sysfs_kf_seq_show,
178};
179
180static const struct kernfs_ops sysfs_file_kfops_wo = {
181 .write = sysfs_kf_write,
182};
183
184static const struct kernfs_ops sysfs_file_kfops_rw = {
185 .seq_show = sysfs_kf_seq_show,
186 .write = sysfs_kf_write,
187};
188
189static const struct kernfs_ops sysfs_bin_kfops_ro = {
190 .read = sysfs_kf_bin_read,
191};
192
193static const struct kernfs_ops sysfs_bin_kfops_wo = {
194 .write = sysfs_kf_bin_write,
195};
196
197static const struct kernfs_ops sysfs_bin_kfops_rw = {
198 .read = sysfs_kf_bin_read,
199 .write = sysfs_kf_bin_write,
200 .mmap = sysfs_kf_bin_mmap,
201};
202
Tejun Heo58292cbe2013-09-11 22:29:04 -0400203int sysfs_add_file_mode_ns(struct sysfs_dirent *dir_sd,
Tejun Heoa7dc66d2013-11-28 14:54:23 -0500204 const struct attribute *attr, bool is_bin,
Tejun Heo496f7392013-11-28 14:54:24 -0500205 umode_t mode, const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
Tejun Heo517e64f2013-11-28 14:54:29 -0500207 struct lock_class_key *key = NULL;
Tejun Heof6acf8b2013-11-28 14:54:21 -0500208 const struct kernfs_ops *ops;
Tejun Heoa26cd722007-06-14 03:45:14 +0900209 struct sysfs_dirent *sd;
Tejun Heo471bd7b2013-11-28 14:54:22 -0500210 loff_t size;
Tejun Heoa26cd722007-06-14 03:45:14 +0900211
Tejun Heoa7dc66d2013-11-28 14:54:23 -0500212 if (!is_bin) {
Tejun Heof6acf8b2013-11-28 14:54:21 -0500213 struct kobject *kobj = dir_sd->priv;
214 const struct sysfs_ops *sysfs_ops = kobj->ktype->sysfs_ops;
215
216 /* every kobject with an attribute needs a ktype assigned */
217 if (WARN(!sysfs_ops, KERN_ERR
218 "missing sysfs attribute operations for kobject: %s\n",
219 kobject_name(kobj)))
220 return -EINVAL;
221
222 if (sysfs_ops->show && sysfs_ops->store)
223 ops = &sysfs_file_kfops_rw;
224 else if (sysfs_ops->show)
225 ops = &sysfs_file_kfops_ro;
226 else if (sysfs_ops->store)
227 ops = &sysfs_file_kfops_wo;
228 else
229 ops = &sysfs_file_kfops_empty;
Tejun Heo471bd7b2013-11-28 14:54:22 -0500230
231 size = PAGE_SIZE;
Tejun Heof6acf8b2013-11-28 14:54:21 -0500232 } else {
233 struct bin_attribute *battr = (void *)attr;
234
235 if ((battr->read && battr->write) || battr->mmap)
236 ops = &sysfs_bin_kfops_rw;
237 else if (battr->read)
238 ops = &sysfs_bin_kfops_ro;
239 else if (battr->write)
240 ops = &sysfs_bin_kfops_wo;
241 else
242 ops = &sysfs_file_kfops_empty;
Tejun Heo471bd7b2013-11-28 14:54:22 -0500243
244 size = battr->size;
Tejun Heof6acf8b2013-11-28 14:54:21 -0500245 }
246
Tejun Heo517e64f2013-11-28 14:54:29 -0500247#ifdef CONFIG_DEBUG_LOCK_ALLOC
248 if (!attr->ignore_lockdep)
249 key = attr->key ?: (struct lock_class_key *)&attr->skey;
250#endif
251 sd = kernfs_create_file_ns_key(dir_sd, attr->name, mode, size,
252 ops, (void *)attr, ns, key);
Tejun Heo496f7392013-11-28 14:54:24 -0500253 if (IS_ERR(sd)) {
254 if (PTR_ERR(sd) == -EEXIST)
255 sysfs_warn_dup(dir_sd, attr->name);
256 return PTR_ERR(sd);
257 }
258 return 0;
259}
260
James Bottomley0f423892008-03-20 20:47:52 -0500261int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,
Tejun Heoa7dc66d2013-11-28 14:54:23 -0500262 bool is_bin)
James Bottomley0f423892008-03-20 20:47:52 -0500263{
Tejun Heoa7dc66d2013-11-28 14:54:23 -0500264 return sysfs_add_file_mode_ns(dir_sd, attr, is_bin, attr->mode, NULL);
James Bottomley0f423892008-03-20 20:47:52 -0500265}
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267/**
Tejun Heo58292cbe2013-09-11 22:29:04 -0400268 * sysfs_create_file_ns - create an attribute file for an object with custom ns
269 * @kobj: object we're creating for
270 * @attr: attribute descriptor
271 * @ns: namespace the new file should belong to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 */
Tejun Heo58292cbe2013-09-11 22:29:04 -0400273int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
274 const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Tejun Heo608e2662007-06-14 04:27:22 +0900276 BUG_ON(!kobj || !kobj->sd || !attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Tejun Heoa7dc66d2013-11-28 14:54:23 -0500278 return sysfs_add_file_mode_ns(kobj->sd, attr, false, attr->mode, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280}
Tejun Heo58292cbe2013-09-11 22:29:04 -0400281EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Andi Kleen1c205ae2010-01-05 12:48:01 +0100283int sysfs_create_files(struct kobject *kobj, const struct attribute **ptr)
284{
285 int err = 0;
286 int i;
287
288 for (i = 0; ptr[i] && !err; i++)
289 err = sysfs_create_file(kobj, ptr[i]);
290 if (err)
291 while (--i >= 0)
292 sysfs_remove_file(kobj, ptr[i]);
293 return err;
294}
Greg Kroah-Hartman1b866752013-08-21 16:17:47 -0700295EXPORT_SYMBOL_GPL(sysfs_create_files);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297/**
Alan Sterndfa87c82007-02-20 15:02:44 -0500298 * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
299 * @kobj: object we're acting for.
300 * @attr: attribute descriptor.
301 * @group: group name.
302 */
303int sysfs_add_file_to_group(struct kobject *kobj,
304 const struct attribute *attr, const char *group)
305{
Tejun Heo608e2662007-06-14 04:27:22 +0900306 struct sysfs_dirent *dir_sd;
Alan Sterndfa87c82007-02-20 15:02:44 -0500307 int error;
308
Tejun Heoccf73cf2013-11-28 14:54:30 -0500309 if (group) {
310 dir_sd = kernfs_find_and_get(kobj->sd, group);
311 } else {
312 dir_sd = kobj->sd;
313 kernfs_get(dir_sd);
314 }
James Bottomley11f24fb2008-01-02 18:44:05 -0600315
Tejun Heo608e2662007-06-14 04:27:22 +0900316 if (!dir_sd)
317 return -ENOENT;
318
Tejun Heoa7dc66d2013-11-28 14:54:23 -0500319 error = sysfs_add_file(dir_sd, attr, false);
Tejun Heoccf73cf2013-11-28 14:54:30 -0500320 kernfs_put(dir_sd);
Tejun Heo608e2662007-06-14 04:27:22 +0900321
Alan Sterndfa87c82007-02-20 15:02:44 -0500322 return error;
323}
324EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326/**
Kay Sievers31e5abe2005-04-18 21:57:32 -0700327 * sysfs_chmod_file - update the modified mode value on an object attribute.
328 * @kobj: object we're acting for.
329 * @attr: attribute descriptor.
330 * @mode: file permissions.
331 *
332 */
Jean Delvare49c19402010-07-02 16:54:05 +0200333int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
Al Viro48176a92011-07-24 03:40:40 -0400334 umode_t mode)
Kay Sievers31e5abe2005-04-18 21:57:32 -0700335{
Eric W. Biederman06fc0d62009-11-20 16:08:54 -0800336 struct sysfs_dirent *sd;
Maneesh Sonibc062b12005-07-29 12:13:35 -0700337 struct iattr newattrs;
Tejun Heo51225032007-06-14 04:27:25 +0900338 int rc;
Kay Sievers31e5abe2005-04-18 21:57:32 -0700339
Tejun Heoccf73cf2013-11-28 14:54:30 -0500340 sd = kernfs_find_and_get(kobj->sd, attr->name);
Eric W. Biederman06fc0d62009-11-20 16:08:54 -0800341 if (!sd)
Tejun Heo5d604182013-11-23 17:21:52 -0500342 return -ENOENT;
Tejun Heo51225032007-06-14 04:27:25 +0900343
Eric W. Biederman06fc0d62009-11-20 16:08:54 -0800344 newattrs.ia_mode = (mode & S_IALLUGO) | (sd->s_mode & ~S_IALLUGO);
Eric W. Biederman4c6974f2009-11-07 23:27:02 -0800345 newattrs.ia_valid = ATTR_MODE;
Tejun Heof88123e2007-09-20 16:05:10 +0900346
Tejun Heo5d604182013-11-23 17:21:52 -0500347 rc = kernfs_setattr(sd, &newattrs);
348
Tejun Heoccf73cf2013-11-28 14:54:30 -0500349 kernfs_put(sd);
Tejun Heo51225032007-06-14 04:27:25 +0900350 return rc;
Kay Sievers31e5abe2005-04-18 21:57:32 -0700351}
352EXPORT_SYMBOL_GPL(sysfs_chmod_file);
353
Kay Sievers31e5abe2005-04-18 21:57:32 -0700354/**
Tejun Heo58292cbe2013-09-11 22:29:04 -0400355 * sysfs_remove_file_ns - remove an object attribute with a custom ns tag
356 * @kobj: object we're acting for
357 * @attr: attribute descriptor
358 * @ns: namespace tag of the file to remove
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 *
Tejun Heo58292cbe2013-09-11 22:29:04 -0400360 * Hash the attribute name and namespace tag and kill the victim.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 */
Tejun Heo58292cbe2013-09-11 22:29:04 -0400362void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
363 const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
Tejun Heo58292cbe2013-09-11 22:29:04 -0400365 struct sysfs_dirent *dir_sd = kobj->sd;
Eric W. Biederman487505c2011-10-12 21:53:38 +0000366
Tejun Heo879f40d2013-11-23 17:21:49 -0500367 kernfs_remove_by_name_ns(dir_sd, attr->name, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
Tejun Heo58292cbe2013-09-11 22:29:04 -0400369EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Greg Kroah-Hartman1b18dc22013-08-21 16:28:26 -0700371void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr)
Andi Kleen1c205ae2010-01-05 12:48:01 +0100372{
373 int i;
374 for (i = 0; ptr[i]; i++)
375 sysfs_remove_file(kobj, ptr[i]);
376}
Greg Kroah-Hartman1b866752013-08-21 16:17:47 -0700377EXPORT_SYMBOL_GPL(sysfs_remove_files);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Alan Sterndfa87c82007-02-20 15:02:44 -0500379/**
380 * sysfs_remove_file_from_group - remove an attribute file from a group.
381 * @kobj: object we're acting for.
382 * @attr: attribute descriptor.
383 * @group: group name.
384 */
385void sysfs_remove_file_from_group(struct kobject *kobj,
386 const struct attribute *attr, const char *group)
387{
Tejun Heo608e2662007-06-14 04:27:22 +0900388 struct sysfs_dirent *dir_sd;
Alan Sterndfa87c82007-02-20 15:02:44 -0500389
Tejun Heoccf73cf2013-11-28 14:54:30 -0500390 if (group) {
391 dir_sd = kernfs_find_and_get(kobj->sd, group);
392 } else {
393 dir_sd = kobj->sd;
394 kernfs_get(dir_sd);
395 }
396
Tejun Heo608e2662007-06-14 04:27:22 +0900397 if (dir_sd) {
Tejun Heo879f40d2013-11-23 17:21:49 -0500398 kernfs_remove_by_name(dir_sd, attr->name);
Tejun Heoccf73cf2013-11-28 14:54:30 -0500399 kernfs_put(dir_sd);
Alan Sterndfa87c82007-02-20 15:02:44 -0500400 }
401}
402EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
403
Tejun Heo3124eb12013-10-01 17:42:09 -0400404/**
405 * sysfs_create_bin_file - create binary file for object.
406 * @kobj: object.
407 * @attr: attribute descriptor.
408 */
409int sysfs_create_bin_file(struct kobject *kobj,
410 const struct bin_attribute *attr)
411{
412 BUG_ON(!kobj || !kobj->sd || !attr);
413
Tejun Heoa7dc66d2013-11-28 14:54:23 -0500414 return sysfs_add_file(kobj->sd, &attr->attr, true);
Tejun Heo3124eb12013-10-01 17:42:09 -0400415}
416EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
417
418/**
419 * sysfs_remove_bin_file - remove binary file for object.
420 * @kobj: object.
421 * @attr: attribute descriptor.
422 */
423void sysfs_remove_bin_file(struct kobject *kobj,
424 const struct bin_attribute *attr)
425{
Tejun Heo879f40d2013-11-23 17:21:49 -0500426 kernfs_remove_by_name(kobj->sd, attr->attr.name);
Tejun Heo3124eb12013-10-01 17:42:09 -0400427}
428EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);
429
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400430struct sysfs_schedule_callback_struct {
Alex Chiang66942062009-03-13 12:07:36 -0600431 struct list_head workq_list;
432 struct kobject *kobj;
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400433 void (*func)(void *);
434 void *data;
Alan Stern523ded72007-04-26 00:12:04 -0700435 struct module *owner;
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400436 struct work_struct work;
437};
438
Alex Chiangd1102712009-03-25 15:11:36 -0600439static struct workqueue_struct *sysfs_workqueue;
Alex Chiang66942062009-03-13 12:07:36 -0600440static DEFINE_MUTEX(sysfs_workq_mutex);
441static LIST_HEAD(sysfs_workq);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400442static void sysfs_schedule_callback_work(struct work_struct *work)
443{
444 struct sysfs_schedule_callback_struct *ss = container_of(work,
445 struct sysfs_schedule_callback_struct, work);
446
447 (ss->func)(ss->data);
448 kobject_put(ss->kobj);
Alan Stern523ded72007-04-26 00:12:04 -0700449 module_put(ss->owner);
Alex Chiang66942062009-03-13 12:07:36 -0600450 mutex_lock(&sysfs_workq_mutex);
451 list_del(&ss->workq_list);
452 mutex_unlock(&sysfs_workq_mutex);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400453 kfree(ss);
454}
455
456/**
457 * sysfs_schedule_callback - helper to schedule a callback for a kobject
458 * @kobj: object we're acting for.
459 * @func: callback function to invoke later.
460 * @data: argument to pass to @func.
Alan Stern523ded72007-04-26 00:12:04 -0700461 * @owner: module owning the callback code
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400462 *
463 * sysfs attribute methods must not unregister themselves or their parent
464 * kobject (which would amount to the same thing). Attempts to do so will
465 * deadlock, since unregistration is mutually exclusive with driver
466 * callbacks.
467 *
468 * Instead methods can call this routine, which will attempt to allocate
469 * and schedule a workqueue request to call back @func with @data as its
470 * argument in the workqueue's process context. @kobj will be pinned
471 * until @func returns.
472 *
473 * Returns 0 if the request was submitted, -ENOMEM if storage could not
Alex Chiang66942062009-03-13 12:07:36 -0600474 * be allocated, -ENODEV if a reference to @owner isn't available,
475 * -EAGAIN if a callback has already been scheduled for @kobj.
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400476 */
477int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
Alan Stern523ded72007-04-26 00:12:04 -0700478 void *data, struct module *owner)
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400479{
Alex Chiang66942062009-03-13 12:07:36 -0600480 struct sysfs_schedule_callback_struct *ss, *tmp;
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400481
Alan Stern523ded72007-04-26 00:12:04 -0700482 if (!try_module_get(owner))
483 return -ENODEV;
Alex Chiang66942062009-03-13 12:07:36 -0600484
485 mutex_lock(&sysfs_workq_mutex);
486 list_for_each_entry_safe(ss, tmp, &sysfs_workq, workq_list)
487 if (ss->kobj == kobj) {
Alex Chiangd1102712009-03-25 15:11:36 -0600488 module_put(owner);
Alex Chiang66942062009-03-13 12:07:36 -0600489 mutex_unlock(&sysfs_workq_mutex);
490 return -EAGAIN;
491 }
492 mutex_unlock(&sysfs_workq_mutex);
493
Alex Chiangd1102712009-03-25 15:11:36 -0600494 if (sysfs_workqueue == NULL) {
Andrew Morton086a3772009-05-07 12:36:53 -0700495 sysfs_workqueue = create_singlethread_workqueue("sysfsd");
Alex Chiangd1102712009-03-25 15:11:36 -0600496 if (sysfs_workqueue == NULL) {
497 module_put(owner);
498 return -ENOMEM;
499 }
500 }
501
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400502 ss = kmalloc(sizeof(*ss), GFP_KERNEL);
Alan Stern523ded72007-04-26 00:12:04 -0700503 if (!ss) {
504 module_put(owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400505 return -ENOMEM;
Alan Stern523ded72007-04-26 00:12:04 -0700506 }
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400507 kobject_get(kobj);
508 ss->kobj = kobj;
509 ss->func = func;
510 ss->data = data;
Alan Stern523ded72007-04-26 00:12:04 -0700511 ss->owner = owner;
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400512 INIT_WORK(&ss->work, sysfs_schedule_callback_work);
Alex Chiang66942062009-03-13 12:07:36 -0600513 INIT_LIST_HEAD(&ss->workq_list);
514 mutex_lock(&sysfs_workq_mutex);
515 list_add_tail(&ss->workq_list, &sysfs_workq);
516 mutex_unlock(&sysfs_workq_mutex);
Alex Chiangd1102712009-03-25 15:11:36 -0600517 queue_work(sysfs_workqueue, &ss->work);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400518 return 0;
519}
520EXPORT_SYMBOL_GPL(sysfs_schedule_callback);