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> |
1da177e4 LT |
24 | |
25 | #include "sysfs.h" | |
26 | ||
85a4ffad TH |
27 | /* |
28 | * There's one sysfs_buffer for each open file and one | |
29 | * sysfs_open_dirent for each sysfs_dirent with one or more open | |
30 | * files. | |
31 | * | |
32 | * filp->private_data points to sysfs_buffer and | |
33 | * sysfs_dirent->s_attr.open points to sysfs_open_dirent. s_attr.open | |
34 | * is protected by sysfs_open_dirent_lock. | |
35 | */ | |
d7b37889 | 36 | static DEFINE_SPINLOCK(sysfs_open_dirent_lock); |
85a4ffad TH |
37 | |
38 | struct sysfs_open_dirent { | |
39 | atomic_t refcnt; | |
a4e8b912 TH |
40 | atomic_t event; |
41 | wait_queue_head_t poll; | |
85a4ffad TH |
42 | struct list_head buffers; /* goes through sysfs_buffer.list */ |
43 | }; | |
44 | ||
73107cb3 TH |
45 | struct sysfs_buffer { |
46 | size_t count; | |
1b18dc2b GKH |
47 | char *page; |
48 | const struct sysfs_ops *ops; | |
52e8c209 | 49 | struct mutex mutex; |
73107cb3 | 50 | int event; |
85a4ffad | 51 | struct list_head list; |
73107cb3 | 52 | }; |
1da177e4 LT |
53 | |
54 | /** | |
55 | * fill_read_buffer - allocate and fill buffer from object. | |
56 | * @dentry: dentry pointer. | |
57 | * @buffer: data buffer for file. | |
58 | * | |
59 | * Allocate @buffer->page, if it hasn't been already, then call the | |
ab9bf4be GKH |
60 | * kobject's show() method to fill the buffer with this attribute's |
61 | * data. | |
82244b16 ON |
62 | * This is called only once, on the file's first read unless an error |
63 | * is returned. | |
1da177e4 | 64 | */ |
1b18dc2b | 65 | static int fill_read_buffer(struct dentry *dentry, struct sysfs_buffer *buffer) |
1da177e4 | 66 | { |
0ab66088 | 67 | struct sysfs_dirent *attr_sd = dentry->d_fsdata; |
b1fc3d61 | 68 | struct kobject *kobj = attr_sd->s_parent->s_dir.kobj; |
1b18dc2b | 69 | const struct sysfs_ops *ops = buffer->ops; |
1da177e4 LT |
70 | int ret = 0; |
71 | ssize_t count; | |
72 | ||
73 | if (!buffer->page) | |
74 | buffer->page = (char *) get_zeroed_page(GFP_KERNEL); | |
75 | if (!buffer->page) | |
76 | return -ENOMEM; | |
77 | ||
0ab66088 | 78 | /* need attr_sd for attr and ops, its parent for kobj */ |
e72ceb8c | 79 | if (!sysfs_get_active(attr_sd)) |
0ab66088 TH |
80 | return -ENODEV; |
81 | ||
a4e8b912 | 82 | buffer->event = atomic_read(&attr_sd->s_attr.open->event); |
b1fc3d61 | 83 | count = ops->show(kobj, attr_sd->s_attr.attr, buffer->page); |
0ab66088 | 84 | |
e72ceb8c | 85 | sysfs_put_active(attr_sd); |
0ab66088 | 86 | |
8118a859 MX |
87 | /* |
88 | * The code works fine with PAGE_SIZE return but it's likely to | |
89 | * indicate truncated result or overflow in normal use cases. | |
90 | */ | |
815d2d50 AM |
91 | if (count >= (ssize_t)PAGE_SIZE) { |
92 | print_symbol("fill_read_buffer: %s returned bad count\n", | |
93 | (unsigned long)ops->show); | |
94 | /* Try to struggle along */ | |
95 | count = PAGE_SIZE - 1; | |
96 | } | |
aea585ef | 97 | if (count >= 0) |
1da177e4 | 98 | buffer->count = count; |
aea585ef | 99 | else |
1da177e4 LT |
100 | ret = count; |
101 | return ret; | |
102 | } | |
103 | ||
1da177e4 | 104 | /** |
ab9bf4be | 105 | * sysfs_read_file - read an attribute. |
1da177e4 LT |
106 | * @file: file pointer. |
107 | * @buf: buffer to fill. | |
108 | * @count: number of bytes to read. | |
109 | * @ppos: starting offset in file. | |
110 | * | |
111 | * Userspace wants to read an attribute file. The attribute descriptor | |
112 | * is in the file's ->d_fsdata. The target object is in the directory's | |
113 | * ->d_fsdata. | |
114 | * | |
115 | * We call fill_read_buffer() to allocate and fill the buffer from the | |
116 | * object's show() method exactly once (if the read is happening from | |
117 | * the beginning of the file). That should fill the entire buffer with | |
118 | * all the data the object has to offer for that attribute. | |
119 | * We then call flush_read_buffer() to copy the buffer to userspace | |
120 | * in the increments specified. | |
121 | */ | |
122 | ||
123 | static ssize_t | |
124 | sysfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos) | |
125 | { | |
1b18dc2b | 126 | struct sysfs_buffer *buffer = file->private_data; |
1da177e4 LT |
127 | ssize_t retval = 0; |
128 | ||
52e8c209 | 129 | mutex_lock(&buffer->mutex); |
aea585ef TH |
130 | /* |
131 | * Fill on zero offset and the first read so that silly things like | |
132 | * "dd bs=1 skip=N" can work on sysfs files. | |
133 | */ | |
134 | if (*ppos == 0 || !buffer->page) { | |
1b18dc2b | 135 | retval = fill_read_buffer(file->f_path.dentry, buffer); |
e7b0d26a | 136 | if (retval) |
1da177e4 LT |
137 | goto out; |
138 | } | |
5c1fdf41 | 139 | pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n", |
8e24eea7 | 140 | __func__, count, *ppos, buffer->page); |
92f4c701 AM |
141 | retval = simple_read_from_buffer(buf, count, ppos, buffer->page, |
142 | buffer->count); | |
1da177e4 | 143 | out: |
52e8c209 | 144 | mutex_unlock(&buffer->mutex); |
1da177e4 LT |
145 | return retval; |
146 | } | |
147 | ||
1da177e4 LT |
148 | /** |
149 | * fill_write_buffer - copy buffer from userspace. | |
150 | * @buffer: data buffer for file. | |
67be2dd1 | 151 | * @buf: data from user. |
1da177e4 LT |
152 | * @count: number of bytes in @userbuf. |
153 | * | |
154 | * Allocate @buffer->page if it hasn't been already, then | |
155 | * copy the user-supplied buffer into it. | |
156 | */ | |
1b18dc2b GKH |
157 | static int fill_write_buffer(struct sysfs_buffer *buffer, |
158 | const char __user *buf, size_t count) | |
1da177e4 LT |
159 | { |
160 | int error; | |
161 | ||
162 | if (!buffer->page) | |
163 | buffer->page = (char *)get_zeroed_page(GFP_KERNEL); | |
164 | if (!buffer->page) | |
165 | return -ENOMEM; | |
166 | ||
167 | if (count >= PAGE_SIZE) | |
6e0dd741 | 168 | count = PAGE_SIZE - 1; |
1b18dc2b | 169 | error = copy_from_user(buffer->page, buf, count); |
aea585ef TH |
170 | |
171 | /* | |
172 | * If buf is assumed to contain a string, terminate it by \0, so | |
173 | * e.g. sscanf() can scan the string easily. | |
174 | */ | |
035ed7a4 | 175 | buffer->page[count] = 0; |
1da177e4 LT |
176 | return error ? -EFAULT : count; |
177 | } | |
178 | ||
1da177e4 LT |
179 | /** |
180 | * flush_write_buffer - push buffer to kobject. | |
3d41088f | 181 | * @dentry: dentry to the attribute |
1da177e4 | 182 | * @buffer: data buffer for file. |
3d41088f | 183 | * @count: number of bytes |
1da177e4 LT |
184 | * |
185 | * Get the correct pointers for the kobject and the attribute we're | |
ab9bf4be | 186 | * dealing with, then call the store() method for the attribute, |
1da177e4 LT |
187 | * passing the buffer that we acquired in fill_write_buffer(). |
188 | */ | |
ddfd6d07 GKH |
189 | static int flush_write_buffer(struct dentry *dentry, |
190 | struct sysfs_buffer *buffer, size_t count) | |
1da177e4 | 191 | { |
3e519038 | 192 | struct sysfs_dirent *attr_sd = dentry->d_fsdata; |
b1fc3d61 | 193 | struct kobject *kobj = attr_sd->s_parent->s_dir.kobj; |
1b18dc2b | 194 | const struct sysfs_ops *ops = buffer->ops; |
0ab66088 TH |
195 | int rc; |
196 | ||
197 | /* need attr_sd for attr and ops, its parent for kobj */ | |
e72ceb8c | 198 | if (!sysfs_get_active(attr_sd)) |
0ab66088 TH |
199 | return -ENODEV; |
200 | ||
b1fc3d61 | 201 | rc = ops->store(kobj, attr_sd->s_attr.attr, buffer->page, count); |
0ab66088 | 202 | |
e72ceb8c | 203 | sysfs_put_active(attr_sd); |
1da177e4 | 204 | |
0ab66088 | 205 | return rc; |
1da177e4 LT |
206 | } |
207 | ||
208 | ||
209 | /** | |
210 | * sysfs_write_file - write an attribute. | |
211 | * @file: file pointer | |
212 | * @buf: data to write | |
213 | * @count: number of bytes | |
214 | * @ppos: starting offset | |
215 | * | |
216 | * Similar to sysfs_read_file(), though working in the opposite direction. | |
217 | * We allocate and fill the data from the user in fill_write_buffer(), | |
218 | * then push it to the kobject in flush_write_buffer(). | |
219 | * There is no easy way for us to know if userspace is only doing a partial | |
220 | * write, so we don't support them. We expect the entire buffer to come | |
ab9bf4be | 221 | * on the first write. |
1da177e4 | 222 | * Hint: if you're writing a value, first read the file, modify only the |
ab9bf4be | 223 | * the value you're changing, then write entire buffer back. |
1da177e4 | 224 | */ |
ddfd6d07 GKH |
225 | static ssize_t sysfs_write_file(struct file *file, const char __user *buf, |
226 | size_t count, loff_t *ppos) | |
1da177e4 | 227 | { |
1b18dc2b | 228 | struct sysfs_buffer *buffer = file->private_data; |
1da177e4 LT |
229 | ssize_t len; |
230 | ||
52e8c209 | 231 | mutex_lock(&buffer->mutex); |
1da177e4 LT |
232 | len = fill_write_buffer(buffer, buf, count); |
233 | if (len > 0) | |
f427f5d5 | 234 | len = flush_write_buffer(file->f_path.dentry, buffer, len); |
1da177e4 LT |
235 | if (len > 0) |
236 | *ppos += len; | |
52e8c209 | 237 | mutex_unlock(&buffer->mutex); |
1da177e4 LT |
238 | return len; |
239 | } | |
240 | ||
85a4ffad TH |
241 | /** |
242 | * sysfs_get_open_dirent - get or create sysfs_open_dirent | |
243 | * @sd: target sysfs_dirent | |
244 | * @buffer: sysfs_buffer for this instance of open | |
245 | * | |
246 | * If @sd->s_attr.open exists, increment its reference count; | |
247 | * otherwise, create one. @buffer is chained to the buffers | |
248 | * list. | |
249 | * | |
250 | * LOCKING: | |
251 | * Kernel thread context (may sleep). | |
252 | * | |
253 | * RETURNS: | |
254 | * 0 on success, -errno on failure. | |
255 | */ | |
256 | static int sysfs_get_open_dirent(struct sysfs_dirent *sd, | |
257 | struct sysfs_buffer *buffer) | |
258 | { | |
259 | struct sysfs_open_dirent *od, *new_od = NULL; | |
260 | ||
261 | retry: | |
83db93f4 | 262 | spin_lock_irq(&sysfs_open_dirent_lock); |
85a4ffad TH |
263 | |
264 | if (!sd->s_attr.open && new_od) { | |
265 | sd->s_attr.open = new_od; | |
266 | new_od = NULL; | |
267 | } | |
268 | ||
269 | od = sd->s_attr.open; | |
270 | if (od) { | |
271 | atomic_inc(&od->refcnt); | |
272 | list_add_tail(&buffer->list, &od->buffers); | |
273 | } | |
274 | ||
83db93f4 | 275 | spin_unlock_irq(&sysfs_open_dirent_lock); |
85a4ffad TH |
276 | |
277 | if (od) { | |
278 | kfree(new_od); | |
279 | return 0; | |
280 | } | |
281 | ||
282 | /* not there, initialize a new one and retry */ | |
283 | new_od = kmalloc(sizeof(*new_od), GFP_KERNEL); | |
284 | if (!new_od) | |
285 | return -ENOMEM; | |
286 | ||
287 | atomic_set(&new_od->refcnt, 0); | |
a4e8b912 TH |
288 | atomic_set(&new_od->event, 1); |
289 | init_waitqueue_head(&new_od->poll); | |
85a4ffad TH |
290 | INIT_LIST_HEAD(&new_od->buffers); |
291 | goto retry; | |
292 | } | |
293 | ||
294 | /** | |
295 | * sysfs_put_open_dirent - put sysfs_open_dirent | |
296 | * @sd: target sysfs_dirent | |
297 | * @buffer: associated sysfs_buffer | |
298 | * | |
299 | * Put @sd->s_attr.open and unlink @buffer from the buffers list. | |
300 | * If reference count reaches zero, disassociate and free it. | |
301 | * | |
302 | * LOCKING: | |
303 | * None. | |
304 | */ | |
305 | static void sysfs_put_open_dirent(struct sysfs_dirent *sd, | |
306 | struct sysfs_buffer *buffer) | |
307 | { | |
308 | struct sysfs_open_dirent *od = sd->s_attr.open; | |
83db93f4 | 309 | unsigned long flags; |
85a4ffad | 310 | |
83db93f4 | 311 | spin_lock_irqsave(&sysfs_open_dirent_lock, flags); |
85a4ffad TH |
312 | |
313 | list_del(&buffer->list); | |
314 | if (atomic_dec_and_test(&od->refcnt)) | |
315 | sd->s_attr.open = NULL; | |
316 | else | |
317 | od = NULL; | |
318 | ||
83db93f4 | 319 | spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags); |
85a4ffad TH |
320 | |
321 | kfree(od); | |
322 | } | |
323 | ||
94bebf4d | 324 | static int sysfs_open_file(struct inode *inode, struct file *file) |
1da177e4 | 325 | { |
3e519038 | 326 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; |
b1fc3d61 | 327 | struct kobject *kobj = attr_sd->s_parent->s_dir.kobj; |
000f2a4d | 328 | struct sysfs_buffer *buffer; |
52cf25d0 | 329 | const struct sysfs_ops *ops; |
000f2a4d | 330 | int error = -EACCES; |
1da177e4 | 331 | |
0ab66088 | 332 | /* need attr_sd for attr and ops, its parent for kobj */ |
e72ceb8c | 333 | if (!sysfs_get_active(attr_sd)) |
0ab66088 | 334 | return -ENODEV; |
1da177e4 | 335 | |
000f2a4d KS |
336 | /* every kobject with an attribute needs a ktype assigned */ |
337 | if (kobj->ktype && kobj->ktype->sysfs_ops) | |
1da177e4 | 338 | ops = kobj->ktype->sysfs_ops; |
000f2a4d | 339 | else { |
07ac62a6 GKH |
340 | WARN(1, KERN_ERR |
341 | "missing sysfs attribute operations for kobject: %s\n", | |
342 | kobject_name(kobj)); | |
7b595756 | 343 | goto err_out; |
000f2a4d | 344 | } |
1da177e4 LT |
345 | |
346 | /* File needs write support. | |
ab9bf4be | 347 | * The inode's perms must say it's ok, |
1da177e4 LT |
348 | * and we must have a store method. |
349 | */ | |
350 | if (file->f_mode & FMODE_WRITE) { | |
1da177e4 | 351 | if (!(inode->i_mode & S_IWUGO) || !ops->store) |
7b595756 | 352 | goto err_out; |
1da177e4 LT |
353 | } |
354 | ||
355 | /* File needs read support. | |
356 | * The inode's perms must say it's ok, and we there | |
357 | * must be a show method for it. | |
358 | */ | |
359 | if (file->f_mode & FMODE_READ) { | |
360 | if (!(inode->i_mode & S_IRUGO) || !ops->show) | |
7b595756 | 361 | goto err_out; |
1da177e4 LT |
362 | } |
363 | ||
364 | /* No error? Great, allocate a buffer for the file, and store it | |
365 | * it in file->private_data for easy access. | |
366 | */ | |
0ab66088 | 367 | error = -ENOMEM; |
58d49283 | 368 | buffer = kzalloc(sizeof(struct sysfs_buffer), GFP_KERNEL); |
0ab66088 | 369 | if (!buffer) |
7b595756 | 370 | goto err_out; |
1da177e4 | 371 | |
52e8c209 | 372 | mutex_init(&buffer->mutex); |
0ab66088 | 373 | buffer->ops = ops; |
0ab66088 TH |
374 | file->private_data = buffer; |
375 | ||
85a4ffad TH |
376 | /* make sure we have open dirent struct */ |
377 | error = sysfs_get_open_dirent(attr_sd, buffer); | |
378 | if (error) | |
379 | goto err_free; | |
380 | ||
b05f0548 | 381 | /* open succeeded, put active references */ |
e72ceb8c | 382 | sysfs_put_active(attr_sd); |
0ab66088 TH |
383 | return 0; |
384 | ||
85a4ffad TH |
385 | err_free: |
386 | kfree(buffer); | |
7b595756 | 387 | err_out: |
e72ceb8c | 388 | sysfs_put_active(attr_sd); |
1da177e4 LT |
389 | return error; |
390 | } | |
391 | ||
85a4ffad | 392 | static int sysfs_release(struct inode *inode, struct file *filp) |
1da177e4 | 393 | { |
85a4ffad | 394 | struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata; |
73107cb3 | 395 | struct sysfs_buffer *buffer = filp->private_data; |
1da177e4 | 396 | |
85a4ffad TH |
397 | sysfs_put_open_dirent(sd, buffer); |
398 | ||
50ab1a72 TH |
399 | if (buffer->page) |
400 | free_page((unsigned long)buffer->page); | |
401 | kfree(buffer); | |
402 | ||
1da177e4 LT |
403 | return 0; |
404 | } | |
405 | ||
4508a7a7 N |
406 | /* Sysfs attribute files are pollable. The idea is that you read |
407 | * the content and then you use 'poll' or 'select' to wait for | |
408 | * the content to change. When the content changes (assuming the | |
409 | * manager for the kobject supports notification), poll will | |
410 | * return POLLERR|POLLPRI, and select will return the fd whether | |
411 | * it is waiting for read, write, or exceptions. | |
412 | * Once poll/select indicates that the value has changed, you | |
2424b5dd | 413 | * need to close and re-open the file, or seek to 0 and read again. |
4508a7a7 N |
414 | * Reminder: this only works for attributes which actively support |
415 | * it, and it is not possible to test an attribute from userspace | |
a93720ee | 416 | * to see if it supports poll (Neither 'poll' nor 'select' return |
4508a7a7 N |
417 | * an appropriate error code). When in doubt, set a suitable timeout value. |
418 | */ | |
419 | static unsigned int sysfs_poll(struct file *filp, poll_table *wait) | |
420 | { | |
1b18dc2b | 421 | struct sysfs_buffer *buffer = filp->private_data; |
0ab66088 | 422 | struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata; |
a4e8b912 | 423 | struct sysfs_open_dirent *od = attr_sd->s_attr.open; |
0ab66088 TH |
424 | |
425 | /* need parent for the kobj, grab both */ | |
e72ceb8c | 426 | if (!sysfs_get_active(attr_sd)) |
0ab66088 | 427 | goto trigger; |
4508a7a7 | 428 | |
a4e8b912 | 429 | poll_wait(filp, &od->poll, wait); |
4508a7a7 | 430 | |
e72ceb8c | 431 | sysfs_put_active(attr_sd); |
0ab66088 | 432 | |
a4e8b912 | 433 | if (buffer->event != atomic_read(&od->event)) |
0ab66088 | 434 | goto trigger; |
4508a7a7 | 435 | |
1af3557a | 436 | return DEFAULT_POLLMASK; |
0ab66088 TH |
437 | |
438 | trigger: | |
1af3557a | 439 | return DEFAULT_POLLMASK|POLLERR|POLLPRI; |
4508a7a7 N |
440 | } |
441 | ||
f1282c84 NB |
442 | void sysfs_notify_dirent(struct sysfs_dirent *sd) |
443 | { | |
444 | struct sysfs_open_dirent *od; | |
83db93f4 | 445 | unsigned long flags; |
f1282c84 | 446 | |
83db93f4 | 447 | spin_lock_irqsave(&sysfs_open_dirent_lock, flags); |
f1282c84 | 448 | |
fc60bb83 ND |
449 | if (!WARN_ON(sysfs_type(sd) != SYSFS_KOBJ_ATTR)) { |
450 | od = sd->s_attr.open; | |
451 | if (od) { | |
452 | atomic_inc(&od->event); | |
453 | wake_up_interruptible(&od->poll); | |
454 | } | |
f1282c84 NB |
455 | } |
456 | ||
83db93f4 | 457 | spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags); |
f1282c84 NB |
458 | } |
459 | EXPORT_SYMBOL_GPL(sysfs_notify_dirent); | |
460 | ||
8c0e3998 | 461 | void sysfs_notify(struct kobject *k, const char *dir, const char *attr) |
4508a7a7 | 462 | { |
51225039 | 463 | struct sysfs_dirent *sd = k->sd; |
4508a7a7 | 464 | |
51225039 TH |
465 | mutex_lock(&sysfs_mutex); |
466 | ||
467 | if (sd && dir) | |
cfec0bc8 | 468 | sd = sysfs_find_dirent(sd, dir, NULL); |
51225039 | 469 | if (sd && attr) |
cfec0bc8 | 470 | sd = sysfs_find_dirent(sd, attr, NULL); |
f1282c84 NB |
471 | if (sd) |
472 | sysfs_notify_dirent(sd); | |
51225039 TH |
473 | |
474 | mutex_unlock(&sysfs_mutex); | |
4508a7a7 N |
475 | } |
476 | EXPORT_SYMBOL_GPL(sysfs_notify); | |
477 | ||
4b6f5d20 | 478 | const struct file_operations sysfs_file_operations = { |
1da177e4 LT |
479 | .read = sysfs_read_file, |
480 | .write = sysfs_write_file, | |
481 | .llseek = generic_file_llseek, | |
482 | .open = sysfs_open_file, | |
483 | .release = sysfs_release, | |
4508a7a7 | 484 | .poll = sysfs_poll, |
1da177e4 LT |
485 | }; |
486 | ||
58292cbe TH |
487 | int sysfs_add_file_mode_ns(struct sysfs_dirent *dir_sd, |
488 | const struct attribute *attr, int type, | |
489 | umode_t amode, const void *ns) | |
1da177e4 | 490 | { |
0f423895 | 491 | umode_t mode = (amode & S_IALLUGO) | S_IFREG; |
fb6896da | 492 | struct sysfs_addrm_cxt acxt; |
a26cd722 | 493 | struct sysfs_dirent *sd; |
23dc2799 | 494 | int rc; |
1da177e4 | 495 | |
3007e997 TH |
496 | sd = sysfs_new_dirent(attr->name, mode, type); |
497 | if (!sd) | |
498 | return -ENOMEM; | |
487505c2 EB |
499 | |
500 | sd->s_ns = ns; | |
b1fc3d61 | 501 | sd->s_attr.attr = (void *)attr; |
a2db6842 | 502 | sysfs_dirent_init_lockdep(sd); |
1da177e4 | 503 | |
d69ac5a0 TH |
504 | sysfs_addrm_start(&acxt); |
505 | rc = sysfs_add_one(&acxt, sd, dir_sd); | |
23dc2799 | 506 | sysfs_addrm_finish(&acxt); |
a26cd722 | 507 | |
23dc2799 | 508 | if (rc) |
967e35dc | 509 | sysfs_put(sd); |
3007e997 | 510 | |
23dc2799 | 511 | return rc; |
1da177e4 LT |
512 | } |
513 | ||
514 | ||
0f423895 JB |
515 | int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr, |
516 | int type) | |
517 | { | |
58292cbe | 518 | return sysfs_add_file_mode_ns(dir_sd, attr, type, attr->mode, NULL); |
0f423895 JB |
519 | } |
520 | ||
1da177e4 | 521 | /** |
58292cbe TH |
522 | * sysfs_create_file_ns - create an attribute file for an object with custom ns |
523 | * @kobj: object we're creating for | |
524 | * @attr: attribute descriptor | |
525 | * @ns: namespace the new file should belong to | |
1da177e4 | 526 | */ |
58292cbe TH |
527 | int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr, |
528 | const void *ns) | |
1da177e4 | 529 | { |
608e266a | 530 | BUG_ON(!kobj || !kobj->sd || !attr); |
1da177e4 | 531 | |
58292cbe TH |
532 | return sysfs_add_file_mode_ns(kobj->sd, attr, SYSFS_KOBJ_ATTR, |
533 | attr->mode, ns); | |
1da177e4 LT |
534 | |
535 | } | |
58292cbe | 536 | EXPORT_SYMBOL_GPL(sysfs_create_file_ns); |
1da177e4 | 537 | |
1c205ae1 AK |
538 | int sysfs_create_files(struct kobject *kobj, const struct attribute **ptr) |
539 | { | |
540 | int err = 0; | |
541 | int i; | |
542 | ||
543 | for (i = 0; ptr[i] && !err; i++) | |
544 | err = sysfs_create_file(kobj, ptr[i]); | |
545 | if (err) | |
546 | while (--i >= 0) | |
547 | sysfs_remove_file(kobj, ptr[i]); | |
548 | return err; | |
549 | } | |
1b866757 | 550 | EXPORT_SYMBOL_GPL(sysfs_create_files); |
1da177e4 | 551 | |
dfa87c82 AS |
552 | /** |
553 | * sysfs_add_file_to_group - add an attribute file to a pre-existing group. | |
554 | * @kobj: object we're acting for. | |
555 | * @attr: attribute descriptor. | |
556 | * @group: group name. | |
557 | */ | |
558 | int sysfs_add_file_to_group(struct kobject *kobj, | |
559 | const struct attribute *attr, const char *group) | |
560 | { | |
608e266a | 561 | struct sysfs_dirent *dir_sd; |
dfa87c82 AS |
562 | int error; |
563 | ||
11f24fbd | 564 | if (group) |
388975cc | 565 | dir_sd = sysfs_get_dirent(kobj->sd, group); |
11f24fbd JB |
566 | else |
567 | dir_sd = sysfs_get(kobj->sd); | |
568 | ||
608e266a TH |
569 | if (!dir_sd) |
570 | return -ENOENT; | |
571 | ||
572 | error = sysfs_add_file(dir_sd, attr, SYSFS_KOBJ_ATTR); | |
573 | sysfs_put(dir_sd); | |
574 | ||
dfa87c82 AS |
575 | return error; |
576 | } | |
577 | EXPORT_SYMBOL_GPL(sysfs_add_file_to_group); | |
578 | ||
31e5abe9 KS |
579 | /** |
580 | * sysfs_chmod_file - update the modified mode value on an object attribute. | |
581 | * @kobj: object we're acting for. | |
582 | * @attr: attribute descriptor. | |
583 | * @mode: file permissions. | |
584 | * | |
585 | */ | |
49c19400 | 586 | int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr, |
48176a97 | 587 | umode_t mode) |
31e5abe9 | 588 | { |
06fc0d66 | 589 | struct sysfs_dirent *sd; |
bc062b1b | 590 | struct iattr newattrs; |
51225039 TH |
591 | int rc; |
592 | ||
06fc0d66 | 593 | mutex_lock(&sysfs_mutex); |
51225039 | 594 | |
06fc0d66 | 595 | rc = -ENOENT; |
cfec0bc8 | 596 | sd = sysfs_find_dirent(kobj->sd, attr->name, NULL); |
06fc0d66 | 597 | if (!sd) |
51225039 | 598 | goto out; |
f88123ea | 599 | |
06fc0d66 | 600 | newattrs.ia_mode = (mode & S_IALLUGO) | (sd->s_mode & ~S_IALLUGO); |
4c6974f5 | 601 | newattrs.ia_valid = ATTR_MODE; |
06fc0d66 | 602 | rc = sysfs_sd_setattr(sd, &newattrs); |
f88123ea | 603 | |
51225039 | 604 | out: |
06fc0d66 | 605 | mutex_unlock(&sysfs_mutex); |
51225039 | 606 | return rc; |
31e5abe9 KS |
607 | } |
608 | EXPORT_SYMBOL_GPL(sysfs_chmod_file); | |
609 | ||
1da177e4 | 610 | /** |
58292cbe TH |
611 | * sysfs_remove_file_ns - remove an object attribute with a custom ns tag |
612 | * @kobj: object we're acting for | |
613 | * @attr: attribute descriptor | |
614 | * @ns: namespace tag of the file to remove | |
1da177e4 | 615 | * |
58292cbe | 616 | * Hash the attribute name and namespace tag and kill the victim. |
1da177e4 | 617 | */ |
58292cbe TH |
618 | void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr, |
619 | const void *ns) | |
1da177e4 | 620 | { |
58292cbe | 621 | struct sysfs_dirent *dir_sd = kobj->sd; |
487505c2 | 622 | |
cfec0bc8 | 623 | sysfs_hash_and_remove(dir_sd, attr->name, ns); |
1da177e4 | 624 | } |
58292cbe | 625 | EXPORT_SYMBOL_GPL(sysfs_remove_file_ns); |
1da177e4 | 626 | |
1b18dc2b | 627 | void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr) |
1c205ae1 AK |
628 | { |
629 | int i; | |
630 | for (i = 0; ptr[i]; i++) | |
631 | sysfs_remove_file(kobj, ptr[i]); | |
632 | } | |
1b866757 | 633 | EXPORT_SYMBOL_GPL(sysfs_remove_files); |
1da177e4 | 634 | |
dfa87c82 AS |
635 | /** |
636 | * sysfs_remove_file_from_group - remove an attribute file from a group. | |
637 | * @kobj: object we're acting for. | |
638 | * @attr: attribute descriptor. | |
639 | * @group: group name. | |
640 | */ | |
641 | void sysfs_remove_file_from_group(struct kobject *kobj, | |
642 | const struct attribute *attr, const char *group) | |
643 | { | |
608e266a | 644 | struct sysfs_dirent *dir_sd; |
dfa87c82 | 645 | |
11f24fbd | 646 | if (group) |
388975cc | 647 | dir_sd = sysfs_get_dirent(kobj->sd, group); |
11f24fbd JB |
648 | else |
649 | dir_sd = sysfs_get(kobj->sd); | |
608e266a | 650 | if (dir_sd) { |
cfec0bc8 | 651 | sysfs_hash_and_remove(dir_sd, attr->name, NULL); |
608e266a | 652 | sysfs_put(dir_sd); |
dfa87c82 AS |
653 | } |
654 | } | |
655 | EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group); | |
656 | ||
d9a9cdfb | 657 | struct sysfs_schedule_callback_struct { |
66942064 AC |
658 | struct list_head workq_list; |
659 | struct kobject *kobj; | |
d9a9cdfb AS |
660 | void (*func)(void *); |
661 | void *data; | |
523ded71 | 662 | struct module *owner; |
d9a9cdfb AS |
663 | struct work_struct work; |
664 | }; | |
665 | ||
d110271e | 666 | static struct workqueue_struct *sysfs_workqueue; |
66942064 AC |
667 | static DEFINE_MUTEX(sysfs_workq_mutex); |
668 | static LIST_HEAD(sysfs_workq); | |
d9a9cdfb AS |
669 | static void sysfs_schedule_callback_work(struct work_struct *work) |
670 | { | |
671 | struct sysfs_schedule_callback_struct *ss = container_of(work, | |
672 | struct sysfs_schedule_callback_struct, work); | |
673 | ||
674 | (ss->func)(ss->data); | |
675 | kobject_put(ss->kobj); | |
523ded71 | 676 | module_put(ss->owner); |
66942064 AC |
677 | mutex_lock(&sysfs_workq_mutex); |
678 | list_del(&ss->workq_list); | |
679 | mutex_unlock(&sysfs_workq_mutex); | |
d9a9cdfb AS |
680 | kfree(ss); |
681 | } | |
682 | ||
683 | /** | |
684 | * sysfs_schedule_callback - helper to schedule a callback for a kobject | |
685 | * @kobj: object we're acting for. | |
686 | * @func: callback function to invoke later. | |
687 | * @data: argument to pass to @func. | |
523ded71 | 688 | * @owner: module owning the callback code |
d9a9cdfb AS |
689 | * |
690 | * sysfs attribute methods must not unregister themselves or their parent | |
691 | * kobject (which would amount to the same thing). Attempts to do so will | |
692 | * deadlock, since unregistration is mutually exclusive with driver | |
693 | * callbacks. | |
694 | * | |
695 | * Instead methods can call this routine, which will attempt to allocate | |
696 | * and schedule a workqueue request to call back @func with @data as its | |
697 | * argument in the workqueue's process context. @kobj will be pinned | |
698 | * until @func returns. | |
699 | * | |
700 | * Returns 0 if the request was submitted, -ENOMEM if storage could not | |
66942064 AC |
701 | * be allocated, -ENODEV if a reference to @owner isn't available, |
702 | * -EAGAIN if a callback has already been scheduled for @kobj. | |
d9a9cdfb AS |
703 | */ |
704 | int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *), | |
523ded71 | 705 | void *data, struct module *owner) |
d9a9cdfb | 706 | { |
66942064 | 707 | struct sysfs_schedule_callback_struct *ss, *tmp; |
d9a9cdfb | 708 | |
523ded71 AS |
709 | if (!try_module_get(owner)) |
710 | return -ENODEV; | |
66942064 AC |
711 | |
712 | mutex_lock(&sysfs_workq_mutex); | |
713 | list_for_each_entry_safe(ss, tmp, &sysfs_workq, workq_list) | |
714 | if (ss->kobj == kobj) { | |
d110271e | 715 | module_put(owner); |
66942064 AC |
716 | mutex_unlock(&sysfs_workq_mutex); |
717 | return -EAGAIN; | |
718 | } | |
719 | mutex_unlock(&sysfs_workq_mutex); | |
720 | ||
d110271e | 721 | if (sysfs_workqueue == NULL) { |
086a377e | 722 | sysfs_workqueue = create_singlethread_workqueue("sysfsd"); |
d110271e AC |
723 | if (sysfs_workqueue == NULL) { |
724 | module_put(owner); | |
725 | return -ENOMEM; | |
726 | } | |
727 | } | |
728 | ||
d9a9cdfb | 729 | ss = kmalloc(sizeof(*ss), GFP_KERNEL); |
523ded71 AS |
730 | if (!ss) { |
731 | module_put(owner); | |
d9a9cdfb | 732 | return -ENOMEM; |
523ded71 | 733 | } |
d9a9cdfb AS |
734 | kobject_get(kobj); |
735 | ss->kobj = kobj; | |
736 | ss->func = func; | |
737 | ss->data = data; | |
523ded71 | 738 | ss->owner = owner; |
d9a9cdfb | 739 | INIT_WORK(&ss->work, sysfs_schedule_callback_work); |
66942064 AC |
740 | INIT_LIST_HEAD(&ss->workq_list); |
741 | mutex_lock(&sysfs_workq_mutex); | |
742 | list_add_tail(&ss->workq_list, &sysfs_workq); | |
743 | mutex_unlock(&sysfs_workq_mutex); | |
d110271e | 744 | queue_work(sysfs_workqueue, &ss->work); |
d9a9cdfb AS |
745 | return 0; |
746 | } | |
747 | EXPORT_SYMBOL_GPL(sysfs_schedule_callback); |