bin.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * bin.c - binary file operations for sysfs.
  3. *
  4. * Copyright (c) 2003 Patrick Mochel
  5. * Copyright (c) 2003 Matthew Wilcox
  6. * Copyright (c) 2004 Silicon Graphics, Inc.
  7. */
  8. #undef DEBUG
  9. #include <linux/errno.h>
  10. #include <linux/fs.h>
  11. #include <linux/kernel.h>
  12. #include <linux/kobject.h>
  13. #include <linux/module.h>
  14. #include <linux/slab.h>
  15. #include <asm/uaccess.h>
  16. #include <asm/semaphore.h>
  17. #include "sysfs.h"
  18. static int
  19. fill_read(struct dentry *dentry, char *buffer, loff_t off, size_t count)
  20. {
  21. struct bin_attribute * attr = to_bin_attr(dentry);
  22. struct kobject * kobj = to_kobj(dentry->d_parent);
  23. if (!attr->read)
  24. return -EIO;
  25. return attr->read(kobj, buffer, off, count);
  26. }
  27. static ssize_t
  28. read(struct file *file, char __user *userbuf, size_t bytes, loff_t *off)
  29. {
  30. char *buffer = file->private_data;
  31. struct dentry *dentry = file->f_path.dentry;
  32. int size = dentry->d_inode->i_size;
  33. loff_t offs = *off;
  34. int count = min_t(size_t, bytes, PAGE_SIZE);
  35. if (size) {
  36. if (offs > size)
  37. return 0;
  38. if (offs + count > size)
  39. count = size - offs;
  40. }
  41. count = fill_read(dentry, buffer, offs, count);
  42. if (count < 0)
  43. return count;
  44. if (copy_to_user(userbuf, buffer, count))
  45. return -EFAULT;
  46. pr_debug("offs = %lld, *off = %lld, count = %d\n", offs, *off, count);
  47. *off = offs + count;
  48. return count;
  49. }
  50. static int
  51. flush_write(struct dentry *dentry, char *buffer, loff_t offset, size_t count)
  52. {
  53. struct bin_attribute *attr = to_bin_attr(dentry);
  54. struct kobject *kobj = to_kobj(dentry->d_parent);
  55. if (!attr->write)
  56. return -EIO;
  57. return attr->write(kobj, buffer, offset, count);
  58. }
  59. static ssize_t write(struct file *file, const char __user *userbuf,
  60. size_t bytes, loff_t *off)
  61. {
  62. char *buffer = file->private_data;
  63. struct dentry *dentry = file->f_path.dentry;
  64. int size = dentry->d_inode->i_size;
  65. loff_t offs = *off;
  66. int count = min_t(size_t, bytes, PAGE_SIZE);
  67. if (size) {
  68. if (offs > size)
  69. return 0;
  70. if (offs + count > size)
  71. count = size - offs;
  72. }
  73. if (copy_from_user(buffer, userbuf, count))
  74. return -EFAULT;
  75. count = flush_write(dentry, buffer, offs, count);
  76. if (count > 0)
  77. *off = offs + count;
  78. return count;
  79. }
  80. static int mmap(struct file *file, struct vm_area_struct *vma)
  81. {
  82. struct dentry *dentry = file->f_path.dentry;
  83. struct bin_attribute *attr = to_bin_attr(dentry);
  84. struct kobject *kobj = to_kobj(dentry->d_parent);
  85. if (!attr->mmap)
  86. return -EINVAL;
  87. return attr->mmap(kobj, attr, vma);
  88. }
  89. static int open(struct inode * inode, struct file * file)
  90. {
  91. struct kobject *kobj = sysfs_get_kobject(file->f_path.dentry->d_parent);
  92. struct bin_attribute * attr = to_bin_attr(file->f_path.dentry);
  93. int error = -EINVAL;
  94. if (!kobj || !attr)
  95. goto Done;
  96. /* Grab the module reference for this attribute if we have one */
  97. error = -ENODEV;
  98. if (!try_module_get(attr->attr.owner))
  99. goto Done;
  100. error = -EACCES;
  101. if ((file->f_mode & FMODE_WRITE) && !(attr->write || attr->mmap))
  102. goto Error;
  103. if ((file->f_mode & FMODE_READ) && !(attr->read || attr->mmap))
  104. goto Error;
  105. error = -ENOMEM;
  106. file->private_data = kmalloc(PAGE_SIZE, GFP_KERNEL);
  107. if (!file->private_data)
  108. goto Error;
  109. error = 0;
  110. goto Done;
  111. Error:
  112. module_put(attr->attr.owner);
  113. Done:
  114. if (error)
  115. kobject_put(kobj);
  116. return error;
  117. }
  118. static int release(struct inode * inode, struct file * file)
  119. {
  120. struct kobject * kobj = to_kobj(file->f_path.dentry->d_parent);
  121. struct bin_attribute * attr = to_bin_attr(file->f_path.dentry);
  122. u8 * buffer = file->private_data;
  123. kobject_put(kobj);
  124. module_put(attr->attr.owner);
  125. kfree(buffer);
  126. return 0;
  127. }
  128. const struct file_operations bin_fops = {
  129. .read = read,
  130. .write = write,
  131. .mmap = mmap,
  132. .llseek = generic_file_llseek,
  133. .open = open,
  134. .release = release,
  135. };
  136. /**
  137. * sysfs_create_bin_file - create binary file for object.
  138. * @kobj: object.
  139. * @attr: attribute descriptor.
  140. */
  141. int sysfs_create_bin_file(struct kobject * kobj, struct bin_attribute * attr)
  142. {
  143. BUG_ON(!kobj || !kobj->dentry || !attr);
  144. return sysfs_add_file(kobj->dentry, &attr->attr, SYSFS_KOBJ_BIN_ATTR);
  145. }
  146. /**
  147. * sysfs_remove_bin_file - remove binary file for object.
  148. * @kobj: object.
  149. * @attr: attribute descriptor.
  150. */
  151. void sysfs_remove_bin_file(struct kobject * kobj, struct bin_attribute * attr)
  152. {
  153. if (sysfs_hash_and_remove(kobj->dentry, attr->attr.name) < 0) {
  154. printk(KERN_ERR "%s: "
  155. "bad dentry or inode or no such file: \"%s\"\n",
  156. __FUNCTION__, attr->attr.name);
  157. dump_stack();
  158. }
  159. }
  160. EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
  161. EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);