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 <asm/uaccess.h>
10 #include <asm/semaphore.h>
14 #define to_subsys(k) container_of(k,struct subsystem,kset.kobj)
15 #define to_sattr(a) container_of(a,struct subsys_attribute,attr)
18 * Subsystem file operations.
19 * These operations allow subsystems to have files that can be
23 subsys_attr_show(struct kobject * kobj, struct attribute * attr, char * page)
25 struct subsystem * s = to_subsys(kobj);
26 struct subsys_attribute * sattr = to_sattr(attr);
30 ret = sattr->show(s,page);
35 subsys_attr_store(struct kobject * kobj, struct attribute * attr,
36 const char * page, size_t count)
38 struct subsystem * s = to_subsys(kobj);
39 struct subsys_attribute * sattr = to_sattr(attr);
43 ret = sattr->store(s,page,count);
47 static struct sysfs_ops subsys_sysfs_ops = {
48 .show = subsys_attr_show,
49 .store = subsys_attr_store,
57 struct sysfs_ops * ops;
64 * fill_read_buffer - allocate and fill buffer from object.
65 * @dentry: dentry pointer.
66 * @buffer: data buffer for file.
68 * Allocate @buffer->page, if it hasn't been already, then call the
69 * kobject's show() method to fill the buffer with this attribute's
71 * This is called only once, on the file's first read.
73 static int fill_read_buffer(struct dentry * dentry, struct sysfs_buffer * buffer)
75 struct attribute * attr = to_attr(dentry);
76 struct kobject * kobj = to_kobj(dentry->d_parent);
77 struct sysfs_ops * ops = buffer->ops;
82 buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
86 count = ops->show(kobj,attr,buffer->page);
87 buffer->needs_read_fill = 0;
88 BUG_ON(count > (ssize_t)PAGE_SIZE);
90 buffer->count = count;
98 * flush_read_buffer - push buffer to userspace.
99 * @buffer: data buffer for file.
100 * @buf: user-passed buffer.
101 * @count: number of bytes requested.
102 * @ppos: file position.
104 * Copy the buffer we filled in fill_read_buffer() to userspace.
105 * This is done at the reader's leisure, copying and advancing
106 * the amount they specify each time.
107 * This may be called continuously until the buffer is empty.
109 static int flush_read_buffer(struct sysfs_buffer * buffer, char __user * buf,
110 size_t count, loff_t * ppos)
114 if (*ppos > buffer->count)
117 if (count > (buffer->count - *ppos))
118 count = buffer->count - *ppos;
120 error = copy_to_user(buf,buffer->page + *ppos,count);
123 return error ? -EFAULT : count;
127 * sysfs_read_file - read an attribute.
128 * @file: file pointer.
129 * @buf: buffer to fill.
130 * @count: number of bytes to read.
131 * @ppos: starting offset in file.
133 * Userspace wants to read an attribute file. The attribute descriptor
134 * is in the file's ->d_fsdata. The target object is in the directory's
137 * We call fill_read_buffer() to allocate and fill the buffer from the
138 * object's show() method exactly once (if the read is happening from
139 * the beginning of the file). That should fill the entire buffer with
140 * all the data the object has to offer for that attribute.
141 * We then call flush_read_buffer() to copy the buffer to userspace
142 * in the increments specified.
146 sysfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
148 struct sysfs_buffer * buffer = file->private_data;
152 if (buffer->needs_read_fill) {
153 if ((retval = fill_read_buffer(file->f_dentry,buffer)))
156 pr_debug("%s: count = %d, ppos = %lld, buf = %s\n",
157 __FUNCTION__,count,*ppos,buffer->page);
158 retval = flush_read_buffer(buffer,buf,count,ppos);
166 * fill_write_buffer - copy buffer from userspace.
167 * @buffer: data buffer for file.
168 * @buf: data from user.
169 * @count: number of bytes in @userbuf.
171 * Allocate @buffer->page if it hasn't been already, then
172 * copy the user-supplied buffer into it.
176 fill_write_buffer(struct sysfs_buffer * buffer, const char __user * buf, size_t count)
181 buffer->page = (char *)get_zeroed_page(GFP_KERNEL);
185 if (count >= PAGE_SIZE)
187 error = copy_from_user(buffer->page,buf,count);
188 buffer->needs_read_fill = 1;
189 return error ? -EFAULT : count;
194 * flush_write_buffer - push buffer to kobject.
195 * @dentry: dentry to the attribute
196 * @buffer: data buffer for file.
197 * @count: number of bytes
199 * Get the correct pointers for the kobject and the attribute we're
200 * dealing with, then call the store() method for the attribute,
201 * passing the buffer that we acquired in fill_write_buffer().
205 flush_write_buffer(struct dentry * dentry, struct sysfs_buffer * buffer, size_t count)
207 struct attribute * attr = to_attr(dentry);
208 struct kobject * kobj = to_kobj(dentry->d_parent);
209 struct sysfs_ops * ops = buffer->ops;
211 return ops->store(kobj,attr,buffer->page,count);
216 * sysfs_write_file - write an attribute.
217 * @file: file pointer
218 * @buf: data to write
219 * @count: number of bytes
220 * @ppos: starting offset
222 * Similar to sysfs_read_file(), though working in the opposite direction.
223 * We allocate and fill the data from the user in fill_write_buffer(),
224 * then push it to the kobject in flush_write_buffer().
225 * There is no easy way for us to know if userspace is only doing a partial
226 * write, so we don't support them. We expect the entire buffer to come
227 * on the first write.
228 * Hint: if you're writing a value, first read the file, modify only the
229 * the value you're changing, then write entire buffer back.
233 sysfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
235 struct sysfs_buffer * buffer = file->private_data;
239 len = fill_write_buffer(buffer, buf, count);
241 len = flush_write_buffer(file->f_dentry, buffer, len);
248 static int check_perm(struct inode * inode, struct file * file)
250 struct kobject *kobj = sysfs_get_kobject(file->f_dentry->d_parent);
251 struct attribute * attr = to_attr(file->f_dentry);
252 struct sysfs_buffer * buffer;
253 struct sysfs_ops * ops = NULL;
259 /* Grab the module reference for this attribute if we have one */
260 if (!try_module_get(attr->owner)) {
265 /* if the kobject has no ktype, then we assume that it is a subsystem
266 * itself, and use ops for it.
268 if (kobj->kset && kobj->kset->ktype)
269 ops = kobj->kset->ktype->sysfs_ops;
270 else if (kobj->ktype)
271 ops = kobj->ktype->sysfs_ops;
273 ops = &subsys_sysfs_ops;
275 /* No sysfs operations, either from having no subsystem,
276 * or the subsystem have no operations.
281 /* File needs write support.
282 * The inode's perms must say it's ok,
283 * and we must have a store method.
285 if (file->f_mode & FMODE_WRITE) {
287 if (!(inode->i_mode & S_IWUGO) || !ops->store)
292 /* File needs read support.
293 * The inode's perms must say it's ok, and we there
294 * must be a show method for it.
296 if (file->f_mode & FMODE_READ) {
297 if (!(inode->i_mode & S_IRUGO) || !ops->show)
301 /* No error? Great, allocate a buffer for the file, and store it
302 * it in file->private_data for easy access.
304 buffer = kmalloc(sizeof(struct sysfs_buffer),GFP_KERNEL);
306 memset(buffer,0,sizeof(struct sysfs_buffer));
307 init_MUTEX(&buffer->sem);
308 buffer->needs_read_fill = 1;
310 file->private_data = buffer;
320 module_put(attr->owner);
327 static int sysfs_open_file(struct inode * inode, struct file * filp)
329 return check_perm(inode,filp);
332 static int sysfs_release(struct inode * inode, struct file * filp)
334 struct kobject * kobj = to_kobj(filp->f_dentry->d_parent);
335 struct attribute * attr = to_attr(filp->f_dentry);
336 struct module * owner = attr->owner;
337 struct sysfs_buffer * buffer = filp->private_data;
341 /* After this point, attr should not be accessed. */
346 free_page((unsigned long)buffer->page);
352 struct file_operations sysfs_file_operations = {
353 .read = sysfs_read_file,
354 .write = sysfs_write_file,
355 .llseek = generic_file_llseek,
356 .open = sysfs_open_file,
357 .release = sysfs_release,
361 int sysfs_add_file(struct dentry * dir, const struct attribute * attr, int type)
363 struct sysfs_dirent * parent_sd = dir->d_fsdata;
364 umode_t mode = (attr->mode & S_IALLUGO) | S_IFREG;
367 down(&dir->d_inode->i_sem);
368 error = sysfs_make_dirent(parent_sd, NULL, (void *) attr, mode, type);
369 up(&dir->d_inode->i_sem);
376 * sysfs_create_file - create an attribute file for an object.
377 * @kobj: object we're creating for.
378 * @attr: atrribute descriptor.
381 int sysfs_create_file(struct kobject * kobj, const struct attribute * attr)
383 BUG_ON(!kobj || !kobj->dentry || !attr);
385 return sysfs_add_file(kobj->dentry, attr, SYSFS_KOBJ_ATTR);
391 * sysfs_update_file - update the modified timestamp on an object attribute.
392 * @kobj: object we're acting for.
393 * @attr: attribute descriptor.
395 int sysfs_update_file(struct kobject * kobj, const struct attribute * attr)
397 struct dentry * dir = kobj->dentry;
398 struct dentry * victim;
401 down(&dir->d_inode->i_sem);
402 victim = lookup_one_len(attr->name, dir, strlen(attr->name));
403 if (!IS_ERR(victim)) {
404 /* make sure dentry is really there */
405 if (victim->d_inode &&
406 (victim->d_parent->d_inode == dir->d_inode)) {
407 victim->d_inode->i_mtime = CURRENT_TIME;
408 fsnotify_modify(victim);
411 * Drop reference from initial sysfs_get_dentry().
419 * Drop the reference acquired from sysfs_get_dentry() above.
423 up(&dir->d_inode->i_sem);
430 * sysfs_chmod_file - update the modified mode value on an object attribute.
431 * @kobj: object we're acting for.
432 * @attr: attribute descriptor.
433 * @mode: file permissions.
436 int sysfs_chmod_file(struct kobject *kobj, struct attribute *attr, mode_t mode)
438 struct dentry *dir = kobj->dentry;
439 struct dentry *victim;
440 struct inode * inode;
441 struct iattr newattrs;
444 down(&dir->d_inode->i_sem);
445 victim = lookup_one_len(attr->name, dir, strlen(attr->name));
446 if (!IS_ERR(victim)) {
447 if (victim->d_inode &&
448 (victim->d_parent->d_inode == dir->d_inode)) {
449 inode = victim->d_inode;
451 newattrs.ia_mode = (mode & S_IALLUGO) |
452 (inode->i_mode & ~S_IALLUGO);
453 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
454 res = notify_change(victim, &newattrs);
459 up(&dir->d_inode->i_sem);
463 EXPORT_SYMBOL_GPL(sysfs_chmod_file);
467 * sysfs_remove_file - remove an object attribute.
468 * @kobj: object we're acting for.
469 * @attr: attribute descriptor.
471 * Hash the attribute name and kill the victim.
474 void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr)
476 sysfs_hash_and_remove(kobj->dentry,attr->name);
480 EXPORT_SYMBOL_GPL(sysfs_create_file);
481 EXPORT_SYMBOL_GPL(sysfs_remove_file);
482 EXPORT_SYMBOL_GPL(sysfs_update_file);