blob: c324ee906df0d02b1a7ddfe033a81b5791626ace [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Tejun Heo6d66f5c2007-09-20 17:31:38 +09002 * fs/sysfs/file.c - sysfs regular (text) file implementation
3 *
4 * Copyright (c) 2001-3 Patrick Mochel
5 * Copyright (c) 2007 SUSE Linux Products GmbH
6 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
7 *
8 * This file is released under the GPLv2.
9 *
10 * Please see Documentation/filesystems/sysfs.txt for more information.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
13#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/kobject.h>
Andrew Morton815d2d52008-03-04 15:09:07 -080015#include <linux/kallsyms.h>
Robert P. J. Dayc6f87732008-03-13 22:41:52 -040016#include <linux/slab.h>
Miklos Szeredi93265d12008-06-16 13:46:47 +020017#include <linux/fsnotify.h>
Christoph Hellwig5f45f1a2005-06-23 00:09:12 -070018#include <linux/namei.h>
NeilBrown4508a7a2006-03-20 17:53:53 +110019#include <linux/poll.h>
Oliver Neukum94bebf42006-12-20 10:52:44 +010020#include <linux/list.h>
Dave Young52e8c2092007-07-26 11:03:54 +000021#include <linux/mutex.h>
Andrew Mortonae87221d2007-08-24 16:11:54 -070022#include <linux/limits.h>
Greg Kroah-Hartman060cc742013-08-21 16:34:59 -070023#include <linux/uaccess.h>
Tejun Heo13c589d2013-10-01 17:42:02 -040024#include <linux/seq_file.h>
Tejun Heo73d97142013-10-01 17:42:07 -040025#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#include "sysfs.h"
28
Tejun Heo85a4ffa2007-09-20 16:05:12 +090029/*
Tejun Heo58282d82013-10-01 17:41:59 -040030 * There's one sysfs_open_file for each open file and one sysfs_open_dirent
Tejun Heoc75ec762013-10-01 17:41:58 -040031 * for each sysfs_dirent with one or more open files.
Tejun Heo85a4ffa2007-09-20 16:05:12 +090032 *
Tejun Heoc75ec762013-10-01 17:41:58 -040033 * sysfs_dirent->s_attr.open points to sysfs_open_dirent. s_attr.open is
34 * protected by sysfs_open_dirent_lock.
35 *
Tejun Heo13c589d2013-10-01 17:42:02 -040036 * filp->private_data points to seq_file whose ->private points to
37 * sysfs_open_file. sysfs_open_files are chained at
Tejun Heo58282d82013-10-01 17:41:59 -040038 * sysfs_open_dirent->files, which is protected by sysfs_open_file_mutex.
Tejun Heo85a4ffa2007-09-20 16:05:12 +090039 */
Jiri Slabyd7b37882007-11-21 14:55:19 -080040static DEFINE_SPINLOCK(sysfs_open_dirent_lock);
Tejun Heoc75ec762013-10-01 17:41:58 -040041static DEFINE_MUTEX(sysfs_open_file_mutex);
Tejun Heo85a4ffa2007-09-20 16:05:12 +090042
43struct sysfs_open_dirent {
44 atomic_t refcnt;
Tejun Heoa4e8b912007-09-20 16:05:12 +090045 atomic_t event;
46 wait_queue_head_t poll;
Tejun Heo58282d82013-10-01 17:41:59 -040047 struct list_head files; /* goes through sysfs_open_file.list */
Tejun Heo85a4ffa2007-09-20 16:05:12 +090048};
49
Tejun Heo58282d82013-10-01 17:41:59 -040050struct sysfs_open_file {
Tejun Heobcafe4e2013-10-01 17:42:00 -040051 struct sysfs_dirent *sd;
52 struct file *file;
Dave Young52e8c2092007-07-26 11:03:54 +000053 struct mutex mutex;
Tejun Heo73107cb2007-06-14 03:45:16 +090054 int event;
Tejun Heo85a4ffa2007-09-20 16:05:12 +090055 struct list_head list;
Tejun Heo73d97142013-10-01 17:42:07 -040056
57 bool mmapped;
58 const struct vm_operations_struct *vm_ops;
Tejun Heo73107cb2007-06-14 03:45:16 +090059};
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Tejun Heof9b9a622013-10-01 17:42:05 -040061static bool sysfs_is_bin(struct sysfs_dirent *sd)
62{
63 return sysfs_type(sd) == SYSFS_KOBJ_BIN_ATTR;
64}
65
Tejun Heo13c589d2013-10-01 17:42:02 -040066static struct sysfs_open_file *sysfs_of(struct file *file)
67{
68 return ((struct seq_file *)file->private_data)->private;
69}
70
Tejun Heo375b6112013-10-01 17:41:57 -040071/*
72 * Determine ktype->sysfs_ops for the given sysfs_dirent. This function
73 * must be called while holding an active reference.
74 */
75static const struct sysfs_ops *sysfs_file_ops(struct sysfs_dirent *sd)
76{
77 struct kobject *kobj = sd->s_parent->s_dir.kobj;
78
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 Heo13c589d2013-10-01 17:42:02 -040089static int sysfs_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;
92 struct kobject *kobj = of->sd->s_parent->s_dir.kobj;
Tejun Heo375b6112013-10-01 17:41:57 -040093 const struct sysfs_ops *ops;
Tejun Heo13c589d2013-10-01 17:42:02 -040094 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 ssize_t count;
96
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 /*
105 * Need @of->sd for attr and ops, its parent for kobj. @of->mutex
106 * nests outside active ref and is just to ensure that the ops
107 * aren't called concurrently for the same open file.
108 */
109 mutex_lock(&of->mutex);
110 if (!sysfs_get_active(of->sd)) {
111 mutex_unlock(&of->mutex);
Tejun Heo0ab66082007-06-14 03:45:16 +0900112 return -ENODEV;
Tejun Heo13c589d2013-10-01 17:42:02 -0400113 }
Tejun Heo0ab66082007-06-14 03:45:16 +0900114
Tejun Heo13c589d2013-10-01 17:42:02 -0400115 of->event = atomic_read(&of->sd->s_attr.open->event);
Tejun Heo375b6112013-10-01 17:41:57 -0400116
Tejun Heo13c589d2013-10-01 17:42:02 -0400117 /*
118 * Lookup @ops and invoke show(). Control may reach here via seq
119 * file lseek even if @ops->show() isn't implemented.
120 */
121 ops = sysfs_file_ops(of->sd);
122 if (ops->show)
123 count = ops->show(kobj, of->sd->s_attr.attr, buf);
124 else
125 count = 0;
Tejun Heo0ab66082007-06-14 03:45:16 +0900126
Tejun Heo13c589d2013-10-01 17:42:02 -0400127 sysfs_put_active(of->sd);
128 mutex_unlock(&of->mutex);
129
130 if (count < 0)
131 return count;
Tejun Heo0ab66082007-06-14 03:45:16 +0900132
Miao Xie8118a852007-11-21 14:55:19 -0800133 /*
134 * The code works fine with PAGE_SIZE return but it's likely to
135 * indicate truncated result or overflow in normal use cases.
136 */
Andrew Morton815d2d52008-03-04 15:09:07 -0800137 if (count >= (ssize_t)PAGE_SIZE) {
138 print_symbol("fill_read_buffer: %s returned bad count\n",
139 (unsigned long)ops->show);
140 /* Try to struggle along */
141 count = PAGE_SIZE - 1;
142 }
Tejun Heo13c589d2013-10-01 17:42:02 -0400143 seq_commit(sf, count);
144 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400147/*
148 * Read method for bin files. As reading a bin file can have side-effects,
149 * the exact offset and bytes specified in read(2) call should be passed to
150 * the read callback making it difficult to use seq_file. Implement
151 * simplistic custom buffering for bin files.
152 */
153static ssize_t sysfs_bin_read(struct file *file, char __user *userbuf,
154 size_t bytes, loff_t *off)
155{
156 struct sysfs_open_file *of = sysfs_of(file);
157 struct bin_attribute *battr = of->sd->s_bin_attr.bin_attr;
158 struct kobject *kobj = of->sd->s_parent->s_dir.kobj;
159 int size = file_inode(file)->i_size;
160 int count = min_t(size_t, bytes, PAGE_SIZE);
161 loff_t offs = *off;
162 char *buf;
163
164 if (!bytes)
165 return 0;
166
167 if (size) {
168 if (offs > size)
169 return 0;
170 if (offs + count > size)
171 count = size - offs;
172 }
173
174 buf = kmalloc(count, GFP_KERNEL);
175 if (!buf)
176 return -ENOMEM;
177
178 /* need of->sd for battr, its parent for kobj */
179 mutex_lock(&of->mutex);
180 if (!sysfs_get_active(of->sd)) {
181 count = -ENODEV;
182 mutex_unlock(&of->mutex);
183 goto out_free;
184 }
185
186 if (battr->read)
187 count = battr->read(file, kobj, battr, buf, offs, count);
188 else
189 count = -EIO;
190
191 sysfs_put_active(of->sd);
192 mutex_unlock(&of->mutex);
193
194 if (count < 0)
195 goto out_free;
196
197 if (copy_to_user(userbuf, buf, count)) {
198 count = -EFAULT;
199 goto out_free;
200 }
201
202 pr_debug("offs = %lld, *off = %lld, count = %d\n", offs, *off, count);
203
204 *off = offs + count;
205
206 out_free:
207 kfree(buf);
208 return count;
209}
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211/**
Tejun Heo8ef445f2013-10-01 17:42:01 -0400212 * flush_write_buffer - push buffer to kobject
213 * @of: open file
214 * @buf: data buffer for file
Tejun Heof9b9a622013-10-01 17:42:05 -0400215 * @off: file offset to write to
Tejun Heo8ef445f2013-10-01 17:42:01 -0400216 * @count: number of bytes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 *
Tejun Heo8ef445f2013-10-01 17:42:01 -0400218 * Get the correct pointers for the kobject and the attribute we're dealing
219 * with, then call the store() method for it with @buf.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 */
Tejun Heof9b9a622013-10-01 17:42:05 -0400221static int flush_write_buffer(struct sysfs_open_file *of, char *buf, loff_t off,
Tejun Heo8ef445f2013-10-01 17:42:01 -0400222 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Tejun Heobcafe4e2013-10-01 17:42:00 -0400224 struct kobject *kobj = of->sd->s_parent->s_dir.kobj;
Tejun Heo8ef445f2013-10-01 17:42:01 -0400225 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Tejun Heo8ef445f2013-10-01 17:42:01 -0400227 /*
228 * Need @of->sd for attr and ops, its parent for kobj. @of->mutex
229 * nests outside active ref and is just to ensure that the ops
230 * aren't called concurrently for the same open file.
231 */
232 mutex_lock(&of->mutex);
233 if (!sysfs_get_active(of->sd)) {
234 mutex_unlock(&of->mutex);
Tejun Heo0ab66082007-06-14 03:45:16 +0900235 return -ENODEV;
Tejun Heo8ef445f2013-10-01 17:42:01 -0400236 }
Tejun Heo0ab66082007-06-14 03:45:16 +0900237
Tejun Heof9b9a622013-10-01 17:42:05 -0400238 if (sysfs_is_bin(of->sd)) {
239 struct bin_attribute *battr = of->sd->s_bin_attr.bin_attr;
240
241 rc = -EIO;
242 if (battr->write)
243 rc = battr->write(of->file, kobj, battr, buf, off,
244 count);
245 } else {
246 const struct sysfs_ops *ops = sysfs_file_ops(of->sd);
247
248 rc = ops->store(kobj, of->sd->s_attr.attr, buf, count);
249 }
Tejun Heo0ab66082007-06-14 03:45:16 +0900250
Tejun Heobcafe4e2013-10-01 17:42:00 -0400251 sysfs_put_active(of->sd);
Tejun Heo8ef445f2013-10-01 17:42:01 -0400252 mutex_unlock(&of->mutex);
Tejun Heo0ab66082007-06-14 03:45:16 +0900253
254 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257/**
Tejun Heo8ef445f2013-10-01 17:42:01 -0400258 * sysfs_write_file - write an attribute
259 * @file: file pointer
260 * @user_buf: data to write
261 * @count: number of bytes
262 * @ppos: starting offset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 *
Tejun Heo8ef445f2013-10-01 17:42:01 -0400264 * Copy data in from userland and pass it to the matching
265 * sysfs_ops->store() by invoking flush_write_buffer().
266 *
267 * There is no easy way for us to know if userspace is only doing a partial
268 * write, so we don't support them. We expect the entire buffer to come on
269 * the first write. Hint: if you're writing a value, first read the file,
270 * modify only the the value you're changing, then write entire buffer
271 * back.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 */
Tejun Heo8ef445f2013-10-01 17:42:01 -0400273static ssize_t sysfs_write_file(struct file *file, const char __user *user_buf,
Greg Kroah-Hartmanddfd6d02013-08-21 16:33:34 -0700274 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Tejun Heo13c589d2013-10-01 17:42:02 -0400276 struct sysfs_open_file *of = sysfs_of(file);
Tejun Heof9b9a622013-10-01 17:42:05 -0400277 ssize_t len = min_t(size_t, count, PAGE_SIZE);
Tejun Heo8ef445f2013-10-01 17:42:01 -0400278 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Tejun Heof9b9a622013-10-01 17:42:05 -0400280 if (sysfs_is_bin(of->sd)) {
281 loff_t size = file_inode(file)->i_size;
282
283 if (size <= *ppos)
284 return 0;
285 len = min_t(ssize_t, len, size - *ppos);
286 }
287
Tejun Heo8ef445f2013-10-01 17:42:01 -0400288 if (!len)
289 return 0;
290
291 buf = kmalloc(len + 1, GFP_KERNEL);
292 if (!buf)
293 return -ENOMEM;
294
295 if (copy_from_user(buf, user_buf, len)) {
296 len = -EFAULT;
297 goto out_free;
298 }
299 buf[len] = '\0'; /* guarantee string termination */
300
Tejun Heof9b9a622013-10-01 17:42:05 -0400301 len = flush_write_buffer(of, buf, *ppos, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 if (len > 0)
303 *ppos += len;
Tejun Heo8ef445f2013-10-01 17:42:01 -0400304out_free:
305 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return len;
307}
308
Tejun Heo73d97142013-10-01 17:42:07 -0400309static void sysfs_bin_vma_open(struct vm_area_struct *vma)
310{
311 struct file *file = vma->vm_file;
312 struct sysfs_open_file *of = sysfs_of(file);
313
314 if (!of->vm_ops)
315 return;
316
317 if (!sysfs_get_active(of->sd))
318 return;
319
320 if (of->vm_ops->open)
321 of->vm_ops->open(vma);
322
323 sysfs_put_active(of->sd);
324}
325
326static int sysfs_bin_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
327{
328 struct file *file = vma->vm_file;
329 struct sysfs_open_file *of = sysfs_of(file);
330 int ret;
331
332 if (!of->vm_ops)
333 return VM_FAULT_SIGBUS;
334
335 if (!sysfs_get_active(of->sd))
336 return VM_FAULT_SIGBUS;
337
338 ret = VM_FAULT_SIGBUS;
339 if (of->vm_ops->fault)
340 ret = of->vm_ops->fault(vma, vmf);
341
342 sysfs_put_active(of->sd);
343 return ret;
344}
345
346static int sysfs_bin_page_mkwrite(struct vm_area_struct *vma,
347 struct vm_fault *vmf)
348{
349 struct file *file = vma->vm_file;
350 struct sysfs_open_file *of = sysfs_of(file);
351 int ret;
352
353 if (!of->vm_ops)
354 return VM_FAULT_SIGBUS;
355
356 if (!sysfs_get_active(of->sd))
357 return VM_FAULT_SIGBUS;
358
359 ret = 0;
360 if (of->vm_ops->page_mkwrite)
361 ret = of->vm_ops->page_mkwrite(vma, vmf);
362 else
363 file_update_time(file);
364
365 sysfs_put_active(of->sd);
366 return ret;
367}
368
369static int sysfs_bin_access(struct vm_area_struct *vma, unsigned long addr,
370 void *buf, int len, int write)
371{
372 struct file *file = vma->vm_file;
373 struct sysfs_open_file *of = sysfs_of(file);
374 int ret;
375
376 if (!of->vm_ops)
377 return -EINVAL;
378
379 if (!sysfs_get_active(of->sd))
380 return -EINVAL;
381
382 ret = -EINVAL;
383 if (of->vm_ops->access)
384 ret = of->vm_ops->access(vma, addr, buf, len, write);
385
386 sysfs_put_active(of->sd);
387 return ret;
388}
389
390#ifdef CONFIG_NUMA
391static int sysfs_bin_set_policy(struct vm_area_struct *vma,
392 struct mempolicy *new)
393{
394 struct file *file = vma->vm_file;
395 struct sysfs_open_file *of = sysfs_of(file);
396 int ret;
397
398 if (!of->vm_ops)
399 return 0;
400
401 if (!sysfs_get_active(of->sd))
402 return -EINVAL;
403
404 ret = 0;
405 if (of->vm_ops->set_policy)
406 ret = of->vm_ops->set_policy(vma, new);
407
408 sysfs_put_active(of->sd);
409 return ret;
410}
411
412static struct mempolicy *sysfs_bin_get_policy(struct vm_area_struct *vma,
413 unsigned long addr)
414{
415 struct file *file = vma->vm_file;
416 struct sysfs_open_file *of = sysfs_of(file);
417 struct mempolicy *pol;
418
419 if (!of->vm_ops)
420 return vma->vm_policy;
421
422 if (!sysfs_get_active(of->sd))
423 return vma->vm_policy;
424
425 pol = vma->vm_policy;
426 if (of->vm_ops->get_policy)
427 pol = of->vm_ops->get_policy(vma, addr);
428
429 sysfs_put_active(of->sd);
430 return pol;
431}
432
433static int sysfs_bin_migrate(struct vm_area_struct *vma, const nodemask_t *from,
434 const nodemask_t *to, unsigned long flags)
435{
436 struct file *file = vma->vm_file;
437 struct sysfs_open_file *of = sysfs_of(file);
438 int ret;
439
440 if (!of->vm_ops)
441 return 0;
442
443 if (!sysfs_get_active(of->sd))
444 return 0;
445
446 ret = 0;
447 if (of->vm_ops->migrate)
448 ret = of->vm_ops->migrate(vma, from, to, flags);
449
450 sysfs_put_active(of->sd);
451 return ret;
452}
453#endif
454
455static const struct vm_operations_struct sysfs_bin_vm_ops = {
456 .open = sysfs_bin_vma_open,
457 .fault = sysfs_bin_fault,
458 .page_mkwrite = sysfs_bin_page_mkwrite,
459 .access = sysfs_bin_access,
460#ifdef CONFIG_NUMA
461 .set_policy = sysfs_bin_set_policy,
462 .get_policy = sysfs_bin_get_policy,
463 .migrate = sysfs_bin_migrate,
464#endif
465};
466
467static int sysfs_bin_mmap(struct file *file, struct vm_area_struct *vma)
468{
469 struct sysfs_open_file *of = sysfs_of(file);
470 struct bin_attribute *battr = of->sd->s_bin_attr.bin_attr;
471 struct kobject *kobj = of->sd->s_parent->s_dir.kobj;
472 int rc;
473
474 mutex_lock(&of->mutex);
475
476 /* need of->sd for battr, its parent for kobj */
477 rc = -ENODEV;
478 if (!sysfs_get_active(of->sd))
479 goto out_unlock;
480
481 rc = -EINVAL;
482 if (!battr->mmap)
483 goto out_put;
484
485 rc = battr->mmap(file, kobj, battr, vma);
486 if (rc)
487 goto out_put;
488
489 /*
490 * PowerPC's pci_mmap of legacy_mem uses shmem_zero_setup()
491 * to satisfy versions of X which crash if the mmap fails: that
492 * substitutes a new vm_file, and we don't then want bin_vm_ops.
493 */
494 if (vma->vm_file != file)
495 goto out_put;
496
497 rc = -EINVAL;
498 if (of->mmapped && of->vm_ops != vma->vm_ops)
499 goto out_put;
500
501 /*
502 * It is not possible to successfully wrap close.
503 * So error if someone is trying to use close.
504 */
505 rc = -EINVAL;
506 if (vma->vm_ops && vma->vm_ops->close)
507 goto out_put;
508
509 rc = 0;
510 of->mmapped = 1;
511 of->vm_ops = vma->vm_ops;
512 vma->vm_ops = &sysfs_bin_vm_ops;
513out_put:
514 sysfs_put_active(of->sd);
515out_unlock:
516 mutex_unlock(&of->mutex);
517
518 return rc;
519}
520
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900521/**
522 * sysfs_get_open_dirent - get or create sysfs_open_dirent
523 * @sd: target sysfs_dirent
Tejun Heo58282d82013-10-01 17:41:59 -0400524 * @of: sysfs_open_file for this instance of open
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900525 *
526 * If @sd->s_attr.open exists, increment its reference count;
Tejun Heo58282d82013-10-01 17:41:59 -0400527 * otherwise, create one. @of is chained to the files list.
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900528 *
529 * LOCKING:
530 * Kernel thread context (may sleep).
531 *
532 * RETURNS:
533 * 0 on success, -errno on failure.
534 */
535static int sysfs_get_open_dirent(struct sysfs_dirent *sd,
Tejun Heo58282d82013-10-01 17:41:59 -0400536 struct sysfs_open_file *of)
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900537{
538 struct sysfs_open_dirent *od, *new_od = NULL;
539
540 retry:
Tejun Heoc75ec762013-10-01 17:41:58 -0400541 mutex_lock(&sysfs_open_file_mutex);
Neil Brown83db93f2009-09-15 16:05:51 -0700542 spin_lock_irq(&sysfs_open_dirent_lock);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900543
544 if (!sd->s_attr.open && new_od) {
545 sd->s_attr.open = new_od;
546 new_od = NULL;
547 }
548
549 od = sd->s_attr.open;
550 if (od) {
551 atomic_inc(&od->refcnt);
Tejun Heo58282d82013-10-01 17:41:59 -0400552 list_add_tail(&of->list, &od->files);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900553 }
554
Neil Brown83db93f2009-09-15 16:05:51 -0700555 spin_unlock_irq(&sysfs_open_dirent_lock);
Tejun Heoc75ec762013-10-01 17:41:58 -0400556 mutex_unlock(&sysfs_open_file_mutex);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900557
558 if (od) {
559 kfree(new_od);
560 return 0;
561 }
562
563 /* not there, initialize a new one and retry */
564 new_od = kmalloc(sizeof(*new_od), GFP_KERNEL);
565 if (!new_od)
566 return -ENOMEM;
567
568 atomic_set(&new_od->refcnt, 0);
Tejun Heoa4e8b912007-09-20 16:05:12 +0900569 atomic_set(&new_od->event, 1);
570 init_waitqueue_head(&new_od->poll);
Tejun Heo58282d82013-10-01 17:41:59 -0400571 INIT_LIST_HEAD(&new_od->files);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900572 goto retry;
573}
574
575/**
576 * sysfs_put_open_dirent - put sysfs_open_dirent
577 * @sd: target sysfs_dirent
Tejun Heo58282d82013-10-01 17:41:59 -0400578 * @of: associated sysfs_open_file
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900579 *
Tejun Heo58282d82013-10-01 17:41:59 -0400580 * Put @sd->s_attr.open and unlink @of from the files list. If
581 * reference count reaches zero, disassociate and free it.
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900582 *
583 * LOCKING:
584 * None.
585 */
586static void sysfs_put_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 = sd->s_attr.open;
Neil Brown83db93f2009-09-15 16:05:51 -0700590 unsigned long flags;
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900591
Tejun Heoc75ec762013-10-01 17:41:58 -0400592 mutex_lock(&sysfs_open_file_mutex);
Neil Brown83db93f2009-09-15 16:05:51 -0700593 spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900594
Tejun Heo73d97142013-10-01 17:42:07 -0400595 if (of)
596 list_del(&of->list);
597
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900598 if (atomic_dec_and_test(&od->refcnt))
599 sd->s_attr.open = NULL;
600 else
601 od = NULL;
602
Neil Brown83db93f2009-09-15 16:05:51 -0700603 spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
Tejun Heoc75ec762013-10-01 17:41:58 -0400604 mutex_unlock(&sysfs_open_file_mutex);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900605
606 kfree(od);
607}
608
Oliver Neukum94bebf42006-12-20 10:52:44 +0100609static int sysfs_open_file(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
Tejun Heo3e519032007-06-14 03:45:15 +0900611 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900612 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
Tejun Heo58282d82013-10-01 17:41:59 -0400613 struct sysfs_open_file *of;
Tejun Heo49fe6042013-10-01 17:42:08 -0400614 bool has_read, has_write;
Kay Sievers000f2a42007-11-02 13:47:53 +0100615 int error = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Tejun Heo0ab66082007-06-14 03:45:16 +0900617 /* need attr_sd for attr and ops, its parent for kobj */
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800618 if (!sysfs_get_active(attr_sd))
Tejun Heo0ab66082007-06-14 03:45:16 +0900619 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Tejun Heo49fe6042013-10-01 17:42:08 -0400621 if (sysfs_is_bin(attr_sd)) {
622 struct bin_attribute *battr = attr_sd->s_bin_attr.bin_attr;
623
624 has_read = battr->read || battr->mmap;
625 has_write = battr->write || battr->mmap;
626 } else {
627 const struct sysfs_ops *ops = sysfs_file_ops(attr_sd);
628
629 /* every kobject with an attribute needs a ktype assigned */
630 if (WARN(!ops, KERN_ERR
631 "missing sysfs attribute operations for kobject: %s\n",
632 kobject_name(kobj)))
633 goto err_out;
634
635 has_read = ops->show;
636 has_write = ops->store;
637 }
638
639 /* check perms and supported operations */
640 if ((file->f_mode & FMODE_WRITE) &&
641 (!(inode->i_mode & S_IWUGO) || !has_write))
Tejun Heo7b595752007-06-14 03:45:17 +0900642 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Tejun Heo49fe6042013-10-01 17:42:08 -0400644 if ((file->f_mode & FMODE_READ) &&
645 (!(inode->i_mode & S_IRUGO) || !has_read))
646 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Tejun Heo13c589d2013-10-01 17:42:02 -0400648 /* allocate a sysfs_open_file for the file */
Tejun Heo0ab66082007-06-14 03:45:16 +0900649 error = -ENOMEM;
Tejun Heo58282d82013-10-01 17:41:59 -0400650 of = kzalloc(sizeof(struct sysfs_open_file), GFP_KERNEL);
651 if (!of)
Tejun Heo7b595752007-06-14 03:45:17 +0900652 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Tejun Heo58282d82013-10-01 17:41:59 -0400654 mutex_init(&of->mutex);
Tejun Heobcafe4e2013-10-01 17:42:00 -0400655 of->sd = attr_sd;
656 of->file = file;
Tejun Heo13c589d2013-10-01 17:42:02 -0400657
658 /*
Tejun Heo49fe6042013-10-01 17:42:08 -0400659 * Always instantiate seq_file even if read access doesn't use
660 * seq_file or is not requested. This unifies private data access
661 * and readable regular files are the vast majority anyway.
Tejun Heo13c589d2013-10-01 17:42:02 -0400662 */
Tejun Heo49fe6042013-10-01 17:42:08 -0400663 if (sysfs_is_bin(attr_sd))
664 error = single_open(file, NULL, of);
665 else
666 error = single_open(file, sysfs_seq_show, of);
Tejun Heo13c589d2013-10-01 17:42:02 -0400667 if (error)
668 goto err_free;
669
670 /* seq_file clears PWRITE unconditionally, restore it if WRITE */
671 if (file->f_mode & FMODE_WRITE)
672 file->f_mode |= FMODE_PWRITE;
Tejun Heo0ab66082007-06-14 03:45:16 +0900673
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900674 /* make sure we have open dirent struct */
Tejun Heo58282d82013-10-01 17:41:59 -0400675 error = sysfs_get_open_dirent(attr_sd, of);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900676 if (error)
Tejun Heo13c589d2013-10-01 17:42:02 -0400677 goto err_close;
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900678
Tejun Heob05f0542007-09-20 16:05:10 +0900679 /* open succeeded, put active references */
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800680 sysfs_put_active(attr_sd);
Tejun Heo0ab66082007-06-14 03:45:16 +0900681 return 0;
682
Tejun Heo13c589d2013-10-01 17:42:02 -0400683err_close:
684 single_release(inode, file);
685err_free:
Tejun Heo58282d82013-10-01 17:41:59 -0400686 kfree(of);
Tejun Heo13c589d2013-10-01 17:42:02 -0400687err_out:
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800688 sysfs_put_active(attr_sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return error;
690}
691
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900692static int sysfs_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900694 struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata;
Tejun Heo13c589d2013-10-01 17:42:02 -0400695 struct sysfs_open_file *of = sysfs_of(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Tejun Heo58282d82013-10-01 17:41:59 -0400697 sysfs_put_open_dirent(sd, of);
Tejun Heo13c589d2013-10-01 17:42:02 -0400698 single_release(inode, filp);
Tejun Heo58282d82013-10-01 17:41:59 -0400699 kfree(of);
Tejun Heo50ab1a72007-09-20 16:05:10 +0900700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return 0;
702}
703
Tejun Heo73d97142013-10-01 17:42:07 -0400704void sysfs_unmap_bin_file(struct sysfs_dirent *sd)
705{
706 struct sysfs_open_dirent *od;
707 struct sysfs_open_file *of;
708
709 if (!sysfs_is_bin(sd))
710 return;
711
712 spin_lock_irq(&sysfs_open_dirent_lock);
713 od = sd->s_attr.open;
714 if (od)
715 atomic_inc(&od->refcnt);
716 spin_unlock_irq(&sysfs_open_dirent_lock);
717 if (!od)
718 return;
719
720 mutex_lock(&sysfs_open_file_mutex);
721 list_for_each_entry(of, &od->files, list) {
722 struct inode *inode = file_inode(of->file);
723 unmap_mapping_range(inode->i_mapping, 0, 0, 1);
724 }
725 mutex_unlock(&sysfs_open_file_mutex);
726
727 sysfs_put_open_dirent(sd, NULL);
728}
729
NeilBrown4508a7a2006-03-20 17:53:53 +1100730/* Sysfs attribute files are pollable. The idea is that you read
731 * the content and then you use 'poll' or 'select' to wait for
732 * the content to change. When the content changes (assuming the
733 * manager for the kobject supports notification), poll will
734 * return POLLERR|POLLPRI, and select will return the fd whether
735 * it is waiting for read, write, or exceptions.
736 * Once poll/select indicates that the value has changed, you
Dan Williams2424b5d2008-04-07 15:35:01 -0700737 * need to close and re-open the file, or seek to 0 and read again.
NeilBrown4508a7a2006-03-20 17:53:53 +1100738 * Reminder: this only works for attributes which actively support
739 * it, and it is not possible to test an attribute from userspace
Rolf Eike Beera93720e2007-08-10 13:51:07 -0700740 * to see if it supports poll (Neither 'poll' nor 'select' return
NeilBrown4508a7a2006-03-20 17:53:53 +1100741 * an appropriate error code). When in doubt, set a suitable timeout value.
742 */
743static unsigned int sysfs_poll(struct file *filp, poll_table *wait)
744{
Tejun Heo13c589d2013-10-01 17:42:02 -0400745 struct sysfs_open_file *of = sysfs_of(filp);
Tejun Heo0ab66082007-06-14 03:45:16 +0900746 struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata;
Tejun Heoa4e8b912007-09-20 16:05:12 +0900747 struct sysfs_open_dirent *od = attr_sd->s_attr.open;
Tejun Heo0ab66082007-06-14 03:45:16 +0900748
749 /* need parent for the kobj, grab both */
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800750 if (!sysfs_get_active(attr_sd))
Tejun Heo0ab66082007-06-14 03:45:16 +0900751 goto trigger;
NeilBrown4508a7a2006-03-20 17:53:53 +1100752
Tejun Heoa4e8b912007-09-20 16:05:12 +0900753 poll_wait(filp, &od->poll, wait);
NeilBrown4508a7a2006-03-20 17:53:53 +1100754
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800755 sysfs_put_active(attr_sd);
NeilBrown4508a7a2006-03-20 17:53:53 +1100756
Tejun Heo58282d82013-10-01 17:41:59 -0400757 if (of->event != atomic_read(&od->event))
Tejun Heo0ab66082007-06-14 03:45:16 +0900758 goto trigger;
759
KOSAKI Motohiro1af35572009-04-09 13:53:22 +0900760 return DEFAULT_POLLMASK;
Tejun Heo0ab66082007-06-14 03:45:16 +0900761
762 trigger:
KOSAKI Motohiro1af35572009-04-09 13:53:22 +0900763 return DEFAULT_POLLMASK|POLLERR|POLLPRI;
NeilBrown4508a7a2006-03-20 17:53:53 +1100764}
765
Neil Brownf1282c82008-07-16 08:58:04 +1000766void sysfs_notify_dirent(struct sysfs_dirent *sd)
767{
768 struct sysfs_open_dirent *od;
Neil Brown83db93f2009-09-15 16:05:51 -0700769 unsigned long flags;
Neil Brownf1282c82008-07-16 08:58:04 +1000770
Neil Brown83db93f2009-09-15 16:05:51 -0700771 spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
Neil Brownf1282c82008-07-16 08:58:04 +1000772
Nick Dyerfc60bb82013-06-07 15:45:13 +0100773 if (!WARN_ON(sysfs_type(sd) != SYSFS_KOBJ_ATTR)) {
774 od = sd->s_attr.open;
775 if (od) {
776 atomic_inc(&od->event);
777 wake_up_interruptible(&od->poll);
778 }
Neil Brownf1282c82008-07-16 08:58:04 +1000779 }
780
Neil Brown83db93f2009-09-15 16:05:51 -0700781 spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
Neil Brownf1282c82008-07-16 08:58:04 +1000782}
783EXPORT_SYMBOL_GPL(sysfs_notify_dirent);
784
Trent Piepho8c0e3992008-09-25 16:45:13 -0700785void sysfs_notify(struct kobject *k, const char *dir, const char *attr)
NeilBrown4508a7a2006-03-20 17:53:53 +1100786{
Tejun Heo51225032007-06-14 04:27:25 +0900787 struct sysfs_dirent *sd = k->sd;
NeilBrown4508a7a2006-03-20 17:53:53 +1100788
Tejun Heo51225032007-06-14 04:27:25 +0900789 mutex_lock(&sysfs_mutex);
NeilBrown4508a7a2006-03-20 17:53:53 +1100790
Tejun Heo51225032007-06-14 04:27:25 +0900791 if (sd && dir)
Tejun Heocfec0bc2013-09-11 22:29:09 -0400792 sd = sysfs_find_dirent(sd, dir, NULL);
Tejun Heo51225032007-06-14 04:27:25 +0900793 if (sd && attr)
Tejun Heocfec0bc2013-09-11 22:29:09 -0400794 sd = sysfs_find_dirent(sd, attr, NULL);
Neil Brownf1282c82008-07-16 08:58:04 +1000795 if (sd)
796 sysfs_notify_dirent(sd);
Tejun Heo51225032007-06-14 04:27:25 +0900797
798 mutex_unlock(&sysfs_mutex);
NeilBrown4508a7a2006-03-20 17:53:53 +1100799}
800EXPORT_SYMBOL_GPL(sysfs_notify);
801
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800802const struct file_operations sysfs_file_operations = {
Tejun Heo13c589d2013-10-01 17:42:02 -0400803 .read = seq_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 .write = sysfs_write_file,
Tejun Heo13c589d2013-10-01 17:42:02 -0400805 .llseek = seq_lseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 .open = sysfs_open_file,
807 .release = sysfs_release,
NeilBrown4508a7a2006-03-20 17:53:53 +1100808 .poll = sysfs_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809};
810
Tejun Heof9b9a622013-10-01 17:42:05 -0400811const struct file_operations sysfs_bin_operations = {
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400812 .read = sysfs_bin_read,
Tejun Heof9b9a622013-10-01 17:42:05 -0400813 .write = sysfs_write_file,
814 .llseek = generic_file_llseek,
Tejun Heo73d97142013-10-01 17:42:07 -0400815 .mmap = sysfs_bin_mmap,
Tejun Heo49fe6042013-10-01 17:42:08 -0400816 .open = sysfs_open_file,
817 .release = sysfs_release,
818 .poll = sysfs_poll,
Tejun Heof9b9a622013-10-01 17:42:05 -0400819};
820
Tejun Heo58292cbe2013-09-11 22:29:04 -0400821int sysfs_add_file_mode_ns(struct sysfs_dirent *dir_sd,
822 const struct attribute *attr, int type,
823 umode_t amode, const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
James Bottomley0f423892008-03-20 20:47:52 -0500825 umode_t mode = (amode & S_IALLUGO) | S_IFREG;
Tejun Heofb6896da2007-06-14 04:27:24 +0900826 struct sysfs_addrm_cxt acxt;
Tejun Heoa26cd722007-06-14 03:45:14 +0900827 struct sysfs_dirent *sd;
Tejun Heo23dc2792007-08-02 21:38:03 +0900828 int rc;
Tejun Heoa26cd722007-06-14 03:45:14 +0900829
Tejun Heo3e519032007-06-14 03:45:15 +0900830 sd = sysfs_new_dirent(attr->name, mode, type);
Tejun Heo3007e992007-06-14 04:27:23 +0900831 if (!sd)
832 return -ENOMEM;
Eric W. Biederman487505c2011-10-12 21:53:38 +0000833
834 sd->s_ns = ns;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900835 sd->s_attr.attr = (void *)attr;
Eric W. Biedermana2db6842010-02-11 15:20:00 -0800836 sysfs_dirent_init_lockdep(sd);
Tejun Heoa26cd722007-06-14 03:45:14 +0900837
Tejun Heod69ac5a2013-09-18 17:15:35 -0400838 sysfs_addrm_start(&acxt);
839 rc = sysfs_add_one(&acxt, sd, dir_sd);
Tejun Heo23dc2792007-08-02 21:38:03 +0900840 sysfs_addrm_finish(&acxt);
Tejun Heo3007e992007-06-14 04:27:23 +0900841
Tejun Heo23dc2792007-08-02 21:38:03 +0900842 if (rc)
Tejun Heo967e35d2007-07-18 16:38:11 +0900843 sysfs_put(sd);
Tejun Heo3007e992007-06-14 04:27:23 +0900844
Tejun Heo23dc2792007-08-02 21:38:03 +0900845 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
847
848
James Bottomley0f423892008-03-20 20:47:52 -0500849int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,
850 int type)
851{
Tejun Heo58292cbe2013-09-11 22:29:04 -0400852 return sysfs_add_file_mode_ns(dir_sd, attr, type, attr->mode, NULL);
James Bottomley0f423892008-03-20 20:47:52 -0500853}
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855/**
Tejun Heo58292cbe2013-09-11 22:29:04 -0400856 * sysfs_create_file_ns - create an attribute file for an object with custom ns
857 * @kobj: object we're creating for
858 * @attr: attribute descriptor
859 * @ns: namespace the new file should belong to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 */
Tejun Heo58292cbe2013-09-11 22:29:04 -0400861int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
862 const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
Tejun Heo608e2662007-06-14 04:27:22 +0900864 BUG_ON(!kobj || !kobj->sd || !attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Tejun Heo58292cbe2013-09-11 22:29:04 -0400866 return sysfs_add_file_mode_ns(kobj->sd, attr, SYSFS_KOBJ_ATTR,
867 attr->mode, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
869}
Tejun Heo58292cbe2013-09-11 22:29:04 -0400870EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Andi Kleen1c205ae2010-01-05 12:48:01 +0100872int sysfs_create_files(struct kobject *kobj, const struct attribute **ptr)
873{
874 int err = 0;
875 int i;
876
877 for (i = 0; ptr[i] && !err; i++)
878 err = sysfs_create_file(kobj, ptr[i]);
879 if (err)
880 while (--i >= 0)
881 sysfs_remove_file(kobj, ptr[i]);
882 return err;
883}
Greg Kroah-Hartman1b866752013-08-21 16:17:47 -0700884EXPORT_SYMBOL_GPL(sysfs_create_files);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886/**
Alan Sterndfa87c82007-02-20 15:02:44 -0500887 * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
888 * @kobj: object we're acting for.
889 * @attr: attribute descriptor.
890 * @group: group name.
891 */
892int sysfs_add_file_to_group(struct kobject *kobj,
893 const struct attribute *attr, const char *group)
894{
Tejun Heo608e2662007-06-14 04:27:22 +0900895 struct sysfs_dirent *dir_sd;
Alan Sterndfa87c82007-02-20 15:02:44 -0500896 int error;
897
James Bottomley11f24fb2008-01-02 18:44:05 -0600898 if (group)
Tejun Heo388975c2013-09-11 23:19:13 -0400899 dir_sd = sysfs_get_dirent(kobj->sd, group);
James Bottomley11f24fb2008-01-02 18:44:05 -0600900 else
901 dir_sd = sysfs_get(kobj->sd);
902
Tejun Heo608e2662007-06-14 04:27:22 +0900903 if (!dir_sd)
904 return -ENOENT;
905
906 error = sysfs_add_file(dir_sd, attr, SYSFS_KOBJ_ATTR);
907 sysfs_put(dir_sd);
908
Alan Sterndfa87c82007-02-20 15:02:44 -0500909 return error;
910}
911EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913/**
Kay Sievers31e5abe2005-04-18 21:57:32 -0700914 * sysfs_chmod_file - update the modified mode value on an object attribute.
915 * @kobj: object we're acting for.
916 * @attr: attribute descriptor.
917 * @mode: file permissions.
918 *
919 */
Jean Delvare49c19402010-07-02 16:54:05 +0200920int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
Al Viro48176a92011-07-24 03:40:40 -0400921 umode_t mode)
Kay Sievers31e5abe2005-04-18 21:57:32 -0700922{
Eric W. Biederman06fc0d62009-11-20 16:08:54 -0800923 struct sysfs_dirent *sd;
Maneesh Sonibc062b12005-07-29 12:13:35 -0700924 struct iattr newattrs;
Tejun Heo51225032007-06-14 04:27:25 +0900925 int rc;
Kay Sievers31e5abe2005-04-18 21:57:32 -0700926
Eric W. Biederman06fc0d62009-11-20 16:08:54 -0800927 mutex_lock(&sysfs_mutex);
928
Tejun Heo51225032007-06-14 04:27:25 +0900929 rc = -ENOENT;
Tejun Heocfec0bc2013-09-11 22:29:09 -0400930 sd = sysfs_find_dirent(kobj->sd, attr->name, NULL);
Eric W. Biederman06fc0d62009-11-20 16:08:54 -0800931 if (!sd)
Tejun Heo51225032007-06-14 04:27:25 +0900932 goto out;
933
Eric W. Biederman06fc0d62009-11-20 16:08:54 -0800934 newattrs.ia_mode = (mode & S_IALLUGO) | (sd->s_mode & ~S_IALLUGO);
Eric W. Biederman4c6974f2009-11-07 23:27:02 -0800935 newattrs.ia_valid = ATTR_MODE;
Eric W. Biederman06fc0d62009-11-20 16:08:54 -0800936 rc = sysfs_sd_setattr(sd, &newattrs);
Tejun Heof88123e2007-09-20 16:05:10 +0900937
Tejun Heo51225032007-06-14 04:27:25 +0900938 out:
Eric W. Biederman06fc0d62009-11-20 16:08:54 -0800939 mutex_unlock(&sysfs_mutex);
Tejun Heo51225032007-06-14 04:27:25 +0900940 return rc;
Kay Sievers31e5abe2005-04-18 21:57:32 -0700941}
942EXPORT_SYMBOL_GPL(sysfs_chmod_file);
943
Kay Sievers31e5abe2005-04-18 21:57:32 -0700944/**
Tejun Heo58292cbe2013-09-11 22:29:04 -0400945 * sysfs_remove_file_ns - remove an object attribute with a custom ns tag
946 * @kobj: object we're acting for
947 * @attr: attribute descriptor
948 * @ns: namespace tag of the file to remove
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 *
Tejun Heo58292cbe2013-09-11 22:29:04 -0400950 * Hash the attribute name and namespace tag and kill the victim.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 */
Tejun Heo58292cbe2013-09-11 22:29:04 -0400952void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
953 const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
Tejun Heo58292cbe2013-09-11 22:29:04 -0400955 struct sysfs_dirent *dir_sd = kobj->sd;
Eric W. Biederman487505c2011-10-12 21:53:38 +0000956
Tejun Heocfec0bc2013-09-11 22:29:09 -0400957 sysfs_hash_and_remove(dir_sd, attr->name, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958}
Tejun Heo58292cbe2013-09-11 22:29:04 -0400959EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Greg Kroah-Hartman1b18dc22013-08-21 16:28:26 -0700961void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr)
Andi Kleen1c205ae2010-01-05 12:48:01 +0100962{
963 int i;
964 for (i = 0; ptr[i]; i++)
965 sysfs_remove_file(kobj, ptr[i]);
966}
Greg Kroah-Hartman1b866752013-08-21 16:17:47 -0700967EXPORT_SYMBOL_GPL(sysfs_remove_files);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Alan Sterndfa87c82007-02-20 15:02:44 -0500969/**
970 * sysfs_remove_file_from_group - remove an attribute file from a group.
971 * @kobj: object we're acting for.
972 * @attr: attribute descriptor.
973 * @group: group name.
974 */
975void sysfs_remove_file_from_group(struct kobject *kobj,
976 const struct attribute *attr, const char *group)
977{
Tejun Heo608e2662007-06-14 04:27:22 +0900978 struct sysfs_dirent *dir_sd;
Alan Sterndfa87c82007-02-20 15:02:44 -0500979
James Bottomley11f24fb2008-01-02 18:44:05 -0600980 if (group)
Tejun Heo388975c2013-09-11 23:19:13 -0400981 dir_sd = sysfs_get_dirent(kobj->sd, group);
James Bottomley11f24fb2008-01-02 18:44:05 -0600982 else
983 dir_sd = sysfs_get(kobj->sd);
Tejun Heo608e2662007-06-14 04:27:22 +0900984 if (dir_sd) {
Tejun Heocfec0bc2013-09-11 22:29:09 -0400985 sysfs_hash_and_remove(dir_sd, attr->name, NULL);
Tejun Heo608e2662007-06-14 04:27:22 +0900986 sysfs_put(dir_sd);
Alan Sterndfa87c82007-02-20 15:02:44 -0500987 }
988}
989EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
990
Tejun Heo3124eb12013-10-01 17:42:09 -0400991/**
992 * sysfs_create_bin_file - create binary file for object.
993 * @kobj: object.
994 * @attr: attribute descriptor.
995 */
996int sysfs_create_bin_file(struct kobject *kobj,
997 const struct bin_attribute *attr)
998{
999 BUG_ON(!kobj || !kobj->sd || !attr);
1000
1001 return sysfs_add_file(kobj->sd, &attr->attr, SYSFS_KOBJ_BIN_ATTR);
1002}
1003EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
1004
1005/**
1006 * sysfs_remove_bin_file - remove binary file for object.
1007 * @kobj: object.
1008 * @attr: attribute descriptor.
1009 */
1010void sysfs_remove_bin_file(struct kobject *kobj,
1011 const struct bin_attribute *attr)
1012{
1013 sysfs_hash_and_remove(kobj->sd, attr->attr.name, NULL);
1014}
1015EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);
1016
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001017struct sysfs_schedule_callback_struct {
Alex Chiang66942062009-03-13 12:07:36 -06001018 struct list_head workq_list;
1019 struct kobject *kobj;
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001020 void (*func)(void *);
1021 void *data;
Alan Stern523ded72007-04-26 00:12:04 -07001022 struct module *owner;
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001023 struct work_struct work;
1024};
1025
Alex Chiangd1102712009-03-25 15:11:36 -06001026static struct workqueue_struct *sysfs_workqueue;
Alex Chiang66942062009-03-13 12:07:36 -06001027static DEFINE_MUTEX(sysfs_workq_mutex);
1028static LIST_HEAD(sysfs_workq);
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001029static void sysfs_schedule_callback_work(struct work_struct *work)
1030{
1031 struct sysfs_schedule_callback_struct *ss = container_of(work,
1032 struct sysfs_schedule_callback_struct, work);
1033
1034 (ss->func)(ss->data);
1035 kobject_put(ss->kobj);
Alan Stern523ded72007-04-26 00:12:04 -07001036 module_put(ss->owner);
Alex Chiang66942062009-03-13 12:07:36 -06001037 mutex_lock(&sysfs_workq_mutex);
1038 list_del(&ss->workq_list);
1039 mutex_unlock(&sysfs_workq_mutex);
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001040 kfree(ss);
1041}
1042
1043/**
1044 * sysfs_schedule_callback - helper to schedule a callback for a kobject
1045 * @kobj: object we're acting for.
1046 * @func: callback function to invoke later.
1047 * @data: argument to pass to @func.
Alan Stern523ded72007-04-26 00:12:04 -07001048 * @owner: module owning the callback code
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001049 *
1050 * sysfs attribute methods must not unregister themselves or their parent
1051 * kobject (which would amount to the same thing). Attempts to do so will
1052 * deadlock, since unregistration is mutually exclusive with driver
1053 * callbacks.
1054 *
1055 * Instead methods can call this routine, which will attempt to allocate
1056 * and schedule a workqueue request to call back @func with @data as its
1057 * argument in the workqueue's process context. @kobj will be pinned
1058 * until @func returns.
1059 *
1060 * Returns 0 if the request was submitted, -ENOMEM if storage could not
Alex Chiang66942062009-03-13 12:07:36 -06001061 * be allocated, -ENODEV if a reference to @owner isn't available,
1062 * -EAGAIN if a callback has already been scheduled for @kobj.
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001063 */
1064int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
Alan Stern523ded72007-04-26 00:12:04 -07001065 void *data, struct module *owner)
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001066{
Alex Chiang66942062009-03-13 12:07:36 -06001067 struct sysfs_schedule_callback_struct *ss, *tmp;
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001068
Alan Stern523ded72007-04-26 00:12:04 -07001069 if (!try_module_get(owner))
1070 return -ENODEV;
Alex Chiang66942062009-03-13 12:07:36 -06001071
1072 mutex_lock(&sysfs_workq_mutex);
1073 list_for_each_entry_safe(ss, tmp, &sysfs_workq, workq_list)
1074 if (ss->kobj == kobj) {
Alex Chiangd1102712009-03-25 15:11:36 -06001075 module_put(owner);
Alex Chiang66942062009-03-13 12:07:36 -06001076 mutex_unlock(&sysfs_workq_mutex);
1077 return -EAGAIN;
1078 }
1079 mutex_unlock(&sysfs_workq_mutex);
1080
Alex Chiangd1102712009-03-25 15:11:36 -06001081 if (sysfs_workqueue == NULL) {
Andrew Morton086a3772009-05-07 12:36:53 -07001082 sysfs_workqueue = create_singlethread_workqueue("sysfsd");
Alex Chiangd1102712009-03-25 15:11:36 -06001083 if (sysfs_workqueue == NULL) {
1084 module_put(owner);
1085 return -ENOMEM;
1086 }
1087 }
1088
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001089 ss = kmalloc(sizeof(*ss), GFP_KERNEL);
Alan Stern523ded72007-04-26 00:12:04 -07001090 if (!ss) {
1091 module_put(owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001092 return -ENOMEM;
Alan Stern523ded72007-04-26 00:12:04 -07001093 }
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001094 kobject_get(kobj);
1095 ss->kobj = kobj;
1096 ss->func = func;
1097 ss->data = data;
Alan Stern523ded72007-04-26 00:12:04 -07001098 ss->owner = owner;
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001099 INIT_WORK(&ss->work, sysfs_schedule_callback_work);
Alex Chiang66942062009-03-13 12:07:36 -06001100 INIT_LIST_HEAD(&ss->workq_list);
1101 mutex_lock(&sysfs_workq_mutex);
1102 list_add_tail(&ss->workq_list, &sysfs_workq);
1103 mutex_unlock(&sysfs_workq_mutex);
Alex Chiangd1102712009-03-25 15:11:36 -06001104 queue_work(sysfs_workqueue, &ss->work);
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001105 return 0;
1106}
1107EXPORT_SYMBOL_GPL(sysfs_schedule_callback);