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
);
92 BUG_ON(count
> (ssize_t
)PAGE_SIZE
);
94 buffer
->count
= count
;
102 * flush_read_buffer - push buffer to userspace.
103 * @buffer: data buffer for file.
104 * @userbuf: user-passed buffer.
105 * @count: number of bytes requested.
106 * @ppos: file position.
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.
113 static int flush_read_buffer(struct sysfs_buffer
* buffer
, char __user
* buf
,
114 size_t count
, loff_t
* ppos
)
118 if (count
> (buffer
->count
- *ppos
))
119 count
= buffer
->count
- *ppos
;
121 error
= copy_to_user(buf
,buffer
->page
+ *ppos
,count
);
124 return error
? -EFAULT
: count
;
128 * sysfs_read_file - read an attribute.
129 * @file: file pointer.
130 * @buf: buffer to fill.
131 * @count: number of bytes to read.
132 * @ppos: starting offset in file.
134 * Userspace wants to read an attribute file. The attribute descriptor
135 * is in the file's ->d_fsdata. The target object is in the directory's
138 * We call fill_read_buffer() to allocate and fill the buffer from the
139 * object's show() method exactly once (if the read is happening from
140 * the beginning of the file). That should fill the entire buffer with
141 * all the data the object has to offer for that attribute.
142 * We then call flush_read_buffer() to copy the buffer to userspace
143 * in the increments specified.
147 sysfs_read_file(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
149 struct sysfs_buffer
* buffer
= file
->private_data
;
153 if ((retval
= fill_read_buffer(file
,buffer
)))
156 pr_debug("%s: count = %d, ppos = %lld, buf = %s\n",
157 __FUNCTION__
,count
,*ppos
,buffer
->page
);
158 return flush_read_buffer(buffer
,buf
,count
,ppos
);
163 * fill_write_buffer - copy buffer from userspace.
164 * @buffer: data buffer for file.
165 * @userbuf: data from user.
166 * @count: number of bytes in @userbuf.
168 * Allocate @buffer->page if it hasn't been already, then
169 * copy the user-supplied buffer into it.
173 fill_write_buffer(struct sysfs_buffer
* buffer
, const char __user
* buf
, size_t count
)
178 buffer
->page
= (char *)get_zeroed_page(GFP_KERNEL
);
182 if (count
>= PAGE_SIZE
)
183 count
= PAGE_SIZE
- 1;
184 error
= copy_from_user(buffer
->page
,buf
,count
);
185 return error
? -EFAULT
: count
;
190 * flush_write_buffer - push buffer to kobject.
191 * @file: file pointer.
192 * @buffer: data buffer for file.
194 * Get the correct pointers for the kobject and the attribute we're
195 * dealing with, then call the store() method for the attribute,
196 * passing the buffer that we acquired in fill_write_buffer().
200 flush_write_buffer(struct file
* file
, struct sysfs_buffer
* buffer
, size_t count
)
202 struct attribute
* attr
= file
->f_dentry
->d_fsdata
;
203 struct kobject
* kobj
= file
->f_dentry
->d_parent
->d_fsdata
;
204 struct sysfs_ops
* ops
= buffer
->ops
;
206 return ops
->store(kobj
,attr
,buffer
->page
,count
);
211 * sysfs_write_file - write an attribute.
212 * @file: file pointer
213 * @buf: data to write
214 * @count: number of bytes
215 * @ppos: starting offset
217 * Similar to sysfs_read_file(), though working in the opposite direction.
218 * We allocate and fill the data from the user in fill_write_buffer(),
219 * then push it to the kobject in flush_write_buffer().
220 * There is no easy way for us to know if userspace is only doing a partial
221 * write, so we don't support them. We expect the entire buffer to come
222 * on the first write.
223 * Hint: if you're writing a value, first read the file, modify only the
224 * the value you're changing, then write entire buffer back.
228 sysfs_write_file(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*ppos
)
230 struct sysfs_buffer
* buffer
= file
->private_data
;
232 count
= fill_write_buffer(buffer
,buf
,count
);
234 count
= flush_write_buffer(file
,buffer
,count
);
240 static int check_perm(struct inode
* inode
, struct file
* file
)
242 struct kobject
*kobj
= sysfs_get_kobject(file
->f_dentry
->d_parent
);
243 struct attribute
* attr
= file
->f_dentry
->d_fsdata
;
244 struct sysfs_buffer
* buffer
;
245 struct sysfs_ops
* ops
= NULL
;
251 /* Grab the module reference for this attribute if we have one */
252 if (!try_module_get(attr
->owner
)) {
257 /* if the kobject has no ktype, then we assume that it is a subsystem
258 * itself, and use ops for it.
260 if (kobj
->kset
&& kobj
->kset
->ktype
)
261 ops
= kobj
->kset
->ktype
->sysfs_ops
;
262 else if (kobj
->ktype
)
263 ops
= kobj
->ktype
->sysfs_ops
;
265 ops
= &subsys_sysfs_ops
;
267 /* No sysfs operations, either from having no subsystem,
268 * or the subsystem have no operations.
273 /* File needs write support.
274 * The inode's perms must say it's ok,
275 * and we must have a store method.
277 if (file
->f_mode
& FMODE_WRITE
) {
279 if (!(inode
->i_mode
& S_IWUGO
) || !ops
->store
)
284 /* File needs read support.
285 * The inode's perms must say it's ok, and we there
286 * must be a show method for it.
288 if (file
->f_mode
& FMODE_READ
) {
289 if (!(inode
->i_mode
& S_IRUGO
) || !ops
->show
)
293 /* No error? Great, allocate a buffer for the file, and store it
294 * it in file->private_data for easy access.
296 buffer
= kmalloc(sizeof(struct sysfs_buffer
),GFP_KERNEL
);
298 memset(buffer
,0,sizeof(struct sysfs_buffer
));
300 file
->private_data
= buffer
;
310 module_put(attr
->owner
);
317 static int sysfs_open_file(struct inode
* inode
, struct file
* filp
)
319 return check_perm(inode
,filp
);
322 static int sysfs_release(struct inode
* inode
, struct file
* filp
)
324 struct kobject
* kobj
= filp
->f_dentry
->d_parent
->d_fsdata
;
325 struct attribute
* attr
= filp
->f_dentry
->d_fsdata
;
326 struct sysfs_buffer
* buffer
= filp
->private_data
;
330 module_put(attr
->owner
);
334 free_page((unsigned long)buffer
->page
);
340 static struct file_operations sysfs_file_operations
= {
341 .read
= sysfs_read_file
,
342 .write
= sysfs_write_file
,
343 .llseek
= generic_file_llseek
,
344 .open
= sysfs_open_file
,
345 .release
= sysfs_release
,
349 int sysfs_add_file(struct dentry
* dir
, const struct attribute
* attr
)
351 struct dentry
* dentry
;
354 down(&dir
->d_inode
->i_sem
);
355 dentry
= sysfs_get_dentry(dir
,attr
->name
);
356 if (!IS_ERR(dentry
)) {
357 error
= sysfs_create(dentry
,
358 (attr
->mode
& S_IALLUGO
) | S_IFREG
,
361 dentry
->d_fsdata
= (void *)attr
;
364 error
= PTR_ERR(dentry
);
365 up(&dir
->d_inode
->i_sem
);
371 * sysfs_create_file - create an attribute file for an object.
372 * @kobj: object we're creating for.
373 * @attr: atrribute descriptor.
376 int sysfs_create_file(struct kobject
* kobj
, const struct attribute
* attr
)
379 return sysfs_add_file(kobj
->dentry
,attr
);
385 * sysfs_update_file - update the modified timestamp on an object attribute.
386 * @kobj: object we're acting for.
387 * @attr: attribute descriptor.
389 * Also call dnotify for the dentry, which lots of userspace programs
392 int sysfs_update_file(struct kobject
* kobj
, const struct attribute
* attr
)
394 struct dentry
* dir
= kobj
->dentry
;
395 struct dentry
* victim
;
398 down(&dir
->d_inode
->i_sem
);
399 victim
= sysfs_get_dentry(dir
, attr
->name
);
400 if (!IS_ERR(victim
)) {
401 /* make sure dentry is really there */
402 if (victim
->d_inode
&&
403 (victim
->d_parent
->d_inode
== dir
->d_inode
)) {
404 victim
->d_inode
->i_mtime
= CURRENT_TIME
;
405 dnotify_parent(victim
, DN_MODIFY
);
408 * Drop reference from initial sysfs_get_dentry().
415 * Drop the reference acquired from sysfs_get_dentry() above.
419 up(&dir
->d_inode
->i_sem
);
426 * sysfs_remove_file - remove an object attribute.
427 * @kobj: object we're acting for.
428 * @attr: attribute descriptor.
430 * Hash the attribute name and kill the victim.
433 void sysfs_remove_file(struct kobject
* kobj
, const struct attribute
* attr
)
435 sysfs_hash_and_remove(kobj
->dentry
,attr
->name
);
439 EXPORT_SYMBOL(sysfs_create_file
);
440 EXPORT_SYMBOL(sysfs_remove_file
);
441 EXPORT_SYMBOL(sysfs_update_file
);