vfs: remove extraneous NULL d_inode check from do_filp_open
[linux-2.6-block.git] / fs / sysfs / bin.c
CommitLineData
1da177e4 1/*
6d66f5cd 2 * fs/sysfs/bin.c - sysfs binary file implementation
1da177e4
LT
3 *
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Matthew Wilcox
6 * Copyright (c) 2004 Silicon Graphics, Inc.
6d66f5cd
TH
7 * Copyright (c) 2007 SUSE Linux Products GmbH
8 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
9 *
10 * This file is released under the GPLv2.
11 *
12 * Please see Documentation/filesystems/sysfs.txt for more information.
1da177e4
LT
13 */
14
15#undef DEBUG
16
17#include <linux/errno.h>
18#include <linux/fs.h>
995982ca 19#include <linux/kernel.h>
1da177e4
LT
20#include <linux/kobject.h>
21#include <linux/module.h>
22#include <linux/slab.h>
869512ab 23#include <linux/mutex.h>
e0edd3c6 24#include <linux/mm.h>
1da177e4
LT
25
26#include <asm/uaccess.h>
27
28#include "sysfs.h"
29
e0edd3c6
EB
30/*
31 * There's one bin_buffer for each open file.
32 *
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
36 */
37static DEFINE_MUTEX(sysfs_bin_lock);
38
eb361653 39struct bin_buffer {
e0edd3c6
EB
40 struct mutex mutex;
41 void *buffer;
42 int mmapped;
f0f37e2f 43 const struct vm_operations_struct *vm_ops;
e0edd3c6
EB
44 struct file *file;
45 struct hlist_node list;
eb361653
TH
46};
47
1da177e4
LT
48static int
49fill_read(struct dentry *dentry, char *buffer, loff_t off, size_t count)
50{
3e519038 51 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
b1fc3d61
TH
52 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
53 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
0ab66088
TH
54 int rc;
55
56 /* need attr_sd for attr, its parent for kobj */
57 if (!sysfs_get_active_two(attr_sd))
58 return -ENODEV;
1da177e4 59
0ab66088
TH
60 rc = -EIO;
61 if (attr->read)
91a69029 62 rc = attr->read(kobj, attr, buffer, off, count);
1da177e4 63
0ab66088
TH
64 sysfs_put_active_two(attr_sd);
65
66 return rc;
1da177e4
LT
67}
68
69static ssize_t
93e3cd82 70read(struct file *file, char __user *userbuf, size_t bytes, loff_t *off)
1da177e4 71{
eb361653 72 struct bin_buffer *bb = file->private_data;
f427f5d5 73 struct dentry *dentry = file->f_path.dentry;
1da177e4
LT
74 int size = dentry->d_inode->i_size;
75 loff_t offs = *off;
93e3cd82 76 int count = min_t(size_t, bytes, PAGE_SIZE);
b31ca3f5 77 char *temp;
1da177e4 78
4503efd0
GKH
79 if (!bytes)
80 return 0;
81
1da177e4
LT
82 if (size) {
83 if (offs > size)
84 return 0;
85 if (offs + count > size)
86 count = size - offs;
87 }
88
b31ca3f5
NP
89 temp = kmalloc(count, GFP_KERNEL);
90 if (!temp)
91 return -ENOMEM;
92
eb361653
TH
93 mutex_lock(&bb->mutex);
94
95 count = fill_read(dentry, bb->buffer, offs, count);
b31ca3f5
NP
96 if (count < 0) {
97 mutex_unlock(&bb->mutex);
98 goto out_free;
99 }
1da177e4 100
b31ca3f5
NP
101 memcpy(temp, bb->buffer, count);
102
103 mutex_unlock(&bb->mutex);
104
105 if (copy_to_user(userbuf, temp, count)) {
eb361653 106 count = -EFAULT;
b31ca3f5 107 goto out_free;
eb361653 108 }
1da177e4 109
93e3cd82 110 pr_debug("offs = %lld, *off = %lld, count = %d\n", offs, *off, count);
1da177e4
LT
111
112 *off = offs + count;
113
b31ca3f5
NP
114 out_free:
115 kfree(temp);
1da177e4
LT
116 return count;
117}
118
119static int
120flush_write(struct dentry *dentry, char *buffer, loff_t offset, size_t count)
121{
3e519038 122 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
b1fc3d61
TH
123 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
124 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
0ab66088
TH
125 int rc;
126
127 /* need attr_sd for attr, its parent for kobj */
128 if (!sysfs_get_active_two(attr_sd))
129 return -ENODEV;
1da177e4 130
0ab66088
TH
131 rc = -EIO;
132 if (attr->write)
91a69029 133 rc = attr->write(kobj, attr, buffer, offset, count);
1da177e4 134
0ab66088
TH
135 sysfs_put_active_two(attr_sd);
136
137 return rc;
1da177e4
LT
138}
139
93e3cd82
TH
140static ssize_t write(struct file *file, const char __user *userbuf,
141 size_t bytes, loff_t *off)
1da177e4 142{
eb361653 143 struct bin_buffer *bb = file->private_data;
f427f5d5 144 struct dentry *dentry = file->f_path.dentry;
1da177e4
LT
145 int size = dentry->d_inode->i_size;
146 loff_t offs = *off;
93e3cd82 147 int count = min_t(size_t, bytes, PAGE_SIZE);
b31ca3f5 148 char *temp;
1da177e4 149
4503efd0
GKH
150 if (!bytes)
151 return 0;
152
1da177e4
LT
153 if (size) {
154 if (offs > size)
155 return 0;
156 if (offs + count > size)
157 count = size - offs;
158 }
159
1c8542c7
LZ
160 temp = memdup_user(userbuf, count);
161 if (IS_ERR(temp))
162 return PTR_ERR(temp);
1da177e4 163
b31ca3f5
NP
164 mutex_lock(&bb->mutex);
165
166 memcpy(bb->buffer, temp, count);
167
eb361653 168 count = flush_write(dentry, bb->buffer, offs, count);
b31ca3f5
NP
169 mutex_unlock(&bb->mutex);
170
1da177e4
LT
171 if (count > 0)
172 *off = offs + count;
eb361653 173
d5ce5b40 174 kfree(temp);
1da177e4
LT
175 return count;
176}
177
e0edd3c6
EB
178static void bin_vma_open(struct vm_area_struct *vma)
179{
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;
183
184 if (!bb->vm_ops || !bb->vm_ops->open)
185 return;
186
187 if (!sysfs_get_active_two(attr_sd))
188 return;
189
190 bb->vm_ops->open(vma);
191
192 sysfs_put_active_two(attr_sd);
193}
194
195static void bin_vma_close(struct vm_area_struct *vma)
196{
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;
200
201 if (!bb->vm_ops || !bb->vm_ops->close)
202 return;
203
204 if (!sysfs_get_active_two(attr_sd))
205 return;
206
207 bb->vm_ops->close(vma);
208
209 sysfs_put_active_two(attr_sd);
210}
211
212static int bin_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
213{
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;
218
219 if (!bb->vm_ops || !bb->vm_ops->fault)
220 return VM_FAULT_SIGBUS;
221
222 if (!sysfs_get_active_two(attr_sd))
223 return VM_FAULT_SIGBUS;
224
225 ret = bb->vm_ops->fault(vma, vmf);
226
227 sysfs_put_active_two(attr_sd);
228 return ret;
229}
230
851a039c 231static int bin_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
e0edd3c6
EB
232{
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;
237
095160ae 238 if (!bb->vm_ops)
851a039c 239 return VM_FAULT_SIGBUS;
e0edd3c6 240
095160ae
HD
241 if (!bb->vm_ops->page_mkwrite)
242 return 0;
243
e0edd3c6 244 if (!sysfs_get_active_two(attr_sd))
851a039c 245 return VM_FAULT_SIGBUS;
e0edd3c6 246
851a039c 247 ret = bb->vm_ops->page_mkwrite(vma, vmf);
e0edd3c6
EB
248
249 sysfs_put_active_two(attr_sd);
250 return ret;
251}
252
253static int bin_access(struct vm_area_struct *vma, unsigned long addr,
254 void *buf, int len, int write)
255{
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;
260
261 if (!bb->vm_ops || !bb->vm_ops->access)
262 return -EINVAL;
263
264 if (!sysfs_get_active_two(attr_sd))
265 return -EINVAL;
266
267 ret = bb->vm_ops->access(vma, addr, buf, len, write);
268
269 sysfs_put_active_two(attr_sd);
270 return ret;
271}
272
095160ae
HD
273#ifdef CONFIG_NUMA
274static int bin_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
275{
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;
280
281 if (!bb->vm_ops || !bb->vm_ops->set_policy)
282 return 0;
283
284 if (!sysfs_get_active_two(attr_sd))
285 return -EINVAL;
286
287 ret = bb->vm_ops->set_policy(vma, new);
288
289 sysfs_put_active_two(attr_sd);
290 return ret;
291}
292
293static struct mempolicy *bin_get_policy(struct vm_area_struct *vma,
294 unsigned long addr)
295{
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;
300
301 if (!bb->vm_ops || !bb->vm_ops->get_policy)
302 return vma->vm_policy;
303
304 if (!sysfs_get_active_two(attr_sd))
305 return vma->vm_policy;
306
307 pol = bb->vm_ops->get_policy(vma, addr);
308
309 sysfs_put_active_two(attr_sd);
310 return pol;
311}
312
313static int bin_migrate(struct vm_area_struct *vma, const nodemask_t *from,
314 const nodemask_t *to, unsigned long flags)
315{
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;
320
321 if (!bb->vm_ops || !bb->vm_ops->migrate)
322 return 0;
323
324 if (!sysfs_get_active_two(attr_sd))
325 return 0;
326
327 ret = bb->vm_ops->migrate(vma, from, to, flags);
328
329 sysfs_put_active_two(attr_sd);
330 return ret;
331}
332#endif
333
f0f37e2f 334static const struct vm_operations_struct bin_vm_ops = {
e0edd3c6
EB
335 .open = bin_vma_open,
336 .close = bin_vma_close,
337 .fault = bin_fault,
338 .page_mkwrite = bin_page_mkwrite,
339 .access = bin_access,
095160ae
HD
340#ifdef CONFIG_NUMA
341 .set_policy = bin_set_policy,
342 .get_policy = bin_get_policy,
343 .migrate = bin_migrate,
344#endif
e0edd3c6
EB
345};
346
1da177e4
LT
347static int mmap(struct file *file, struct vm_area_struct *vma)
348{
eb361653 349 struct bin_buffer *bb = file->private_data;
3e519038 350 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
b1fc3d61
TH
351 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
352 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
eb361653 353 int rc;
1da177e4 354
eb361653 355 mutex_lock(&bb->mutex);
0ab66088
TH
356
357 /* need attr_sd for attr, its parent for kobj */
e0edd3c6 358 rc = -ENODEV;
0ab66088 359 if (!sysfs_get_active_two(attr_sd))
e0edd3c6 360 goto out_unlock;
0ab66088
TH
361
362 rc = -EINVAL;
e0edd3c6
EB
363 if (!attr->mmap)
364 goto out_put;
0ab66088 365
e0edd3c6 366 rc = attr->mmap(kobj, attr, vma);
e0edd3c6
EB
367 if (rc)
368 goto out_put;
0ab66088 369
095160ae
HD
370 /*
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.
374 */
375 if (vma->vm_file != file)
e0edd3c6
EB
376 goto out_put;
377
e0edd3c6 378 rc = -EINVAL;
095160ae 379 if (bb->mmapped && bb->vm_ops != vma->vm_ops)
e0edd3c6 380 goto out_put;
e0edd3c6
EB
381
382 rc = 0;
383 bb->mmapped = 1;
095160ae
HD
384 bb->vm_ops = vma->vm_ops;
385 vma->vm_ops = &bin_vm_ops;
e0edd3c6
EB
386out_put:
387 sysfs_put_active_two(attr_sd);
388out_unlock:
eb361653
TH
389 mutex_unlock(&bb->mutex);
390
391 return rc;
1da177e4
LT
392}
393
394static int open(struct inode * inode, struct file * file)
395{
3e519038 396 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
b1fc3d61 397 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
eb361653 398 struct bin_buffer *bb = NULL;
0ab66088 399 int error;
1da177e4 400
078ce640
TH
401 /* binary file operations requires both @sd and its parent */
402 if (!sysfs_get_active_two(attr_sd))
0ab66088 403 return -ENODEV;
1da177e4 404
1da177e4
LT
405 error = -EACCES;
406 if ((file->f_mode & FMODE_WRITE) && !(attr->write || attr->mmap))
7b595756 407 goto err_out;
1da177e4 408 if ((file->f_mode & FMODE_READ) && !(attr->read || attr->mmap))
7b595756 409 goto err_out;
1da177e4
LT
410
411 error = -ENOMEM;
eb361653
TH
412 bb = kzalloc(sizeof(*bb), GFP_KERNEL);
413 if (!bb)
7b595756 414 goto err_out;
1da177e4 415
eb361653
TH
416 bb->buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
417 if (!bb->buffer)
7b595756 418 goto err_out;
eb361653
TH
419
420 mutex_init(&bb->mutex);
e0edd3c6 421 bb->file = file;
eb361653
TH
422 file->private_data = bb;
423
e0edd3c6
EB
424 mutex_lock(&sysfs_bin_lock);
425 hlist_add_head(&bb->list, &attr_sd->s_bin_attr.buffers);
426 mutex_unlock(&sysfs_bin_lock);
427
078ce640
TH
428 /* open succeeded, put active references */
429 sysfs_put_active_two(attr_sd);
0ab66088 430 return 0;
1da177e4 431
7b595756 432 err_out:
078ce640 433 sysfs_put_active_two(attr_sd);
0ab66088 434 kfree(bb);
1da177e4
LT
435 return error;
436}
437
438static int release(struct inode * inode, struct file * file)
439{
eb361653 440 struct bin_buffer *bb = file->private_data;
1da177e4 441
e0edd3c6
EB
442 mutex_lock(&sysfs_bin_lock);
443 hlist_del(&bb->list);
444 mutex_unlock(&sysfs_bin_lock);
445
eb361653
TH
446 kfree(bb->buffer);
447 kfree(bb);
1da177e4
LT
448 return 0;
449}
450
4b6f5d20 451const struct file_operations bin_fops = {
1da177e4
LT
452 .read = read,
453 .write = write,
454 .mmap = mmap,
455 .llseek = generic_file_llseek,
456 .open = open,
457 .release = release,
458};
459
e0edd3c6
EB
460
461void unmap_bin_file(struct sysfs_dirent *attr_sd)
462{
463 struct bin_buffer *bb;
464 struct hlist_node *tmp;
465
466 if (sysfs_type(attr_sd) != SYSFS_KOBJ_BIN_ATTR)
467 return;
468
469 mutex_lock(&sysfs_bin_lock);
470
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;
473
474 unmap_mapping_range(inode->i_mapping, 0, 0, 1);
475 }
476
477 mutex_unlock(&sysfs_bin_lock);
478}
479
1da177e4
LT
480/**
481 * sysfs_create_bin_file - create binary file for object.
482 * @kobj: object.
483 * @attr: attribute descriptor.
1da177e4
LT
484 */
485
486int sysfs_create_bin_file(struct kobject * kobj, struct bin_attribute * attr)
487{
608e266a 488 BUG_ON(!kobj || !kobj->sd || !attr);
1da177e4 489
608e266a 490 return sysfs_add_file(kobj->sd, &attr->attr, SYSFS_KOBJ_BIN_ATTR);
1da177e4
LT
491}
492
493
494/**
495 * sysfs_remove_bin_file - remove binary file for object.
496 * @kobj: object.
497 * @attr: attribute descriptor.
1da177e4
LT
498 */
499
995982ca 500void sysfs_remove_bin_file(struct kobject * kobj, struct bin_attribute * attr)
1da177e4 501{
5f1835da 502 sysfs_hash_and_remove(kobj->sd, attr->attr.name);
1da177e4
LT
503}
504
505EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
506EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);