blob: 74e3478d9cb4de530c668f155eb0a6c908e57d11 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Tejun Heo6d66f5c2007-09-20 17:31:38 +09002 * fs/sysfs/file.c - sysfs regular (text) file implementation
3 *
4 * Copyright (c) 2001-3 Patrick Mochel
5 * Copyright (c) 2007 SUSE Linux Products GmbH
6 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
7 *
8 * This file is released under the GPLv2.
9 *
10 * Please see Documentation/filesystems/sysfs.txt for more information.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
13#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/kobject.h>
Andrew Morton815d2d52008-03-04 15:09:07 -080015#include <linux/kallsyms.h>
Robert P. J. Dayc6f87732008-03-13 22:41:52 -040016#include <linux/slab.h>
Miklos Szeredi93265d12008-06-16 13:46:47 +020017#include <linux/fsnotify.h>
Christoph Hellwig5f45f1a2005-06-23 00:09:12 -070018#include <linux/namei.h>
NeilBrown4508a7a2006-03-20 17:53:53 +110019#include <linux/poll.h>
Oliver Neukum94bebf42006-12-20 10:52:44 +010020#include <linux/list.h>
Dave Young52e8c202007-07-26 11:03:54 +000021#include <linux/mutex.h>
Andrew Mortonae87221d2007-08-24 16:11:54 -070022#include <linux/limits.h>
Greg Kroah-Hartman060cc742013-08-21 16:34:59 -070023#include <linux/uaccess.h>
Tejun Heo13c589d2013-10-01 17:42:02 -040024#include <linux/seq_file.h>
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;
Tejun Heod19b9842013-11-28 14:54:26 -0500149 const struct kernfs_ops *ops;
Tejun Heoc2b19da2013-11-28 14:54:16 -0500150
151 /*
152 * @of->mutex nests outside active ref and is just to ensure that
153 * the ops aren't called concurrently for the same open file.
154 */
155 mutex_lock(&of->mutex);
156 if (!sysfs_get_active(of->sd))
157 return ERR_PTR(-ENODEV);
158
Tejun Heod19b9842013-11-28 14:54:26 -0500159 ops = kernfs_ops(of->sd);
160 if (ops->seq_start) {
161 return ops->seq_start(sf, ppos);
162 } else {
163 /*
164 * The same behavior and code as single_open(). Returns
165 * !NULL if pos is at the beginning; otherwise, NULL.
166 */
167 return NULL + !*ppos;
168 }
Tejun Heoc2b19da2013-11-28 14:54:16 -0500169}
170
171static void *kernfs_seq_next(struct seq_file *sf, void *v, loff_t *ppos)
172{
Tejun Heod19b9842013-11-28 14:54:26 -0500173 struct sysfs_open_file *of = sf->private;
174 const struct kernfs_ops *ops = kernfs_ops(of->sd);
175
176 if (ops->seq_next) {
177 return ops->seq_next(sf, v, ppos);
178 } else {
179 /*
180 * The same behavior and code as single_open(), always
181 * terminate after the initial read.
182 */
183 ++*ppos;
184 return NULL;
185 }
Tejun Heoc2b19da2013-11-28 14:54:16 -0500186}
187
188static void kernfs_seq_stop(struct seq_file *sf, void *v)
189{
190 struct sysfs_open_file *of = sf->private;
Tejun Heod19b9842013-11-28 14:54:26 -0500191 const struct kernfs_ops *ops = kernfs_ops(of->sd);
192
193 if (ops->seq_stop)
194 ops->seq_stop(sf, v);
Tejun Heoc2b19da2013-11-28 14:54:16 -0500195
196 sysfs_put_active(of->sd);
197 mutex_unlock(&of->mutex);
198}
199
200static int kernfs_seq_show(struct seq_file *sf, void *v)
201{
202 struct sysfs_open_file *of = sf->private;
203
204 of->event = atomic_read(&of->sd->s_attr.open->event);
205
Tejun Heof6acf8b2013-11-28 14:54:21 -0500206 return of->sd->s_attr.ops->seq_show(sf, v);
Tejun Heoc2b19da2013-11-28 14:54:16 -0500207}
208
209static const struct seq_operations kernfs_seq_ops = {
210 .start = kernfs_seq_start,
211 .next = kernfs_seq_next,
212 .stop = kernfs_seq_stop,
213 .show = kernfs_seq_show,
214};
215
216/*
217 * As reading a bin file can have side-effects, the exact offset and bytes
218 * specified in read(2) call should be passed to the read callback making
219 * it difficult to use seq_file. Implement simplistic custom buffering for
220 * bin files.
221 */
222static ssize_t kernfs_file_direct_read(struct sysfs_open_file *of,
223 char __user *user_buf, size_t count,
224 loff_t *ppos)
225{
226 ssize_t len = min_t(size_t, count, PAGE_SIZE);
Tejun Heof6acf8b2013-11-28 14:54:21 -0500227 const struct kernfs_ops *ops;
Tejun Heoc2b19da2013-11-28 14:54:16 -0500228 char *buf;
229
230 buf = kmalloc(len, GFP_KERNEL);
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400231 if (!buf)
232 return -ENOMEM;
233
Tejun Heoc2b19da2013-11-28 14:54:16 -0500234 /*
235 * @of->mutex nests outside active ref and is just to ensure that
236 * the ops aren't called concurrently for the same open file.
237 */
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400238 mutex_lock(&of->mutex);
239 if (!sysfs_get_active(of->sd)) {
Tejun Heoc2b19da2013-11-28 14:54:16 -0500240 len = -ENODEV;
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400241 mutex_unlock(&of->mutex);
242 goto out_free;
243 }
244
Tejun Heof6acf8b2013-11-28 14:54:21 -0500245 ops = kernfs_ops(of->sd);
246 if (ops->read)
247 len = ops->read(of, buf, len, *ppos);
248 else
249 len = -EINVAL;
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400250
251 sysfs_put_active(of->sd);
252 mutex_unlock(&of->mutex);
253
Tejun Heoc2b19da2013-11-28 14:54:16 -0500254 if (len < 0)
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400255 goto out_free;
256
Tejun Heoc2b19da2013-11-28 14:54:16 -0500257 if (copy_to_user(user_buf, buf, len)) {
258 len = -EFAULT;
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400259 goto out_free;
260 }
261
Tejun Heoc2b19da2013-11-28 14:54:16 -0500262 *ppos += len;
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400263
264 out_free:
265 kfree(buf);
Tejun Heoc2b19da2013-11-28 14:54:16 -0500266 return len;
267}
268
269/**
270 * kernfs_file_read - kernfs vfs read callback
271 * @file: file pointer
272 * @user_buf: data to write
273 * @count: number of bytes
274 * @ppos: starting offset
275 */
276static ssize_t kernfs_file_read(struct file *file, char __user *user_buf,
277 size_t count, loff_t *ppos)
278{
279 struct sysfs_open_file *of = sysfs_of(file);
280
Tejun Heof6acf8b2013-11-28 14:54:21 -0500281 if (of->sd->s_flags & SYSFS_FLAG_HAS_SEQ_SHOW)
Tejun Heoc2b19da2013-11-28 14:54:16 -0500282 return seq_read(file, user_buf, count, ppos);
Tejun Heof6acf8b2013-11-28 14:54:21 -0500283 else
284 return kernfs_file_direct_read(of, user_buf, count, ppos);
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400285}
286
Tejun Heo50b38ca2013-11-28 14:54:17 -0500287/* kernfs write callback for regular sysfs files */
288static ssize_t sysfs_kf_write(struct sysfs_open_file *of, char *buf,
289 size_t count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Tejun Heo50b38ca2013-11-28 14:54:17 -0500291 const struct sysfs_ops *ops = sysfs_file_ops(of->sd);
Tejun Heo7c6e2d32013-11-28 14:54:14 -0500292 struct kobject *kobj = of->sd->s_parent->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Tejun Heo50b38ca2013-11-28 14:54:17 -0500294 if (!count)
295 return 0;
296
297 return ops->store(kobj, of->sd->priv, buf, count);
298}
299
300/* kernfs write callback for bin sysfs files */
301static ssize_t sysfs_kf_bin_write(struct sysfs_open_file *of, char *buf,
302 size_t count, loff_t pos)
303{
304 struct bin_attribute *battr = of->sd->priv;
305 struct kobject *kobj = of->sd->s_parent->priv;
306 loff_t size = file_inode(of->file)->i_size;
307
308 if (size) {
309 if (size <= pos)
310 return 0;
311 count = min_t(ssize_t, count, size - pos);
Tejun Heo8ef445f2013-10-01 17:42:01 -0400312 }
Tejun Heo50b38ca2013-11-28 14:54:17 -0500313 if (!count)
314 return 0;
Tejun Heo0ab66082007-06-14 03:45:16 +0900315
Tejun Heo50b38ca2013-11-28 14:54:17 -0500316 if (!battr->write)
317 return -EIO;
Tejun Heof9b9a622013-10-01 17:42:05 -0400318
Tejun Heo50b38ca2013-11-28 14:54:17 -0500319 return battr->write(of->file, kobj, battr, buf, pos, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322/**
Tejun Heo50b38ca2013-11-28 14:54:17 -0500323 * kernfs_file_write - kernfs vfs write callback
Tejun Heo8ef445f2013-10-01 17:42:01 -0400324 * @file: file pointer
325 * @user_buf: data to write
326 * @count: number of bytes
327 * @ppos: starting offset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 *
Tejun Heo50b38ca2013-11-28 14:54:17 -0500329 * Copy data in from userland and pass it to the matching kernfs write
330 * operation.
Tejun Heo8ef445f2013-10-01 17:42:01 -0400331 *
332 * There is no easy way for us to know if userspace is only doing a partial
333 * write, so we don't support them. We expect the entire buffer to come on
334 * the first write. Hint: if you're writing a value, first read the file,
335 * modify only the the value you're changing, then write entire buffer
336 * back.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 */
Tejun Heo50b38ca2013-11-28 14:54:17 -0500338static ssize_t kernfs_file_write(struct file *file, const char __user *user_buf,
339 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Tejun Heo13c589d2013-10-01 17:42:02 -0400341 struct sysfs_open_file *of = sysfs_of(file);
Tejun Heof9b9a622013-10-01 17:42:05 -0400342 ssize_t len = min_t(size_t, count, PAGE_SIZE);
Tejun Heof6acf8b2013-11-28 14:54:21 -0500343 const struct kernfs_ops *ops;
Tejun Heo8ef445f2013-10-01 17:42:01 -0400344 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Tejun Heo8ef445f2013-10-01 17:42:01 -0400346 buf = kmalloc(len + 1, GFP_KERNEL);
347 if (!buf)
348 return -ENOMEM;
349
350 if (copy_from_user(buf, user_buf, len)) {
351 len = -EFAULT;
352 goto out_free;
353 }
354 buf[len] = '\0'; /* guarantee string termination */
355
Tejun Heo50b38ca2013-11-28 14:54:17 -0500356 /*
357 * @of->mutex nests outside active ref and is just to ensure that
358 * the ops aren't called concurrently for the same open file.
359 */
360 mutex_lock(&of->mutex);
361 if (!sysfs_get_active(of->sd)) {
362 mutex_unlock(&of->mutex);
363 len = -ENODEV;
364 goto out_free;
365 }
366
Tejun Heof6acf8b2013-11-28 14:54:21 -0500367 ops = kernfs_ops(of->sd);
368 if (ops->write)
369 len = ops->write(of, buf, len, *ppos);
Tejun Heo50b38ca2013-11-28 14:54:17 -0500370 else
Tejun Heof6acf8b2013-11-28 14:54:21 -0500371 len = -EINVAL;
Tejun Heo50b38ca2013-11-28 14:54:17 -0500372
373 sysfs_put_active(of->sd);
374 mutex_unlock(&of->mutex);
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 if (len > 0)
377 *ppos += len;
Tejun Heo8ef445f2013-10-01 17:42:01 -0400378out_free:
379 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return len;
381}
382
Tejun Heofdbffaa2013-11-28 14:54:18 -0500383static int sysfs_kf_bin_mmap(struct sysfs_open_file *of,
384 struct vm_area_struct *vma)
385{
386 struct bin_attribute *battr = of->sd->priv;
387 struct kobject *kobj = of->sd->s_parent->priv;
388
389 if (!battr->mmap)
390 return -ENODEV;
391
392 return battr->mmap(of->file, kobj, battr, vma);
393}
394
395static void kernfs_vma_open(struct vm_area_struct *vma)
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
400 if (!of->vm_ops)
401 return;
402
403 if (!sysfs_get_active(of->sd))
404 return;
405
406 if (of->vm_ops->open)
407 of->vm_ops->open(vma);
408
409 sysfs_put_active(of->sd);
410}
411
Tejun Heofdbffaa2013-11-28 14:54:18 -0500412static int kernfs_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Tejun Heo73d97142013-10-01 17:42:07 -0400413{
414 struct file *file = vma->vm_file;
415 struct sysfs_open_file *of = sysfs_of(file);
416 int ret;
417
418 if (!of->vm_ops)
419 return VM_FAULT_SIGBUS;
420
421 if (!sysfs_get_active(of->sd))
422 return VM_FAULT_SIGBUS;
423
424 ret = VM_FAULT_SIGBUS;
425 if (of->vm_ops->fault)
426 ret = of->vm_ops->fault(vma, vmf);
427
428 sysfs_put_active(of->sd);
429 return ret;
430}
431
Tejun Heofdbffaa2013-11-28 14:54:18 -0500432static int kernfs_vma_page_mkwrite(struct vm_area_struct *vma,
433 struct vm_fault *vmf)
Tejun Heo73d97142013-10-01 17:42:07 -0400434{
435 struct file *file = vma->vm_file;
436 struct sysfs_open_file *of = sysfs_of(file);
437 int ret;
438
439 if (!of->vm_ops)
440 return VM_FAULT_SIGBUS;
441
442 if (!sysfs_get_active(of->sd))
443 return VM_FAULT_SIGBUS;
444
445 ret = 0;
446 if (of->vm_ops->page_mkwrite)
447 ret = of->vm_ops->page_mkwrite(vma, vmf);
448 else
449 file_update_time(file);
450
451 sysfs_put_active(of->sd);
452 return ret;
453}
454
Tejun Heofdbffaa2013-11-28 14:54:18 -0500455static int kernfs_vma_access(struct vm_area_struct *vma, unsigned long addr,
456 void *buf, int len, int write)
Tejun Heo73d97142013-10-01 17:42:07 -0400457{
458 struct file *file = vma->vm_file;
459 struct sysfs_open_file *of = sysfs_of(file);
460 int ret;
461
462 if (!of->vm_ops)
463 return -EINVAL;
464
465 if (!sysfs_get_active(of->sd))
466 return -EINVAL;
467
468 ret = -EINVAL;
469 if (of->vm_ops->access)
470 ret = of->vm_ops->access(vma, addr, buf, len, write);
471
472 sysfs_put_active(of->sd);
473 return ret;
474}
475
476#ifdef CONFIG_NUMA
Tejun Heofdbffaa2013-11-28 14:54:18 -0500477static int kernfs_vma_set_policy(struct vm_area_struct *vma,
478 struct mempolicy *new)
Tejun Heo73d97142013-10-01 17:42:07 -0400479{
480 struct file *file = vma->vm_file;
481 struct sysfs_open_file *of = sysfs_of(file);
482 int ret;
483
484 if (!of->vm_ops)
485 return 0;
486
487 if (!sysfs_get_active(of->sd))
488 return -EINVAL;
489
490 ret = 0;
491 if (of->vm_ops->set_policy)
492 ret = of->vm_ops->set_policy(vma, new);
493
494 sysfs_put_active(of->sd);
495 return ret;
496}
497
Tejun Heofdbffaa2013-11-28 14:54:18 -0500498static struct mempolicy *kernfs_vma_get_policy(struct vm_area_struct *vma,
499 unsigned long addr)
Tejun Heo73d97142013-10-01 17:42:07 -0400500{
501 struct file *file = vma->vm_file;
502 struct sysfs_open_file *of = sysfs_of(file);
503 struct mempolicy *pol;
504
505 if (!of->vm_ops)
506 return vma->vm_policy;
507
508 if (!sysfs_get_active(of->sd))
509 return vma->vm_policy;
510
511 pol = vma->vm_policy;
512 if (of->vm_ops->get_policy)
513 pol = of->vm_ops->get_policy(vma, addr);
514
515 sysfs_put_active(of->sd);
516 return pol;
517}
518
Tejun Heofdbffaa2013-11-28 14:54:18 -0500519static int kernfs_vma_migrate(struct vm_area_struct *vma,
520 const nodemask_t *from, const nodemask_t *to,
521 unsigned long flags)
Tejun Heo73d97142013-10-01 17:42:07 -0400522{
523 struct file *file = vma->vm_file;
524 struct sysfs_open_file *of = sysfs_of(file);
525 int ret;
526
527 if (!of->vm_ops)
528 return 0;
529
530 if (!sysfs_get_active(of->sd))
531 return 0;
532
533 ret = 0;
534 if (of->vm_ops->migrate)
535 ret = of->vm_ops->migrate(vma, from, to, flags);
536
537 sysfs_put_active(of->sd);
538 return ret;
539}
540#endif
541
Tejun Heofdbffaa2013-11-28 14:54:18 -0500542static const struct vm_operations_struct kernfs_vm_ops = {
543 .open = kernfs_vma_open,
544 .fault = kernfs_vma_fault,
545 .page_mkwrite = kernfs_vma_page_mkwrite,
546 .access = kernfs_vma_access,
Tejun Heo73d97142013-10-01 17:42:07 -0400547#ifdef CONFIG_NUMA
Tejun Heofdbffaa2013-11-28 14:54:18 -0500548 .set_policy = kernfs_vma_set_policy,
549 .get_policy = kernfs_vma_get_policy,
550 .migrate = kernfs_vma_migrate,
Tejun Heo73d97142013-10-01 17:42:07 -0400551#endif
552};
553
Tejun Heofdbffaa2013-11-28 14:54:18 -0500554static int kernfs_file_mmap(struct file *file, struct vm_area_struct *vma)
Tejun Heo73d97142013-10-01 17:42:07 -0400555{
556 struct sysfs_open_file *of = sysfs_of(file);
Tejun Heof6acf8b2013-11-28 14:54:21 -0500557 const struct kernfs_ops *ops;
Tejun Heo73d97142013-10-01 17:42:07 -0400558 int rc;
559
560 mutex_lock(&of->mutex);
561
Tejun Heo73d97142013-10-01 17:42:07 -0400562 rc = -ENODEV;
563 if (!sysfs_get_active(of->sd))
564 goto out_unlock;
565
Tejun Heof6acf8b2013-11-28 14:54:21 -0500566 ops = kernfs_ops(of->sd);
567 if (ops->mmap)
568 rc = ops->mmap(of, vma);
Tejun Heo73d97142013-10-01 17:42:07 -0400569 if (rc)
570 goto out_put;
571
572 /*
573 * PowerPC's pci_mmap of legacy_mem uses shmem_zero_setup()
574 * to satisfy versions of X which crash if the mmap fails: that
575 * substitutes a new vm_file, and we don't then want bin_vm_ops.
576 */
577 if (vma->vm_file != file)
578 goto out_put;
579
580 rc = -EINVAL;
581 if (of->mmapped && of->vm_ops != vma->vm_ops)
582 goto out_put;
583
584 /*
585 * It is not possible to successfully wrap close.
586 * So error if someone is trying to use close.
587 */
588 rc = -EINVAL;
589 if (vma->vm_ops && vma->vm_ops->close)
590 goto out_put;
591
592 rc = 0;
593 of->mmapped = 1;
594 of->vm_ops = vma->vm_ops;
Tejun Heofdbffaa2013-11-28 14:54:18 -0500595 vma->vm_ops = &kernfs_vm_ops;
Tejun Heo73d97142013-10-01 17:42:07 -0400596out_put:
597 sysfs_put_active(of->sd);
598out_unlock:
599 mutex_unlock(&of->mutex);
600
601 return rc;
602}
603
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900604/**
605 * sysfs_get_open_dirent - get or create sysfs_open_dirent
606 * @sd: target sysfs_dirent
Tejun Heo58282d82013-10-01 17:41:59 -0400607 * @of: sysfs_open_file for this instance of open
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900608 *
609 * If @sd->s_attr.open exists, increment its reference count;
Tejun Heo58282d82013-10-01 17:41:59 -0400610 * otherwise, create one. @of is chained to the files list.
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900611 *
612 * LOCKING:
613 * Kernel thread context (may sleep).
614 *
615 * RETURNS:
616 * 0 on success, -errno on failure.
617 */
618static int sysfs_get_open_dirent(struct sysfs_dirent *sd,
Tejun Heo58282d82013-10-01 17:41:59 -0400619 struct sysfs_open_file *of)
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900620{
621 struct sysfs_open_dirent *od, *new_od = NULL;
622
623 retry:
Tejun Heoc75ec762013-10-01 17:41:58 -0400624 mutex_lock(&sysfs_open_file_mutex);
Neil Brown83db93f2009-09-15 16:05:51 -0700625 spin_lock_irq(&sysfs_open_dirent_lock);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900626
627 if (!sd->s_attr.open && new_od) {
628 sd->s_attr.open = new_od;
629 new_od = NULL;
630 }
631
632 od = sd->s_attr.open;
633 if (od) {
634 atomic_inc(&od->refcnt);
Tejun Heo58282d82013-10-01 17:41:59 -0400635 list_add_tail(&of->list, &od->files);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900636 }
637
Neil Brown83db93f2009-09-15 16:05:51 -0700638 spin_unlock_irq(&sysfs_open_dirent_lock);
Tejun Heoc75ec762013-10-01 17:41:58 -0400639 mutex_unlock(&sysfs_open_file_mutex);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900640
641 if (od) {
642 kfree(new_od);
643 return 0;
644 }
645
646 /* not there, initialize a new one and retry */
647 new_od = kmalloc(sizeof(*new_od), GFP_KERNEL);
648 if (!new_od)
649 return -ENOMEM;
650
651 atomic_set(&new_od->refcnt, 0);
Tejun Heoa4e8b912007-09-20 16:05:12 +0900652 atomic_set(&new_od->event, 1);
653 init_waitqueue_head(&new_od->poll);
Tejun Heo58282d82013-10-01 17:41:59 -0400654 INIT_LIST_HEAD(&new_od->files);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900655 goto retry;
656}
657
658/**
659 * sysfs_put_open_dirent - put sysfs_open_dirent
660 * @sd: target sysfs_dirent
Tejun Heo58282d82013-10-01 17:41:59 -0400661 * @of: associated sysfs_open_file
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900662 *
Tejun Heo58282d82013-10-01 17:41:59 -0400663 * Put @sd->s_attr.open and unlink @of from the files list. If
664 * reference count reaches zero, disassociate and free it.
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900665 *
666 * LOCKING:
667 * None.
668 */
669static void sysfs_put_open_dirent(struct sysfs_dirent *sd,
Tejun Heo58282d82013-10-01 17:41:59 -0400670 struct sysfs_open_file *of)
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900671{
672 struct sysfs_open_dirent *od = sd->s_attr.open;
Neil Brown83db93f2009-09-15 16:05:51 -0700673 unsigned long flags;
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900674
Tejun Heoc75ec762013-10-01 17:41:58 -0400675 mutex_lock(&sysfs_open_file_mutex);
Neil Brown83db93f2009-09-15 16:05:51 -0700676 spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900677
Tejun Heo73d97142013-10-01 17:42:07 -0400678 if (of)
679 list_del(&of->list);
680
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900681 if (atomic_dec_and_test(&od->refcnt))
682 sd->s_attr.open = NULL;
683 else
684 od = NULL;
685
Neil Brown83db93f2009-09-15 16:05:51 -0700686 spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
Tejun Heoc75ec762013-10-01 17:41:58 -0400687 mutex_unlock(&sysfs_open_file_mutex);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900688
689 kfree(od);
690}
691
Tejun Heoc6fb4492013-11-28 14:54:19 -0500692static int kernfs_file_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
Tejun Heo3e519032007-06-14 03:45:15 +0900694 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
Tejun Heof6acf8b2013-11-28 14:54:21 -0500695 const struct kernfs_ops *ops;
Tejun Heo58282d82013-10-01 17:41:59 -0400696 struct sysfs_open_file *of;
Tejun Heo027a4852013-11-17 11:17:36 +0900697 bool has_read, has_write, has_mmap;
Kay Sievers000f2a42007-11-02 13:47:53 +0100698 int error = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800700 if (!sysfs_get_active(attr_sd))
Tejun Heo0ab66082007-06-14 03:45:16 +0900701 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Tejun Heof6acf8b2013-11-28 14:54:21 -0500703 ops = kernfs_ops(attr_sd);
Tejun Heo49fe6042013-10-01 17:42:08 -0400704
Tejun Heof6acf8b2013-11-28 14:54:21 -0500705 has_read = ops->seq_show || ops->read || ops->mmap;
706 has_write = ops->write || ops->mmap;
707 has_mmap = ops->mmap;
Tejun Heo49fe6042013-10-01 17:42:08 -0400708
709 /* check perms and supported operations */
710 if ((file->f_mode & FMODE_WRITE) &&
711 (!(inode->i_mode & S_IWUGO) || !has_write))
Tejun Heo7b595752007-06-14 03:45:17 +0900712 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Tejun Heo49fe6042013-10-01 17:42:08 -0400714 if ((file->f_mode & FMODE_READ) &&
715 (!(inode->i_mode & S_IRUGO) || !has_read))
716 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Tejun Heo13c589d2013-10-01 17:42:02 -0400718 /* allocate a sysfs_open_file for the file */
Tejun Heo0ab66082007-06-14 03:45:16 +0900719 error = -ENOMEM;
Tejun Heo58282d82013-10-01 17:41:59 -0400720 of = kzalloc(sizeof(struct sysfs_open_file), GFP_KERNEL);
721 if (!of)
Tejun Heo7b595752007-06-14 03:45:17 +0900722 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Tejun Heo027a4852013-11-17 11:17:36 +0900724 /*
725 * The following is done to give a different lockdep key to
726 * @of->mutex for files which implement mmap. This is a rather
727 * crude way to avoid false positive lockdep warning around
728 * mm->mmap_sem - mmap nests @of->mutex under mm->mmap_sem and
729 * reading /sys/block/sda/trace/act_mask grabs sr_mutex, under
730 * which mm->mmap_sem nests, while holding @of->mutex. As each
731 * open file has a separate mutex, it's okay as long as those don't
732 * happen on the same file. At this point, we can't easily give
733 * each file a separate locking class. Let's differentiate on
734 * whether the file has mmap or not for now.
735 */
736 if (has_mmap)
737 mutex_init(&of->mutex);
738 else
739 mutex_init(&of->mutex);
740
Tejun Heobcafe4e2013-10-01 17:42:00 -0400741 of->sd = attr_sd;
742 of->file = file;
Tejun Heo13c589d2013-10-01 17:42:02 -0400743
744 /*
Tejun Heo49fe6042013-10-01 17:42:08 -0400745 * Always instantiate seq_file even if read access doesn't use
746 * seq_file or is not requested. This unifies private data access
747 * and readable regular files are the vast majority anyway.
Tejun Heo13c589d2013-10-01 17:42:02 -0400748 */
Tejun Heof6acf8b2013-11-28 14:54:21 -0500749 if (ops->seq_show)
Tejun Heoc2b19da2013-11-28 14:54:16 -0500750 error = seq_open(file, &kernfs_seq_ops);
Tejun Heof6acf8b2013-11-28 14:54:21 -0500751 else
752 error = seq_open(file, NULL);
Tejun Heo13c589d2013-10-01 17:42:02 -0400753 if (error)
754 goto err_free;
755
Tejun Heoc2b19da2013-11-28 14:54:16 -0500756 ((struct seq_file *)file->private_data)->private = of;
757
Tejun Heo13c589d2013-10-01 17:42:02 -0400758 /* seq_file clears PWRITE unconditionally, restore it if WRITE */
759 if (file->f_mode & FMODE_WRITE)
760 file->f_mode |= FMODE_PWRITE;
Tejun Heo0ab66082007-06-14 03:45:16 +0900761
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900762 /* make sure we have open dirent struct */
Tejun Heo58282d82013-10-01 17:41:59 -0400763 error = sysfs_get_open_dirent(attr_sd, of);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900764 if (error)
Tejun Heo13c589d2013-10-01 17:42:02 -0400765 goto err_close;
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900766
Tejun Heob05f0542007-09-20 16:05:10 +0900767 /* open succeeded, put active references */
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800768 sysfs_put_active(attr_sd);
Tejun Heo0ab66082007-06-14 03:45:16 +0900769 return 0;
770
Tejun Heo13c589d2013-10-01 17:42:02 -0400771err_close:
Tejun Heoc2b19da2013-11-28 14:54:16 -0500772 seq_release(inode, file);
Tejun Heo13c589d2013-10-01 17:42:02 -0400773err_free:
Tejun Heo58282d82013-10-01 17:41:59 -0400774 kfree(of);
Tejun Heo13c589d2013-10-01 17:42:02 -0400775err_out:
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800776 sysfs_put_active(attr_sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 return error;
778}
779
Tejun Heoc6fb4492013-11-28 14:54:19 -0500780static int kernfs_file_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900782 struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata;
Tejun Heo13c589d2013-10-01 17:42:02 -0400783 struct sysfs_open_file *of = sysfs_of(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Tejun Heo58282d82013-10-01 17:41:59 -0400785 sysfs_put_open_dirent(sd, of);
Tejun Heoc2b19da2013-11-28 14:54:16 -0500786 seq_release(inode, filp);
Tejun Heo58282d82013-10-01 17:41:59 -0400787 kfree(of);
Tejun Heo50ab1a72007-09-20 16:05:10 +0900788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 return 0;
790}
791
Tejun Heo73d97142013-10-01 17:42:07 -0400792void sysfs_unmap_bin_file(struct sysfs_dirent *sd)
793{
794 struct sysfs_open_dirent *od;
795 struct sysfs_open_file *of;
796
Tejun Heof6acf8b2013-11-28 14:54:21 -0500797 if (!(sd->s_flags & SYSFS_FLAG_HAS_MMAP))
Tejun Heo73d97142013-10-01 17:42:07 -0400798 return;
799
800 spin_lock_irq(&sysfs_open_dirent_lock);
801 od = sd->s_attr.open;
802 if (od)
803 atomic_inc(&od->refcnt);
804 spin_unlock_irq(&sysfs_open_dirent_lock);
805 if (!od)
806 return;
807
808 mutex_lock(&sysfs_open_file_mutex);
809 list_for_each_entry(of, &od->files, list) {
810 struct inode *inode = file_inode(of->file);
811 unmap_mapping_range(inode->i_mapping, 0, 0, 1);
812 }
813 mutex_unlock(&sysfs_open_file_mutex);
814
815 sysfs_put_open_dirent(sd, NULL);
816}
817
NeilBrown4508a7a2006-03-20 17:53:53 +1100818/* Sysfs attribute files are pollable. The idea is that you read
819 * the content and then you use 'poll' or 'select' to wait for
820 * the content to change. When the content changes (assuming the
821 * manager for the kobject supports notification), poll will
822 * return POLLERR|POLLPRI, and select will return the fd whether
823 * it is waiting for read, write, or exceptions.
824 * Once poll/select indicates that the value has changed, you
Dan Williams2424b5d2008-04-07 15:35:01 -0700825 * need to close and re-open the file, or seek to 0 and read again.
NeilBrown4508a7a2006-03-20 17:53:53 +1100826 * Reminder: this only works for attributes which actively support
827 * it, and it is not possible to test an attribute from userspace
Rolf Eike Beera93720e2007-08-10 13:51:07 -0700828 * to see if it supports poll (Neither 'poll' nor 'select' return
NeilBrown4508a7a2006-03-20 17:53:53 +1100829 * an appropriate error code). When in doubt, set a suitable timeout value.
830 */
Tejun Heoc6fb4492013-11-28 14:54:19 -0500831static unsigned int kernfs_file_poll(struct file *filp, poll_table *wait)
NeilBrown4508a7a2006-03-20 17:53:53 +1100832{
Tejun Heo13c589d2013-10-01 17:42:02 -0400833 struct sysfs_open_file *of = sysfs_of(filp);
Tejun Heo0ab66082007-06-14 03:45:16 +0900834 struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata;
Tejun Heoa4e8b912007-09-20 16:05:12 +0900835 struct sysfs_open_dirent *od = attr_sd->s_attr.open;
Tejun Heo0ab66082007-06-14 03:45:16 +0900836
837 /* need parent for the kobj, grab both */
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800838 if (!sysfs_get_active(attr_sd))
Tejun Heo0ab66082007-06-14 03:45:16 +0900839 goto trigger;
NeilBrown4508a7a2006-03-20 17:53:53 +1100840
Tejun Heoa4e8b912007-09-20 16:05:12 +0900841 poll_wait(filp, &od->poll, wait);
NeilBrown4508a7a2006-03-20 17:53:53 +1100842
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800843 sysfs_put_active(attr_sd);
NeilBrown4508a7a2006-03-20 17:53:53 +1100844
Tejun Heo58282d82013-10-01 17:41:59 -0400845 if (of->event != atomic_read(&od->event))
Tejun Heo0ab66082007-06-14 03:45:16 +0900846 goto trigger;
847
KOSAKI Motohiro1af35572009-04-09 13:53:22 +0900848 return DEFAULT_POLLMASK;
Tejun Heo0ab66082007-06-14 03:45:16 +0900849
850 trigger:
KOSAKI Motohiro1af35572009-04-09 13:53:22 +0900851 return DEFAULT_POLLMASK|POLLERR|POLLPRI;
NeilBrown4508a7a2006-03-20 17:53:53 +1100852}
853
Neil Brownf1282c82008-07-16 08:58:04 +1000854void sysfs_notify_dirent(struct sysfs_dirent *sd)
855{
856 struct sysfs_open_dirent *od;
Neil Brown83db93f2009-09-15 16:05:51 -0700857 unsigned long flags;
Neil Brownf1282c82008-07-16 08:58:04 +1000858
Neil Brown83db93f2009-09-15 16:05:51 -0700859 spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
Neil Brownf1282c82008-07-16 08:58:04 +1000860
Nick Dyerfc60bb82013-06-07 15:45:13 +0100861 if (!WARN_ON(sysfs_type(sd) != SYSFS_KOBJ_ATTR)) {
862 od = sd->s_attr.open;
863 if (od) {
864 atomic_inc(&od->event);
865 wake_up_interruptible(&od->poll);
866 }
Neil Brownf1282c82008-07-16 08:58:04 +1000867 }
868
Neil Brown83db93f2009-09-15 16:05:51 -0700869 spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
Neil Brownf1282c82008-07-16 08:58:04 +1000870}
871EXPORT_SYMBOL_GPL(sysfs_notify_dirent);
872
Trent Piepho8c0e3992008-09-25 16:45:13 -0700873void sysfs_notify(struct kobject *k, const char *dir, const char *attr)
NeilBrown4508a7a2006-03-20 17:53:53 +1100874{
Tejun Heo51225032007-06-14 04:27:25 +0900875 struct sysfs_dirent *sd = k->sd;
NeilBrown4508a7a2006-03-20 17:53:53 +1100876
Tejun Heo51225032007-06-14 04:27:25 +0900877 mutex_lock(&sysfs_mutex);
NeilBrown4508a7a2006-03-20 17:53:53 +1100878
Tejun Heo51225032007-06-14 04:27:25 +0900879 if (sd && dir)
Tejun Heocfec0bc2013-09-11 22:29:09 -0400880 sd = sysfs_find_dirent(sd, dir, NULL);
Tejun Heo51225032007-06-14 04:27:25 +0900881 if (sd && attr)
Tejun Heocfec0bc2013-09-11 22:29:09 -0400882 sd = sysfs_find_dirent(sd, attr, NULL);
Neil Brownf1282c82008-07-16 08:58:04 +1000883 if (sd)
884 sysfs_notify_dirent(sd);
Tejun Heo51225032007-06-14 04:27:25 +0900885
886 mutex_unlock(&sysfs_mutex);
NeilBrown4508a7a2006-03-20 17:53:53 +1100887}
888EXPORT_SYMBOL_GPL(sysfs_notify);
889
Tejun Heoc6fb4492013-11-28 14:54:19 -0500890const struct file_operations kernfs_file_operations = {
Tejun Heoc2b19da2013-11-28 14:54:16 -0500891 .read = kernfs_file_read,
Tejun Heo50b38ca2013-11-28 14:54:17 -0500892 .write = kernfs_file_write,
Tejun Heo044e3bc2013-11-01 13:16:53 -0400893 .llseek = generic_file_llseek,
Tejun Heofdbffaa2013-11-28 14:54:18 -0500894 .mmap = kernfs_file_mmap,
Tejun Heoc6fb4492013-11-28 14:54:19 -0500895 .open = kernfs_file_open,
896 .release = kernfs_file_release,
897 .poll = kernfs_file_poll,
Tejun Heof9b9a622013-10-01 17:42:05 -0400898};
899
Tejun Heof6acf8b2013-11-28 14:54:21 -0500900static const struct kernfs_ops sysfs_file_kfops_empty = {
901};
902
903static const struct kernfs_ops sysfs_file_kfops_ro = {
904 .seq_show = sysfs_kf_seq_show,
905};
906
907static const struct kernfs_ops sysfs_file_kfops_wo = {
908 .write = sysfs_kf_write,
909};
910
911static const struct kernfs_ops sysfs_file_kfops_rw = {
912 .seq_show = sysfs_kf_seq_show,
913 .write = sysfs_kf_write,
914};
915
916static const struct kernfs_ops sysfs_bin_kfops_ro = {
917 .read = sysfs_kf_bin_read,
918};
919
920static const struct kernfs_ops sysfs_bin_kfops_wo = {
921 .write = sysfs_kf_bin_write,
922};
923
924static const struct kernfs_ops sysfs_bin_kfops_rw = {
925 .read = sysfs_kf_bin_read,
926 .write = sysfs_kf_bin_write,
927 .mmap = sysfs_kf_bin_mmap,
928};
929
Tejun Heo58292cbe2013-09-11 22:29:04 -0400930int sysfs_add_file_mode_ns(struct sysfs_dirent *dir_sd,
Tejun Heoa7dc66d2013-11-28 14:54:23 -0500931 const struct attribute *attr, bool is_bin,
Tejun Heo496f7392013-11-28 14:54:24 -0500932 umode_t mode, const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
Tejun Heof6acf8b2013-11-28 14:54:21 -0500934 const struct kernfs_ops *ops;
Tejun Heoa26cd722007-06-14 03:45:14 +0900935 struct sysfs_dirent *sd;
Tejun Heo471bd7b2013-11-28 14:54:22 -0500936 loff_t size;
Tejun Heoa26cd722007-06-14 03:45:14 +0900937
Tejun Heoa7dc66d2013-11-28 14:54:23 -0500938 if (!is_bin) {
Tejun Heof6acf8b2013-11-28 14:54:21 -0500939 struct kobject *kobj = dir_sd->priv;
940 const struct sysfs_ops *sysfs_ops = kobj->ktype->sysfs_ops;
941
942 /* every kobject with an attribute needs a ktype assigned */
943 if (WARN(!sysfs_ops, KERN_ERR
944 "missing sysfs attribute operations for kobject: %s\n",
945 kobject_name(kobj)))
946 return -EINVAL;
947
948 if (sysfs_ops->show && sysfs_ops->store)
949 ops = &sysfs_file_kfops_rw;
950 else if (sysfs_ops->show)
951 ops = &sysfs_file_kfops_ro;
952 else if (sysfs_ops->store)
953 ops = &sysfs_file_kfops_wo;
954 else
955 ops = &sysfs_file_kfops_empty;
Tejun Heo471bd7b2013-11-28 14:54:22 -0500956
957 size = PAGE_SIZE;
Tejun Heof6acf8b2013-11-28 14:54:21 -0500958 } else {
959 struct bin_attribute *battr = (void *)attr;
960
961 if ((battr->read && battr->write) || battr->mmap)
962 ops = &sysfs_bin_kfops_rw;
963 else if (battr->read)
964 ops = &sysfs_bin_kfops_ro;
965 else if (battr->write)
966 ops = &sysfs_bin_kfops_wo;
967 else
968 ops = &sysfs_file_kfops_empty;
Tejun Heo471bd7b2013-11-28 14:54:22 -0500969
970 size = battr->size;
Tejun Heof6acf8b2013-11-28 14:54:21 -0500971 }
972
Tejun Heo496f7392013-11-28 14:54:24 -0500973 sd = kernfs_create_file_ns(dir_sd, attr->name, mode, size,
974 ops, (void *)attr, ns);
975 if (IS_ERR(sd)) {
976 if (PTR_ERR(sd) == -EEXIST)
977 sysfs_warn_dup(dir_sd, attr->name);
978 return PTR_ERR(sd);
979 }
980 return 0;
981}
982
983/**
984 * kernfs_create_file_ns - create a file
985 * @parent: directory to create the file in
986 * @name: name of the file
987 * @mode: mode of the file
988 * @size: size of the file
989 * @ops: kernfs operations for the file
990 * @priv: private data for the file
991 * @ns: optional namespace tag of the file
992 *
993 * Returns the created node on success, ERR_PTR() value on error.
994 */
995struct sysfs_dirent *kernfs_create_file_ns(struct sysfs_dirent *parent,
996 const char *name,
997 umode_t mode, loff_t size,
998 const struct kernfs_ops *ops,
999 void *priv, const void *ns)
1000{
1001 struct sysfs_addrm_cxt acxt;
1002 struct sysfs_dirent *sd;
1003 int rc;
1004
1005 sd = sysfs_new_dirent(name, (mode & S_IALLUGO) | S_IFREG,
1006 SYSFS_KOBJ_ATTR);
Tejun Heo3007e992007-06-14 04:27:23 +09001007 if (!sd)
Tejun Heo496f7392013-11-28 14:54:24 -05001008 return ERR_PTR(-ENOMEM);
Eric W. Biederman487505c2011-10-12 21:53:38 +00001009
Tejun Heof6acf8b2013-11-28 14:54:21 -05001010 sd->s_attr.ops = ops;
Tejun Heo471bd7b2013-11-28 14:54:22 -05001011 sd->s_attr.size = size;
Eric W. Biederman487505c2011-10-12 21:53:38 +00001012 sd->s_ns = ns;
Tejun Heo496f7392013-11-28 14:54:24 -05001013 sd->priv = priv;
Eric W. Biedermana2db6842010-02-11 15:20:00 -08001014 sysfs_dirent_init_lockdep(sd);
Tejun Heoa26cd722007-06-14 03:45:14 +09001015
Tejun Heof6acf8b2013-11-28 14:54:21 -05001016 /*
1017 * sd->s_attr.ops is accesible only while holding active ref. We
1018 * need to know whether some ops are implemented outside active
1019 * ref. Cache their existence in flags.
1020 */
1021 if (ops->seq_show)
1022 sd->s_flags |= SYSFS_FLAG_HAS_SEQ_SHOW;
1023 if (ops->mmap)
1024 sd->s_flags |= SYSFS_FLAG_HAS_MMAP;
1025
Tejun Heod69ac5a2013-09-18 17:15:35 -04001026 sysfs_addrm_start(&acxt);
Tejun Heo2d0cfbe2013-11-28 14:54:25 -05001027 rc = sysfs_add_one(&acxt, sd, parent);
Tejun Heo23dc2792007-08-02 21:38:03 +09001028 sysfs_addrm_finish(&acxt);
Tejun Heo3007e992007-06-14 04:27:23 +09001029
Tejun Heo496f7392013-11-28 14:54:24 -05001030 if (rc) {
Tejun Heo967e35d2007-07-18 16:38:11 +09001031 sysfs_put(sd);
Tejun Heo496f7392013-11-28 14:54:24 -05001032 return ERR_PTR(rc);
1033 }
1034 return sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035}
1036
James Bottomley0f423892008-03-20 20:47:52 -05001037int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,
Tejun Heoa7dc66d2013-11-28 14:54:23 -05001038 bool is_bin)
James Bottomley0f423892008-03-20 20:47:52 -05001039{
Tejun Heoa7dc66d2013-11-28 14:54:23 -05001040 return sysfs_add_file_mode_ns(dir_sd, attr, is_bin, attr->mode, NULL);
James Bottomley0f423892008-03-20 20:47:52 -05001041}
1042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043/**
Tejun Heo58292cbe2013-09-11 22:29:04 -04001044 * sysfs_create_file_ns - create an attribute file for an object with custom ns
1045 * @kobj: object we're creating for
1046 * @attr: attribute descriptor
1047 * @ns: namespace the new file should belong to
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 */
Tejun Heo58292cbe2013-09-11 22:29:04 -04001049int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
1050 const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
Tejun Heo608e2662007-06-14 04:27:22 +09001052 BUG_ON(!kobj || !kobj->sd || !attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Tejun Heoa7dc66d2013-11-28 14:54:23 -05001054 return sysfs_add_file_mode_ns(kobj->sd, attr, false, attr->mode, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056}
Tejun Heo58292cbe2013-09-11 22:29:04 -04001057EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Andi Kleen1c205ae2010-01-05 12:48:01 +01001059int sysfs_create_files(struct kobject *kobj, const struct attribute **ptr)
1060{
1061 int err = 0;
1062 int i;
1063
1064 for (i = 0; ptr[i] && !err; i++)
1065 err = sysfs_create_file(kobj, ptr[i]);
1066 if (err)
1067 while (--i >= 0)
1068 sysfs_remove_file(kobj, ptr[i]);
1069 return err;
1070}
Greg Kroah-Hartman1b866752013-08-21 16:17:47 -07001071EXPORT_SYMBOL_GPL(sysfs_create_files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073/**
Alan Sterndfa87c82007-02-20 15:02:44 -05001074 * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
1075 * @kobj: object we're acting for.
1076 * @attr: attribute descriptor.
1077 * @group: group name.
1078 */
1079int sysfs_add_file_to_group(struct kobject *kobj,
1080 const struct attribute *attr, const char *group)
1081{
Tejun Heo608e2662007-06-14 04:27:22 +09001082 struct sysfs_dirent *dir_sd;
Alan Sterndfa87c82007-02-20 15:02:44 -05001083 int error;
1084
James Bottomley11f24fb2008-01-02 18:44:05 -06001085 if (group)
Tejun Heo388975c2013-09-11 23:19:13 -04001086 dir_sd = sysfs_get_dirent(kobj->sd, group);
James Bottomley11f24fb2008-01-02 18:44:05 -06001087 else
1088 dir_sd = sysfs_get(kobj->sd);
1089
Tejun Heo608e2662007-06-14 04:27:22 +09001090 if (!dir_sd)
1091 return -ENOENT;
1092
Tejun Heoa7dc66d2013-11-28 14:54:23 -05001093 error = sysfs_add_file(dir_sd, attr, false);
Tejun Heo608e2662007-06-14 04:27:22 +09001094 sysfs_put(dir_sd);
1095
Alan Sterndfa87c82007-02-20 15:02:44 -05001096 return error;
1097}
1098EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
1099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100/**
Kay Sievers31e5abe2005-04-18 21:57:32 -07001101 * sysfs_chmod_file - update the modified mode value on an object attribute.
1102 * @kobj: object we're acting for.
1103 * @attr: attribute descriptor.
1104 * @mode: file permissions.
1105 *
1106 */
Jean Delvare49c19402010-07-02 16:54:05 +02001107int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
Al Viro48176a92011-07-24 03:40:40 -04001108 umode_t mode)
Kay Sievers31e5abe2005-04-18 21:57:32 -07001109{
Eric W. Biederman06fc0d62009-11-20 16:08:54 -08001110 struct sysfs_dirent *sd;
Maneesh Sonibc062b12005-07-29 12:13:35 -07001111 struct iattr newattrs;
Tejun Heo51225032007-06-14 04:27:25 +09001112 int rc;
Kay Sievers31e5abe2005-04-18 21:57:32 -07001113
Tejun Heo5d604182013-11-23 17:21:52 -05001114 sd = sysfs_get_dirent(kobj->sd, attr->name);
Eric W. Biederman06fc0d62009-11-20 16:08:54 -08001115 if (!sd)
Tejun Heo5d604182013-11-23 17:21:52 -05001116 return -ENOENT;
Tejun Heo51225032007-06-14 04:27:25 +09001117
Eric W. Biederman06fc0d62009-11-20 16:08:54 -08001118 newattrs.ia_mode = (mode & S_IALLUGO) | (sd->s_mode & ~S_IALLUGO);
Eric W. Biederman4c6974f2009-11-07 23:27:02 -08001119 newattrs.ia_valid = ATTR_MODE;
Tejun Heof88123e2007-09-20 16:05:10 +09001120
Tejun Heo5d604182013-11-23 17:21:52 -05001121 rc = kernfs_setattr(sd, &newattrs);
1122
1123 sysfs_put(sd);
Tejun Heo51225032007-06-14 04:27:25 +09001124 return rc;
Kay Sievers31e5abe2005-04-18 21:57:32 -07001125}
1126EXPORT_SYMBOL_GPL(sysfs_chmod_file);
1127
Kay Sievers31e5abe2005-04-18 21:57:32 -07001128/**
Tejun Heo58292cbe2013-09-11 22:29:04 -04001129 * sysfs_remove_file_ns - remove an object attribute with a custom ns tag
1130 * @kobj: object we're acting for
1131 * @attr: attribute descriptor
1132 * @ns: namespace tag of the file to remove
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 *
Tejun Heo58292cbe2013-09-11 22:29:04 -04001134 * Hash the attribute name and namespace tag and kill the victim.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 */
Tejun Heo58292cbe2013-09-11 22:29:04 -04001136void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
1137 const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
Tejun Heo58292cbe2013-09-11 22:29:04 -04001139 struct sysfs_dirent *dir_sd = kobj->sd;
Eric W. Biederman487505c2011-10-12 21:53:38 +00001140
Tejun Heo879f40d2013-11-23 17:21:49 -05001141 kernfs_remove_by_name_ns(dir_sd, attr->name, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142}
Tejun Heo58292cbe2013-09-11 22:29:04 -04001143EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Greg Kroah-Hartman1b18dc22013-08-21 16:28:26 -07001145void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr)
Andi Kleen1c205ae2010-01-05 12:48:01 +01001146{
1147 int i;
1148 for (i = 0; ptr[i]; i++)
1149 sysfs_remove_file(kobj, ptr[i]);
1150}
Greg Kroah-Hartman1b866752013-08-21 16:17:47 -07001151EXPORT_SYMBOL_GPL(sysfs_remove_files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Alan Sterndfa87c82007-02-20 15:02:44 -05001153/**
1154 * sysfs_remove_file_from_group - remove an attribute file from a group.
1155 * @kobj: object we're acting for.
1156 * @attr: attribute descriptor.
1157 * @group: group name.
1158 */
1159void sysfs_remove_file_from_group(struct kobject *kobj,
1160 const struct attribute *attr, const char *group)
1161{
Tejun Heo608e2662007-06-14 04:27:22 +09001162 struct sysfs_dirent *dir_sd;
Alan Sterndfa87c82007-02-20 15:02:44 -05001163
James Bottomley11f24fb2008-01-02 18:44:05 -06001164 if (group)
Tejun Heo388975c2013-09-11 23:19:13 -04001165 dir_sd = sysfs_get_dirent(kobj->sd, group);
James Bottomley11f24fb2008-01-02 18:44:05 -06001166 else
1167 dir_sd = sysfs_get(kobj->sd);
Tejun Heo608e2662007-06-14 04:27:22 +09001168 if (dir_sd) {
Tejun Heo879f40d2013-11-23 17:21:49 -05001169 kernfs_remove_by_name(dir_sd, attr->name);
Tejun Heo608e2662007-06-14 04:27:22 +09001170 sysfs_put(dir_sd);
Alan Sterndfa87c82007-02-20 15:02:44 -05001171 }
1172}
1173EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
1174
Tejun Heo3124eb12013-10-01 17:42:09 -04001175/**
1176 * sysfs_create_bin_file - create binary file for object.
1177 * @kobj: object.
1178 * @attr: attribute descriptor.
1179 */
1180int sysfs_create_bin_file(struct kobject *kobj,
1181 const struct bin_attribute *attr)
1182{
1183 BUG_ON(!kobj || !kobj->sd || !attr);
1184
Tejun Heoa7dc66d2013-11-28 14:54:23 -05001185 return sysfs_add_file(kobj->sd, &attr->attr, true);
Tejun Heo3124eb12013-10-01 17:42:09 -04001186}
1187EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
1188
1189/**
1190 * sysfs_remove_bin_file - remove binary file for object.
1191 * @kobj: object.
1192 * @attr: attribute descriptor.
1193 */
1194void sysfs_remove_bin_file(struct kobject *kobj,
1195 const struct bin_attribute *attr)
1196{
Tejun Heo879f40d2013-11-23 17:21:49 -05001197 kernfs_remove_by_name(kobj->sd, attr->attr.name);
Tejun Heo3124eb12013-10-01 17:42:09 -04001198}
1199EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);
1200
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001201struct sysfs_schedule_callback_struct {
Alex Chiang66942062009-03-13 12:07:36 -06001202 struct list_head workq_list;
1203 struct kobject *kobj;
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001204 void (*func)(void *);
1205 void *data;
Alan Stern523ded72007-04-26 00:12:04 -07001206 struct module *owner;
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001207 struct work_struct work;
1208};
1209
Alex Chiangd1102712009-03-25 15:11:36 -06001210static struct workqueue_struct *sysfs_workqueue;
Alex Chiang66942062009-03-13 12:07:36 -06001211static DEFINE_MUTEX(sysfs_workq_mutex);
1212static LIST_HEAD(sysfs_workq);
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001213static void sysfs_schedule_callback_work(struct work_struct *work)
1214{
1215 struct sysfs_schedule_callback_struct *ss = container_of(work,
1216 struct sysfs_schedule_callback_struct, work);
1217
1218 (ss->func)(ss->data);
1219 kobject_put(ss->kobj);
Alan Stern523ded72007-04-26 00:12:04 -07001220 module_put(ss->owner);
Alex Chiang66942062009-03-13 12:07:36 -06001221 mutex_lock(&sysfs_workq_mutex);
1222 list_del(&ss->workq_list);
1223 mutex_unlock(&sysfs_workq_mutex);
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001224 kfree(ss);
1225}
1226
1227/**
1228 * sysfs_schedule_callback - helper to schedule a callback for a kobject
1229 * @kobj: object we're acting for.
1230 * @func: callback function to invoke later.
1231 * @data: argument to pass to @func.
Alan Stern523ded72007-04-26 00:12:04 -07001232 * @owner: module owning the callback code
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001233 *
1234 * sysfs attribute methods must not unregister themselves or their parent
1235 * kobject (which would amount to the same thing). Attempts to do so will
1236 * deadlock, since unregistration is mutually exclusive with driver
1237 * callbacks.
1238 *
1239 * Instead methods can call this routine, which will attempt to allocate
1240 * and schedule a workqueue request to call back @func with @data as its
1241 * argument in the workqueue's process context. @kobj will be pinned
1242 * until @func returns.
1243 *
1244 * Returns 0 if the request was submitted, -ENOMEM if storage could not
Alex Chiang66942062009-03-13 12:07:36 -06001245 * be allocated, -ENODEV if a reference to @owner isn't available,
1246 * -EAGAIN if a callback has already been scheduled for @kobj.
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001247 */
1248int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
Alan Stern523ded72007-04-26 00:12:04 -07001249 void *data, struct module *owner)
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001250{
Alex Chiang66942062009-03-13 12:07:36 -06001251 struct sysfs_schedule_callback_struct *ss, *tmp;
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001252
Alan Stern523ded72007-04-26 00:12:04 -07001253 if (!try_module_get(owner))
1254 return -ENODEV;
Alex Chiang66942062009-03-13 12:07:36 -06001255
1256 mutex_lock(&sysfs_workq_mutex);
1257 list_for_each_entry_safe(ss, tmp, &sysfs_workq, workq_list)
1258 if (ss->kobj == kobj) {
Alex Chiangd1102712009-03-25 15:11:36 -06001259 module_put(owner);
Alex Chiang66942062009-03-13 12:07:36 -06001260 mutex_unlock(&sysfs_workq_mutex);
1261 return -EAGAIN;
1262 }
1263 mutex_unlock(&sysfs_workq_mutex);
1264
Alex Chiangd1102712009-03-25 15:11:36 -06001265 if (sysfs_workqueue == NULL) {
Andrew Morton086a3772009-05-07 12:36:53 -07001266 sysfs_workqueue = create_singlethread_workqueue("sysfsd");
Alex Chiangd1102712009-03-25 15:11:36 -06001267 if (sysfs_workqueue == NULL) {
1268 module_put(owner);
1269 return -ENOMEM;
1270 }
1271 }
1272
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001273 ss = kmalloc(sizeof(*ss), GFP_KERNEL);
Alan Stern523ded72007-04-26 00:12:04 -07001274 if (!ss) {
1275 module_put(owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001276 return -ENOMEM;
Alan Stern523ded72007-04-26 00:12:04 -07001277 }
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001278 kobject_get(kobj);
1279 ss->kobj = kobj;
1280 ss->func = func;
1281 ss->data = data;
Alan Stern523ded72007-04-26 00:12:04 -07001282 ss->owner = owner;
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001283 INIT_WORK(&ss->work, sysfs_schedule_callback_work);
Alex Chiang66942062009-03-13 12:07:36 -06001284 INIT_LIST_HEAD(&ss->workq_list);
1285 mutex_lock(&sysfs_workq_mutex);
1286 list_add_tail(&ss->workq_list, &sysfs_workq);
1287 mutex_unlock(&sysfs_workq_mutex);
Alex Chiangd1102712009-03-25 15:11:36 -06001288 queue_work(sysfs_workqueue, &ss->work);
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001289 return 0;
1290}
1291EXPORT_SYMBOL_GPL(sysfs_schedule_callback);