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