file.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 <asm/uaccess.h>
  30. #include <asm/semaphore.h>
  31. #include <linux/configfs.h>
  32. #include "configfs_internal.h"
  33. struct configfs_buffer {
  34. size_t count;
  35. loff_t pos;
  36. char * page;
  37. struct configfs_item_operations * ops;
  38. struct semaphore sem;
  39. int needs_read_fill;
  40. };
  41. /**
  42. * fill_read_buffer - allocate and fill buffer from item.
  43. * @dentry: dentry pointer.
  44. * @buffer: data buffer for file.
  45. *
  46. * Allocate @buffer->page, if it hasn't been already, then call the
  47. * config_item's show() method to fill the buffer with this attribute's
  48. * data.
  49. * This is called only once, on the file's first read.
  50. */
  51. static int fill_read_buffer(struct dentry * dentry, struct configfs_buffer * buffer)
  52. {
  53. struct configfs_attribute * attr = to_attr(dentry);
  54. struct config_item * item = to_item(dentry->d_parent);
  55. struct configfs_item_operations * ops = buffer->ops;
  56. int ret = 0;
  57. ssize_t count;
  58. if (!buffer->page)
  59. buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
  60. if (!buffer->page)
  61. return -ENOMEM;
  62. count = ops->show_attribute(item,attr,buffer->page);
  63. buffer->needs_read_fill = 0;
  64. BUG_ON(count > (ssize_t)PAGE_SIZE);
  65. if (count >= 0)
  66. buffer->count = count;
  67. else
  68. ret = count;
  69. return ret;
  70. }
  71. /**
  72. * flush_read_buffer - push buffer to userspace.
  73. * @buffer: data buffer for file.
  74. * @userbuf: user-passed buffer.
  75. * @count: number of bytes requested.
  76. * @ppos: file position.
  77. *
  78. * Copy the buffer we filled in fill_read_buffer() to userspace.
  79. * This is done at the reader's leisure, copying and advancing
  80. * the amount they specify each time.
  81. * This may be called continuously until the buffer is empty.
  82. */
  83. static int flush_read_buffer(struct configfs_buffer * buffer, char __user * buf,
  84. size_t count, loff_t * ppos)
  85. {
  86. int error;
  87. if (*ppos > buffer->count)
  88. return 0;
  89. if (count > (buffer->count - *ppos))
  90. count = buffer->count - *ppos;
  91. error = copy_to_user(buf,buffer->page + *ppos,count);
  92. if (!error)
  93. *ppos += count;
  94. return error ? -EFAULT : count;
  95. }
  96. /**
  97. * configfs_read_file - read an attribute.
  98. * @file: file pointer.
  99. * @buf: buffer to fill.
  100. * @count: number of bytes to read.
  101. * @ppos: starting offset in file.
  102. *
  103. * Userspace wants to read an attribute file. The attribute descriptor
  104. * is in the file's ->d_fsdata. The target item is in the directory's
  105. * ->d_fsdata.
  106. *
  107. * We call fill_read_buffer() to allocate and fill the buffer from the
  108. * item's show() method exactly once (if the read is happening from
  109. * the beginning of the file). That should fill the entire buffer with
  110. * all the data the item has to offer for that attribute.
  111. * We then call flush_read_buffer() to copy the buffer to userspace
  112. * in the increments specified.
  113. */
  114. static ssize_t
  115. configfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  116. {
  117. struct configfs_buffer * buffer = file->private_data;
  118. ssize_t retval = 0;
  119. down(&buffer->sem);
  120. if (buffer->needs_read_fill) {
  121. if ((retval = fill_read_buffer(file->f_path.dentry,buffer)))
  122. goto out;
  123. }
  124. pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",
  125. __FUNCTION__, count, *ppos, buffer->page);
  126. retval = flush_read_buffer(buffer,buf,count,ppos);
  127. out:
  128. up(&buffer->sem);
  129. return retval;
  130. }
  131. /**
  132. * fill_write_buffer - copy buffer from userspace.
  133. * @buffer: data buffer for file.
  134. * @buf: data from user.
  135. * @count: number of bytes in @userbuf.
  136. *
  137. * Allocate @buffer->page if it hasn't been already, then
  138. * copy the user-supplied buffer into it.
  139. */
  140. static int
  141. fill_write_buffer(struct configfs_buffer * buffer, const char __user * buf, size_t count)
  142. {
  143. int error;
  144. if (!buffer->page)
  145. buffer->page = (char *)__get_free_pages(GFP_KERNEL, 0);
  146. if (!buffer->page)
  147. return -ENOMEM;
  148. if (count >= PAGE_SIZE)
  149. count = PAGE_SIZE - 1;
  150. error = copy_from_user(buffer->page,buf,count);
  151. buffer->needs_read_fill = 1;
  152. /* if buf is assumed to contain a string, terminate it by \0,
  153. * so e.g. sscanf() can scan the string easily */
  154. buffer->page[count] = 0;
  155. return error ? -EFAULT : count;
  156. }
  157. /**
  158. * flush_write_buffer - push buffer to config_item.
  159. * @dentry: dentry to the attribute
  160. * @buffer: data buffer for file.
  161. * @count: number of bytes
  162. *
  163. * Get the correct pointers for the config_item and the attribute we're
  164. * dealing with, then call the store() method for the attribute,
  165. * passing the buffer that we acquired in fill_write_buffer().
  166. */
  167. static int
  168. flush_write_buffer(struct dentry * dentry, struct configfs_buffer * buffer, size_t count)
  169. {
  170. struct configfs_attribute * attr = to_attr(dentry);
  171. struct config_item * item = to_item(dentry->d_parent);
  172. struct configfs_item_operations * ops = buffer->ops;
  173. return ops->store_attribute(item,attr,buffer->page,count);
  174. }
  175. /**
  176. * configfs_write_file - write an attribute.
  177. * @file: file pointer
  178. * @buf: data to write
  179. * @count: number of bytes
  180. * @ppos: starting offset
  181. *
  182. * Similar to configfs_read_file(), though working in the opposite direction.
  183. * We allocate and fill the data from the user in fill_write_buffer(),
  184. * then push it to the config_item in flush_write_buffer().
  185. * There is no easy way for us to know if userspace is only doing a partial
  186. * write, so we don't support them. We expect the entire buffer to come
  187. * on the first write.
  188. * Hint: if you're writing a value, first read the file, modify only the
  189. * the value you're changing, then write entire buffer back.
  190. */
  191. static ssize_t
  192. configfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  193. {
  194. struct configfs_buffer * buffer = file->private_data;
  195. ssize_t len;
  196. down(&buffer->sem);
  197. len = fill_write_buffer(buffer, buf, count);
  198. if (len > 0)
  199. len = flush_write_buffer(file->f_path.dentry, buffer, count);
  200. if (len > 0)
  201. *ppos += len;
  202. up(&buffer->sem);
  203. return len;
  204. }
  205. static int check_perm(struct inode * inode, struct file * file)
  206. {
  207. struct config_item *item = configfs_get_config_item(file->f_path.dentry->d_parent);
  208. struct configfs_attribute * attr = to_attr(file->f_path.dentry);
  209. struct configfs_buffer * buffer;
  210. struct configfs_item_operations * ops = NULL;
  211. int error = 0;
  212. if (!item || !attr)
  213. goto Einval;
  214. /* Grab the module reference for this attribute if we have one */
  215. if (!try_module_get(attr->ca_owner)) {
  216. error = -ENODEV;
  217. goto Done;
  218. }
  219. if (item->ci_type)
  220. ops = item->ci_type->ct_item_ops;
  221. else
  222. goto Eaccess;
  223. /* File needs write support.
  224. * The inode's perms must say it's ok,
  225. * and we must have a store method.
  226. */
  227. if (file->f_mode & FMODE_WRITE) {
  228. if (!(inode->i_mode & S_IWUGO) || !ops->store_attribute)
  229. goto Eaccess;
  230. }
  231. /* File needs read support.
  232. * The inode's perms must say it's ok, and we there
  233. * must be a show method for it.
  234. */
  235. if (file->f_mode & FMODE_READ) {
  236. if (!(inode->i_mode & S_IRUGO) || !ops->show_attribute)
  237. goto Eaccess;
  238. }
  239. /* No error? Great, allocate a buffer for the file, and store it
  240. * it in file->private_data for easy access.
  241. */
  242. buffer = kzalloc(sizeof(struct configfs_buffer),GFP_KERNEL);
  243. if (!buffer) {
  244. error = -ENOMEM;
  245. goto Enomem;
  246. }
  247. init_MUTEX(&buffer->sem);
  248. buffer->needs_read_fill = 1;
  249. buffer->ops = ops;
  250. file->private_data = buffer;
  251. goto Done;
  252. Einval:
  253. error = -EINVAL;
  254. goto Done;
  255. Eaccess:
  256. error = -EACCES;
  257. Enomem:
  258. module_put(attr->ca_owner);
  259. Done:
  260. if (error && item)
  261. config_item_put(item);
  262. return error;
  263. }
  264. static int configfs_open_file(struct inode * inode, struct file * filp)
  265. {
  266. return check_perm(inode,filp);
  267. }
  268. static int configfs_release(struct inode * inode, struct file * filp)
  269. {
  270. struct config_item * item = to_item(filp->f_path.dentry->d_parent);
  271. struct configfs_attribute * attr = to_attr(filp->f_path.dentry);
  272. struct module * owner = attr->ca_owner;
  273. struct configfs_buffer * buffer = filp->private_data;
  274. if (item)
  275. config_item_put(item);
  276. /* After this point, attr should not be accessed. */
  277. module_put(owner);
  278. if (buffer) {
  279. if (buffer->page)
  280. free_page((unsigned long)buffer->page);
  281. kfree(buffer);
  282. }
  283. return 0;
  284. }
  285. const struct file_operations configfs_file_operations = {
  286. .read = configfs_read_file,
  287. .write = configfs_write_file,
  288. .llseek = generic_file_llseek,
  289. .open = configfs_open_file,
  290. .release = configfs_release,
  291. };
  292. int configfs_add_file(struct dentry * dir, const struct configfs_attribute * attr, int type)
  293. {
  294. struct configfs_dirent * parent_sd = dir->d_fsdata;
  295. umode_t mode = (attr->ca_mode & S_IALLUGO) | S_IFREG;
  296. int error = 0;
  297. mutex_lock(&dir->d_inode->i_mutex);
  298. error = configfs_make_dirent(parent_sd, NULL, (void *) attr, mode, type);
  299. mutex_unlock(&dir->d_inode->i_mutex);
  300. return error;
  301. }
  302. /**
  303. * configfs_create_file - create an attribute file for an item.
  304. * @item: item we're creating for.
  305. * @attr: atrribute descriptor.
  306. */
  307. int configfs_create_file(struct config_item * item, const struct configfs_attribute * attr)
  308. {
  309. BUG_ON(!item || !item->ci_dentry || !attr);
  310. return configfs_add_file(item->ci_dentry, attr,
  311. CONFIGFS_ITEM_ATTR);
  312. }