blob: d72c1ce485598600c13ff7d484306c0a3aad85e7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * file.c - operations for regular (text) files.
3 */
4
5#include <linux/module.h>
6#include <linux/dnotify.h>
7#include <linux/kobject.h>
Christoph Hellwig5f45f1a2005-06-23 00:09:12 -07008#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <asm/uaccess.h>
10#include <asm/semaphore.h>
11
12#include "sysfs.h"
13
14#define to_subsys(k) container_of(k,struct subsystem,kset.kobj)
15#define to_sattr(a) container_of(a,struct subsys_attribute,attr)
16
Martin Waitz3d410882005-06-23 22:05:21 -070017/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * Subsystem file operations.
19 * These operations allow subsystems to have files that can be
20 * read/written.
21 */
22static ssize_t
23subsys_attr_show(struct kobject * kobj, struct attribute * attr, char * page)
24{
25 struct subsystem * s = to_subsys(kobj);
26 struct subsys_attribute * sattr = to_sattr(attr);
Dmitry Torokhovc76d0ab2005-04-29 01:22:00 -050027 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29 if (sattr->show)
30 ret = sattr->show(s,page);
31 return ret;
32}
33
34static ssize_t
35subsys_attr_store(struct kobject * kobj, struct attribute * attr,
36 const char * page, size_t count)
37{
38 struct subsystem * s = to_subsys(kobj);
39 struct subsys_attribute * sattr = to_sattr(attr);
Dmitry Torokhovc76d0ab2005-04-29 01:22:00 -050040 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42 if (sattr->store)
43 ret = sattr->store(s,page,count);
44 return ret;
45}
46
47static struct sysfs_ops subsys_sysfs_ops = {
48 .show = subsys_attr_show,
49 .store = subsys_attr_store,
50};
51
52
53struct sysfs_buffer {
54 size_t count;
55 loff_t pos;
56 char * page;
57 struct sysfs_ops * ops;
58 struct semaphore sem;
59 int needs_read_fill;
60};
61
62
63/**
64 * fill_read_buffer - allocate and fill buffer from object.
65 * @dentry: dentry pointer.
66 * @buffer: data buffer for file.
67 *
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
70 * data.
71 * This is called only once, on the file's first read.
72 */
73static int fill_read_buffer(struct dentry * dentry, struct sysfs_buffer * buffer)
74{
75 struct attribute * attr = to_attr(dentry);
76 struct kobject * kobj = to_kobj(dentry->d_parent);
77 struct sysfs_ops * ops = buffer->ops;
78 int ret = 0;
79 ssize_t count;
80
81 if (!buffer->page)
82 buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
83 if (!buffer->page)
84 return -ENOMEM;
85
86 count = ops->show(kobj,attr,buffer->page);
87 buffer->needs_read_fill = 0;
88 BUG_ON(count > (ssize_t)PAGE_SIZE);
89 if (count >= 0)
90 buffer->count = count;
91 else
92 ret = count;
93 return ret;
94}
95
96
97/**
98 * flush_read_buffer - push buffer to userspace.
99 * @buffer: data buffer for file.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700100 * @buf: user-passed buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 * @count: number of bytes requested.
102 * @ppos: file position.
103 *
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.
108 */
109static int flush_read_buffer(struct sysfs_buffer * buffer, char __user * buf,
110 size_t count, loff_t * ppos)
111{
112 int error;
113
114 if (*ppos > buffer->count)
115 return 0;
116
117 if (count > (buffer->count - *ppos))
118 count = buffer->count - *ppos;
119
120 error = copy_to_user(buf,buffer->page + *ppos,count);
121 if (!error)
122 *ppos += count;
123 return error ? -EFAULT : count;
124}
125
126/**
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.
132 *
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
135 * ->d_fsdata.
136 *
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.
143 */
144
145static ssize_t
146sysfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
147{
148 struct sysfs_buffer * buffer = file->private_data;
149 ssize_t retval = 0;
150
151 down(&buffer->sem);
152 if (buffer->needs_read_fill) {
153 if ((retval = fill_read_buffer(file->f_dentry,buffer)))
154 goto out;
155 }
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);
159out:
160 up(&buffer->sem);
161 return retval;
162}
163
164
165/**
166 * fill_write_buffer - copy buffer from userspace.
167 * @buffer: data buffer for file.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700168 * @buf: data from user.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 * @count: number of bytes in @userbuf.
170 *
171 * Allocate @buffer->page if it hasn't been already, then
172 * copy the user-supplied buffer into it.
173 */
174
175static int
176fill_write_buffer(struct sysfs_buffer * buffer, const char __user * buf, size_t count)
177{
178 int error;
179
180 if (!buffer->page)
181 buffer->page = (char *)get_zeroed_page(GFP_KERNEL);
182 if (!buffer->page)
183 return -ENOMEM;
184
185 if (count >= PAGE_SIZE)
Jon Smirl9d9d27f2005-06-14 09:54:54 -0400186 count = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 error = copy_from_user(buffer->page,buf,count);
188 buffer->needs_read_fill = 1;
189 return error ? -EFAULT : count;
190}
191
192
193/**
194 * flush_write_buffer - push buffer to kobject.
Martin Waitz3d410882005-06-23 22:05:21 -0700195 * @dentry: dentry to the attribute
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 * @buffer: data buffer for file.
Martin Waitz3d410882005-06-23 22:05:21 -0700197 * @count: number of bytes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 *
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().
202 */
203
204static int
205flush_write_buffer(struct dentry * dentry, struct sysfs_buffer * buffer, size_t count)
206{
207 struct attribute * attr = to_attr(dentry);
208 struct kobject * kobj = to_kobj(dentry->d_parent);
209 struct sysfs_ops * ops = buffer->ops;
210
211 return ops->store(kobj,attr,buffer->page,count);
212}
213
214
215/**
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
221 *
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.
230 */
231
232static ssize_t
233sysfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
234{
235 struct sysfs_buffer * buffer = file->private_data;
236 ssize_t len;
237
238 down(&buffer->sem);
239 len = fill_write_buffer(buffer, buf, count);
240 if (len > 0)
241 len = flush_write_buffer(file->f_dentry, buffer, len);
242 if (len > 0)
243 *ppos += len;
244 up(&buffer->sem);
245 return len;
246}
247
248static int check_perm(struct inode * inode, struct file * file)
249{
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;
254 int error = 0;
255
256 if (!kobj || !attr)
257 goto Einval;
258
259 /* Grab the module reference for this attribute if we have one */
260 if (!try_module_get(attr->owner)) {
261 error = -ENODEV;
262 goto Done;
263 }
264
265 /* if the kobject has no ktype, then we assume that it is a subsystem
266 * itself, and use ops for it.
267 */
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;
272 else
273 ops = &subsys_sysfs_ops;
274
275 /* No sysfs operations, either from having no subsystem,
276 * or the subsystem have no operations.
277 */
278 if (!ops)
279 goto Eaccess;
280
281 /* File needs write support.
282 * The inode's perms must say it's ok,
283 * and we must have a store method.
284 */
285 if (file->f_mode & FMODE_WRITE) {
286
287 if (!(inode->i_mode & S_IWUGO) || !ops->store)
288 goto Eaccess;
289
290 }
291
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.
295 */
296 if (file->f_mode & FMODE_READ) {
297 if (!(inode->i_mode & S_IRUGO) || !ops->show)
298 goto Eaccess;
299 }
300
301 /* No error? Great, allocate a buffer for the file, and store it
302 * it in file->private_data for easy access.
303 */
304 buffer = kmalloc(sizeof(struct sysfs_buffer),GFP_KERNEL);
305 if (buffer) {
306 memset(buffer,0,sizeof(struct sysfs_buffer));
307 init_MUTEX(&buffer->sem);
308 buffer->needs_read_fill = 1;
309 buffer->ops = ops;
310 file->private_data = buffer;
311 } else
312 error = -ENOMEM;
313 goto Done;
314
315 Einval:
316 error = -EINVAL;
317 goto Done;
318 Eaccess:
319 error = -EACCES;
320 module_put(attr->owner);
321 Done:
322 if (error && kobj)
323 kobject_put(kobj);
324 return error;
325}
326
327static int sysfs_open_file(struct inode * inode, struct file * filp)
328{
329 return check_perm(inode,filp);
330}
331
332static int sysfs_release(struct inode * inode, struct file * filp)
333{
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;
338
339 if (kobj)
340 kobject_put(kobj);
341 /* After this point, attr should not be accessed. */
342 module_put(owner);
343
344 if (buffer) {
345 if (buffer->page)
346 free_page((unsigned long)buffer->page);
347 kfree(buffer);
348 }
349 return 0;
350}
351
352struct 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,
358};
359
360
361int sysfs_add_file(struct dentry * dir, const struct attribute * attr, int type)
362{
363 struct sysfs_dirent * parent_sd = dir->d_fsdata;
364 umode_t mode = (attr->mode & S_IALLUGO) | S_IFREG;
365 int error = 0;
366
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);
370
371 return error;
372}
373
374
375/**
376 * sysfs_create_file - create an attribute file for an object.
377 * @kobj: object we're creating for.
378 * @attr: atrribute descriptor.
379 */
380
381int sysfs_create_file(struct kobject * kobj, const struct attribute * attr)
382{
383 BUG_ON(!kobj || !kobj->dentry || !attr);
384
385 return sysfs_add_file(kobj->dentry, attr, SYSFS_KOBJ_ATTR);
386
387}
388
389
390/**
391 * sysfs_update_file - update the modified timestamp on an object attribute.
392 * @kobj: object we're acting for.
393 * @attr: attribute descriptor.
394 *
395 * Also call dnotify for the dentry, which lots of userspace programs
396 * use.
397 */
398int sysfs_update_file(struct kobject * kobj, const struct attribute * attr)
399{
400 struct dentry * dir = kobj->dentry;
401 struct dentry * victim;
402 int res = -ENOENT;
403
404 down(&dir->d_inode->i_sem);
Christoph Hellwig5f45f1a2005-06-23 00:09:12 -0700405 victim = lookup_one_len(attr->name, dir, strlen(attr->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if (!IS_ERR(victim)) {
407 /* make sure dentry is really there */
408 if (victim->d_inode &&
409 (victim->d_parent->d_inode == dir->d_inode)) {
410 victim->d_inode->i_mtime = CURRENT_TIME;
411 dnotify_parent(victim, DN_MODIFY);
412
413 /**
414 * Drop reference from initial sysfs_get_dentry().
415 */
416 dput(victim);
417 res = 0;
418 } else
419 d_drop(victim);
420
421 /**
422 * Drop the reference acquired from sysfs_get_dentry() above.
423 */
424 dput(victim);
425 }
426 up(&dir->d_inode->i_sem);
427
428 return res;
429}
430
431
432/**
Kay Sievers31e5abe2005-04-18 21:57:32 -0700433 * sysfs_chmod_file - update the modified mode value on an object attribute.
434 * @kobj: object we're acting for.
435 * @attr: attribute descriptor.
436 * @mode: file permissions.
437 *
438 */
439int sysfs_chmod_file(struct kobject *kobj, struct attribute *attr, mode_t mode)
440{
441 struct dentry *dir = kobj->dentry;
442 struct dentry *victim;
443 struct sysfs_dirent *sd;
444 umode_t umode = (mode & S_IALLUGO) | S_IFREG;
445 int res = -ENOENT;
446
447 down(&dir->d_inode->i_sem);
Christoph Hellwig5f45f1a2005-06-23 00:09:12 -0700448 victim = lookup_one_len(attr->name, dir, strlen(attr->name));
Kay Sievers31e5abe2005-04-18 21:57:32 -0700449 if (!IS_ERR(victim)) {
450 if (victim->d_inode &&
451 (victim->d_parent->d_inode == dir->d_inode)) {
452 sd = victim->d_fsdata;
453 attr->mode = mode;
454 sd->s_mode = umode;
455 victim->d_inode->i_mode = umode;
456 dput(victim);
457 res = 0;
458 }
459 }
460 up(&dir->d_inode->i_sem);
461
462 return res;
463}
464EXPORT_SYMBOL_GPL(sysfs_chmod_file);
465
466
467/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 * sysfs_remove_file - remove an object attribute.
469 * @kobj: object we're acting for.
470 * @attr: attribute descriptor.
471 *
472 * Hash the attribute name and kill the victim.
473 */
474
475void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr)
476{
477 sysfs_hash_and_remove(kobj->dentry,attr->name);
478}
479
480
481EXPORT_SYMBOL_GPL(sysfs_create_file);
482EXPORT_SYMBOL_GPL(sysfs_remove_file);
483EXPORT_SYMBOL_GPL(sysfs_update_file);
484