blob: b695b8b229fc85243349b29d88f1ecc5a7e9d3fb [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 Young52e8c2092007-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>
Tejun Heo73d97142013-10-01 17:42:07 -040025#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#include "sysfs.h"
28
Tejun Heo85a4ffa2007-09-20 16:05:12 +090029/*
Tejun Heo58282d82013-10-01 17:41:59 -040030 * There's one sysfs_open_file for each open file and one sysfs_open_dirent
Tejun Heoc75ec762013-10-01 17:41:58 -040031 * for each sysfs_dirent with one or more open files.
Tejun Heo85a4ffa2007-09-20 16:05:12 +090032 *
Tejun Heoc75ec762013-10-01 17:41:58 -040033 * sysfs_dirent->s_attr.open points to sysfs_open_dirent. s_attr.open is
34 * protected by sysfs_open_dirent_lock.
35 *
Tejun Heo13c589d2013-10-01 17:42:02 -040036 * filp->private_data points to seq_file whose ->private points to
37 * sysfs_open_file. sysfs_open_files are chained at
Tejun Heo58282d82013-10-01 17:41:59 -040038 * sysfs_open_dirent->files, which is protected by sysfs_open_file_mutex.
Tejun Heo85a4ffa2007-09-20 16:05:12 +090039 */
Jiri Slabyd7b37882007-11-21 14:55:19 -080040static DEFINE_SPINLOCK(sysfs_open_dirent_lock);
Tejun Heoc75ec762013-10-01 17:41:58 -040041static DEFINE_MUTEX(sysfs_open_file_mutex);
Tejun Heo85a4ffa2007-09-20 16:05:12 +090042
43struct sysfs_open_dirent {
44 atomic_t refcnt;
Tejun Heoa4e8b912007-09-20 16:05:12 +090045 atomic_t event;
46 wait_queue_head_t poll;
Tejun Heo58282d82013-10-01 17:41:59 -040047 struct list_head files; /* goes through sysfs_open_file.list */
Tejun Heo85a4ffa2007-09-20 16:05:12 +090048};
49
Tejun Heo58282d82013-10-01 17:41:59 -040050struct sysfs_open_file {
Tejun Heobcafe4e2013-10-01 17:42:00 -040051 struct sysfs_dirent *sd;
52 struct file *file;
Dave Young52e8c2092007-07-26 11:03:54 +000053 struct mutex mutex;
Tejun Heo73107cb2007-06-14 03:45:16 +090054 int event;
Tejun Heo85a4ffa2007-09-20 16:05:12 +090055 struct list_head list;
Tejun Heo73d97142013-10-01 17:42:07 -040056
57 bool mmapped;
58 const struct vm_operations_struct *vm_ops;
Tejun Heo73107cb2007-06-14 03:45:16 +090059};
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Tejun Heof9b9a622013-10-01 17:42:05 -040061static bool sysfs_is_bin(struct sysfs_dirent *sd)
62{
63 return sysfs_type(sd) == SYSFS_KOBJ_BIN_ATTR;
64}
65
Tejun Heo13c589d2013-10-01 17:42:02 -040066static struct sysfs_open_file *sysfs_of(struct file *file)
67{
68 return ((struct seq_file *)file->private_data)->private;
69}
70
Tejun Heo375b6112013-10-01 17:41:57 -040071/*
72 * Determine ktype->sysfs_ops for the given sysfs_dirent. This function
73 * must be called while holding an active reference.
74 */
75static const struct sysfs_ops *sysfs_file_ops(struct sysfs_dirent *sd)
76{
Tejun Heo7c6e2d32013-11-28 14:54:14 -050077 struct kobject *kobj = sd->s_parent->priv;
Tejun Heo375b6112013-10-01 17:41:57 -040078
Tejun Heo785a1622013-10-14 09:27:11 -040079 if (!sysfs_ignore_lockdep(sd))
80 lockdep_assert_held(sd);
Tejun Heo375b6112013-10-01 17:41:57 -040081 return kobj->ktype ? kobj->ktype->sysfs_ops : NULL;
82}
83
Tejun Heo13c589d2013-10-01 17:42:02 -040084/*
85 * Reads on sysfs are handled through seq_file, which takes care of hairy
86 * details like buffering and seeking. The following function pipes
87 * sysfs_ops->show() result through seq_file.
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 */
Tejun Heoc2b19da2013-11-28 14:54:16 -050089static int sysfs_kf_seq_show(struct seq_file *sf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Tejun Heo13c589d2013-10-01 17:42:02 -040091 struct sysfs_open_file *of = sf->private;
Tejun Heo7c6e2d32013-11-28 14:54:14 -050092 struct kobject *kobj = of->sd->s_parent->priv;
Tejun Heoc2b19da2013-11-28 14:54:16 -050093 const struct sysfs_ops *ops = sysfs_file_ops(of->sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 ssize_t count;
Tejun Heoc2b19da2013-11-28 14:54:16 -050095 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Tejun Heo13c589d2013-10-01 17:42:02 -040097 /* acquire buffer and ensure that it's >= PAGE_SIZE */
98 count = seq_get_buf(sf, &buf);
99 if (count < PAGE_SIZE) {
100 seq_commit(sf, -1);
101 return 0;
102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Tejun Heo13c589d2013-10-01 17:42:02 -0400104 /*
Tejun Heoc2b19da2013-11-28 14:54:16 -0500105 * Invoke show(). Control may reach here via seq file lseek even
106 * if @ops->show() isn't implemented.
Tejun Heo13c589d2013-10-01 17:42:02 -0400107 */
Tejun Heoc2b19da2013-11-28 14:54:16 -0500108 if (ops->show) {
Tejun Heo7c6e2d32013-11-28 14:54:14 -0500109 count = ops->show(kobj, of->sd->priv, buf);
Tejun Heoc2b19da2013-11-28 14:54:16 -0500110 if (count < 0)
111 return count;
112 }
Tejun Heo0ab66082007-06-14 03:45:16 +0900113
Miao Xie8118a852007-11-21 14:55:19 -0800114 /*
115 * The code works fine with PAGE_SIZE return but it's likely to
116 * indicate truncated result or overflow in normal use cases.
117 */
Andrew Morton815d2d52008-03-04 15:09:07 -0800118 if (count >= (ssize_t)PAGE_SIZE) {
119 print_symbol("fill_read_buffer: %s returned bad count\n",
120 (unsigned long)ops->show);
121 /* Try to struggle along */
122 count = PAGE_SIZE - 1;
123 }
Tejun Heo13c589d2013-10-01 17:42:02 -0400124 seq_commit(sf, count);
125 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
Tejun Heoc2b19da2013-11-28 14:54:16 -0500128static ssize_t sysfs_kf_bin_read(struct sysfs_open_file *of, char *buf,
129 size_t count, loff_t pos)
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400130{
Tejun Heo7c6e2d32013-11-28 14:54:14 -0500131 struct bin_attribute *battr = of->sd->priv;
132 struct kobject *kobj = of->sd->s_parent->priv;
Tejun Heoc2b19da2013-11-28 14:54:16 -0500133 loff_t size = file_inode(of->file)->i_size;
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400134
Tejun Heoc2b19da2013-11-28 14:54:16 -0500135 if (!count)
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400136 return 0;
137
138 if (size) {
Tejun Heoc2b19da2013-11-28 14:54:16 -0500139 if (pos > size)
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400140 return 0;
Tejun Heoc2b19da2013-11-28 14:54:16 -0500141 if (pos + count > size)
142 count = size - pos;
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400143 }
144
Tejun Heoc2b19da2013-11-28 14:54:16 -0500145 if (!battr->read)
146 return -EIO;
147
148 return battr->read(of->file, kobj, battr, buf, pos, count);
149}
150
151static void *kernfs_seq_start(struct seq_file *sf, loff_t *ppos)
152{
153 struct sysfs_open_file *of = sf->private;
154
155 /*
156 * @of->mutex nests outside active ref and is just to ensure that
157 * the ops aren't called concurrently for the same open file.
158 */
159 mutex_lock(&of->mutex);
160 if (!sysfs_get_active(of->sd))
161 return ERR_PTR(-ENODEV);
162
163 /*
164 * The same behavior and code as single_open(). Returns !NULL if
165 * pos is at the beginning; otherwise, NULL.
166 */
167 return NULL + !*ppos;
168}
169
170static void *kernfs_seq_next(struct seq_file *sf, void *v, loff_t *ppos)
171{
172 /*
173 * The same behavior and code as single_open(), always terminate
174 * after the initial read.
175 */
176 ++*ppos;
177 return NULL;
178}
179
180static void kernfs_seq_stop(struct seq_file *sf, void *v)
181{
182 struct sysfs_open_file *of = sf->private;
183
184 sysfs_put_active(of->sd);
185 mutex_unlock(&of->mutex);
186}
187
188static int kernfs_seq_show(struct seq_file *sf, void *v)
189{
190 struct sysfs_open_file *of = sf->private;
191
192 of->event = atomic_read(&of->sd->s_attr.open->event);
193
194 return sysfs_kf_seq_show(sf, v);
195}
196
197static const struct seq_operations kernfs_seq_ops = {
198 .start = kernfs_seq_start,
199 .next = kernfs_seq_next,
200 .stop = kernfs_seq_stop,
201 .show = kernfs_seq_show,
202};
203
204/*
205 * As reading a bin file can have side-effects, the exact offset and bytes
206 * specified in read(2) call should be passed to the read callback making
207 * it difficult to use seq_file. Implement simplistic custom buffering for
208 * bin files.
209 */
210static ssize_t kernfs_file_direct_read(struct sysfs_open_file *of,
211 char __user *user_buf, size_t count,
212 loff_t *ppos)
213{
214 ssize_t len = min_t(size_t, count, PAGE_SIZE);
215 char *buf;
216
217 buf = kmalloc(len, GFP_KERNEL);
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400218 if (!buf)
219 return -ENOMEM;
220
Tejun Heoc2b19da2013-11-28 14:54:16 -0500221 /*
222 * @of->mutex nests outside active ref and is just to ensure that
223 * the ops aren't called concurrently for the same open file.
224 */
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400225 mutex_lock(&of->mutex);
226 if (!sysfs_get_active(of->sd)) {
Tejun Heoc2b19da2013-11-28 14:54:16 -0500227 len = -ENODEV;
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400228 mutex_unlock(&of->mutex);
229 goto out_free;
230 }
231
Tejun Heoc2b19da2013-11-28 14:54:16 -0500232 len = sysfs_kf_bin_read(of, buf, len, *ppos);
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400233
234 sysfs_put_active(of->sd);
235 mutex_unlock(&of->mutex);
236
Tejun Heoc2b19da2013-11-28 14:54:16 -0500237 if (len < 0)
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400238 goto out_free;
239
Tejun Heoc2b19da2013-11-28 14:54:16 -0500240 if (copy_to_user(user_buf, buf, len)) {
241 len = -EFAULT;
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400242 goto out_free;
243 }
244
Tejun Heoc2b19da2013-11-28 14:54:16 -0500245 *ppos += len;
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400246
247 out_free:
248 kfree(buf);
Tejun Heoc2b19da2013-11-28 14:54:16 -0500249 return len;
250}
251
252/**
253 * kernfs_file_read - kernfs vfs read callback
254 * @file: file pointer
255 * @user_buf: data to write
256 * @count: number of bytes
257 * @ppos: starting offset
258 */
259static ssize_t kernfs_file_read(struct file *file, char __user *user_buf,
260 size_t count, loff_t *ppos)
261{
262 struct sysfs_open_file *of = sysfs_of(file);
263
264 if (sysfs_is_bin(of->sd))
265 return kernfs_file_direct_read(of, user_buf, count, ppos);
266 else
267 return seq_read(file, user_buf, count, ppos);
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400268}
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270/**
Tejun Heo8ef445f2013-10-01 17:42:01 -0400271 * flush_write_buffer - push buffer to kobject
272 * @of: open file
273 * @buf: data buffer for file
Tejun Heof9b9a622013-10-01 17:42:05 -0400274 * @off: file offset to write to
Tejun Heo8ef445f2013-10-01 17:42:01 -0400275 * @count: number of bytes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 *
Tejun Heo8ef445f2013-10-01 17:42:01 -0400277 * Get the correct pointers for the kobject and the attribute we're dealing
278 * with, then call the store() method for it with @buf.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 */
Tejun Heof9b9a622013-10-01 17:42:05 -0400280static int flush_write_buffer(struct sysfs_open_file *of, char *buf, loff_t off,
Tejun Heo8ef445f2013-10-01 17:42:01 -0400281 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
Tejun Heo7c6e2d32013-11-28 14:54:14 -0500283 struct kobject *kobj = of->sd->s_parent->priv;
Tejun Heo8ef445f2013-10-01 17:42:01 -0400284 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Tejun Heo8ef445f2013-10-01 17:42:01 -0400286 /*
287 * Need @of->sd for attr and ops, its parent for kobj. @of->mutex
288 * nests outside active ref and is just to ensure that the ops
289 * aren't called concurrently for the same open file.
290 */
291 mutex_lock(&of->mutex);
292 if (!sysfs_get_active(of->sd)) {
293 mutex_unlock(&of->mutex);
Tejun Heo0ab66082007-06-14 03:45:16 +0900294 return -ENODEV;
Tejun Heo8ef445f2013-10-01 17:42:01 -0400295 }
Tejun Heo0ab66082007-06-14 03:45:16 +0900296
Tejun Heof9b9a622013-10-01 17:42:05 -0400297 if (sysfs_is_bin(of->sd)) {
Tejun Heo7c6e2d32013-11-28 14:54:14 -0500298 struct bin_attribute *battr = of->sd->priv;
Tejun Heof9b9a622013-10-01 17:42:05 -0400299
300 rc = -EIO;
301 if (battr->write)
302 rc = battr->write(of->file, kobj, battr, buf, off,
303 count);
304 } else {
305 const struct sysfs_ops *ops = sysfs_file_ops(of->sd);
306
Tejun Heo7c6e2d32013-11-28 14:54:14 -0500307 rc = ops->store(kobj, of->sd->priv, buf, count);
Tejun Heof9b9a622013-10-01 17:42:05 -0400308 }
Tejun Heo0ab66082007-06-14 03:45:16 +0900309
Tejun Heobcafe4e2013-10-01 17:42:00 -0400310 sysfs_put_active(of->sd);
Tejun Heo8ef445f2013-10-01 17:42:01 -0400311 mutex_unlock(&of->mutex);
Tejun Heo0ab66082007-06-14 03:45:16 +0900312
313 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316/**
Tejun Heo8ef445f2013-10-01 17:42:01 -0400317 * sysfs_write_file - write an attribute
318 * @file: file pointer
319 * @user_buf: data to write
320 * @count: number of bytes
321 * @ppos: starting offset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 *
Tejun Heo8ef445f2013-10-01 17:42:01 -0400323 * Copy data in from userland and pass it to the matching
324 * sysfs_ops->store() by invoking flush_write_buffer().
325 *
326 * There is no easy way for us to know if userspace is only doing a partial
327 * write, so we don't support them. We expect the entire buffer to come on
328 * the first write. Hint: if you're writing a value, first read the file,
329 * modify only the the value you're changing, then write entire buffer
330 * back.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 */
Tejun Heo8ef445f2013-10-01 17:42:01 -0400332static ssize_t sysfs_write_file(struct file *file, const char __user *user_buf,
Greg Kroah-Hartmanddfd6d02013-08-21 16:33:34 -0700333 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Tejun Heo13c589d2013-10-01 17:42:02 -0400335 struct sysfs_open_file *of = sysfs_of(file);
Tejun Heof9b9a622013-10-01 17:42:05 -0400336 ssize_t len = min_t(size_t, count, PAGE_SIZE);
Ming Leib9c06222013-10-23 21:44:53 +0800337 loff_t size = file_inode(file)->i_size;
Tejun Heo8ef445f2013-10-01 17:42:01 -0400338 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Ming Leib9c06222013-10-23 21:44:53 +0800340 if (sysfs_is_bin(of->sd) && size) {
Tejun Heof9b9a622013-10-01 17:42:05 -0400341 if (size <= *ppos)
342 return 0;
343 len = min_t(ssize_t, len, size - *ppos);
344 }
345
Tejun Heo8ef445f2013-10-01 17:42:01 -0400346 if (!len)
347 return 0;
348
349 buf = kmalloc(len + 1, GFP_KERNEL);
350 if (!buf)
351 return -ENOMEM;
352
353 if (copy_from_user(buf, user_buf, len)) {
354 len = -EFAULT;
355 goto out_free;
356 }
357 buf[len] = '\0'; /* guarantee string termination */
358
Tejun Heof9b9a622013-10-01 17:42:05 -0400359 len = flush_write_buffer(of, buf, *ppos, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (len > 0)
361 *ppos += len;
Tejun Heo8ef445f2013-10-01 17:42:01 -0400362out_free:
363 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return len;
365}
366
Tejun Heo73d97142013-10-01 17:42:07 -0400367static void sysfs_bin_vma_open(struct vm_area_struct *vma)
368{
369 struct file *file = vma->vm_file;
370 struct sysfs_open_file *of = sysfs_of(file);
371
372 if (!of->vm_ops)
373 return;
374
375 if (!sysfs_get_active(of->sd))
376 return;
377
378 if (of->vm_ops->open)
379 of->vm_ops->open(vma);
380
381 sysfs_put_active(of->sd);
382}
383
384static int sysfs_bin_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
385{
386 struct file *file = vma->vm_file;
387 struct sysfs_open_file *of = sysfs_of(file);
388 int ret;
389
390 if (!of->vm_ops)
391 return VM_FAULT_SIGBUS;
392
393 if (!sysfs_get_active(of->sd))
394 return VM_FAULT_SIGBUS;
395
396 ret = VM_FAULT_SIGBUS;
397 if (of->vm_ops->fault)
398 ret = of->vm_ops->fault(vma, vmf);
399
400 sysfs_put_active(of->sd);
401 return ret;
402}
403
404static int sysfs_bin_page_mkwrite(struct vm_area_struct *vma,
405 struct vm_fault *vmf)
406{
407 struct file *file = vma->vm_file;
408 struct sysfs_open_file *of = sysfs_of(file);
409 int ret;
410
411 if (!of->vm_ops)
412 return VM_FAULT_SIGBUS;
413
414 if (!sysfs_get_active(of->sd))
415 return VM_FAULT_SIGBUS;
416
417 ret = 0;
418 if (of->vm_ops->page_mkwrite)
419 ret = of->vm_ops->page_mkwrite(vma, vmf);
420 else
421 file_update_time(file);
422
423 sysfs_put_active(of->sd);
424 return ret;
425}
426
427static int sysfs_bin_access(struct vm_area_struct *vma, unsigned long addr,
428 void *buf, int len, int write)
429{
430 struct file *file = vma->vm_file;
431 struct sysfs_open_file *of = sysfs_of(file);
432 int ret;
433
434 if (!of->vm_ops)
435 return -EINVAL;
436
437 if (!sysfs_get_active(of->sd))
438 return -EINVAL;
439
440 ret = -EINVAL;
441 if (of->vm_ops->access)
442 ret = of->vm_ops->access(vma, addr, buf, len, write);
443
444 sysfs_put_active(of->sd);
445 return ret;
446}
447
448#ifdef CONFIG_NUMA
449static int sysfs_bin_set_policy(struct vm_area_struct *vma,
450 struct mempolicy *new)
451{
452 struct file *file = vma->vm_file;
453 struct sysfs_open_file *of = sysfs_of(file);
454 int ret;
455
456 if (!of->vm_ops)
457 return 0;
458
459 if (!sysfs_get_active(of->sd))
460 return -EINVAL;
461
462 ret = 0;
463 if (of->vm_ops->set_policy)
464 ret = of->vm_ops->set_policy(vma, new);
465
466 sysfs_put_active(of->sd);
467 return ret;
468}
469
470static struct mempolicy *sysfs_bin_get_policy(struct vm_area_struct *vma,
471 unsigned long addr)
472{
473 struct file *file = vma->vm_file;
474 struct sysfs_open_file *of = sysfs_of(file);
475 struct mempolicy *pol;
476
477 if (!of->vm_ops)
478 return vma->vm_policy;
479
480 if (!sysfs_get_active(of->sd))
481 return vma->vm_policy;
482
483 pol = vma->vm_policy;
484 if (of->vm_ops->get_policy)
485 pol = of->vm_ops->get_policy(vma, addr);
486
487 sysfs_put_active(of->sd);
488 return pol;
489}
490
491static int sysfs_bin_migrate(struct vm_area_struct *vma, const nodemask_t *from,
492 const nodemask_t *to, unsigned long flags)
493{
494 struct file *file = vma->vm_file;
495 struct sysfs_open_file *of = sysfs_of(file);
496 int ret;
497
498 if (!of->vm_ops)
499 return 0;
500
501 if (!sysfs_get_active(of->sd))
502 return 0;
503
504 ret = 0;
505 if (of->vm_ops->migrate)
506 ret = of->vm_ops->migrate(vma, from, to, flags);
507
508 sysfs_put_active(of->sd);
509 return ret;
510}
511#endif
512
513static const struct vm_operations_struct sysfs_bin_vm_ops = {
514 .open = sysfs_bin_vma_open,
515 .fault = sysfs_bin_fault,
516 .page_mkwrite = sysfs_bin_page_mkwrite,
517 .access = sysfs_bin_access,
518#ifdef CONFIG_NUMA
519 .set_policy = sysfs_bin_set_policy,
520 .get_policy = sysfs_bin_get_policy,
521 .migrate = sysfs_bin_migrate,
522#endif
523};
524
525static int sysfs_bin_mmap(struct file *file, struct vm_area_struct *vma)
526{
527 struct sysfs_open_file *of = sysfs_of(file);
Tejun Heo7c6e2d32013-11-28 14:54:14 -0500528 struct bin_attribute *battr = of->sd->priv;
529 struct kobject *kobj = of->sd->s_parent->priv;
Tejun Heo73d97142013-10-01 17:42:07 -0400530 int rc;
531
532 mutex_lock(&of->mutex);
533
534 /* need of->sd for battr, its parent for kobj */
535 rc = -ENODEV;
536 if (!sysfs_get_active(of->sd))
537 goto out_unlock;
538
Tejun Heo73d97142013-10-01 17:42:07 -0400539 if (!battr->mmap)
540 goto out_put;
541
542 rc = battr->mmap(file, kobj, battr, vma);
543 if (rc)
544 goto out_put;
545
546 /*
547 * PowerPC's pci_mmap of legacy_mem uses shmem_zero_setup()
548 * to satisfy versions of X which crash if the mmap fails: that
549 * substitutes a new vm_file, and we don't then want bin_vm_ops.
550 */
551 if (vma->vm_file != file)
552 goto out_put;
553
554 rc = -EINVAL;
555 if (of->mmapped && of->vm_ops != vma->vm_ops)
556 goto out_put;
557
558 /*
559 * It is not possible to successfully wrap close.
560 * So error if someone is trying to use close.
561 */
562 rc = -EINVAL;
563 if (vma->vm_ops && vma->vm_ops->close)
564 goto out_put;
565
566 rc = 0;
567 of->mmapped = 1;
568 of->vm_ops = vma->vm_ops;
569 vma->vm_ops = &sysfs_bin_vm_ops;
570out_put:
571 sysfs_put_active(of->sd);
572out_unlock:
573 mutex_unlock(&of->mutex);
574
575 return rc;
576}
577
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900578/**
579 * sysfs_get_open_dirent - get or create sysfs_open_dirent
580 * @sd: target sysfs_dirent
Tejun Heo58282d82013-10-01 17:41:59 -0400581 * @of: sysfs_open_file for this instance of open
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900582 *
583 * If @sd->s_attr.open exists, increment its reference count;
Tejun Heo58282d82013-10-01 17:41:59 -0400584 * otherwise, create one. @of is chained to the files list.
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900585 *
586 * LOCKING:
587 * Kernel thread context (may sleep).
588 *
589 * RETURNS:
590 * 0 on success, -errno on failure.
591 */
592static int sysfs_get_open_dirent(struct sysfs_dirent *sd,
Tejun Heo58282d82013-10-01 17:41:59 -0400593 struct sysfs_open_file *of)
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900594{
595 struct sysfs_open_dirent *od, *new_od = NULL;
596
597 retry:
Tejun Heoc75ec762013-10-01 17:41:58 -0400598 mutex_lock(&sysfs_open_file_mutex);
Neil Brown83db93f2009-09-15 16:05:51 -0700599 spin_lock_irq(&sysfs_open_dirent_lock);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900600
601 if (!sd->s_attr.open && new_od) {
602 sd->s_attr.open = new_od;
603 new_od = NULL;
604 }
605
606 od = sd->s_attr.open;
607 if (od) {
608 atomic_inc(&od->refcnt);
Tejun Heo58282d82013-10-01 17:41:59 -0400609 list_add_tail(&of->list, &od->files);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900610 }
611
Neil Brown83db93f2009-09-15 16:05:51 -0700612 spin_unlock_irq(&sysfs_open_dirent_lock);
Tejun Heoc75ec762013-10-01 17:41:58 -0400613 mutex_unlock(&sysfs_open_file_mutex);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900614
615 if (od) {
616 kfree(new_od);
617 return 0;
618 }
619
620 /* not there, initialize a new one and retry */
621 new_od = kmalloc(sizeof(*new_od), GFP_KERNEL);
622 if (!new_od)
623 return -ENOMEM;
624
625 atomic_set(&new_od->refcnt, 0);
Tejun Heoa4e8b912007-09-20 16:05:12 +0900626 atomic_set(&new_od->event, 1);
627 init_waitqueue_head(&new_od->poll);
Tejun Heo58282d82013-10-01 17:41:59 -0400628 INIT_LIST_HEAD(&new_od->files);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900629 goto retry;
630}
631
632/**
633 * sysfs_put_open_dirent - put sysfs_open_dirent
634 * @sd: target sysfs_dirent
Tejun Heo58282d82013-10-01 17:41:59 -0400635 * @of: associated sysfs_open_file
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900636 *
Tejun Heo58282d82013-10-01 17:41:59 -0400637 * Put @sd->s_attr.open and unlink @of from the files list. If
638 * reference count reaches zero, disassociate and free it.
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900639 *
640 * LOCKING:
641 * None.
642 */
643static void sysfs_put_open_dirent(struct sysfs_dirent *sd,
Tejun Heo58282d82013-10-01 17:41:59 -0400644 struct sysfs_open_file *of)
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900645{
646 struct sysfs_open_dirent *od = sd->s_attr.open;
Neil Brown83db93f2009-09-15 16:05:51 -0700647 unsigned long flags;
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900648
Tejun Heoc75ec762013-10-01 17:41:58 -0400649 mutex_lock(&sysfs_open_file_mutex);
Neil Brown83db93f2009-09-15 16:05:51 -0700650 spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900651
Tejun Heo73d97142013-10-01 17:42:07 -0400652 if (of)
653 list_del(&of->list);
654
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900655 if (atomic_dec_and_test(&od->refcnt))
656 sd->s_attr.open = NULL;
657 else
658 od = NULL;
659
Neil Brown83db93f2009-09-15 16:05:51 -0700660 spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
Tejun Heoc75ec762013-10-01 17:41:58 -0400661 mutex_unlock(&sysfs_open_file_mutex);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900662
663 kfree(od);
664}
665
Oliver Neukum94bebf42006-12-20 10:52:44 +0100666static int sysfs_open_file(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Tejun Heo3e519032007-06-14 03:45:15 +0900668 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
Tejun Heo7c6e2d32013-11-28 14:54:14 -0500669 struct kobject *kobj = attr_sd->s_parent->priv;
Tejun Heo58282d82013-10-01 17:41:59 -0400670 struct sysfs_open_file *of;
Tejun Heo027a4852013-11-17 11:17:36 +0900671 bool has_read, has_write, has_mmap;
Kay Sievers000f2a42007-11-02 13:47:53 +0100672 int error = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Tejun Heo0ab66082007-06-14 03:45:16 +0900674 /* need attr_sd for attr and ops, its parent for kobj */
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800675 if (!sysfs_get_active(attr_sd))
Tejun Heo0ab66082007-06-14 03:45:16 +0900676 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Tejun Heo49fe6042013-10-01 17:42:08 -0400678 if (sysfs_is_bin(attr_sd)) {
Tejun Heo7c6e2d32013-11-28 14:54:14 -0500679 struct bin_attribute *battr = attr_sd->priv;
Tejun Heo49fe6042013-10-01 17:42:08 -0400680
681 has_read = battr->read || battr->mmap;
682 has_write = battr->write || battr->mmap;
Tejun Heo027a4852013-11-17 11:17:36 +0900683 has_mmap = battr->mmap;
Tejun Heo49fe6042013-10-01 17:42:08 -0400684 } else {
685 const struct sysfs_ops *ops = sysfs_file_ops(attr_sd);
686
687 /* every kobject with an attribute needs a ktype assigned */
688 if (WARN(!ops, KERN_ERR
689 "missing sysfs attribute operations for kobject: %s\n",
690 kobject_name(kobj)))
691 goto err_out;
692
693 has_read = ops->show;
694 has_write = ops->store;
Tejun Heo027a4852013-11-17 11:17:36 +0900695 has_mmap = false;
Tejun Heo49fe6042013-10-01 17:42:08 -0400696 }
697
698 /* check perms and supported operations */
699 if ((file->f_mode & FMODE_WRITE) &&
700 (!(inode->i_mode & S_IWUGO) || !has_write))
Tejun Heo7b595752007-06-14 03:45:17 +0900701 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Tejun Heo49fe6042013-10-01 17:42:08 -0400703 if ((file->f_mode & FMODE_READ) &&
704 (!(inode->i_mode & S_IRUGO) || !has_read))
705 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Tejun Heo13c589d2013-10-01 17:42:02 -0400707 /* allocate a sysfs_open_file for the file */
Tejun Heo0ab66082007-06-14 03:45:16 +0900708 error = -ENOMEM;
Tejun Heo58282d82013-10-01 17:41:59 -0400709 of = kzalloc(sizeof(struct sysfs_open_file), GFP_KERNEL);
710 if (!of)
Tejun Heo7b595752007-06-14 03:45:17 +0900711 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Tejun Heo027a4852013-11-17 11:17:36 +0900713 /*
714 * The following is done to give a different lockdep key to
715 * @of->mutex for files which implement mmap. This is a rather
716 * crude way to avoid false positive lockdep warning around
717 * mm->mmap_sem - mmap nests @of->mutex under mm->mmap_sem and
718 * reading /sys/block/sda/trace/act_mask grabs sr_mutex, under
719 * which mm->mmap_sem nests, while holding @of->mutex. As each
720 * open file has a separate mutex, it's okay as long as those don't
721 * happen on the same file. At this point, we can't easily give
722 * each file a separate locking class. Let's differentiate on
723 * whether the file has mmap or not for now.
724 */
725 if (has_mmap)
726 mutex_init(&of->mutex);
727 else
728 mutex_init(&of->mutex);
729
Tejun Heobcafe4e2013-10-01 17:42:00 -0400730 of->sd = attr_sd;
731 of->file = file;
Tejun Heo13c589d2013-10-01 17:42:02 -0400732
733 /*
Tejun Heo49fe6042013-10-01 17:42:08 -0400734 * Always instantiate seq_file even if read access doesn't use
735 * seq_file or is not requested. This unifies private data access
736 * and readable regular files are the vast majority anyway.
Tejun Heo13c589d2013-10-01 17:42:02 -0400737 */
Tejun Heo49fe6042013-10-01 17:42:08 -0400738 if (sysfs_is_bin(attr_sd))
Tejun Heoc2b19da2013-11-28 14:54:16 -0500739 error = seq_open(file, NULL);
Tejun Heo49fe6042013-10-01 17:42:08 -0400740 else
Tejun Heoc2b19da2013-11-28 14:54:16 -0500741 error = seq_open(file, &kernfs_seq_ops);
Tejun Heo13c589d2013-10-01 17:42:02 -0400742 if (error)
743 goto err_free;
744
Tejun Heoc2b19da2013-11-28 14:54:16 -0500745 ((struct seq_file *)file->private_data)->private = of;
746
Tejun Heo13c589d2013-10-01 17:42:02 -0400747 /* seq_file clears PWRITE unconditionally, restore it if WRITE */
748 if (file->f_mode & FMODE_WRITE)
749 file->f_mode |= FMODE_PWRITE;
Tejun Heo0ab66082007-06-14 03:45:16 +0900750
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900751 /* make sure we have open dirent struct */
Tejun Heo58282d82013-10-01 17:41:59 -0400752 error = sysfs_get_open_dirent(attr_sd, of);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900753 if (error)
Tejun Heo13c589d2013-10-01 17:42:02 -0400754 goto err_close;
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900755
Tejun Heob05f0542007-09-20 16:05:10 +0900756 /* open succeeded, put active references */
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800757 sysfs_put_active(attr_sd);
Tejun Heo0ab66082007-06-14 03:45:16 +0900758 return 0;
759
Tejun Heo13c589d2013-10-01 17:42:02 -0400760err_close:
Tejun Heoc2b19da2013-11-28 14:54:16 -0500761 seq_release(inode, file);
Tejun Heo13c589d2013-10-01 17:42:02 -0400762err_free:
Tejun Heo58282d82013-10-01 17:41:59 -0400763 kfree(of);
Tejun Heo13c589d2013-10-01 17:42:02 -0400764err_out:
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800765 sysfs_put_active(attr_sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 return error;
767}
768
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900769static int sysfs_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900771 struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata;
Tejun Heo13c589d2013-10-01 17:42:02 -0400772 struct sysfs_open_file *of = sysfs_of(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Tejun Heo58282d82013-10-01 17:41:59 -0400774 sysfs_put_open_dirent(sd, of);
Tejun Heoc2b19da2013-11-28 14:54:16 -0500775 seq_release(inode, filp);
Tejun Heo58282d82013-10-01 17:41:59 -0400776 kfree(of);
Tejun Heo50ab1a72007-09-20 16:05:10 +0900777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 return 0;
779}
780
Tejun Heo73d97142013-10-01 17:42:07 -0400781void sysfs_unmap_bin_file(struct sysfs_dirent *sd)
782{
783 struct sysfs_open_dirent *od;
784 struct sysfs_open_file *of;
785
786 if (!sysfs_is_bin(sd))
787 return;
788
789 spin_lock_irq(&sysfs_open_dirent_lock);
790 od = sd->s_attr.open;
791 if (od)
792 atomic_inc(&od->refcnt);
793 spin_unlock_irq(&sysfs_open_dirent_lock);
794 if (!od)
795 return;
796
797 mutex_lock(&sysfs_open_file_mutex);
798 list_for_each_entry(of, &od->files, list) {
799 struct inode *inode = file_inode(of->file);
800 unmap_mapping_range(inode->i_mapping, 0, 0, 1);
801 }
802 mutex_unlock(&sysfs_open_file_mutex);
803
804 sysfs_put_open_dirent(sd, NULL);
805}
806
NeilBrown4508a7a2006-03-20 17:53:53 +1100807/* Sysfs attribute files are pollable. The idea is that you read
808 * the content and then you use 'poll' or 'select' to wait for
809 * the content to change. When the content changes (assuming the
810 * manager for the kobject supports notification), poll will
811 * return POLLERR|POLLPRI, and select will return the fd whether
812 * it is waiting for read, write, or exceptions.
813 * Once poll/select indicates that the value has changed, you
Dan Williams2424b5d2008-04-07 15:35:01 -0700814 * need to close and re-open the file, or seek to 0 and read again.
NeilBrown4508a7a2006-03-20 17:53:53 +1100815 * Reminder: this only works for attributes which actively support
816 * it, and it is not possible to test an attribute from userspace
Rolf Eike Beera93720e2007-08-10 13:51:07 -0700817 * to see if it supports poll (Neither 'poll' nor 'select' return
NeilBrown4508a7a2006-03-20 17:53:53 +1100818 * an appropriate error code). When in doubt, set a suitable timeout value.
819 */
820static unsigned int sysfs_poll(struct file *filp, poll_table *wait)
821{
Tejun Heo13c589d2013-10-01 17:42:02 -0400822 struct sysfs_open_file *of = sysfs_of(filp);
Tejun Heo0ab66082007-06-14 03:45:16 +0900823 struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata;
Tejun Heoa4e8b912007-09-20 16:05:12 +0900824 struct sysfs_open_dirent *od = attr_sd->s_attr.open;
Tejun Heo0ab66082007-06-14 03:45:16 +0900825
826 /* need parent for the kobj, grab both */
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800827 if (!sysfs_get_active(attr_sd))
Tejun Heo0ab66082007-06-14 03:45:16 +0900828 goto trigger;
NeilBrown4508a7a2006-03-20 17:53:53 +1100829
Tejun Heoa4e8b912007-09-20 16:05:12 +0900830 poll_wait(filp, &od->poll, wait);
NeilBrown4508a7a2006-03-20 17:53:53 +1100831
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800832 sysfs_put_active(attr_sd);
NeilBrown4508a7a2006-03-20 17:53:53 +1100833
Tejun Heo58282d82013-10-01 17:41:59 -0400834 if (of->event != atomic_read(&od->event))
Tejun Heo0ab66082007-06-14 03:45:16 +0900835 goto trigger;
836
KOSAKI Motohiro1af35572009-04-09 13:53:22 +0900837 return DEFAULT_POLLMASK;
Tejun Heo0ab66082007-06-14 03:45:16 +0900838
839 trigger:
KOSAKI Motohiro1af35572009-04-09 13:53:22 +0900840 return DEFAULT_POLLMASK|POLLERR|POLLPRI;
NeilBrown4508a7a2006-03-20 17:53:53 +1100841}
842
Neil Brownf1282c82008-07-16 08:58:04 +1000843void sysfs_notify_dirent(struct sysfs_dirent *sd)
844{
845 struct sysfs_open_dirent *od;
Neil Brown83db93f2009-09-15 16:05:51 -0700846 unsigned long flags;
Neil Brownf1282c82008-07-16 08:58:04 +1000847
Neil Brown83db93f2009-09-15 16:05:51 -0700848 spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
Neil Brownf1282c82008-07-16 08:58:04 +1000849
Nick Dyerfc60bb82013-06-07 15:45:13 +0100850 if (!WARN_ON(sysfs_type(sd) != SYSFS_KOBJ_ATTR)) {
851 od = sd->s_attr.open;
852 if (od) {
853 atomic_inc(&od->event);
854 wake_up_interruptible(&od->poll);
855 }
Neil Brownf1282c82008-07-16 08:58:04 +1000856 }
857
Neil Brown83db93f2009-09-15 16:05:51 -0700858 spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
Neil Brownf1282c82008-07-16 08:58:04 +1000859}
860EXPORT_SYMBOL_GPL(sysfs_notify_dirent);
861
Trent Piepho8c0e3992008-09-25 16:45:13 -0700862void sysfs_notify(struct kobject *k, const char *dir, const char *attr)
NeilBrown4508a7a2006-03-20 17:53:53 +1100863{
Tejun Heo51225032007-06-14 04:27:25 +0900864 struct sysfs_dirent *sd = k->sd;
NeilBrown4508a7a2006-03-20 17:53:53 +1100865
Tejun Heo51225032007-06-14 04:27:25 +0900866 mutex_lock(&sysfs_mutex);
NeilBrown4508a7a2006-03-20 17:53:53 +1100867
Tejun Heo51225032007-06-14 04:27:25 +0900868 if (sd && dir)
Tejun Heocfec0bc2013-09-11 22:29:09 -0400869 sd = sysfs_find_dirent(sd, dir, NULL);
Tejun Heo51225032007-06-14 04:27:25 +0900870 if (sd && attr)
Tejun Heocfec0bc2013-09-11 22:29:09 -0400871 sd = sysfs_find_dirent(sd, attr, NULL);
Neil Brownf1282c82008-07-16 08:58:04 +1000872 if (sd)
873 sysfs_notify_dirent(sd);
Tejun Heo51225032007-06-14 04:27:25 +0900874
875 mutex_unlock(&sysfs_mutex);
NeilBrown4508a7a2006-03-20 17:53:53 +1100876}
877EXPORT_SYMBOL_GPL(sysfs_notify);
878
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800879const struct file_operations sysfs_file_operations = {
Tejun Heoc2b19da2013-11-28 14:54:16 -0500880 .read = kernfs_file_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 .write = sysfs_write_file,
Tejun Heo044e3bc2013-11-01 13:16:53 -0400882 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 .open = sysfs_open_file,
884 .release = sysfs_release,
NeilBrown4508a7a2006-03-20 17:53:53 +1100885 .poll = sysfs_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886};
887
Tejun Heof9b9a622013-10-01 17:42:05 -0400888const struct file_operations sysfs_bin_operations = {
Tejun Heoc2b19da2013-11-28 14:54:16 -0500889 .read = kernfs_file_read,
Tejun Heof9b9a622013-10-01 17:42:05 -0400890 .write = sysfs_write_file,
891 .llseek = generic_file_llseek,
Tejun Heo73d97142013-10-01 17:42:07 -0400892 .mmap = sysfs_bin_mmap,
Tejun Heo49fe6042013-10-01 17:42:08 -0400893 .open = sysfs_open_file,
894 .release = sysfs_release,
895 .poll = sysfs_poll,
Tejun Heof9b9a622013-10-01 17:42:05 -0400896};
897
Tejun Heo58292cbe2013-09-11 22:29:04 -0400898int sysfs_add_file_mode_ns(struct sysfs_dirent *dir_sd,
899 const struct attribute *attr, int type,
900 umode_t amode, const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
James Bottomley0f423892008-03-20 20:47:52 -0500902 umode_t mode = (amode & S_IALLUGO) | S_IFREG;
Tejun Heofb6896da2007-06-14 04:27:24 +0900903 struct sysfs_addrm_cxt acxt;
Tejun Heoa26cd722007-06-14 03:45:14 +0900904 struct sysfs_dirent *sd;
Tejun Heo23dc2792007-08-02 21:38:03 +0900905 int rc;
Tejun Heoa26cd722007-06-14 03:45:14 +0900906
Tejun Heo3e519032007-06-14 03:45:15 +0900907 sd = sysfs_new_dirent(attr->name, mode, type);
Tejun Heo3007e992007-06-14 04:27:23 +0900908 if (!sd)
909 return -ENOMEM;
Eric W. Biederman487505c2011-10-12 21:53:38 +0000910
911 sd->s_ns = ns;
Tejun Heo7c6e2d32013-11-28 14:54:14 -0500912 sd->priv = (void *)attr;
Eric W. Biedermana2db6842010-02-11 15:20:00 -0800913 sysfs_dirent_init_lockdep(sd);
Tejun Heoa26cd722007-06-14 03:45:14 +0900914
Tejun Heod69ac5a2013-09-18 17:15:35 -0400915 sysfs_addrm_start(&acxt);
916 rc = sysfs_add_one(&acxt, sd, dir_sd);
Tejun Heo23dc2792007-08-02 21:38:03 +0900917 sysfs_addrm_finish(&acxt);
Tejun Heo3007e992007-06-14 04:27:23 +0900918
Tejun Heo23dc2792007-08-02 21:38:03 +0900919 if (rc)
Tejun Heo967e35d2007-07-18 16:38:11 +0900920 sysfs_put(sd);
Tejun Heo3007e992007-06-14 04:27:23 +0900921
Tejun Heo23dc2792007-08-02 21:38:03 +0900922 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923}
924
925
James Bottomley0f423892008-03-20 20:47:52 -0500926int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,
927 int type)
928{
Tejun Heo58292cbe2013-09-11 22:29:04 -0400929 return sysfs_add_file_mode_ns(dir_sd, attr, type, attr->mode, NULL);
James Bottomley0f423892008-03-20 20:47:52 -0500930}
931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932/**
Tejun Heo58292cbe2013-09-11 22:29:04 -0400933 * sysfs_create_file_ns - create an attribute file for an object with custom ns
934 * @kobj: object we're creating for
935 * @attr: attribute descriptor
936 * @ns: namespace the new file should belong to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 */
Tejun Heo58292cbe2013-09-11 22:29:04 -0400938int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
939 const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
Tejun Heo608e2662007-06-14 04:27:22 +0900941 BUG_ON(!kobj || !kobj->sd || !attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Tejun Heo58292cbe2013-09-11 22:29:04 -0400943 return sysfs_add_file_mode_ns(kobj->sd, attr, SYSFS_KOBJ_ATTR,
944 attr->mode, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946}
Tejun Heo58292cbe2013-09-11 22:29:04 -0400947EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Andi Kleen1c205ae2010-01-05 12:48:01 +0100949int sysfs_create_files(struct kobject *kobj, const struct attribute **ptr)
950{
951 int err = 0;
952 int i;
953
954 for (i = 0; ptr[i] && !err; i++)
955 err = sysfs_create_file(kobj, ptr[i]);
956 if (err)
957 while (--i >= 0)
958 sysfs_remove_file(kobj, ptr[i]);
959 return err;
960}
Greg Kroah-Hartman1b866752013-08-21 16:17:47 -0700961EXPORT_SYMBOL_GPL(sysfs_create_files);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
963/**
Alan Sterndfa87c82007-02-20 15:02:44 -0500964 * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
965 * @kobj: object we're acting for.
966 * @attr: attribute descriptor.
967 * @group: group name.
968 */
969int sysfs_add_file_to_group(struct kobject *kobj,
970 const struct attribute *attr, const char *group)
971{
Tejun Heo608e2662007-06-14 04:27:22 +0900972 struct sysfs_dirent *dir_sd;
Alan Sterndfa87c82007-02-20 15:02:44 -0500973 int error;
974
James Bottomley11f24fb2008-01-02 18:44:05 -0600975 if (group)
Tejun Heo388975c2013-09-11 23:19:13 -0400976 dir_sd = sysfs_get_dirent(kobj->sd, group);
James Bottomley11f24fb2008-01-02 18:44:05 -0600977 else
978 dir_sd = sysfs_get(kobj->sd);
979
Tejun Heo608e2662007-06-14 04:27:22 +0900980 if (!dir_sd)
981 return -ENOENT;
982
983 error = sysfs_add_file(dir_sd, attr, SYSFS_KOBJ_ATTR);
984 sysfs_put(dir_sd);
985
Alan Sterndfa87c82007-02-20 15:02:44 -0500986 return error;
987}
988EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990/**
Kay Sievers31e5abe2005-04-18 21:57:32 -0700991 * sysfs_chmod_file - update the modified mode value on an object attribute.
992 * @kobj: object we're acting for.
993 * @attr: attribute descriptor.
994 * @mode: file permissions.
995 *
996 */
Jean Delvare49c19402010-07-02 16:54:05 +0200997int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
Al Viro48176a92011-07-24 03:40:40 -0400998 umode_t mode)
Kay Sievers31e5abe2005-04-18 21:57:32 -0700999{
Eric W. Biederman06fc0d62009-11-20 16:08:54 -08001000 struct sysfs_dirent *sd;
Maneesh Sonibc062b12005-07-29 12:13:35 -07001001 struct iattr newattrs;
Tejun Heo51225032007-06-14 04:27:25 +09001002 int rc;
Kay Sievers31e5abe2005-04-18 21:57:32 -07001003
Tejun Heo5d604182013-11-23 17:21:52 -05001004 sd = sysfs_get_dirent(kobj->sd, attr->name);
Eric W. Biederman06fc0d62009-11-20 16:08:54 -08001005 if (!sd)
Tejun Heo5d604182013-11-23 17:21:52 -05001006 return -ENOENT;
Tejun Heo51225032007-06-14 04:27:25 +09001007
Eric W. Biederman06fc0d62009-11-20 16:08:54 -08001008 newattrs.ia_mode = (mode & S_IALLUGO) | (sd->s_mode & ~S_IALLUGO);
Eric W. Biederman4c6974f2009-11-07 23:27:02 -08001009 newattrs.ia_valid = ATTR_MODE;
Tejun Heof88123e2007-09-20 16:05:10 +09001010
Tejun Heo5d604182013-11-23 17:21:52 -05001011 rc = kernfs_setattr(sd, &newattrs);
1012
1013 sysfs_put(sd);
Tejun Heo51225032007-06-14 04:27:25 +09001014 return rc;
Kay Sievers31e5abe2005-04-18 21:57:32 -07001015}
1016EXPORT_SYMBOL_GPL(sysfs_chmod_file);
1017
Kay Sievers31e5abe2005-04-18 21:57:32 -07001018/**
Tejun Heo58292cbe2013-09-11 22:29:04 -04001019 * sysfs_remove_file_ns - remove an object attribute with a custom ns tag
1020 * @kobj: object we're acting for
1021 * @attr: attribute descriptor
1022 * @ns: namespace tag of the file to remove
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 *
Tejun Heo58292cbe2013-09-11 22:29:04 -04001024 * Hash the attribute name and namespace tag and kill the victim.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 */
Tejun Heo58292cbe2013-09-11 22:29:04 -04001026void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
1027 const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{
Tejun Heo58292cbe2013-09-11 22:29:04 -04001029 struct sysfs_dirent *dir_sd = kobj->sd;
Eric W. Biederman487505c2011-10-12 21:53:38 +00001030
Tejun Heo879f40d2013-11-23 17:21:49 -05001031 kernfs_remove_by_name_ns(dir_sd, attr->name, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032}
Tejun Heo58292cbe2013-09-11 22:29:04 -04001033EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Greg Kroah-Hartman1b18dc22013-08-21 16:28:26 -07001035void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr)
Andi Kleen1c205ae2010-01-05 12:48:01 +01001036{
1037 int i;
1038 for (i = 0; ptr[i]; i++)
1039 sysfs_remove_file(kobj, ptr[i]);
1040}
Greg Kroah-Hartman1b866752013-08-21 16:17:47 -07001041EXPORT_SYMBOL_GPL(sysfs_remove_files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Alan Sterndfa87c82007-02-20 15:02:44 -05001043/**
1044 * sysfs_remove_file_from_group - remove an attribute file from a group.
1045 * @kobj: object we're acting for.
1046 * @attr: attribute descriptor.
1047 * @group: group name.
1048 */
1049void sysfs_remove_file_from_group(struct kobject *kobj,
1050 const struct attribute *attr, const char *group)
1051{
Tejun Heo608e2662007-06-14 04:27:22 +09001052 struct sysfs_dirent *dir_sd;
Alan Sterndfa87c82007-02-20 15:02:44 -05001053
James Bottomley11f24fb2008-01-02 18:44:05 -06001054 if (group)
Tejun Heo388975c2013-09-11 23:19:13 -04001055 dir_sd = sysfs_get_dirent(kobj->sd, group);
James Bottomley11f24fb2008-01-02 18:44:05 -06001056 else
1057 dir_sd = sysfs_get(kobj->sd);
Tejun Heo608e2662007-06-14 04:27:22 +09001058 if (dir_sd) {
Tejun Heo879f40d2013-11-23 17:21:49 -05001059 kernfs_remove_by_name(dir_sd, attr->name);
Tejun Heo608e2662007-06-14 04:27:22 +09001060 sysfs_put(dir_sd);
Alan Sterndfa87c82007-02-20 15:02:44 -05001061 }
1062}
1063EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
1064
Tejun Heo3124eb12013-10-01 17:42:09 -04001065/**
1066 * sysfs_create_bin_file - create binary file for object.
1067 * @kobj: object.
1068 * @attr: attribute descriptor.
1069 */
1070int sysfs_create_bin_file(struct kobject *kobj,
1071 const struct bin_attribute *attr)
1072{
1073 BUG_ON(!kobj || !kobj->sd || !attr);
1074
1075 return sysfs_add_file(kobj->sd, &attr->attr, SYSFS_KOBJ_BIN_ATTR);
1076}
1077EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
1078
1079/**
1080 * sysfs_remove_bin_file - remove binary file for object.
1081 * @kobj: object.
1082 * @attr: attribute descriptor.
1083 */
1084void sysfs_remove_bin_file(struct kobject *kobj,
1085 const struct bin_attribute *attr)
1086{
Tejun Heo879f40d2013-11-23 17:21:49 -05001087 kernfs_remove_by_name(kobj->sd, attr->attr.name);
Tejun Heo3124eb12013-10-01 17:42:09 -04001088}
1089EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);
1090
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001091struct sysfs_schedule_callback_struct {
Alex Chiang66942062009-03-13 12:07:36 -06001092 struct list_head workq_list;
1093 struct kobject *kobj;
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001094 void (*func)(void *);
1095 void *data;
Alan Stern523ded72007-04-26 00:12:04 -07001096 struct module *owner;
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001097 struct work_struct work;
1098};
1099
Alex Chiangd1102712009-03-25 15:11:36 -06001100static struct workqueue_struct *sysfs_workqueue;
Alex Chiang66942062009-03-13 12:07:36 -06001101static DEFINE_MUTEX(sysfs_workq_mutex);
1102static LIST_HEAD(sysfs_workq);
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001103static void sysfs_schedule_callback_work(struct work_struct *work)
1104{
1105 struct sysfs_schedule_callback_struct *ss = container_of(work,
1106 struct sysfs_schedule_callback_struct, work);
1107
1108 (ss->func)(ss->data);
1109 kobject_put(ss->kobj);
Alan Stern523ded72007-04-26 00:12:04 -07001110 module_put(ss->owner);
Alex Chiang66942062009-03-13 12:07:36 -06001111 mutex_lock(&sysfs_workq_mutex);
1112 list_del(&ss->workq_list);
1113 mutex_unlock(&sysfs_workq_mutex);
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001114 kfree(ss);
1115}
1116
1117/**
1118 * sysfs_schedule_callback - helper to schedule a callback for a kobject
1119 * @kobj: object we're acting for.
1120 * @func: callback function to invoke later.
1121 * @data: argument to pass to @func.
Alan Stern523ded72007-04-26 00:12:04 -07001122 * @owner: module owning the callback code
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001123 *
1124 * sysfs attribute methods must not unregister themselves or their parent
1125 * kobject (which would amount to the same thing). Attempts to do so will
1126 * deadlock, since unregistration is mutually exclusive with driver
1127 * callbacks.
1128 *
1129 * Instead methods can call this routine, which will attempt to allocate
1130 * and schedule a workqueue request to call back @func with @data as its
1131 * argument in the workqueue's process context. @kobj will be pinned
1132 * until @func returns.
1133 *
1134 * Returns 0 if the request was submitted, -ENOMEM if storage could not
Alex Chiang66942062009-03-13 12:07:36 -06001135 * be allocated, -ENODEV if a reference to @owner isn't available,
1136 * -EAGAIN if a callback has already been scheduled for @kobj.
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001137 */
1138int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
Alan Stern523ded72007-04-26 00:12:04 -07001139 void *data, struct module *owner)
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001140{
Alex Chiang66942062009-03-13 12:07:36 -06001141 struct sysfs_schedule_callback_struct *ss, *tmp;
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001142
Alan Stern523ded72007-04-26 00:12:04 -07001143 if (!try_module_get(owner))
1144 return -ENODEV;
Alex Chiang66942062009-03-13 12:07:36 -06001145
1146 mutex_lock(&sysfs_workq_mutex);
1147 list_for_each_entry_safe(ss, tmp, &sysfs_workq, workq_list)
1148 if (ss->kobj == kobj) {
Alex Chiangd1102712009-03-25 15:11:36 -06001149 module_put(owner);
Alex Chiang66942062009-03-13 12:07:36 -06001150 mutex_unlock(&sysfs_workq_mutex);
1151 return -EAGAIN;
1152 }
1153 mutex_unlock(&sysfs_workq_mutex);
1154
Alex Chiangd1102712009-03-25 15:11:36 -06001155 if (sysfs_workqueue == NULL) {
Andrew Morton086a3772009-05-07 12:36:53 -07001156 sysfs_workqueue = create_singlethread_workqueue("sysfsd");
Alex Chiangd1102712009-03-25 15:11:36 -06001157 if (sysfs_workqueue == NULL) {
1158 module_put(owner);
1159 return -ENOMEM;
1160 }
1161 }
1162
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001163 ss = kmalloc(sizeof(*ss), GFP_KERNEL);
Alan Stern523ded72007-04-26 00:12:04 -07001164 if (!ss) {
1165 module_put(owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001166 return -ENOMEM;
Alan Stern523ded72007-04-26 00:12:04 -07001167 }
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001168 kobject_get(kobj);
1169 ss->kobj = kobj;
1170 ss->func = func;
1171 ss->data = data;
Alan Stern523ded72007-04-26 00:12:04 -07001172 ss->owner = owner;
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001173 INIT_WORK(&ss->work, sysfs_schedule_callback_work);
Alex Chiang66942062009-03-13 12:07:36 -06001174 INIT_LIST_HEAD(&ss->workq_list);
1175 mutex_lock(&sysfs_workq_mutex);
1176 list_add_tail(&ss->workq_list, &sysfs_workq);
1177 mutex_unlock(&sysfs_workq_mutex);
Alex Chiangd1102712009-03-25 15:11:36 -06001178 queue_work(sysfs_workqueue, &ss->work);
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001179 return 0;
1180}
1181EXPORT_SYMBOL_GPL(sysfs_schedule_callback);