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>
9 #include <asm/semaphore.h>
13 #define to_subsys(k) container_of(k,struct subsystem,kset.kobj)
14 #define to_sattr(a) container_of(a,struct subsys_attribute,attr)
17 * Subsystem file operations.
18 * These operations allow subsystems to have files that can be
22 subsys_attr_show(struct kobject
* kobj
, struct attribute
* attr
, char * page
)
24 struct subsystem
* s
= to_subsys(kobj
);
25 struct subsys_attribute
* sattr
= to_sattr(attr
);
29 ret
= sattr
->show(s
,page
);
34 subsys_attr_store(struct kobject
* kobj
, struct attribute
* attr
,
35 const char * page
, size_t count
)
37 struct subsystem
* s
= to_subsys(kobj
);
38 struct subsys_attribute
* sattr
= to_sattr(attr
);
42 ret
= sattr
->store(s
,page
,count
);
46 static struct sysfs_ops subsys_sysfs_ops
= {
47 .show
= subsys_attr_show
,
48 .store
= subsys_attr_store
,
56 struct sysfs_ops
* ops
;
63 * fill_read_buffer - allocate and fill buffer from object.
64 * @dentry: dentry pointer.
65 * @buffer: data buffer for file.
67 * Allocate @buffer->page, if it hasn't been already, then call the
68 * kobject's show() method to fill the buffer with this attribute's
70 * This is called only once, on the file's first read.
72 static int fill_read_buffer(struct dentry
* dentry
, struct sysfs_buffer
* buffer
)
74 struct attribute
* attr
= to_attr(dentry
);
75 struct kobject
* kobj
= to_kobj(dentry
->d_parent
);
76 struct sysfs_ops
* ops
= buffer
->ops
;
81 buffer
->page
= (char *) get_zeroed_page(GFP_KERNEL
);
85 count
= ops
->show(kobj
,attr
,buffer
->page
);
86 buffer
->needs_read_fill
= 0;
87 BUG_ON(count
> (ssize_t
)PAGE_SIZE
);
89 buffer
->count
= count
;
97 * flush_read_buffer - push buffer to userspace.
98 * @buffer: data buffer for file.
99 * @buf: user-passed buffer.
100 * @count: number of bytes requested.
101 * @ppos: file position.
103 * Copy the buffer we filled in fill_read_buffer() to userspace.
104 * This is done at the reader's leisure, copying and advancing
105 * the amount they specify each time.
106 * This may be called continuously until the buffer is empty.
108 static int flush_read_buffer(struct sysfs_buffer
* buffer
, char __user
* buf
,
109 size_t count
, loff_t
* ppos
)
113 if (*ppos
> buffer
->count
)
116 if (count
> (buffer
->count
- *ppos
))
117 count
= buffer
->count
- *ppos
;
119 error
= copy_to_user(buf
,buffer
->page
+ *ppos
,count
);
122 return error
? -EFAULT
: count
;
126 * sysfs_read_file - read an attribute.
127 * @file: file pointer.
128 * @buf: buffer to fill.
129 * @count: number of bytes to read.
130 * @ppos: starting offset in file.
132 * Userspace wants to read an attribute file. The attribute descriptor
133 * is in the file's ->d_fsdata. The target object is in the directory's
136 * We call fill_read_buffer() to allocate and fill the buffer from the
137 * object's show() method exactly once (if the read is happening from
138 * the beginning of the file). That should fill the entire buffer with
139 * all the data the object has to offer for that attribute.
140 * We then call flush_read_buffer() to copy the buffer to userspace
141 * in the increments specified.
145 sysfs_read_file(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
147 struct sysfs_buffer
* buffer
= file
->private_data
;
151 if (buffer
->needs_read_fill
) {
152 if ((retval
= fill_read_buffer(file
->f_dentry
,buffer
)))
155 pr_debug("%s: count = %d, ppos = %lld, buf = %s\n",
156 __FUNCTION__
,count
,*ppos
,buffer
->page
);
157 retval
= flush_read_buffer(buffer
,buf
,count
,ppos
);
165 * fill_write_buffer - copy buffer from userspace.
166 * @buffer: data buffer for file.
167 * @buf: data from user.
168 * @count: number of bytes in @userbuf.
170 * Allocate @buffer->page if it hasn't been already, then
171 * copy the user-supplied buffer into it.
175 fill_write_buffer(struct sysfs_buffer
* buffer
, const char __user
* buf
, size_t count
)
180 buffer
->page
= (char *)get_zeroed_page(GFP_KERNEL
);
184 if (count
>= PAGE_SIZE
)
185 count
= PAGE_SIZE
- 1;
186 error
= copy_from_user(buffer
->page
,buf
,count
);
187 buffer
->needs_read_fill
= 1;
188 return error
? -EFAULT
: count
;
193 * flush_write_buffer - push buffer to kobject.
194 * @file: file pointer.
195 * @buffer: data buffer for file.
197 * Get the correct pointers for the kobject and the attribute we're
198 * dealing with, then call the store() method for the attribute,
199 * passing the buffer that we acquired in fill_write_buffer().
203 flush_write_buffer(struct dentry
* dentry
, struct sysfs_buffer
* buffer
, size_t count
)
205 struct attribute
* attr
= to_attr(dentry
);
206 struct kobject
* kobj
= to_kobj(dentry
->d_parent
);
207 struct sysfs_ops
* ops
= buffer
->ops
;
209 return ops
->store(kobj
,attr
,buffer
->page
,count
);
214 * sysfs_write_file - write an attribute.
215 * @file: file pointer
216 * @buf: data to write
217 * @count: number of bytes
218 * @ppos: starting offset
220 * Similar to sysfs_read_file(), though working in the opposite direction.
221 * We allocate and fill the data from the user in fill_write_buffer(),
222 * then push it to the kobject in flush_write_buffer().
223 * There is no easy way for us to know if userspace is only doing a partial
224 * write, so we don't support them. We expect the entire buffer to come
225 * on the first write.
226 * Hint: if you're writing a value, first read the file, modify only the
227 * the value you're changing, then write entire buffer back.
231 sysfs_write_file(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*ppos
)
233 struct sysfs_buffer
* buffer
= file
->private_data
;
237 len
= fill_write_buffer(buffer
, buf
, count
);
239 len
= flush_write_buffer(file
->f_dentry
, buffer
, len
);
246 static int check_perm(struct inode
* inode
, struct file
* file
)
248 struct kobject
*kobj
= sysfs_get_kobject(file
->f_dentry
->d_parent
);
249 struct attribute
* attr
= to_attr(file
->f_dentry
);
250 struct sysfs_buffer
* buffer
;
251 struct sysfs_ops
* ops
= NULL
;
257 /* Grab the module reference for this attribute if we have one */
258 if (!try_module_get(attr
->owner
)) {
263 /* if the kobject has no ktype, then we assume that it is a subsystem
264 * itself, and use ops for it.
266 if (kobj
->kset
&& kobj
->kset
->ktype
)
267 ops
= kobj
->kset
->ktype
->sysfs_ops
;
268 else if (kobj
->ktype
)
269 ops
= kobj
->ktype
->sysfs_ops
;
271 ops
= &subsys_sysfs_ops
;
273 /* No sysfs operations, either from having no subsystem,
274 * or the subsystem have no operations.
279 /* File needs write support.
280 * The inode's perms must say it's ok,
281 * and we must have a store method.
283 if (file
->f_mode
& FMODE_WRITE
) {
285 if (!(inode
->i_mode
& S_IWUGO
) || !ops
->store
)
290 /* File needs read support.
291 * The inode's perms must say it's ok, and we there
292 * must be a show method for it.
294 if (file
->f_mode
& FMODE_READ
) {
295 if (!(inode
->i_mode
& S_IRUGO
) || !ops
->show
)
299 /* No error? Great, allocate a buffer for the file, and store it
300 * it in file->private_data for easy access.
302 buffer
= kmalloc(sizeof(struct sysfs_buffer
),GFP_KERNEL
);
304 memset(buffer
,0,sizeof(struct sysfs_buffer
));
305 init_MUTEX(&buffer
->sem
);
306 buffer
->needs_read_fill
= 1;
308 file
->private_data
= buffer
;
318 module_put(attr
->owner
);
325 static int sysfs_open_file(struct inode
* inode
, struct file
* filp
)
327 return check_perm(inode
,filp
);
330 static int sysfs_release(struct inode
* inode
, struct file
* filp
)
332 struct kobject
* kobj
= to_kobj(filp
->f_dentry
->d_parent
);
333 struct attribute
* attr
= to_attr(filp
->f_dentry
);
334 struct module
* owner
= attr
->owner
;
335 struct sysfs_buffer
* buffer
= filp
->private_data
;
339 /* After this point, attr should not be accessed. */
344 free_page((unsigned long)buffer
->page
);
350 struct file_operations sysfs_file_operations
= {
351 .read
= sysfs_read_file
,
352 .write
= sysfs_write_file
,
353 .llseek
= generic_file_llseek
,
354 .open
= sysfs_open_file
,
355 .release
= sysfs_release
,
359 int sysfs_add_file(struct dentry
* dir
, const struct attribute
* attr
, int type
)
361 struct sysfs_dirent
* parent_sd
= dir
->d_fsdata
;
362 umode_t mode
= (attr
->mode
& S_IALLUGO
) | S_IFREG
;
365 down(&dir
->d_inode
->i_sem
);
366 error
= sysfs_make_dirent(parent_sd
, NULL
, (void *) attr
, mode
, type
);
367 up(&dir
->d_inode
->i_sem
);
374 * sysfs_create_file - create an attribute file for an object.
375 * @kobj: object we're creating for.
376 * @attr: atrribute descriptor.
379 int sysfs_create_file(struct kobject
* kobj
, const struct attribute
* attr
)
381 BUG_ON(!kobj
|| !kobj
->dentry
|| !attr
);
383 return sysfs_add_file(kobj
->dentry
, attr
, SYSFS_KOBJ_ATTR
);
389 * sysfs_update_file - update the modified timestamp on an object attribute.
390 * @kobj: object we're acting for.
391 * @attr: attribute descriptor.
393 * Also call dnotify for the dentry, which lots of userspace programs
396 int sysfs_update_file(struct kobject
* kobj
, const struct attribute
* attr
)
398 struct dentry
* dir
= kobj
->dentry
;
399 struct dentry
* victim
;
402 down(&dir
->d_inode
->i_sem
);
403 victim
= sysfs_get_dentry(dir
, attr
->name
);
404 if (!IS_ERR(victim
)) {
405 /* make sure dentry is really there */
406 if (victim
->d_inode
&&
407 (victim
->d_parent
->d_inode
== dir
->d_inode
)) {
408 victim
->d_inode
->i_mtime
= CURRENT_TIME
;
409 dnotify_parent(victim
, DN_MODIFY
);
412 * Drop reference from initial sysfs_get_dentry().
420 * Drop the reference acquired from sysfs_get_dentry() above.
424 up(&dir
->d_inode
->i_sem
);
431 * sysfs_chmod_file - update the modified mode value on an object attribute.
432 * @kobj: object we're acting for.
433 * @attr: attribute descriptor.
434 * @mode: file permissions.
437 int sysfs_chmod_file(struct kobject
*kobj
, struct attribute
*attr
, mode_t mode
)
439 struct dentry
*dir
= kobj
->dentry
;
440 struct dentry
*victim
;
441 struct sysfs_dirent
*sd
;
442 umode_t umode
= (mode
& S_IALLUGO
) | S_IFREG
;
445 down(&dir
->d_inode
->i_sem
);
446 victim
= sysfs_get_dentry(dir
, attr
->name
);
447 if (!IS_ERR(victim
)) {
448 if (victim
->d_inode
&&
449 (victim
->d_parent
->d_inode
== dir
->d_inode
)) {
450 sd
= victim
->d_fsdata
;
453 victim
->d_inode
->i_mode
= umode
;
458 up(&dir
->d_inode
->i_sem
);
462 EXPORT_SYMBOL_GPL(sysfs_chmod_file
);
466 * sysfs_remove_file - remove an object attribute.
467 * @kobj: object we're acting for.
468 * @attr: attribute descriptor.
470 * Hash the attribute name and kill the victim.
473 void sysfs_remove_file(struct kobject
* kobj
, const struct attribute
* attr
)
475 sysfs_hash_and_remove(kobj
->dentry
,attr
->name
);
479 EXPORT_SYMBOL_GPL(sysfs_create_file
);
480 EXPORT_SYMBOL_GPL(sysfs_remove_file
);
481 EXPORT_SYMBOL_GPL(sysfs_update_file
);