file.c 9.6 KB

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