]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
6d66f5cd TH |
2 | * 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. | |
1da177e4 LT |
11 | */ |
12 | ||
13 | #include <linux/module.h> | |
1da177e4 | 14 | #include <linux/kobject.h> |
815d2d50 | 15 | #include <linux/kallsyms.h> |
c6f87733 | 16 | #include <linux/slab.h> |
93265d13 | 17 | #include <linux/fsnotify.h> |
5f45f1a7 | 18 | #include <linux/namei.h> |
4508a7a7 | 19 | #include <linux/poll.h> |
94bebf4d | 20 | #include <linux/list.h> |
52e8c209 | 21 | #include <linux/mutex.h> |
ae87221d | 22 | #include <linux/limits.h> |
060cc749 | 23 | #include <linux/uaccess.h> |
13c589d5 | 24 | #include <linux/seq_file.h> |
73d97146 | 25 | #include <linux/mm.h> |
1da177e4 LT |
26 | |
27 | #include "sysfs.h" | |
28 | ||
85a4ffad | 29 | /* |
58282d8d | 30 | * There's one sysfs_open_file for each open file and one sysfs_open_dirent |
c75ec764 | 31 | * for each sysfs_dirent with one or more open files. |
85a4ffad | 32 | * |
c75ec764 TH |
33 | * sysfs_dirent->s_attr.open points to sysfs_open_dirent. s_attr.open is |
34 | * protected by sysfs_open_dirent_lock. | |
35 | * | |
13c589d5 TH |
36 | * filp->private_data points to seq_file whose ->private points to |
37 | * sysfs_open_file. sysfs_open_files are chained at | |
58282d8d | 38 | * sysfs_open_dirent->files, which is protected by sysfs_open_file_mutex. |
85a4ffad | 39 | */ |
d7b37889 | 40 | static DEFINE_SPINLOCK(sysfs_open_dirent_lock); |
c75ec764 | 41 | static DEFINE_MUTEX(sysfs_open_file_mutex); |
85a4ffad TH |
42 | |
43 | struct sysfs_open_dirent { | |
44 | atomic_t refcnt; | |
a4e8b912 TH |
45 | atomic_t event; |
46 | wait_queue_head_t poll; | |
58282d8d | 47 | struct list_head files; /* goes through sysfs_open_file.list */ |
85a4ffad TH |
48 | }; |
49 | ||
13c589d5 TH |
50 | static struct sysfs_open_file *sysfs_of(struct file *file) |
51 | { | |
52 | return ((struct seq_file *)file->private_data)->private; | |
53 | } | |
54 | ||
f6acf8bb TH |
55 | /* |
56 | * Determine the kernfs_ops for the given sysfs_dirent. This function must | |
57 | * be called while holding an active reference. | |
58 | */ | |
59 | static const struct kernfs_ops *kernfs_ops(struct sysfs_dirent *sd) | |
60 | { | |
61 | if (!sysfs_ignore_lockdep(sd)) | |
62 | lockdep_assert_held(sd); | |
63 | return sd->s_attr.ops; | |
64 | } | |
65 | ||
375b611e TH |
66 | /* |
67 | * Determine ktype->sysfs_ops for the given sysfs_dirent. This function | |
68 | * must be called while holding an active reference. | |
69 | */ | |
70 | static const struct sysfs_ops *sysfs_file_ops(struct sysfs_dirent *sd) | |
71 | { | |
7c6e2d36 | 72 | struct kobject *kobj = sd->s_parent->priv; |
375b611e | 73 | |
785a162d TH |
74 | if (!sysfs_ignore_lockdep(sd)) |
75 | lockdep_assert_held(sd); | |
375b611e TH |
76 | return kobj->ktype ? kobj->ktype->sysfs_ops : NULL; |
77 | } | |
78 | ||
13c589d5 TH |
79 | /* |
80 | * Reads on sysfs are handled through seq_file, which takes care of hairy | |
81 | * details like buffering and seeking. The following function pipes | |
82 | * sysfs_ops->show() result through seq_file. | |
1da177e4 | 83 | */ |
c2b19daf | 84 | static int sysfs_kf_seq_show(struct seq_file *sf, void *v) |
1da177e4 | 85 | { |
13c589d5 | 86 | struct sysfs_open_file *of = sf->private; |
7c6e2d36 | 87 | struct kobject *kobj = of->sd->s_parent->priv; |
c2b19daf | 88 | const struct sysfs_ops *ops = sysfs_file_ops(of->sd); |
1da177e4 | 89 | ssize_t count; |
c2b19daf | 90 | char *buf; |
1da177e4 | 91 | |
13c589d5 TH |
92 | /* acquire buffer and ensure that it's >= PAGE_SIZE */ |
93 | count = seq_get_buf(sf, &buf); | |
94 | if (count < PAGE_SIZE) { | |
95 | seq_commit(sf, -1); | |
96 | return 0; | |
97 | } | |
1da177e4 | 98 | |
13c589d5 | 99 | /* |
c2b19daf TH |
100 | * Invoke show(). Control may reach here via seq file lseek even |
101 | * if @ops->show() isn't implemented. | |
13c589d5 | 102 | */ |
c2b19daf | 103 | if (ops->show) { |
7c6e2d36 | 104 | count = ops->show(kobj, of->sd->priv, buf); |
c2b19daf TH |
105 | if (count < 0) |
106 | return count; | |
107 | } | |
0ab66088 | 108 | |
8118a859 MX |
109 | /* |
110 | * The code works fine with PAGE_SIZE return but it's likely to | |
111 | * indicate truncated result or overflow in normal use cases. | |
112 | */ | |
815d2d50 AM |
113 | if (count >= (ssize_t)PAGE_SIZE) { |
114 | print_symbol("fill_read_buffer: %s returned bad count\n", | |
115 | (unsigned long)ops->show); | |
116 | /* Try to struggle along */ | |
117 | count = PAGE_SIZE - 1; | |
118 | } | |
13c589d5 TH |
119 | seq_commit(sf, count); |
120 | return 0; | |
1da177e4 LT |
121 | } |
122 | ||
c2b19daf TH |
123 | static ssize_t sysfs_kf_bin_read(struct sysfs_open_file *of, char *buf, |
124 | size_t count, loff_t pos) | |
2f0c6b75 | 125 | { |
7c6e2d36 TH |
126 | struct bin_attribute *battr = of->sd->priv; |
127 | struct kobject *kobj = of->sd->s_parent->priv; | |
c2b19daf | 128 | loff_t size = file_inode(of->file)->i_size; |
2f0c6b75 | 129 | |
c2b19daf | 130 | if (!count) |
2f0c6b75 TH |
131 | return 0; |
132 | ||
133 | if (size) { | |
c2b19daf | 134 | if (pos > size) |
2f0c6b75 | 135 | return 0; |
c2b19daf TH |
136 | if (pos + count > size) |
137 | count = size - pos; | |
2f0c6b75 TH |
138 | } |
139 | ||
c2b19daf TH |
140 | if (!battr->read) |
141 | return -EIO; | |
142 | ||
143 | return battr->read(of->file, kobj, battr, buf, pos, count); | |
144 | } | |
145 | ||
146 | static void *kernfs_seq_start(struct seq_file *sf, loff_t *ppos) | |
147 | { | |
148 | struct sysfs_open_file *of = sf->private; | |
149 | ||
150 | /* | |
151 | * @of->mutex nests outside active ref and is just to ensure that | |
152 | * the ops aren't called concurrently for the same open file. | |
153 | */ | |
154 | mutex_lock(&of->mutex); | |
155 | if (!sysfs_get_active(of->sd)) | |
156 | return ERR_PTR(-ENODEV); | |
157 | ||
158 | /* | |
159 | * The same behavior and code as single_open(). Returns !NULL if | |
160 | * pos is at the beginning; otherwise, NULL. | |
161 | */ | |
162 | return NULL + !*ppos; | |
163 | } | |
164 | ||
165 | static void *kernfs_seq_next(struct seq_file *sf, void *v, loff_t *ppos) | |
166 | { | |
167 | /* | |
168 | * The same behavior and code as single_open(), always terminate | |
169 | * after the initial read. | |
170 | */ | |
171 | ++*ppos; | |
172 | return NULL; | |
173 | } | |
174 | ||
175 | static void kernfs_seq_stop(struct seq_file *sf, void *v) | |
176 | { | |
177 | struct sysfs_open_file *of = sf->private; | |
178 | ||
179 | sysfs_put_active(of->sd); | |
180 | mutex_unlock(&of->mutex); | |
181 | } | |
182 | ||
183 | static int kernfs_seq_show(struct seq_file *sf, void *v) | |
184 | { | |
185 | struct sysfs_open_file *of = sf->private; | |
186 | ||
187 | of->event = atomic_read(&of->sd->s_attr.open->event); | |
188 | ||
f6acf8bb | 189 | return of->sd->s_attr.ops->seq_show(sf, v); |
c2b19daf TH |
190 | } |
191 | ||
192 | static const struct seq_operations kernfs_seq_ops = { | |
193 | .start = kernfs_seq_start, | |
194 | .next = kernfs_seq_next, | |
195 | .stop = kernfs_seq_stop, | |
196 | .show = kernfs_seq_show, | |
197 | }; | |
198 | ||
199 | /* | |
200 | * As reading a bin file can have side-effects, the exact offset and bytes | |
201 | * specified in read(2) call should be passed to the read callback making | |
202 | * it difficult to use seq_file. Implement simplistic custom buffering for | |
203 | * bin files. | |
204 | */ | |
205 | static ssize_t kernfs_file_direct_read(struct sysfs_open_file *of, | |
206 | char __user *user_buf, size_t count, | |
207 | loff_t *ppos) | |
208 | { | |
209 | ssize_t len = min_t(size_t, count, PAGE_SIZE); | |
f6acf8bb | 210 | const struct kernfs_ops *ops; |
c2b19daf TH |
211 | char *buf; |
212 | ||
213 | buf = kmalloc(len, GFP_KERNEL); | |
2f0c6b75 TH |
214 | if (!buf) |
215 | return -ENOMEM; | |
216 | ||
c2b19daf TH |
217 | /* |
218 | * @of->mutex nests outside active ref and is just to ensure that | |
219 | * the ops aren't called concurrently for the same open file. | |
220 | */ | |
2f0c6b75 TH |
221 | mutex_lock(&of->mutex); |
222 | if (!sysfs_get_active(of->sd)) { | |
c2b19daf | 223 | len = -ENODEV; |
2f0c6b75 TH |
224 | mutex_unlock(&of->mutex); |
225 | goto out_free; | |
226 | } | |
227 | ||
f6acf8bb TH |
228 | ops = kernfs_ops(of->sd); |
229 | if (ops->read) | |
230 | len = ops->read(of, buf, len, *ppos); | |
231 | else | |
232 | len = -EINVAL; | |
2f0c6b75 TH |
233 | |
234 | sysfs_put_active(of->sd); | |
235 | mutex_unlock(&of->mutex); | |
236 | ||
c2b19daf | 237 | if (len < 0) |
2f0c6b75 TH |
238 | goto out_free; |
239 | ||
c2b19daf TH |
240 | if (copy_to_user(user_buf, buf, len)) { |
241 | len = -EFAULT; | |
2f0c6b75 TH |
242 | goto out_free; |
243 | } | |
244 | ||
c2b19daf | 245 | *ppos += len; |
2f0c6b75 TH |
246 | |
247 | out_free: | |
248 | kfree(buf); | |
c2b19daf TH |
249 | return len; |
250 | } | |
251 | ||
252 | /** | |
253 | * kernfs_file_read - kernfs vfs read callback | |
254 | * @file: file pointer | |
255 | * @user_buf: data to write | |
256 | * @count: number of bytes | |
257 | * @ppos: starting offset | |
258 | */ | |
259 | static ssize_t kernfs_file_read(struct file *file, char __user *user_buf, | |
260 | size_t count, loff_t *ppos) | |
261 | { | |
262 | struct sysfs_open_file *of = sysfs_of(file); | |
263 | ||
f6acf8bb | 264 | if (of->sd->s_flags & SYSFS_FLAG_HAS_SEQ_SHOW) |
c2b19daf | 265 | return seq_read(file, user_buf, count, ppos); |
f6acf8bb TH |
266 | else |
267 | return kernfs_file_direct_read(of, user_buf, count, ppos); | |
2f0c6b75 TH |
268 | } |
269 | ||
50b38ca0 TH |
270 | /* kernfs write callback for regular sysfs files */ |
271 | static ssize_t sysfs_kf_write(struct sysfs_open_file *of, char *buf, | |
272 | size_t count, loff_t pos) | |
1da177e4 | 273 | { |
50b38ca0 | 274 | const struct sysfs_ops *ops = sysfs_file_ops(of->sd); |
7c6e2d36 | 275 | struct kobject *kobj = of->sd->s_parent->priv; |
0ab66088 | 276 | |
50b38ca0 TH |
277 | if (!count) |
278 | return 0; | |
0ab66088 | 279 | |
50b38ca0 TH |
280 | return ops->store(kobj, of->sd->priv, buf, count); |
281 | } | |
f9b9a621 | 282 | |
50b38ca0 TH |
283 | /* kernfs write callback for bin sysfs files */ |
284 | static ssize_t sysfs_kf_bin_write(struct sysfs_open_file *of, char *buf, | |
285 | size_t count, loff_t pos) | |
286 | { | |
287 | struct bin_attribute *battr = of->sd->priv; | |
288 | struct kobject *kobj = of->sd->s_parent->priv; | |
289 | loff_t size = file_inode(of->file)->i_size; | |
f9b9a621 | 290 | |
50b38ca0 TH |
291 | if (size) { |
292 | if (size <= pos) | |
293 | return 0; | |
294 | count = min_t(ssize_t, count, size - pos); | |
f9b9a621 | 295 | } |
50b38ca0 TH |
296 | if (!count) |
297 | return 0; | |
0ab66088 | 298 | |
50b38ca0 TH |
299 | if (!battr->write) |
300 | return -EIO; | |
1da177e4 | 301 | |
50b38ca0 | 302 | return battr->write(of->file, kobj, battr, buf, pos, count); |
1da177e4 LT |
303 | } |
304 | ||
1da177e4 | 305 | /** |
50b38ca0 | 306 | * kernfs_file_write - kernfs vfs write callback |
8ef445f0 TH |
307 | * @file: file pointer |
308 | * @user_buf: data to write | |
309 | * @count: number of bytes | |
310 | * @ppos: starting offset | |
311 | * | |
50b38ca0 TH |
312 | * Copy data in from userland and pass it to the matching kernfs write |
313 | * operation. | |
1da177e4 | 314 | * |
8ef445f0 TH |
315 | * There is no easy way for us to know if userspace is only doing a partial |
316 | * write, so we don't support them. We expect the entire buffer to come on | |
317 | * the first write. Hint: if you're writing a value, first read the file, | |
318 | * modify only the the value you're changing, then write entire buffer | |
319 | * back. | |
1da177e4 | 320 | */ |
50b38ca0 TH |
321 | static ssize_t kernfs_file_write(struct file *file, const char __user *user_buf, |
322 | size_t count, loff_t *ppos) | |
1da177e4 | 323 | { |
13c589d5 | 324 | struct sysfs_open_file *of = sysfs_of(file); |
f9b9a621 | 325 | ssize_t len = min_t(size_t, count, PAGE_SIZE); |
f6acf8bb | 326 | const struct kernfs_ops *ops; |
8ef445f0 | 327 | char *buf; |
1da177e4 | 328 | |
8ef445f0 TH |
329 | buf = kmalloc(len + 1, GFP_KERNEL); |
330 | if (!buf) | |
331 | return -ENOMEM; | |
332 | ||
333 | if (copy_from_user(buf, user_buf, len)) { | |
334 | len = -EFAULT; | |
335 | goto out_free; | |
336 | } | |
337 | buf[len] = '\0'; /* guarantee string termination */ | |
338 | ||
50b38ca0 TH |
339 | /* |
340 | * @of->mutex nests outside active ref and is just to ensure that | |
341 | * the ops aren't called concurrently for the same open file. | |
342 | */ | |
343 | mutex_lock(&of->mutex); | |
344 | if (!sysfs_get_active(of->sd)) { | |
345 | mutex_unlock(&of->mutex); | |
346 | len = -ENODEV; | |
347 | goto out_free; | |
348 | } | |
349 | ||
f6acf8bb TH |
350 | ops = kernfs_ops(of->sd); |
351 | if (ops->write) | |
352 | len = ops->write(of, buf, len, *ppos); | |
50b38ca0 | 353 | else |
f6acf8bb | 354 | len = -EINVAL; |
50b38ca0 TH |
355 | |
356 | sysfs_put_active(of->sd); | |
357 | mutex_unlock(&of->mutex); | |
358 | ||
1da177e4 LT |
359 | if (len > 0) |
360 | *ppos += len; | |
8ef445f0 TH |
361 | out_free: |
362 | kfree(buf); | |
1da177e4 LT |
363 | return len; |
364 | } | |
365 | ||
fdbffaa4 TH |
366 | static int sysfs_kf_bin_mmap(struct sysfs_open_file *of, |
367 | struct vm_area_struct *vma) | |
368 | { | |
369 | struct bin_attribute *battr = of->sd->priv; | |
370 | struct kobject *kobj = of->sd->s_parent->priv; | |
371 | ||
372 | if (!battr->mmap) | |
373 | return -ENODEV; | |
374 | ||
375 | return battr->mmap(of->file, kobj, battr, vma); | |
376 | } | |
377 | ||
378 | static void kernfs_vma_open(struct vm_area_struct *vma) | |
73d97146 TH |
379 | { |
380 | struct file *file = vma->vm_file; | |
381 | struct sysfs_open_file *of = sysfs_of(file); | |
382 | ||
383 | if (!of->vm_ops) | |
384 | return; | |
385 | ||
386 | if (!sysfs_get_active(of->sd)) | |
387 | return; | |
388 | ||
389 | if (of->vm_ops->open) | |
390 | of->vm_ops->open(vma); | |
391 | ||
392 | sysfs_put_active(of->sd); | |
393 | } | |
394 | ||
fdbffaa4 | 395 | static int kernfs_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
73d97146 TH |
396 | { |
397 | struct file *file = vma->vm_file; | |
398 | struct sysfs_open_file *of = sysfs_of(file); | |
399 | int ret; | |
400 | ||
401 | if (!of->vm_ops) | |
402 | return VM_FAULT_SIGBUS; | |
403 | ||
404 | if (!sysfs_get_active(of->sd)) | |
405 | return VM_FAULT_SIGBUS; | |
406 | ||
407 | ret = VM_FAULT_SIGBUS; | |
408 | if (of->vm_ops->fault) | |
409 | ret = of->vm_ops->fault(vma, vmf); | |
410 | ||
411 | sysfs_put_active(of->sd); | |
412 | return ret; | |
413 | } | |
414 | ||
fdbffaa4 TH |
415 | static int kernfs_vma_page_mkwrite(struct vm_area_struct *vma, |
416 | struct vm_fault *vmf) | |
73d97146 TH |
417 | { |
418 | struct file *file = vma->vm_file; | |
419 | struct sysfs_open_file *of = sysfs_of(file); | |
420 | int ret; | |
421 | ||
422 | if (!of->vm_ops) | |
423 | return VM_FAULT_SIGBUS; | |
424 | ||
425 | if (!sysfs_get_active(of->sd)) | |
426 | return VM_FAULT_SIGBUS; | |
427 | ||
428 | ret = 0; | |
429 | if (of->vm_ops->page_mkwrite) | |
430 | ret = of->vm_ops->page_mkwrite(vma, vmf); | |
431 | else | |
432 | file_update_time(file); | |
433 | ||
434 | sysfs_put_active(of->sd); | |
435 | return ret; | |
436 | } | |
437 | ||
fdbffaa4 TH |
438 | static int kernfs_vma_access(struct vm_area_struct *vma, unsigned long addr, |
439 | void *buf, int len, int write) | |
73d97146 TH |
440 | { |
441 | struct file *file = vma->vm_file; | |
442 | struct sysfs_open_file *of = sysfs_of(file); | |
443 | int ret; | |
444 | ||
445 | if (!of->vm_ops) | |
446 | return -EINVAL; | |
447 | ||
448 | if (!sysfs_get_active(of->sd)) | |
449 | return -EINVAL; | |
450 | ||
451 | ret = -EINVAL; | |
452 | if (of->vm_ops->access) | |
453 | ret = of->vm_ops->access(vma, addr, buf, len, write); | |
454 | ||
455 | sysfs_put_active(of->sd); | |
456 | return ret; | |
457 | } | |
458 | ||
459 | #ifdef CONFIG_NUMA | |
fdbffaa4 TH |
460 | static int kernfs_vma_set_policy(struct vm_area_struct *vma, |
461 | struct mempolicy *new) | |
73d97146 TH |
462 | { |
463 | struct file *file = vma->vm_file; | |
464 | struct sysfs_open_file *of = sysfs_of(file); | |
465 | int ret; | |
466 | ||
467 | if (!of->vm_ops) | |
468 | return 0; | |
469 | ||
470 | if (!sysfs_get_active(of->sd)) | |
471 | return -EINVAL; | |
472 | ||
473 | ret = 0; | |
474 | if (of->vm_ops->set_policy) | |
475 | ret = of->vm_ops->set_policy(vma, new); | |
476 | ||
477 | sysfs_put_active(of->sd); | |
478 | return ret; | |
479 | } | |
480 | ||
fdbffaa4 TH |
481 | static struct mempolicy *kernfs_vma_get_policy(struct vm_area_struct *vma, |
482 | unsigned long addr) | |
73d97146 TH |
483 | { |
484 | struct file *file = vma->vm_file; | |
485 | struct sysfs_open_file *of = sysfs_of(file); | |
486 | struct mempolicy *pol; | |
487 | ||
488 | if (!of->vm_ops) | |
489 | return vma->vm_policy; | |
490 | ||
491 | if (!sysfs_get_active(of->sd)) | |
492 | return vma->vm_policy; | |
493 | ||
494 | pol = vma->vm_policy; | |
495 | if (of->vm_ops->get_policy) | |
496 | pol = of->vm_ops->get_policy(vma, addr); | |
497 | ||
498 | sysfs_put_active(of->sd); | |
499 | return pol; | |
500 | } | |
501 | ||
fdbffaa4 TH |
502 | static int kernfs_vma_migrate(struct vm_area_struct *vma, |
503 | const nodemask_t *from, const nodemask_t *to, | |
504 | unsigned long flags) | |
73d97146 TH |
505 | { |
506 | struct file *file = vma->vm_file; | |
507 | struct sysfs_open_file *of = sysfs_of(file); | |
508 | int ret; | |
509 | ||
510 | if (!of->vm_ops) | |
511 | return 0; | |
512 | ||
513 | if (!sysfs_get_active(of->sd)) | |
514 | return 0; | |
515 | ||
516 | ret = 0; | |
517 | if (of->vm_ops->migrate) | |
518 | ret = of->vm_ops->migrate(vma, from, to, flags); | |
519 | ||
520 | sysfs_put_active(of->sd); | |
521 | return ret; | |
522 | } | |
523 | #endif | |
524 | ||
fdbffaa4 TH |
525 | static const struct vm_operations_struct kernfs_vm_ops = { |
526 | .open = kernfs_vma_open, | |
527 | .fault = kernfs_vma_fault, | |
528 | .page_mkwrite = kernfs_vma_page_mkwrite, | |
529 | .access = kernfs_vma_access, | |
73d97146 | 530 | #ifdef CONFIG_NUMA |
fdbffaa4 TH |
531 | .set_policy = kernfs_vma_set_policy, |
532 | .get_policy = kernfs_vma_get_policy, | |
533 | .migrate = kernfs_vma_migrate, | |
73d97146 TH |
534 | #endif |
535 | }; | |
536 | ||
fdbffaa4 | 537 | static int kernfs_file_mmap(struct file *file, struct vm_area_struct *vma) |
73d97146 TH |
538 | { |
539 | struct sysfs_open_file *of = sysfs_of(file); | |
f6acf8bb | 540 | const struct kernfs_ops *ops; |
73d97146 TH |
541 | int rc; |
542 | ||
543 | mutex_lock(&of->mutex); | |
544 | ||
73d97146 TH |
545 | rc = -ENODEV; |
546 | if (!sysfs_get_active(of->sd)) | |
547 | goto out_unlock; | |
548 | ||
f6acf8bb TH |
549 | ops = kernfs_ops(of->sd); |
550 | if (ops->mmap) | |
551 | rc = ops->mmap(of, vma); | |
73d97146 TH |
552 | if (rc) |
553 | goto out_put; | |
554 | ||
555 | /* | |
556 | * PowerPC's pci_mmap of legacy_mem uses shmem_zero_setup() | |
557 | * to satisfy versions of X which crash if the mmap fails: that | |
558 | * substitutes a new vm_file, and we don't then want bin_vm_ops. | |
559 | */ | |
560 | if (vma->vm_file != file) | |
561 | goto out_put; | |
562 | ||
563 | rc = -EINVAL; | |
564 | if (of->mmapped && of->vm_ops != vma->vm_ops) | |
565 | goto out_put; | |
566 | ||
567 | /* | |
568 | * It is not possible to successfully wrap close. | |
569 | * So error if someone is trying to use close. | |
570 | */ | |
571 | rc = -EINVAL; | |
572 | if (vma->vm_ops && vma->vm_ops->close) | |
573 | goto out_put; | |
574 | ||
575 | rc = 0; | |
576 | of->mmapped = 1; | |
577 | of->vm_ops = vma->vm_ops; | |
fdbffaa4 | 578 | vma->vm_ops = &kernfs_vm_ops; |
73d97146 TH |
579 | out_put: |
580 | sysfs_put_active(of->sd); | |
581 | out_unlock: | |
582 | mutex_unlock(&of->mutex); | |
583 | ||
584 | return rc; | |
585 | } | |
586 | ||
85a4ffad TH |
587 | /** |
588 | * sysfs_get_open_dirent - get or create sysfs_open_dirent | |
589 | * @sd: target sysfs_dirent | |
58282d8d | 590 | * @of: sysfs_open_file for this instance of open |
85a4ffad TH |
591 | * |
592 | * If @sd->s_attr.open exists, increment its reference count; | |
58282d8d | 593 | * otherwise, create one. @of is chained to the files list. |
85a4ffad TH |
594 | * |
595 | * LOCKING: | |
596 | * Kernel thread context (may sleep). | |
597 | * | |
598 | * RETURNS: | |
599 | * 0 on success, -errno on failure. | |
600 | */ | |
601 | static int sysfs_get_open_dirent(struct sysfs_dirent *sd, | |
58282d8d | 602 | struct sysfs_open_file *of) |
85a4ffad TH |
603 | { |
604 | struct sysfs_open_dirent *od, *new_od = NULL; | |
605 | ||
606 | retry: | |
c75ec764 | 607 | mutex_lock(&sysfs_open_file_mutex); |
83db93f4 | 608 | spin_lock_irq(&sysfs_open_dirent_lock); |
85a4ffad TH |
609 | |
610 | if (!sd->s_attr.open && new_od) { | |
611 | sd->s_attr.open = new_od; | |
612 | new_od = NULL; | |
613 | } | |
614 | ||
615 | od = sd->s_attr.open; | |
616 | if (od) { | |
617 | atomic_inc(&od->refcnt); | |
58282d8d | 618 | list_add_tail(&of->list, &od->files); |
85a4ffad TH |
619 | } |
620 | ||
83db93f4 | 621 | spin_unlock_irq(&sysfs_open_dirent_lock); |
c75ec764 | 622 | mutex_unlock(&sysfs_open_file_mutex); |
85a4ffad TH |
623 | |
624 | if (od) { | |
625 | kfree(new_od); | |
626 | return 0; | |
627 | } | |
628 | ||
629 | /* not there, initialize a new one and retry */ | |
630 | new_od = kmalloc(sizeof(*new_od), GFP_KERNEL); | |
631 | if (!new_od) | |
632 | return -ENOMEM; | |
633 | ||
634 | atomic_set(&new_od->refcnt, 0); | |
a4e8b912 TH |
635 | atomic_set(&new_od->event, 1); |
636 | init_waitqueue_head(&new_od->poll); | |
58282d8d | 637 | INIT_LIST_HEAD(&new_od->files); |
85a4ffad TH |
638 | goto retry; |
639 | } | |
640 | ||
641 | /** | |
642 | * sysfs_put_open_dirent - put sysfs_open_dirent | |
643 | * @sd: target sysfs_dirent | |
58282d8d | 644 | * @of: associated sysfs_open_file |
85a4ffad | 645 | * |
58282d8d TH |
646 | * Put @sd->s_attr.open and unlink @of from the files list. If |
647 | * reference count reaches zero, disassociate and free it. | |
85a4ffad TH |
648 | * |
649 | * LOCKING: | |
650 | * None. | |
651 | */ | |
652 | static void sysfs_put_open_dirent(struct sysfs_dirent *sd, | |
58282d8d | 653 | struct sysfs_open_file *of) |
85a4ffad TH |
654 | { |
655 | struct sysfs_open_dirent *od = sd->s_attr.open; | |
83db93f4 | 656 | unsigned long flags; |
85a4ffad | 657 | |
c75ec764 | 658 | mutex_lock(&sysfs_open_file_mutex); |
83db93f4 | 659 | spin_lock_irqsave(&sysfs_open_dirent_lock, flags); |
85a4ffad | 660 | |
73d97146 TH |
661 | if (of) |
662 | list_del(&of->list); | |
663 | ||
85a4ffad TH |
664 | if (atomic_dec_and_test(&od->refcnt)) |
665 | sd->s_attr.open = NULL; | |
666 | else | |
667 | od = NULL; | |
668 | ||
83db93f4 | 669 | spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags); |
c75ec764 | 670 | mutex_unlock(&sysfs_open_file_mutex); |
85a4ffad TH |
671 | |
672 | kfree(od); | |
673 | } | |
674 | ||
c6fb4495 | 675 | static int kernfs_file_open(struct inode *inode, struct file *file) |
1da177e4 | 676 | { |
3e519038 | 677 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; |
f6acf8bb | 678 | const struct kernfs_ops *ops; |
58282d8d | 679 | struct sysfs_open_file *of; |
027a485d | 680 | bool has_read, has_write, has_mmap; |
000f2a4d | 681 | int error = -EACCES; |
1da177e4 | 682 | |
e72ceb8c | 683 | if (!sysfs_get_active(attr_sd)) |
0ab66088 | 684 | return -ENODEV; |
1da177e4 | 685 | |
f6acf8bb | 686 | ops = kernfs_ops(attr_sd); |
1da177e4 | 687 | |
f6acf8bb TH |
688 | has_read = ops->seq_show || ops->read || ops->mmap; |
689 | has_write = ops->write || ops->mmap; | |
690 | has_mmap = ops->mmap; | |
1da177e4 | 691 | |
49fe6047 TH |
692 | /* check perms and supported operations */ |
693 | if ((file->f_mode & FMODE_WRITE) && | |
694 | (!(inode->i_mode & S_IWUGO) || !has_write)) | |
695 | goto err_out; | |
696 | ||
697 | if ((file->f_mode & FMODE_READ) && | |
698 | (!(inode->i_mode & S_IRUGO) || !has_read)) | |
699 | goto err_out; | |
700 | ||
13c589d5 | 701 | /* allocate a sysfs_open_file for the file */ |
0ab66088 | 702 | error = -ENOMEM; |
58282d8d TH |
703 | of = kzalloc(sizeof(struct sysfs_open_file), GFP_KERNEL); |
704 | if (!of) | |
7b595756 | 705 | goto err_out; |
1da177e4 | 706 | |
027a485d TH |
707 | /* |
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 | ||
bcafe4ee TH |
724 | of->sd = attr_sd; |
725 | of->file = file; | |
13c589d5 TH |
726 | |
727 | /* | |
49fe6047 TH |
728 | * 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. | |
13c589d5 | 731 | */ |
f6acf8bb | 732 | if (ops->seq_show) |
c2b19daf | 733 | error = seq_open(file, &kernfs_seq_ops); |
f6acf8bb TH |
734 | else |
735 | error = seq_open(file, NULL); | |
13c589d5 TH |
736 | if (error) |
737 | goto err_free; | |
738 | ||
c2b19daf TH |
739 | ((struct seq_file *)file->private_data)->private = of; |
740 | ||
13c589d5 TH |
741 | /* seq_file clears PWRITE unconditionally, restore it if WRITE */ |
742 | if (file->f_mode & FMODE_WRITE) | |
743 | file->f_mode |= FMODE_PWRITE; | |
0ab66088 | 744 | |
85a4ffad | 745 | /* make sure we have open dirent struct */ |
58282d8d | 746 | error = sysfs_get_open_dirent(attr_sd, of); |
85a4ffad | 747 | if (error) |
13c589d5 | 748 | goto err_close; |
85a4ffad | 749 | |
b05f0548 | 750 | /* open succeeded, put active references */ |
e72ceb8c | 751 | sysfs_put_active(attr_sd); |
0ab66088 TH |
752 | return 0; |
753 | ||
13c589d5 | 754 | err_close: |
c2b19daf | 755 | seq_release(inode, file); |
13c589d5 | 756 | err_free: |
58282d8d | 757 | kfree(of); |
13c589d5 | 758 | err_out: |
e72ceb8c | 759 | sysfs_put_active(attr_sd); |
1da177e4 LT |
760 | return error; |
761 | } | |
762 | ||
c6fb4495 | 763 | static int kernfs_file_release(struct inode *inode, struct file *filp) |
1da177e4 | 764 | { |
85a4ffad | 765 | struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata; |
13c589d5 | 766 | struct sysfs_open_file *of = sysfs_of(filp); |
1da177e4 | 767 | |
58282d8d | 768 | sysfs_put_open_dirent(sd, of); |
c2b19daf | 769 | seq_release(inode, filp); |
58282d8d | 770 | kfree(of); |
50ab1a72 | 771 | |
1da177e4 LT |
772 | return 0; |
773 | } | |
774 | ||
73d97146 TH |
775 | void sysfs_unmap_bin_file(struct sysfs_dirent *sd) |
776 | { | |
777 | struct sysfs_open_dirent *od; | |
778 | struct sysfs_open_file *of; | |
779 | ||
f6acf8bb | 780 | if (!(sd->s_flags & SYSFS_FLAG_HAS_MMAP)) |
73d97146 TH |
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 | ||
4508a7a7 N |
801 | /* 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 | |
2424b5dd | 808 | * need to close and re-open the file, or seek to 0 and read again. |
4508a7a7 N |
809 | * Reminder: this only works for attributes which actively support |
810 | * it, and it is not possible to test an attribute from userspace | |
a93720ee | 811 | * to see if it supports poll (Neither 'poll' nor 'select' return |
4508a7a7 N |
812 | * an appropriate error code). When in doubt, set a suitable timeout value. |
813 | */ | |
c6fb4495 | 814 | static unsigned int kernfs_file_poll(struct file *filp, poll_table *wait) |
4508a7a7 | 815 | { |
13c589d5 | 816 | struct sysfs_open_file *of = sysfs_of(filp); |
0ab66088 | 817 | struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata; |
a4e8b912 | 818 | struct sysfs_open_dirent *od = attr_sd->s_attr.open; |
0ab66088 TH |
819 | |
820 | /* need parent for the kobj, grab both */ | |
e72ceb8c | 821 | if (!sysfs_get_active(attr_sd)) |
0ab66088 | 822 | goto trigger; |
4508a7a7 | 823 | |
a4e8b912 | 824 | poll_wait(filp, &od->poll, wait); |
4508a7a7 | 825 | |
e72ceb8c | 826 | sysfs_put_active(attr_sd); |
0ab66088 | 827 | |
58282d8d | 828 | if (of->event != atomic_read(&od->event)) |
0ab66088 | 829 | goto trigger; |
4508a7a7 | 830 | |
1af3557a | 831 | return DEFAULT_POLLMASK; |
0ab66088 TH |
832 | |
833 | trigger: | |
1af3557a | 834 | return DEFAULT_POLLMASK|POLLERR|POLLPRI; |
4508a7a7 N |
835 | } |
836 | ||
f1282c84 NB |
837 | void sysfs_notify_dirent(struct sysfs_dirent *sd) |
838 | { | |
839 | struct sysfs_open_dirent *od; | |
83db93f4 | 840 | unsigned long flags; |
f1282c84 | 841 | |
83db93f4 | 842 | spin_lock_irqsave(&sysfs_open_dirent_lock, flags); |
f1282c84 | 843 | |
fc60bb83 ND |
844 | 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 | } | |
f1282c84 NB |
850 | } |
851 | ||
83db93f4 | 852 | spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags); |
f1282c84 NB |
853 | } |
854 | EXPORT_SYMBOL_GPL(sysfs_notify_dirent); | |
855 | ||
8c0e3998 | 856 | void sysfs_notify(struct kobject *k, const char *dir, const char *attr) |
4508a7a7 | 857 | { |
51225039 | 858 | struct sysfs_dirent *sd = k->sd; |
4508a7a7 | 859 | |
51225039 TH |
860 | mutex_lock(&sysfs_mutex); |
861 | ||
862 | if (sd && dir) | |
cfec0bc8 | 863 | sd = sysfs_find_dirent(sd, dir, NULL); |
51225039 | 864 | if (sd && attr) |
cfec0bc8 | 865 | sd = sysfs_find_dirent(sd, attr, NULL); |
f1282c84 NB |
866 | if (sd) |
867 | sysfs_notify_dirent(sd); | |
51225039 TH |
868 | |
869 | mutex_unlock(&sysfs_mutex); | |
4508a7a7 N |
870 | } |
871 | EXPORT_SYMBOL_GPL(sysfs_notify); | |
872 | ||
c6fb4495 | 873 | const struct file_operations kernfs_file_operations = { |
c2b19daf | 874 | .read = kernfs_file_read, |
50b38ca0 | 875 | .write = kernfs_file_write, |
044e3bc3 | 876 | .llseek = generic_file_llseek, |
fdbffaa4 | 877 | .mmap = kernfs_file_mmap, |
c6fb4495 TH |
878 | .open = kernfs_file_open, |
879 | .release = kernfs_file_release, | |
880 | .poll = kernfs_file_poll, | |
f9b9a621 TH |
881 | }; |
882 | ||
f6acf8bb TH |
883 | static const struct kernfs_ops sysfs_file_kfops_empty = { |
884 | }; | |
885 | ||
886 | static const struct kernfs_ops sysfs_file_kfops_ro = { | |
887 | .seq_show = sysfs_kf_seq_show, | |
888 | }; | |
889 | ||
890 | static const struct kernfs_ops sysfs_file_kfops_wo = { | |
891 | .write = sysfs_kf_write, | |
892 | }; | |
893 | ||
894 | static const struct kernfs_ops sysfs_file_kfops_rw = { | |
895 | .seq_show = sysfs_kf_seq_show, | |
896 | .write = sysfs_kf_write, | |
897 | }; | |
898 | ||
899 | static const struct kernfs_ops sysfs_bin_kfops_ro = { | |
900 | .read = sysfs_kf_bin_read, | |
901 | }; | |
902 | ||
903 | static const struct kernfs_ops sysfs_bin_kfops_wo = { | |
904 | .write = sysfs_kf_bin_write, | |
905 | }; | |
906 | ||
907 | static const struct kernfs_ops sysfs_bin_kfops_rw = { | |
908 | .read = sysfs_kf_bin_read, | |
909 | .write = sysfs_kf_bin_write, | |
910 | .mmap = sysfs_kf_bin_mmap, | |
911 | }; | |
912 | ||
58292cbe | 913 | int sysfs_add_file_mode_ns(struct sysfs_dirent *dir_sd, |
a7dc66df | 914 | const struct attribute *attr, bool is_bin, |
58292cbe | 915 | umode_t amode, const void *ns) |
1da177e4 | 916 | { |
0f423895 | 917 | umode_t mode = (amode & S_IALLUGO) | S_IFREG; |
f6acf8bb | 918 | const struct kernfs_ops *ops; |
fb6896da | 919 | struct sysfs_addrm_cxt acxt; |
a26cd722 | 920 | struct sysfs_dirent *sd; |
471bd7b7 | 921 | loff_t size; |
23dc2799 | 922 | int rc; |
1da177e4 | 923 | |
a7dc66df | 924 | if (!is_bin) { |
f6acf8bb TH |
925 | struct kobject *kobj = dir_sd->priv; |
926 | const struct sysfs_ops *sysfs_ops = kobj->ktype->sysfs_ops; | |
927 | ||
928 | /* every kobject with an attribute needs a ktype assigned */ | |
929 | if (WARN(!sysfs_ops, KERN_ERR | |
930 | "missing sysfs attribute operations for kobject: %s\n", | |
931 | kobject_name(kobj))) | |
932 | return -EINVAL; | |
933 | ||
934 | if (sysfs_ops->show && sysfs_ops->store) | |
935 | ops = &sysfs_file_kfops_rw; | |
936 | else if (sysfs_ops->show) | |
937 | ops = &sysfs_file_kfops_ro; | |
938 | else if (sysfs_ops->store) | |
939 | ops = &sysfs_file_kfops_wo; | |
940 | else | |
941 | ops = &sysfs_file_kfops_empty; | |
471bd7b7 TH |
942 | |
943 | size = PAGE_SIZE; | |
f6acf8bb TH |
944 | } else { |
945 | struct bin_attribute *battr = (void *)attr; | |
946 | ||
947 | if ((battr->read && battr->write) || battr->mmap) | |
948 | ops = &sysfs_bin_kfops_rw; | |
949 | else if (battr->read) | |
950 | ops = &sysfs_bin_kfops_ro; | |
951 | else if (battr->write) | |
952 | ops = &sysfs_bin_kfops_wo; | |
953 | else | |
954 | ops = &sysfs_file_kfops_empty; | |
471bd7b7 TH |
955 | |
956 | size = battr->size; | |
f6acf8bb TH |
957 | } |
958 | ||
a7dc66df | 959 | sd = sysfs_new_dirent(attr->name, mode, SYSFS_KOBJ_ATTR); |
3007e997 TH |
960 | if (!sd) |
961 | return -ENOMEM; | |
487505c2 | 962 | |
f6acf8bb | 963 | sd->s_attr.ops = ops; |
471bd7b7 | 964 | sd->s_attr.size = size; |
487505c2 | 965 | sd->s_ns = ns; |
7c6e2d36 | 966 | sd->priv = (void *)attr; |
a2db6842 | 967 | sysfs_dirent_init_lockdep(sd); |
1da177e4 | 968 | |
f6acf8bb TH |
969 | /* |
970 | * sd->s_attr.ops is accesible only while holding active ref. We | |
971 | * need to know whether some ops are implemented outside active | |
972 | * ref. Cache their existence in flags. | |
973 | */ | |
974 | if (ops->seq_show) | |
975 | sd->s_flags |= SYSFS_FLAG_HAS_SEQ_SHOW; | |
976 | if (ops->mmap) | |
977 | sd->s_flags |= SYSFS_FLAG_HAS_MMAP; | |
978 | ||
d69ac5a0 TH |
979 | sysfs_addrm_start(&acxt); |
980 | rc = sysfs_add_one(&acxt, sd, dir_sd); | |
23dc2799 | 981 | sysfs_addrm_finish(&acxt); |
a26cd722 | 982 | |
23dc2799 | 983 | if (rc) |
967e35dc | 984 | sysfs_put(sd); |
3007e997 | 985 | |
23dc2799 | 986 | return rc; |
1da177e4 LT |
987 | } |
988 | ||
0f423895 | 989 | int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr, |
a7dc66df | 990 | bool is_bin) |
0f423895 | 991 | { |
a7dc66df | 992 | return sysfs_add_file_mode_ns(dir_sd, attr, is_bin, attr->mode, NULL); |
0f423895 JB |
993 | } |
994 | ||
1da177e4 | 995 | /** |
58292cbe TH |
996 | * sysfs_create_file_ns - create an attribute file for an object with custom ns |
997 | * @kobj: object we're creating for | |
998 | * @attr: attribute descriptor | |
999 | * @ns: namespace the new file should belong to | |
1da177e4 | 1000 | */ |
58292cbe TH |
1001 | int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr, |
1002 | const void *ns) | |
1da177e4 | 1003 | { |
608e266a | 1004 | BUG_ON(!kobj || !kobj->sd || !attr); |
1da177e4 | 1005 | |
a7dc66df | 1006 | return sysfs_add_file_mode_ns(kobj->sd, attr, false, attr->mode, ns); |
1da177e4 LT |
1007 | |
1008 | } | |
58292cbe | 1009 | EXPORT_SYMBOL_GPL(sysfs_create_file_ns); |
1da177e4 | 1010 | |
1c205ae1 AK |
1011 | int sysfs_create_files(struct kobject *kobj, const struct attribute **ptr) |
1012 | { | |
1013 | int err = 0; | |
1014 | int i; | |
1015 | ||
1016 | for (i = 0; ptr[i] && !err; i++) | |
1017 | err = sysfs_create_file(kobj, ptr[i]); | |
1018 | if (err) | |
1019 | while (--i >= 0) | |
1020 | sysfs_remove_file(kobj, ptr[i]); | |
1021 | return err; | |
1022 | } | |
1b866757 | 1023 | EXPORT_SYMBOL_GPL(sysfs_create_files); |
1da177e4 | 1024 | |
dfa87c82 AS |
1025 | /** |
1026 | * sysfs_add_file_to_group - add an attribute file to a pre-existing group. | |
1027 | * @kobj: object we're acting for. | |
1028 | * @attr: attribute descriptor. | |
1029 | * @group: group name. | |
1030 | */ | |
1031 | int sysfs_add_file_to_group(struct kobject *kobj, | |
1032 | const struct attribute *attr, const char *group) | |
1033 | { | |
608e266a | 1034 | struct sysfs_dirent *dir_sd; |
dfa87c82 AS |
1035 | int error; |
1036 | ||
11f24fbd | 1037 | if (group) |
388975cc | 1038 | dir_sd = sysfs_get_dirent(kobj->sd, group); |
11f24fbd JB |
1039 | else |
1040 | dir_sd = sysfs_get(kobj->sd); | |
1041 | ||
608e266a TH |
1042 | if (!dir_sd) |
1043 | return -ENOENT; | |
1044 | ||
a7dc66df | 1045 | error = sysfs_add_file(dir_sd, attr, false); |
608e266a TH |
1046 | sysfs_put(dir_sd); |
1047 | ||
dfa87c82 AS |
1048 | return error; |
1049 | } | |
1050 | EXPORT_SYMBOL_GPL(sysfs_add_file_to_group); | |
1051 | ||
31e5abe9 KS |
1052 | /** |
1053 | * sysfs_chmod_file - update the modified mode value on an object attribute. | |
1054 | * @kobj: object we're acting for. | |
1055 | * @attr: attribute descriptor. | |
1056 | * @mode: file permissions. | |
1057 | * | |
1058 | */ | |
49c19400 | 1059 | int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr, |
48176a97 | 1060 | umode_t mode) |
31e5abe9 | 1061 | { |
06fc0d66 | 1062 | struct sysfs_dirent *sd; |
bc062b1b | 1063 | struct iattr newattrs; |
51225039 TH |
1064 | int rc; |
1065 | ||
5d60418e | 1066 | sd = sysfs_get_dirent(kobj->sd, attr->name); |
06fc0d66 | 1067 | if (!sd) |
5d60418e | 1068 | return -ENOENT; |
f88123ea | 1069 | |
06fc0d66 | 1070 | newattrs.ia_mode = (mode & S_IALLUGO) | (sd->s_mode & ~S_IALLUGO); |
4c6974f5 | 1071 | newattrs.ia_valid = ATTR_MODE; |
f88123ea | 1072 | |
5d60418e TH |
1073 | rc = kernfs_setattr(sd, &newattrs); |
1074 | ||
1075 | sysfs_put(sd); | |
51225039 | 1076 | return rc; |
31e5abe9 KS |
1077 | } |
1078 | EXPORT_SYMBOL_GPL(sysfs_chmod_file); | |
1079 | ||
1da177e4 | 1080 | /** |
58292cbe TH |
1081 | * sysfs_remove_file_ns - remove an object attribute with a custom ns tag |
1082 | * @kobj: object we're acting for | |
1083 | * @attr: attribute descriptor | |
1084 | * @ns: namespace tag of the file to remove | |
1da177e4 | 1085 | * |
58292cbe | 1086 | * Hash the attribute name and namespace tag and kill the victim. |
1da177e4 | 1087 | */ |
58292cbe TH |
1088 | void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr, |
1089 | const void *ns) | |
1da177e4 | 1090 | { |
58292cbe | 1091 | struct sysfs_dirent *dir_sd = kobj->sd; |
487505c2 | 1092 | |
879f40d1 | 1093 | kernfs_remove_by_name_ns(dir_sd, attr->name, ns); |
1da177e4 | 1094 | } |
58292cbe | 1095 | EXPORT_SYMBOL_GPL(sysfs_remove_file_ns); |
1da177e4 | 1096 | |
1b18dc2b | 1097 | void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr) |
1c205ae1 AK |
1098 | { |
1099 | int i; | |
1100 | for (i = 0; ptr[i]; i++) | |
1101 | sysfs_remove_file(kobj, ptr[i]); | |
1102 | } | |
1b866757 | 1103 | EXPORT_SYMBOL_GPL(sysfs_remove_files); |
1da177e4 | 1104 | |
dfa87c82 AS |
1105 | /** |
1106 | * sysfs_remove_file_from_group - remove an attribute file from a group. | |
1107 | * @kobj: object we're acting for. | |
1108 | * @attr: attribute descriptor. | |
1109 | * @group: group name. | |
1110 | */ | |
1111 | void sysfs_remove_file_from_group(struct kobject *kobj, | |
1112 | const struct attribute *attr, const char *group) | |
1113 | { | |
608e266a | 1114 | struct sysfs_dirent *dir_sd; |
dfa87c82 | 1115 | |
11f24fbd | 1116 | if (group) |
388975cc | 1117 | dir_sd = sysfs_get_dirent(kobj->sd, group); |
11f24fbd JB |
1118 | else |
1119 | dir_sd = sysfs_get(kobj->sd); | |
608e266a | 1120 | if (dir_sd) { |
879f40d1 | 1121 | kernfs_remove_by_name(dir_sd, attr->name); |
608e266a | 1122 | sysfs_put(dir_sd); |
dfa87c82 AS |
1123 | } |
1124 | } | |
1125 | EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group); | |
1126 | ||
3124eb16 TH |
1127 | /** |
1128 | * sysfs_create_bin_file - create binary file for object. | |
1129 | * @kobj: object. | |
1130 | * @attr: attribute descriptor. | |
1131 | */ | |
1132 | int sysfs_create_bin_file(struct kobject *kobj, | |
1133 | const struct bin_attribute *attr) | |
1134 | { | |
1135 | BUG_ON(!kobj || !kobj->sd || !attr); | |
1136 | ||
a7dc66df | 1137 | return sysfs_add_file(kobj->sd, &attr->attr, true); |
3124eb16 TH |
1138 | } |
1139 | EXPORT_SYMBOL_GPL(sysfs_create_bin_file); | |
1140 | ||
1141 | /** | |
1142 | * sysfs_remove_bin_file - remove binary file for object. | |
1143 | * @kobj: object. | |
1144 | * @attr: attribute descriptor. | |
1145 | */ | |
1146 | void sysfs_remove_bin_file(struct kobject *kobj, | |
1147 | const struct bin_attribute *attr) | |
1148 | { | |
879f40d1 | 1149 | kernfs_remove_by_name(kobj->sd, attr->attr.name); |
3124eb16 TH |
1150 | } |
1151 | EXPORT_SYMBOL_GPL(sysfs_remove_bin_file); | |
1152 | ||
d9a9cdfb | 1153 | struct sysfs_schedule_callback_struct { |
66942064 AC |
1154 | struct list_head workq_list; |
1155 | struct kobject *kobj; | |
d9a9cdfb AS |
1156 | void (*func)(void *); |
1157 | void *data; | |
523ded71 | 1158 | struct module *owner; |
d9a9cdfb AS |
1159 | struct work_struct work; |
1160 | }; | |
1161 | ||
d110271e | 1162 | static struct workqueue_struct *sysfs_workqueue; |
66942064 AC |
1163 | static DEFINE_MUTEX(sysfs_workq_mutex); |
1164 | static LIST_HEAD(sysfs_workq); | |
d9a9cdfb AS |
1165 | static void sysfs_schedule_callback_work(struct work_struct *work) |
1166 | { | |
1167 | struct sysfs_schedule_callback_struct *ss = container_of(work, | |
1168 | struct sysfs_schedule_callback_struct, work); | |
1169 | ||
1170 | (ss->func)(ss->data); | |
1171 | kobject_put(ss->kobj); | |
523ded71 | 1172 | module_put(ss->owner); |
66942064 AC |
1173 | mutex_lock(&sysfs_workq_mutex); |
1174 | list_del(&ss->workq_list); | |
1175 | mutex_unlock(&sysfs_workq_mutex); | |
d9a9cdfb AS |
1176 | kfree(ss); |
1177 | } | |
1178 | ||
1179 | /** | |
1180 | * sysfs_schedule_callback - helper to schedule a callback for a kobject | |
1181 | * @kobj: object we're acting for. | |
1182 | * @func: callback function to invoke later. | |
1183 | * @data: argument to pass to @func. | |
523ded71 | 1184 | * @owner: module owning the callback code |
d9a9cdfb AS |
1185 | * |
1186 | * sysfs attribute methods must not unregister themselves or their parent | |
1187 | * kobject (which would amount to the same thing). Attempts to do so will | |
1188 | * deadlock, since unregistration is mutually exclusive with driver | |
1189 | * callbacks. | |
1190 | * | |
1191 | * Instead methods can call this routine, which will attempt to allocate | |
1192 | * and schedule a workqueue request to call back @func with @data as its | |
1193 | * argument in the workqueue's process context. @kobj will be pinned | |
1194 | * until @func returns. | |
1195 | * | |
1196 | * Returns 0 if the request was submitted, -ENOMEM if storage could not | |
66942064 AC |
1197 | * be allocated, -ENODEV if a reference to @owner isn't available, |
1198 | * -EAGAIN if a callback has already been scheduled for @kobj. | |
d9a9cdfb AS |
1199 | */ |
1200 | int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *), | |
523ded71 | 1201 | void *data, struct module *owner) |
d9a9cdfb | 1202 | { |
66942064 | 1203 | struct sysfs_schedule_callback_struct *ss, *tmp; |
d9a9cdfb | 1204 | |
523ded71 AS |
1205 | if (!try_module_get(owner)) |
1206 | return -ENODEV; | |
66942064 AC |
1207 | |
1208 | mutex_lock(&sysfs_workq_mutex); | |
1209 | list_for_each_entry_safe(ss, tmp, &sysfs_workq, workq_list) | |
1210 | if (ss->kobj == kobj) { | |
d110271e | 1211 | module_put(owner); |
66942064 AC |
1212 | mutex_unlock(&sysfs_workq_mutex); |
1213 | return -EAGAIN; | |
1214 | } | |
1215 | mutex_unlock(&sysfs_workq_mutex); | |
1216 | ||
d110271e | 1217 | if (sysfs_workqueue == NULL) { |
086a377e | 1218 | sysfs_workqueue = create_singlethread_workqueue("sysfsd"); |
d110271e AC |
1219 | if (sysfs_workqueue == NULL) { |
1220 | module_put(owner); | |
1221 | return -ENOMEM; | |
1222 | } | |
1223 | } | |
1224 | ||
d9a9cdfb | 1225 | ss = kmalloc(sizeof(*ss), GFP_KERNEL); |
523ded71 AS |
1226 | if (!ss) { |
1227 | module_put(owner); | |
d9a9cdfb | 1228 | return -ENOMEM; |
523ded71 | 1229 | } |
d9a9cdfb AS |
1230 | kobject_get(kobj); |
1231 | ss->kobj = kobj; | |
1232 | ss->func = func; | |
1233 | ss->data = data; | |
523ded71 | 1234 | ss->owner = owner; |
d9a9cdfb | 1235 | INIT_WORK(&ss->work, sysfs_schedule_callback_work); |
66942064 AC |
1236 | INIT_LIST_HEAD(&ss->workq_list); |
1237 | mutex_lock(&sysfs_workq_mutex); | |
1238 | list_add_tail(&ss->workq_list, &sysfs_workq); | |
1239 | mutex_unlock(&sysfs_workq_mutex); | |
d110271e | 1240 | queue_work(sysfs_workqueue, &ss->work); |
d9a9cdfb AS |
1241 | return 0; |
1242 | } | |
1243 | EXPORT_SYMBOL_GPL(sysfs_schedule_callback); |