blob: 69cca0f4ccf39319d4aec56e5d15fc498069107a [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 Heo13c589d2013-10-01 17:42:02 -040050static struct sysfs_open_file *sysfs_of(struct file *file)
51{
52 return ((struct seq_file *)file->private_data)->private;
53}
54
Tejun Heo375b6112013-10-01 17:41:57 -040055/*
Tejun Heof6acf8b2013-11-28 14:54:21 -050056 * Determine the kernfs_ops for the given sysfs_dirent. This function must
57 * be called while holding an active reference.
58 */
59static const struct kernfs_ops *kernfs_ops(struct sysfs_dirent *sd)
60{
61 if (!sysfs_ignore_lockdep(sd))
62 lockdep_assert_held(sd);
63 return sd->s_attr.ops;
64}
65
66/*
Tejun Heo375b6112013-10-01 17:41:57 -040067 * Determine ktype->sysfs_ops for the given sysfs_dirent. This function
68 * must be called while holding an active reference.
69 */
70static const struct sysfs_ops *sysfs_file_ops(struct sysfs_dirent *sd)
71{
Tejun Heo7c6e2d32013-11-28 14:54:14 -050072 struct kobject *kobj = sd->s_parent->priv;
Tejun Heo375b6112013-10-01 17:41:57 -040073
Tejun Heo785a1622013-10-14 09:27:11 -040074 if (!sysfs_ignore_lockdep(sd))
75 lockdep_assert_held(sd);
Tejun Heo375b6112013-10-01 17:41:57 -040076 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 Heoc2b19da2013-11-28 14:54:16 -050084static int sysfs_kf_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;
Tejun Heo7c6e2d32013-11-28 14:54:14 -050087 struct kobject *kobj = of->sd->s_parent->priv;
Tejun Heoc2b19da2013-11-28 14:54:16 -050088 const struct sysfs_ops *ops = sysfs_file_ops(of->sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 ssize_t count;
Tejun Heoc2b19da2013-11-28 14:54:16 -050090 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
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 /*
Tejun Heoc2b19da2013-11-28 14:54:16 -0500100 * Invoke show(). Control may reach here via seq file lseek even
101 * if @ops->show() isn't implemented.
Tejun Heo13c589d2013-10-01 17:42:02 -0400102 */
Tejun Heoc2b19da2013-11-28 14:54:16 -0500103 if (ops->show) {
Tejun Heo7c6e2d32013-11-28 14:54:14 -0500104 count = ops->show(kobj, of->sd->priv, buf);
Tejun Heoc2b19da2013-11-28 14:54:16 -0500105 if (count < 0)
106 return count;
107 }
Tejun Heo0ab66082007-06-14 03:45:16 +0900108
Miao Xie8118a852007-11-21 14:55:19 -0800109 /*
110 * The code works fine with PAGE_SIZE return but it's likely to
111 * indicate truncated result or overflow in normal use cases.
112 */
Andrew Morton815d2d52008-03-04 15:09:07 -0800113 if (count >= (ssize_t)PAGE_SIZE) {
114 print_symbol("fill_read_buffer: %s returned bad count\n",
115 (unsigned long)ops->show);
116 /* Try to struggle along */
117 count = PAGE_SIZE - 1;
118 }
Tejun Heo13c589d2013-10-01 17:42:02 -0400119 seq_commit(sf, count);
120 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
Tejun Heoc2b19da2013-11-28 14:54:16 -0500123static ssize_t sysfs_kf_bin_read(struct sysfs_open_file *of, char *buf,
124 size_t count, loff_t pos)
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400125{
Tejun Heo7c6e2d32013-11-28 14:54:14 -0500126 struct bin_attribute *battr = of->sd->priv;
127 struct kobject *kobj = of->sd->s_parent->priv;
Tejun Heoc2b19da2013-11-28 14:54:16 -0500128 loff_t size = file_inode(of->file)->i_size;
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400129
Tejun Heoc2b19da2013-11-28 14:54:16 -0500130 if (!count)
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400131 return 0;
132
133 if (size) {
Tejun Heoc2b19da2013-11-28 14:54:16 -0500134 if (pos > size)
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400135 return 0;
Tejun Heoc2b19da2013-11-28 14:54:16 -0500136 if (pos + count > size)
137 count = size - pos;
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400138 }
139
Tejun Heoc2b19da2013-11-28 14:54:16 -0500140 if (!battr->read)
141 return -EIO;
142
143 return battr->read(of->file, kobj, battr, buf, pos, count);
144}
145
146static void *kernfs_seq_start(struct seq_file *sf, loff_t *ppos)
147{
148 struct sysfs_open_file *of = sf->private;
149
150 /*
151 * @of->mutex nests outside active ref and is just to ensure that
152 * the ops aren't called concurrently for the same open file.
153 */
154 mutex_lock(&of->mutex);
155 if (!sysfs_get_active(of->sd))
156 return ERR_PTR(-ENODEV);
157
158 /*
159 * The same behavior and code as single_open(). Returns !NULL if
160 * pos is at the beginning; otherwise, NULL.
161 */
162 return NULL + !*ppos;
163}
164
165static void *kernfs_seq_next(struct seq_file *sf, void *v, loff_t *ppos)
166{
167 /*
168 * The same behavior and code as single_open(), always terminate
169 * after the initial read.
170 */
171 ++*ppos;
172 return NULL;
173}
174
175static void kernfs_seq_stop(struct seq_file *sf, void *v)
176{
177 struct sysfs_open_file *of = sf->private;
178
179 sysfs_put_active(of->sd);
180 mutex_unlock(&of->mutex);
181}
182
183static int kernfs_seq_show(struct seq_file *sf, void *v)
184{
185 struct sysfs_open_file *of = sf->private;
186
187 of->event = atomic_read(&of->sd->s_attr.open->event);
188
Tejun Heof6acf8b2013-11-28 14:54:21 -0500189 return of->sd->s_attr.ops->seq_show(sf, v);
Tejun Heoc2b19da2013-11-28 14:54:16 -0500190}
191
192static const struct seq_operations kernfs_seq_ops = {
193 .start = kernfs_seq_start,
194 .next = kernfs_seq_next,
195 .stop = kernfs_seq_stop,
196 .show = kernfs_seq_show,
197};
198
199/*
200 * As reading a bin file can have side-effects, the exact offset and bytes
201 * specified in read(2) call should be passed to the read callback making
202 * it difficult to use seq_file. Implement simplistic custom buffering for
203 * bin files.
204 */
205static ssize_t kernfs_file_direct_read(struct sysfs_open_file *of,
206 char __user *user_buf, size_t count,
207 loff_t *ppos)
208{
209 ssize_t len = min_t(size_t, count, PAGE_SIZE);
Tejun Heof6acf8b2013-11-28 14:54:21 -0500210 const struct kernfs_ops *ops;
Tejun Heoc2b19da2013-11-28 14:54:16 -0500211 char *buf;
212
213 buf = kmalloc(len, GFP_KERNEL);
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400214 if (!buf)
215 return -ENOMEM;
216
Tejun Heoc2b19da2013-11-28 14:54:16 -0500217 /*
218 * @of->mutex nests outside active ref and is just to ensure that
219 * the ops aren't called concurrently for the same open file.
220 */
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400221 mutex_lock(&of->mutex);
222 if (!sysfs_get_active(of->sd)) {
Tejun Heoc2b19da2013-11-28 14:54:16 -0500223 len = -ENODEV;
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400224 mutex_unlock(&of->mutex);
225 goto out_free;
226 }
227
Tejun Heof6acf8b2013-11-28 14:54:21 -0500228 ops = kernfs_ops(of->sd);
229 if (ops->read)
230 len = ops->read(of, buf, len, *ppos);
231 else
232 len = -EINVAL;
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
Tejun Heof6acf8b2013-11-28 14:54:21 -0500264 if (of->sd->s_flags & SYSFS_FLAG_HAS_SEQ_SHOW)
Tejun Heoc2b19da2013-11-28 14:54:16 -0500265 return seq_read(file, user_buf, count, ppos);
Tejun Heof6acf8b2013-11-28 14:54:21 -0500266 else
267 return kernfs_file_direct_read(of, user_buf, count, ppos);
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400268}
269
Tejun Heo50b38ca2013-11-28 14:54:17 -0500270/* kernfs write callback for regular sysfs files */
271static ssize_t sysfs_kf_write(struct sysfs_open_file *of, char *buf,
272 size_t count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
Tejun Heo50b38ca2013-11-28 14:54:17 -0500274 const struct sysfs_ops *ops = sysfs_file_ops(of->sd);
Tejun Heo7c6e2d32013-11-28 14:54:14 -0500275 struct kobject *kobj = of->sd->s_parent->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Tejun Heo50b38ca2013-11-28 14:54:17 -0500277 if (!count)
278 return 0;
279
280 return ops->store(kobj, of->sd->priv, buf, count);
281}
282
283/* kernfs write callback for bin sysfs files */
284static ssize_t sysfs_kf_bin_write(struct sysfs_open_file *of, char *buf,
285 size_t count, loff_t pos)
286{
287 struct bin_attribute *battr = of->sd->priv;
288 struct kobject *kobj = of->sd->s_parent->priv;
289 loff_t size = file_inode(of->file)->i_size;
290
291 if (size) {
292 if (size <= pos)
293 return 0;
294 count = min_t(ssize_t, count, size - pos);
Tejun Heo8ef445f2013-10-01 17:42:01 -0400295 }
Tejun Heo50b38ca2013-11-28 14:54:17 -0500296 if (!count)
297 return 0;
Tejun Heo0ab66082007-06-14 03:45:16 +0900298
Tejun Heo50b38ca2013-11-28 14:54:17 -0500299 if (!battr->write)
300 return -EIO;
Tejun Heof9b9a622013-10-01 17:42:05 -0400301
Tejun Heo50b38ca2013-11-28 14:54:17 -0500302 return battr->write(of->file, kobj, battr, buf, pos, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305/**
Tejun Heo50b38ca2013-11-28 14:54:17 -0500306 * kernfs_file_write - kernfs vfs write callback
Tejun Heo8ef445f2013-10-01 17:42:01 -0400307 * @file: file pointer
308 * @user_buf: data to write
309 * @count: number of bytes
310 * @ppos: starting offset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 *
Tejun Heo50b38ca2013-11-28 14:54:17 -0500312 * Copy data in from userland and pass it to the matching kernfs write
313 * operation.
Tejun Heo8ef445f2013-10-01 17:42:01 -0400314 *
315 * There is no easy way for us to know if userspace is only doing a partial
316 * write, so we don't support them. We expect the entire buffer to come on
317 * the first write. Hint: if you're writing a value, first read the file,
318 * modify only the the value you're changing, then write entire buffer
319 * back.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 */
Tejun Heo50b38ca2013-11-28 14:54:17 -0500321static ssize_t kernfs_file_write(struct file *file, const char __user *user_buf,
322 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
Tejun Heo13c589d2013-10-01 17:42:02 -0400324 struct sysfs_open_file *of = sysfs_of(file);
Tejun Heof9b9a622013-10-01 17:42:05 -0400325 ssize_t len = min_t(size_t, count, PAGE_SIZE);
Tejun Heof6acf8b2013-11-28 14:54:21 -0500326 const struct kernfs_ops *ops;
Tejun Heo8ef445f2013-10-01 17:42:01 -0400327 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Tejun Heo8ef445f2013-10-01 17:42:01 -0400329 buf = kmalloc(len + 1, GFP_KERNEL);
330 if (!buf)
331 return -ENOMEM;
332
333 if (copy_from_user(buf, user_buf, len)) {
334 len = -EFAULT;
335 goto out_free;
336 }
337 buf[len] = '\0'; /* guarantee string termination */
338
Tejun Heo50b38ca2013-11-28 14:54:17 -0500339 /*
340 * @of->mutex nests outside active ref and is just to ensure that
341 * the ops aren't called concurrently for the same open file.
342 */
343 mutex_lock(&of->mutex);
344 if (!sysfs_get_active(of->sd)) {
345 mutex_unlock(&of->mutex);
346 len = -ENODEV;
347 goto out_free;
348 }
349
Tejun Heof6acf8b2013-11-28 14:54:21 -0500350 ops = kernfs_ops(of->sd);
351 if (ops->write)
352 len = ops->write(of, buf, len, *ppos);
Tejun Heo50b38ca2013-11-28 14:54:17 -0500353 else
Tejun Heof6acf8b2013-11-28 14:54:21 -0500354 len = -EINVAL;
Tejun Heo50b38ca2013-11-28 14:54:17 -0500355
356 sysfs_put_active(of->sd);
357 mutex_unlock(&of->mutex);
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (len > 0)
360 *ppos += len;
Tejun Heo8ef445f2013-10-01 17:42:01 -0400361out_free:
362 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return len;
364}
365
Tejun Heofdbffaa2013-11-28 14:54:18 -0500366static int sysfs_kf_bin_mmap(struct sysfs_open_file *of,
367 struct vm_area_struct *vma)
368{
369 struct bin_attribute *battr = of->sd->priv;
370 struct kobject *kobj = of->sd->s_parent->priv;
371
372 if (!battr->mmap)
373 return -ENODEV;
374
375 return battr->mmap(of->file, kobj, battr, vma);
376}
377
378static void kernfs_vma_open(struct vm_area_struct *vma)
Tejun Heo73d97142013-10-01 17:42:07 -0400379{
380 struct file *file = vma->vm_file;
381 struct sysfs_open_file *of = sysfs_of(file);
382
383 if (!of->vm_ops)
384 return;
385
386 if (!sysfs_get_active(of->sd))
387 return;
388
389 if (of->vm_ops->open)
390 of->vm_ops->open(vma);
391
392 sysfs_put_active(of->sd);
393}
394
Tejun Heofdbffaa2013-11-28 14:54:18 -0500395static int kernfs_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Tejun Heo73d97142013-10-01 17:42:07 -0400396{
397 struct file *file = vma->vm_file;
398 struct sysfs_open_file *of = sysfs_of(file);
399 int ret;
400
401 if (!of->vm_ops)
402 return VM_FAULT_SIGBUS;
403
404 if (!sysfs_get_active(of->sd))
405 return VM_FAULT_SIGBUS;
406
407 ret = VM_FAULT_SIGBUS;
408 if (of->vm_ops->fault)
409 ret = of->vm_ops->fault(vma, vmf);
410
411 sysfs_put_active(of->sd);
412 return ret;
413}
414
Tejun Heofdbffaa2013-11-28 14:54:18 -0500415static int kernfs_vma_page_mkwrite(struct vm_area_struct *vma,
416 struct vm_fault *vmf)
Tejun Heo73d97142013-10-01 17:42:07 -0400417{
418 struct file *file = vma->vm_file;
419 struct sysfs_open_file *of = sysfs_of(file);
420 int ret;
421
422 if (!of->vm_ops)
423 return VM_FAULT_SIGBUS;
424
425 if (!sysfs_get_active(of->sd))
426 return VM_FAULT_SIGBUS;
427
428 ret = 0;
429 if (of->vm_ops->page_mkwrite)
430 ret = of->vm_ops->page_mkwrite(vma, vmf);
431 else
432 file_update_time(file);
433
434 sysfs_put_active(of->sd);
435 return ret;
436}
437
Tejun Heofdbffaa2013-11-28 14:54:18 -0500438static int kernfs_vma_access(struct vm_area_struct *vma, unsigned long addr,
439 void *buf, int len, int write)
Tejun Heo73d97142013-10-01 17:42:07 -0400440{
441 struct file *file = vma->vm_file;
442 struct sysfs_open_file *of = sysfs_of(file);
443 int ret;
444
445 if (!of->vm_ops)
446 return -EINVAL;
447
448 if (!sysfs_get_active(of->sd))
449 return -EINVAL;
450
451 ret = -EINVAL;
452 if (of->vm_ops->access)
453 ret = of->vm_ops->access(vma, addr, buf, len, write);
454
455 sysfs_put_active(of->sd);
456 return ret;
457}
458
459#ifdef CONFIG_NUMA
Tejun Heofdbffaa2013-11-28 14:54:18 -0500460static int kernfs_vma_set_policy(struct vm_area_struct *vma,
461 struct mempolicy *new)
Tejun Heo73d97142013-10-01 17:42:07 -0400462{
463 struct file *file = vma->vm_file;
464 struct sysfs_open_file *of = sysfs_of(file);
465 int ret;
466
467 if (!of->vm_ops)
468 return 0;
469
470 if (!sysfs_get_active(of->sd))
471 return -EINVAL;
472
473 ret = 0;
474 if (of->vm_ops->set_policy)
475 ret = of->vm_ops->set_policy(vma, new);
476
477 sysfs_put_active(of->sd);
478 return ret;
479}
480
Tejun Heofdbffaa2013-11-28 14:54:18 -0500481static struct mempolicy *kernfs_vma_get_policy(struct vm_area_struct *vma,
482 unsigned long addr)
Tejun Heo73d97142013-10-01 17:42:07 -0400483{
484 struct file *file = vma->vm_file;
485 struct sysfs_open_file *of = sysfs_of(file);
486 struct mempolicy *pol;
487
488 if (!of->vm_ops)
489 return vma->vm_policy;
490
491 if (!sysfs_get_active(of->sd))
492 return vma->vm_policy;
493
494 pol = vma->vm_policy;
495 if (of->vm_ops->get_policy)
496 pol = of->vm_ops->get_policy(vma, addr);
497
498 sysfs_put_active(of->sd);
499 return pol;
500}
501
Tejun Heofdbffaa2013-11-28 14:54:18 -0500502static int kernfs_vma_migrate(struct vm_area_struct *vma,
503 const nodemask_t *from, const nodemask_t *to,
504 unsigned long flags)
Tejun Heo73d97142013-10-01 17:42:07 -0400505{
506 struct file *file = vma->vm_file;
507 struct sysfs_open_file *of = sysfs_of(file);
508 int ret;
509
510 if (!of->vm_ops)
511 return 0;
512
513 if (!sysfs_get_active(of->sd))
514 return 0;
515
516 ret = 0;
517 if (of->vm_ops->migrate)
518 ret = of->vm_ops->migrate(vma, from, to, flags);
519
520 sysfs_put_active(of->sd);
521 return ret;
522}
523#endif
524
Tejun Heofdbffaa2013-11-28 14:54:18 -0500525static const struct vm_operations_struct kernfs_vm_ops = {
526 .open = kernfs_vma_open,
527 .fault = kernfs_vma_fault,
528 .page_mkwrite = kernfs_vma_page_mkwrite,
529 .access = kernfs_vma_access,
Tejun Heo73d97142013-10-01 17:42:07 -0400530#ifdef CONFIG_NUMA
Tejun Heofdbffaa2013-11-28 14:54:18 -0500531 .set_policy = kernfs_vma_set_policy,
532 .get_policy = kernfs_vma_get_policy,
533 .migrate = kernfs_vma_migrate,
Tejun Heo73d97142013-10-01 17:42:07 -0400534#endif
535};
536
Tejun Heofdbffaa2013-11-28 14:54:18 -0500537static int kernfs_file_mmap(struct file *file, struct vm_area_struct *vma)
Tejun Heo73d97142013-10-01 17:42:07 -0400538{
539 struct sysfs_open_file *of = sysfs_of(file);
Tejun Heof6acf8b2013-11-28 14:54:21 -0500540 const struct kernfs_ops *ops;
Tejun Heo73d97142013-10-01 17:42:07 -0400541 int rc;
542
543 mutex_lock(&of->mutex);
544
Tejun Heo73d97142013-10-01 17:42:07 -0400545 rc = -ENODEV;
546 if (!sysfs_get_active(of->sd))
547 goto out_unlock;
548
Tejun Heof6acf8b2013-11-28 14:54:21 -0500549 ops = kernfs_ops(of->sd);
550 if (ops->mmap)
551 rc = ops->mmap(of, vma);
Tejun Heo73d97142013-10-01 17:42:07 -0400552 if (rc)
553 goto out_put;
554
555 /*
556 * PowerPC's pci_mmap of legacy_mem uses shmem_zero_setup()
557 * to satisfy versions of X which crash if the mmap fails: that
558 * substitutes a new vm_file, and we don't then want bin_vm_ops.
559 */
560 if (vma->vm_file != file)
561 goto out_put;
562
563 rc = -EINVAL;
564 if (of->mmapped && of->vm_ops != vma->vm_ops)
565 goto out_put;
566
567 /*
568 * It is not possible to successfully wrap close.
569 * So error if someone is trying to use close.
570 */
571 rc = -EINVAL;
572 if (vma->vm_ops && vma->vm_ops->close)
573 goto out_put;
574
575 rc = 0;
576 of->mmapped = 1;
577 of->vm_ops = vma->vm_ops;
Tejun Heofdbffaa2013-11-28 14:54:18 -0500578 vma->vm_ops = &kernfs_vm_ops;
Tejun Heo73d97142013-10-01 17:42:07 -0400579out_put:
580 sysfs_put_active(of->sd);
581out_unlock:
582 mutex_unlock(&of->mutex);
583
584 return rc;
585}
586
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900587/**
588 * sysfs_get_open_dirent - get or create sysfs_open_dirent
589 * @sd: target sysfs_dirent
Tejun Heo58282d82013-10-01 17:41:59 -0400590 * @of: sysfs_open_file for this instance of open
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900591 *
592 * If @sd->s_attr.open exists, increment its reference count;
Tejun Heo58282d82013-10-01 17:41:59 -0400593 * otherwise, create one. @of is chained to the files list.
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900594 *
595 * LOCKING:
596 * Kernel thread context (may sleep).
597 *
598 * RETURNS:
599 * 0 on success, -errno on failure.
600 */
601static int sysfs_get_open_dirent(struct sysfs_dirent *sd,
Tejun Heo58282d82013-10-01 17:41:59 -0400602 struct sysfs_open_file *of)
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900603{
604 struct sysfs_open_dirent *od, *new_od = NULL;
605
606 retry:
Tejun Heoc75ec762013-10-01 17:41:58 -0400607 mutex_lock(&sysfs_open_file_mutex);
Neil Brown83db93f2009-09-15 16:05:51 -0700608 spin_lock_irq(&sysfs_open_dirent_lock);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900609
610 if (!sd->s_attr.open && new_od) {
611 sd->s_attr.open = new_od;
612 new_od = NULL;
613 }
614
615 od = sd->s_attr.open;
616 if (od) {
617 atomic_inc(&od->refcnt);
Tejun Heo58282d82013-10-01 17:41:59 -0400618 list_add_tail(&of->list, &od->files);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900619 }
620
Neil Brown83db93f2009-09-15 16:05:51 -0700621 spin_unlock_irq(&sysfs_open_dirent_lock);
Tejun Heoc75ec762013-10-01 17:41:58 -0400622 mutex_unlock(&sysfs_open_file_mutex);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900623
624 if (od) {
625 kfree(new_od);
626 return 0;
627 }
628
629 /* not there, initialize a new one and retry */
630 new_od = kmalloc(sizeof(*new_od), GFP_KERNEL);
631 if (!new_od)
632 return -ENOMEM;
633
634 atomic_set(&new_od->refcnt, 0);
Tejun Heoa4e8b912007-09-20 16:05:12 +0900635 atomic_set(&new_od->event, 1);
636 init_waitqueue_head(&new_od->poll);
Tejun Heo58282d82013-10-01 17:41:59 -0400637 INIT_LIST_HEAD(&new_od->files);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900638 goto retry;
639}
640
641/**
642 * sysfs_put_open_dirent - put sysfs_open_dirent
643 * @sd: target sysfs_dirent
Tejun Heo58282d82013-10-01 17:41:59 -0400644 * @of: associated sysfs_open_file
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900645 *
Tejun Heo58282d82013-10-01 17:41:59 -0400646 * Put @sd->s_attr.open and unlink @of from the files list. If
647 * reference count reaches zero, disassociate and free it.
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900648 *
649 * LOCKING:
650 * None.
651 */
652static void sysfs_put_open_dirent(struct sysfs_dirent *sd,
Tejun Heo58282d82013-10-01 17:41:59 -0400653 struct sysfs_open_file *of)
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900654{
655 struct sysfs_open_dirent *od = sd->s_attr.open;
Neil Brown83db93f2009-09-15 16:05:51 -0700656 unsigned long flags;
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900657
Tejun Heoc75ec762013-10-01 17:41:58 -0400658 mutex_lock(&sysfs_open_file_mutex);
Neil Brown83db93f2009-09-15 16:05:51 -0700659 spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900660
Tejun Heo73d97142013-10-01 17:42:07 -0400661 if (of)
662 list_del(&of->list);
663
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900664 if (atomic_dec_and_test(&od->refcnt))
665 sd->s_attr.open = NULL;
666 else
667 od = NULL;
668
Neil Brown83db93f2009-09-15 16:05:51 -0700669 spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
Tejun Heoc75ec762013-10-01 17:41:58 -0400670 mutex_unlock(&sysfs_open_file_mutex);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900671
672 kfree(od);
673}
674
Tejun Heoc6fb4492013-11-28 14:54:19 -0500675static int kernfs_file_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Tejun Heo3e519032007-06-14 03:45:15 +0900677 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
Tejun Heof6acf8b2013-11-28 14:54:21 -0500678 const struct kernfs_ops *ops;
Tejun Heo58282d82013-10-01 17:41:59 -0400679 struct sysfs_open_file *of;
Tejun Heo027a4852013-11-17 11:17:36 +0900680 bool has_read, has_write, has_mmap;
Kay Sievers000f2a42007-11-02 13:47:53 +0100681 int error = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800683 if (!sysfs_get_active(attr_sd))
Tejun Heo0ab66082007-06-14 03:45:16 +0900684 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Tejun Heof6acf8b2013-11-28 14:54:21 -0500686 ops = kernfs_ops(attr_sd);
Tejun Heo49fe6042013-10-01 17:42:08 -0400687
Tejun Heof6acf8b2013-11-28 14:54:21 -0500688 has_read = ops->seq_show || ops->read || ops->mmap;
689 has_write = ops->write || ops->mmap;
690 has_mmap = ops->mmap;
Tejun Heo49fe6042013-10-01 17:42:08 -0400691
692 /* check perms and supported operations */
693 if ((file->f_mode & FMODE_WRITE) &&
694 (!(inode->i_mode & S_IWUGO) || !has_write))
Tejun Heo7b595752007-06-14 03:45:17 +0900695 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Tejun Heo49fe6042013-10-01 17:42:08 -0400697 if ((file->f_mode & FMODE_READ) &&
698 (!(inode->i_mode & S_IRUGO) || !has_read))
699 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Tejun Heo13c589d2013-10-01 17:42:02 -0400701 /* allocate a sysfs_open_file for the file */
Tejun Heo0ab66082007-06-14 03:45:16 +0900702 error = -ENOMEM;
Tejun Heo58282d82013-10-01 17:41:59 -0400703 of = kzalloc(sizeof(struct sysfs_open_file), GFP_KERNEL);
704 if (!of)
Tejun Heo7b595752007-06-14 03:45:17 +0900705 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Tejun Heo027a4852013-11-17 11:17:36 +0900707 /*
708 * The following is done to give a different lockdep key to
709 * @of->mutex for files which implement mmap. This is a rather
710 * crude way to avoid false positive lockdep warning around
711 * mm->mmap_sem - mmap nests @of->mutex under mm->mmap_sem and
712 * reading /sys/block/sda/trace/act_mask grabs sr_mutex, under
713 * which mm->mmap_sem nests, while holding @of->mutex. As each
714 * open file has a separate mutex, it's okay as long as those don't
715 * happen on the same file. At this point, we can't easily give
716 * each file a separate locking class. Let's differentiate on
717 * whether the file has mmap or not for now.
718 */
719 if (has_mmap)
720 mutex_init(&of->mutex);
721 else
722 mutex_init(&of->mutex);
723
Tejun Heobcafe4e2013-10-01 17:42:00 -0400724 of->sd = attr_sd;
725 of->file = file;
Tejun Heo13c589d2013-10-01 17:42:02 -0400726
727 /*
Tejun Heo49fe6042013-10-01 17:42:08 -0400728 * Always instantiate seq_file even if read access doesn't use
729 * seq_file or is not requested. This unifies private data access
730 * and readable regular files are the vast majority anyway.
Tejun Heo13c589d2013-10-01 17:42:02 -0400731 */
Tejun Heof6acf8b2013-11-28 14:54:21 -0500732 if (ops->seq_show)
Tejun Heoc2b19da2013-11-28 14:54:16 -0500733 error = seq_open(file, &kernfs_seq_ops);
Tejun Heof6acf8b2013-11-28 14:54:21 -0500734 else
735 error = seq_open(file, NULL);
Tejun Heo13c589d2013-10-01 17:42:02 -0400736 if (error)
737 goto err_free;
738
Tejun Heoc2b19da2013-11-28 14:54:16 -0500739 ((struct seq_file *)file->private_data)->private = of;
740
Tejun Heo13c589d2013-10-01 17:42:02 -0400741 /* seq_file clears PWRITE unconditionally, restore it if WRITE */
742 if (file->f_mode & FMODE_WRITE)
743 file->f_mode |= FMODE_PWRITE;
Tejun Heo0ab66082007-06-14 03:45:16 +0900744
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900745 /* make sure we have open dirent struct */
Tejun Heo58282d82013-10-01 17:41:59 -0400746 error = sysfs_get_open_dirent(attr_sd, of);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900747 if (error)
Tejun Heo13c589d2013-10-01 17:42:02 -0400748 goto err_close;
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900749
Tejun Heob05f0542007-09-20 16:05:10 +0900750 /* open succeeded, put active references */
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800751 sysfs_put_active(attr_sd);
Tejun Heo0ab66082007-06-14 03:45:16 +0900752 return 0;
753
Tejun Heo13c589d2013-10-01 17:42:02 -0400754err_close:
Tejun Heoc2b19da2013-11-28 14:54:16 -0500755 seq_release(inode, file);
Tejun Heo13c589d2013-10-01 17:42:02 -0400756err_free:
Tejun Heo58282d82013-10-01 17:41:59 -0400757 kfree(of);
Tejun Heo13c589d2013-10-01 17:42:02 -0400758err_out:
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800759 sysfs_put_active(attr_sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return error;
761}
762
Tejun Heoc6fb4492013-11-28 14:54:19 -0500763static int kernfs_file_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900765 struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata;
Tejun Heo13c589d2013-10-01 17:42:02 -0400766 struct sysfs_open_file *of = sysfs_of(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Tejun Heo58282d82013-10-01 17:41:59 -0400768 sysfs_put_open_dirent(sd, of);
Tejun Heoc2b19da2013-11-28 14:54:16 -0500769 seq_release(inode, filp);
Tejun Heo58282d82013-10-01 17:41:59 -0400770 kfree(of);
Tejun Heo50ab1a72007-09-20 16:05:10 +0900771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 return 0;
773}
774
Tejun Heo73d97142013-10-01 17:42:07 -0400775void sysfs_unmap_bin_file(struct sysfs_dirent *sd)
776{
777 struct sysfs_open_dirent *od;
778 struct sysfs_open_file *of;
779
Tejun Heof6acf8b2013-11-28 14:54:21 -0500780 if (!(sd->s_flags & SYSFS_FLAG_HAS_MMAP))
Tejun Heo73d97142013-10-01 17:42:07 -0400781 return;
782
783 spin_lock_irq(&sysfs_open_dirent_lock);
784 od = sd->s_attr.open;
785 if (od)
786 atomic_inc(&od->refcnt);
787 spin_unlock_irq(&sysfs_open_dirent_lock);
788 if (!od)
789 return;
790
791 mutex_lock(&sysfs_open_file_mutex);
792 list_for_each_entry(of, &od->files, list) {
793 struct inode *inode = file_inode(of->file);
794 unmap_mapping_range(inode->i_mapping, 0, 0, 1);
795 }
796 mutex_unlock(&sysfs_open_file_mutex);
797
798 sysfs_put_open_dirent(sd, NULL);
799}
800
NeilBrown4508a7a2006-03-20 17:53:53 +1100801/* Sysfs attribute files are pollable. The idea is that you read
802 * the content and then you use 'poll' or 'select' to wait for
803 * the content to change. When the content changes (assuming the
804 * manager for the kobject supports notification), poll will
805 * return POLLERR|POLLPRI, and select will return the fd whether
806 * it is waiting for read, write, or exceptions.
807 * Once poll/select indicates that the value has changed, you
Dan Williams2424b5d2008-04-07 15:35:01 -0700808 * need to close and re-open the file, or seek to 0 and read again.
NeilBrown4508a7a2006-03-20 17:53:53 +1100809 * Reminder: this only works for attributes which actively support
810 * it, and it is not possible to test an attribute from userspace
Rolf Eike Beera93720e2007-08-10 13:51:07 -0700811 * to see if it supports poll (Neither 'poll' nor 'select' return
NeilBrown4508a7a2006-03-20 17:53:53 +1100812 * an appropriate error code). When in doubt, set a suitable timeout value.
813 */
Tejun Heoc6fb4492013-11-28 14:54:19 -0500814static unsigned int kernfs_file_poll(struct file *filp, poll_table *wait)
NeilBrown4508a7a2006-03-20 17:53:53 +1100815{
Tejun Heo13c589d2013-10-01 17:42:02 -0400816 struct sysfs_open_file *of = sysfs_of(filp);
Tejun Heo0ab66082007-06-14 03:45:16 +0900817 struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata;
Tejun Heoa4e8b912007-09-20 16:05:12 +0900818 struct sysfs_open_dirent *od = attr_sd->s_attr.open;
Tejun Heo0ab66082007-06-14 03:45:16 +0900819
820 /* need parent for the kobj, grab both */
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800821 if (!sysfs_get_active(attr_sd))
Tejun Heo0ab66082007-06-14 03:45:16 +0900822 goto trigger;
NeilBrown4508a7a2006-03-20 17:53:53 +1100823
Tejun Heoa4e8b912007-09-20 16:05:12 +0900824 poll_wait(filp, &od->poll, wait);
NeilBrown4508a7a2006-03-20 17:53:53 +1100825
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800826 sysfs_put_active(attr_sd);
NeilBrown4508a7a2006-03-20 17:53:53 +1100827
Tejun Heo58282d82013-10-01 17:41:59 -0400828 if (of->event != atomic_read(&od->event))
Tejun Heo0ab66082007-06-14 03:45:16 +0900829 goto trigger;
830
KOSAKI Motohiro1af35572009-04-09 13:53:22 +0900831 return DEFAULT_POLLMASK;
Tejun Heo0ab66082007-06-14 03:45:16 +0900832
833 trigger:
KOSAKI Motohiro1af35572009-04-09 13:53:22 +0900834 return DEFAULT_POLLMASK|POLLERR|POLLPRI;
NeilBrown4508a7a2006-03-20 17:53:53 +1100835}
836
Neil Brownf1282c82008-07-16 08:58:04 +1000837void sysfs_notify_dirent(struct sysfs_dirent *sd)
838{
839 struct sysfs_open_dirent *od;
Neil Brown83db93f2009-09-15 16:05:51 -0700840 unsigned long flags;
Neil Brownf1282c82008-07-16 08:58:04 +1000841
Neil Brown83db93f2009-09-15 16:05:51 -0700842 spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
Neil Brownf1282c82008-07-16 08:58:04 +1000843
Nick Dyerfc60bb82013-06-07 15:45:13 +0100844 if (!WARN_ON(sysfs_type(sd) != SYSFS_KOBJ_ATTR)) {
845 od = sd->s_attr.open;
846 if (od) {
847 atomic_inc(&od->event);
848 wake_up_interruptible(&od->poll);
849 }
Neil Brownf1282c82008-07-16 08:58:04 +1000850 }
851
Neil Brown83db93f2009-09-15 16:05:51 -0700852 spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
Neil Brownf1282c82008-07-16 08:58:04 +1000853}
854EXPORT_SYMBOL_GPL(sysfs_notify_dirent);
855
Trent Piepho8c0e3992008-09-25 16:45:13 -0700856void sysfs_notify(struct kobject *k, const char *dir, const char *attr)
NeilBrown4508a7a2006-03-20 17:53:53 +1100857{
Tejun Heo51225032007-06-14 04:27:25 +0900858 struct sysfs_dirent *sd = k->sd;
NeilBrown4508a7a2006-03-20 17:53:53 +1100859
Tejun Heo51225032007-06-14 04:27:25 +0900860 mutex_lock(&sysfs_mutex);
NeilBrown4508a7a2006-03-20 17:53:53 +1100861
Tejun Heo51225032007-06-14 04:27:25 +0900862 if (sd && dir)
Tejun Heocfec0bc2013-09-11 22:29:09 -0400863 sd = sysfs_find_dirent(sd, dir, NULL);
Tejun Heo51225032007-06-14 04:27:25 +0900864 if (sd && attr)
Tejun Heocfec0bc2013-09-11 22:29:09 -0400865 sd = sysfs_find_dirent(sd, attr, NULL);
Neil Brownf1282c82008-07-16 08:58:04 +1000866 if (sd)
867 sysfs_notify_dirent(sd);
Tejun Heo51225032007-06-14 04:27:25 +0900868
869 mutex_unlock(&sysfs_mutex);
NeilBrown4508a7a2006-03-20 17:53:53 +1100870}
871EXPORT_SYMBOL_GPL(sysfs_notify);
872
Tejun Heoc6fb4492013-11-28 14:54:19 -0500873const struct file_operations kernfs_file_operations = {
Tejun Heoc2b19da2013-11-28 14:54:16 -0500874 .read = kernfs_file_read,
Tejun Heo50b38ca2013-11-28 14:54:17 -0500875 .write = kernfs_file_write,
Tejun Heo044e3bc2013-11-01 13:16:53 -0400876 .llseek = generic_file_llseek,
Tejun Heofdbffaa2013-11-28 14:54:18 -0500877 .mmap = kernfs_file_mmap,
Tejun Heoc6fb4492013-11-28 14:54:19 -0500878 .open = kernfs_file_open,
879 .release = kernfs_file_release,
880 .poll = kernfs_file_poll,
Tejun Heof9b9a622013-10-01 17:42:05 -0400881};
882
Tejun Heof6acf8b2013-11-28 14:54:21 -0500883static const struct kernfs_ops sysfs_file_kfops_empty = {
884};
885
886static const struct kernfs_ops sysfs_file_kfops_ro = {
887 .seq_show = sysfs_kf_seq_show,
888};
889
890static const struct kernfs_ops sysfs_file_kfops_wo = {
891 .write = sysfs_kf_write,
892};
893
894static const struct kernfs_ops sysfs_file_kfops_rw = {
895 .seq_show = sysfs_kf_seq_show,
896 .write = sysfs_kf_write,
897};
898
899static const struct kernfs_ops sysfs_bin_kfops_ro = {
900 .read = sysfs_kf_bin_read,
901};
902
903static const struct kernfs_ops sysfs_bin_kfops_wo = {
904 .write = sysfs_kf_bin_write,
905};
906
907static const struct kernfs_ops sysfs_bin_kfops_rw = {
908 .read = sysfs_kf_bin_read,
909 .write = sysfs_kf_bin_write,
910 .mmap = sysfs_kf_bin_mmap,
911};
912
Tejun Heo58292cbe2013-09-11 22:29:04 -0400913int sysfs_add_file_mode_ns(struct sysfs_dirent *dir_sd,
Tejun Heoa7dc66d2013-11-28 14:54:23 -0500914 const struct attribute *attr, bool is_bin,
Tejun Heo496f7392013-11-28 14:54:24 -0500915 umode_t mode, const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
Tejun Heof6acf8b2013-11-28 14:54:21 -0500917 const struct kernfs_ops *ops;
Tejun Heoa26cd722007-06-14 03:45:14 +0900918 struct sysfs_dirent *sd;
Tejun Heo471bd7b2013-11-28 14:54:22 -0500919 loff_t size;
Tejun Heoa26cd722007-06-14 03:45:14 +0900920
Tejun Heoa7dc66d2013-11-28 14:54:23 -0500921 if (!is_bin) {
Tejun Heof6acf8b2013-11-28 14:54:21 -0500922 struct kobject *kobj = dir_sd->priv;
923 const struct sysfs_ops *sysfs_ops = kobj->ktype->sysfs_ops;
924
925 /* every kobject with an attribute needs a ktype assigned */
926 if (WARN(!sysfs_ops, KERN_ERR
927 "missing sysfs attribute operations for kobject: %s\n",
928 kobject_name(kobj)))
929 return -EINVAL;
930
931 if (sysfs_ops->show && sysfs_ops->store)
932 ops = &sysfs_file_kfops_rw;
933 else if (sysfs_ops->show)
934 ops = &sysfs_file_kfops_ro;
935 else if (sysfs_ops->store)
936 ops = &sysfs_file_kfops_wo;
937 else
938 ops = &sysfs_file_kfops_empty;
Tejun Heo471bd7b2013-11-28 14:54:22 -0500939
940 size = PAGE_SIZE;
Tejun Heof6acf8b2013-11-28 14:54:21 -0500941 } else {
942 struct bin_attribute *battr = (void *)attr;
943
944 if ((battr->read && battr->write) || battr->mmap)
945 ops = &sysfs_bin_kfops_rw;
946 else if (battr->read)
947 ops = &sysfs_bin_kfops_ro;
948 else if (battr->write)
949 ops = &sysfs_bin_kfops_wo;
950 else
951 ops = &sysfs_file_kfops_empty;
Tejun Heo471bd7b2013-11-28 14:54:22 -0500952
953 size = battr->size;
Tejun Heof6acf8b2013-11-28 14:54:21 -0500954 }
955
Tejun Heo496f7392013-11-28 14:54:24 -0500956 sd = kernfs_create_file_ns(dir_sd, attr->name, mode, size,
957 ops, (void *)attr, ns);
958 if (IS_ERR(sd)) {
959 if (PTR_ERR(sd) == -EEXIST)
960 sysfs_warn_dup(dir_sd, attr->name);
961 return PTR_ERR(sd);
962 }
963 return 0;
964}
965
966/**
967 * kernfs_create_file_ns - create a file
968 * @parent: directory to create the file in
969 * @name: name of the file
970 * @mode: mode of the file
971 * @size: size of the file
972 * @ops: kernfs operations for the file
973 * @priv: private data for the file
974 * @ns: optional namespace tag of the file
975 *
976 * Returns the created node on success, ERR_PTR() value on error.
977 */
978struct sysfs_dirent *kernfs_create_file_ns(struct sysfs_dirent *parent,
979 const char *name,
980 umode_t mode, loff_t size,
981 const struct kernfs_ops *ops,
982 void *priv, const void *ns)
983{
984 struct sysfs_addrm_cxt acxt;
985 struct sysfs_dirent *sd;
986 int rc;
987
988 sd = sysfs_new_dirent(name, (mode & S_IALLUGO) | S_IFREG,
989 SYSFS_KOBJ_ATTR);
Tejun Heo3007e992007-06-14 04:27:23 +0900990 if (!sd)
Tejun Heo496f7392013-11-28 14:54:24 -0500991 return ERR_PTR(-ENOMEM);
Eric W. Biederman487505c2011-10-12 21:53:38 +0000992
Tejun Heof6acf8b2013-11-28 14:54:21 -0500993 sd->s_attr.ops = ops;
Tejun Heo471bd7b2013-11-28 14:54:22 -0500994 sd->s_attr.size = size;
Eric W. Biederman487505c2011-10-12 21:53:38 +0000995 sd->s_ns = ns;
Tejun Heo496f7392013-11-28 14:54:24 -0500996 sd->priv = priv;
Eric W. Biedermana2db6842010-02-11 15:20:00 -0800997 sysfs_dirent_init_lockdep(sd);
Tejun Heoa26cd722007-06-14 03:45:14 +0900998
Tejun Heof6acf8b2013-11-28 14:54:21 -0500999 /*
1000 * sd->s_attr.ops is accesible only while holding active ref. We
1001 * need to know whether some ops are implemented outside active
1002 * ref. Cache their existence in flags.
1003 */
1004 if (ops->seq_show)
1005 sd->s_flags |= SYSFS_FLAG_HAS_SEQ_SHOW;
1006 if (ops->mmap)
1007 sd->s_flags |= SYSFS_FLAG_HAS_MMAP;
1008
Tejun Heod69ac5a2013-09-18 17:15:35 -04001009 sysfs_addrm_start(&acxt);
Tejun Heo496f7392013-11-28 14:54:24 -05001010 rc = __sysfs_add_one(&acxt, sd, parent);
Tejun Heo23dc2792007-08-02 21:38:03 +09001011 sysfs_addrm_finish(&acxt);
Tejun Heo3007e992007-06-14 04:27:23 +09001012
Tejun Heo496f7392013-11-28 14:54:24 -05001013 if (rc) {
Tejun Heo967e35d2007-07-18 16:38:11 +09001014 sysfs_put(sd);
Tejun Heo496f7392013-11-28 14:54:24 -05001015 return ERR_PTR(rc);
1016 }
1017 return sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018}
1019
James Bottomley0f423892008-03-20 20:47:52 -05001020int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,
Tejun Heoa7dc66d2013-11-28 14:54:23 -05001021 bool is_bin)
James Bottomley0f423892008-03-20 20:47:52 -05001022{
Tejun Heoa7dc66d2013-11-28 14:54:23 -05001023 return sysfs_add_file_mode_ns(dir_sd, attr, is_bin, attr->mode, NULL);
James Bottomley0f423892008-03-20 20:47:52 -05001024}
1025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026/**
Tejun Heo58292cbe2013-09-11 22:29:04 -04001027 * sysfs_create_file_ns - create an attribute file for an object with custom ns
1028 * @kobj: object we're creating for
1029 * @attr: attribute descriptor
1030 * @ns: namespace the new file should belong to
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 */
Tejun Heo58292cbe2013-09-11 22:29:04 -04001032int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
1033 const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
Tejun Heo608e2662007-06-14 04:27:22 +09001035 BUG_ON(!kobj || !kobj->sd || !attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Tejun Heoa7dc66d2013-11-28 14:54:23 -05001037 return sysfs_add_file_mode_ns(kobj->sd, attr, false, attr->mode, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039}
Tejun Heo58292cbe2013-09-11 22:29:04 -04001040EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Andi Kleen1c205ae2010-01-05 12:48:01 +01001042int sysfs_create_files(struct kobject *kobj, const struct attribute **ptr)
1043{
1044 int err = 0;
1045 int i;
1046
1047 for (i = 0; ptr[i] && !err; i++)
1048 err = sysfs_create_file(kobj, ptr[i]);
1049 if (err)
1050 while (--i >= 0)
1051 sysfs_remove_file(kobj, ptr[i]);
1052 return err;
1053}
Greg Kroah-Hartman1b866752013-08-21 16:17:47 -07001054EXPORT_SYMBOL_GPL(sysfs_create_files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056/**
Alan Sterndfa87c82007-02-20 15:02:44 -05001057 * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
1058 * @kobj: object we're acting for.
1059 * @attr: attribute descriptor.
1060 * @group: group name.
1061 */
1062int sysfs_add_file_to_group(struct kobject *kobj,
1063 const struct attribute *attr, const char *group)
1064{
Tejun Heo608e2662007-06-14 04:27:22 +09001065 struct sysfs_dirent *dir_sd;
Alan Sterndfa87c82007-02-20 15:02:44 -05001066 int error;
1067
James Bottomley11f24fb2008-01-02 18:44:05 -06001068 if (group)
Tejun Heo388975c2013-09-11 23:19:13 -04001069 dir_sd = sysfs_get_dirent(kobj->sd, group);
James Bottomley11f24fb2008-01-02 18:44:05 -06001070 else
1071 dir_sd = sysfs_get(kobj->sd);
1072
Tejun Heo608e2662007-06-14 04:27:22 +09001073 if (!dir_sd)
1074 return -ENOENT;
1075
Tejun Heoa7dc66d2013-11-28 14:54:23 -05001076 error = sysfs_add_file(dir_sd, attr, false);
Tejun Heo608e2662007-06-14 04:27:22 +09001077 sysfs_put(dir_sd);
1078
Alan Sterndfa87c82007-02-20 15:02:44 -05001079 return error;
1080}
1081EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
1082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083/**
Kay Sievers31e5abe2005-04-18 21:57:32 -07001084 * sysfs_chmod_file - update the modified mode value on an object attribute.
1085 * @kobj: object we're acting for.
1086 * @attr: attribute descriptor.
1087 * @mode: file permissions.
1088 *
1089 */
Jean Delvare49c19402010-07-02 16:54:05 +02001090int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
Al Viro48176a92011-07-24 03:40:40 -04001091 umode_t mode)
Kay Sievers31e5abe2005-04-18 21:57:32 -07001092{
Eric W. Biederman06fc0d62009-11-20 16:08:54 -08001093 struct sysfs_dirent *sd;
Maneesh Sonibc062b12005-07-29 12:13:35 -07001094 struct iattr newattrs;
Tejun Heo51225032007-06-14 04:27:25 +09001095 int rc;
Kay Sievers31e5abe2005-04-18 21:57:32 -07001096
Tejun Heo5d604182013-11-23 17:21:52 -05001097 sd = sysfs_get_dirent(kobj->sd, attr->name);
Eric W. Biederman06fc0d62009-11-20 16:08:54 -08001098 if (!sd)
Tejun Heo5d604182013-11-23 17:21:52 -05001099 return -ENOENT;
Tejun Heo51225032007-06-14 04:27:25 +09001100
Eric W. Biederman06fc0d62009-11-20 16:08:54 -08001101 newattrs.ia_mode = (mode & S_IALLUGO) | (sd->s_mode & ~S_IALLUGO);
Eric W. Biederman4c6974f2009-11-07 23:27:02 -08001102 newattrs.ia_valid = ATTR_MODE;
Tejun Heof88123e2007-09-20 16:05:10 +09001103
Tejun Heo5d604182013-11-23 17:21:52 -05001104 rc = kernfs_setattr(sd, &newattrs);
1105
1106 sysfs_put(sd);
Tejun Heo51225032007-06-14 04:27:25 +09001107 return rc;
Kay Sievers31e5abe2005-04-18 21:57:32 -07001108}
1109EXPORT_SYMBOL_GPL(sysfs_chmod_file);
1110
Kay Sievers31e5abe2005-04-18 21:57:32 -07001111/**
Tejun Heo58292cbe2013-09-11 22:29:04 -04001112 * sysfs_remove_file_ns - remove an object attribute with a custom ns tag
1113 * @kobj: object we're acting for
1114 * @attr: attribute descriptor
1115 * @ns: namespace tag of the file to remove
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 *
Tejun Heo58292cbe2013-09-11 22:29:04 -04001117 * Hash the attribute name and namespace tag and kill the victim.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 */
Tejun Heo58292cbe2013-09-11 22:29:04 -04001119void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
1120 const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
Tejun Heo58292cbe2013-09-11 22:29:04 -04001122 struct sysfs_dirent *dir_sd = kobj->sd;
Eric W. Biederman487505c2011-10-12 21:53:38 +00001123
Tejun Heo879f40d2013-11-23 17:21:49 -05001124 kernfs_remove_by_name_ns(dir_sd, attr->name, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125}
Tejun Heo58292cbe2013-09-11 22:29:04 -04001126EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Greg Kroah-Hartman1b18dc22013-08-21 16:28:26 -07001128void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr)
Andi Kleen1c205ae2010-01-05 12:48:01 +01001129{
1130 int i;
1131 for (i = 0; ptr[i]; i++)
1132 sysfs_remove_file(kobj, ptr[i]);
1133}
Greg Kroah-Hartman1b866752013-08-21 16:17:47 -07001134EXPORT_SYMBOL_GPL(sysfs_remove_files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Alan Sterndfa87c82007-02-20 15:02:44 -05001136/**
1137 * sysfs_remove_file_from_group - remove an attribute file from a group.
1138 * @kobj: object we're acting for.
1139 * @attr: attribute descriptor.
1140 * @group: group name.
1141 */
1142void sysfs_remove_file_from_group(struct kobject *kobj,
1143 const struct attribute *attr, const char *group)
1144{
Tejun Heo608e2662007-06-14 04:27:22 +09001145 struct sysfs_dirent *dir_sd;
Alan Sterndfa87c82007-02-20 15:02:44 -05001146
James Bottomley11f24fb2008-01-02 18:44:05 -06001147 if (group)
Tejun Heo388975c2013-09-11 23:19:13 -04001148 dir_sd = sysfs_get_dirent(kobj->sd, group);
James Bottomley11f24fb2008-01-02 18:44:05 -06001149 else
1150 dir_sd = sysfs_get(kobj->sd);
Tejun Heo608e2662007-06-14 04:27:22 +09001151 if (dir_sd) {
Tejun Heo879f40d2013-11-23 17:21:49 -05001152 kernfs_remove_by_name(dir_sd, attr->name);
Tejun Heo608e2662007-06-14 04:27:22 +09001153 sysfs_put(dir_sd);
Alan Sterndfa87c82007-02-20 15:02:44 -05001154 }
1155}
1156EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
1157
Tejun Heo3124eb12013-10-01 17:42:09 -04001158/**
1159 * sysfs_create_bin_file - create binary file for object.
1160 * @kobj: object.
1161 * @attr: attribute descriptor.
1162 */
1163int sysfs_create_bin_file(struct kobject *kobj,
1164 const struct bin_attribute *attr)
1165{
1166 BUG_ON(!kobj || !kobj->sd || !attr);
1167
Tejun Heoa7dc66d2013-11-28 14:54:23 -05001168 return sysfs_add_file(kobj->sd, &attr->attr, true);
Tejun Heo3124eb12013-10-01 17:42:09 -04001169}
1170EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
1171
1172/**
1173 * sysfs_remove_bin_file - remove binary file for object.
1174 * @kobj: object.
1175 * @attr: attribute descriptor.
1176 */
1177void sysfs_remove_bin_file(struct kobject *kobj,
1178 const struct bin_attribute *attr)
1179{
Tejun Heo879f40d2013-11-23 17:21:49 -05001180 kernfs_remove_by_name(kobj->sd, attr->attr.name);
Tejun Heo3124eb12013-10-01 17:42:09 -04001181}
1182EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);
1183
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001184struct sysfs_schedule_callback_struct {
Alex Chiang66942062009-03-13 12:07:36 -06001185 struct list_head workq_list;
1186 struct kobject *kobj;
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001187 void (*func)(void *);
1188 void *data;
Alan Stern523ded72007-04-26 00:12:04 -07001189 struct module *owner;
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001190 struct work_struct work;
1191};
1192
Alex Chiangd1102712009-03-25 15:11:36 -06001193static struct workqueue_struct *sysfs_workqueue;
Alex Chiang66942062009-03-13 12:07:36 -06001194static DEFINE_MUTEX(sysfs_workq_mutex);
1195static LIST_HEAD(sysfs_workq);
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001196static void sysfs_schedule_callback_work(struct work_struct *work)
1197{
1198 struct sysfs_schedule_callback_struct *ss = container_of(work,
1199 struct sysfs_schedule_callback_struct, work);
1200
1201 (ss->func)(ss->data);
1202 kobject_put(ss->kobj);
Alan Stern523ded72007-04-26 00:12:04 -07001203 module_put(ss->owner);
Alex Chiang66942062009-03-13 12:07:36 -06001204 mutex_lock(&sysfs_workq_mutex);
1205 list_del(&ss->workq_list);
1206 mutex_unlock(&sysfs_workq_mutex);
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001207 kfree(ss);
1208}
1209
1210/**
1211 * sysfs_schedule_callback - helper to schedule a callback for a kobject
1212 * @kobj: object we're acting for.
1213 * @func: callback function to invoke later.
1214 * @data: argument to pass to @func.
Alan Stern523ded72007-04-26 00:12:04 -07001215 * @owner: module owning the callback code
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001216 *
1217 * sysfs attribute methods must not unregister themselves or their parent
1218 * kobject (which would amount to the same thing). Attempts to do so will
1219 * deadlock, since unregistration is mutually exclusive with driver
1220 * callbacks.
1221 *
1222 * Instead methods can call this routine, which will attempt to allocate
1223 * and schedule a workqueue request to call back @func with @data as its
1224 * argument in the workqueue's process context. @kobj will be pinned
1225 * until @func returns.
1226 *
1227 * Returns 0 if the request was submitted, -ENOMEM if storage could not
Alex Chiang66942062009-03-13 12:07:36 -06001228 * be allocated, -ENODEV if a reference to @owner isn't available,
1229 * -EAGAIN if a callback has already been scheduled for @kobj.
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001230 */
1231int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
Alan Stern523ded72007-04-26 00:12:04 -07001232 void *data, struct module *owner)
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001233{
Alex Chiang66942062009-03-13 12:07:36 -06001234 struct sysfs_schedule_callback_struct *ss, *tmp;
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001235
Alan Stern523ded72007-04-26 00:12:04 -07001236 if (!try_module_get(owner))
1237 return -ENODEV;
Alex Chiang66942062009-03-13 12:07:36 -06001238
1239 mutex_lock(&sysfs_workq_mutex);
1240 list_for_each_entry_safe(ss, tmp, &sysfs_workq, workq_list)
1241 if (ss->kobj == kobj) {
Alex Chiangd1102712009-03-25 15:11:36 -06001242 module_put(owner);
Alex Chiang66942062009-03-13 12:07:36 -06001243 mutex_unlock(&sysfs_workq_mutex);
1244 return -EAGAIN;
1245 }
1246 mutex_unlock(&sysfs_workq_mutex);
1247
Alex Chiangd1102712009-03-25 15:11:36 -06001248 if (sysfs_workqueue == NULL) {
Andrew Morton086a3772009-05-07 12:36:53 -07001249 sysfs_workqueue = create_singlethread_workqueue("sysfsd");
Alex Chiangd1102712009-03-25 15:11:36 -06001250 if (sysfs_workqueue == NULL) {
1251 module_put(owner);
1252 return -ENOMEM;
1253 }
1254 }
1255
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001256 ss = kmalloc(sizeof(*ss), GFP_KERNEL);
Alan Stern523ded72007-04-26 00:12:04 -07001257 if (!ss) {
1258 module_put(owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001259 return -ENOMEM;
Alan Stern523ded72007-04-26 00:12:04 -07001260 }
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001261 kobject_get(kobj);
1262 ss->kobj = kobj;
1263 ss->func = func;
1264 ss->data = data;
Alan Stern523ded72007-04-26 00:12:04 -07001265 ss->owner = owner;
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001266 INIT_WORK(&ss->work, sysfs_schedule_callback_work);
Alex Chiang66942062009-03-13 12:07:36 -06001267 INIT_LIST_HEAD(&ss->workq_list);
1268 mutex_lock(&sysfs_workq_mutex);
1269 list_add_tail(&ss->workq_list, &sysfs_workq);
1270 mutex_unlock(&sysfs_workq_mutex);
Alex Chiangd1102712009-03-25 15:11:36 -06001271 queue_work(sysfs_workqueue, &ss->work);
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001272 return 0;
1273}
1274EXPORT_SYMBOL_GPL(sysfs_schedule_callback);