2 * file.c - operations for regular (text) files.
5 #include <linux/module.h>
6 #include <linux/fsnotify.h>
7 #include <linux/kobject.h>
8 #include <linux/namei.h>
9 #include <linux/poll.h>
10 #include <linux/limits.h>
11 #include <asm/uaccess.h>
12 #include <asm/semaphore.h>
16 #define to_subsys(k) container_of(k,struct subsystem,kset.kobj)
17 #define to_sattr(a) container_of(a,struct subsys_attribute,attr)
20 * Subsystem file operations.
21 * These operations allow subsystems to have files that can be
25 subsys_attr_show(struct kobject * kobj, struct attribute * attr, char * page)
27 struct subsystem * s = to_subsys(kobj);
28 struct subsys_attribute * sattr = to_sattr(attr);
32 ret = sattr->show(s,page);
37 subsys_attr_store(struct kobject * kobj, struct attribute * attr,
38 const char * page, size_t count)
40 struct subsystem * s = to_subsys(kobj);
41 struct subsys_attribute * sattr = to_sattr(attr);
45 ret = sattr->store(s,page,count);
49 static struct sysfs_ops subsys_sysfs_ops = {
50 .show = subsys_attr_show,
51 .store = subsys_attr_store,
59 struct sysfs_ops * ops;
67 * fill_read_buffer - allocate and fill buffer from object.
68 * @dentry: dentry pointer.
69 * @buffer: data buffer for file.
71 * Allocate @buffer->page, if it hasn't been already, then call the
72 * kobject's show() method to fill the buffer with this attribute's
74 * This is called only once, on the file's first read.
76 static int fill_read_buffer(struct dentry * dentry, struct sysfs_buffer * buffer)
78 struct sysfs_dirent * sd = dentry->d_fsdata;
79 struct attribute * attr = to_attr(dentry);
80 struct kobject * kobj = to_kobj(dentry->d_parent);
81 struct sysfs_ops * ops = buffer->ops;
86 buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
90 buffer->event = atomic_read(&sd->s_event);
91 count = ops->show(kobj,attr,buffer->page);
92 buffer->needs_read_fill = 0;
93 BUG_ON(count > (ssize_t)PAGE_SIZE);
95 buffer->count = count;
103 * flush_read_buffer - push buffer to userspace.
104 * @buffer: data buffer for file.
105 * @buf: user-passed buffer.
106 * @count: number of bytes requested.
107 * @ppos: file position.
109 * Copy the buffer we filled in fill_read_buffer() to userspace.
110 * This is done at the reader's leisure, copying and advancing
111 * the amount they specify each time.
112 * This may be called continuously until the buffer is empty.
114 static int flush_read_buffer(struct sysfs_buffer * buffer, char __user * buf,
115 size_t count, loff_t * ppos)
119 if (*ppos > buffer->count)
122 if (count > (buffer->count - *ppos))
123 count = buffer->count - *ppos;
125 error = copy_to_user(buf,buffer->page + *ppos,count);
128 return error ? -EFAULT : count;
132 * sysfs_read_file - read an attribute.
133 * @file: file pointer.
134 * @buf: buffer to fill.
135 * @count: number of bytes to read.
136 * @ppos: starting offset in file.
138 * Userspace wants to read an attribute file. The attribute descriptor
139 * is in the file's ->d_fsdata. The target object is in the directory's
142 * We call fill_read_buffer() to allocate and fill the buffer from the
143 * object's show() method exactly once (if the read is happening from
144 * the beginning of the file). That should fill the entire buffer with
145 * all the data the object has to offer for that attribute.
146 * We then call flush_read_buffer() to copy the buffer to userspace
147 * in the increments specified.
151 sysfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
153 struct sysfs_buffer * buffer = file->private_data;
157 if (buffer->needs_read_fill) {
158 if ((retval = fill_read_buffer(file->f_dentry,buffer)))
161 pr_debug("%s: count = %d, ppos = %lld, buf = %s\n",
162 __FUNCTION__,count,*ppos,buffer->page);
163 retval = flush_read_buffer(buffer,buf,count,ppos);
171 * fill_write_buffer - copy buffer from userspace.
172 * @buffer: data buffer for file.
173 * @buf: data from user.
174 * @count: number of bytes in @userbuf.
176 * Allocate @buffer->page if it hasn't been already, then
177 * copy the user-supplied buffer into it.
181 fill_write_buffer(struct sysfs_buffer * buffer, const char __user * buf, size_t count)
186 buffer->page = (char *)get_zeroed_page(GFP_KERNEL);
190 if (count >= PAGE_SIZE)
191 count = PAGE_SIZE - 1;
192 error = copy_from_user(buffer->page,buf,count);
193 buffer->needs_read_fill = 1;
194 return error ? -EFAULT : count;
199 * flush_write_buffer - push buffer to kobject.
200 * @dentry: dentry to the attribute
201 * @buffer: data buffer for file.
202 * @count: number of bytes
204 * Get the correct pointers for the kobject and the attribute we're
205 * dealing with, then call the store() method for the attribute,
206 * passing the buffer that we acquired in fill_write_buffer().
210 flush_write_buffer(struct dentry * dentry, struct sysfs_buffer * buffer, size_t count)
212 struct attribute * attr = to_attr(dentry);
213 struct kobject * kobj = to_kobj(dentry->d_parent);
214 struct sysfs_ops * ops = buffer->ops;
216 return ops->store(kobj,attr,buffer->page,count);
221 * sysfs_write_file - write an attribute.
222 * @file: file pointer
223 * @buf: data to write
224 * @count: number of bytes
225 * @ppos: starting offset
227 * Similar to sysfs_read_file(), though working in the opposite direction.
228 * We allocate and fill the data from the user in fill_write_buffer(),
229 * then push it to the kobject in flush_write_buffer().
230 * There is no easy way for us to know if userspace is only doing a partial
231 * write, so we don't support them. We expect the entire buffer to come
232 * on the first write.
233 * Hint: if you're writing a value, first read the file, modify only the
234 * the value you're changing, then write entire buffer back.
238 sysfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
240 struct sysfs_buffer * buffer = file->private_data;
244 len = fill_write_buffer(buffer, buf, count);
246 len = flush_write_buffer(file->f_dentry, buffer, len);
253 static int check_perm(struct inode * inode, struct file * file)
255 struct kobject *kobj = sysfs_get_kobject(file->f_dentry->d_parent);
256 struct attribute * attr = to_attr(file->f_dentry);
257 struct sysfs_buffer * buffer;
258 struct sysfs_ops * ops = NULL;
264 /* Grab the module reference for this attribute if we have one */
265 if (!try_module_get(attr->owner)) {
270 /* if the kobject has no ktype, then we assume that it is a subsystem
271 * itself, and use ops for it.
273 if (kobj->kset && kobj->kset->ktype)
274 ops = kobj->kset->ktype->sysfs_ops;
275 else if (kobj->ktype)
276 ops = kobj->ktype->sysfs_ops;
278 ops = &subsys_sysfs_ops;
280 /* No sysfs operations, either from having no subsystem,
281 * or the subsystem have no operations.
286 /* File needs write support.
287 * The inode's perms must say it's ok,
288 * and we must have a store method.
290 if (file->f_mode & FMODE_WRITE) {
292 if (!(inode->i_mode & S_IWUGO) || !ops->store)
297 /* File needs read support.
298 * The inode's perms must say it's ok, and we there
299 * must be a show method for it.
301 if (file->f_mode & FMODE_READ) {
302 if (!(inode->i_mode & S_IRUGO) || !ops->show)
306 /* No error? Great, allocate a buffer for the file, and store it
307 * it in file->private_data for easy access.
309 buffer = kzalloc(sizeof(struct sysfs_buffer), GFP_KERNEL);
311 init_MUTEX(&buffer->sem);
312 buffer->needs_read_fill = 1;
314 file->private_data = buffer;
324 module_put(attr->owner);
331 char last_sysfs_file[PATH_MAX];
333 static int sysfs_open_file(struct inode * inode, struct file * filp)
335 char *p = d_path(filp->f_dentry, sysfs_mount, last_sysfs_file,
336 sizeof(last_sysfs_file));
338 memmove(last_sysfs_file, p, strlen(p) + 1);
339 return check_perm(inode,filp);
342 static int sysfs_release(struct inode * inode, struct file * filp)
344 struct kobject * kobj = to_kobj(filp->f_dentry->d_parent);
345 struct attribute * attr = to_attr(filp->f_dentry);
346 struct module * owner = attr->owner;
347 struct sysfs_buffer * buffer = filp->private_data;
351 /* After this point, attr should not be accessed. */
356 free_page((unsigned long)buffer->page);
362 /* Sysfs attribute files are pollable. The idea is that you read
363 * the content and then you use 'poll' or 'select' to wait for
364 * the content to change. When the content changes (assuming the
365 * manager for the kobject supports notification), poll will
366 * return POLLERR|POLLPRI, and select will return the fd whether
367 * it is waiting for read, write, or exceptions.
368 * Once poll/select indicates that the value has changed, you
369 * need to close and re-open the file, as simply seeking and reading
370 * again will not get new data, or reset the state of 'poll'.
371 * Reminder: this only works for attributes which actively support
372 * it, and it is not possible to test an attribute from userspace
373 * to see if it supports poll (Nether 'poll' or 'select' return
374 * an appropriate error code). When in doubt, set a suitable timeout value.
376 static unsigned int sysfs_poll(struct file *filp, poll_table *wait)
378 struct sysfs_buffer * buffer = filp->private_data;
379 struct kobject * kobj = to_kobj(filp->f_dentry->d_parent);
380 struct sysfs_dirent * sd = filp->f_dentry->d_fsdata;
383 poll_wait(filp, &kobj->poll, wait);
385 if (buffer->event != atomic_read(&sd->s_event)) {
386 res = POLLERR|POLLPRI;
387 buffer->needs_read_fill = 1;
394 static struct dentry *step_down(struct dentry *dir, const char * name)
398 if (dir == NULL || dir->d_inode == NULL)
401 mutex_lock(&dir->d_inode->i_mutex);
402 de = lookup_one_len(name, dir, strlen(name));
403 mutex_unlock(&dir->d_inode->i_mutex);
407 if (de->d_inode == NULL) {
414 void sysfs_notify(struct kobject * k, char *dir, char *attr)
416 struct dentry *de = k->dentry;
420 de = step_down(de, dir);
422 de = step_down(de, attr);
424 struct sysfs_dirent * sd = de->d_fsdata;
426 atomic_inc(&sd->s_event);
427 wake_up_interruptible(&k->poll);
431 EXPORT_SYMBOL_GPL(sysfs_notify);
433 const struct file_operations sysfs_file_operations = {
434 .read = sysfs_read_file,
435 .write = sysfs_write_file,
436 .llseek = generic_file_llseek,
437 .open = sysfs_open_file,
438 .release = sysfs_release,
443 int sysfs_add_file(struct dentry * dir, const struct attribute * attr, int type)
445 struct sysfs_dirent * parent_sd = dir->d_fsdata;
446 umode_t mode = (attr->mode & S_IALLUGO) | S_IFREG;
449 mutex_lock(&dir->d_inode->i_mutex);
450 if (!sysfs_dirent_exist(parent_sd, attr->name))
451 error = sysfs_make_dirent(parent_sd, NULL, (void *)attr,
453 mutex_unlock(&dir->d_inode->i_mutex);
460 * sysfs_create_file - create an attribute file for an object.
461 * @kobj: object we're creating for.
462 * @attr: atrribute descriptor.
465 int sysfs_create_file(struct kobject * kobj, const struct attribute * attr)
467 BUG_ON(!kobj || !kobj->dentry || !attr);
469 return sysfs_add_file(kobj->dentry, attr, SYSFS_KOBJ_ATTR);
475 * sysfs_update_file - update the modified timestamp on an object attribute.
476 * @kobj: object we're acting for.
477 * @attr: attribute descriptor.
479 int sysfs_update_file(struct kobject * kobj, const struct attribute * attr)
481 struct dentry * dir = kobj->dentry;
482 struct dentry * victim;
485 mutex_lock(&dir->d_inode->i_mutex);
486 victim = lookup_one_len(attr->name, dir, strlen(attr->name));
487 if (!IS_ERR(victim)) {
488 /* make sure dentry is really there */
489 if (victim->d_inode &&
490 (victim->d_parent->d_inode == dir->d_inode)) {
491 victim->d_inode->i_mtime = CURRENT_TIME;
492 fsnotify_modify(victim);
495 * Drop reference from initial sysfs_get_dentry().
503 * Drop the reference acquired from sysfs_get_dentry() above.
507 mutex_unlock(&dir->d_inode->i_mutex);
514 * sysfs_chmod_file - update the modified mode value on an object attribute.
515 * @kobj: object we're acting for.
516 * @attr: attribute descriptor.
517 * @mode: file permissions.
520 int sysfs_chmod_file(struct kobject *kobj, struct attribute *attr, mode_t mode)
522 struct dentry *dir = kobj->dentry;
523 struct dentry *victim;
524 struct inode * inode;
525 struct iattr newattrs;
528 mutex_lock(&dir->d_inode->i_mutex);
529 victim = lookup_one_len(attr->name, dir, strlen(attr->name));
530 if (!IS_ERR(victim)) {
531 if (victim->d_inode &&
532 (victim->d_parent->d_inode == dir->d_inode)) {
533 inode = victim->d_inode;
534 mutex_lock(&inode->i_mutex);
535 newattrs.ia_mode = (mode & S_IALLUGO) |
536 (inode->i_mode & ~S_IALLUGO);
537 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
538 res = notify_change(victim, &newattrs);
539 mutex_unlock(&inode->i_mutex);
543 mutex_unlock(&dir->d_inode->i_mutex);
547 EXPORT_SYMBOL_GPL(sysfs_chmod_file);
551 * sysfs_remove_file - remove an object attribute.
552 * @kobj: object we're acting for.
553 * @attr: attribute descriptor.
555 * Hash the attribute name and kill the victim.
558 void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr)
560 sysfs_hash_and_remove(kobj->dentry,attr->name);
564 EXPORT_SYMBOL_GPL(sysfs_create_file);
565 EXPORT_SYMBOL_GPL(sysfs_remove_file);
566 EXPORT_SYMBOL_GPL(sysfs_update_file);