Driver core: fix race in sysfs between sysfs_remove_file() and read()/write()
[linux-2.6/mini2440.git] / fs / sysfs / file.c
blobcba4c1c7383c1834692268bd6b6a7e86c41e3777
1 /*
2 * file.c - operations for regular (text) files.
3 */
5 #include <linux/module.h>
6 #include <linux/fsnotify.h>
7 #include <linux/kobject.h>
8 #include <linux/namei.h>
9 #include <linux/poll.h>
10 #include <linux/list.h>
11 #include <asm/uaccess.h>
12 #include <asm/semaphore.h>
14 #include "sysfs.h"
16 #define to_subsys(k) container_of(k,struct subsystem,kset.kobj)
17 #define to_sattr(a) container_of(a,struct subsys_attribute,attr)
20 * Subsystem file operations.
21 * These operations allow subsystems to have files that can be
22 * read/written.
24 static ssize_t
25 subsys_attr_show(struct kobject * kobj, struct attribute * attr, char * page)
27 struct subsystem * s = to_subsys(kobj);
28 struct subsys_attribute * sattr = to_sattr(attr);
29 ssize_t ret = -EIO;
31 if (sattr->show)
32 ret = sattr->show(s,page);
33 return ret;
36 static ssize_t
37 subsys_attr_store(struct kobject * kobj, struct attribute * attr,
38 const char * page, size_t count)
40 struct subsystem * s = to_subsys(kobj);
41 struct subsys_attribute * sattr = to_sattr(attr);
42 ssize_t ret = -EIO;
44 if (sattr->store)
45 ret = sattr->store(s,page,count);
46 return ret;
49 static struct sysfs_ops subsys_sysfs_ops = {
50 .show = subsys_attr_show,
51 .store = subsys_attr_store,
54 /**
55 * add_to_collection - add buffer to a collection
56 * @buffer: buffer to be added
57 * @node inode of set to add to
60 static inline void
61 add_to_collection(struct sysfs_buffer *buffer, struct inode *node)
63 struct sysfs_buffer_collection *set = node->i_private;
65 mutex_lock(&node->i_mutex);
66 list_add(&buffer->associates, &set->associates);
67 mutex_unlock(&node->i_mutex);
70 static inline void
71 remove_from_collection(struct sysfs_buffer *buffer, struct inode *node)
73 mutex_lock(&node->i_mutex);
74 list_del(&buffer->associates);
75 mutex_unlock(&node->i_mutex);
78 /**
79 * fill_read_buffer - allocate and fill buffer from object.
80 * @dentry: dentry pointer.
81 * @buffer: data buffer for file.
83 * Allocate @buffer->page, if it hasn't been already, then call the
84 * kobject's show() method to fill the buffer with this attribute's
85 * data.
86 * This is called only once, on the file's first read.
88 static int fill_read_buffer(struct dentry * dentry, struct sysfs_buffer * buffer)
90 struct sysfs_dirent * sd = dentry->d_fsdata;
91 struct attribute * attr = to_attr(dentry);
92 struct kobject * kobj = to_kobj(dentry->d_parent);
93 struct sysfs_ops * ops = buffer->ops;
94 int ret = 0;
95 ssize_t count;
97 if (!buffer->page)
98 buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
99 if (!buffer->page)
100 return -ENOMEM;
102 buffer->event = atomic_read(&sd->s_event);
103 count = ops->show(kobj,attr,buffer->page);
104 buffer->needs_read_fill = 0;
105 BUG_ON(count > (ssize_t)PAGE_SIZE);
106 if (count >= 0)
107 buffer->count = count;
108 else
109 ret = count;
110 return ret;
115 * flush_read_buffer - push buffer to userspace.
116 * @buffer: data buffer for file.
117 * @buf: user-passed buffer.
118 * @count: number of bytes requested.
119 * @ppos: file position.
121 * Copy the buffer we filled in fill_read_buffer() to userspace.
122 * This is done at the reader's leisure, copying and advancing
123 * the amount they specify each time.
124 * This may be called continuously until the buffer is empty.
126 static int flush_read_buffer(struct sysfs_buffer * buffer, char __user * buf,
127 size_t count, loff_t * ppos)
129 int error;
131 if (*ppos > buffer->count)
132 return 0;
134 if (count > (buffer->count - *ppos))
135 count = buffer->count - *ppos;
137 error = copy_to_user(buf,buffer->page + *ppos,count);
138 if (!error)
139 *ppos += count;
140 return error ? -EFAULT : count;
144 * sysfs_read_file - read an attribute.
145 * @file: file pointer.
146 * @buf: buffer to fill.
147 * @count: number of bytes to read.
148 * @ppos: starting offset in file.
150 * Userspace wants to read an attribute file. The attribute descriptor
151 * is in the file's ->d_fsdata. The target object is in the directory's
152 * ->d_fsdata.
154 * We call fill_read_buffer() to allocate and fill the buffer from the
155 * object's show() method exactly once (if the read is happening from
156 * the beginning of the file). That should fill the entire buffer with
157 * all the data the object has to offer for that attribute.
158 * We then call flush_read_buffer() to copy the buffer to userspace
159 * in the increments specified.
162 static ssize_t
163 sysfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
165 struct sysfs_buffer * buffer = file->private_data;
166 ssize_t retval = 0;
168 down(&buffer->sem);
169 if (buffer->orphaned) {
170 retval = -ENODEV;
171 goto out;
173 if (buffer->needs_read_fill) {
174 if ((retval = fill_read_buffer(file->f_path.dentry,buffer)))
175 goto out;
177 pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",
178 __FUNCTION__, count, *ppos, buffer->page);
179 retval = flush_read_buffer(buffer,buf,count,ppos);
180 out:
181 up(&buffer->sem);
182 return retval;
186 * fill_write_buffer - copy buffer from userspace.
187 * @buffer: data buffer for file.
188 * @buf: data from user.
189 * @count: number of bytes in @userbuf.
191 * Allocate @buffer->page if it hasn't been already, then
192 * copy the user-supplied buffer into it.
195 static int
196 fill_write_buffer(struct sysfs_buffer * buffer, const char __user * buf, size_t count)
198 int error;
200 if (!buffer->page)
201 buffer->page = (char *)get_zeroed_page(GFP_KERNEL);
202 if (!buffer->page)
203 return -ENOMEM;
205 if (count >= PAGE_SIZE)
206 count = PAGE_SIZE - 1;
207 error = copy_from_user(buffer->page,buf,count);
208 buffer->needs_read_fill = 1;
209 /* if buf is assumed to contain a string, terminate it by \0,
210 so e.g. sscanf() can scan the string easily */
211 buffer->page[count] = 0;
212 return error ? -EFAULT : count;
217 * flush_write_buffer - push buffer to kobject.
218 * @dentry: dentry to the attribute
219 * @buffer: data buffer for file.
220 * @count: number of bytes
222 * Get the correct pointers for the kobject and the attribute we're
223 * dealing with, then call the store() method for the attribute,
224 * passing the buffer that we acquired in fill_write_buffer().
227 static int
228 flush_write_buffer(struct dentry * dentry, struct sysfs_buffer * buffer, size_t count)
230 struct attribute * attr = to_attr(dentry);
231 struct kobject * kobj = to_kobj(dentry->d_parent);
232 struct sysfs_ops * ops = buffer->ops;
234 return ops->store(kobj,attr,buffer->page,count);
239 * sysfs_write_file - write an attribute.
240 * @file: file pointer
241 * @buf: data to write
242 * @count: number of bytes
243 * @ppos: starting offset
245 * Similar to sysfs_read_file(), though working in the opposite direction.
246 * We allocate and fill the data from the user in fill_write_buffer(),
247 * then push it to the kobject in flush_write_buffer().
248 * There is no easy way for us to know if userspace is only doing a partial
249 * write, so we don't support them. We expect the entire buffer to come
250 * on the first write.
251 * Hint: if you're writing a value, first read the file, modify only the
252 * the value you're changing, then write entire buffer back.
255 static ssize_t
256 sysfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
258 struct sysfs_buffer * buffer = file->private_data;
259 ssize_t len;
261 down(&buffer->sem);
262 if (buffer->orphaned) {
263 len = -ENODEV;
264 goto out;
266 len = fill_write_buffer(buffer, buf, count);
267 if (len > 0)
268 len = flush_write_buffer(file->f_path.dentry, buffer, len);
269 if (len > 0)
270 *ppos += len;
271 out:
272 up(&buffer->sem);
273 return len;
276 static int sysfs_open_file(struct inode *inode, struct file *file)
278 struct kobject *kobj = sysfs_get_kobject(file->f_path.dentry->d_parent);
279 struct attribute * attr = to_attr(file->f_path.dentry);
280 struct sysfs_buffer_collection *set;
281 struct sysfs_buffer * buffer;
282 struct sysfs_ops * ops = NULL;
283 int error = 0;
285 if (!kobj || !attr)
286 goto Einval;
288 /* Grab the module reference for this attribute if we have one */
289 if (!try_module_get(attr->owner)) {
290 error = -ENODEV;
291 goto Done;
294 /* if the kobject has no ktype, then we assume that it is a subsystem
295 * itself, and use ops for it.
297 if (kobj->kset && kobj->kset->ktype)
298 ops = kobj->kset->ktype->sysfs_ops;
299 else if (kobj->ktype)
300 ops = kobj->ktype->sysfs_ops;
301 else
302 ops = &subsys_sysfs_ops;
304 /* No sysfs operations, either from having no subsystem,
305 * or the subsystem have no operations.
307 if (!ops)
308 goto Eaccess;
310 /* make sure we have a collection to add our buffers to */
311 mutex_lock(&inode->i_mutex);
312 if (!(set = inode->i_private)) {
313 if (!(set = inode->i_private = kmalloc(sizeof(struct sysfs_buffer_collection), GFP_KERNEL))) {
314 error = -ENOMEM;
315 goto Done;
316 } else {
317 INIT_LIST_HEAD(&set->associates);
320 mutex_unlock(&inode->i_mutex);
322 /* File needs write support.
323 * The inode's perms must say it's ok,
324 * and we must have a store method.
326 if (file->f_mode & FMODE_WRITE) {
328 if (!(inode->i_mode & S_IWUGO) || !ops->store)
329 goto Eaccess;
333 /* File needs read support.
334 * The inode's perms must say it's ok, and we there
335 * must be a show method for it.
337 if (file->f_mode & FMODE_READ) {
338 if (!(inode->i_mode & S_IRUGO) || !ops->show)
339 goto Eaccess;
342 /* No error? Great, allocate a buffer for the file, and store it
343 * it in file->private_data for easy access.
345 buffer = kzalloc(sizeof(struct sysfs_buffer), GFP_KERNEL);
346 if (buffer) {
347 INIT_LIST_HEAD(&buffer->associates);
348 init_MUTEX(&buffer->sem);
349 buffer->needs_read_fill = 1;
350 buffer->ops = ops;
351 add_to_collection(buffer, inode);
352 file->private_data = buffer;
353 } else
354 error = -ENOMEM;
355 goto Done;
357 Einval:
358 error = -EINVAL;
359 goto Done;
360 Eaccess:
361 error = -EACCES;
362 module_put(attr->owner);
363 Done:
364 if (error && kobj)
365 kobject_put(kobj);
366 return error;
369 static int sysfs_release(struct inode * inode, struct file * filp)
371 struct kobject * kobj = to_kobj(filp->f_path.dentry->d_parent);
372 struct attribute * attr = to_attr(filp->f_path.dentry);
373 struct module * owner = attr->owner;
374 struct sysfs_buffer * buffer = filp->private_data;
376 if (buffer)
377 remove_from_collection(buffer, inode);
378 if (kobj)
379 kobject_put(kobj);
380 /* After this point, attr should not be accessed. */
381 module_put(owner);
383 if (buffer) {
384 if (buffer->page)
385 free_page((unsigned long)buffer->page);
386 kfree(buffer);
388 return 0;
391 /* Sysfs attribute files are pollable. The idea is that you read
392 * the content and then you use 'poll' or 'select' to wait for
393 * the content to change. When the content changes (assuming the
394 * manager for the kobject supports notification), poll will
395 * return POLLERR|POLLPRI, and select will return the fd whether
396 * it is waiting for read, write, or exceptions.
397 * Once poll/select indicates that the value has changed, you
398 * need to close and re-open the file, as simply seeking and reading
399 * again will not get new data, or reset the state of 'poll'.
400 * Reminder: this only works for attributes which actively support
401 * it, and it is not possible to test an attribute from userspace
402 * to see if it supports poll (Nether 'poll' or 'select' return
403 * an appropriate error code). When in doubt, set a suitable timeout value.
405 static unsigned int sysfs_poll(struct file *filp, poll_table *wait)
407 struct sysfs_buffer * buffer = filp->private_data;
408 struct kobject * kobj = to_kobj(filp->f_path.dentry->d_parent);
409 struct sysfs_dirent * sd = filp->f_path.dentry->d_fsdata;
410 int res = 0;
412 poll_wait(filp, &kobj->poll, wait);
414 if (buffer->event != atomic_read(&sd->s_event)) {
415 res = POLLERR|POLLPRI;
416 buffer->needs_read_fill = 1;
419 return res;
423 static struct dentry *step_down(struct dentry *dir, const char * name)
425 struct dentry * de;
427 if (dir == NULL || dir->d_inode == NULL)
428 return NULL;
430 mutex_lock(&dir->d_inode->i_mutex);
431 de = lookup_one_len(name, dir, strlen(name));
432 mutex_unlock(&dir->d_inode->i_mutex);
433 dput(dir);
434 if (IS_ERR(de))
435 return NULL;
436 if (de->d_inode == NULL) {
437 dput(de);
438 return NULL;
440 return de;
443 void sysfs_notify(struct kobject * k, char *dir, char *attr)
445 struct dentry *de = k->dentry;
446 if (de)
447 dget(de);
448 if (de && dir)
449 de = step_down(de, dir);
450 if (de && attr)
451 de = step_down(de, attr);
452 if (de) {
453 struct sysfs_dirent * sd = de->d_fsdata;
454 if (sd)
455 atomic_inc(&sd->s_event);
456 wake_up_interruptible(&k->poll);
457 dput(de);
460 EXPORT_SYMBOL_GPL(sysfs_notify);
462 const struct file_operations sysfs_file_operations = {
463 .read = sysfs_read_file,
464 .write = sysfs_write_file,
465 .llseek = generic_file_llseek,
466 .open = sysfs_open_file,
467 .release = sysfs_release,
468 .poll = sysfs_poll,
472 int sysfs_add_file(struct dentry * dir, const struct attribute * attr, int type)
474 struct sysfs_dirent * parent_sd = dir->d_fsdata;
475 umode_t mode = (attr->mode & S_IALLUGO) | S_IFREG;
476 int error = -EEXIST;
478 mutex_lock(&dir->d_inode->i_mutex);
479 if (!sysfs_dirent_exist(parent_sd, attr->name))
480 error = sysfs_make_dirent(parent_sd, NULL, (void *)attr,
481 mode, type);
482 mutex_unlock(&dir->d_inode->i_mutex);
484 return error;
489 * sysfs_create_file - create an attribute file for an object.
490 * @kobj: object we're creating for.
491 * @attr: atrribute descriptor.
494 int sysfs_create_file(struct kobject * kobj, const struct attribute * attr)
496 BUG_ON(!kobj || !kobj->dentry || !attr);
498 return sysfs_add_file(kobj->dentry, attr, SYSFS_KOBJ_ATTR);
504 * sysfs_update_file - update the modified timestamp on an object attribute.
505 * @kobj: object we're acting for.
506 * @attr: attribute descriptor.
508 int sysfs_update_file(struct kobject * kobj, const struct attribute * attr)
510 struct dentry * dir = kobj->dentry;
511 struct dentry * victim;
512 int res = -ENOENT;
514 mutex_lock(&dir->d_inode->i_mutex);
515 victim = lookup_one_len(attr->name, dir, strlen(attr->name));
516 if (!IS_ERR(victim)) {
517 /* make sure dentry is really there */
518 if (victim->d_inode &&
519 (victim->d_parent->d_inode == dir->d_inode)) {
520 victim->d_inode->i_mtime = CURRENT_TIME;
521 fsnotify_modify(victim);
522 res = 0;
523 } else
524 d_drop(victim);
527 * Drop the reference acquired from lookup_one_len() above.
529 dput(victim);
531 mutex_unlock(&dir->d_inode->i_mutex);
533 return res;
538 * sysfs_chmod_file - update the modified mode value on an object attribute.
539 * @kobj: object we're acting for.
540 * @attr: attribute descriptor.
541 * @mode: file permissions.
544 int sysfs_chmod_file(struct kobject *kobj, struct attribute *attr, mode_t mode)
546 struct dentry *dir = kobj->dentry;
547 struct dentry *victim;
548 struct inode * inode;
549 struct iattr newattrs;
550 int res = -ENOENT;
552 mutex_lock(&dir->d_inode->i_mutex);
553 victim = lookup_one_len(attr->name, dir, strlen(attr->name));
554 if (!IS_ERR(victim)) {
555 if (victim->d_inode &&
556 (victim->d_parent->d_inode == dir->d_inode)) {
557 inode = victim->d_inode;
558 mutex_lock(&inode->i_mutex);
559 newattrs.ia_mode = (mode & S_IALLUGO) |
560 (inode->i_mode & ~S_IALLUGO);
561 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
562 res = notify_change(victim, &newattrs);
563 mutex_unlock(&inode->i_mutex);
565 dput(victim);
567 mutex_unlock(&dir->d_inode->i_mutex);
569 return res;
571 EXPORT_SYMBOL_GPL(sysfs_chmod_file);
575 * sysfs_remove_file - remove an object attribute.
576 * @kobj: object we're acting for.
577 * @attr: attribute descriptor.
579 * Hash the attribute name and kill the victim.
582 void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr)
584 sysfs_hash_and_remove(kobj->dentry, attr->name);
588 EXPORT_SYMBOL_GPL(sysfs_create_file);
589 EXPORT_SYMBOL_GPL(sysfs_remove_file);
590 EXPORT_SYMBOL_GPL(sysfs_update_file);