blob: 02797a134cf8dd50800287f6f9f1efdcef239a72 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Tejun Heo6d66f5c2007-09-20 17:31:38 +09002 * fs/sysfs/file.c - sysfs regular (text) file implementation
3 *
4 * Copyright (c) 2001-3 Patrick Mochel
5 * Copyright (c) 2007 SUSE Linux Products GmbH
6 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
7 *
8 * This file is released under the GPLv2.
9 *
10 * Please see Documentation/filesystems/sysfs.txt for more information.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
13#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/kobject.h>
Andrew Morton815d2d52008-03-04 15:09:07 -080015#include <linux/kallsyms.h>
Robert P. J. Dayc6f87732008-03-13 22:41:52 -040016#include <linux/slab.h>
Miklos Szeredi93265d12008-06-16 13:46:47 +020017#include <linux/fsnotify.h>
Christoph Hellwig5f45f1a2005-06-23 00:09:12 -070018#include <linux/namei.h>
NeilBrown4508a7a2006-03-20 17:53:53 +110019#include <linux/poll.h>
Oliver Neukum94bebf42006-12-20 10:52:44 +010020#include <linux/list.h>
Dave Young52e8c202007-07-26 11:03:54 +000021#include <linux/mutex.h>
Andrew Mortonae87221d2007-08-24 16:11:54 -070022#include <linux/limits.h>
Greg Kroah-Hartman060cc742013-08-21 16:34:59 -070023#include <linux/uaccess.h>
Tejun Heo13c589d2013-10-01 17:42:02 -040024#include <linux/seq_file.h>
Tejun Heo73d97142013-10-01 17:42:07 -040025#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#include "sysfs.h"
28
Tejun Heo85a4ffa2007-09-20 16:05:12 +090029/*
Tejun Heo58282d82013-10-01 17:41:59 -040030 * There's one sysfs_open_file for each open file and one sysfs_open_dirent
Tejun Heoc75ec762013-10-01 17:41:58 -040031 * for each sysfs_dirent with one or more open files.
Tejun Heo85a4ffa2007-09-20 16:05:12 +090032 *
Tejun Heoc75ec762013-10-01 17:41:58 -040033 * sysfs_dirent->s_attr.open points to sysfs_open_dirent. s_attr.open is
34 * protected by sysfs_open_dirent_lock.
35 *
Tejun Heo13c589d2013-10-01 17:42:02 -040036 * filp->private_data points to seq_file whose ->private points to
37 * sysfs_open_file. sysfs_open_files are chained at
Tejun Heo58282d82013-10-01 17:41:59 -040038 * sysfs_open_dirent->files, which is protected by sysfs_open_file_mutex.
Tejun Heo85a4ffa2007-09-20 16:05:12 +090039 */
Jiri Slabyd7b37882007-11-21 14:55:19 -080040static DEFINE_SPINLOCK(sysfs_open_dirent_lock);
Tejun Heoc75ec762013-10-01 17:41:58 -040041static DEFINE_MUTEX(sysfs_open_file_mutex);
Tejun Heo85a4ffa2007-09-20 16:05:12 +090042
43struct sysfs_open_dirent {
44 atomic_t refcnt;
Tejun Heoa4e8b912007-09-20 16:05:12 +090045 atomic_t event;
46 wait_queue_head_t poll;
Tejun Heo58282d82013-10-01 17:41:59 -040047 struct list_head files; /* goes through sysfs_open_file.list */
Tejun Heo85a4ffa2007-09-20 16:05:12 +090048};
49
Tejun Heo58282d82013-10-01 17:41:59 -040050struct sysfs_open_file {
Tejun Heobcafe4e2013-10-01 17:42:00 -040051 struct sysfs_dirent *sd;
52 struct file *file;
Dave Young52e8c202007-07-26 11:03:54 +000053 struct mutex mutex;
Tejun Heo73107cb2007-06-14 03:45:16 +090054 int event;
Tejun Heo85a4ffa2007-09-20 16:05:12 +090055 struct list_head list;
Tejun Heo73d97142013-10-01 17:42:07 -040056
57 bool mmapped;
58 const struct vm_operations_struct *vm_ops;
Tejun Heo73107cb2007-06-14 03:45:16 +090059};
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Tejun Heof9b9a622013-10-01 17:42:05 -040061static bool sysfs_is_bin(struct sysfs_dirent *sd)
62{
63 return sysfs_type(sd) == SYSFS_KOBJ_BIN_ATTR;
64}
65
Tejun Heo13c589d2013-10-01 17:42:02 -040066static struct sysfs_open_file *sysfs_of(struct file *file)
67{
68 return ((struct seq_file *)file->private_data)->private;
69}
70
Tejun Heo375b6112013-10-01 17:41:57 -040071/*
72 * Determine ktype->sysfs_ops for the given sysfs_dirent. This function
73 * must be called while holding an active reference.
74 */
75static const struct sysfs_ops *sysfs_file_ops(struct sysfs_dirent *sd)
76{
77 struct kobject *kobj = sd->s_parent->s_dir.kobj;
78
79 lockdep_assert_held(sd);
80 return kobj->ktype ? kobj->ktype->sysfs_ops : NULL;
81}
82
Tejun Heo13c589d2013-10-01 17:42:02 -040083/*
84 * Reads on sysfs are handled through seq_file, which takes care of hairy
85 * details like buffering and seeking. The following function pipes
86 * sysfs_ops->show() result through seq_file.
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 */
Tejun Heo13c589d2013-10-01 17:42:02 -040088static int sysfs_seq_show(struct seq_file *sf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Tejun Heo13c589d2013-10-01 17:42:02 -040090 struct sysfs_open_file *of = sf->private;
91 struct kobject *kobj = of->sd->s_parent->s_dir.kobj;
Tejun Heo375b6112013-10-01 17:41:57 -040092 const struct sysfs_ops *ops;
Tejun Heo13c589d2013-10-01 17:42:02 -040093 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 ssize_t count;
95
Tejun Heo13c589d2013-10-01 17:42:02 -040096 /* acquire buffer and ensure that it's >= PAGE_SIZE */
97 count = seq_get_buf(sf, &buf);
98 if (count < PAGE_SIZE) {
99 seq_commit(sf, -1);
100 return 0;
101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Tejun Heo13c589d2013-10-01 17:42:02 -0400103 /*
104 * Need @of->sd for attr and ops, its parent for kobj. @of->mutex
105 * nests outside active ref and is just to ensure that the ops
106 * aren't called concurrently for the same open file.
107 */
108 mutex_lock(&of->mutex);
109 if (!sysfs_get_active(of->sd)) {
110 mutex_unlock(&of->mutex);
Tejun Heo0ab66082007-06-14 03:45:16 +0900111 return -ENODEV;
Tejun Heo13c589d2013-10-01 17:42:02 -0400112 }
Tejun Heo0ab66082007-06-14 03:45:16 +0900113
Tejun Heo13c589d2013-10-01 17:42:02 -0400114 of->event = atomic_read(&of->sd->s_attr.open->event);
Tejun Heo375b6112013-10-01 17:41:57 -0400115
Tejun Heo13c589d2013-10-01 17:42:02 -0400116 /*
117 * Lookup @ops and invoke show(). Control may reach here via seq
118 * file lseek even if @ops->show() isn't implemented.
119 */
120 ops = sysfs_file_ops(of->sd);
121 if (ops->show)
122 count = ops->show(kobj, of->sd->s_attr.attr, buf);
123 else
124 count = 0;
Tejun Heo0ab66082007-06-14 03:45:16 +0900125
Tejun Heo13c589d2013-10-01 17:42:02 -0400126 sysfs_put_active(of->sd);
127 mutex_unlock(&of->mutex);
128
129 if (count < 0)
130 return count;
Tejun Heo0ab66082007-06-14 03:45:16 +0900131
Miao Xie8118a852007-11-21 14:55:19 -0800132 /*
133 * The code works fine with PAGE_SIZE return but it's likely to
134 * indicate truncated result or overflow in normal use cases.
135 */
Andrew Morton815d2d52008-03-04 15:09:07 -0800136 if (count >= (ssize_t)PAGE_SIZE) {
137 print_symbol("fill_read_buffer: %s returned bad count\n",
138 (unsigned long)ops->show);
139 /* Try to struggle along */
140 count = PAGE_SIZE - 1;
141 }
Tejun Heo13c589d2013-10-01 17:42:02 -0400142 seq_commit(sf, count);
143 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400146/*
147 * Read method for bin files. As reading a bin file can have side-effects,
148 * the exact offset and bytes specified in read(2) call should be passed to
149 * the read callback making it difficult to use seq_file. Implement
150 * simplistic custom buffering for bin files.
151 */
152static ssize_t sysfs_bin_read(struct file *file, char __user *userbuf,
153 size_t bytes, loff_t *off)
154{
155 struct sysfs_open_file *of = sysfs_of(file);
156 struct bin_attribute *battr = of->sd->s_bin_attr.bin_attr;
157 struct kobject *kobj = of->sd->s_parent->s_dir.kobj;
158 int size = file_inode(file)->i_size;
159 int count = min_t(size_t, bytes, PAGE_SIZE);
160 loff_t offs = *off;
161 char *buf;
162
163 if (!bytes)
164 return 0;
165
166 if (size) {
167 if (offs > size)
168 return 0;
169 if (offs + count > size)
170 count = size - offs;
171 }
172
173 buf = kmalloc(count, GFP_KERNEL);
174 if (!buf)
175 return -ENOMEM;
176
177 /* need of->sd for battr, its parent for kobj */
178 mutex_lock(&of->mutex);
179 if (!sysfs_get_active(of->sd)) {
180 count = -ENODEV;
181 mutex_unlock(&of->mutex);
182 goto out_free;
183 }
184
185 if (battr->read)
186 count = battr->read(file, kobj, battr, buf, offs, count);
187 else
188 count = -EIO;
189
190 sysfs_put_active(of->sd);
191 mutex_unlock(&of->mutex);
192
193 if (count < 0)
194 goto out_free;
195
196 if (copy_to_user(userbuf, buf, count)) {
197 count = -EFAULT;
198 goto out_free;
199 }
200
201 pr_debug("offs = %lld, *off = %lld, count = %d\n", offs, *off, count);
202
203 *off = offs + count;
204
205 out_free:
206 kfree(buf);
207 return count;
208}
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210/**
Tejun Heo8ef445f2013-10-01 17:42:01 -0400211 * flush_write_buffer - push buffer to kobject
212 * @of: open file
213 * @buf: data buffer for file
Tejun Heof9b9a622013-10-01 17:42:05 -0400214 * @off: file offset to write to
Tejun Heo8ef445f2013-10-01 17:42:01 -0400215 * @count: number of bytes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 *
Tejun Heo8ef445f2013-10-01 17:42:01 -0400217 * Get the correct pointers for the kobject and the attribute we're dealing
218 * with, then call the store() method for it with @buf.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 */
Tejun Heof9b9a622013-10-01 17:42:05 -0400220static int flush_write_buffer(struct sysfs_open_file *of, char *buf, loff_t off,
Tejun Heo8ef445f2013-10-01 17:42:01 -0400221 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
Tejun Heobcafe4e2013-10-01 17:42:00 -0400223 struct kobject *kobj = of->sd->s_parent->s_dir.kobj;
Tejun Heo8ef445f2013-10-01 17:42:01 -0400224 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Tejun Heo8ef445f2013-10-01 17:42:01 -0400226 /*
227 * Need @of->sd for attr and ops, its parent for kobj. @of->mutex
228 * nests outside active ref and is just to ensure that the ops
229 * aren't called concurrently for the same open file.
230 */
231 mutex_lock(&of->mutex);
232 if (!sysfs_get_active(of->sd)) {
233 mutex_unlock(&of->mutex);
Tejun Heo0ab66082007-06-14 03:45:16 +0900234 return -ENODEV;
Tejun Heo8ef445f2013-10-01 17:42:01 -0400235 }
Tejun Heo0ab66082007-06-14 03:45:16 +0900236
Tejun Heof9b9a622013-10-01 17:42:05 -0400237 if (sysfs_is_bin(of->sd)) {
238 struct bin_attribute *battr = of->sd->s_bin_attr.bin_attr;
239
240 rc = -EIO;
241 if (battr->write)
242 rc = battr->write(of->file, kobj, battr, buf, off,
243 count);
244 } else {
245 const struct sysfs_ops *ops = sysfs_file_ops(of->sd);
246
247 rc = ops->store(kobj, of->sd->s_attr.attr, buf, count);
248 }
Tejun Heo0ab66082007-06-14 03:45:16 +0900249
Tejun Heobcafe4e2013-10-01 17:42:00 -0400250 sysfs_put_active(of->sd);
Tejun Heo8ef445f2013-10-01 17:42:01 -0400251 mutex_unlock(&of->mutex);
Tejun Heo0ab66082007-06-14 03:45:16 +0900252
253 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256/**
Tejun Heo8ef445f2013-10-01 17:42:01 -0400257 * sysfs_write_file - write an attribute
258 * @file: file pointer
259 * @user_buf: data to write
260 * @count: number of bytes
261 * @ppos: starting offset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 *
Tejun Heo8ef445f2013-10-01 17:42:01 -0400263 * Copy data in from userland and pass it to the matching
264 * sysfs_ops->store() by invoking flush_write_buffer().
265 *
266 * There is no easy way for us to know if userspace is only doing a partial
267 * write, so we don't support them. We expect the entire buffer to come on
268 * the first write. Hint: if you're writing a value, first read the file,
269 * modify only the the value you're changing, then write entire buffer
270 * back.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 */
Tejun Heo8ef445f2013-10-01 17:42:01 -0400272static ssize_t sysfs_write_file(struct file *file, const char __user *user_buf,
Greg Kroah-Hartmanddfd6d02013-08-21 16:33:34 -0700273 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
Tejun Heo13c589d2013-10-01 17:42:02 -0400275 struct sysfs_open_file *of = sysfs_of(file);
Tejun Heof9b9a622013-10-01 17:42:05 -0400276 ssize_t len = min_t(size_t, count, PAGE_SIZE);
Tejun Heo8ef445f2013-10-01 17:42:01 -0400277 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Tejun Heof9b9a622013-10-01 17:42:05 -0400279 if (sysfs_is_bin(of->sd)) {
280 loff_t size = file_inode(file)->i_size;
281
282 if (size <= *ppos)
283 return 0;
284 len = min_t(ssize_t, len, size - *ppos);
285 }
286
Tejun Heo8ef445f2013-10-01 17:42:01 -0400287 if (!len)
288 return 0;
289
290 buf = kmalloc(len + 1, GFP_KERNEL);
291 if (!buf)
292 return -ENOMEM;
293
294 if (copy_from_user(buf, user_buf, len)) {
295 len = -EFAULT;
296 goto out_free;
297 }
298 buf[len] = '\0'; /* guarantee string termination */
299
Tejun Heof9b9a622013-10-01 17:42:05 -0400300 len = flush_write_buffer(of, buf, *ppos, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 if (len > 0)
302 *ppos += len;
Tejun Heo8ef445f2013-10-01 17:42:01 -0400303out_free:
304 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 return len;
306}
307
Tejun Heo73d97142013-10-01 17:42:07 -0400308static void sysfs_bin_vma_open(struct vm_area_struct *vma)
309{
310 struct file *file = vma->vm_file;
311 struct sysfs_open_file *of = sysfs_of(file);
312
313 if (!of->vm_ops)
314 return;
315
316 if (!sysfs_get_active(of->sd))
317 return;
318
319 if (of->vm_ops->open)
320 of->vm_ops->open(vma);
321
322 sysfs_put_active(of->sd);
323}
324
325static int sysfs_bin_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
326{
327 struct file *file = vma->vm_file;
328 struct sysfs_open_file *of = sysfs_of(file);
329 int ret;
330
331 if (!of->vm_ops)
332 return VM_FAULT_SIGBUS;
333
334 if (!sysfs_get_active(of->sd))
335 return VM_FAULT_SIGBUS;
336
337 ret = VM_FAULT_SIGBUS;
338 if (of->vm_ops->fault)
339 ret = of->vm_ops->fault(vma, vmf);
340
341 sysfs_put_active(of->sd);
342 return ret;
343}
344
345static int sysfs_bin_page_mkwrite(struct vm_area_struct *vma,
346 struct vm_fault *vmf)
347{
348 struct file *file = vma->vm_file;
349 struct sysfs_open_file *of = sysfs_of(file);
350 int ret;
351
352 if (!of->vm_ops)
353 return VM_FAULT_SIGBUS;
354
355 if (!sysfs_get_active(of->sd))
356 return VM_FAULT_SIGBUS;
357
358 ret = 0;
359 if (of->vm_ops->page_mkwrite)
360 ret = of->vm_ops->page_mkwrite(vma, vmf);
361 else
362 file_update_time(file);
363
364 sysfs_put_active(of->sd);
365 return ret;
366}
367
368static int sysfs_bin_access(struct vm_area_struct *vma, unsigned long addr,
369 void *buf, int len, int write)
370{
371 struct file *file = vma->vm_file;
372 struct sysfs_open_file *of = sysfs_of(file);
373 int ret;
374
375 if (!of->vm_ops)
376 return -EINVAL;
377
378 if (!sysfs_get_active(of->sd))
379 return -EINVAL;
380
381 ret = -EINVAL;
382 if (of->vm_ops->access)
383 ret = of->vm_ops->access(vma, addr, buf, len, write);
384
385 sysfs_put_active(of->sd);
386 return ret;
387}
388
389#ifdef CONFIG_NUMA
390static int sysfs_bin_set_policy(struct vm_area_struct *vma,
391 struct mempolicy *new)
392{
393 struct file *file = vma->vm_file;
394 struct sysfs_open_file *of = sysfs_of(file);
395 int ret;
396
397 if (!of->vm_ops)
398 return 0;
399
400 if (!sysfs_get_active(of->sd))
401 return -EINVAL;
402
403 ret = 0;
404 if (of->vm_ops->set_policy)
405 ret = of->vm_ops->set_policy(vma, new);
406
407 sysfs_put_active(of->sd);
408 return ret;
409}
410
411static struct mempolicy *sysfs_bin_get_policy(struct vm_area_struct *vma,
412 unsigned long addr)
413{
414 struct file *file = vma->vm_file;
415 struct sysfs_open_file *of = sysfs_of(file);
416 struct mempolicy *pol;
417
418 if (!of->vm_ops)
419 return vma->vm_policy;
420
421 if (!sysfs_get_active(of->sd))
422 return vma->vm_policy;
423
424 pol = vma->vm_policy;
425 if (of->vm_ops->get_policy)
426 pol = of->vm_ops->get_policy(vma, addr);
427
428 sysfs_put_active(of->sd);
429 return pol;
430}
431
432static int sysfs_bin_migrate(struct vm_area_struct *vma, const nodemask_t *from,
433 const nodemask_t *to, unsigned long flags)
434{
435 struct file *file = vma->vm_file;
436 struct sysfs_open_file *of = sysfs_of(file);
437 int ret;
438
439 if (!of->vm_ops)
440 return 0;
441
442 if (!sysfs_get_active(of->sd))
443 return 0;
444
445 ret = 0;
446 if (of->vm_ops->migrate)
447 ret = of->vm_ops->migrate(vma, from, to, flags);
448
449 sysfs_put_active(of->sd);
450 return ret;
451}
452#endif
453
454static const struct vm_operations_struct sysfs_bin_vm_ops = {
455 .open = sysfs_bin_vma_open,
456 .fault = sysfs_bin_fault,
457 .page_mkwrite = sysfs_bin_page_mkwrite,
458 .access = sysfs_bin_access,
459#ifdef CONFIG_NUMA
460 .set_policy = sysfs_bin_set_policy,
461 .get_policy = sysfs_bin_get_policy,
462 .migrate = sysfs_bin_migrate,
463#endif
464};
465
466static int sysfs_bin_mmap(struct file *file, struct vm_area_struct *vma)
467{
468 struct sysfs_open_file *of = sysfs_of(file);
469 struct bin_attribute *battr = of->sd->s_bin_attr.bin_attr;
470 struct kobject *kobj = of->sd->s_parent->s_dir.kobj;
471 int rc;
472
473 mutex_lock(&of->mutex);
474
475 /* need of->sd for battr, its parent for kobj */
476 rc = -ENODEV;
477 if (!sysfs_get_active(of->sd))
478 goto out_unlock;
479
480 rc = -EINVAL;
481 if (!battr->mmap)
482 goto out_put;
483
484 rc = battr->mmap(file, kobj, battr, vma);
485 if (rc)
486 goto out_put;
487
488 /*
489 * PowerPC's pci_mmap of legacy_mem uses shmem_zero_setup()
490 * to satisfy versions of X which crash if the mmap fails: that
491 * substitutes a new vm_file, and we don't then want bin_vm_ops.
492 */
493 if (vma->vm_file != file)
494 goto out_put;
495
496 rc = -EINVAL;
497 if (of->mmapped && of->vm_ops != vma->vm_ops)
498 goto out_put;
499
500 /*
501 * It is not possible to successfully wrap close.
502 * So error if someone is trying to use close.
503 */
504 rc = -EINVAL;
505 if (vma->vm_ops && vma->vm_ops->close)
506 goto out_put;
507
508 rc = 0;
509 of->mmapped = 1;
510 of->vm_ops = vma->vm_ops;
511 vma->vm_ops = &sysfs_bin_vm_ops;
512out_put:
513 sysfs_put_active(of->sd);
514out_unlock:
515 mutex_unlock(&of->mutex);
516
517 return rc;
518}
519
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900520/**
521 * sysfs_get_open_dirent - get or create sysfs_open_dirent
522 * @sd: target sysfs_dirent
Tejun Heo58282d82013-10-01 17:41:59 -0400523 * @of: sysfs_open_file for this instance of open
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900524 *
525 * If @sd->s_attr.open exists, increment its reference count;
Tejun Heo58282d82013-10-01 17:41:59 -0400526 * otherwise, create one. @of is chained to the files list.
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900527 *
528 * LOCKING:
529 * Kernel thread context (may sleep).
530 *
531 * RETURNS:
532 * 0 on success, -errno on failure.
533 */
534static int sysfs_get_open_dirent(struct sysfs_dirent *sd,
Tejun Heo58282d82013-10-01 17:41:59 -0400535 struct sysfs_open_file *of)
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900536{
537 struct sysfs_open_dirent *od, *new_od = NULL;
538
539 retry:
Tejun Heoc75ec762013-10-01 17:41:58 -0400540 mutex_lock(&sysfs_open_file_mutex);
Neil Brown83db93f2009-09-15 16:05:51 -0700541 spin_lock_irq(&sysfs_open_dirent_lock);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900542
543 if (!sd->s_attr.open && new_od) {
544 sd->s_attr.open = new_od;
545 new_od = NULL;
546 }
547
548 od = sd->s_attr.open;
549 if (od) {
550 atomic_inc(&od->refcnt);
Tejun Heo58282d82013-10-01 17:41:59 -0400551 list_add_tail(&of->list, &od->files);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900552 }
553
Neil Brown83db93f2009-09-15 16:05:51 -0700554 spin_unlock_irq(&sysfs_open_dirent_lock);
Tejun Heoc75ec762013-10-01 17:41:58 -0400555 mutex_unlock(&sysfs_open_file_mutex);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900556
557 if (od) {
558 kfree(new_od);
559 return 0;
560 }
561
562 /* not there, initialize a new one and retry */
563 new_od = kmalloc(sizeof(*new_od), GFP_KERNEL);
564 if (!new_od)
565 return -ENOMEM;
566
567 atomic_set(&new_od->refcnt, 0);
Tejun Heoa4e8b912007-09-20 16:05:12 +0900568 atomic_set(&new_od->event, 1);
569 init_waitqueue_head(&new_od->poll);
Tejun Heo58282d82013-10-01 17:41:59 -0400570 INIT_LIST_HEAD(&new_od->files);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900571 goto retry;
572}
573
574/**
575 * sysfs_put_open_dirent - put sysfs_open_dirent
576 * @sd: target sysfs_dirent
Tejun Heo58282d82013-10-01 17:41:59 -0400577 * @of: associated sysfs_open_file
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900578 *
Tejun Heo58282d82013-10-01 17:41:59 -0400579 * Put @sd->s_attr.open and unlink @of from the files list. If
580 * reference count reaches zero, disassociate and free it.
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900581 *
582 * LOCKING:
583 * None.
584 */
585static void sysfs_put_open_dirent(struct sysfs_dirent *sd,
Tejun Heo58282d82013-10-01 17:41:59 -0400586 struct sysfs_open_file *of)
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900587{
588 struct sysfs_open_dirent *od = sd->s_attr.open;
Neil Brown83db93f2009-09-15 16:05:51 -0700589 unsigned long flags;
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900590
Tejun Heoc75ec762013-10-01 17:41:58 -0400591 mutex_lock(&sysfs_open_file_mutex);
Neil Brown83db93f2009-09-15 16:05:51 -0700592 spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900593
Tejun Heo73d97142013-10-01 17:42:07 -0400594 if (of)
595 list_del(&of->list);
596
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900597 if (atomic_dec_and_test(&od->refcnt))
598 sd->s_attr.open = NULL;
599 else
600 od = NULL;
601
Neil Brown83db93f2009-09-15 16:05:51 -0700602 spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
Tejun Heoc75ec762013-10-01 17:41:58 -0400603 mutex_unlock(&sysfs_open_file_mutex);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900604
605 kfree(od);
606}
607
Oliver Neukum94bebf42006-12-20 10:52:44 +0100608static int sysfs_open_file(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Tejun Heo3e519032007-06-14 03:45:15 +0900610 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900611 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
Tejun Heo58282d82013-10-01 17:41:59 -0400612 struct sysfs_open_file *of;
Emese Revfy52cf25d2010-01-19 02:58:23 +0100613 const struct sysfs_ops *ops;
Kay Sievers000f2a42007-11-02 13:47:53 +0100614 int error = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Tejun Heo0ab66082007-06-14 03:45:16 +0900616 /* need attr_sd for attr and ops, its parent for kobj */
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800617 if (!sysfs_get_active(attr_sd))
Tejun Heo0ab66082007-06-14 03:45:16 +0900618 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Kay Sievers000f2a42007-11-02 13:47:53 +0100620 /* every kobject with an attribute needs a ktype assigned */
Tejun Heo375b6112013-10-01 17:41:57 -0400621 ops = sysfs_file_ops(attr_sd);
622 if (WARN(!ops, KERN_ERR
623 "missing sysfs attribute operations for kobject: %s\n",
624 kobject_name(kobj)))
Tejun Heo7b595752007-06-14 03:45:17 +0900625 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 /* File needs write support.
Greg Kroah-Hartmanab9bf4b2013-08-21 16:21:17 -0700628 * The inode's perms must say it's ok,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 * and we must have a store method.
630 */
631 if (file->f_mode & FMODE_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 if (!(inode->i_mode & S_IWUGO) || !ops->store)
Tejun Heo7b595752007-06-14 03:45:17 +0900633 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
635
636 /* File needs read support.
637 * The inode's perms must say it's ok, and we there
638 * must be a show method for it.
639 */
640 if (file->f_mode & FMODE_READ) {
641 if (!(inode->i_mode & S_IRUGO) || !ops->show)
Tejun Heo7b595752007-06-14 03:45:17 +0900642 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
644
Tejun Heo13c589d2013-10-01 17:42:02 -0400645 /* allocate a sysfs_open_file for the file */
Tejun Heo0ab66082007-06-14 03:45:16 +0900646 error = -ENOMEM;
Tejun Heo58282d82013-10-01 17:41:59 -0400647 of = kzalloc(sizeof(struct sysfs_open_file), GFP_KERNEL);
648 if (!of)
Tejun Heo7b595752007-06-14 03:45:17 +0900649 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Tejun Heo58282d82013-10-01 17:41:59 -0400651 mutex_init(&of->mutex);
Tejun Heobcafe4e2013-10-01 17:42:00 -0400652 of->sd = attr_sd;
653 of->file = file;
Tejun Heo13c589d2013-10-01 17:42:02 -0400654
655 /*
656 * Always instantiate seq_file even if read access is not
657 * implemented or requested. This unifies private data access and
658 * most files are readable anyway.
659 */
660 error = single_open(file, sysfs_seq_show, of);
661 if (error)
662 goto err_free;
663
664 /* seq_file clears PWRITE unconditionally, restore it if WRITE */
665 if (file->f_mode & FMODE_WRITE)
666 file->f_mode |= FMODE_PWRITE;
Tejun Heo0ab66082007-06-14 03:45:16 +0900667
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900668 /* make sure we have open dirent struct */
Tejun Heo58282d82013-10-01 17:41:59 -0400669 error = sysfs_get_open_dirent(attr_sd, of);
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900670 if (error)
Tejun Heo13c589d2013-10-01 17:42:02 -0400671 goto err_close;
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900672
Tejun Heob05f0542007-09-20 16:05:10 +0900673 /* open succeeded, put active references */
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800674 sysfs_put_active(attr_sd);
Tejun Heo0ab66082007-06-14 03:45:16 +0900675 return 0;
676
Tejun Heo13c589d2013-10-01 17:42:02 -0400677err_close:
678 single_release(inode, file);
679err_free:
Tejun Heo58282d82013-10-01 17:41:59 -0400680 kfree(of);
Tejun Heo13c589d2013-10-01 17:42:02 -0400681err_out:
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800682 sysfs_put_active(attr_sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return error;
684}
685
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900686static int sysfs_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
Tejun Heo85a4ffa2007-09-20 16:05:12 +0900688 struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata;
Tejun Heo13c589d2013-10-01 17:42:02 -0400689 struct sysfs_open_file *of = sysfs_of(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Tejun Heo58282d82013-10-01 17:41:59 -0400691 sysfs_put_open_dirent(sd, of);
Tejun Heo13c589d2013-10-01 17:42:02 -0400692 single_release(inode, filp);
Tejun Heo58282d82013-10-01 17:41:59 -0400693 kfree(of);
Tejun Heo50ab1a72007-09-20 16:05:10 +0900694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return 0;
696}
697
Tejun Heo73d97142013-10-01 17:42:07 -0400698void sysfs_unmap_bin_file(struct sysfs_dirent *sd)
699{
700 struct sysfs_open_dirent *od;
701 struct sysfs_open_file *of;
702
703 if (!sysfs_is_bin(sd))
704 return;
705
706 spin_lock_irq(&sysfs_open_dirent_lock);
707 od = sd->s_attr.open;
708 if (od)
709 atomic_inc(&od->refcnt);
710 spin_unlock_irq(&sysfs_open_dirent_lock);
711 if (!od)
712 return;
713
714 mutex_lock(&sysfs_open_file_mutex);
715 list_for_each_entry(of, &od->files, list) {
716 struct inode *inode = file_inode(of->file);
717 unmap_mapping_range(inode->i_mapping, 0, 0, 1);
718 }
719 mutex_unlock(&sysfs_open_file_mutex);
720
721 sysfs_put_open_dirent(sd, NULL);
722}
723
NeilBrown4508a7a2006-03-20 17:53:53 +1100724/* Sysfs attribute files are pollable. The idea is that you read
725 * the content and then you use 'poll' or 'select' to wait for
726 * the content to change. When the content changes (assuming the
727 * manager for the kobject supports notification), poll will
728 * return POLLERR|POLLPRI, and select will return the fd whether
729 * it is waiting for read, write, or exceptions.
730 * Once poll/select indicates that the value has changed, you
Dan Williams2424b5d2008-04-07 15:35:01 -0700731 * need to close and re-open the file, or seek to 0 and read again.
NeilBrown4508a7a2006-03-20 17:53:53 +1100732 * Reminder: this only works for attributes which actively support
733 * it, and it is not possible to test an attribute from userspace
Rolf Eike Beera93720e2007-08-10 13:51:07 -0700734 * to see if it supports poll (Neither 'poll' nor 'select' return
NeilBrown4508a7a2006-03-20 17:53:53 +1100735 * an appropriate error code). When in doubt, set a suitable timeout value.
736 */
737static unsigned int sysfs_poll(struct file *filp, poll_table *wait)
738{
Tejun Heo13c589d2013-10-01 17:42:02 -0400739 struct sysfs_open_file *of = sysfs_of(filp);
Tejun Heo0ab66082007-06-14 03:45:16 +0900740 struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata;
Tejun Heoa4e8b912007-09-20 16:05:12 +0900741 struct sysfs_open_dirent *od = attr_sd->s_attr.open;
Tejun Heo0ab66082007-06-14 03:45:16 +0900742
743 /* need parent for the kobj, grab both */
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800744 if (!sysfs_get_active(attr_sd))
Tejun Heo0ab66082007-06-14 03:45:16 +0900745 goto trigger;
NeilBrown4508a7a2006-03-20 17:53:53 +1100746
Tejun Heoa4e8b912007-09-20 16:05:12 +0900747 poll_wait(filp, &od->poll, wait);
NeilBrown4508a7a2006-03-20 17:53:53 +1100748
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800749 sysfs_put_active(attr_sd);
NeilBrown4508a7a2006-03-20 17:53:53 +1100750
Tejun Heo58282d82013-10-01 17:41:59 -0400751 if (of->event != atomic_read(&od->event))
Tejun Heo0ab66082007-06-14 03:45:16 +0900752 goto trigger;
753
KOSAKI Motohiro1af35572009-04-09 13:53:22 +0900754 return DEFAULT_POLLMASK;
Tejun Heo0ab66082007-06-14 03:45:16 +0900755
756 trigger:
KOSAKI Motohiro1af35572009-04-09 13:53:22 +0900757 return DEFAULT_POLLMASK|POLLERR|POLLPRI;
NeilBrown4508a7a2006-03-20 17:53:53 +1100758}
759
Neil Brownf1282c82008-07-16 08:58:04 +1000760void sysfs_notify_dirent(struct sysfs_dirent *sd)
761{
762 struct sysfs_open_dirent *od;
Neil Brown83db93f2009-09-15 16:05:51 -0700763 unsigned long flags;
Neil Brownf1282c82008-07-16 08:58:04 +1000764
Neil Brown83db93f2009-09-15 16:05:51 -0700765 spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
Neil Brownf1282c82008-07-16 08:58:04 +1000766
Nick Dyerfc60bb82013-06-07 15:45:13 +0100767 if (!WARN_ON(sysfs_type(sd) != SYSFS_KOBJ_ATTR)) {
768 od = sd->s_attr.open;
769 if (od) {
770 atomic_inc(&od->event);
771 wake_up_interruptible(&od->poll);
772 }
Neil Brownf1282c82008-07-16 08:58:04 +1000773 }
774
Neil Brown83db93f2009-09-15 16:05:51 -0700775 spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
Neil Brownf1282c82008-07-16 08:58:04 +1000776}
777EXPORT_SYMBOL_GPL(sysfs_notify_dirent);
778
Trent Piepho8c0e3992008-09-25 16:45:13 -0700779void sysfs_notify(struct kobject *k, const char *dir, const char *attr)
NeilBrown4508a7a2006-03-20 17:53:53 +1100780{
Tejun Heo51225032007-06-14 04:27:25 +0900781 struct sysfs_dirent *sd = k->sd;
NeilBrown4508a7a2006-03-20 17:53:53 +1100782
Tejun Heo51225032007-06-14 04:27:25 +0900783 mutex_lock(&sysfs_mutex);
NeilBrown4508a7a2006-03-20 17:53:53 +1100784
Tejun Heo51225032007-06-14 04:27:25 +0900785 if (sd && dir)
Tejun Heocfec0bc2013-09-11 22:29:09 -0400786 sd = sysfs_find_dirent(sd, dir, NULL);
Tejun Heo51225032007-06-14 04:27:25 +0900787 if (sd && attr)
Tejun Heocfec0bc2013-09-11 22:29:09 -0400788 sd = sysfs_find_dirent(sd, attr, NULL);
Neil Brownf1282c82008-07-16 08:58:04 +1000789 if (sd)
790 sysfs_notify_dirent(sd);
Tejun Heo51225032007-06-14 04:27:25 +0900791
792 mutex_unlock(&sysfs_mutex);
NeilBrown4508a7a2006-03-20 17:53:53 +1100793}
794EXPORT_SYMBOL_GPL(sysfs_notify);
795
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800796const struct file_operations sysfs_file_operations = {
Tejun Heo13c589d2013-10-01 17:42:02 -0400797 .read = seq_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 .write = sysfs_write_file,
Tejun Heo13c589d2013-10-01 17:42:02 -0400799 .llseek = seq_lseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 .open = sysfs_open_file,
801 .release = sysfs_release,
NeilBrown4508a7a2006-03-20 17:53:53 +1100802 .poll = sysfs_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803};
804
Tejun Heof9b9a622013-10-01 17:42:05 -0400805const struct file_operations sysfs_bin_operations = {
Tejun Heo2f0c6b72013-10-01 17:42:06 -0400806 .read = sysfs_bin_read,
Tejun Heof9b9a622013-10-01 17:42:05 -0400807 .write = sysfs_write_file,
808 .llseek = generic_file_llseek,
Tejun Heo73d97142013-10-01 17:42:07 -0400809 .mmap = sysfs_bin_mmap,
Tejun Heof9b9a622013-10-01 17:42:05 -0400810};
811
Tejun Heo58292cbe2013-09-11 22:29:04 -0400812int sysfs_add_file_mode_ns(struct sysfs_dirent *dir_sd,
813 const struct attribute *attr, int type,
814 umode_t amode, const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
James Bottomley0f423892008-03-20 20:47:52 -0500816 umode_t mode = (amode & S_IALLUGO) | S_IFREG;
Tejun Heofb6896d2007-06-14 04:27:24 +0900817 struct sysfs_addrm_cxt acxt;
Tejun Heoa26cd722007-06-14 03:45:14 +0900818 struct sysfs_dirent *sd;
Tejun Heo23dc2792007-08-02 21:38:03 +0900819 int rc;
Tejun Heoa26cd722007-06-14 03:45:14 +0900820
Tejun Heo3e519032007-06-14 03:45:15 +0900821 sd = sysfs_new_dirent(attr->name, mode, type);
Tejun Heo3007e992007-06-14 04:27:23 +0900822 if (!sd)
823 return -ENOMEM;
Eric W. Biederman487505c2011-10-12 21:53:38 +0000824
825 sd->s_ns = ns;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900826 sd->s_attr.attr = (void *)attr;
Eric W. Biedermana2db6842010-02-11 15:20:00 -0800827 sysfs_dirent_init_lockdep(sd);
Tejun Heoa26cd722007-06-14 03:45:14 +0900828
Tejun Heod69ac5a2013-09-18 17:15:35 -0400829 sysfs_addrm_start(&acxt);
830 rc = sysfs_add_one(&acxt, sd, dir_sd);
Tejun Heo23dc2792007-08-02 21:38:03 +0900831 sysfs_addrm_finish(&acxt);
Tejun Heo3007e992007-06-14 04:27:23 +0900832
Tejun Heo23dc2792007-08-02 21:38:03 +0900833 if (rc)
Tejun Heo967e35d2007-07-18 16:38:11 +0900834 sysfs_put(sd);
Tejun Heo3007e992007-06-14 04:27:23 +0900835
Tejun Heo23dc2792007-08-02 21:38:03 +0900836 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
838
839
James Bottomley0f423892008-03-20 20:47:52 -0500840int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,
841 int type)
842{
Tejun Heo58292cbe2013-09-11 22:29:04 -0400843 return sysfs_add_file_mode_ns(dir_sd, attr, type, attr->mode, NULL);
James Bottomley0f423892008-03-20 20:47:52 -0500844}
845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846/**
Tejun Heo58292cbe2013-09-11 22:29:04 -0400847 * sysfs_create_file_ns - create an attribute file for an object with custom ns
848 * @kobj: object we're creating for
849 * @attr: attribute descriptor
850 * @ns: namespace the new file should belong to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 */
Tejun Heo58292cbe2013-09-11 22:29:04 -0400852int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
853 const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
Tejun Heo608e2662007-06-14 04:27:22 +0900855 BUG_ON(!kobj || !kobj->sd || !attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Tejun Heo58292cbe2013-09-11 22:29:04 -0400857 return sysfs_add_file_mode_ns(kobj->sd, attr, SYSFS_KOBJ_ATTR,
858 attr->mode, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
860}
Tejun Heo58292cbe2013-09-11 22:29:04 -0400861EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Andi Kleen1c205ae2010-01-05 12:48:01 +0100863int sysfs_create_files(struct kobject *kobj, const struct attribute **ptr)
864{
865 int err = 0;
866 int i;
867
868 for (i = 0; ptr[i] && !err; i++)
869 err = sysfs_create_file(kobj, ptr[i]);
870 if (err)
871 while (--i >= 0)
872 sysfs_remove_file(kobj, ptr[i]);
873 return err;
874}
Greg Kroah-Hartman1b866752013-08-21 16:17:47 -0700875EXPORT_SYMBOL_GPL(sysfs_create_files);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877/**
Alan Sterndfa87c82007-02-20 15:02:44 -0500878 * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
879 * @kobj: object we're acting for.
880 * @attr: attribute descriptor.
881 * @group: group name.
882 */
883int sysfs_add_file_to_group(struct kobject *kobj,
884 const struct attribute *attr, const char *group)
885{
Tejun Heo608e2662007-06-14 04:27:22 +0900886 struct sysfs_dirent *dir_sd;
Alan Sterndfa87c82007-02-20 15:02:44 -0500887 int error;
888
James Bottomley11f24fb2008-01-02 18:44:05 -0600889 if (group)
Tejun Heo388975c2013-09-11 23:19:13 -0400890 dir_sd = sysfs_get_dirent(kobj->sd, group);
James Bottomley11f24fb2008-01-02 18:44:05 -0600891 else
892 dir_sd = sysfs_get(kobj->sd);
893
Tejun Heo608e2662007-06-14 04:27:22 +0900894 if (!dir_sd)
895 return -ENOENT;
896
897 error = sysfs_add_file(dir_sd, attr, SYSFS_KOBJ_ATTR);
898 sysfs_put(dir_sd);
899
Alan Sterndfa87c82007-02-20 15:02:44 -0500900 return error;
901}
902EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904/**
Kay Sievers31e5abe2005-04-18 21:57:32 -0700905 * sysfs_chmod_file - update the modified mode value on an object attribute.
906 * @kobj: object we're acting for.
907 * @attr: attribute descriptor.
908 * @mode: file permissions.
909 *
910 */
Jean Delvare49c19402010-07-02 16:54:05 +0200911int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
Al Viro48176a92011-07-24 03:40:40 -0400912 umode_t mode)
Kay Sievers31e5abe2005-04-18 21:57:32 -0700913{
Eric W. Biederman06fc0d62009-11-20 16:08:54 -0800914 struct sysfs_dirent *sd;
Maneesh Sonibc062b12005-07-29 12:13:35 -0700915 struct iattr newattrs;
Tejun Heo51225032007-06-14 04:27:25 +0900916 int rc;
Kay Sievers31e5abe2005-04-18 21:57:32 -0700917
Eric W. Biederman06fc0d62009-11-20 16:08:54 -0800918 mutex_lock(&sysfs_mutex);
919
Tejun Heo51225032007-06-14 04:27:25 +0900920 rc = -ENOENT;
Tejun Heocfec0bc2013-09-11 22:29:09 -0400921 sd = sysfs_find_dirent(kobj->sd, attr->name, NULL);
Eric W. Biederman06fc0d62009-11-20 16:08:54 -0800922 if (!sd)
Tejun Heo51225032007-06-14 04:27:25 +0900923 goto out;
924
Eric W. Biederman06fc0d62009-11-20 16:08:54 -0800925 newattrs.ia_mode = (mode & S_IALLUGO) | (sd->s_mode & ~S_IALLUGO);
Eric W. Biederman4c6974f2009-11-07 23:27:02 -0800926 newattrs.ia_valid = ATTR_MODE;
Eric W. Biederman06fc0d62009-11-20 16:08:54 -0800927 rc = sysfs_sd_setattr(sd, &newattrs);
Tejun Heof88123e2007-09-20 16:05:10 +0900928
Tejun Heo51225032007-06-14 04:27:25 +0900929 out:
Eric W. Biederman06fc0d62009-11-20 16:08:54 -0800930 mutex_unlock(&sysfs_mutex);
Tejun Heo51225032007-06-14 04:27:25 +0900931 return rc;
Kay Sievers31e5abe2005-04-18 21:57:32 -0700932}
933EXPORT_SYMBOL_GPL(sysfs_chmod_file);
934
Kay Sievers31e5abe2005-04-18 21:57:32 -0700935/**
Tejun Heo58292cbe2013-09-11 22:29:04 -0400936 * sysfs_remove_file_ns - remove an object attribute with a custom ns tag
937 * @kobj: object we're acting for
938 * @attr: attribute descriptor
939 * @ns: namespace tag of the file to remove
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 *
Tejun Heo58292cbe2013-09-11 22:29:04 -0400941 * Hash the attribute name and namespace tag and kill the victim.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 */
Tejun Heo58292cbe2013-09-11 22:29:04 -0400943void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
944 const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
Tejun Heo58292cbe2013-09-11 22:29:04 -0400946 struct sysfs_dirent *dir_sd = kobj->sd;
Eric W. Biederman487505c2011-10-12 21:53:38 +0000947
Tejun Heocfec0bc2013-09-11 22:29:09 -0400948 sysfs_hash_and_remove(dir_sd, attr->name, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949}
Tejun Heo58292cbe2013-09-11 22:29:04 -0400950EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Greg Kroah-Hartman1b18dc22013-08-21 16:28:26 -0700952void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr)
Andi Kleen1c205ae2010-01-05 12:48:01 +0100953{
954 int i;
955 for (i = 0; ptr[i]; i++)
956 sysfs_remove_file(kobj, ptr[i]);
957}
Greg Kroah-Hartman1b866752013-08-21 16:17:47 -0700958EXPORT_SYMBOL_GPL(sysfs_remove_files);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Alan Sterndfa87c82007-02-20 15:02:44 -0500960/**
961 * sysfs_remove_file_from_group - remove an attribute file from a group.
962 * @kobj: object we're acting for.
963 * @attr: attribute descriptor.
964 * @group: group name.
965 */
966void sysfs_remove_file_from_group(struct kobject *kobj,
967 const struct attribute *attr, const char *group)
968{
Tejun Heo608e2662007-06-14 04:27:22 +0900969 struct sysfs_dirent *dir_sd;
Alan Sterndfa87c82007-02-20 15:02:44 -0500970
James Bottomley11f24fb2008-01-02 18:44:05 -0600971 if (group)
Tejun Heo388975c2013-09-11 23:19:13 -0400972 dir_sd = sysfs_get_dirent(kobj->sd, group);
James Bottomley11f24fb2008-01-02 18:44:05 -0600973 else
974 dir_sd = sysfs_get(kobj->sd);
Tejun Heo608e2662007-06-14 04:27:22 +0900975 if (dir_sd) {
Tejun Heocfec0bc2013-09-11 22:29:09 -0400976 sysfs_hash_and_remove(dir_sd, attr->name, NULL);
Tejun Heo608e2662007-06-14 04:27:22 +0900977 sysfs_put(dir_sd);
Alan Sterndfa87c82007-02-20 15:02:44 -0500978 }
979}
980EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
981
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400982struct sysfs_schedule_callback_struct {
Alex Chiang66942062009-03-13 12:07:36 -0600983 struct list_head workq_list;
984 struct kobject *kobj;
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400985 void (*func)(void *);
986 void *data;
Alan Stern523ded72007-04-26 00:12:04 -0700987 struct module *owner;
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400988 struct work_struct work;
989};
990
Alex Chiangd1102712009-03-25 15:11:36 -0600991static struct workqueue_struct *sysfs_workqueue;
Alex Chiang66942062009-03-13 12:07:36 -0600992static DEFINE_MUTEX(sysfs_workq_mutex);
993static LIST_HEAD(sysfs_workq);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400994static void sysfs_schedule_callback_work(struct work_struct *work)
995{
996 struct sysfs_schedule_callback_struct *ss = container_of(work,
997 struct sysfs_schedule_callback_struct, work);
998
999 (ss->func)(ss->data);
1000 kobject_put(ss->kobj);
Alan Stern523ded72007-04-26 00:12:04 -07001001 module_put(ss->owner);
Alex Chiang66942062009-03-13 12:07:36 -06001002 mutex_lock(&sysfs_workq_mutex);
1003 list_del(&ss->workq_list);
1004 mutex_unlock(&sysfs_workq_mutex);
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001005 kfree(ss);
1006}
1007
1008/**
1009 * sysfs_schedule_callback - helper to schedule a callback for a kobject
1010 * @kobj: object we're acting for.
1011 * @func: callback function to invoke later.
1012 * @data: argument to pass to @func.
Alan Stern523ded72007-04-26 00:12:04 -07001013 * @owner: module owning the callback code
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001014 *
1015 * sysfs attribute methods must not unregister themselves or their parent
1016 * kobject (which would amount to the same thing). Attempts to do so will
1017 * deadlock, since unregistration is mutually exclusive with driver
1018 * callbacks.
1019 *
1020 * Instead methods can call this routine, which will attempt to allocate
1021 * and schedule a workqueue request to call back @func with @data as its
1022 * argument in the workqueue's process context. @kobj will be pinned
1023 * until @func returns.
1024 *
1025 * Returns 0 if the request was submitted, -ENOMEM if storage could not
Alex Chiang66942062009-03-13 12:07:36 -06001026 * be allocated, -ENODEV if a reference to @owner isn't available,
1027 * -EAGAIN if a callback has already been scheduled for @kobj.
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001028 */
1029int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
Alan Stern523ded72007-04-26 00:12:04 -07001030 void *data, struct module *owner)
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001031{
Alex Chiang66942062009-03-13 12:07:36 -06001032 struct sysfs_schedule_callback_struct *ss, *tmp;
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001033
Alan Stern523ded72007-04-26 00:12:04 -07001034 if (!try_module_get(owner))
1035 return -ENODEV;
Alex Chiang66942062009-03-13 12:07:36 -06001036
1037 mutex_lock(&sysfs_workq_mutex);
1038 list_for_each_entry_safe(ss, tmp, &sysfs_workq, workq_list)
1039 if (ss->kobj == kobj) {
Alex Chiangd1102712009-03-25 15:11:36 -06001040 module_put(owner);
Alex Chiang66942062009-03-13 12:07:36 -06001041 mutex_unlock(&sysfs_workq_mutex);
1042 return -EAGAIN;
1043 }
1044 mutex_unlock(&sysfs_workq_mutex);
1045
Alex Chiangd1102712009-03-25 15:11:36 -06001046 if (sysfs_workqueue == NULL) {
Andrew Morton086a3772009-05-07 12:36:53 -07001047 sysfs_workqueue = create_singlethread_workqueue("sysfsd");
Alex Chiangd1102712009-03-25 15:11:36 -06001048 if (sysfs_workqueue == NULL) {
1049 module_put(owner);
1050 return -ENOMEM;
1051 }
1052 }
1053
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001054 ss = kmalloc(sizeof(*ss), GFP_KERNEL);
Alan Stern523ded72007-04-26 00:12:04 -07001055 if (!ss) {
1056 module_put(owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001057 return -ENOMEM;
Alan Stern523ded72007-04-26 00:12:04 -07001058 }
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001059 kobject_get(kobj);
1060 ss->kobj = kobj;
1061 ss->func = func;
1062 ss->data = data;
Alan Stern523ded72007-04-26 00:12:04 -07001063 ss->owner = owner;
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001064 INIT_WORK(&ss->work, sysfs_schedule_callback_work);
Alex Chiang66942062009-03-13 12:07:36 -06001065 INIT_LIST_HEAD(&ss->workq_list);
1066 mutex_lock(&sysfs_workq_mutex);
1067 list_add_tail(&ss->workq_list, &sysfs_workq);
1068 mutex_unlock(&sysfs_workq_mutex);
Alex Chiangd1102712009-03-25 15:11:36 -06001069 queue_work(sysfs_workqueue, &ss->work);
Alan Sternd9a9cdf2007-03-15 15:50:34 -04001070 return 0;
1071}
1072EXPORT_SYMBOL_GPL(sysfs_schedule_callback);