file.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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 <linux/vmalloc.h>
  31. #include <asm/uaccess.h>
  32. #include <linux/configfs.h>
  33. #include "configfs_internal.h"
  34. /*
  35. * A simple attribute can only be 4096 characters. Why 4k? Because the
  36. * original code limited it to PAGE_SIZE. That's a bad idea, though,
  37. * because an attribute of 16k on ia64 won't work on x86. So we limit to
  38. * 4k, our minimum common page size.
  39. */
  40. #define SIMPLE_ATTR_SIZE 4096
  41. struct configfs_buffer {
  42. size_t count;
  43. loff_t pos;
  44. char * page;
  45. struct configfs_item_operations * ops;
  46. struct mutex mutex;
  47. int needs_read_fill;
  48. bool read_in_progress;
  49. bool write_in_progress;
  50. char *bin_buffer;
  51. int bin_buffer_size;
  52. };
  53. /**
  54. * fill_read_buffer - allocate and fill buffer from item.
  55. * @dentry: dentry pointer.
  56. * @buffer: data buffer for file.
  57. *
  58. * Allocate @buffer->page, if it hasn't been already, then call the
  59. * config_item's show() method to fill the buffer with this attribute's
  60. * data.
  61. * This is called only once, on the file's first read.
  62. */
  63. static int fill_read_buffer(struct dentry * dentry, struct configfs_buffer * buffer)
  64. {
  65. struct configfs_attribute * attr = to_attr(dentry);
  66. struct config_item * item = to_item(dentry->d_parent);
  67. int ret = 0;
  68. ssize_t count;
  69. if (!buffer->page)
  70. buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
  71. if (!buffer->page)
  72. return -ENOMEM;
  73. count = attr->show(item, buffer->page);
  74. buffer->needs_read_fill = 0;
  75. BUG_ON(count > (ssize_t)SIMPLE_ATTR_SIZE);
  76. if (count >= 0)
  77. buffer->count = count;
  78. else
  79. ret = count;
  80. return ret;
  81. }
  82. /**
  83. * configfs_read_file - read an attribute.
  84. * @file: file pointer.
  85. * @buf: buffer to fill.
  86. * @count: number of bytes to read.
  87. * @ppos: starting offset in file.
  88. *
  89. * Userspace wants to read an attribute file. The attribute descriptor
  90. * is in the file's ->d_fsdata. The target item is in the directory's
  91. * ->d_fsdata.
  92. *
  93. * We call fill_read_buffer() to allocate and fill the buffer from the
  94. * item's show() method exactly once (if the read is happening from
  95. * the beginning of the file). That should fill the entire buffer with
  96. * all the data the item has to offer for that attribute.
  97. * We then call flush_read_buffer() to copy the buffer to userspace
  98. * in the increments specified.
  99. */
  100. static ssize_t
  101. configfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  102. {
  103. struct configfs_buffer * buffer = file->private_data;
  104. ssize_t retval = 0;
  105. mutex_lock(&buffer->mutex);
  106. if (buffer->needs_read_fill) {
  107. if ((retval = fill_read_buffer(file->f_path.dentry,buffer)))
  108. goto out;
  109. }
  110. pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",
  111. __func__, count, *ppos, buffer->page);
  112. retval = simple_read_from_buffer(buf, count, ppos, buffer->page,
  113. buffer->count);
  114. out:
  115. mutex_unlock(&buffer->mutex);
  116. return retval;
  117. }
  118. /**
  119. * configfs_read_bin_file - read a binary attribute.
  120. * @file: file pointer.
  121. * @buf: buffer to fill.
  122. * @count: number of bytes to read.
  123. * @ppos: starting offset in file.
  124. *
  125. * Userspace wants to read a binary attribute file. The attribute
  126. * descriptor is in the file's ->d_fsdata. The target item is in the
  127. * directory's ->d_fsdata.
  128. *
  129. * We check whether we need to refill the buffer. If so we will
  130. * call the attributes' attr->read() twice. The first time we
  131. * will pass a NULL as a buffer pointer, which the attributes' method
  132. * will use to return the size of the buffer required. If no error
  133. * occurs we will allocate the buffer using vmalloc and call
  134. * attr->read() again passing that buffer as an argument.
  135. * Then we just copy to user-space using simple_read_from_buffer.
  136. */
  137. static ssize_t
  138. configfs_read_bin_file(struct file *file, char __user *buf,
  139. size_t count, loff_t *ppos)
  140. {
  141. struct configfs_buffer *buffer = file->private_data;
  142. struct dentry *dentry = file->f_path.dentry;
  143. struct config_item *item = to_item(dentry->d_parent);
  144. struct configfs_bin_attribute *bin_attr = to_bin_attr(dentry);
  145. ssize_t retval = 0;
  146. ssize_t len = min_t(size_t, count, PAGE_SIZE);
  147. mutex_lock(&buffer->mutex);
  148. /* we don't support switching read/write modes */
  149. if (buffer->write_in_progress) {
  150. retval = -ETXTBSY;
  151. goto out;
  152. }
  153. buffer->read_in_progress = 1;
  154. if (buffer->needs_read_fill) {
  155. /* perform first read with buf == NULL to get extent */
  156. len = bin_attr->read(item, NULL, 0);
  157. if (len <= 0) {
  158. retval = len;
  159. goto out;
  160. }
  161. /* do not exceed the maximum value */
  162. if (bin_attr->cb_max_size && len > bin_attr->cb_max_size) {
  163. retval = -EFBIG;
  164. goto out;
  165. }
  166. buffer->bin_buffer = vmalloc(len);
  167. if (buffer->bin_buffer == NULL) {
  168. retval = -ENOMEM;
  169. goto out;
  170. }
  171. buffer->bin_buffer_size = len;
  172. /* perform second read to fill buffer */
  173. len = bin_attr->read(item, buffer->bin_buffer, len);
  174. if (len < 0) {
  175. retval = len;
  176. vfree(buffer->bin_buffer);
  177. buffer->bin_buffer_size = 0;
  178. buffer->bin_buffer = NULL;
  179. goto out;
  180. }
  181. buffer->needs_read_fill = 0;
  182. }
  183. retval = simple_read_from_buffer(buf, count, ppos, buffer->bin_buffer,
  184. buffer->bin_buffer_size);
  185. out:
  186. mutex_unlock(&buffer->mutex);
  187. return retval;
  188. }
  189. /**
  190. * fill_write_buffer - copy buffer from userspace.
  191. * @buffer: data buffer for file.
  192. * @buf: data from user.
  193. * @count: number of bytes in @userbuf.
  194. *
  195. * Allocate @buffer->page if it hasn't been already, then
  196. * copy the user-supplied buffer into it.
  197. */
  198. static int
  199. fill_write_buffer(struct configfs_buffer * buffer, const char __user * buf, size_t count)
  200. {
  201. int error;
  202. if (!buffer->page)
  203. buffer->page = (char *)__get_free_pages(GFP_KERNEL, 0);
  204. if (!buffer->page)
  205. return -ENOMEM;
  206. if (count >= SIMPLE_ATTR_SIZE)
  207. count = SIMPLE_ATTR_SIZE - 1;
  208. error = copy_from_user(buffer->page,buf,count);
  209. buffer->needs_read_fill = 1;
  210. /* if buf is assumed to contain a string, terminate it by \0,
  211. * so e.g. sscanf() can scan the string easily */
  212. buffer->page[count] = 0;
  213. return error ? -EFAULT : count;
  214. }
  215. /**
  216. * flush_write_buffer - push buffer to config_item.
  217. * @dentry: dentry to the attribute
  218. * @buffer: data buffer for file.
  219. * @count: number of bytes
  220. *
  221. * Get the correct pointers for the config_item and the attribute we're
  222. * dealing with, then call the store() method for the attribute,
  223. * passing the buffer that we acquired in fill_write_buffer().
  224. */
  225. static int
  226. flush_write_buffer(struct dentry * dentry, struct configfs_buffer * buffer, size_t count)
  227. {
  228. struct configfs_attribute * attr = to_attr(dentry);
  229. struct config_item * item = to_item(dentry->d_parent);
  230. return attr->store(item, buffer->page, count);
  231. }
  232. /**
  233. * configfs_write_file - write an attribute.
  234. * @file: file pointer
  235. * @buf: data to write
  236. * @count: number of bytes
  237. * @ppos: starting offset
  238. *
  239. * Similar to configfs_read_file(), though working in the opposite direction.
  240. * We allocate and fill the data from the user in fill_write_buffer(),
  241. * then push it to the config_item in flush_write_buffer().
  242. * There is no easy way for us to know if userspace is only doing a partial
  243. * write, so we don't support them. We expect the entire buffer to come
  244. * on the first write.
  245. * Hint: if you're writing a value, first read the file, modify only the
  246. * the value you're changing, then write entire buffer back.
  247. */
  248. static ssize_t
  249. configfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  250. {
  251. struct configfs_buffer * buffer = file->private_data;
  252. ssize_t len;
  253. mutex_lock(&buffer->mutex);
  254. len = fill_write_buffer(buffer, buf, count);
  255. if (len > 0)
  256. len = flush_write_buffer(file->f_path.dentry, buffer, len);
  257. if (len > 0)
  258. *ppos += len;
  259. mutex_unlock(&buffer->mutex);
  260. return len;
  261. }
  262. /**
  263. * configfs_write_bin_file - write a binary attribute.
  264. * @file: file pointer
  265. * @buf: data to write
  266. * @count: number of bytes
  267. * @ppos: starting offset
  268. *
  269. * Writing to a binary attribute file is similar to a normal read.
  270. * We buffer the consecutive writes (binary attribute files do not
  271. * support lseek) in a continuously growing buffer, but we don't
  272. * commit until the close of the file.
  273. */
  274. static ssize_t
  275. configfs_write_bin_file(struct file *file, const char __user *buf,
  276. size_t count, loff_t *ppos)
  277. {
  278. struct configfs_buffer *buffer = file->private_data;
  279. struct dentry *dentry = file->f_path.dentry;
  280. struct configfs_bin_attribute *bin_attr = to_bin_attr(dentry);
  281. void *tbuf = NULL;
  282. ssize_t len;
  283. mutex_lock(&buffer->mutex);
  284. /* we don't support switching read/write modes */
  285. if (buffer->read_in_progress) {
  286. len = -ETXTBSY;
  287. goto out;
  288. }
  289. buffer->write_in_progress = 1;
  290. /* buffer grows? */
  291. if (*ppos + count > buffer->bin_buffer_size) {
  292. if (bin_attr->cb_max_size &&
  293. *ppos + count > bin_attr->cb_max_size) {
  294. len = -EFBIG;
  295. }
  296. tbuf = vmalloc(*ppos + count);
  297. if (tbuf == NULL) {
  298. len = -ENOMEM;
  299. goto out;
  300. }
  301. /* copy old contents */
  302. if (buffer->bin_buffer) {
  303. memcpy(tbuf, buffer->bin_buffer,
  304. buffer->bin_buffer_size);
  305. vfree(buffer->bin_buffer);
  306. }
  307. /* clear the new area */
  308. memset(tbuf + buffer->bin_buffer_size, 0,
  309. *ppos + count - buffer->bin_buffer_size);
  310. buffer->bin_buffer = tbuf;
  311. buffer->bin_buffer_size = *ppos + count;
  312. }
  313. len = simple_write_to_buffer(buffer->bin_buffer,
  314. buffer->bin_buffer_size, ppos, buf, count);
  315. if (len > 0)
  316. *ppos += len;
  317. out:
  318. mutex_unlock(&buffer->mutex);
  319. return len;
  320. }
  321. static int check_perm(struct inode * inode, struct file * file, int type)
  322. {
  323. struct config_item *item = configfs_get_config_item(file->f_path.dentry->d_parent);
  324. struct configfs_attribute * attr = to_attr(file->f_path.dentry);
  325. struct configfs_bin_attribute *bin_attr = NULL;
  326. struct configfs_buffer * buffer;
  327. struct configfs_item_operations * ops = NULL;
  328. int error = 0;
  329. if (!item || !attr)
  330. goto Einval;
  331. if (type & CONFIGFS_ITEM_BIN_ATTR)
  332. bin_attr = to_bin_attr(file->f_path.dentry);
  333. /* Grab the module reference for this attribute if we have one */
  334. if (!try_module_get(attr->ca_owner)) {
  335. error = -ENODEV;
  336. goto Done;
  337. }
  338. if (item->ci_type)
  339. ops = item->ci_type->ct_item_ops;
  340. else
  341. goto Eaccess;
  342. /* File needs write support.
  343. * The inode's perms must say it's ok,
  344. * and we must have a store method.
  345. */
  346. if (file->f_mode & FMODE_WRITE) {
  347. if (!(inode->i_mode & S_IWUGO))
  348. goto Eaccess;
  349. if ((type & CONFIGFS_ITEM_ATTR) && !attr->store)
  350. goto Eaccess;
  351. if ((type & CONFIGFS_ITEM_BIN_ATTR) && !bin_attr->write)
  352. goto Eaccess;
  353. }
  354. /* File needs read support.
  355. * The inode's perms must say it's ok, and we there
  356. * must be a show method for it.
  357. */
  358. if (file->f_mode & FMODE_READ) {
  359. if (!(inode->i_mode & S_IRUGO))
  360. goto Eaccess;
  361. if ((type & CONFIGFS_ITEM_ATTR) && !attr->show)
  362. goto Eaccess;
  363. if ((type & CONFIGFS_ITEM_BIN_ATTR) && !bin_attr->read)
  364. goto Eaccess;
  365. }
  366. /* No error? Great, allocate a buffer for the file, and store it
  367. * it in file->private_data for easy access.
  368. */
  369. buffer = kzalloc(sizeof(struct configfs_buffer),GFP_KERNEL);
  370. if (!buffer) {
  371. error = -ENOMEM;
  372. goto Enomem;
  373. }
  374. mutex_init(&buffer->mutex);
  375. buffer->needs_read_fill = 1;
  376. buffer->read_in_progress = 0;
  377. buffer->write_in_progress = 0;
  378. buffer->ops = ops;
  379. file->private_data = buffer;
  380. goto Done;
  381. Einval:
  382. error = -EINVAL;
  383. goto Done;
  384. Eaccess:
  385. error = -EACCES;
  386. Enomem:
  387. module_put(attr->ca_owner);
  388. Done:
  389. if (error && item)
  390. config_item_put(item);
  391. return error;
  392. }
  393. static int configfs_release(struct inode *inode, struct file *filp)
  394. {
  395. struct config_item * item = to_item(filp->f_path.dentry->d_parent);
  396. struct configfs_attribute * attr = to_attr(filp->f_path.dentry);
  397. struct module * owner = attr->ca_owner;
  398. struct configfs_buffer * buffer = filp->private_data;
  399. if (item)
  400. config_item_put(item);
  401. /* After this point, attr should not be accessed. */
  402. module_put(owner);
  403. if (buffer) {
  404. if (buffer->page)
  405. free_page((unsigned long)buffer->page);
  406. mutex_destroy(&buffer->mutex);
  407. kfree(buffer);
  408. }
  409. return 0;
  410. }
  411. static int configfs_open_file(struct inode *inode, struct file *filp)
  412. {
  413. return check_perm(inode, filp, CONFIGFS_ITEM_ATTR);
  414. }
  415. static int configfs_open_bin_file(struct inode *inode, struct file *filp)
  416. {
  417. return check_perm(inode, filp, CONFIGFS_ITEM_BIN_ATTR);
  418. }
  419. static int configfs_release_bin_file(struct inode *inode, struct file *filp)
  420. {
  421. struct configfs_buffer *buffer = filp->private_data;
  422. struct dentry *dentry = filp->f_path.dentry;
  423. struct config_item *item = to_item(dentry->d_parent);
  424. struct configfs_bin_attribute *bin_attr = to_bin_attr(dentry);
  425. ssize_t len = 0;
  426. int ret;
  427. buffer->read_in_progress = 0;
  428. if (buffer->write_in_progress) {
  429. buffer->write_in_progress = 0;
  430. len = bin_attr->write(item, buffer->bin_buffer,
  431. buffer->bin_buffer_size);
  432. /* vfree on NULL is safe */
  433. vfree(buffer->bin_buffer);
  434. buffer->bin_buffer = NULL;
  435. buffer->bin_buffer_size = 0;
  436. buffer->needs_read_fill = 1;
  437. }
  438. ret = configfs_release(inode, filp);
  439. if (len < 0)
  440. return len;
  441. return ret;
  442. }
  443. const struct file_operations configfs_file_operations = {
  444. .read = configfs_read_file,
  445. .write = configfs_write_file,
  446. .llseek = generic_file_llseek,
  447. .open = configfs_open_file,
  448. .release = configfs_release,
  449. };
  450. const struct file_operations configfs_bin_file_operations = {
  451. .read = configfs_read_bin_file,
  452. .write = configfs_write_bin_file,
  453. .llseek = NULL, /* bin file is not seekable */
  454. .open = configfs_open_bin_file,
  455. .release = configfs_release_bin_file,
  456. };
  457. /**
  458. * configfs_create_file - create an attribute file for an item.
  459. * @item: item we're creating for.
  460. * @attr: atrribute descriptor.
  461. */
  462. int configfs_create_file(struct config_item * item, const struct configfs_attribute * attr)
  463. {
  464. struct dentry *dir = item->ci_dentry;
  465. struct configfs_dirent *parent_sd = dir->d_fsdata;
  466. umode_t mode = (attr->ca_mode & S_IALLUGO) | S_IFREG;
  467. int error = 0;
  468. inode_lock_nested(d_inode(dir), I_MUTEX_NORMAL);
  469. error = configfs_make_dirent(parent_sd, NULL, (void *) attr, mode,
  470. CONFIGFS_ITEM_ATTR);
  471. inode_unlock(d_inode(dir));
  472. return error;
  473. }
  474. /**
  475. * configfs_create_bin_file - create a binary attribute file for an item.
  476. * @item: item we're creating for.
  477. * @attr: atrribute descriptor.
  478. */
  479. int configfs_create_bin_file(struct config_item *item,
  480. const struct configfs_bin_attribute *bin_attr)
  481. {
  482. struct dentry *dir = item->ci_dentry;
  483. struct configfs_dirent *parent_sd = dir->d_fsdata;
  484. umode_t mode = (bin_attr->cb_attr.ca_mode & S_IALLUGO) | S_IFREG;
  485. int error = 0;
  486. inode_lock_nested(dir->d_inode, I_MUTEX_NORMAL);
  487. error = configfs_make_dirent(parent_sd, NULL, (void *) bin_attr, mode,
  488. CONFIGFS_ITEM_BIN_ATTR);
  489. inode_unlock(dir->d_inode);
  490. return error;
  491. }