blob: a43df04c81f9fa171ee3a9bf808eb010d5484059 [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 Heo58282d82013-10-01 17:41:59 -040050struct sysfs_open_file {
Tejun Heobcafe4e2013-10-01 17:42:00 -040051 struct sysfs_dirent *sd;
52 struct file *file;
Dave Young52e8c202007-07-26 11:03:54 +000053 struct mutex mutex;
Tejun Heo73107cb2007-06-14 03:45:16 +090054 int event;
Tejun Heo85a4ffa2007-09-20 16:05:12 +090055 struct list_head list;
Tejun Heo73d97142013-10-01 17:42:07 -040056
57 bool mmapped;
58 const struct vm_operations_struct *vm_ops;
Tejun Heo73107cb2007-06-14 03:45:16 +090059};
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Tejun Heof9b9a622013-10-01 17:42:05 -040061static bool sysfs_is_bin(struct sysfs_dirent *sd)
62{
63 return sysfs_type(sd) == SYSFS_KOBJ_BIN_ATTR;
64}
65
Tejun Heo13c589d2013-10-01 17:42:02 -040066static struct sysfs_open_file *sysfs_of(struct file *file)
67{
68 return ((struct seq_file *)file->private_data)->private;
69}
70
Tejun Heo375b6112013-10-01 17:41:57 -040071/*
72 * Determine ktype->sysfs_ops for the given sysfs_dirent. This function
73 * must be called while holding an active reference.
74 */
75static const struct sysfs_ops *sysfs_file_ops(struct sysfs_dirent *sd)
76{
Tejun Heo7c6e2d32013-11-28 14:54:14 -050077 struct kobject *kobj = sd->s_parent->priv;
Tejun Heo375b6112013-10-01 17:41:57 -040078
Tejun Heo785a1622013-10-14 09:27:11 -040079 if (!sysfs_ignore_lockdep(sd))
80 lockdep_assert_held(sd);
Tejun Heo375b6112013-10-01 17:41:57 -040081 return kobj->ktype ? kobj->ktype->sysfs_ops : NULL;
82}
83
Tejun Heo13c589d2013-10-01 17:42:02 -040084/*
85 * Reads on sysfs are handled through seq_file, which takes care of hairy
86 * details like buffering and seeking. The following function pipes
87 * sysfs_ops->show() result through seq_file.
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 */
Tejun Heoc2b19da2013-11-28 14:54:16 -050089static int sysfs_kf_seq_show(struct seq_file *sf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Tejun Heo13c589d2013-10-01 17:42:02 -040091 struct sysfs_open_file *of = sf->private;
Tejun Heo7c6e2d32013-11-28 14:54:14 -050092 struct kobject *kobj = of->sd->s_parent->priv;
Tejun Heoc2b19da2013-11-28 14:54:16 -050093 const struct sysfs_ops *ops = sysfs_file_ops(of->sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 ssize_t count;
Tejun Heoc2b19da2013-11-28 14:54:16 -050095 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Tejun Heo13c589d2013-10-01 17:42:02 -040097 /* acquire buffer and ensure that it's >= PAGE_SIZE */
98 count = seq_get_buf(sf, &buf);
99 if (count < PAGE_SIZE) {
100 seq_commit(sf, -1);
101 return 0;
102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Tejun Heo13c589d2013-10-01 17:42:02 -0400104 /*
Tejun Heoc2b19da2013-11-28 14:54:16 -0500105 * Invoke show(). Control may reach here via seq file lseek even
106 * if @ops->show() isn't implemented.
Tejun Heo13c589d2013-10-01 17:42:02 -0400107 */
Tejun Heoc2b19da2013-11-28 14:54:16 -0500108 if (ops->show) {
Tejun Heo7c6e2d32013-11-28 14:54:14 -0500109 count = ops->show(kobj, of->sd->priv, buf);
Tejun Heoc2b19da2013-11-28 14:54:16 -0500110 if (count < 0)
111 return count;
112 }
Tejun Heo0ab66082007-06-14 03:45:16 +0900113
Miao Xie8118a852007-11-21 14:55:19 -0800114 /*
115 * The code works fine with PAGE_SIZE return but it's likely to
116 * indicate truncated result or overflow in normal use cases.
117 */
Andrew Morton815d2d52008-03-04 15:09:07 -0800118 if (count >= (ssize_t)PAGE_SIZE) {
119 print_symbol("fill_read_buffer: %s returned bad count\n",
120 (unsigned long)ops->show);
121 /* Try to struggle along */
122 count = PAGE_SIZE - 1;
123 }
Tejun Heo13c589d2013-10-01 17:42:02 -0400124 seq_commit(sf, count);
125 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
Tejun Heoc2b19da2013-11-28 14:54:16 -0500128static ssize_t sysfs_kf_bin_read(struct sysfs_open_file *of, char *buf,
129 size_t count, loff_t pos)
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400130{
Tejun Heo7c6e2d32013-11-28 14:54:14 -0500131 struct bin_attribute *battr = of->sd->priv;
132 struct kobject *kobj = of->sd->s_parent->priv;
Tejun Heoc2b19da2013-11-28 14:54:16 -0500133 loff_t size = file_inode(of->file)->i_size;
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400134
Tejun Heoc2b19da2013-11-28 14:54:16 -0500135 if (!count)
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400136 return 0;
137
138 if (size) {
Tejun Heoc2b19da2013-11-28 14:54:16 -0500139 if (pos > size)
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400140 return 0;
Tejun Heoc2b19da2013-11-28 14:54:16 -0500141 if (pos + count > size)
142 count = size - pos;
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400143 }
144
Tejun Heoc2b19da2013-11-28 14:54:16 -0500145 if (!battr->read)
146 return -EIO;
147
148 return battr->read(of->file, kobj, battr, buf, pos, count);
149}
150
151static void *kernfs_seq_start(struct seq_file *sf, loff_t *ppos)
152{
153 struct sysfs_open_file *of = sf->private;
154
155 /*
156 * @of->mutex nests outside active ref and is just to ensure that
157 * the ops aren't called concurrently for the same open file.
158 */
159 mutex_lock(&of->mutex);
160 if (!sysfs_get_active(of->sd))
161 return ERR_PTR(-ENODEV);
162
163 /*
164 * The same behavior and code as single_open(). Returns !NULL if
165 * pos is at the beginning; otherwise, NULL.
166 */
167 return NULL + !*ppos;
168}
169
170static void *kernfs_seq_next(struct seq_file *sf, void *v, loff_t *ppos)
171{
172 /*
173 * The same behavior and code as single_open(), always terminate
174 * after the initial read.
175 */
176 ++*ppos;
177 return NULL;
178}
179
180static void kernfs_seq_stop(struct seq_file *sf, void *v)
181{
182 struct sysfs_open_file *of = sf->private;
183
184 sysfs_put_active(of->sd);
185 mutex_unlock(&of->mutex);
186}
187
188static int kernfs_seq_show(struct seq_file *sf, void *v)
189{
190 struct sysfs_open_file *of = sf->private;
191
192 of->event = atomic_read(&of->sd->s_attr.open->event);
193
194 return sysfs_kf_seq_show(sf, v);
195}
196
197static const struct seq_operations kernfs_seq_ops = {
198 .start = kernfs_seq_start,
199 .next = kernfs_seq_next,
200 .stop = kernfs_seq_stop,
201 .show = kernfs_seq_show,
202};
203
204/*
205 * As reading a bin file can have side-effects, the exact offset and bytes
206 * specified in read(2) call should be passed to the read callback making
207 * it difficult to use seq_file. Implement simplistic custom buffering for
208 * bin files.
209 */
210static ssize_t kernfs_file_direct_read(struct sysfs_open_file *of,
211 char __user *user_buf, size_t count,
212 loff_t *ppos)
213{
214 ssize_t len = min_t(size_t, count, PAGE_SIZE);
215 char *buf;
216
217 buf = kmalloc(len, GFP_KERNEL);
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400218 if (!buf)
219 return -ENOMEM;
220
Tejun Heoc2b19da2013-11-28 14:54:16 -0500221 /*
222 * @of->mutex nests outside active ref and is just to ensure that
223 * the ops aren't called concurrently for the same open file.
224 */
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400225 mutex_lock(&of->mutex);
226 if (!sysfs_get_active(of->sd)) {
Tejun Heoc2b19da2013-11-28 14:54:16 -0500227 len = -ENODEV;
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400228 mutex_unlock(&of->mutex);
229 goto out_free;
230 }
231
Tejun Heoc2b19da2013-11-28 14:54:16 -0500232 len = sysfs_kf_bin_read(of, buf, len, *ppos);
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400233
234 sysfs_put_active(of->sd);
235 mutex_unlock(&of->mutex);
236
Tejun Heoc2b19da2013-11-28 14:54:16 -0500237 if (len < 0)
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400238 goto out_free;
239
Tejun Heoc2b19da2013-11-28 14:54:16 -0500240 if (copy_to_user(user_buf, buf, len)) {
241 len = -EFAULT;
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400242 goto out_free;
243 }
244
Tejun Heoc2b19da2013-11-28 14:54:16 -0500245 *ppos += len;
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400246
247 out_free:
248 kfree(buf);
Tejun Heoc2b19da2013-11-28 14:54:16 -0500249 return len;
250}
251
252/**
253 * kernfs_file_read - kernfs vfs read callback
254 * @file: file pointer
255 * @user_buf: data to write
256 * @count: number of bytes
257 * @ppos: starting offset
258 */
259static ssize_t kernfs_file_read(struct file *file, char __user *user_buf,
260 size_t count, loff_t *ppos)
261{
262 struct sysfs_open_file *of = sysfs_of(file);
263
264 if (sysfs_is_bin(of->sd))
265 return kernfs_file_direct_read(of, user_buf, count, ppos);
266 else
267 return seq_read(file, user_buf, count, ppos);
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400268}
269
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 Heo8ef445f2013-10-01 17:42:01 -0400326 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Tejun Heo8ef445f2013-10-01 17:42:01 -0400328 buf = kmalloc(len + 1, GFP_KERNEL);
329 if (!buf)
330 return -ENOMEM;
331
332 if (copy_from_user(buf, user_buf, len)) {
333 len = -EFAULT;
334 goto out_free;
335 }
336 buf[len] = '\0'; /* guarantee string termination */
337
Tejun Heo50b38ca2013-11-28 14:54:17 -0500338 /*
339 * @of->mutex nests outside active ref and is just to ensure that
340 * the ops aren't called concurrently for the same open file.
341 */
342 mutex_lock(&of->mutex);
343 if (!sysfs_get_active(of->sd)) {
344 mutex_unlock(&of->mutex);
345 len = -ENODEV;
346 goto out_free;
347 }
348
349 if (sysfs_is_bin(of->sd))
350 len = sysfs_kf_bin_write(of, buf, len, *ppos);
351 else
352 len = sysfs_kf_write(of, buf, len, *ppos);
353
354 sysfs_put_active(of->sd);
355 mutex_unlock(&of->mutex);
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (len > 0)
358 *ppos += len;
Tejun Heo8ef445f2013-10-01 17:42:01 -0400359out_free:
360 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return len;
362}
363
Tejun Heofdbffaa2013-11-28 14:54:18 -0500364static int sysfs_kf_bin_mmap(struct sysfs_open_file *of,
365 struct vm_area_struct *vma)
366{
367 struct bin_attribute *battr = of->sd->priv;
368 struct kobject *kobj = of->sd->s_parent->priv;
369
370 if (!battr->mmap)
371 return -ENODEV;
372
373 return battr->mmap(of->file, kobj, battr, vma);
374}
375
376static void kernfs_vma_open(struct vm_area_struct *vma)
Tejun Heo73d97142013-10-01 17:42:07 -0400377{
378 struct file *file = vma->vm_file;
379 struct sysfs_open_file *of = sysfs_of(file);
380
381 if (!of->vm_ops)
382 return;
383
384 if (!sysfs_get_active(of->sd))
385 return;
386
387 if (of->vm_ops->open)
388 of->vm_ops->open(vma);
389
390 sysfs_put_active(of->sd);
391}
392
Tejun Heofdbffaa2013-11-28 14:54:18 -0500393static int kernfs_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Tejun Heo73d97142013-10-01 17:42:07 -0400394{
395 struct file *file = vma->vm_file;
396 struct sysfs_open_file *of = sysfs_of(file);
397 int ret;
398
399 if (!of->vm_ops)
400 return VM_FAULT_SIGBUS;
401
402 if (!sysfs_get_active(of->sd))
403 return VM_FAULT_SIGBUS;
404
405 ret = VM_FAULT_SIGBUS;
406 if (of->vm_ops->fault)
407 ret = of->vm_ops->fault(vma, vmf);
408
409 sysfs_put_active(of->sd);
410 return ret;
411}
412
Tejun Heofdbffaa2013-11-28 14:54:18 -0500413static int kernfs_vma_page_mkwrite(struct vm_area_struct *vma,
414 struct vm_fault *vmf)
Tejun Heo73d97142013-10-01 17:42:07 -0400415{
416 struct file *file = vma->vm_file;
417 struct sysfs_open_file *of = sysfs_of(file);
418 int ret;
419
420 if (!of->vm_ops)
421 return VM_FAULT_SIGBUS;
422
423 if (!sysfs_get_active(of->sd))
424 return VM_FAULT_SIGBUS;
425
426 ret = 0;
427 if (of->vm_ops->page_mkwrite)
428 ret = of->vm_ops->page_mkwrite(vma, vmf);
429 else
430 file_update_time(file);
431
432 sysfs_put_active(of->sd);
433 return ret;
434}
435
Tejun Heofdbffaa2013-11-28 14:54:18 -0500436static int kernfs_vma_access(struct vm_area_struct *vma, unsigned long addr,
437 void *buf, int len, int write)
Tejun Heo73d97142013-10-01 17:42:07 -0400438{
439 struct file *file = vma->vm_file;
440 struct sysfs_open_file *of = sysfs_of(file);
441 int ret;
442
443 if (!of->vm_ops)
444 return -EINVAL;
445
446 if (!sysfs_get_active(of->sd))
447 return -EINVAL;
448
449 ret = -EINVAL;
450 if (of->vm_ops->access)
451 ret = of->vm_ops->access(vma, addr, buf, len, write);
452
453 sysfs_put_active(of->sd);
454 return ret;
455}
456
457#ifdef CONFIG_NUMA
Tejun Heofdbffaa2013-11-28 14:54:18 -0500458static int kernfs_vma_set_policy(struct vm_area_struct *vma,
459 struct mempolicy *new)
Tejun Heo73d97142013-10-01 17:42:07 -0400460{
461 struct file *file = vma->vm_file;
462 struct sysfs_open_file *of = sysfs_of(file);
463 int ret;
464
465 if (!of->vm_ops)
466 return 0;
467
468 if (!sysfs_get_active(of->sd))
469 return -EINVAL;
470
471 ret = 0;
472 if (of->vm_ops->set_policy)
473 ret = of->vm_ops->set_policy(vma, new);
474
475 sysfs_put_active(of->sd);
476 return ret;
477}
478
Tejun Heofdbffaa2013-11-28 14:54:18 -0500479static struct mempolicy *kernfs_vma_get_policy(struct vm_area_struct *vma,
480 unsigned long addr)
Tejun Heo73d97142013-10-01 17:42:07 -0400481{
482 struct file *file = vma->vm_file;
483 struct sysfs_open_file *of = sysfs_of(file);
484 struct mempolicy *pol;
485
486 if (!of->vm_ops)
487 return vma->vm_policy;
488
489 if (!sysfs_get_active(of->sd))
490 return vma->vm_policy;
491
492 pol = vma->vm_policy;
493 if (of->vm_ops->get_policy)
494 pol = of->vm_ops->get_policy(vma, addr);
495
496 sysfs_put_active(of->sd);
497 return pol;
498}
499
Tejun Heofdbffaa2013-11-28 14:54:18 -0500500static int kernfs_vma_migrate(struct vm_area_struct *vma,
501 const nodemask_t *from, const nodemask_t *to,
502 unsigned long flags)
Tejun Heo73d97142013-10-01 17:42:07 -0400503{
504 struct file *file = vma->vm_file;
505 struct sysfs_open_file *of = sysfs_of(file);
506 int ret;
507
508 if (!of->vm_ops)
509 return 0;
510
511 if (!sysfs_get_active(of->sd))
512 return 0;
513
514 ret = 0;
515 if (of->vm_ops->migrate)
516 ret = of->vm_ops->migrate(vma, from, to, flags);
517
518 sysfs_put_active(of->sd);
519 return ret;
520}
521#endif
522
Tejun Heofdbffaa2013-11-28 14:54:18 -0500523static const struct vm_operations_struct kernfs_vm_ops = {
524 .open = kernfs_vma_open,
525 .fault = kernfs_vma_fault,
526 .page_mkwrite = kernfs_vma_page_mkwrite,
527 .access = kernfs_vma_access,
Tejun Heo73d97142013-10-01 17:42:07 -0400528#ifdef CONFIG_NUMA
Tejun Heofdbffaa2013-11-28 14:54:18 -0500529 .set_policy = kernfs_vma_set_policy,
530 .get_policy = kernfs_vma_get_policy,
531 .migrate = kernfs_vma_migrate,
Tejun Heo73d97142013-10-01 17:42:07 -0400532#endif
533};
534
Tejun Heofdbffaa2013-11-28 14:54:18 -0500535static int kernfs_file_mmap(struct file *file, struct vm_area_struct *vma)
Tejun Heo73d97142013-10-01 17:42:07 -0400536{
537 struct sysfs_open_file *of = sysfs_of(file);
Tejun Heo73d97142013-10-01 17:42:07 -0400538 int rc;
539
540 mutex_lock(&of->mutex);
541
Tejun Heo73d97142013-10-01 17:42:07 -0400542 rc = -ENODEV;
543 if (!sysfs_get_active(of->sd))
544 goto out_unlock;
545
Tejun Heofdbffaa2013-11-28 14:54:18 -0500546 if (sysfs_is_bin(of->sd))
547 rc = sysfs_kf_bin_mmap(of, vma);
Tejun Heo73d97142013-10-01 17:42:07 -0400548 if (rc)
549 goto out_put;
550
551 /*
552 * PowerPC's pci_mmap of legacy_mem uses shmem_zero_setup()
553 * to satisfy versions of X which crash if the mmap fails: that
554 * substitutes a new vm_file, and we don't then want bin_vm_ops.
555 */
556 if (vma->vm_file != file)
557 goto out_put;
558
559 rc = -EINVAL;
560 if (of->mmapped && of->vm_ops != vma->vm_ops)
561 goto out_put;
562
563 /*
564 * It is not possible to successfully wrap close.
565 * So error if someone is trying to use close.
566 */
567 rc = -EINVAL;
568 if (vma->vm_ops && vma->vm_ops->close)
569 goto out_put;
570
571 rc = 0;
572 of->mmapped = 1;
573 of->vm_ops = vma->vm_ops;
Tejun Heofdbffaa2013-11-28 14:54:18 -0500574 vma->vm_ops = &kernfs_vm_ops;
Tejun Heo73d97142013-10-01 17:42:07 -0400575out_put:
576 sysfs_put_active(of->sd);
577out_unlock:
578 mutex_unlock(&of->mutex);
579
580 return rc;
581}
582
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900583/**
584 * sysfs_get_open_dirent - get or create sysfs_open_dirent
585 * @sd: target sysfs_dirent
Tejun Heo58282d82013-10-01 17:41:59 -0400586 * @of: sysfs_open_file for this instance of open
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900587 *
588 * If @sd->s_attr.open exists, increment its reference count;
Tejun Heo58282d82013-10-01 17:41:59 -0400589 * otherwise, create one. @of is chained to the files list.
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900590 *
591 * LOCKING:
592 * Kernel thread context (may sleep).
593 *
594 * RETURNS:
595 * 0 on success, -errno on failure.
596 */
597static int sysfs_get_open_dirent(struct sysfs_dirent *sd,
Tejun Heo58282d82013-10-01 17:41:59 -0400598 struct sysfs_open_file *of)
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900599{
600 struct sysfs_open_dirent *od, *new_od = NULL;
601
602 retry:
Tejun Heoc75ec762013-10-01 17:41:58 -0400603 mutex_lock(&sysfs_open_file_mutex);
Neil Brown83db93f2009-09-15 16:05:51 -0700604 spin_lock_irq(&sysfs_open_dirent_lock);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900605
606 if (!sd->s_attr.open && new_od) {
607 sd->s_attr.open = new_od;
608 new_od = NULL;
609 }
610
611 od = sd->s_attr.open;
612 if (od) {
613 atomic_inc(&od->refcnt);
Tejun Heo58282d82013-10-01 17:41:59 -0400614 list_add_tail(&of->list, &od->files);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900615 }
616
Neil Brown83db93f2009-09-15 16:05:51 -0700617 spin_unlock_irq(&sysfs_open_dirent_lock);
Tejun Heoc75ec762013-10-01 17:41:58 -0400618 mutex_unlock(&sysfs_open_file_mutex);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900619
620 if (od) {
621 kfree(new_od);
622 return 0;
623 }
624
625 /* not there, initialize a new one and retry */
626 new_od = kmalloc(sizeof(*new_od), GFP_KERNEL);
627 if (!new_od)
628 return -ENOMEM;
629
630 atomic_set(&new_od->refcnt, 0);
Tejun Heoa4e8b912007-09-20 16:05:12 +0900631 atomic_set(&new_od->event, 1);
632 init_waitqueue_head(&new_od->poll);
Tejun Heo58282d82013-10-01 17:41:59 -0400633 INIT_LIST_HEAD(&new_od->files);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900634 goto retry;
635}
636
637/**
638 * sysfs_put_open_dirent - put sysfs_open_dirent
639 * @sd: target sysfs_dirent
Tejun Heo58282d82013-10-01 17:41:59 -0400640 * @of: associated sysfs_open_file
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900641 *
Tejun Heo58282d82013-10-01 17:41:59 -0400642 * Put @sd->s_attr.open and unlink @of from the files list. If
643 * reference count reaches zero, disassociate and free it.
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900644 *
645 * LOCKING:
646 * None.
647 */
648static void sysfs_put_open_dirent(struct sysfs_dirent *sd,
Tejun Heo58282d82013-10-01 17:41:59 -0400649 struct sysfs_open_file *of)
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900650{
651 struct sysfs_open_dirent *od = sd->s_attr.open;
Neil Brown83db93f2009-09-15 16:05:51 -0700652 unsigned long flags;
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900653
Tejun Heoc75ec762013-10-01 17:41:58 -0400654 mutex_lock(&sysfs_open_file_mutex);
Neil Brown83db93f2009-09-15 16:05:51 -0700655 spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900656
Tejun Heo73d97142013-10-01 17:42:07 -0400657 if (of)
658 list_del(&of->list);
659
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900660 if (atomic_dec_and_test(&od->refcnt))
661 sd->s_attr.open = NULL;
662 else
663 od = NULL;
664
Neil Brown83db93f2009-09-15 16:05:51 -0700665 spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
Tejun Heoc75ec762013-10-01 17:41:58 -0400666 mutex_unlock(&sysfs_open_file_mutex);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900667
668 kfree(od);
669}
670
Tejun Heoc6fb4492013-11-28 14:54:19 -0500671static int kernfs_file_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Tejun Heo3e519032007-06-14 03:45:15 +0900673 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
Tejun Heo7c6e2d32013-11-28 14:54:14 -0500674 struct kobject *kobj = attr_sd->s_parent->priv;
Tejun Heo58282d82013-10-01 17:41:59 -0400675 struct sysfs_open_file *of;
Tejun Heo027a4852013-11-17 11:17:36 +0900676 bool has_read, has_write, has_mmap;
Kay Sievers000f2a42007-11-02 13:47:53 +0100677 int error = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Tejun Heo0ab66082007-06-14 03:45:16 +0900679 /* need attr_sd for attr and ops, its parent for kobj */
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800680 if (!sysfs_get_active(attr_sd))
Tejun Heo0ab66082007-06-14 03:45:16 +0900681 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Tejun Heo49fe6042013-10-01 17:42:08 -0400683 if (sysfs_is_bin(attr_sd)) {
Tejun Heo7c6e2d32013-11-28 14:54:14 -0500684 struct bin_attribute *battr = attr_sd->priv;
Tejun Heo49fe6042013-10-01 17:42:08 -0400685
686 has_read = battr->read || battr->mmap;
687 has_write = battr->write || battr->mmap;
Tejun Heo027a4852013-11-17 11:17:36 +0900688 has_mmap = battr->mmap;
Tejun Heo49fe6042013-10-01 17:42:08 -0400689 } else {
690 const struct sysfs_ops *ops = sysfs_file_ops(attr_sd);
691
692 /* every kobject with an attribute needs a ktype assigned */
693 if (WARN(!ops, KERN_ERR
694 "missing sysfs attribute operations for kobject: %s\n",
695 kobject_name(kobj)))
696 goto err_out;
697
698 has_read = ops->show;
699 has_write = ops->store;
Tejun Heo027a4852013-11-17 11:17:36 +0900700 has_mmap = false;
Tejun Heo49fe6042013-10-01 17:42:08 -0400701 }
702
703 /* check perms and supported operations */
704 if ((file->f_mode & FMODE_WRITE) &&
705 (!(inode->i_mode & S_IWUGO) || !has_write))
Tejun Heo7b595752007-06-14 03:45:17 +0900706 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Tejun Heo49fe6042013-10-01 17:42:08 -0400708 if ((file->f_mode & FMODE_READ) &&
709 (!(inode->i_mode & S_IRUGO) || !has_read))
710 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Tejun Heo13c589d2013-10-01 17:42:02 -0400712 /* allocate a sysfs_open_file for the file */
Tejun Heo0ab66082007-06-14 03:45:16 +0900713 error = -ENOMEM;
Tejun Heo58282d82013-10-01 17:41:59 -0400714 of = kzalloc(sizeof(struct sysfs_open_file), GFP_KERNEL);
715 if (!of)
Tejun Heo7b595752007-06-14 03:45:17 +0900716 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Tejun Heo027a4852013-11-17 11:17:36 +0900718 /*
719 * The following is done to give a different lockdep key to
720 * @of->mutex for files which implement mmap. This is a rather
721 * crude way to avoid false positive lockdep warning around
722 * mm->mmap_sem - mmap nests @of->mutex under mm->mmap_sem and
723 * reading /sys/block/sda/trace/act_mask grabs sr_mutex, under
724 * which mm->mmap_sem nests, while holding @of->mutex. As each
725 * open file has a separate mutex, it's okay as long as those don't
726 * happen on the same file. At this point, we can't easily give
727 * each file a separate locking class. Let's differentiate on
728 * whether the file has mmap or not for now.
729 */
730 if (has_mmap)
731 mutex_init(&of->mutex);
732 else
733 mutex_init(&of->mutex);
734
Tejun Heobcafe4e2013-10-01 17:42:00 -0400735 of->sd = attr_sd;
736 of->file = file;
Tejun Heo13c589d2013-10-01 17:42:02 -0400737
738 /*
Tejun Heo49fe6042013-10-01 17:42:08 -0400739 * Always instantiate seq_file even if read access doesn't use
740 * seq_file or is not requested. This unifies private data access
741 * and readable regular files are the vast majority anyway.
Tejun Heo13c589d2013-10-01 17:42:02 -0400742 */
Tejun Heo49fe6042013-10-01 17:42:08 -0400743 if (sysfs_is_bin(attr_sd))
Tejun Heoc2b19da2013-11-28 14:54:16 -0500744 error = seq_open(file, NULL);
Tejun Heo49fe6042013-10-01 17:42:08 -0400745 else
Tejun Heoc2b19da2013-11-28 14:54:16 -0500746 error = seq_open(file, &kernfs_seq_ops);
Tejun Heo13c589d2013-10-01 17:42:02 -0400747 if (error)
748 goto err_free;
749
Tejun Heoc2b19da2013-11-28 14:54:16 -0500750 ((struct seq_file *)file->private_data)->private = of;
751
Tejun Heo13c589d2013-10-01 17:42:02 -0400752 /* seq_file clears PWRITE unconditionally, restore it if WRITE */
753 if (file->f_mode & FMODE_WRITE)
754 file->f_mode |= FMODE_PWRITE;
Tejun Heo0ab66082007-06-14 03:45:16 +0900755
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900756 /* make sure we have open dirent struct */
Tejun Heo58282d82013-10-01 17:41:59 -0400757 error = sysfs_get_open_dirent(attr_sd, of);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900758 if (error)
Tejun Heo13c589d2013-10-01 17:42:02 -0400759 goto err_close;
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900760
Tejun Heob05f0542007-09-20 16:05:10 +0900761 /* open succeeded, put active references */
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800762 sysfs_put_active(attr_sd);
Tejun Heo0ab66082007-06-14 03:45:16 +0900763 return 0;
764
Tejun Heo13c589d2013-10-01 17:42:02 -0400765err_close:
Tejun Heoc2b19da2013-11-28 14:54:16 -0500766 seq_release(inode, file);
Tejun Heo13c589d2013-10-01 17:42:02 -0400767err_free:
Tejun Heo58282d82013-10-01 17:41:59 -0400768 kfree(of);
Tejun Heo13c589d2013-10-01 17:42:02 -0400769err_out:
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800770 sysfs_put_active(attr_sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 return error;
772}
773
Tejun Heoc6fb4492013-11-28 14:54:19 -0500774static int kernfs_file_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900776 struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata;
Tejun Heo13c589d2013-10-01 17:42:02 -0400777 struct sysfs_open_file *of = sysfs_of(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Tejun Heo58282d82013-10-01 17:41:59 -0400779 sysfs_put_open_dirent(sd, of);
Tejun Heoc2b19da2013-11-28 14:54:16 -0500780 seq_release(inode, filp);
Tejun Heo58282d82013-10-01 17:41:59 -0400781 kfree(of);
Tejun Heo50ab1a72007-09-20 16:05:10 +0900782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 return 0;
784}
785
Tejun Heo73d97142013-10-01 17:42:07 -0400786void sysfs_unmap_bin_file(struct sysfs_dirent *sd)
787{
788 struct sysfs_open_dirent *od;
789 struct sysfs_open_file *of;
790
791 if (!sysfs_is_bin(sd))
792 return;
793
794 spin_lock_irq(&sysfs_open_dirent_lock);
795 od = sd->s_attr.open;
796 if (od)
797 atomic_inc(&od->refcnt);
798 spin_unlock_irq(&sysfs_open_dirent_lock);
799 if (!od)
800 return;
801
802 mutex_lock(&sysfs_open_file_mutex);
803 list_for_each_entry(of, &od->files, list) {
804 struct inode *inode = file_inode(of->file);
805 unmap_mapping_range(inode->i_mapping, 0, 0, 1);
806 }
807 mutex_unlock(&sysfs_open_file_mutex);
808
809 sysfs_put_open_dirent(sd, NULL);
810}
811
NeilBrown4508a7a2006-03-20 17:53:53 +1100812/* Sysfs attribute files are pollable. The idea is that you read
813 * the content and then you use 'poll' or 'select' to wait for
814 * the content to change. When the content changes (assuming the
815 * manager for the kobject supports notification), poll will
816 * return POLLERR|POLLPRI, and select will return the fd whether
817 * it is waiting for read, write, or exceptions.
818 * Once poll/select indicates that the value has changed, you
Dan Williams2424b5d2008-04-07 15:35:01 -0700819 * need to close and re-open the file, or seek to 0 and read again.
NeilBrown4508a7a2006-03-20 17:53:53 +1100820 * Reminder: this only works for attributes which actively support
821 * it, and it is not possible to test an attribute from userspace
Rolf Eike Beera93720e2007-08-10 13:51:07 -0700822 * to see if it supports poll (Neither 'poll' nor 'select' return
NeilBrown4508a7a2006-03-20 17:53:53 +1100823 * an appropriate error code). When in doubt, set a suitable timeout value.
824 */
Tejun Heoc6fb4492013-11-28 14:54:19 -0500825static unsigned int kernfs_file_poll(struct file *filp, poll_table *wait)
NeilBrown4508a7a2006-03-20 17:53:53 +1100826{
Tejun Heo13c589d2013-10-01 17:42:02 -0400827 struct sysfs_open_file *of = sysfs_of(filp);
Tejun Heo0ab66082007-06-14 03:45:16 +0900828 struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata;
Tejun Heoa4e8b912007-09-20 16:05:12 +0900829 struct sysfs_open_dirent *od = attr_sd->s_attr.open;
Tejun Heo0ab66082007-06-14 03:45:16 +0900830
831 /* need parent for the kobj, grab both */
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800832 if (!sysfs_get_active(attr_sd))
Tejun Heo0ab66082007-06-14 03:45:16 +0900833 goto trigger;
NeilBrown4508a7a2006-03-20 17:53:53 +1100834
Tejun Heoa4e8b912007-09-20 16:05:12 +0900835 poll_wait(filp, &od->poll, wait);
NeilBrown4508a7a2006-03-20 17:53:53 +1100836
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800837 sysfs_put_active(attr_sd);
NeilBrown4508a7a2006-03-20 17:53:53 +1100838
Tejun Heo58282d82013-10-01 17:41:59 -0400839 if (of->event != atomic_read(&od->event))
Tejun Heo0ab66082007-06-14 03:45:16 +0900840 goto trigger;
841
KOSAKI Motohiro1af35572009-04-09 13:53:22 +0900842 return DEFAULT_POLLMASK;
Tejun Heo0ab66082007-06-14 03:45:16 +0900843
844 trigger:
KOSAKI Motohiro1af35572009-04-09 13:53:22 +0900845 return DEFAULT_POLLMASK|POLLERR|POLLPRI;
NeilBrown4508a7a2006-03-20 17:53:53 +1100846}
847
Neil Brownf1282c82008-07-16 08:58:04 +1000848void sysfs_notify_dirent(struct sysfs_dirent *sd)
849{
850 struct sysfs_open_dirent *od;
Neil Brown83db93f2009-09-15 16:05:51 -0700851 unsigned long flags;
Neil Brownf1282c82008-07-16 08:58:04 +1000852
Neil Brown83db93f2009-09-15 16:05:51 -0700853 spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
Neil Brownf1282c82008-07-16 08:58:04 +1000854
Nick Dyerfc60bb82013-06-07 15:45:13 +0100855 if (!WARN_ON(sysfs_type(sd) != SYSFS_KOBJ_ATTR)) {
856 od = sd->s_attr.open;
857 if (od) {
858 atomic_inc(&od->event);
859 wake_up_interruptible(&od->poll);
860 }
Neil Brownf1282c82008-07-16 08:58:04 +1000861 }
862
Neil Brown83db93f2009-09-15 16:05:51 -0700863 spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
Neil Brownf1282c82008-07-16 08:58:04 +1000864}
865EXPORT_SYMBOL_GPL(sysfs_notify_dirent);
866
Trent Piepho8c0e3992008-09-25 16:45:13 -0700867void sysfs_notify(struct kobject *k, const char *dir, const char *attr)
NeilBrown4508a7a2006-03-20 17:53:53 +1100868{
Tejun Heo51225032007-06-14 04:27:25 +0900869 struct sysfs_dirent *sd = k->sd;
NeilBrown4508a7a2006-03-20 17:53:53 +1100870
Tejun Heo51225032007-06-14 04:27:25 +0900871 mutex_lock(&sysfs_mutex);
NeilBrown4508a7a2006-03-20 17:53:53 +1100872
Tejun Heo51225032007-06-14 04:27:25 +0900873 if (sd && dir)
Tejun Heocfec0bc2013-09-11 22:29:09 -0400874 sd = sysfs_find_dirent(sd, dir, NULL);
Tejun Heo51225032007-06-14 04:27:25 +0900875 if (sd && attr)
Tejun Heocfec0bc2013-09-11 22:29:09 -0400876 sd = sysfs_find_dirent(sd, attr, NULL);
Neil Brownf1282c82008-07-16 08:58:04 +1000877 if (sd)
878 sysfs_notify_dirent(sd);
Tejun Heo51225032007-06-14 04:27:25 +0900879
880 mutex_unlock(&sysfs_mutex);
NeilBrown4508a7a2006-03-20 17:53:53 +1100881}
882EXPORT_SYMBOL_GPL(sysfs_notify);
883
Tejun Heoc6fb4492013-11-28 14:54:19 -0500884const struct file_operations kernfs_file_operations = {
Tejun Heoc2b19da2013-11-28 14:54:16 -0500885 .read = kernfs_file_read,
Tejun Heo50b38ca2013-11-28 14:54:17 -0500886 .write = kernfs_file_write,
Tejun Heo044e3bc2013-11-01 13:16:53 -0400887 .llseek = generic_file_llseek,
Tejun Heofdbffaa2013-11-28 14:54:18 -0500888 .mmap = kernfs_file_mmap,
Tejun Heoc6fb4492013-11-28 14:54:19 -0500889 .open = kernfs_file_open,
890 .release = kernfs_file_release,
891 .poll = kernfs_file_poll,
Tejun Heof9b9a622013-10-01 17:42:05 -0400892};
893
Tejun Heo58292cbe2013-09-11 22:29:04 -0400894int sysfs_add_file_mode_ns(struct sysfs_dirent *dir_sd,
895 const struct attribute *attr, int type,
896 umode_t amode, const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
James Bottomley0f423892008-03-20 20:47:52 -0500898 umode_t mode = (amode & S_IALLUGO) | S_IFREG;
Tejun Heofb6896d2007-06-14 04:27:24 +0900899 struct sysfs_addrm_cxt acxt;
Tejun Heoa26cd722007-06-14 03:45:14 +0900900 struct sysfs_dirent *sd;
Tejun Heo23dc2792007-08-02 21:38:03 +0900901 int rc;
Tejun Heoa26cd722007-06-14 03:45:14 +0900902
Tejun Heo3e519032007-06-14 03:45:15 +0900903 sd = sysfs_new_dirent(attr->name, mode, type);
Tejun Heo3007e992007-06-14 04:27:23 +0900904 if (!sd)
905 return -ENOMEM;
Eric W. Biederman487505c2011-10-12 21:53:38 +0000906
907 sd->s_ns = ns;
Tejun Heo7c6e2d32013-11-28 14:54:14 -0500908 sd->priv = (void *)attr;
Eric W. Biedermana2db6842010-02-11 15:20:00 -0800909 sysfs_dirent_init_lockdep(sd);
Tejun Heoa26cd722007-06-14 03:45:14 +0900910
Tejun Heod69ac5a2013-09-18 17:15:35 -0400911 sysfs_addrm_start(&acxt);
912 rc = sysfs_add_one(&acxt, sd, dir_sd);
Tejun Heo23dc2792007-08-02 21:38:03 +0900913 sysfs_addrm_finish(&acxt);
Tejun Heo3007e992007-06-14 04:27:23 +0900914
Tejun Heo23dc2792007-08-02 21:38:03 +0900915 if (rc)
Tejun Heo967e35d2007-07-18 16:38:11 +0900916 sysfs_put(sd);
Tejun Heo3007e992007-06-14 04:27:23 +0900917
Tejun Heo23dc2792007-08-02 21:38:03 +0900918 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
921
James Bottomley0f423892008-03-20 20:47:52 -0500922int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,
923 int type)
924{
Tejun Heo58292cbe2013-09-11 22:29:04 -0400925 return sysfs_add_file_mode_ns(dir_sd, attr, type, attr->mode, NULL);
James Bottomley0f423892008-03-20 20:47:52 -0500926}
927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928/**
Tejun Heo58292cbe2013-09-11 22:29:04 -0400929 * sysfs_create_file_ns - create an attribute file for an object with custom ns
930 * @kobj: object we're creating for
931 * @attr: attribute descriptor
932 * @ns: namespace the new file should belong to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 */
Tejun Heo58292cbe2013-09-11 22:29:04 -0400934int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
935 const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
Tejun Heo608e2662007-06-14 04:27:22 +0900937 BUG_ON(!kobj || !kobj->sd || !attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Tejun Heo58292cbe2013-09-11 22:29:04 -0400939 return sysfs_add_file_mode_ns(kobj->sd, attr, SYSFS_KOBJ_ATTR,
940 attr->mode, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942}
Tejun Heo58292cbe2013-09-11 22:29:04 -0400943EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Andi Kleen1c205ae2010-01-05 12:48:01 +0100945int sysfs_create_files(struct kobject *kobj, const struct attribute **ptr)
946{
947 int err = 0;
948 int i;
949
950 for (i = 0; ptr[i] && !err; i++)
951 err = sysfs_create_file(kobj, ptr[i]);
952 if (err)
953 while (--i >= 0)
954 sysfs_remove_file(kobj, ptr[i]);
955 return err;
956}
Greg Kroah-Hartman1b866752013-08-21 16:17:47 -0700957EXPORT_SYMBOL_GPL(sysfs_create_files);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959/**
Alan Sterndfa87c82007-02-20 15:02:44 -0500960 * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
961 * @kobj: object we're acting for.
962 * @attr: attribute descriptor.
963 * @group: group name.
964 */
965int sysfs_add_file_to_group(struct kobject *kobj,
966 const struct attribute *attr, const char *group)
967{
Tejun Heo608e2662007-06-14 04:27:22 +0900968 struct sysfs_dirent *dir_sd;
Alan Sterndfa87c82007-02-20 15:02:44 -0500969 int error;
970
James Bottomley11f24fb2008-01-02 18:44:05 -0600971 if (group)
Tejun Heo388975c2013-09-11 23:19:13 -0400972 dir_sd = sysfs_get_dirent(kobj->sd, group);
James Bottomley11f24fb2008-01-02 18:44:05 -0600973 else
974 dir_sd = sysfs_get(kobj->sd);
975
Tejun Heo608e2662007-06-14 04:27:22 +0900976 if (!dir_sd)
977 return -ENOENT;
978
979 error = sysfs_add_file(dir_sd, attr, SYSFS_KOBJ_ATTR);
980 sysfs_put(dir_sd);
981
Alan Sterndfa87c82007-02-20 15:02:44 -0500982 return error;
983}
984EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986/**
Kay Sievers31e5abe2005-04-18 21:57:32 -0700987 * sysfs_chmod_file - update the modified mode value on an object attribute.
988 * @kobj: object we're acting for.
989 * @attr: attribute descriptor.
990 * @mode: file permissions.
991 *
992 */
Jean Delvare49c19402010-07-02 16:54:05 +0200993int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
Al Viro48176a92011-07-24 03:40:40 -0400994 umode_t mode)
Kay Sievers31e5abe2005-04-18 21:57:32 -0700995{
Eric W. Biederman06fc0d62009-11-20 16:08:54 -0800996 struct sysfs_dirent *sd;
Maneesh Sonibc062b12005-07-29 12:13:35 -0700997 struct iattr newattrs;
Tejun Heo51225032007-06-14 04:27:25 +0900998 int rc;
Kay Sievers31e5abe2005-04-18 21:57:32 -0700999
Tejun Heo5d604182013-11-23 17:21:52 -05001000 sd = sysfs_get_dirent(kobj->sd, attr->name);
Eric W. Biederman06fc0d62009-11-20 16:08:54 -08001001 if (!sd)
Tejun Heo5d604182013-11-23 17:21:52 -05001002 return -ENOENT;
Tejun Heo51225032007-06-14 04:27:25 +09001003
Eric W. Biederman06fc0d62009-11-20 16:08:54 -08001004 newattrs.ia_mode = (mode & S_IALLUGO) | (sd->s_mode & ~S_IALLUGO);
Eric W. Biederman4c6974f2009-11-07 23:27:02 -08001005 newattrs.ia_valid = ATTR_MODE;
Tejun Heof88123e2007-09-20 16:05:10 +09001006
Tejun Heo5d604182013-11-23 17:21:52 -05001007 rc = kernfs_setattr(sd, &newattrs);
1008
1009 sysfs_put(sd);
Tejun Heo51225032007-06-14 04:27:25 +09001010 return rc;
Kay Sievers31e5abe2005-04-18 21:57:32 -07001011}
1012EXPORT_SYMBOL_GPL(sysfs_chmod_file);
1013
Kay Sievers31e5abe2005-04-18 21:57:32 -07001014/**
Tejun Heo58292cbe2013-09-11 22:29:04 -04001015 * sysfs_remove_file_ns - remove an object attribute with a custom ns tag
1016 * @kobj: object we're acting for
1017 * @attr: attribute descriptor
1018 * @ns: namespace tag of the file to remove
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 *
Tejun Heo58292cbe2013-09-11 22:29:04 -04001020 * Hash the attribute name and namespace tag and kill the victim.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 */
Tejun Heo58292cbe2013-09-11 22:29:04 -04001022void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
1023 const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
Tejun Heo58292cbe2013-09-11 22:29:04 -04001025 struct sysfs_dirent *dir_sd = kobj->sd;
Eric W. Biederman487505c2011-10-12 21:53:38 +00001026
Tejun Heo879f40d2013-11-23 17:21:49 -05001027 kernfs_remove_by_name_ns(dir_sd, attr->name, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028}
Tejun Heo58292cbe2013-09-11 22:29:04 -04001029EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Greg Kroah-Hartman1b18dc22013-08-21 16:28:26 -07001031void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr)
Andi Kleen1c205ae2010-01-05 12:48:01 +01001032{
1033 int i;
1034 for (i = 0; ptr[i]; i++)
1035 sysfs_remove_file(kobj, ptr[i]);
1036}
Greg Kroah-Hartman1b866752013-08-21 16:17:47 -07001037EXPORT_SYMBOL_GPL(sysfs_remove_files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Alan Sterndfa87c82007-02-20 15:02:44 -05001039/**
1040 * sysfs_remove_file_from_group - remove an attribute file from a group.
1041 * @kobj: object we're acting for.
1042 * @attr: attribute descriptor.
1043 * @group: group name.
1044 */
1045void sysfs_remove_file_from_group(struct kobject *kobj,
1046 const struct attribute *attr, const char *group)
1047{
Tejun Heo608e2662007-06-14 04:27:22 +09001048 struct sysfs_dirent *dir_sd;
Alan Sterndfa87c82007-02-20 15:02:44 -05001049
James Bottomley11f24fb2008-01-02 18:44:05 -06001050 if (group)
Tejun Heo388975c2013-09-11 23:19:13 -04001051 dir_sd = sysfs_get_dirent(kobj->sd, group);
James Bottomley11f24fb2008-01-02 18:44:05 -06001052 else
1053 dir_sd = sysfs_get(kobj->sd);
Tejun Heo608e2662007-06-14 04:27:22 +09001054 if (dir_sd) {
Tejun Heo879f40d2013-11-23 17:21:49 -05001055 kernfs_remove_by_name(dir_sd, attr->name);
Tejun Heo608e2662007-06-14 04:27:22 +09001056 sysfs_put(dir_sd);
Alan Sterndfa87c82007-02-20 15:02:44 -05001057 }
1058}
1059EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
1060
Tejun Heo3124eb12013-10-01 17:42:09 -04001061/**
1062 * sysfs_create_bin_file - create binary file for object.
1063 * @kobj: object.
1064 * @attr: attribute descriptor.
1065 */
1066int sysfs_create_bin_file(struct kobject *kobj,
1067 const struct bin_attribute *attr)
1068{
1069 BUG_ON(!kobj || !kobj->sd || !attr);
1070
1071 return sysfs_add_file(kobj->sd, &attr->attr, SYSFS_KOBJ_BIN_ATTR);
1072}
1073EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
1074
1075/**
1076 * sysfs_remove_bin_file - remove binary file for object.
1077 * @kobj: object.
1078 * @attr: attribute descriptor.
1079 */
1080void sysfs_remove_bin_file(struct kobject *kobj,
1081 const struct bin_attribute *attr)
1082{
Tejun Heo879f40d2013-11-23 17:21:49 -05001083 kernfs_remove_by_name(kobj->sd, attr->attr.name);
Tejun Heo3124eb12013-10-01 17:42:09 -04001084}
1085EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);
1086
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001087struct sysfs_schedule_callback_struct {
Alex Chiang66942062009-03-13 12:07:36 -06001088 struct list_head workq_list;
1089 struct kobject *kobj;
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001090 void (*func)(void *);
1091 void *data;
Alan Stern523ded72007-04-26 00:12:04 -07001092 struct module *owner;
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001093 struct work_struct work;
1094};
1095
Alex Chiangd1102712009-03-25 15:11:36 -06001096static struct workqueue_struct *sysfs_workqueue;
Alex Chiang66942062009-03-13 12:07:36 -06001097static DEFINE_MUTEX(sysfs_workq_mutex);
1098static LIST_HEAD(sysfs_workq);
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001099static void sysfs_schedule_callback_work(struct work_struct *work)
1100{
1101 struct sysfs_schedule_callback_struct *ss = container_of(work,
1102 struct sysfs_schedule_callback_struct, work);
1103
1104 (ss->func)(ss->data);
1105 kobject_put(ss->kobj);
Alan Stern523ded72007-04-26 00:12:04 -07001106 module_put(ss->owner);
Alex Chiang66942062009-03-13 12:07:36 -06001107 mutex_lock(&sysfs_workq_mutex);
1108 list_del(&ss->workq_list);
1109 mutex_unlock(&sysfs_workq_mutex);
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001110 kfree(ss);
1111}
1112
1113/**
1114 * sysfs_schedule_callback - helper to schedule a callback for a kobject
1115 * @kobj: object we're acting for.
1116 * @func: callback function to invoke later.
1117 * @data: argument to pass to @func.
Alan Stern523ded72007-04-26 00:12:04 -07001118 * @owner: module owning the callback code
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001119 *
1120 * sysfs attribute methods must not unregister themselves or their parent
1121 * kobject (which would amount to the same thing). Attempts to do so will
1122 * deadlock, since unregistration is mutually exclusive with driver
1123 * callbacks.
1124 *
1125 * Instead methods can call this routine, which will attempt to allocate
1126 * and schedule a workqueue request to call back @func with @data as its
1127 * argument in the workqueue's process context. @kobj will be pinned
1128 * until @func returns.
1129 *
1130 * Returns 0 if the request was submitted, -ENOMEM if storage could not
Alex Chiang66942062009-03-13 12:07:36 -06001131 * be allocated, -ENODEV if a reference to @owner isn't available,
1132 * -EAGAIN if a callback has already been scheduled for @kobj.
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001133 */
1134int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
Alan Stern523ded72007-04-26 00:12:04 -07001135 void *data, struct module *owner)
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001136{
Alex Chiang66942062009-03-13 12:07:36 -06001137 struct sysfs_schedule_callback_struct *ss, *tmp;
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001138
Alan Stern523ded72007-04-26 00:12:04 -07001139 if (!try_module_get(owner))
1140 return -ENODEV;
Alex Chiang66942062009-03-13 12:07:36 -06001141
1142 mutex_lock(&sysfs_workq_mutex);
1143 list_for_each_entry_safe(ss, tmp, &sysfs_workq, workq_list)
1144 if (ss->kobj == kobj) {
Alex Chiangd1102712009-03-25 15:11:36 -06001145 module_put(owner);
Alex Chiang66942062009-03-13 12:07:36 -06001146 mutex_unlock(&sysfs_workq_mutex);
1147 return -EAGAIN;
1148 }
1149 mutex_unlock(&sysfs_workq_mutex);
1150
Alex Chiangd1102712009-03-25 15:11:36 -06001151 if (sysfs_workqueue == NULL) {
Andrew Morton086a3772009-05-07 12:36:53 -07001152 sysfs_workqueue = create_singlethread_workqueue("sysfsd");
Alex Chiangd1102712009-03-25 15:11:36 -06001153 if (sysfs_workqueue == NULL) {
1154 module_put(owner);
1155 return -ENOMEM;
1156 }
1157 }
1158
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001159 ss = kmalloc(sizeof(*ss), GFP_KERNEL);
Alan Stern523ded72007-04-26 00:12:04 -07001160 if (!ss) {
1161 module_put(owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001162 return -ENOMEM;
Alan Stern523ded72007-04-26 00:12:04 -07001163 }
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001164 kobject_get(kobj);
1165 ss->kobj = kobj;
1166 ss->func = func;
1167 ss->data = data;
Alan Stern523ded72007-04-26 00:12:04 -07001168 ss->owner = owner;
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001169 INIT_WORK(&ss->work, sysfs_schedule_callback_work);
Alex Chiang66942062009-03-13 12:07:36 -06001170 INIT_LIST_HEAD(&ss->workq_list);
1171 mutex_lock(&sysfs_workq_mutex);
1172 list_add_tail(&ss->workq_list, &sysfs_workq);
1173 mutex_unlock(&sysfs_workq_mutex);
Alex Chiangd1102712009-03-25 15:11:36 -06001174 queue_work(sysfs_workqueue, &ss->work);
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001175 return 0;
1176}
1177EXPORT_SYMBOL_GPL(sysfs_schedule_callback);