blob: acba5835577e64bfddb64bc693d94658d7d0e10c [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 Heof9b9a622013-10-01 17:42:05 -040050static bool sysfs_is_bin(struct sysfs_dirent *sd)
51{
52 return sysfs_type(sd) == SYSFS_KOBJ_BIN_ATTR;
53}
54
Tejun Heo13c589d2013-10-01 17:42:02 -040055static struct sysfs_open_file *sysfs_of(struct file *file)
56{
57 return ((struct seq_file *)file->private_data)->private;
58}
59
Tejun Heo375b6112013-10-01 17:41:57 -040060/*
61 * Determine ktype->sysfs_ops for the given sysfs_dirent. This function
62 * must be called while holding an active reference.
63 */
64static const struct sysfs_ops *sysfs_file_ops(struct sysfs_dirent *sd)
65{
Tejun Heo7c6e2d32013-11-28 14:54:14 -050066 struct kobject *kobj = sd->s_parent->priv;
Tejun Heo375b6112013-10-01 17:41:57 -040067
Tejun Heo785a1622013-10-14 09:27:11 -040068 if (!sysfs_ignore_lockdep(sd))
69 lockdep_assert_held(sd);
Tejun Heo375b6112013-10-01 17:41:57 -040070 return kobj->ktype ? kobj->ktype->sysfs_ops : NULL;
71}
72
Tejun Heo13c589d2013-10-01 17:42:02 -040073/*
74 * Reads on sysfs are handled through seq_file, which takes care of hairy
75 * details like buffering and seeking. The following function pipes
76 * sysfs_ops->show() result through seq_file.
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 */
Tejun Heoc2b19da2013-11-28 14:54:16 -050078static int sysfs_kf_seq_show(struct seq_file *sf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Tejun Heo13c589d2013-10-01 17:42:02 -040080 struct sysfs_open_file *of = sf->private;
Tejun Heo7c6e2d32013-11-28 14:54:14 -050081 struct kobject *kobj = of->sd->s_parent->priv;
Tejun Heoc2b19da2013-11-28 14:54:16 -050082 const struct sysfs_ops *ops = sysfs_file_ops(of->sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 ssize_t count;
Tejun Heoc2b19da2013-11-28 14:54:16 -050084 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Tejun Heo13c589d2013-10-01 17:42:02 -040086 /* acquire buffer and ensure that it's >= PAGE_SIZE */
87 count = seq_get_buf(sf, &buf);
88 if (count < PAGE_SIZE) {
89 seq_commit(sf, -1);
90 return 0;
91 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Tejun Heo13c589d2013-10-01 17:42:02 -040093 /*
Tejun Heoc2b19da2013-11-28 14:54:16 -050094 * Invoke show(). Control may reach here via seq file lseek even
95 * if @ops->show() isn't implemented.
Tejun Heo13c589d2013-10-01 17:42:02 -040096 */
Tejun Heoc2b19da2013-11-28 14:54:16 -050097 if (ops->show) {
Tejun Heo7c6e2d32013-11-28 14:54:14 -050098 count = ops->show(kobj, of->sd->priv, buf);
Tejun Heoc2b19da2013-11-28 14:54:16 -050099 if (count < 0)
100 return count;
101 }
Tejun Heo0ab66082007-06-14 03:45:16 +0900102
Miao Xie8118a852007-11-21 14:55:19 -0800103 /*
104 * The code works fine with PAGE_SIZE return but it's likely to
105 * indicate truncated result or overflow in normal use cases.
106 */
Andrew Morton815d2d52008-03-04 15:09:07 -0800107 if (count >= (ssize_t)PAGE_SIZE) {
108 print_symbol("fill_read_buffer: %s returned bad count\n",
109 (unsigned long)ops->show);
110 /* Try to struggle along */
111 count = PAGE_SIZE - 1;
112 }
Tejun Heo13c589d2013-10-01 17:42:02 -0400113 seq_commit(sf, count);
114 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
Tejun Heoc2b19da2013-11-28 14:54:16 -0500117static ssize_t sysfs_kf_bin_read(struct sysfs_open_file *of, char *buf,
118 size_t count, loff_t pos)
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400119{
Tejun Heo7c6e2d32013-11-28 14:54:14 -0500120 struct bin_attribute *battr = of->sd->priv;
121 struct kobject *kobj = of->sd->s_parent->priv;
Tejun Heoc2b19da2013-11-28 14:54:16 -0500122 loff_t size = file_inode(of->file)->i_size;
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400123
Tejun Heoc2b19da2013-11-28 14:54:16 -0500124 if (!count)
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400125 return 0;
126
127 if (size) {
Tejun Heoc2b19da2013-11-28 14:54:16 -0500128 if (pos > size)
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400129 return 0;
Tejun Heoc2b19da2013-11-28 14:54:16 -0500130 if (pos + count > size)
131 count = size - pos;
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400132 }
133
Tejun Heoc2b19da2013-11-28 14:54:16 -0500134 if (!battr->read)
135 return -EIO;
136
137 return battr->read(of->file, kobj, battr, buf, pos, count);
138}
139
140static void *kernfs_seq_start(struct seq_file *sf, loff_t *ppos)
141{
142 struct sysfs_open_file *of = sf->private;
143
144 /*
145 * @of->mutex nests outside active ref and is just to ensure that
146 * the ops aren't called concurrently for the same open file.
147 */
148 mutex_lock(&of->mutex);
149 if (!sysfs_get_active(of->sd))
150 return ERR_PTR(-ENODEV);
151
152 /*
153 * The same behavior and code as single_open(). Returns !NULL if
154 * pos is at the beginning; otherwise, NULL.
155 */
156 return NULL + !*ppos;
157}
158
159static void *kernfs_seq_next(struct seq_file *sf, void *v, loff_t *ppos)
160{
161 /*
162 * The same behavior and code as single_open(), always terminate
163 * after the initial read.
164 */
165 ++*ppos;
166 return NULL;
167}
168
169static void kernfs_seq_stop(struct seq_file *sf, void *v)
170{
171 struct sysfs_open_file *of = sf->private;
172
173 sysfs_put_active(of->sd);
174 mutex_unlock(&of->mutex);
175}
176
177static int kernfs_seq_show(struct seq_file *sf, void *v)
178{
179 struct sysfs_open_file *of = sf->private;
180
181 of->event = atomic_read(&of->sd->s_attr.open->event);
182
183 return sysfs_kf_seq_show(sf, v);
184}
185
186static const struct seq_operations kernfs_seq_ops = {
187 .start = kernfs_seq_start,
188 .next = kernfs_seq_next,
189 .stop = kernfs_seq_stop,
190 .show = kernfs_seq_show,
191};
192
193/*
194 * As reading a bin file can have side-effects, the exact offset and bytes
195 * specified in read(2) call should be passed to the read callback making
196 * it difficult to use seq_file. Implement simplistic custom buffering for
197 * bin files.
198 */
199static ssize_t kernfs_file_direct_read(struct sysfs_open_file *of,
200 char __user *user_buf, size_t count,
201 loff_t *ppos)
202{
203 ssize_t len = min_t(size_t, count, PAGE_SIZE);
204 char *buf;
205
206 buf = kmalloc(len, GFP_KERNEL);
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400207 if (!buf)
208 return -ENOMEM;
209
Tejun Heoc2b19da2013-11-28 14:54:16 -0500210 /*
211 * @of->mutex nests outside active ref and is just to ensure that
212 * the ops aren't called concurrently for the same open file.
213 */
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400214 mutex_lock(&of->mutex);
215 if (!sysfs_get_active(of->sd)) {
Tejun Heoc2b19da2013-11-28 14:54:16 -0500216 len = -ENODEV;
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400217 mutex_unlock(&of->mutex);
218 goto out_free;
219 }
220
Tejun Heoc2b19da2013-11-28 14:54:16 -0500221 len = sysfs_kf_bin_read(of, buf, len, *ppos);
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400222
223 sysfs_put_active(of->sd);
224 mutex_unlock(&of->mutex);
225
Tejun Heoc2b19da2013-11-28 14:54:16 -0500226 if (len < 0)
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400227 goto out_free;
228
Tejun Heoc2b19da2013-11-28 14:54:16 -0500229 if (copy_to_user(user_buf, buf, len)) {
230 len = -EFAULT;
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400231 goto out_free;
232 }
233
Tejun Heoc2b19da2013-11-28 14:54:16 -0500234 *ppos += len;
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400235
236 out_free:
237 kfree(buf);
Tejun Heoc2b19da2013-11-28 14:54:16 -0500238 return len;
239}
240
241/**
242 * kernfs_file_read - kernfs vfs read callback
243 * @file: file pointer
244 * @user_buf: data to write
245 * @count: number of bytes
246 * @ppos: starting offset
247 */
248static ssize_t kernfs_file_read(struct file *file, char __user *user_buf,
249 size_t count, loff_t *ppos)
250{
251 struct sysfs_open_file *of = sysfs_of(file);
252
253 if (sysfs_is_bin(of->sd))
254 return kernfs_file_direct_read(of, user_buf, count, ppos);
255 else
256 return seq_read(file, user_buf, count, ppos);
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400257}
258
Tejun Heo50b38ca2013-11-28 14:54:17 -0500259/* kernfs write callback for regular sysfs files */
260static ssize_t sysfs_kf_write(struct sysfs_open_file *of, char *buf,
261 size_t count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Tejun Heo50b38ca2013-11-28 14:54:17 -0500263 const struct sysfs_ops *ops = sysfs_file_ops(of->sd);
Tejun Heo7c6e2d32013-11-28 14:54:14 -0500264 struct kobject *kobj = of->sd->s_parent->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Tejun Heo50b38ca2013-11-28 14:54:17 -0500266 if (!count)
267 return 0;
268
269 return ops->store(kobj, of->sd->priv, buf, count);
270}
271
272/* kernfs write callback for bin sysfs files */
273static ssize_t sysfs_kf_bin_write(struct sysfs_open_file *of, char *buf,
274 size_t count, loff_t pos)
275{
276 struct bin_attribute *battr = of->sd->priv;
277 struct kobject *kobj = of->sd->s_parent->priv;
278 loff_t size = file_inode(of->file)->i_size;
279
280 if (size) {
281 if (size <= pos)
282 return 0;
283 count = min_t(ssize_t, count, size - pos);
Tejun Heo8ef445f2013-10-01 17:42:01 -0400284 }
Tejun Heo50b38ca2013-11-28 14:54:17 -0500285 if (!count)
286 return 0;
Tejun Heo0ab66082007-06-14 03:45:16 +0900287
Tejun Heo50b38ca2013-11-28 14:54:17 -0500288 if (!battr->write)
289 return -EIO;
Tejun Heof9b9a622013-10-01 17:42:05 -0400290
Tejun Heo50b38ca2013-11-28 14:54:17 -0500291 return battr->write(of->file, kobj, battr, buf, pos, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294/**
Tejun Heo50b38ca2013-11-28 14:54:17 -0500295 * kernfs_file_write - kernfs vfs write callback
Tejun Heo8ef445f2013-10-01 17:42:01 -0400296 * @file: file pointer
297 * @user_buf: data to write
298 * @count: number of bytes
299 * @ppos: starting offset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 *
Tejun Heo50b38ca2013-11-28 14:54:17 -0500301 * Copy data in from userland and pass it to the matching kernfs write
302 * operation.
Tejun Heo8ef445f2013-10-01 17:42:01 -0400303 *
304 * There is no easy way for us to know if userspace is only doing a partial
305 * write, so we don't support them. We expect the entire buffer to come on
306 * the first write. Hint: if you're writing a value, first read the file,
307 * modify only the the value you're changing, then write entire buffer
308 * back.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 */
Tejun Heo50b38ca2013-11-28 14:54:17 -0500310static ssize_t kernfs_file_write(struct file *file, const char __user *user_buf,
311 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
Tejun Heo13c589d2013-10-01 17:42:02 -0400313 struct sysfs_open_file *of = sysfs_of(file);
Tejun Heof9b9a622013-10-01 17:42:05 -0400314 ssize_t len = min_t(size_t, count, PAGE_SIZE);
Tejun Heo8ef445f2013-10-01 17:42:01 -0400315 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Tejun Heo8ef445f2013-10-01 17:42:01 -0400317 buf = kmalloc(len + 1, GFP_KERNEL);
318 if (!buf)
319 return -ENOMEM;
320
321 if (copy_from_user(buf, user_buf, len)) {
322 len = -EFAULT;
323 goto out_free;
324 }
325 buf[len] = '\0'; /* guarantee string termination */
326
Tejun Heo50b38ca2013-11-28 14:54:17 -0500327 /*
328 * @of->mutex nests outside active ref and is just to ensure that
329 * the ops aren't called concurrently for the same open file.
330 */
331 mutex_lock(&of->mutex);
332 if (!sysfs_get_active(of->sd)) {
333 mutex_unlock(&of->mutex);
334 len = -ENODEV;
335 goto out_free;
336 }
337
338 if (sysfs_is_bin(of->sd))
339 len = sysfs_kf_bin_write(of, buf, len, *ppos);
340 else
341 len = sysfs_kf_write(of, buf, len, *ppos);
342
343 sysfs_put_active(of->sd);
344 mutex_unlock(&of->mutex);
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (len > 0)
347 *ppos += len;
Tejun Heo8ef445f2013-10-01 17:42:01 -0400348out_free:
349 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return len;
351}
352
Tejun Heofdbffaa2013-11-28 14:54:18 -0500353static int sysfs_kf_bin_mmap(struct sysfs_open_file *of,
354 struct vm_area_struct *vma)
355{
356 struct bin_attribute *battr = of->sd->priv;
357 struct kobject *kobj = of->sd->s_parent->priv;
358
359 if (!battr->mmap)
360 return -ENODEV;
361
362 return battr->mmap(of->file, kobj, battr, vma);
363}
364
365static void kernfs_vma_open(struct vm_area_struct *vma)
Tejun Heo73d97142013-10-01 17:42:07 -0400366{
367 struct file *file = vma->vm_file;
368 struct sysfs_open_file *of = sysfs_of(file);
369
370 if (!of->vm_ops)
371 return;
372
373 if (!sysfs_get_active(of->sd))
374 return;
375
376 if (of->vm_ops->open)
377 of->vm_ops->open(vma);
378
379 sysfs_put_active(of->sd);
380}
381
Tejun Heofdbffaa2013-11-28 14:54:18 -0500382static int kernfs_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Tejun Heo73d97142013-10-01 17:42:07 -0400383{
384 struct file *file = vma->vm_file;
385 struct sysfs_open_file *of = sysfs_of(file);
386 int ret;
387
388 if (!of->vm_ops)
389 return VM_FAULT_SIGBUS;
390
391 if (!sysfs_get_active(of->sd))
392 return VM_FAULT_SIGBUS;
393
394 ret = VM_FAULT_SIGBUS;
395 if (of->vm_ops->fault)
396 ret = of->vm_ops->fault(vma, vmf);
397
398 sysfs_put_active(of->sd);
399 return ret;
400}
401
Tejun Heofdbffaa2013-11-28 14:54:18 -0500402static int kernfs_vma_page_mkwrite(struct vm_area_struct *vma,
403 struct vm_fault *vmf)
Tejun Heo73d97142013-10-01 17:42:07 -0400404{
405 struct file *file = vma->vm_file;
406 struct sysfs_open_file *of = sysfs_of(file);
407 int ret;
408
409 if (!of->vm_ops)
410 return VM_FAULT_SIGBUS;
411
412 if (!sysfs_get_active(of->sd))
413 return VM_FAULT_SIGBUS;
414
415 ret = 0;
416 if (of->vm_ops->page_mkwrite)
417 ret = of->vm_ops->page_mkwrite(vma, vmf);
418 else
419 file_update_time(file);
420
421 sysfs_put_active(of->sd);
422 return ret;
423}
424
Tejun Heofdbffaa2013-11-28 14:54:18 -0500425static int kernfs_vma_access(struct vm_area_struct *vma, unsigned long addr,
426 void *buf, int len, int write)
Tejun Heo73d97142013-10-01 17:42:07 -0400427{
428 struct file *file = vma->vm_file;
429 struct sysfs_open_file *of = sysfs_of(file);
430 int ret;
431
432 if (!of->vm_ops)
433 return -EINVAL;
434
435 if (!sysfs_get_active(of->sd))
436 return -EINVAL;
437
438 ret = -EINVAL;
439 if (of->vm_ops->access)
440 ret = of->vm_ops->access(vma, addr, buf, len, write);
441
442 sysfs_put_active(of->sd);
443 return ret;
444}
445
446#ifdef CONFIG_NUMA
Tejun Heofdbffaa2013-11-28 14:54:18 -0500447static int kernfs_vma_set_policy(struct vm_area_struct *vma,
448 struct mempolicy *new)
Tejun Heo73d97142013-10-01 17:42:07 -0400449{
450 struct file *file = vma->vm_file;
451 struct sysfs_open_file *of = sysfs_of(file);
452 int ret;
453
454 if (!of->vm_ops)
455 return 0;
456
457 if (!sysfs_get_active(of->sd))
458 return -EINVAL;
459
460 ret = 0;
461 if (of->vm_ops->set_policy)
462 ret = of->vm_ops->set_policy(vma, new);
463
464 sysfs_put_active(of->sd);
465 return ret;
466}
467
Tejun Heofdbffaa2013-11-28 14:54:18 -0500468static struct mempolicy *kernfs_vma_get_policy(struct vm_area_struct *vma,
469 unsigned long addr)
Tejun Heo73d97142013-10-01 17:42:07 -0400470{
471 struct file *file = vma->vm_file;
472 struct sysfs_open_file *of = sysfs_of(file);
473 struct mempolicy *pol;
474
475 if (!of->vm_ops)
476 return vma->vm_policy;
477
478 if (!sysfs_get_active(of->sd))
479 return vma->vm_policy;
480
481 pol = vma->vm_policy;
482 if (of->vm_ops->get_policy)
483 pol = of->vm_ops->get_policy(vma, addr);
484
485 sysfs_put_active(of->sd);
486 return pol;
487}
488
Tejun Heofdbffaa2013-11-28 14:54:18 -0500489static int kernfs_vma_migrate(struct vm_area_struct *vma,
490 const nodemask_t *from, const nodemask_t *to,
491 unsigned long flags)
Tejun Heo73d97142013-10-01 17:42:07 -0400492{
493 struct file *file = vma->vm_file;
494 struct sysfs_open_file *of = sysfs_of(file);
495 int ret;
496
497 if (!of->vm_ops)
498 return 0;
499
500 if (!sysfs_get_active(of->sd))
501 return 0;
502
503 ret = 0;
504 if (of->vm_ops->migrate)
505 ret = of->vm_ops->migrate(vma, from, to, flags);
506
507 sysfs_put_active(of->sd);
508 return ret;
509}
510#endif
511
Tejun Heofdbffaa2013-11-28 14:54:18 -0500512static const struct vm_operations_struct kernfs_vm_ops = {
513 .open = kernfs_vma_open,
514 .fault = kernfs_vma_fault,
515 .page_mkwrite = kernfs_vma_page_mkwrite,
516 .access = kernfs_vma_access,
Tejun Heo73d97142013-10-01 17:42:07 -0400517#ifdef CONFIG_NUMA
Tejun Heofdbffaa2013-11-28 14:54:18 -0500518 .set_policy = kernfs_vma_set_policy,
519 .get_policy = kernfs_vma_get_policy,
520 .migrate = kernfs_vma_migrate,
Tejun Heo73d97142013-10-01 17:42:07 -0400521#endif
522};
523
Tejun Heofdbffaa2013-11-28 14:54:18 -0500524static int kernfs_file_mmap(struct file *file, struct vm_area_struct *vma)
Tejun Heo73d97142013-10-01 17:42:07 -0400525{
526 struct sysfs_open_file *of = sysfs_of(file);
Tejun Heo73d97142013-10-01 17:42:07 -0400527 int rc;
528
529 mutex_lock(&of->mutex);
530
Tejun Heo73d97142013-10-01 17:42:07 -0400531 rc = -ENODEV;
532 if (!sysfs_get_active(of->sd))
533 goto out_unlock;
534
Tejun Heofdbffaa2013-11-28 14:54:18 -0500535 if (sysfs_is_bin(of->sd))
536 rc = sysfs_kf_bin_mmap(of, vma);
Tejun Heo73d97142013-10-01 17:42:07 -0400537 if (rc)
538 goto out_put;
539
540 /*
541 * PowerPC's pci_mmap of legacy_mem uses shmem_zero_setup()
542 * to satisfy versions of X which crash if the mmap fails: that
543 * substitutes a new vm_file, and we don't then want bin_vm_ops.
544 */
545 if (vma->vm_file != file)
546 goto out_put;
547
548 rc = -EINVAL;
549 if (of->mmapped && of->vm_ops != vma->vm_ops)
550 goto out_put;
551
552 /*
553 * It is not possible to successfully wrap close.
554 * So error if someone is trying to use close.
555 */
556 rc = -EINVAL;
557 if (vma->vm_ops && vma->vm_ops->close)
558 goto out_put;
559
560 rc = 0;
561 of->mmapped = 1;
562 of->vm_ops = vma->vm_ops;
Tejun Heofdbffaa2013-11-28 14:54:18 -0500563 vma->vm_ops = &kernfs_vm_ops;
Tejun Heo73d97142013-10-01 17:42:07 -0400564out_put:
565 sysfs_put_active(of->sd);
566out_unlock:
567 mutex_unlock(&of->mutex);
568
569 return rc;
570}
571
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900572/**
573 * sysfs_get_open_dirent - get or create sysfs_open_dirent
574 * @sd: target sysfs_dirent
Tejun Heo58282d82013-10-01 17:41:59 -0400575 * @of: sysfs_open_file for this instance of open
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900576 *
577 * If @sd->s_attr.open exists, increment its reference count;
Tejun Heo58282d82013-10-01 17:41:59 -0400578 * otherwise, create one. @of is chained to the files list.
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900579 *
580 * LOCKING:
581 * Kernel thread context (may sleep).
582 *
583 * RETURNS:
584 * 0 on success, -errno on failure.
585 */
586static int sysfs_get_open_dirent(struct sysfs_dirent *sd,
Tejun Heo58282d82013-10-01 17:41:59 -0400587 struct sysfs_open_file *of)
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900588{
589 struct sysfs_open_dirent *od, *new_od = NULL;
590
591 retry:
Tejun Heoc75ec762013-10-01 17:41:58 -0400592 mutex_lock(&sysfs_open_file_mutex);
Neil Brown83db93f2009-09-15 16:05:51 -0700593 spin_lock_irq(&sysfs_open_dirent_lock);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900594
595 if (!sd->s_attr.open && new_od) {
596 sd->s_attr.open = new_od;
597 new_od = NULL;
598 }
599
600 od = sd->s_attr.open;
601 if (od) {
602 atomic_inc(&od->refcnt);
Tejun Heo58282d82013-10-01 17:41:59 -0400603 list_add_tail(&of->list, &od->files);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900604 }
605
Neil Brown83db93f2009-09-15 16:05:51 -0700606 spin_unlock_irq(&sysfs_open_dirent_lock);
Tejun Heoc75ec762013-10-01 17:41:58 -0400607 mutex_unlock(&sysfs_open_file_mutex);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900608
609 if (od) {
610 kfree(new_od);
611 return 0;
612 }
613
614 /* not there, initialize a new one and retry */
615 new_od = kmalloc(sizeof(*new_od), GFP_KERNEL);
616 if (!new_od)
617 return -ENOMEM;
618
619 atomic_set(&new_od->refcnt, 0);
Tejun Heoa4e8b912007-09-20 16:05:12 +0900620 atomic_set(&new_od->event, 1);
621 init_waitqueue_head(&new_od->poll);
Tejun Heo58282d82013-10-01 17:41:59 -0400622 INIT_LIST_HEAD(&new_od->files);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900623 goto retry;
624}
625
626/**
627 * sysfs_put_open_dirent - put sysfs_open_dirent
628 * @sd: target sysfs_dirent
Tejun Heo58282d82013-10-01 17:41:59 -0400629 * @of: associated sysfs_open_file
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900630 *
Tejun Heo58282d82013-10-01 17:41:59 -0400631 * Put @sd->s_attr.open and unlink @of from the files list. If
632 * reference count reaches zero, disassociate and free it.
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900633 *
634 * LOCKING:
635 * None.
636 */
637static void sysfs_put_open_dirent(struct sysfs_dirent *sd,
Tejun Heo58282d82013-10-01 17:41:59 -0400638 struct sysfs_open_file *of)
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900639{
640 struct sysfs_open_dirent *od = sd->s_attr.open;
Neil Brown83db93f2009-09-15 16:05:51 -0700641 unsigned long flags;
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900642
Tejun Heoc75ec762013-10-01 17:41:58 -0400643 mutex_lock(&sysfs_open_file_mutex);
Neil Brown83db93f2009-09-15 16:05:51 -0700644 spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900645
Tejun Heo73d97142013-10-01 17:42:07 -0400646 if (of)
647 list_del(&of->list);
648
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900649 if (atomic_dec_and_test(&od->refcnt))
650 sd->s_attr.open = NULL;
651 else
652 od = NULL;
653
Neil Brown83db93f2009-09-15 16:05:51 -0700654 spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
Tejun Heoc75ec762013-10-01 17:41:58 -0400655 mutex_unlock(&sysfs_open_file_mutex);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900656
657 kfree(od);
658}
659
Tejun Heoc6fb4492013-11-28 14:54:19 -0500660static int kernfs_file_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Tejun Heo3e519032007-06-14 03:45:15 +0900662 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
Tejun Heo7c6e2d32013-11-28 14:54:14 -0500663 struct kobject *kobj = attr_sd->s_parent->priv;
Tejun Heo58282d82013-10-01 17:41:59 -0400664 struct sysfs_open_file *of;
Tejun Heo027a4852013-11-17 11:17:36 +0900665 bool has_read, has_write, has_mmap;
Kay Sievers000f2a42007-11-02 13:47:53 +0100666 int error = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Tejun Heo0ab66082007-06-14 03:45:16 +0900668 /* need attr_sd for attr and ops, its parent for kobj */
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800669 if (!sysfs_get_active(attr_sd))
Tejun Heo0ab66082007-06-14 03:45:16 +0900670 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Tejun Heo49fe6042013-10-01 17:42:08 -0400672 if (sysfs_is_bin(attr_sd)) {
Tejun Heo7c6e2d32013-11-28 14:54:14 -0500673 struct bin_attribute *battr = attr_sd->priv;
Tejun Heo49fe6042013-10-01 17:42:08 -0400674
675 has_read = battr->read || battr->mmap;
676 has_write = battr->write || battr->mmap;
Tejun Heo027a4852013-11-17 11:17:36 +0900677 has_mmap = battr->mmap;
Tejun Heo49fe6042013-10-01 17:42:08 -0400678 } else {
679 const struct sysfs_ops *ops = sysfs_file_ops(attr_sd);
680
681 /* every kobject with an attribute needs a ktype assigned */
682 if (WARN(!ops, KERN_ERR
683 "missing sysfs attribute operations for kobject: %s\n",
684 kobject_name(kobj)))
685 goto err_out;
686
687 has_read = ops->show;
688 has_write = ops->store;
Tejun Heo027a4852013-11-17 11:17:36 +0900689 has_mmap = false;
Tejun Heo49fe6042013-10-01 17:42:08 -0400690 }
691
692 /* check perms and supported operations */
693 if ((file->f_mode & FMODE_WRITE) &&
694 (!(inode->i_mode & S_IWUGO) || !has_write))
Tejun Heo7b595752007-06-14 03:45:17 +0900695 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Tejun Heo49fe6042013-10-01 17:42:08 -0400697 if ((file->f_mode & FMODE_READ) &&
698 (!(inode->i_mode & S_IRUGO) || !has_read))
699 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Tejun Heo13c589d2013-10-01 17:42:02 -0400701 /* allocate a sysfs_open_file for the file */
Tejun Heo0ab66082007-06-14 03:45:16 +0900702 error = -ENOMEM;
Tejun Heo58282d82013-10-01 17:41:59 -0400703 of = kzalloc(sizeof(struct sysfs_open_file), GFP_KERNEL);
704 if (!of)
Tejun Heo7b595752007-06-14 03:45:17 +0900705 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Tejun Heo027a4852013-11-17 11:17:36 +0900707 /*
708 * The following is done to give a different lockdep key to
709 * @of->mutex for files which implement mmap. This is a rather
710 * crude way to avoid false positive lockdep warning around
711 * mm->mmap_sem - mmap nests @of->mutex under mm->mmap_sem and
712 * reading /sys/block/sda/trace/act_mask grabs sr_mutex, under
713 * which mm->mmap_sem nests, while holding @of->mutex. As each
714 * open file has a separate mutex, it's okay as long as those don't
715 * happen on the same file. At this point, we can't easily give
716 * each file a separate locking class. Let's differentiate on
717 * whether the file has mmap or not for now.
718 */
719 if (has_mmap)
720 mutex_init(&of->mutex);
721 else
722 mutex_init(&of->mutex);
723
Tejun Heobcafe4e2013-10-01 17:42:00 -0400724 of->sd = attr_sd;
725 of->file = file;
Tejun Heo13c589d2013-10-01 17:42:02 -0400726
727 /*
Tejun Heo49fe6042013-10-01 17:42:08 -0400728 * Always instantiate seq_file even if read access doesn't use
729 * seq_file or is not requested. This unifies private data access
730 * and readable regular files are the vast majority anyway.
Tejun Heo13c589d2013-10-01 17:42:02 -0400731 */
Tejun Heo49fe6042013-10-01 17:42:08 -0400732 if (sysfs_is_bin(attr_sd))
Tejun Heoc2b19da2013-11-28 14:54:16 -0500733 error = seq_open(file, NULL);
Tejun Heo49fe6042013-10-01 17:42:08 -0400734 else
Tejun Heoc2b19da2013-11-28 14:54:16 -0500735 error = seq_open(file, &kernfs_seq_ops);
Tejun Heo13c589d2013-10-01 17:42:02 -0400736 if (error)
737 goto err_free;
738
Tejun Heoc2b19da2013-11-28 14:54:16 -0500739 ((struct seq_file *)file->private_data)->private = of;
740
Tejun Heo13c589d2013-10-01 17:42:02 -0400741 /* seq_file clears PWRITE unconditionally, restore it if WRITE */
742 if (file->f_mode & FMODE_WRITE)
743 file->f_mode |= FMODE_PWRITE;
Tejun Heo0ab66082007-06-14 03:45:16 +0900744
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900745 /* make sure we have open dirent struct */
Tejun Heo58282d82013-10-01 17:41:59 -0400746 error = sysfs_get_open_dirent(attr_sd, of);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900747 if (error)
Tejun Heo13c589d2013-10-01 17:42:02 -0400748 goto err_close;
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900749
Tejun Heob05f0542007-09-20 16:05:10 +0900750 /* open succeeded, put active references */
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800751 sysfs_put_active(attr_sd);
Tejun Heo0ab66082007-06-14 03:45:16 +0900752 return 0;
753
Tejun Heo13c589d2013-10-01 17:42:02 -0400754err_close:
Tejun Heoc2b19da2013-11-28 14:54:16 -0500755 seq_release(inode, file);
Tejun Heo13c589d2013-10-01 17:42:02 -0400756err_free:
Tejun Heo58282d82013-10-01 17:41:59 -0400757 kfree(of);
Tejun Heo13c589d2013-10-01 17:42:02 -0400758err_out:
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800759 sysfs_put_active(attr_sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return error;
761}
762
Tejun Heoc6fb4492013-11-28 14:54:19 -0500763static int kernfs_file_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900765 struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata;
Tejun Heo13c589d2013-10-01 17:42:02 -0400766 struct sysfs_open_file *of = sysfs_of(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Tejun Heo58282d82013-10-01 17:41:59 -0400768 sysfs_put_open_dirent(sd, of);
Tejun Heoc2b19da2013-11-28 14:54:16 -0500769 seq_release(inode, filp);
Tejun Heo58282d82013-10-01 17:41:59 -0400770 kfree(of);
Tejun Heo50ab1a72007-09-20 16:05:10 +0900771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 return 0;
773}
774
Tejun Heo73d97142013-10-01 17:42:07 -0400775void sysfs_unmap_bin_file(struct sysfs_dirent *sd)
776{
777 struct sysfs_open_dirent *od;
778 struct sysfs_open_file *of;
779
780 if (!sysfs_is_bin(sd))
781 return;
782
783 spin_lock_irq(&sysfs_open_dirent_lock);
784 od = sd->s_attr.open;
785 if (od)
786 atomic_inc(&od->refcnt);
787 spin_unlock_irq(&sysfs_open_dirent_lock);
788 if (!od)
789 return;
790
791 mutex_lock(&sysfs_open_file_mutex);
792 list_for_each_entry(of, &od->files, list) {
793 struct inode *inode = file_inode(of->file);
794 unmap_mapping_range(inode->i_mapping, 0, 0, 1);
795 }
796 mutex_unlock(&sysfs_open_file_mutex);
797
798 sysfs_put_open_dirent(sd, NULL);
799}
800
NeilBrown4508a7a2006-03-20 17:53:53 +1100801/* Sysfs attribute files are pollable. The idea is that you read
802 * the content and then you use 'poll' or 'select' to wait for
803 * the content to change. When the content changes (assuming the
804 * manager for the kobject supports notification), poll will
805 * return POLLERR|POLLPRI, and select will return the fd whether
806 * it is waiting for read, write, or exceptions.
807 * Once poll/select indicates that the value has changed, you
Dan Williams2424b5d2008-04-07 15:35:01 -0700808 * need to close and re-open the file, or seek to 0 and read again.
NeilBrown4508a7a2006-03-20 17:53:53 +1100809 * Reminder: this only works for attributes which actively support
810 * it, and it is not possible to test an attribute from userspace
Rolf Eike Beera93720e2007-08-10 13:51:07 -0700811 * to see if it supports poll (Neither 'poll' nor 'select' return
NeilBrown4508a7a2006-03-20 17:53:53 +1100812 * an appropriate error code). When in doubt, set a suitable timeout value.
813 */
Tejun Heoc6fb4492013-11-28 14:54:19 -0500814static unsigned int kernfs_file_poll(struct file *filp, poll_table *wait)
NeilBrown4508a7a2006-03-20 17:53:53 +1100815{
Tejun Heo13c589d2013-10-01 17:42:02 -0400816 struct sysfs_open_file *of = sysfs_of(filp);
Tejun Heo0ab66082007-06-14 03:45:16 +0900817 struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata;
Tejun Heoa4e8b912007-09-20 16:05:12 +0900818 struct sysfs_open_dirent *od = attr_sd->s_attr.open;
Tejun Heo0ab66082007-06-14 03:45:16 +0900819
820 /* need parent for the kobj, grab both */
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800821 if (!sysfs_get_active(attr_sd))
Tejun Heo0ab66082007-06-14 03:45:16 +0900822 goto trigger;
NeilBrown4508a7a2006-03-20 17:53:53 +1100823
Tejun Heoa4e8b912007-09-20 16:05:12 +0900824 poll_wait(filp, &od->poll, wait);
NeilBrown4508a7a2006-03-20 17:53:53 +1100825
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800826 sysfs_put_active(attr_sd);
NeilBrown4508a7a2006-03-20 17:53:53 +1100827
Tejun Heo58282d82013-10-01 17:41:59 -0400828 if (of->event != atomic_read(&od->event))
Tejun Heo0ab66082007-06-14 03:45:16 +0900829 goto trigger;
830
KOSAKI Motohiro1af35572009-04-09 13:53:22 +0900831 return DEFAULT_POLLMASK;
Tejun Heo0ab66082007-06-14 03:45:16 +0900832
833 trigger:
KOSAKI Motohiro1af35572009-04-09 13:53:22 +0900834 return DEFAULT_POLLMASK|POLLERR|POLLPRI;
NeilBrown4508a7a2006-03-20 17:53:53 +1100835}
836
Neil Brownf1282c82008-07-16 08:58:04 +1000837void sysfs_notify_dirent(struct sysfs_dirent *sd)
838{
839 struct sysfs_open_dirent *od;
Neil Brown83db93f2009-09-15 16:05:51 -0700840 unsigned long flags;
Neil Brownf1282c82008-07-16 08:58:04 +1000841
Neil Brown83db93f2009-09-15 16:05:51 -0700842 spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
Neil Brownf1282c82008-07-16 08:58:04 +1000843
Nick Dyerfc60bb82013-06-07 15:45:13 +0100844 if (!WARN_ON(sysfs_type(sd) != SYSFS_KOBJ_ATTR)) {
845 od = sd->s_attr.open;
846 if (od) {
847 atomic_inc(&od->event);
848 wake_up_interruptible(&od->poll);
849 }
Neil Brownf1282c82008-07-16 08:58:04 +1000850 }
851
Neil Brown83db93f2009-09-15 16:05:51 -0700852 spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
Neil Brownf1282c82008-07-16 08:58:04 +1000853}
854EXPORT_SYMBOL_GPL(sysfs_notify_dirent);
855
Trent Piepho8c0e3992008-09-25 16:45:13 -0700856void sysfs_notify(struct kobject *k, const char *dir, const char *attr)
NeilBrown4508a7a2006-03-20 17:53:53 +1100857{
Tejun Heo51225032007-06-14 04:27:25 +0900858 struct sysfs_dirent *sd = k->sd;
NeilBrown4508a7a2006-03-20 17:53:53 +1100859
Tejun Heo51225032007-06-14 04:27:25 +0900860 mutex_lock(&sysfs_mutex);
NeilBrown4508a7a2006-03-20 17:53:53 +1100861
Tejun Heo51225032007-06-14 04:27:25 +0900862 if (sd && dir)
Tejun Heocfec0bc2013-09-11 22:29:09 -0400863 sd = sysfs_find_dirent(sd, dir, NULL);
Tejun Heo51225032007-06-14 04:27:25 +0900864 if (sd && attr)
Tejun Heocfec0bc2013-09-11 22:29:09 -0400865 sd = sysfs_find_dirent(sd, attr, NULL);
Neil Brownf1282c82008-07-16 08:58:04 +1000866 if (sd)
867 sysfs_notify_dirent(sd);
Tejun Heo51225032007-06-14 04:27:25 +0900868
869 mutex_unlock(&sysfs_mutex);
NeilBrown4508a7a2006-03-20 17:53:53 +1100870}
871EXPORT_SYMBOL_GPL(sysfs_notify);
872
Tejun Heoc6fb4492013-11-28 14:54:19 -0500873const struct file_operations kernfs_file_operations = {
Tejun Heoc2b19da2013-11-28 14:54:16 -0500874 .read = kernfs_file_read,
Tejun Heo50b38ca2013-11-28 14:54:17 -0500875 .write = kernfs_file_write,
Tejun Heo044e3bc2013-11-01 13:16:53 -0400876 .llseek = generic_file_llseek,
Tejun Heofdbffaa2013-11-28 14:54:18 -0500877 .mmap = kernfs_file_mmap,
Tejun Heoc6fb4492013-11-28 14:54:19 -0500878 .open = kernfs_file_open,
879 .release = kernfs_file_release,
880 .poll = kernfs_file_poll,
Tejun Heof9b9a622013-10-01 17:42:05 -0400881};
882
Tejun Heo58292cbe2013-09-11 22:29:04 -0400883int sysfs_add_file_mode_ns(struct sysfs_dirent *dir_sd,
884 const struct attribute *attr, int type,
885 umode_t amode, const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
James Bottomley0f423892008-03-20 20:47:52 -0500887 umode_t mode = (amode & S_IALLUGO) | S_IFREG;
Tejun Heofb6896d2007-06-14 04:27:24 +0900888 struct sysfs_addrm_cxt acxt;
Tejun Heoa26cd722007-06-14 03:45:14 +0900889 struct sysfs_dirent *sd;
Tejun Heo23dc2792007-08-02 21:38:03 +0900890 int rc;
Tejun Heoa26cd722007-06-14 03:45:14 +0900891
Tejun Heo3e519032007-06-14 03:45:15 +0900892 sd = sysfs_new_dirent(attr->name, mode, type);
Tejun Heo3007e992007-06-14 04:27:23 +0900893 if (!sd)
894 return -ENOMEM;
Eric W. Biederman487505c2011-10-12 21:53:38 +0000895
896 sd->s_ns = ns;
Tejun Heo7c6e2d32013-11-28 14:54:14 -0500897 sd->priv = (void *)attr;
Eric W. Biedermana2db6842010-02-11 15:20:00 -0800898 sysfs_dirent_init_lockdep(sd);
Tejun Heoa26cd722007-06-14 03:45:14 +0900899
Tejun Heod69ac5a2013-09-18 17:15:35 -0400900 sysfs_addrm_start(&acxt);
901 rc = sysfs_add_one(&acxt, sd, dir_sd);
Tejun Heo23dc2792007-08-02 21:38:03 +0900902 sysfs_addrm_finish(&acxt);
Tejun Heo3007e992007-06-14 04:27:23 +0900903
Tejun Heo23dc2792007-08-02 21:38:03 +0900904 if (rc)
Tejun Heo967e35d2007-07-18 16:38:11 +0900905 sysfs_put(sd);
Tejun Heo3007e992007-06-14 04:27:23 +0900906
Tejun Heo23dc2792007-08-02 21:38:03 +0900907 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909
910
James Bottomley0f423892008-03-20 20:47:52 -0500911int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,
912 int type)
913{
Tejun Heo58292cbe2013-09-11 22:29:04 -0400914 return sysfs_add_file_mode_ns(dir_sd, attr, type, attr->mode, NULL);
James Bottomley0f423892008-03-20 20:47:52 -0500915}
916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917/**
Tejun Heo58292cbe2013-09-11 22:29:04 -0400918 * sysfs_create_file_ns - create an attribute file for an object with custom ns
919 * @kobj: object we're creating for
920 * @attr: attribute descriptor
921 * @ns: namespace the new file should belong to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 */
Tejun Heo58292cbe2013-09-11 22:29:04 -0400923int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
924 const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
Tejun Heo608e2662007-06-14 04:27:22 +0900926 BUG_ON(!kobj || !kobj->sd || !attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Tejun Heo58292cbe2013-09-11 22:29:04 -0400928 return sysfs_add_file_mode_ns(kobj->sd, attr, SYSFS_KOBJ_ATTR,
929 attr->mode, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
931}
Tejun Heo58292cbe2013-09-11 22:29:04 -0400932EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Andi Kleen1c205ae2010-01-05 12:48:01 +0100934int sysfs_create_files(struct kobject *kobj, const struct attribute **ptr)
935{
936 int err = 0;
937 int i;
938
939 for (i = 0; ptr[i] && !err; i++)
940 err = sysfs_create_file(kobj, ptr[i]);
941 if (err)
942 while (--i >= 0)
943 sysfs_remove_file(kobj, ptr[i]);
944 return err;
945}
Greg Kroah-Hartman1b866752013-08-21 16:17:47 -0700946EXPORT_SYMBOL_GPL(sysfs_create_files);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948/**
Alan Sterndfa87c82007-02-20 15:02:44 -0500949 * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
950 * @kobj: object we're acting for.
951 * @attr: attribute descriptor.
952 * @group: group name.
953 */
954int sysfs_add_file_to_group(struct kobject *kobj,
955 const struct attribute *attr, const char *group)
956{
Tejun Heo608e2662007-06-14 04:27:22 +0900957 struct sysfs_dirent *dir_sd;
Alan Sterndfa87c82007-02-20 15:02:44 -0500958 int error;
959
James Bottomley11f24fb2008-01-02 18:44:05 -0600960 if (group)
Tejun Heo388975c2013-09-11 23:19:13 -0400961 dir_sd = sysfs_get_dirent(kobj->sd, group);
James Bottomley11f24fb2008-01-02 18:44:05 -0600962 else
963 dir_sd = sysfs_get(kobj->sd);
964
Tejun Heo608e2662007-06-14 04:27:22 +0900965 if (!dir_sd)
966 return -ENOENT;
967
968 error = sysfs_add_file(dir_sd, attr, SYSFS_KOBJ_ATTR);
969 sysfs_put(dir_sd);
970
Alan Sterndfa87c82007-02-20 15:02:44 -0500971 return error;
972}
973EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975/**
Kay Sievers31e5abe2005-04-18 21:57:32 -0700976 * sysfs_chmod_file - update the modified mode value on an object attribute.
977 * @kobj: object we're acting for.
978 * @attr: attribute descriptor.
979 * @mode: file permissions.
980 *
981 */
Jean Delvare49c19402010-07-02 16:54:05 +0200982int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
Al Viro48176a92011-07-24 03:40:40 -0400983 umode_t mode)
Kay Sievers31e5abe2005-04-18 21:57:32 -0700984{
Eric W. Biederman06fc0d62009-11-20 16:08:54 -0800985 struct sysfs_dirent *sd;
Maneesh Sonibc062b12005-07-29 12:13:35 -0700986 struct iattr newattrs;
Tejun Heo51225032007-06-14 04:27:25 +0900987 int rc;
Kay Sievers31e5abe2005-04-18 21:57:32 -0700988
Tejun Heo5d604182013-11-23 17:21:52 -0500989 sd = sysfs_get_dirent(kobj->sd, attr->name);
Eric W. Biederman06fc0d62009-11-20 16:08:54 -0800990 if (!sd)
Tejun Heo5d604182013-11-23 17:21:52 -0500991 return -ENOENT;
Tejun Heo51225032007-06-14 04:27:25 +0900992
Eric W. Biederman06fc0d62009-11-20 16:08:54 -0800993 newattrs.ia_mode = (mode & S_IALLUGO) | (sd->s_mode & ~S_IALLUGO);
Eric W. Biederman4c6974f2009-11-07 23:27:02 -0800994 newattrs.ia_valid = ATTR_MODE;
Tejun Heof88123e2007-09-20 16:05:10 +0900995
Tejun Heo5d604182013-11-23 17:21:52 -0500996 rc = kernfs_setattr(sd, &newattrs);
997
998 sysfs_put(sd);
Tejun Heo51225032007-06-14 04:27:25 +0900999 return rc;
Kay Sievers31e5abe2005-04-18 21:57:32 -07001000}
1001EXPORT_SYMBOL_GPL(sysfs_chmod_file);
1002
Kay Sievers31e5abe2005-04-18 21:57:32 -07001003/**
Tejun Heo58292cbe2013-09-11 22:29:04 -04001004 * sysfs_remove_file_ns - remove an object attribute with a custom ns tag
1005 * @kobj: object we're acting for
1006 * @attr: attribute descriptor
1007 * @ns: namespace tag of the file to remove
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 *
Tejun Heo58292cbe2013-09-11 22:29:04 -04001009 * Hash the attribute name and namespace tag and kill the victim.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 */
Tejun Heo58292cbe2013-09-11 22:29:04 -04001011void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
1012 const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
Tejun Heo58292cbe2013-09-11 22:29:04 -04001014 struct sysfs_dirent *dir_sd = kobj->sd;
Eric W. Biederman487505c2011-10-12 21:53:38 +00001015
Tejun Heo879f40d2013-11-23 17:21:49 -05001016 kernfs_remove_by_name_ns(dir_sd, attr->name, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017}
Tejun Heo58292cbe2013-09-11 22:29:04 -04001018EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Greg Kroah-Hartman1b18dc22013-08-21 16:28:26 -07001020void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr)
Andi Kleen1c205ae2010-01-05 12:48:01 +01001021{
1022 int i;
1023 for (i = 0; ptr[i]; i++)
1024 sysfs_remove_file(kobj, ptr[i]);
1025}
Greg Kroah-Hartman1b866752013-08-21 16:17:47 -07001026EXPORT_SYMBOL_GPL(sysfs_remove_files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Alan Sterndfa87c82007-02-20 15:02:44 -05001028/**
1029 * sysfs_remove_file_from_group - remove an attribute file from a group.
1030 * @kobj: object we're acting for.
1031 * @attr: attribute descriptor.
1032 * @group: group name.
1033 */
1034void sysfs_remove_file_from_group(struct kobject *kobj,
1035 const struct attribute *attr, const char *group)
1036{
Tejun Heo608e2662007-06-14 04:27:22 +09001037 struct sysfs_dirent *dir_sd;
Alan Sterndfa87c82007-02-20 15:02:44 -05001038
James Bottomley11f24fb2008-01-02 18:44:05 -06001039 if (group)
Tejun Heo388975c2013-09-11 23:19:13 -04001040 dir_sd = sysfs_get_dirent(kobj->sd, group);
James Bottomley11f24fb2008-01-02 18:44:05 -06001041 else
1042 dir_sd = sysfs_get(kobj->sd);
Tejun Heo608e2662007-06-14 04:27:22 +09001043 if (dir_sd) {
Tejun Heo879f40d2013-11-23 17:21:49 -05001044 kernfs_remove_by_name(dir_sd, attr->name);
Tejun Heo608e2662007-06-14 04:27:22 +09001045 sysfs_put(dir_sd);
Alan Sterndfa87c82007-02-20 15:02:44 -05001046 }
1047}
1048EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
1049
Tejun Heo3124eb12013-10-01 17:42:09 -04001050/**
1051 * sysfs_create_bin_file - create binary file for object.
1052 * @kobj: object.
1053 * @attr: attribute descriptor.
1054 */
1055int sysfs_create_bin_file(struct kobject *kobj,
1056 const struct bin_attribute *attr)
1057{
1058 BUG_ON(!kobj || !kobj->sd || !attr);
1059
1060 return sysfs_add_file(kobj->sd, &attr->attr, SYSFS_KOBJ_BIN_ATTR);
1061}
1062EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
1063
1064/**
1065 * sysfs_remove_bin_file - remove binary file for object.
1066 * @kobj: object.
1067 * @attr: attribute descriptor.
1068 */
1069void sysfs_remove_bin_file(struct kobject *kobj,
1070 const struct bin_attribute *attr)
1071{
Tejun Heo879f40d2013-11-23 17:21:49 -05001072 kernfs_remove_by_name(kobj->sd, attr->attr.name);
Tejun Heo3124eb12013-10-01 17:42:09 -04001073}
1074EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);
1075
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001076struct sysfs_schedule_callback_struct {
Alex Chiang66942062009-03-13 12:07:36 -06001077 struct list_head workq_list;
1078 struct kobject *kobj;
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001079 void (*func)(void *);
1080 void *data;
Alan Stern523ded72007-04-26 00:12:04 -07001081 struct module *owner;
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001082 struct work_struct work;
1083};
1084
Alex Chiangd1102712009-03-25 15:11:36 -06001085static struct workqueue_struct *sysfs_workqueue;
Alex Chiang66942062009-03-13 12:07:36 -06001086static DEFINE_MUTEX(sysfs_workq_mutex);
1087static LIST_HEAD(sysfs_workq);
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001088static void sysfs_schedule_callback_work(struct work_struct *work)
1089{
1090 struct sysfs_schedule_callback_struct *ss = container_of(work,
1091 struct sysfs_schedule_callback_struct, work);
1092
1093 (ss->func)(ss->data);
1094 kobject_put(ss->kobj);
Alan Stern523ded72007-04-26 00:12:04 -07001095 module_put(ss->owner);
Alex Chiang66942062009-03-13 12:07:36 -06001096 mutex_lock(&sysfs_workq_mutex);
1097 list_del(&ss->workq_list);
1098 mutex_unlock(&sysfs_workq_mutex);
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001099 kfree(ss);
1100}
1101
1102/**
1103 * sysfs_schedule_callback - helper to schedule a callback for a kobject
1104 * @kobj: object we're acting for.
1105 * @func: callback function to invoke later.
1106 * @data: argument to pass to @func.
Alan Stern523ded72007-04-26 00:12:04 -07001107 * @owner: module owning the callback code
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001108 *
1109 * sysfs attribute methods must not unregister themselves or their parent
1110 * kobject (which would amount to the same thing). Attempts to do so will
1111 * deadlock, since unregistration is mutually exclusive with driver
1112 * callbacks.
1113 *
1114 * Instead methods can call this routine, which will attempt to allocate
1115 * and schedule a workqueue request to call back @func with @data as its
1116 * argument in the workqueue's process context. @kobj will be pinned
1117 * until @func returns.
1118 *
1119 * Returns 0 if the request was submitted, -ENOMEM if storage could not
Alex Chiang66942062009-03-13 12:07:36 -06001120 * be allocated, -ENODEV if a reference to @owner isn't available,
1121 * -EAGAIN if a callback has already been scheduled for @kobj.
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001122 */
1123int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
Alan Stern523ded72007-04-26 00:12:04 -07001124 void *data, struct module *owner)
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001125{
Alex Chiang66942062009-03-13 12:07:36 -06001126 struct sysfs_schedule_callback_struct *ss, *tmp;
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001127
Alan Stern523ded72007-04-26 00:12:04 -07001128 if (!try_module_get(owner))
1129 return -ENODEV;
Alex Chiang66942062009-03-13 12:07:36 -06001130
1131 mutex_lock(&sysfs_workq_mutex);
1132 list_for_each_entry_safe(ss, tmp, &sysfs_workq, workq_list)
1133 if (ss->kobj == kobj) {
Alex Chiangd1102712009-03-25 15:11:36 -06001134 module_put(owner);
Alex Chiang66942062009-03-13 12:07:36 -06001135 mutex_unlock(&sysfs_workq_mutex);
1136 return -EAGAIN;
1137 }
1138 mutex_unlock(&sysfs_workq_mutex);
1139
Alex Chiangd1102712009-03-25 15:11:36 -06001140 if (sysfs_workqueue == NULL) {
Andrew Morton086a3772009-05-07 12:36:53 -07001141 sysfs_workqueue = create_singlethread_workqueue("sysfsd");
Alex Chiangd1102712009-03-25 15:11:36 -06001142 if (sysfs_workqueue == NULL) {
1143 module_put(owner);
1144 return -ENOMEM;
1145 }
1146 }
1147
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001148 ss = kmalloc(sizeof(*ss), GFP_KERNEL);
Alan Stern523ded72007-04-26 00:12:04 -07001149 if (!ss) {
1150 module_put(owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001151 return -ENOMEM;
Alan Stern523ded72007-04-26 00:12:04 -07001152 }
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001153 kobject_get(kobj);
1154 ss->kobj = kobj;
1155 ss->func = func;
1156 ss->data = data;
Alan Stern523ded72007-04-26 00:12:04 -07001157 ss->owner = owner;
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001158 INIT_WORK(&ss->work, sysfs_schedule_callback_work);
Alex Chiang66942062009-03-13 12:07:36 -06001159 INIT_LIST_HEAD(&ss->workq_list);
1160 mutex_lock(&sysfs_workq_mutex);
1161 list_add_tail(&ss->workq_list, &sysfs_workq);
1162 mutex_unlock(&sysfs_workq_mutex);
Alex Chiangd1102712009-03-25 15:11:36 -06001163 queue_work(sysfs_workqueue, &ss->work);
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001164 return 0;
1165}
1166EXPORT_SYMBOL_GPL(sysfs_schedule_callback);