2 * file.c - operations for regular (text) files.
5 #include <linux/module.h>
6 #include <linux/dnotify.h>
7 #include <linux/kobject.h>
8 #include <asm/uaccess.h>
12 static struct file_operations sysfs_file_operations;
14 static int init_file(struct inode * inode)
16 inode->i_size = PAGE_SIZE;
17 inode->i_fop = &sysfs_file_operations;
21 #define to_subsys(k) container_of(k,struct subsystem,kset.kobj)
22 #define to_sattr(a) container_of(a,struct subsys_attribute,attr)
25 * Subsystem file operations.
26 * These operations allow subsystems to have files that can be
30 subsys_attr_show(struct kobject * kobj, struct attribute * attr, char * page)
32 struct subsystem * s = to_subsys(kobj);
33 struct subsys_attribute * sattr = to_sattr(attr);
37 ret = sattr->show(s,page);
42 subsys_attr_store(struct kobject * kobj, struct attribute * attr,
43 const char * page, size_t count)
45 struct subsystem * s = to_subsys(kobj);
46 struct subsys_attribute * sattr = to_sattr(attr);
50 ret = sattr->store(s,page,count);
54 static struct sysfs_ops subsys_sysfs_ops = {
55 .show = subsys_attr_show,
56 .store = subsys_attr_store,
64 struct sysfs_ops * ops;
69 * fill_read_buffer - allocate and fill buffer from object.
70 * @file: file pointer.
71 * @buffer: data buffer for file.
73 * Allocate @buffer->page, if it hasn't been already, then call the
74 * kobject's show() method to fill the buffer with this attribute's
76 * This is called only once, on the file's first read.
78 static int fill_read_buffer(struct file * file, struct sysfs_buffer * buffer)
80 struct attribute * attr = file->f_dentry->d_fsdata;
81 struct kobject * kobj = file->f_dentry->d_parent->d_fsdata;
82 struct sysfs_ops * ops = buffer->ops;
87 buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
91 count = ops->show(kobj,attr,buffer->page);
93 buffer->count = count;
101 * flush_read_buffer - push buffer to userspace.
102 * @buffer: data buffer for file.
103 * @userbuf: user-passed buffer.
104 * @count: number of bytes requested.
105 * @ppos: file position.
107 * Copy the buffer we filled in fill_read_buffer() to userspace.
108 * This is done at the reader's leisure, copying and advancing
109 * the amount they specify each time.
110 * This may be called continuously until the buffer is empty.
112 static int flush_read_buffer(struct sysfs_buffer * buffer, char __user * buf,
113 size_t count, loff_t * ppos)
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 ((retval = fill_read_buffer(file,buffer)))
155 pr_debug("%s: count = %d, ppos = %lld, buf = %s\n",
156 __FUNCTION__,count,*ppos,buffer->page);
157 return flush_read_buffer(buffer,buf,count,ppos);
162 * fill_write_buffer - copy buffer from userspace.
163 * @buffer: data buffer for file.
164 * @userbuf: data from user.
165 * @count: number of bytes in @userbuf.
167 * Allocate @buffer->page if it hasn't been already, then
168 * copy the user-supplied buffer into it.
172 fill_write_buffer(struct sysfs_buffer * buffer, const char __user * buf, size_t count)
177 buffer->page = (char *)get_zeroed_page(GFP_KERNEL);
181 if (count >= PAGE_SIZE)
182 count = PAGE_SIZE - 1;
183 error = copy_from_user(buffer->page,buf,count);
184 return error ? -EFAULT : count;
189 * flush_write_buffer - push buffer to kobject.
190 * @file: file pointer.
191 * @buffer: data buffer for file.
193 * Get the correct pointers for the kobject and the attribute we're
194 * dealing with, then call the store() method for the attribute,
195 * passing the buffer that we acquired in fill_write_buffer().
199 flush_write_buffer(struct file * file, struct sysfs_buffer * buffer, size_t count)
201 struct attribute * attr = file->f_dentry->d_fsdata;
202 struct kobject * kobj = file->f_dentry->d_parent->d_fsdata;
203 struct sysfs_ops * ops = buffer->ops;
205 return ops->store(kobj,attr,buffer->page,count);
210 * sysfs_write_file - write an attribute.
211 * @file: file pointer
212 * @buf: data to write
213 * @count: number of bytes
214 * @ppos: starting offset
216 * Similar to sysfs_read_file(), though working in the opposite direction.
217 * We allocate and fill the data from the user in fill_write_buffer(),
218 * then push it to the kobject in flush_write_buffer().
219 * There is no easy way for us to know if userspace is only doing a partial
220 * write, so we don't support them. We expect the entire buffer to come
221 * on the first write.
222 * Hint: if you're writing a value, first read the file, modify only the
223 * the value you're changing, then write entire buffer back.
227 sysfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
229 struct sysfs_buffer * buffer = file->private_data;
231 count = fill_write_buffer(buffer,buf,count);
233 count = flush_write_buffer(file,buffer,count);
239 static int check_perm(struct inode * inode, struct file * file)
241 struct kobject *kobj = sysfs_get_kobject(file->f_dentry->d_parent);
242 struct attribute * attr = file->f_dentry->d_fsdata;
243 struct sysfs_buffer * buffer;
244 struct sysfs_ops * ops = NULL;
250 /* Grab the module reference for this attribute if we have one */
251 if (!try_module_get(attr->owner)) {
256 /* if the kobject has no ktype, then we assume that it is a subsystem
257 * itself, and use ops for it.
259 if (kobj->kset && kobj->kset->ktype)
260 ops = kobj->kset->ktype->sysfs_ops;
261 else if (kobj->ktype)
262 ops = kobj->ktype->sysfs_ops;
264 ops = &subsys_sysfs_ops;
266 /* No sysfs operations, either from having no subsystem,
267 * or the subsystem have no operations.
272 /* File needs write support.
273 * The inode's perms must say it's ok,
274 * and we must have a store method.
276 if (file->f_mode & FMODE_WRITE) {
278 if (!(inode->i_mode & S_IWUGO) || !ops->store)
283 /* File needs read support.
284 * The inode's perms must say it's ok, and we there
285 * must be a show method for it.
287 if (file->f_mode & FMODE_READ) {
288 if (!(inode->i_mode & S_IRUGO) || !ops->show)
292 /* No error? Great, allocate a buffer for the file, and store it
293 * it in file->private_data for easy access.
295 buffer = kmalloc(sizeof(struct sysfs_buffer),GFP_KERNEL);
297 memset(buffer,0,sizeof(struct sysfs_buffer));
299 file->private_data = buffer;
309 module_put(attr->owner);
316 static int sysfs_open_file(struct inode * inode, struct file * filp)
318 return check_perm(inode,filp);
321 static int sysfs_release(struct inode * inode, struct file * filp)
323 struct kobject * kobj = filp->f_dentry->d_parent->d_fsdata;
324 struct attribute * attr = filp->f_dentry->d_fsdata;
325 struct sysfs_buffer * buffer = filp->private_data;
329 module_put(attr->owner);
333 free_page((unsigned long)buffer->page);
339 static struct file_operations sysfs_file_operations = {
340 .read = sysfs_read_file,
341 .write = sysfs_write_file,
342 .llseek = generic_file_llseek,
343 .open = sysfs_open_file,
344 .release = sysfs_release,
348 int sysfs_add_file(struct dentry * dir, const struct attribute * attr)
350 struct dentry * dentry;
353 down(&dir->d_inode->i_sem);
354 dentry = sysfs_get_dentry(dir,attr->name);
355 if (!IS_ERR(dentry)) {
356 error = sysfs_create(dentry,
357 (attr->mode & S_IALLUGO) | S_IFREG,
360 dentry->d_fsdata = (void *)attr;
363 error = PTR_ERR(dentry);
364 up(&dir->d_inode->i_sem);
370 * sysfs_create_file - create an attribute file for an object.
371 * @kobj: object we're creating for.
372 * @attr: atrribute descriptor.
375 int sysfs_create_file(struct kobject * kobj, const struct attribute * attr)
378 return sysfs_add_file(kobj->dentry,attr);
384 * sysfs_update_file - update the modified timestamp on an object attribute.
385 * @kobj: object we're acting for.
386 * @attr: attribute descriptor.
388 * Also call dnotify for the dentry, which lots of userspace programs
391 int sysfs_update_file(struct kobject * kobj, const struct attribute * attr)
393 struct dentry * dir = kobj->dentry;
394 struct dentry * victim;
397 down(&dir->d_inode->i_sem);
398 victim = sysfs_get_dentry(dir, attr->name);
399 if (!IS_ERR(victim)) {
400 /* make sure dentry is really there */
401 if (victim->d_inode &&
402 (victim->d_parent->d_inode == dir->d_inode)) {
403 victim->d_inode->i_mtime = CURRENT_TIME;
404 dnotify_parent(victim, DN_MODIFY);
407 * Drop reference from initial sysfs_get_dentry().
414 * Drop the reference acquired from sysfs_get_dentry() above.
418 up(&dir->d_inode->i_sem);
425 * sysfs_remove_file - remove an object attribute.
426 * @kobj: object we're acting for.
427 * @attr: attribute descriptor.
429 * Hash the attribute name and kill the victim.
432 void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr)
434 sysfs_hash_and_remove(kobj->dentry,attr->name);
438 EXPORT_SYMBOL(sysfs_create_file);
439 EXPORT_SYMBOL(sysfs_remove_file);
440 EXPORT_SYMBOL(sysfs_update_file);