blob: 9cfe53e1e00d0c7128a2532644ad79f9f5c2560d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * file.c - operations for regular (text) files.
3 */
4
5#include <linux/module.h>
Robert Love0eeca282005-07-12 17:06:03 -04006#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/kobject.h>
Christoph Hellwig5f45f1a2005-06-23 00:09:12 -07008#include <linux/namei.h>
NeilBrown4508a7a2006-03-20 17:53:53 +11009#include <linux/poll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <asm/uaccess.h>
11#include <asm/semaphore.h>
12
13#include "sysfs.h"
14
15#define to_subsys(k) container_of(k,struct subsystem,kset.kobj)
16#define to_sattr(a) container_of(a,struct subsys_attribute,attr)
17
Martin Waitz3d410882005-06-23 22:05:21 -070018/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * Subsystem file operations.
20 * These operations allow subsystems to have files that can be
21 * read/written.
22 */
23static ssize_t
24subsys_attr_show(struct kobject * kobj, struct attribute * attr, char * page)
25{
26 struct subsystem * s = to_subsys(kobj);
27 struct subsys_attribute * sattr = to_sattr(attr);
Dmitry Torokhovc76d0ab2005-04-29 01:22:00 -050028 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30 if (sattr->show)
31 ret = sattr->show(s,page);
32 return ret;
33}
34
35static ssize_t
36subsys_attr_store(struct kobject * kobj, struct attribute * attr,
37 const char * page, size_t count)
38{
39 struct subsystem * s = to_subsys(kobj);
40 struct subsys_attribute * sattr = to_sattr(attr);
Dmitry Torokhovc76d0ab2005-04-29 01:22:00 -050041 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43 if (sattr->store)
44 ret = sattr->store(s,page,count);
45 return ret;
46}
47
48static struct sysfs_ops subsys_sysfs_ops = {
49 .show = subsys_attr_show,
50 .store = subsys_attr_store,
51};
52
53
54struct sysfs_buffer {
55 size_t count;
56 loff_t pos;
57 char * page;
58 struct sysfs_ops * ops;
59 struct semaphore sem;
60 int needs_read_fill;
NeilBrown4508a7a2006-03-20 17:53:53 +110061 int event;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062};
63
64
65/**
66 * fill_read_buffer - allocate and fill buffer from object.
67 * @dentry: dentry pointer.
68 * @buffer: data buffer for file.
69 *
70 * Allocate @buffer->page, if it hasn't been already, then call the
71 * kobject's show() method to fill the buffer with this attribute's
72 * data.
73 * This is called only once, on the file's first read.
74 */
75static int fill_read_buffer(struct dentry * dentry, struct sysfs_buffer * buffer)
76{
NeilBrown4508a7a2006-03-20 17:53:53 +110077 struct sysfs_dirent * sd = dentry->d_fsdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 struct attribute * attr = to_attr(dentry);
79 struct kobject * kobj = to_kobj(dentry->d_parent);
80 struct sysfs_ops * ops = buffer->ops;
81 int ret = 0;
82 ssize_t count;
83
84 if (!buffer->page)
85 buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
86 if (!buffer->page)
87 return -ENOMEM;
88
NeilBrown4508a7a2006-03-20 17:53:53 +110089 buffer->event = atomic_read(&sd->s_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 count = ops->show(kobj,attr,buffer->page);
91 buffer->needs_read_fill = 0;
92 BUG_ON(count > (ssize_t)PAGE_SIZE);
93 if (count >= 0)
94 buffer->count = count;
95 else
96 ret = count;
97 return ret;
98}
99
100
101/**
102 * flush_read_buffer - push buffer to userspace.
103 * @buffer: data buffer for file.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700104 * @buf: user-passed buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 * @count: number of bytes requested.
106 * @ppos: file position.
107 *
108 * Copy the buffer we filled in fill_read_buffer() to userspace.
109 * This is done at the reader's leisure, copying and advancing
110 * the amount they specify each time.
111 * This may be called continuously until the buffer is empty.
112 */
113static int flush_read_buffer(struct sysfs_buffer * buffer, char __user * buf,
114 size_t count, loff_t * ppos)
115{
116 int error;
117
118 if (*ppos > buffer->count)
119 return 0;
120
121 if (count > (buffer->count - *ppos))
122 count = buffer->count - *ppos;
123
124 error = copy_to_user(buf,buffer->page + *ppos,count);
125 if (!error)
126 *ppos += count;
127 return error ? -EFAULT : count;
128}
129
130/**
131 * sysfs_read_file - read an attribute.
132 * @file: file pointer.
133 * @buf: buffer to fill.
134 * @count: number of bytes to read.
135 * @ppos: starting offset in file.
136 *
137 * Userspace wants to read an attribute file. The attribute descriptor
138 * is in the file's ->d_fsdata. The target object is in the directory's
139 * ->d_fsdata.
140 *
141 * We call fill_read_buffer() to allocate and fill the buffer from the
142 * object's show() method exactly once (if the read is happening from
143 * the beginning of the file). That should fill the entire buffer with
144 * all the data the object has to offer for that attribute.
145 * We then call flush_read_buffer() to copy the buffer to userspace
146 * in the increments specified.
147 */
148
149static ssize_t
150sysfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
151{
152 struct sysfs_buffer * buffer = file->private_data;
153 ssize_t retval = 0;
154
155 down(&buffer->sem);
156 if (buffer->needs_read_fill) {
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800157 if ((retval = fill_read_buffer(file->f_path.dentry,buffer)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 goto out;
159 }
Zach Brown5c1fdf42006-10-03 01:16:06 -0700160 pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",
161 __FUNCTION__, count, *ppos, buffer->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 retval = flush_read_buffer(buffer,buf,count,ppos);
163out:
164 up(&buffer->sem);
165 return retval;
166}
167
168
169/**
170 * fill_write_buffer - copy buffer from userspace.
171 * @buffer: data buffer for file.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700172 * @buf: data from user.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 * @count: number of bytes in @userbuf.
174 *
175 * Allocate @buffer->page if it hasn't been already, then
176 * copy the user-supplied buffer into it.
177 */
178
179static int
180fill_write_buffer(struct sysfs_buffer * buffer, const char __user * buf, size_t count)
181{
182 int error;
183
184 if (!buffer->page)
185 buffer->page = (char *)get_zeroed_page(GFP_KERNEL);
186 if (!buffer->page)
187 return -ENOMEM;
188
189 if (count >= PAGE_SIZE)
Greg Kroah-Hartman6e0dd742006-03-31 15:37:06 -0800190 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 error = copy_from_user(buffer->page,buf,count);
192 buffer->needs_read_fill = 1;
Thomas Maier035ed7a2006-10-22 19:17:47 +0200193 /* if buf is assumed to contain a string, terminate it by \0,
194 so e.g. sscanf() can scan the string easily */
195 buffer->page[count] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return error ? -EFAULT : count;
197}
198
199
200/**
201 * flush_write_buffer - push buffer to kobject.
Martin Waitz3d410882005-06-23 22:05:21 -0700202 * @dentry: dentry to the attribute
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 * @buffer: data buffer for file.
Martin Waitz3d410882005-06-23 22:05:21 -0700204 * @count: number of bytes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 *
206 * Get the correct pointers for the kobject and the attribute we're
207 * dealing with, then call the store() method for the attribute,
208 * passing the buffer that we acquired in fill_write_buffer().
209 */
210
211static int
212flush_write_buffer(struct dentry * dentry, struct sysfs_buffer * buffer, size_t count)
213{
214 struct attribute * attr = to_attr(dentry);
215 struct kobject * kobj = to_kobj(dentry->d_parent);
216 struct sysfs_ops * ops = buffer->ops;
217
218 return ops->store(kobj,attr,buffer->page,count);
219}
220
221
222/**
223 * sysfs_write_file - write an attribute.
224 * @file: file pointer
225 * @buf: data to write
226 * @count: number of bytes
227 * @ppos: starting offset
228 *
229 * Similar to sysfs_read_file(), though working in the opposite direction.
230 * We allocate and fill the data from the user in fill_write_buffer(),
231 * then push it to the kobject in flush_write_buffer().
232 * There is no easy way for us to know if userspace is only doing a partial
233 * write, so we don't support them. We expect the entire buffer to come
234 * on the first write.
235 * Hint: if you're writing a value, first read the file, modify only the
236 * the value you're changing, then write entire buffer back.
237 */
238
239static ssize_t
240sysfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
241{
242 struct sysfs_buffer * buffer = file->private_data;
243 ssize_t len;
244
245 down(&buffer->sem);
246 len = fill_write_buffer(buffer, buf, count);
247 if (len > 0)
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800248 len = flush_write_buffer(file->f_path.dentry, buffer, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 if (len > 0)
250 *ppos += len;
251 up(&buffer->sem);
252 return len;
253}
254
255static int check_perm(struct inode * inode, struct file * file)
256{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800257 struct kobject *kobj = sysfs_get_kobject(file->f_path.dentry->d_parent);
258 struct attribute * attr = to_attr(file->f_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 struct sysfs_buffer * buffer;
260 struct sysfs_ops * ops = NULL;
261 int error = 0;
262
263 if (!kobj || !attr)
264 goto Einval;
265
266 /* Grab the module reference for this attribute if we have one */
267 if (!try_module_get(attr->owner)) {
268 error = -ENODEV;
269 goto Done;
270 }
271
272 /* if the kobject has no ktype, then we assume that it is a subsystem
273 * itself, and use ops for it.
274 */
275 if (kobj->kset && kobj->kset->ktype)
276 ops = kobj->kset->ktype->sysfs_ops;
277 else if (kobj->ktype)
278 ops = kobj->ktype->sysfs_ops;
279 else
280 ops = &subsys_sysfs_ops;
281
282 /* No sysfs operations, either from having no subsystem,
283 * or the subsystem have no operations.
284 */
285 if (!ops)
286 goto Eaccess;
287
288 /* File needs write support.
289 * The inode's perms must say it's ok,
290 * and we must have a store method.
291 */
292 if (file->f_mode & FMODE_WRITE) {
293
294 if (!(inode->i_mode & S_IWUGO) || !ops->store)
295 goto Eaccess;
296
297 }
298
299 /* File needs read support.
300 * The inode's perms must say it's ok, and we there
301 * must be a show method for it.
302 */
303 if (file->f_mode & FMODE_READ) {
304 if (!(inode->i_mode & S_IRUGO) || !ops->show)
305 goto Eaccess;
306 }
307
308 /* No error? Great, allocate a buffer for the file, and store it
309 * it in file->private_data for easy access.
310 */
Eric Sesterhenn58d49282006-02-22 11:18:15 +0100311 buffer = kzalloc(sizeof(struct sysfs_buffer), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (buffer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 init_MUTEX(&buffer->sem);
314 buffer->needs_read_fill = 1;
315 buffer->ops = ops;
316 file->private_data = buffer;
317 } else
318 error = -ENOMEM;
319 goto Done;
320
321 Einval:
322 error = -EINVAL;
323 goto Done;
324 Eaccess:
325 error = -EACCES;
326 module_put(attr->owner);
327 Done:
328 if (error && kobj)
329 kobject_put(kobj);
330 return error;
331}
332
333static int sysfs_open_file(struct inode * inode, struct file * filp)
334{
335 return check_perm(inode,filp);
336}
337
338static int sysfs_release(struct inode * inode, struct file * filp)
339{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800340 struct kobject * kobj = to_kobj(filp->f_path.dentry->d_parent);
341 struct attribute * attr = to_attr(filp->f_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 struct module * owner = attr->owner;
343 struct sysfs_buffer * buffer = filp->private_data;
344
345 if (kobj)
346 kobject_put(kobj);
347 /* After this point, attr should not be accessed. */
348 module_put(owner);
349
350 if (buffer) {
351 if (buffer->page)
352 free_page((unsigned long)buffer->page);
353 kfree(buffer);
354 }
355 return 0;
356}
357
NeilBrown4508a7a2006-03-20 17:53:53 +1100358/* Sysfs attribute files are pollable. The idea is that you read
359 * the content and then you use 'poll' or 'select' to wait for
360 * the content to change. When the content changes (assuming the
361 * manager for the kobject supports notification), poll will
362 * return POLLERR|POLLPRI, and select will return the fd whether
363 * it is waiting for read, write, or exceptions.
364 * Once poll/select indicates that the value has changed, you
365 * need to close and re-open the file, as simply seeking and reading
366 * again will not get new data, or reset the state of 'poll'.
367 * Reminder: this only works for attributes which actively support
368 * it, and it is not possible to test an attribute from userspace
369 * to see if it supports poll (Nether 'poll' or 'select' return
370 * an appropriate error code). When in doubt, set a suitable timeout value.
371 */
372static unsigned int sysfs_poll(struct file *filp, poll_table *wait)
373{
374 struct sysfs_buffer * buffer = filp->private_data;
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800375 struct kobject * kobj = to_kobj(filp->f_path.dentry->d_parent);
376 struct sysfs_dirent * sd = filp->f_path.dentry->d_fsdata;
NeilBrown4508a7a2006-03-20 17:53:53 +1100377 int res = 0;
378
379 poll_wait(filp, &kobj->poll, wait);
380
381 if (buffer->event != atomic_read(&sd->s_event)) {
382 res = POLLERR|POLLPRI;
383 buffer->needs_read_fill = 1;
384 }
385
386 return res;
387}
388
389
390static struct dentry *step_down(struct dentry *dir, const char * name)
391{
392 struct dentry * de;
393
394 if (dir == NULL || dir->d_inode == NULL)
395 return NULL;
396
397 mutex_lock(&dir->d_inode->i_mutex);
398 de = lookup_one_len(name, dir, strlen(name));
399 mutex_unlock(&dir->d_inode->i_mutex);
400 dput(dir);
401 if (IS_ERR(de))
402 return NULL;
403 if (de->d_inode == NULL) {
404 dput(de);
405 return NULL;
406 }
407 return de;
408}
409
410void sysfs_notify(struct kobject * k, char *dir, char *attr)
411{
412 struct dentry *de = k->dentry;
413 if (de)
414 dget(de);
415 if (de && dir)
416 de = step_down(de, dir);
417 if (de && attr)
418 de = step_down(de, attr);
419 if (de) {
420 struct sysfs_dirent * sd = de->d_fsdata;
421 if (sd)
422 atomic_inc(&sd->s_event);
423 wake_up_interruptible(&k->poll);
424 dput(de);
425 }
426}
427EXPORT_SYMBOL_GPL(sysfs_notify);
428
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800429const struct file_operations sysfs_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 .read = sysfs_read_file,
431 .write = sysfs_write_file,
432 .llseek = generic_file_llseek,
433 .open = sysfs_open_file,
434 .release = sysfs_release,
NeilBrown4508a7a2006-03-20 17:53:53 +1100435 .poll = sysfs_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436};
437
438
439int sysfs_add_file(struct dentry * dir, const struct attribute * attr, int type)
440{
441 struct sysfs_dirent * parent_sd = dir->d_fsdata;
442 umode_t mode = (attr->mode & S_IALLUGO) | S_IFREG;
Maneesh Sonic5168652006-03-09 19:40:14 +0530443 int error = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800445 mutex_lock(&dir->d_inode->i_mutex);
Maneesh Sonic5168652006-03-09 19:40:14 +0530446 if (!sysfs_dirent_exist(parent_sd, attr->name))
447 error = sysfs_make_dirent(parent_sd, NULL, (void *)attr,
448 mode, type);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800449 mutex_unlock(&dir->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 return error;
452}
453
454
455/**
456 * sysfs_create_file - create an attribute file for an object.
457 * @kobj: object we're creating for.
458 * @attr: atrribute descriptor.
459 */
460
461int sysfs_create_file(struct kobject * kobj, const struct attribute * attr)
462{
463 BUG_ON(!kobj || !kobj->dentry || !attr);
464
465 return sysfs_add_file(kobj->dentry, attr, SYSFS_KOBJ_ATTR);
466
467}
468
469
470/**
471 * sysfs_update_file - update the modified timestamp on an object attribute.
472 * @kobj: object we're acting for.
473 * @attr: attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 */
475int sysfs_update_file(struct kobject * kobj, const struct attribute * attr)
476{
477 struct dentry * dir = kobj->dentry;
478 struct dentry * victim;
479 int res = -ENOENT;
480
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800481 mutex_lock(&dir->d_inode->i_mutex);
Christoph Hellwig5f45f1a2005-06-23 00:09:12 -0700482 victim = lookup_one_len(attr->name, dir, strlen(attr->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (!IS_ERR(victim)) {
484 /* make sure dentry is really there */
485 if (victim->d_inode &&
486 (victim->d_parent->d_inode == dir->d_inode)) {
487 victim->d_inode->i_mtime = CURRENT_TIME;
Robert Love0eeca282005-07-12 17:06:03 -0400488 fsnotify_modify(victim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 res = 0;
490 } else
491 d_drop(victim);
492
493 /**
Hidetoshi Seto97a50182006-09-20 16:49:02 +0900494 * Drop the reference acquired from lookup_one_len() above.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 */
496 dput(victim);
497 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800498 mutex_unlock(&dir->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 return res;
501}
502
503
504/**
Kay Sievers31e5abe2005-04-18 21:57:32 -0700505 * sysfs_chmod_file - update the modified mode value on an object attribute.
506 * @kobj: object we're acting for.
507 * @attr: attribute descriptor.
508 * @mode: file permissions.
509 *
510 */
511int sysfs_chmod_file(struct kobject *kobj, struct attribute *attr, mode_t mode)
512{
513 struct dentry *dir = kobj->dentry;
514 struct dentry *victim;
Maneesh Sonibc062b12005-07-29 12:13:35 -0700515 struct inode * inode;
516 struct iattr newattrs;
Kay Sievers31e5abe2005-04-18 21:57:32 -0700517 int res = -ENOENT;
518
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800519 mutex_lock(&dir->d_inode->i_mutex);
Christoph Hellwig5f45f1a2005-06-23 00:09:12 -0700520 victim = lookup_one_len(attr->name, dir, strlen(attr->name));
Kay Sievers31e5abe2005-04-18 21:57:32 -0700521 if (!IS_ERR(victim)) {
522 if (victim->d_inode &&
523 (victim->d_parent->d_inode == dir->d_inode)) {
Maneesh Sonibc062b12005-07-29 12:13:35 -0700524 inode = victim->d_inode;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800525 mutex_lock(&inode->i_mutex);
Maneesh Sonibc062b12005-07-29 12:13:35 -0700526 newattrs.ia_mode = (mode & S_IALLUGO) |
527 (inode->i_mode & ~S_IALLUGO);
528 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
529 res = notify_change(victim, &newattrs);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800530 mutex_unlock(&inode->i_mutex);
Kay Sievers31e5abe2005-04-18 21:57:32 -0700531 }
Maneesh Sonibc062b12005-07-29 12:13:35 -0700532 dput(victim);
Kay Sievers31e5abe2005-04-18 21:57:32 -0700533 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800534 mutex_unlock(&dir->d_inode->i_mutex);
Kay Sievers31e5abe2005-04-18 21:57:32 -0700535
536 return res;
537}
538EXPORT_SYMBOL_GPL(sysfs_chmod_file);
539
540
541/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 * sysfs_remove_file - remove an object attribute.
543 * @kobj: object we're acting for.
544 * @attr: attribute descriptor.
545 *
546 * Hash the attribute name and kill the victim.
547 */
548
549void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr)
550{
551 sysfs_hash_and_remove(kobj->dentry,attr->name);
552}
553
554
555EXPORT_SYMBOL_GPL(sysfs_create_file);
556EXPORT_SYMBOL_GPL(sysfs_remove_file);
557EXPORT_SYMBOL_GPL(sysfs_update_file);