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