PM / yenta: Split resume into early and late parts (rev. 4)
[linux/fpc-iii.git] / fs / sysfs / bin.c
blob2524714bece1b87e68d68c6928cfd720f0a9b259
1 /*
2 * fs/sysfs/bin.c - sysfs binary file implementation
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Matthew Wilcox
6 * Copyright (c) 2004 Silicon Graphics, Inc.
7 * Copyright (c) 2007 SUSE Linux Products GmbH
8 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
10 * This file is released under the GPLv2.
12 * Please see Documentation/filesystems/sysfs.txt for more information.
15 #undef DEBUG
17 #include <linux/errno.h>
18 #include <linux/fs.h>
19 #include <linux/kernel.h>
20 #include <linux/kobject.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/mutex.h>
24 #include <linux/mm.h>
26 #include <asm/uaccess.h>
28 #include "sysfs.h"
31 * There's one bin_buffer for each open file.
33 * filp->private_data points to bin_buffer and
34 * sysfs_dirent->s_bin_attr.buffers points to a the bin_buffer s
35 * sysfs_dirent->s_bin_attr.buffers is protected by sysfs_bin_lock
37 static DEFINE_MUTEX(sysfs_bin_lock);
39 struct bin_buffer {
40 struct mutex mutex;
41 void *buffer;
42 int mmapped;
43 struct vm_operations_struct *vm_ops;
44 struct file *file;
45 struct hlist_node list;
48 static int
49 fill_read(struct dentry *dentry, char *buffer, loff_t off, size_t count)
51 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
52 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
53 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
54 int rc;
56 /* need attr_sd for attr, its parent for kobj */
57 if (!sysfs_get_active_two(attr_sd))
58 return -ENODEV;
60 rc = -EIO;
61 if (attr->read)
62 rc = attr->read(kobj, attr, buffer, off, count);
64 sysfs_put_active_two(attr_sd);
66 return rc;
69 static ssize_t
70 read(struct file *file, char __user *userbuf, size_t bytes, loff_t *off)
72 struct bin_buffer *bb = file->private_data;
73 struct dentry *dentry = file->f_path.dentry;
74 int size = dentry->d_inode->i_size;
75 loff_t offs = *off;
76 int count = min_t(size_t, bytes, PAGE_SIZE);
77 char *temp;
79 if (!bytes)
80 return 0;
82 if (size) {
83 if (offs > size)
84 return 0;
85 if (offs + count > size)
86 count = size - offs;
89 temp = kmalloc(count, GFP_KERNEL);
90 if (!temp)
91 return -ENOMEM;
93 mutex_lock(&bb->mutex);
95 count = fill_read(dentry, bb->buffer, offs, count);
96 if (count < 0) {
97 mutex_unlock(&bb->mutex);
98 goto out_free;
101 memcpy(temp, bb->buffer, count);
103 mutex_unlock(&bb->mutex);
105 if (copy_to_user(userbuf, temp, count)) {
106 count = -EFAULT;
107 goto out_free;
110 pr_debug("offs = %lld, *off = %lld, count = %d\n", offs, *off, count);
112 *off = offs + count;
114 out_free:
115 kfree(temp);
116 return count;
119 static int
120 flush_write(struct dentry *dentry, char *buffer, loff_t offset, size_t count)
122 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
123 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
124 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
125 int rc;
127 /* need attr_sd for attr, its parent for kobj */
128 if (!sysfs_get_active_two(attr_sd))
129 return -ENODEV;
131 rc = -EIO;
132 if (attr->write)
133 rc = attr->write(kobj, attr, buffer, offset, count);
135 sysfs_put_active_two(attr_sd);
137 return rc;
140 static ssize_t write(struct file *file, const char __user *userbuf,
141 size_t bytes, loff_t *off)
143 struct bin_buffer *bb = file->private_data;
144 struct dentry *dentry = file->f_path.dentry;
145 int size = dentry->d_inode->i_size;
146 loff_t offs = *off;
147 int count = min_t(size_t, bytes, PAGE_SIZE);
148 char *temp;
150 if (!bytes)
151 return 0;
153 if (size) {
154 if (offs > size)
155 return 0;
156 if (offs + count > size)
157 count = size - offs;
160 temp = memdup_user(userbuf, count);
161 if (IS_ERR(temp))
162 return PTR_ERR(temp);
164 mutex_lock(&bb->mutex);
166 memcpy(bb->buffer, temp, count);
168 count = flush_write(dentry, bb->buffer, offs, count);
169 mutex_unlock(&bb->mutex);
171 if (count > 0)
172 *off = offs + count;
174 kfree(temp);
175 return count;
178 static void bin_vma_open(struct vm_area_struct *vma)
180 struct file *file = vma->vm_file;
181 struct bin_buffer *bb = file->private_data;
182 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
184 if (!bb->vm_ops || !bb->vm_ops->open)
185 return;
187 if (!sysfs_get_active_two(attr_sd))
188 return;
190 bb->vm_ops->open(vma);
192 sysfs_put_active_two(attr_sd);
195 static void bin_vma_close(struct vm_area_struct *vma)
197 struct file *file = vma->vm_file;
198 struct bin_buffer *bb = file->private_data;
199 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
201 if (!bb->vm_ops || !bb->vm_ops->close)
202 return;
204 if (!sysfs_get_active_two(attr_sd))
205 return;
207 bb->vm_ops->close(vma);
209 sysfs_put_active_two(attr_sd);
212 static int bin_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
214 struct file *file = vma->vm_file;
215 struct bin_buffer *bb = file->private_data;
216 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
217 int ret;
219 if (!bb->vm_ops || !bb->vm_ops->fault)
220 return VM_FAULT_SIGBUS;
222 if (!sysfs_get_active_two(attr_sd))
223 return VM_FAULT_SIGBUS;
225 ret = bb->vm_ops->fault(vma, vmf);
227 sysfs_put_active_two(attr_sd);
228 return ret;
231 static int bin_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
233 struct file *file = vma->vm_file;
234 struct bin_buffer *bb = file->private_data;
235 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
236 int ret;
238 if (!bb->vm_ops)
239 return VM_FAULT_SIGBUS;
241 if (!bb->vm_ops->page_mkwrite)
242 return 0;
244 if (!sysfs_get_active_two(attr_sd))
245 return VM_FAULT_SIGBUS;
247 ret = bb->vm_ops->page_mkwrite(vma, vmf);
249 sysfs_put_active_two(attr_sd);
250 return ret;
253 static int bin_access(struct vm_area_struct *vma, unsigned long addr,
254 void *buf, int len, int write)
256 struct file *file = vma->vm_file;
257 struct bin_buffer *bb = file->private_data;
258 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
259 int ret;
261 if (!bb->vm_ops || !bb->vm_ops->access)
262 return -EINVAL;
264 if (!sysfs_get_active_two(attr_sd))
265 return -EINVAL;
267 ret = bb->vm_ops->access(vma, addr, buf, len, write);
269 sysfs_put_active_two(attr_sd);
270 return ret;
273 #ifdef CONFIG_NUMA
274 static int bin_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
276 struct file *file = vma->vm_file;
277 struct bin_buffer *bb = file->private_data;
278 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
279 int ret;
281 if (!bb->vm_ops || !bb->vm_ops->set_policy)
282 return 0;
284 if (!sysfs_get_active_two(attr_sd))
285 return -EINVAL;
287 ret = bb->vm_ops->set_policy(vma, new);
289 sysfs_put_active_two(attr_sd);
290 return ret;
293 static struct mempolicy *bin_get_policy(struct vm_area_struct *vma,
294 unsigned long addr)
296 struct file *file = vma->vm_file;
297 struct bin_buffer *bb = file->private_data;
298 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
299 struct mempolicy *pol;
301 if (!bb->vm_ops || !bb->vm_ops->get_policy)
302 return vma->vm_policy;
304 if (!sysfs_get_active_two(attr_sd))
305 return vma->vm_policy;
307 pol = bb->vm_ops->get_policy(vma, addr);
309 sysfs_put_active_two(attr_sd);
310 return pol;
313 static int bin_migrate(struct vm_area_struct *vma, const nodemask_t *from,
314 const nodemask_t *to, unsigned long flags)
316 struct file *file = vma->vm_file;
317 struct bin_buffer *bb = file->private_data;
318 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
319 int ret;
321 if (!bb->vm_ops || !bb->vm_ops->migrate)
322 return 0;
324 if (!sysfs_get_active_two(attr_sd))
325 return 0;
327 ret = bb->vm_ops->migrate(vma, from, to, flags);
329 sysfs_put_active_two(attr_sd);
330 return ret;
332 #endif
334 static struct vm_operations_struct bin_vm_ops = {
335 .open = bin_vma_open,
336 .close = bin_vma_close,
337 .fault = bin_fault,
338 .page_mkwrite = bin_page_mkwrite,
339 .access = bin_access,
340 #ifdef CONFIG_NUMA
341 .set_policy = bin_set_policy,
342 .get_policy = bin_get_policy,
343 .migrate = bin_migrate,
344 #endif
347 static int mmap(struct file *file, struct vm_area_struct *vma)
349 struct bin_buffer *bb = file->private_data;
350 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
351 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
352 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
353 int rc;
355 mutex_lock(&bb->mutex);
357 /* need attr_sd for attr, its parent for kobj */
358 rc = -ENODEV;
359 if (!sysfs_get_active_two(attr_sd))
360 goto out_unlock;
362 rc = -EINVAL;
363 if (!attr->mmap)
364 goto out_put;
366 rc = attr->mmap(kobj, attr, vma);
367 if (rc)
368 goto out_put;
371 * PowerPC's pci_mmap of legacy_mem uses shmem_zero_setup()
372 * to satisfy versions of X which crash if the mmap fails: that
373 * substitutes a new vm_file, and we don't then want bin_vm_ops.
375 if (vma->vm_file != file)
376 goto out_put;
378 rc = -EINVAL;
379 if (bb->mmapped && bb->vm_ops != vma->vm_ops)
380 goto out_put;
382 rc = 0;
383 bb->mmapped = 1;
384 bb->vm_ops = vma->vm_ops;
385 vma->vm_ops = &bin_vm_ops;
386 out_put:
387 sysfs_put_active_two(attr_sd);
388 out_unlock:
389 mutex_unlock(&bb->mutex);
391 return rc;
394 static int open(struct inode * inode, struct file * file)
396 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
397 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
398 struct bin_buffer *bb = NULL;
399 int error;
401 /* binary file operations requires both @sd and its parent */
402 if (!sysfs_get_active_two(attr_sd))
403 return -ENODEV;
405 error = -EACCES;
406 if ((file->f_mode & FMODE_WRITE) && !(attr->write || attr->mmap))
407 goto err_out;
408 if ((file->f_mode & FMODE_READ) && !(attr->read || attr->mmap))
409 goto err_out;
411 error = -ENOMEM;
412 bb = kzalloc(sizeof(*bb), GFP_KERNEL);
413 if (!bb)
414 goto err_out;
416 bb->buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
417 if (!bb->buffer)
418 goto err_out;
420 mutex_init(&bb->mutex);
421 bb->file = file;
422 file->private_data = bb;
424 mutex_lock(&sysfs_bin_lock);
425 hlist_add_head(&bb->list, &attr_sd->s_bin_attr.buffers);
426 mutex_unlock(&sysfs_bin_lock);
428 /* open succeeded, put active references */
429 sysfs_put_active_two(attr_sd);
430 return 0;
432 err_out:
433 sysfs_put_active_two(attr_sd);
434 kfree(bb);
435 return error;
438 static int release(struct inode * inode, struct file * file)
440 struct bin_buffer *bb = file->private_data;
442 mutex_lock(&sysfs_bin_lock);
443 hlist_del(&bb->list);
444 mutex_unlock(&sysfs_bin_lock);
446 kfree(bb->buffer);
447 kfree(bb);
448 return 0;
451 const struct file_operations bin_fops = {
452 .read = read,
453 .write = write,
454 .mmap = mmap,
455 .llseek = generic_file_llseek,
456 .open = open,
457 .release = release,
461 void unmap_bin_file(struct sysfs_dirent *attr_sd)
463 struct bin_buffer *bb;
464 struct hlist_node *tmp;
466 if (sysfs_type(attr_sd) != SYSFS_KOBJ_BIN_ATTR)
467 return;
469 mutex_lock(&sysfs_bin_lock);
471 hlist_for_each_entry(bb, tmp, &attr_sd->s_bin_attr.buffers, list) {
472 struct inode *inode = bb->file->f_path.dentry->d_inode;
474 unmap_mapping_range(inode->i_mapping, 0, 0, 1);
477 mutex_unlock(&sysfs_bin_lock);
481 * sysfs_create_bin_file - create binary file for object.
482 * @kobj: object.
483 * @attr: attribute descriptor.
486 int sysfs_create_bin_file(struct kobject * kobj, struct bin_attribute * attr)
488 BUG_ON(!kobj || !kobj->sd || !attr);
490 return sysfs_add_file(kobj->sd, &attr->attr, SYSFS_KOBJ_BIN_ATTR);
495 * sysfs_remove_bin_file - remove binary file for object.
496 * @kobj: object.
497 * @attr: attribute descriptor.
500 void sysfs_remove_bin_file(struct kobject * kobj, struct bin_attribute * attr)
502 sysfs_hash_and_remove(kobj->sd, attr->attr.name);
505 EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
506 EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);