file.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * file.c - operations for regular (text) files.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public
  17. * License along with this program; if not, write to the
  18. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. * Boston, MA 021110-1307, USA.
  20. *
  21. * Based on sysfs:
  22. * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
  23. *
  24. * configfs Copyright (C) 2005 Oracle. All rights reserved.
  25. */
  26. #include <linux/fs.h>
  27. #include <linux/module.h>
  28. #include <linux/slab.h>
  29. #include <linux/mutex.h>
  30. #include <asm/uaccess.h>
  31. #include <linux/configfs.h>
  32. #include "configfs_internal.h"
  33. /*
  34. * A simple attribute can only be 4096 characters. Why 4k? Because the
  35. * original code limited it to PAGE_SIZE. That's a bad idea, though,
  36. * because an attribute of 16k on ia64 won't work on x86. So we limit to
  37. * 4k, our minimum common page size.
  38. */
  39. #define SIMPLE_ATTR_SIZE 4096
  40. struct configfs_buffer {
  41. size_t count;
  42. loff_t pos;
  43. char * page;
  44. struct configfs_item_operations * ops;
  45. struct mutex mutex;
  46. int needs_read_fill;
  47. };
  48. /**
  49. * fill_read_buffer - allocate and fill buffer from item.
  50. * @dentry: dentry pointer.
  51. * @buffer: data buffer for file.
  52. *
  53. * Allocate @buffer->page, if it hasn't been already, then call the
  54. * config_item's show() method to fill the buffer with this attribute's
  55. * data.
  56. * This is called only once, on the file's first read.
  57. */
  58. static int fill_read_buffer(struct dentry * dentry, struct configfs_buffer * buffer)
  59. {
  60. struct configfs_attribute * attr = to_attr(dentry);
  61. struct config_item * item = to_item(dentry->d_parent);
  62. int ret = 0;
  63. ssize_t count;
  64. if (!buffer->page)
  65. buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
  66. if (!buffer->page)
  67. return -ENOMEM;
  68. count = attr->show(item, buffer->page);
  69. buffer->needs_read_fill = 0;
  70. BUG_ON(count > (ssize_t)SIMPLE_ATTR_SIZE);
  71. if (count >= 0)
  72. buffer->count = count;
  73. else
  74. ret = count;
  75. return ret;
  76. }
  77. /**
  78. * configfs_read_file - read an attribute.
  79. * @file: file pointer.
  80. * @buf: buffer to fill.
  81. * @count: number of bytes to read.
  82. * @ppos: starting offset in file.
  83. *
  84. * Userspace wants to read an attribute file. The attribute descriptor
  85. * is in the file's ->d_fsdata. The target item is in the directory's
  86. * ->d_fsdata.
  87. *
  88. * We call fill_read_buffer() to allocate and fill the buffer from the
  89. * item's show() method exactly once (if the read is happening from
  90. * the beginning of the file). That should fill the entire buffer with
  91. * all the data the item has to offer for that attribute.
  92. * We then call flush_read_buffer() to copy the buffer to userspace
  93. * in the increments specified.
  94. */
  95. static ssize_t
  96. configfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  97. {
  98. struct configfs_buffer * buffer = file->private_data;
  99. ssize_t retval = 0;
  100. mutex_lock(&buffer->mutex);
  101. if (buffer->needs_read_fill) {
  102. if ((retval = fill_read_buffer(file->f_path.dentry,buffer)))
  103. goto out;
  104. }
  105. pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",
  106. __func__, count, *ppos, buffer->page);
  107. retval = simple_read_from_buffer(buf, count, ppos, buffer->page,
  108. buffer->count);
  109. out:
  110. mutex_unlock(&buffer->mutex);
  111. return retval;
  112. }
  113. /**
  114. * fill_write_buffer - copy buffer from userspace.
  115. * @buffer: data buffer for file.
  116. * @buf: data from user.
  117. * @count: number of bytes in @userbuf.
  118. *
  119. * Allocate @buffer->page if it hasn't been already, then
  120. * copy the user-supplied buffer into it.
  121. */
  122. static int
  123. fill_write_buffer(struct configfs_buffer * buffer, const char __user * buf, size_t count)
  124. {
  125. int error;
  126. if (!buffer->page)
  127. buffer->page = (char *)__get_free_pages(GFP_KERNEL, 0);
  128. if (!buffer->page)
  129. return -ENOMEM;
  130. if (count >= SIMPLE_ATTR_SIZE)
  131. count = SIMPLE_ATTR_SIZE - 1;
  132. error = copy_from_user(buffer->page,buf,count);
  133. buffer->needs_read_fill = 1;
  134. /* if buf is assumed to contain a string, terminate it by \0,
  135. * so e.g. sscanf() can scan the string easily */
  136. buffer->page[count] = 0;
  137. return error ? -EFAULT : count;
  138. }
  139. /**
  140. * flush_write_buffer - push buffer to config_item.
  141. * @dentry: dentry to the attribute
  142. * @buffer: data buffer for file.
  143. * @count: number of bytes
  144. *
  145. * Get the correct pointers for the config_item and the attribute we're
  146. * dealing with, then call the store() method for the attribute,
  147. * passing the buffer that we acquired in fill_write_buffer().
  148. */
  149. static int
  150. flush_write_buffer(struct dentry * dentry, struct configfs_buffer * buffer, size_t count)
  151. {
  152. struct configfs_attribute * attr = to_attr(dentry);
  153. struct config_item * item = to_item(dentry->d_parent);
  154. return attr->store(item, buffer->page, count);
  155. }
  156. /**
  157. * configfs_write_file - write an attribute.
  158. * @file: file pointer
  159. * @buf: data to write
  160. * @count: number of bytes
  161. * @ppos: starting offset
  162. *
  163. * Similar to configfs_read_file(), though working in the opposite direction.
  164. * We allocate and fill the data from the user in fill_write_buffer(),
  165. * then push it to the config_item in flush_write_buffer().
  166. * There is no easy way for us to know if userspace is only doing a partial
  167. * write, so we don't support them. We expect the entire buffer to come
  168. * on the first write.
  169. * Hint: if you're writing a value, first read the file, modify only the
  170. * the value you're changing, then write entire buffer back.
  171. */
  172. static ssize_t
  173. configfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  174. {
  175. struct configfs_buffer * buffer = file->private_data;
  176. ssize_t len;
  177. mutex_lock(&buffer->mutex);
  178. len = fill_write_buffer(buffer, buf, count);
  179. if (len > 0)
  180. len = flush_write_buffer(file->f_path.dentry, buffer, len);
  181. if (len > 0)
  182. *ppos += len;
  183. mutex_unlock(&buffer->mutex);
  184. return len;
  185. }
  186. static int check_perm(struct inode * inode, struct file * file)
  187. {
  188. struct config_item *item = configfs_get_config_item(file->f_path.dentry->d_parent);
  189. struct configfs_attribute * attr = to_attr(file->f_path.dentry);
  190. struct configfs_buffer * buffer;
  191. struct configfs_item_operations * ops = NULL;
  192. int error = 0;
  193. if (!item || !attr)
  194. goto Einval;
  195. /* Grab the module reference for this attribute if we have one */
  196. if (!try_module_get(attr->ca_owner)) {
  197. error = -ENODEV;
  198. goto Done;
  199. }
  200. if (item->ci_type)
  201. ops = item->ci_type->ct_item_ops;
  202. else
  203. goto Eaccess;
  204. /* File needs write support.
  205. * The inode's perms must say it's ok,
  206. * and we must have a store method.
  207. */
  208. if (file->f_mode & FMODE_WRITE) {
  209. if (!(inode->i_mode & S_IWUGO) || !attr->store)
  210. goto Eaccess;
  211. }
  212. /* File needs read support.
  213. * The inode's perms must say it's ok, and we there
  214. * must be a show method for it.
  215. */
  216. if (file->f_mode & FMODE_READ) {
  217. if (!(inode->i_mode & S_IRUGO) || !attr->show)
  218. goto Eaccess;
  219. }
  220. /* No error? Great, allocate a buffer for the file, and store it
  221. * it in file->private_data for easy access.
  222. */
  223. buffer = kzalloc(sizeof(struct configfs_buffer),GFP_KERNEL);
  224. if (!buffer) {
  225. error = -ENOMEM;
  226. goto Enomem;
  227. }
  228. mutex_init(&buffer->mutex);
  229. buffer->needs_read_fill = 1;
  230. buffer->ops = ops;
  231. file->private_data = buffer;
  232. goto Done;
  233. Einval:
  234. error = -EINVAL;
  235. goto Done;
  236. Eaccess:
  237. error = -EACCES;
  238. Enomem:
  239. module_put(attr->ca_owner);
  240. Done:
  241. if (error && item)
  242. config_item_put(item);
  243. return error;
  244. }
  245. static int configfs_open_file(struct inode * inode, struct file * filp)
  246. {
  247. return check_perm(inode,filp);
  248. }
  249. static int configfs_release(struct inode * inode, struct file * filp)
  250. {
  251. struct config_item * item = to_item(filp->f_path.dentry->d_parent);
  252. struct configfs_attribute * attr = to_attr(filp->f_path.dentry);
  253. struct module * owner = attr->ca_owner;
  254. struct configfs_buffer * buffer = filp->private_data;
  255. if (item)
  256. config_item_put(item);
  257. /* After this point, attr should not be accessed. */
  258. module_put(owner);
  259. if (buffer) {
  260. if (buffer->page)
  261. free_page((unsigned long)buffer->page);
  262. mutex_destroy(&buffer->mutex);
  263. kfree(buffer);
  264. }
  265. return 0;
  266. }
  267. const struct file_operations configfs_file_operations = {
  268. .read = configfs_read_file,
  269. .write = configfs_write_file,
  270. .llseek = generic_file_llseek,
  271. .open = configfs_open_file,
  272. .release = configfs_release,
  273. };
  274. /**
  275. * configfs_create_file - create an attribute file for an item.
  276. * @item: item we're creating for.
  277. * @attr: atrribute descriptor.
  278. */
  279. int configfs_create_file(struct config_item * item, const struct configfs_attribute * attr)
  280. {
  281. struct dentry *dir = item->ci_dentry;
  282. struct configfs_dirent *parent_sd = dir->d_fsdata;
  283. umode_t mode = (attr->ca_mode & S_IALLUGO) | S_IFREG;
  284. int error = 0;
  285. mutex_lock_nested(&d_inode(dir)->i_mutex, I_MUTEX_NORMAL);
  286. error = configfs_make_dirent(parent_sd, NULL, (void *) attr, mode,
  287. CONFIGFS_ITEM_ATTR);
  288. mutex_unlock(&d_inode(dir)->i_mutex);
  289. return error;
  290. }