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/list.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,
55 * add_to_collection - add buffer to a collection
56 * @buffer: buffer to be added
57 * @node inode of set to add to
61 add_to_collection(struct sysfs_buffer *buffer, struct inode *node)
63 struct sysfs_buffer_collection *set = node->i_private;
65 mutex_lock(&node->i_mutex);
66 list_add(&buffer->associates, &set->associates);
67 mutex_unlock(&node->i_mutex);
71 remove_from_collection(struct sysfs_buffer *buffer, struct inode *node)
73 mutex_lock(&node->i_mutex);
74 list_del(&buffer->associates);
75 mutex_unlock(&node->i_mutex);
79 * fill_read_buffer - allocate and fill buffer from object.
80 * @dentry: dentry pointer.
81 * @buffer: data buffer for file.
83 * Allocate @buffer->page, if it hasn't been already, then call the
84 * kobject's show() method to fill the buffer with this attribute's
86 * This is called only once, on the file's first read.
88 static int fill_read_buffer(struct dentry * dentry, struct sysfs_buffer * buffer)
90 struct sysfs_dirent * sd = dentry->d_fsdata;
91 struct attribute * attr = to_attr(dentry);
92 struct kobject * kobj = to_kobj(dentry->d_parent);
93 struct sysfs_ops * ops = buffer->ops;
98 buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
102 buffer->event = atomic_read(&sd->s_event);
103 count = ops->show(kobj,attr,buffer->page);
104 buffer->needs_read_fill = 0;
105 BUG_ON(count > (ssize_t)PAGE_SIZE);
107 buffer->count = count;
115 * flush_read_buffer - push buffer to userspace.
116 * @buffer: data buffer for file.
117 * @buf: user-passed buffer.
118 * @count: number of bytes requested.
119 * @ppos: file position.
121 * Copy the buffer we filled in fill_read_buffer() to userspace.
122 * This is done at the reader's leisure, copying and advancing
123 * the amount they specify each time.
124 * This may be called continuously until the buffer is empty.
126 static int flush_read_buffer(struct sysfs_buffer * buffer, char __user * buf,
127 size_t count, loff_t * ppos)
131 if (*ppos > buffer->count)
134 if (count > (buffer->count - *ppos))
135 count = buffer->count - *ppos;
137 error = copy_to_user(buf,buffer->page + *ppos,count);
140 return error ? -EFAULT : count;
144 * sysfs_read_file - read an attribute.
145 * @file: file pointer.
146 * @buf: buffer to fill.
147 * @count: number of bytes to read.
148 * @ppos: starting offset in file.
150 * Userspace wants to read an attribute file. The attribute descriptor
151 * is in the file's ->d_fsdata. The target object is in the directory's
154 * We call fill_read_buffer() to allocate and fill the buffer from the
155 * object's show() method exactly once (if the read is happening from
156 * the beginning of the file). That should fill the entire buffer with
157 * all the data the object has to offer for that attribute.
158 * We then call flush_read_buffer() to copy the buffer to userspace
159 * in the increments specified.
163 sysfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
165 struct sysfs_buffer * buffer = file->private_data;
169 if (buffer->orphaned) {
173 if (buffer->needs_read_fill) {
174 if ((retval = fill_read_buffer(file->f_path.dentry,buffer)))
177 pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",
178 __FUNCTION__, count, *ppos, buffer->page);
179 retval = flush_read_buffer(buffer,buf,count,ppos);
186 * fill_write_buffer - copy buffer from userspace.
187 * @buffer: data buffer for file.
188 * @buf: data from user.
189 * @count: number of bytes in @userbuf.
191 * Allocate @buffer->page if it hasn't been already, then
192 * copy the user-supplied buffer into it.
196 fill_write_buffer(struct sysfs_buffer * buffer, const char __user * buf, size_t count)
201 buffer->page = (char *)get_zeroed_page(GFP_KERNEL);
205 if (count >= PAGE_SIZE)
206 count = PAGE_SIZE - 1;
207 error = copy_from_user(buffer->page,buf,count);
208 buffer->needs_read_fill = 1;
209 /* if buf is assumed to contain a string, terminate it by \0,
210 so e.g. sscanf() can scan the string easily */
211 buffer->page[count] = 0;
212 return error ? -EFAULT : count;
217 * flush_write_buffer - push buffer to kobject.
218 * @dentry: dentry to the attribute
219 * @buffer: data buffer for file.
220 * @count: number of bytes
222 * Get the correct pointers for the kobject and the attribute we're
223 * dealing with, then call the store() method for the attribute,
224 * passing the buffer that we acquired in fill_write_buffer().
228 flush_write_buffer(struct dentry * dentry, struct sysfs_buffer * buffer, size_t count)
230 struct attribute * attr = to_attr(dentry);
231 struct kobject * kobj = to_kobj(dentry->d_parent);
232 struct sysfs_ops * ops = buffer->ops;
234 return ops->store(kobj,attr,buffer->page,count);
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
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.
256 sysfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
258 struct sysfs_buffer * buffer = file->private_data;
262 if (buffer->orphaned) {
266 len = fill_write_buffer(buffer, buf, count);
268 len = flush_write_buffer(file->f_path.dentry, buffer, len);
276 static int sysfs_open_file(struct inode *inode, struct file *file)
278 struct kobject *kobj = sysfs_get_kobject(file->f_path.dentry->d_parent);
279 struct attribute * attr = to_attr(file->f_path.dentry);
280 struct sysfs_buffer_collection *set;
281 struct sysfs_buffer * buffer;
282 struct sysfs_ops * ops = NULL;
288 /* Grab the module reference for this attribute if we have one */
289 if (!try_module_get(attr->owner)) {
294 /* if the kobject has no ktype, then we assume that it is a subsystem
295 * itself, and use ops for it.
297 if (kobj->kset && kobj->kset->ktype)
298 ops = kobj->kset->ktype->sysfs_ops;
299 else if (kobj->ktype)
300 ops = kobj->ktype->sysfs_ops;
302 ops = &subsys_sysfs_ops;
304 /* No sysfs operations, either from having no subsystem,
305 * or the subsystem have no operations.
310 /* make sure we have a collection to add our buffers to */
311 mutex_lock(&inode->i_mutex);
312 if (!(set = inode->i_private)) {
313 if (!(set = inode->i_private = kmalloc(sizeof(struct sysfs_buffer_collection), GFP_KERNEL))) {
317 INIT_LIST_HEAD(&set->associates);
320 mutex_unlock(&inode->i_mutex);
322 /* File needs write support.
323 * The inode's perms must say it's ok,
324 * and we must have a store method.
326 if (file->f_mode & FMODE_WRITE) {
328 if (!(inode->i_mode & S_IWUGO) || !ops->store)
333 /* File needs read support.
334 * The inode's perms must say it's ok, and we there
335 * must be a show method for it.
337 if (file->f_mode & FMODE_READ) {
338 if (!(inode->i_mode & S_IRUGO) || !ops->show)
342 /* No error? Great, allocate a buffer for the file, and store it
343 * it in file->private_data for easy access.
345 buffer = kzalloc(sizeof(struct sysfs_buffer), GFP_KERNEL);
347 INIT_LIST_HEAD(&buffer->associates);
348 init_MUTEX(&buffer->sem);
349 buffer->needs_read_fill = 1;
351 add_to_collection(buffer, inode);
352 file->private_data = buffer;
362 module_put(attr->owner);
369 static int sysfs_release(struct inode * inode, struct file * filp)
371 struct kobject * kobj = to_kobj(filp->f_path.dentry->d_parent);
372 struct attribute * attr = to_attr(filp->f_path.dentry);
373 struct module * owner = attr->owner;
374 struct sysfs_buffer * buffer = filp->private_data;
377 remove_from_collection(buffer, inode);
379 /* After this point, attr should not be accessed. */
384 free_page((unsigned long)buffer->page);
390 /* Sysfs attribute files are pollable. The idea is that you read
391 * the content and then you use 'poll' or 'select' to wait for
392 * the content to change. When the content changes (assuming the
393 * manager for the kobject supports notification), poll will
394 * return POLLERR|POLLPRI, and select will return the fd whether
395 * it is waiting for read, write, or exceptions.
396 * Once poll/select indicates that the value has changed, you
397 * need to close and re-open the file, as simply seeking and reading
398 * again will not get new data, or reset the state of 'poll'.
399 * Reminder: this only works for attributes which actively support
400 * it, and it is not possible to test an attribute from userspace
401 * to see if it supports poll (Nether 'poll' or 'select' return
402 * an appropriate error code). When in doubt, set a suitable timeout value.
404 static unsigned int sysfs_poll(struct file *filp, poll_table *wait)
406 struct sysfs_buffer * buffer = filp->private_data;
407 struct kobject * kobj = to_kobj(filp->f_path.dentry->d_parent);
408 struct sysfs_dirent * sd = filp->f_path.dentry->d_fsdata;
411 poll_wait(filp, &kobj->poll, wait);
413 if (buffer->event != atomic_read(&sd->s_event)) {
414 res = POLLERR|POLLPRI;
415 buffer->needs_read_fill = 1;
422 static struct dentry *step_down(struct dentry *dir, const char * name)
426 if (dir == NULL || dir->d_inode == NULL)
429 mutex_lock(&dir->d_inode->i_mutex);
430 de = lookup_one_len(name, dir, strlen(name));
431 mutex_unlock(&dir->d_inode->i_mutex);
435 if (de->d_inode == NULL) {
442 void sysfs_notify(struct kobject * k, char *dir, char *attr)
444 struct dentry *de = k->dentry;
448 de = step_down(de, dir);
450 de = step_down(de, attr);
452 struct sysfs_dirent * sd = de->d_fsdata;
454 atomic_inc(&sd->s_event);
455 wake_up_interruptible(&k->poll);
459 EXPORT_SYMBOL_GPL(sysfs_notify);
461 const struct file_operations sysfs_file_operations = {
462 .read = sysfs_read_file,
463 .write = sysfs_write_file,
464 .llseek = generic_file_llseek,
465 .open = sysfs_open_file,
466 .release = sysfs_release,
471 int sysfs_add_file(struct dentry * dir, const struct attribute * attr, int type)
473 struct sysfs_dirent * parent_sd = dir->d_fsdata;
474 umode_t mode = (attr->mode & S_IALLUGO) | S_IFREG;
477 mutex_lock(&dir->d_inode->i_mutex);
478 if (!sysfs_dirent_exist(parent_sd, attr->name))
479 error = sysfs_make_dirent(parent_sd, NULL, (void *)attr,
481 mutex_unlock(&dir->d_inode->i_mutex);
488 * sysfs_create_file - create an attribute file for an object.
489 * @kobj: object we're creating for.
490 * @attr: atrribute descriptor.
493 int sysfs_create_file(struct kobject * kobj, const struct attribute * attr)
495 BUG_ON(!kobj || !kobj->dentry || !attr);
497 return sysfs_add_file(kobj->dentry, attr, SYSFS_KOBJ_ATTR);
503 * sysfs_update_file - update the modified timestamp on an object attribute.
504 * @kobj: object we're acting for.
505 * @attr: attribute descriptor.
507 int sysfs_update_file(struct kobject * kobj, const struct attribute * attr)
509 struct dentry * dir = kobj->dentry;
510 struct dentry * victim;
513 mutex_lock(&dir->d_inode->i_mutex);
514 victim = lookup_one_len(attr->name, dir, strlen(attr->name));
515 if (!IS_ERR(victim)) {
516 /* make sure dentry is really there */
517 if (victim->d_inode &&
518 (victim->d_parent->d_inode == dir->d_inode)) {
519 victim->d_inode->i_mtime = CURRENT_TIME;
520 fsnotify_modify(victim);
526 * Drop the reference acquired from lookup_one_len() above.
530 mutex_unlock(&dir->d_inode->i_mutex);
537 * sysfs_chmod_file - update the modified mode value on an object attribute.
538 * @kobj: object we're acting for.
539 * @attr: attribute descriptor.
540 * @mode: file permissions.
543 int sysfs_chmod_file(struct kobject *kobj, struct attribute *attr, mode_t mode)
545 struct dentry *dir = kobj->dentry;
546 struct dentry *victim;
547 struct inode * inode;
548 struct iattr newattrs;
551 mutex_lock(&dir->d_inode->i_mutex);
552 victim = lookup_one_len(attr->name, dir, strlen(attr->name));
553 if (!IS_ERR(victim)) {
554 if (victim->d_inode &&
555 (victim->d_parent->d_inode == dir->d_inode)) {
556 inode = victim->d_inode;
557 mutex_lock(&inode->i_mutex);
558 newattrs.ia_mode = (mode & S_IALLUGO) |
559 (inode->i_mode & ~S_IALLUGO);
560 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
561 res = notify_change(victim, &newattrs);
562 mutex_unlock(&inode->i_mutex);
566 mutex_unlock(&dir->d_inode->i_mutex);
570 EXPORT_SYMBOL_GPL(sysfs_chmod_file);
574 * sysfs_remove_file - remove an object attribute.
575 * @kobj: object we're acting for.
576 * @attr: attribute descriptor.
578 * Hash the attribute name and kill the victim.
581 void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr)
583 sysfs_hash_and_remove(kobj->dentry, attr->name);
587 EXPORT_SYMBOL_GPL(sysfs_create_file);
588 EXPORT_SYMBOL_GPL(sysfs_remove_file);
589 EXPORT_SYMBOL_GPL(sysfs_update_file);