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 | ||
58282d8d | 50 | struct sysfs_open_file { |
bcafe4ee TH |
51 | struct sysfs_dirent *sd; |
52 | struct file *file; | |
52e8c209 | 53 | struct mutex mutex; |
73107cb3 | 54 | int event; |
85a4ffad | 55 | struct list_head list; |
73d97146 TH |
56 | |
57 | bool mmapped; | |
58 | const struct vm_operations_struct *vm_ops; | |
73107cb3 | 59 | }; |
1da177e4 | 60 | |
f9b9a621 TH |
61 | static bool sysfs_is_bin(struct sysfs_dirent *sd) |
62 | { | |
63 | return sysfs_type(sd) == SYSFS_KOBJ_BIN_ATTR; | |
64 | } | |
65 | ||
13c589d5 TH |
66 | static struct sysfs_open_file *sysfs_of(struct file *file) |
67 | { | |
68 | return ((struct seq_file *)file->private_data)->private; | |
69 | } | |
70 | ||
375b611e TH |
71 | /* |
72 | * Determine ktype->sysfs_ops for the given sysfs_dirent. This function | |
73 | * must be called while holding an active reference. | |
74 | */ | |
75 | static 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 | ||
13c589d5 TH |
83 | /* |
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. | |
1da177e4 | 87 | */ |
13c589d5 | 88 | static int sysfs_seq_show(struct seq_file *sf, void *v) |
1da177e4 | 89 | { |
13c589d5 TH |
90 | struct sysfs_open_file *of = sf->private; |
91 | struct kobject *kobj = of->sd->s_parent->s_dir.kobj; | |
375b611e | 92 | const struct sysfs_ops *ops; |
13c589d5 | 93 | char *buf; |
1da177e4 LT |
94 | ssize_t count; |
95 | ||
13c589d5 TH |
96 | /* 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 | } | |
1da177e4 | 102 | |
13c589d5 TH |
103 | /* |
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); | |
0ab66088 | 111 | return -ENODEV; |
13c589d5 | 112 | } |
0ab66088 | 113 | |
13c589d5 | 114 | of->event = atomic_read(&of->sd->s_attr.open->event); |
375b611e | 115 | |
13c589d5 TH |
116 | /* |
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; | |
0ab66088 | 125 | |
13c589d5 TH |
126 | sysfs_put_active(of->sd); |
127 | mutex_unlock(&of->mutex); | |
128 | ||
129 | if (count < 0) | |
130 | return count; | |
0ab66088 | 131 | |
8118a859 MX |
132 | /* |
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 | */ | |
815d2d50 AM |
136 | 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 | } | |
13c589d5 TH |
142 | seq_commit(sf, count); |
143 | return 0; | |
1da177e4 LT |
144 | } |
145 | ||
2f0c6b75 TH |
146 | /* |
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 | */ | |
152 | static 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 | ||
1da177e4 | 210 | /** |
8ef445f0 TH |
211 | * flush_write_buffer - push buffer to kobject |
212 | * @of: open file | |
213 | * @buf: data buffer for file | |
f9b9a621 | 214 | * @off: file offset to write to |
8ef445f0 | 215 | * @count: number of bytes |
1da177e4 | 216 | * |
8ef445f0 TH |
217 | * Get the correct pointers for the kobject and the attribute we're dealing |
218 | * with, then call the store() method for it with @buf. | |
1da177e4 | 219 | */ |
f9b9a621 | 220 | static int flush_write_buffer(struct sysfs_open_file *of, char *buf, loff_t off, |
8ef445f0 | 221 | size_t count) |
1da177e4 | 222 | { |
bcafe4ee | 223 | struct kobject *kobj = of->sd->s_parent->s_dir.kobj; |
8ef445f0 | 224 | int rc = 0; |
0ab66088 | 225 | |
8ef445f0 TH |
226 | /* |
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); | |
0ab66088 | 234 | return -ENODEV; |
8ef445f0 | 235 | } |
0ab66088 | 236 | |
f9b9a621 TH |
237 | 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 | } | |
0ab66088 | 249 | |
bcafe4ee | 250 | sysfs_put_active(of->sd); |
8ef445f0 | 251 | mutex_unlock(&of->mutex); |
1da177e4 | 252 | |
0ab66088 | 253 | return rc; |
1da177e4 LT |
254 | } |
255 | ||
1da177e4 | 256 | /** |
8ef445f0 TH |
257 | * 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 | |
262 | * | |
263 | * Copy data in from userland and pass it to the matching | |
264 | * sysfs_ops->store() by invoking flush_write_buffer(). | |
1da177e4 | 265 | * |
8ef445f0 TH |
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. | |
1da177e4 | 271 | */ |
8ef445f0 | 272 | static ssize_t sysfs_write_file(struct file *file, const char __user *user_buf, |
ddfd6d07 | 273 | size_t count, loff_t *ppos) |
1da177e4 | 274 | { |
13c589d5 | 275 | struct sysfs_open_file *of = sysfs_of(file); |
f9b9a621 | 276 | ssize_t len = min_t(size_t, count, PAGE_SIZE); |
8ef445f0 | 277 | char *buf; |
1da177e4 | 278 | |
f9b9a621 TH |
279 | 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 | ||
8ef445f0 TH |
287 | 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 | ||
f9b9a621 | 300 | len = flush_write_buffer(of, buf, *ppos, len); |
1da177e4 LT |
301 | if (len > 0) |
302 | *ppos += len; | |
8ef445f0 TH |
303 | out_free: |
304 | kfree(buf); | |
1da177e4 LT |
305 | return len; |
306 | } | |
307 | ||
73d97146 TH |
308 | static 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 | ||
325 | static 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 | ||
345 | static 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 | ||
368 | static 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 | |
390 | static 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 | ||
411 | static 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 | ||
432 | static 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 | ||
454 | static 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 | ||
466 | static 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; | |
512 | out_put: | |
513 | sysfs_put_active(of->sd); | |
514 | out_unlock: | |
515 | mutex_unlock(&of->mutex); | |
516 | ||
517 | return rc; | |
518 | } | |
519 | ||
85a4ffad TH |
520 | /** |
521 | * sysfs_get_open_dirent - get or create sysfs_open_dirent | |
522 | * @sd: target sysfs_dirent | |
58282d8d | 523 | * @of: sysfs_open_file for this instance of open |
85a4ffad TH |
524 | * |
525 | * If @sd->s_attr.open exists, increment its reference count; | |
58282d8d | 526 | * otherwise, create one. @of is chained to the files list. |
85a4ffad TH |
527 | * |
528 | * LOCKING: | |
529 | * Kernel thread context (may sleep). | |
530 | * | |
531 | * RETURNS: | |
532 | * 0 on success, -errno on failure. | |
533 | */ | |
534 | static int sysfs_get_open_dirent(struct sysfs_dirent *sd, | |
58282d8d | 535 | struct sysfs_open_file *of) |
85a4ffad TH |
536 | { |
537 | struct sysfs_open_dirent *od, *new_od = NULL; | |
538 | ||
539 | retry: | |
c75ec764 | 540 | mutex_lock(&sysfs_open_file_mutex); |
83db93f4 | 541 | spin_lock_irq(&sysfs_open_dirent_lock); |
85a4ffad TH |
542 | |
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); | |
58282d8d | 551 | list_add_tail(&of->list, &od->files); |
85a4ffad TH |
552 | } |
553 | ||
83db93f4 | 554 | spin_unlock_irq(&sysfs_open_dirent_lock); |
c75ec764 | 555 | mutex_unlock(&sysfs_open_file_mutex); |
85a4ffad TH |
556 | |
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); | |
a4e8b912 TH |
568 | atomic_set(&new_od->event, 1); |
569 | init_waitqueue_head(&new_od->poll); | |
58282d8d | 570 | INIT_LIST_HEAD(&new_od->files); |
85a4ffad TH |
571 | goto retry; |
572 | } | |
573 | ||
574 | /** | |
575 | * sysfs_put_open_dirent - put sysfs_open_dirent | |
576 | * @sd: target sysfs_dirent | |
58282d8d | 577 | * @of: associated sysfs_open_file |
85a4ffad | 578 | * |
58282d8d TH |
579 | * Put @sd->s_attr.open and unlink @of from the files list. If |
580 | * reference count reaches zero, disassociate and free it. | |
85a4ffad TH |
581 | * |
582 | * LOCKING: | |
583 | * None. | |
584 | */ | |
585 | static void sysfs_put_open_dirent(struct sysfs_dirent *sd, | |
58282d8d | 586 | struct sysfs_open_file *of) |
85a4ffad TH |
587 | { |
588 | struct sysfs_open_dirent *od = sd->s_attr.open; | |
83db93f4 | 589 | unsigned long flags; |
85a4ffad | 590 | |
c75ec764 | 591 | mutex_lock(&sysfs_open_file_mutex); |
83db93f4 | 592 | spin_lock_irqsave(&sysfs_open_dirent_lock, flags); |
85a4ffad | 593 | |
73d97146 TH |
594 | if (of) |
595 | list_del(&of->list); | |
596 | ||
85a4ffad TH |
597 | if (atomic_dec_and_test(&od->refcnt)) |
598 | sd->s_attr.open = NULL; | |
599 | else | |
600 | od = NULL; | |
601 | ||
83db93f4 | 602 | spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags); |
c75ec764 | 603 | mutex_unlock(&sysfs_open_file_mutex); |
85a4ffad TH |
604 | |
605 | kfree(od); | |
606 | } | |
607 | ||
94bebf4d | 608 | static int sysfs_open_file(struct inode *inode, struct file *file) |
1da177e4 | 609 | { |
3e519038 | 610 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; |
b1fc3d61 | 611 | struct kobject *kobj = attr_sd->s_parent->s_dir.kobj; |
58282d8d | 612 | struct sysfs_open_file *of; |
49fe6047 | 613 | bool has_read, has_write; |
000f2a4d | 614 | int error = -EACCES; |
1da177e4 | 615 | |
0ab66088 | 616 | /* need attr_sd for attr and ops, its parent for kobj */ |
e72ceb8c | 617 | if (!sysfs_get_active(attr_sd)) |
0ab66088 | 618 | return -ENODEV; |
1da177e4 | 619 | |
49fe6047 TH |
620 | if (sysfs_is_bin(attr_sd)) { |
621 | struct bin_attribute *battr = attr_sd->s_bin_attr.bin_attr; | |
1da177e4 | 622 | |
49fe6047 TH |
623 | has_read = battr->read || battr->mmap; |
624 | has_write = battr->write || battr->mmap; | |
625 | } else { | |
626 | const struct sysfs_ops *ops = sysfs_file_ops(attr_sd); | |
1da177e4 | 627 | |
49fe6047 TH |
628 | /* every kobject with an attribute needs a ktype assigned */ |
629 | if (WARN(!ops, KERN_ERR | |
630 | "missing sysfs attribute operations for kobject: %s\n", | |
631 | kobject_name(kobj))) | |
7b595756 | 632 | goto err_out; |
49fe6047 TH |
633 | |
634 | has_read = ops->show; | |
635 | has_write = ops->store; | |
1da177e4 LT |
636 | } |
637 | ||
49fe6047 TH |
638 | /* check perms and supported operations */ |
639 | if ((file->f_mode & FMODE_WRITE) && | |
640 | (!(inode->i_mode & S_IWUGO) || !has_write)) | |
641 | goto err_out; | |
642 | ||
643 | if ((file->f_mode & FMODE_READ) && | |
644 | (!(inode->i_mode & S_IRUGO) || !has_read)) | |
645 | goto err_out; | |
646 | ||
13c589d5 | 647 | /* allocate a sysfs_open_file for the file */ |
0ab66088 | 648 | error = -ENOMEM; |
58282d8d TH |
649 | of = kzalloc(sizeof(struct sysfs_open_file), GFP_KERNEL); |
650 | if (!of) | |
7b595756 | 651 | goto err_out; |
1da177e4 | 652 | |
58282d8d | 653 | mutex_init(&of->mutex); |
bcafe4ee TH |
654 | of->sd = attr_sd; |
655 | of->file = file; | |
13c589d5 TH |
656 | |
657 | /* | |
49fe6047 TH |
658 | * Always instantiate seq_file even if read access doesn't use |
659 | * seq_file or is not requested. This unifies private data access | |
660 | * and readable regular files are the vast majority anyway. | |
13c589d5 | 661 | */ |
49fe6047 TH |
662 | if (sysfs_is_bin(attr_sd)) |
663 | error = single_open(file, NULL, of); | |
664 | else | |
665 | error = single_open(file, sysfs_seq_show, of); | |
13c589d5 TH |
666 | if (error) |
667 | goto err_free; | |
668 | ||
669 | /* seq_file clears PWRITE unconditionally, restore it if WRITE */ | |
670 | if (file->f_mode & FMODE_WRITE) | |
671 | file->f_mode |= FMODE_PWRITE; | |
0ab66088 | 672 | |
85a4ffad | 673 | /* make sure we have open dirent struct */ |
58282d8d | 674 | error = sysfs_get_open_dirent(attr_sd, of); |
85a4ffad | 675 | if (error) |
13c589d5 | 676 | goto err_close; |
85a4ffad | 677 | |
b05f0548 | 678 | /* open succeeded, put active references */ |
e72ceb8c | 679 | sysfs_put_active(attr_sd); |
0ab66088 TH |
680 | return 0; |
681 | ||
13c589d5 TH |
682 | err_close: |
683 | single_release(inode, file); | |
684 | err_free: | |
58282d8d | 685 | kfree(of); |
13c589d5 | 686 | err_out: |
e72ceb8c | 687 | sysfs_put_active(attr_sd); |
1da177e4 LT |
688 | return error; |
689 | } | |
690 | ||
85a4ffad | 691 | static int sysfs_release(struct inode *inode, struct file *filp) |
1da177e4 | 692 | { |
85a4ffad | 693 | struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata; |
13c589d5 | 694 | struct sysfs_open_file *of = sysfs_of(filp); |
1da177e4 | 695 | |
58282d8d | 696 | sysfs_put_open_dirent(sd, of); |
13c589d5 | 697 | single_release(inode, filp); |
58282d8d | 698 | kfree(of); |
50ab1a72 | 699 | |
1da177e4 LT |
700 | return 0; |
701 | } | |
702 | ||
73d97146 TH |
703 | void sysfs_unmap_bin_file(struct sysfs_dirent *sd) |
704 | { | |
705 | struct sysfs_open_dirent *od; | |
706 | struct sysfs_open_file *of; | |
707 | ||
708 | if (!sysfs_is_bin(sd)) | |
709 | return; | |
710 | ||
711 | spin_lock_irq(&sysfs_open_dirent_lock); | |
712 | od = sd->s_attr.open; | |
713 | if (od) | |
714 | atomic_inc(&od->refcnt); | |
715 | spin_unlock_irq(&sysfs_open_dirent_lock); | |
716 | if (!od) | |
717 | return; | |
718 | ||
719 | mutex_lock(&sysfs_open_file_mutex); | |
720 | list_for_each_entry(of, &od->files, list) { | |
721 | struct inode *inode = file_inode(of->file); | |
722 | unmap_mapping_range(inode->i_mapping, 0, 0, 1); | |
723 | } | |
724 | mutex_unlock(&sysfs_open_file_mutex); | |
725 | ||
726 | sysfs_put_open_dirent(sd, NULL); | |
727 | } | |
728 | ||
4508a7a7 N |
729 | /* Sysfs attribute files are pollable. The idea is that you read |
730 | * the content and then you use 'poll' or 'select' to wait for | |
731 | * the content to change. When the content changes (assuming the | |
732 | * manager for the kobject supports notification), poll will | |
733 | * return POLLERR|POLLPRI, and select will return the fd whether | |
734 | * it is waiting for read, write, or exceptions. | |
735 | * Once poll/select indicates that the value has changed, you | |
2424b5dd | 736 | * need to close and re-open the file, or seek to 0 and read again. |
4508a7a7 N |
737 | * Reminder: this only works for attributes which actively support |
738 | * it, and it is not possible to test an attribute from userspace | |
a93720ee | 739 | * to see if it supports poll (Neither 'poll' nor 'select' return |
4508a7a7 N |
740 | * an appropriate error code). When in doubt, set a suitable timeout value. |
741 | */ | |
742 | static unsigned int sysfs_poll(struct file *filp, poll_table *wait) | |
743 | { | |
13c589d5 | 744 | struct sysfs_open_file *of = sysfs_of(filp); |
0ab66088 | 745 | struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata; |
a4e8b912 | 746 | struct sysfs_open_dirent *od = attr_sd->s_attr.open; |
0ab66088 TH |
747 | |
748 | /* need parent for the kobj, grab both */ | |
e72ceb8c | 749 | if (!sysfs_get_active(attr_sd)) |
0ab66088 | 750 | goto trigger; |
4508a7a7 | 751 | |
a4e8b912 | 752 | poll_wait(filp, &od->poll, wait); |
4508a7a7 | 753 | |
e72ceb8c | 754 | sysfs_put_active(attr_sd); |
0ab66088 | 755 | |
58282d8d | 756 | if (of->event != atomic_read(&od->event)) |
0ab66088 | 757 | goto trigger; |
4508a7a7 | 758 | |
1af3557a | 759 | return DEFAULT_POLLMASK; |
0ab66088 TH |
760 | |
761 | trigger: | |
1af3557a | 762 | return DEFAULT_POLLMASK|POLLERR|POLLPRI; |
4508a7a7 N |
763 | } |
764 | ||
f1282c84 NB |
765 | void sysfs_notify_dirent(struct sysfs_dirent *sd) |
766 | { | |
767 | struct sysfs_open_dirent *od; | |
83db93f4 | 768 | unsigned long flags; |
f1282c84 | 769 | |
83db93f4 | 770 | spin_lock_irqsave(&sysfs_open_dirent_lock, flags); |
f1282c84 | 771 | |
fc60bb83 ND |
772 | if (!WARN_ON(sysfs_type(sd) != SYSFS_KOBJ_ATTR)) { |
773 | od = sd->s_attr.open; | |
774 | if (od) { | |
775 | atomic_inc(&od->event); | |
776 | wake_up_interruptible(&od->poll); | |
777 | } | |
f1282c84 NB |
778 | } |
779 | ||
83db93f4 | 780 | spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags); |
f1282c84 NB |
781 | } |
782 | EXPORT_SYMBOL_GPL(sysfs_notify_dirent); | |
783 | ||
8c0e3998 | 784 | void sysfs_notify(struct kobject *k, const char *dir, const char *attr) |
4508a7a7 | 785 | { |
51225039 | 786 | struct sysfs_dirent *sd = k->sd; |
4508a7a7 | 787 | |
51225039 TH |
788 | mutex_lock(&sysfs_mutex); |
789 | ||
790 | if (sd && dir) | |
cfec0bc8 | 791 | sd = sysfs_find_dirent(sd, dir, NULL); |
51225039 | 792 | if (sd && attr) |
cfec0bc8 | 793 | sd = sysfs_find_dirent(sd, attr, NULL); |
f1282c84 NB |
794 | if (sd) |
795 | sysfs_notify_dirent(sd); | |
51225039 TH |
796 | |
797 | mutex_unlock(&sysfs_mutex); | |
4508a7a7 N |
798 | } |
799 | EXPORT_SYMBOL_GPL(sysfs_notify); | |
800 | ||
4b6f5d20 | 801 | const struct file_operations sysfs_file_operations = { |
13c589d5 | 802 | .read = seq_read, |
1da177e4 | 803 | .write = sysfs_write_file, |
13c589d5 | 804 | .llseek = seq_lseek, |
1da177e4 LT |
805 | .open = sysfs_open_file, |
806 | .release = sysfs_release, | |
4508a7a7 | 807 | .poll = sysfs_poll, |
1da177e4 LT |
808 | }; |
809 | ||
f9b9a621 | 810 | const struct file_operations sysfs_bin_operations = { |
2f0c6b75 | 811 | .read = sysfs_bin_read, |
f9b9a621 TH |
812 | .write = sysfs_write_file, |
813 | .llseek = generic_file_llseek, | |
73d97146 | 814 | .mmap = sysfs_bin_mmap, |
49fe6047 TH |
815 | .open = sysfs_open_file, |
816 | .release = sysfs_release, | |
817 | .poll = sysfs_poll, | |
f9b9a621 TH |
818 | }; |
819 | ||
58292cbe TH |
820 | int sysfs_add_file_mode_ns(struct sysfs_dirent *dir_sd, |
821 | const struct attribute *attr, int type, | |
822 | umode_t amode, const void *ns) | |
1da177e4 | 823 | { |
0f423895 | 824 | umode_t mode = (amode & S_IALLUGO) | S_IFREG; |
fb6896da | 825 | struct sysfs_addrm_cxt acxt; |
a26cd722 | 826 | struct sysfs_dirent *sd; |
23dc2799 | 827 | int rc; |
1da177e4 | 828 | |
3007e997 TH |
829 | sd = sysfs_new_dirent(attr->name, mode, type); |
830 | if (!sd) | |
831 | return -ENOMEM; | |
487505c2 EB |
832 | |
833 | sd->s_ns = ns; | |
b1fc3d61 | 834 | sd->s_attr.attr = (void *)attr; |
a2db6842 | 835 | sysfs_dirent_init_lockdep(sd); |
1da177e4 | 836 | |
d69ac5a0 TH |
837 | sysfs_addrm_start(&acxt); |
838 | rc = sysfs_add_one(&acxt, sd, dir_sd); | |
23dc2799 | 839 | sysfs_addrm_finish(&acxt); |
a26cd722 | 840 | |
23dc2799 | 841 | if (rc) |
967e35dc | 842 | sysfs_put(sd); |
3007e997 | 843 | |
23dc2799 | 844 | return rc; |
1da177e4 LT |
845 | } |
846 | ||
847 | ||
0f423895 JB |
848 | int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr, |
849 | int type) | |
850 | { | |
58292cbe | 851 | return sysfs_add_file_mode_ns(dir_sd, attr, type, attr->mode, NULL); |
0f423895 JB |
852 | } |
853 | ||
1da177e4 | 854 | /** |
58292cbe TH |
855 | * sysfs_create_file_ns - create an attribute file for an object with custom ns |
856 | * @kobj: object we're creating for | |
857 | * @attr: attribute descriptor | |
858 | * @ns: namespace the new file should belong to | |
1da177e4 | 859 | */ |
58292cbe TH |
860 | int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr, |
861 | const void *ns) | |
1da177e4 | 862 | { |
608e266a | 863 | BUG_ON(!kobj || !kobj->sd || !attr); |
1da177e4 | 864 | |
58292cbe TH |
865 | return sysfs_add_file_mode_ns(kobj->sd, attr, SYSFS_KOBJ_ATTR, |
866 | attr->mode, ns); | |
1da177e4 LT |
867 | |
868 | } | |
58292cbe | 869 | EXPORT_SYMBOL_GPL(sysfs_create_file_ns); |
1da177e4 | 870 | |
1c205ae1 AK |
871 | int sysfs_create_files(struct kobject *kobj, const struct attribute **ptr) |
872 | { | |
873 | int err = 0; | |
874 | int i; | |
875 | ||
876 | for (i = 0; ptr[i] && !err; i++) | |
877 | err = sysfs_create_file(kobj, ptr[i]); | |
878 | if (err) | |
879 | while (--i >= 0) | |
880 | sysfs_remove_file(kobj, ptr[i]); | |
881 | return err; | |
882 | } | |
1b866757 | 883 | EXPORT_SYMBOL_GPL(sysfs_create_files); |
1da177e4 | 884 | |
dfa87c82 AS |
885 | /** |
886 | * sysfs_add_file_to_group - add an attribute file to a pre-existing group. | |
887 | * @kobj: object we're acting for. | |
888 | * @attr: attribute descriptor. | |
889 | * @group: group name. | |
890 | */ | |
891 | int sysfs_add_file_to_group(struct kobject *kobj, | |
892 | const struct attribute *attr, const char *group) | |
893 | { | |
608e266a | 894 | struct sysfs_dirent *dir_sd; |
dfa87c82 AS |
895 | int error; |
896 | ||
11f24fbd | 897 | if (group) |
388975cc | 898 | dir_sd = sysfs_get_dirent(kobj->sd, group); |
11f24fbd JB |
899 | else |
900 | dir_sd = sysfs_get(kobj->sd); | |
901 | ||
608e266a TH |
902 | if (!dir_sd) |
903 | return -ENOENT; | |
904 | ||
905 | error = sysfs_add_file(dir_sd, attr, SYSFS_KOBJ_ATTR); | |
906 | sysfs_put(dir_sd); | |
907 | ||
dfa87c82 AS |
908 | return error; |
909 | } | |
910 | EXPORT_SYMBOL_GPL(sysfs_add_file_to_group); | |
911 | ||
31e5abe9 KS |
912 | /** |
913 | * sysfs_chmod_file - update the modified mode value on an object attribute. | |
914 | * @kobj: object we're acting for. | |
915 | * @attr: attribute descriptor. | |
916 | * @mode: file permissions. | |
917 | * | |
918 | */ | |
49c19400 | 919 | int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr, |
48176a97 | 920 | umode_t mode) |
31e5abe9 | 921 | { |
06fc0d66 | 922 | struct sysfs_dirent *sd; |
bc062b1b | 923 | struct iattr newattrs; |
51225039 TH |
924 | int rc; |
925 | ||
06fc0d66 | 926 | mutex_lock(&sysfs_mutex); |
51225039 | 927 | |
06fc0d66 | 928 | rc = -ENOENT; |
cfec0bc8 | 929 | sd = sysfs_find_dirent(kobj->sd, attr->name, NULL); |
06fc0d66 | 930 | if (!sd) |
51225039 | 931 | goto out; |
f88123ea | 932 | |
06fc0d66 | 933 | newattrs.ia_mode = (mode & S_IALLUGO) | (sd->s_mode & ~S_IALLUGO); |
4c6974f5 | 934 | newattrs.ia_valid = ATTR_MODE; |
06fc0d66 | 935 | rc = sysfs_sd_setattr(sd, &newattrs); |
f88123ea | 936 | |
51225039 | 937 | out: |
06fc0d66 | 938 | mutex_unlock(&sysfs_mutex); |
51225039 | 939 | return rc; |
31e5abe9 KS |
940 | } |
941 | EXPORT_SYMBOL_GPL(sysfs_chmod_file); | |
942 | ||
1da177e4 | 943 | /** |
58292cbe TH |
944 | * sysfs_remove_file_ns - remove an object attribute with a custom ns tag |
945 | * @kobj: object we're acting for | |
946 | * @attr: attribute descriptor | |
947 | * @ns: namespace tag of the file to remove | |
1da177e4 | 948 | * |
58292cbe | 949 | * Hash the attribute name and namespace tag and kill the victim. |
1da177e4 | 950 | */ |
58292cbe TH |
951 | void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr, |
952 | const void *ns) | |
1da177e4 | 953 | { |
58292cbe | 954 | struct sysfs_dirent *dir_sd = kobj->sd; |
487505c2 | 955 | |
cfec0bc8 | 956 | sysfs_hash_and_remove(dir_sd, attr->name, ns); |
1da177e4 | 957 | } |
58292cbe | 958 | EXPORT_SYMBOL_GPL(sysfs_remove_file_ns); |
1da177e4 | 959 | |
1b18dc2b | 960 | void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr) |
1c205ae1 AK |
961 | { |
962 | int i; | |
963 | for (i = 0; ptr[i]; i++) | |
964 | sysfs_remove_file(kobj, ptr[i]); | |
965 | } | |
1b866757 | 966 | EXPORT_SYMBOL_GPL(sysfs_remove_files); |
1da177e4 | 967 | |
dfa87c82 AS |
968 | /** |
969 | * sysfs_remove_file_from_group - remove an attribute file from a group. | |
970 | * @kobj: object we're acting for. | |
971 | * @attr: attribute descriptor. | |
972 | * @group: group name. | |
973 | */ | |
974 | void sysfs_remove_file_from_group(struct kobject *kobj, | |
975 | const struct attribute *attr, const char *group) | |
976 | { | |
608e266a | 977 | struct sysfs_dirent *dir_sd; |
dfa87c82 | 978 | |
11f24fbd | 979 | if (group) |
388975cc | 980 | dir_sd = sysfs_get_dirent(kobj->sd, group); |
11f24fbd JB |
981 | else |
982 | dir_sd = sysfs_get(kobj->sd); | |
608e266a | 983 | if (dir_sd) { |
cfec0bc8 | 984 | sysfs_hash_and_remove(dir_sd, attr->name, NULL); |
608e266a | 985 | sysfs_put(dir_sd); |
dfa87c82 AS |
986 | } |
987 | } | |
988 | EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group); | |
989 | ||
d9a9cdfb | 990 | struct sysfs_schedule_callback_struct { |
66942064 AC |
991 | struct list_head workq_list; |
992 | struct kobject *kobj; | |
d9a9cdfb AS |
993 | void (*func)(void *); |
994 | void *data; | |
523ded71 | 995 | struct module *owner; |
d9a9cdfb AS |
996 | struct work_struct work; |
997 | }; | |
998 | ||
d110271e | 999 | static struct workqueue_struct *sysfs_workqueue; |
66942064 AC |
1000 | static DEFINE_MUTEX(sysfs_workq_mutex); |
1001 | static LIST_HEAD(sysfs_workq); | |
d9a9cdfb AS |
1002 | static void sysfs_schedule_callback_work(struct work_struct *work) |
1003 | { | |
1004 | struct sysfs_schedule_callback_struct *ss = container_of(work, | |
1005 | struct sysfs_schedule_callback_struct, work); | |
1006 | ||
1007 | (ss->func)(ss->data); | |
1008 | kobject_put(ss->kobj); | |
523ded71 | 1009 | module_put(ss->owner); |
66942064 AC |
1010 | mutex_lock(&sysfs_workq_mutex); |
1011 | list_del(&ss->workq_list); | |
1012 | mutex_unlock(&sysfs_workq_mutex); | |
d9a9cdfb AS |
1013 | kfree(ss); |
1014 | } | |
1015 | ||
1016 | /** | |
1017 | * sysfs_schedule_callback - helper to schedule a callback for a kobject | |
1018 | * @kobj: object we're acting for. | |
1019 | * @func: callback function to invoke later. | |
1020 | * @data: argument to pass to @func. | |
523ded71 | 1021 | * @owner: module owning the callback code |
d9a9cdfb AS |
1022 | * |
1023 | * sysfs attribute methods must not unregister themselves or their parent | |
1024 | * kobject (which would amount to the same thing). Attempts to do so will | |
1025 | * deadlock, since unregistration is mutually exclusive with driver | |
1026 | * callbacks. | |
1027 | * | |
1028 | * Instead methods can call this routine, which will attempt to allocate | |
1029 | * and schedule a workqueue request to call back @func with @data as its | |
1030 | * argument in the workqueue's process context. @kobj will be pinned | |
1031 | * until @func returns. | |
1032 | * | |
1033 | * Returns 0 if the request was submitted, -ENOMEM if storage could not | |
66942064 AC |
1034 | * be allocated, -ENODEV if a reference to @owner isn't available, |
1035 | * -EAGAIN if a callback has already been scheduled for @kobj. | |
d9a9cdfb AS |
1036 | */ |
1037 | int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *), | |
523ded71 | 1038 | void *data, struct module *owner) |
d9a9cdfb | 1039 | { |
66942064 | 1040 | struct sysfs_schedule_callback_struct *ss, *tmp; |
d9a9cdfb | 1041 | |
523ded71 AS |
1042 | if (!try_module_get(owner)) |
1043 | return -ENODEV; | |
66942064 AC |
1044 | |
1045 | mutex_lock(&sysfs_workq_mutex); | |
1046 | list_for_each_entry_safe(ss, tmp, &sysfs_workq, workq_list) | |
1047 | if (ss->kobj == kobj) { | |
d110271e | 1048 | module_put(owner); |
66942064 AC |
1049 | mutex_unlock(&sysfs_workq_mutex); |
1050 | return -EAGAIN; | |
1051 | } | |
1052 | mutex_unlock(&sysfs_workq_mutex); | |
1053 | ||
d110271e | 1054 | if (sysfs_workqueue == NULL) { |
086a377e | 1055 | sysfs_workqueue = create_singlethread_workqueue("sysfsd"); |
d110271e AC |
1056 | if (sysfs_workqueue == NULL) { |
1057 | module_put(owner); | |
1058 | return -ENOMEM; | |
1059 | } | |
1060 | } | |
1061 | ||
d9a9cdfb | 1062 | ss = kmalloc(sizeof(*ss), GFP_KERNEL); |
523ded71 AS |
1063 | if (!ss) { |
1064 | module_put(owner); | |
d9a9cdfb | 1065 | return -ENOMEM; |
523ded71 | 1066 | } |
d9a9cdfb AS |
1067 | kobject_get(kobj); |
1068 | ss->kobj = kobj; | |
1069 | ss->func = func; | |
1070 | ss->data = data; | |
523ded71 | 1071 | ss->owner = owner; |
d9a9cdfb | 1072 | INIT_WORK(&ss->work, sysfs_schedule_callback_work); |
66942064 AC |
1073 | INIT_LIST_HEAD(&ss->workq_list); |
1074 | mutex_lock(&sysfs_workq_mutex); | |
1075 | list_add_tail(&ss->workq_list, &sysfs_workq); | |
1076 | mutex_unlock(&sysfs_workq_mutex); | |
d110271e | 1077 | queue_work(sysfs_workqueue, &ss->work); |
d9a9cdfb AS |
1078 | return 0; |
1079 | } | |
1080 | EXPORT_SYMBOL_GPL(sysfs_schedule_callback); |