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. BUG_ON(count > (ssize_t)SIMPLE_ATTR_SIZE);
  75. if (count >= 0) {
  76. buffer->needs_read_fill = 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. goto out;
  296. }
  297. tbuf = vmalloc(*ppos + count);
  298. if (tbuf == NULL) {
  299. len = -ENOMEM;
  300. goto out;
  301. }
  302. /* copy old contents */
  303. if (buffer->bin_buffer) {
  304. memcpy(tbuf, buffer->bin_buffer,
  305. buffer->bin_buffer_size);
  306. vfree(buffer->bin_buffer);
  307. }
  308. /* clear the new area */
  309. memset(tbuf + buffer->bin_buffer_size, 0,
  310. *ppos + count - buffer->bin_buffer_size);
  311. buffer->bin_buffer = tbuf;
  312. buffer->bin_buffer_size = *ppos + count;
  313. }
  314. len = simple_write_to_buffer(buffer->bin_buffer,
  315. buffer->bin_buffer_size, ppos, buf, count);
  316. out:
  317. mutex_unlock(&buffer->mutex);
  318. return len;
  319. }
  320. static int check_perm(struct inode * inode, struct file * file, int type)
  321. {
  322. struct config_item *item = configfs_get_config_item(file->f_path.dentry->d_parent);
  323. struct configfs_attribute * attr = to_attr(file->f_path.dentry);
  324. struct configfs_bin_attribute *bin_attr = NULL;
  325. struct configfs_buffer * buffer;
  326. struct configfs_item_operations * ops = NULL;
  327. int error = 0;
  328. if (!item || !attr)
  329. goto Einval;
  330. if (type & CONFIGFS_ITEM_BIN_ATTR)
  331. bin_attr = to_bin_attr(file->f_path.dentry);
  332. /* Grab the module reference for this attribute if we have one */
  333. if (!try_module_get(attr->ca_owner)) {
  334. error = -ENODEV;
  335. goto Done;
  336. }
  337. if (item->ci_type)
  338. ops = item->ci_type->ct_item_ops;
  339. else
  340. goto Eaccess;
  341. /* File needs write support.
  342. * The inode's perms must say it's ok,
  343. * and we must have a store method.
  344. */
  345. if (file->f_mode & FMODE_WRITE) {
  346. if (!(inode->i_mode & S_IWUGO))
  347. goto Eaccess;
  348. if ((type & CONFIGFS_ITEM_ATTR) && !attr->store)
  349. goto Eaccess;
  350. if ((type & CONFIGFS_ITEM_BIN_ATTR) && !bin_attr->write)
  351. goto Eaccess;
  352. }
  353. /* File needs read support.
  354. * The inode's perms must say it's ok, and we there
  355. * must be a show method for it.
  356. */
  357. if (file->f_mode & FMODE_READ) {
  358. if (!(inode->i_mode & S_IRUGO))
  359. goto Eaccess;
  360. if ((type & CONFIGFS_ITEM_ATTR) && !attr->show)
  361. goto Eaccess;
  362. if ((type & CONFIGFS_ITEM_BIN_ATTR) && !bin_attr->read)
  363. goto Eaccess;
  364. }
  365. /* No error? Great, allocate a buffer for the file, and store it
  366. * it in file->private_data for easy access.
  367. */
  368. buffer = kzalloc(sizeof(struct configfs_buffer),GFP_KERNEL);
  369. if (!buffer) {
  370. error = -ENOMEM;
  371. goto Enomem;
  372. }
  373. mutex_init(&buffer->mutex);
  374. buffer->needs_read_fill = 1;
  375. buffer->read_in_progress = 0;
  376. buffer->write_in_progress = 0;
  377. buffer->ops = ops;
  378. file->private_data = buffer;
  379. goto Done;
  380. Einval:
  381. error = -EINVAL;
  382. goto Done;
  383. Eaccess:
  384. error = -EACCES;
  385. Enomem:
  386. module_put(attr->ca_owner);
  387. Done:
  388. if (error && item)
  389. config_item_put(item);
  390. return error;
  391. }
  392. static int configfs_release(struct inode *inode, struct file *filp)
  393. {
  394. struct config_item * item = to_item(filp->f_path.dentry->d_parent);
  395. struct configfs_attribute * attr = to_attr(filp->f_path.dentry);
  396. struct module * owner = attr->ca_owner;
  397. struct configfs_buffer * buffer = filp->private_data;
  398. if (item)
  399. config_item_put(item);
  400. /* After this point, attr should not be accessed. */
  401. module_put(owner);
  402. if (buffer) {
  403. if (buffer->page)
  404. free_page((unsigned long)buffer->page);
  405. mutex_destroy(&buffer->mutex);
  406. kfree(buffer);
  407. }
  408. return 0;
  409. }
  410. static int configfs_open_file(struct inode *inode, struct file *filp)
  411. {
  412. return check_perm(inode, filp, CONFIGFS_ITEM_ATTR);
  413. }
  414. static int configfs_open_bin_file(struct inode *inode, struct file *filp)
  415. {
  416. return check_perm(inode, filp, CONFIGFS_ITEM_BIN_ATTR);
  417. }
  418. static int configfs_release_bin_file(struct inode *inode, struct file *filp)
  419. {
  420. struct configfs_buffer *buffer = filp->private_data;
  421. struct dentry *dentry = filp->f_path.dentry;
  422. struct config_item *item = to_item(dentry->d_parent);
  423. struct configfs_bin_attribute *bin_attr = to_bin_attr(dentry);
  424. ssize_t len = 0;
  425. int ret;
  426. buffer->read_in_progress = 0;
  427. if (buffer->write_in_progress) {
  428. buffer->write_in_progress = 0;
  429. len = bin_attr->write(item, buffer->bin_buffer,
  430. buffer->bin_buffer_size);
  431. /* vfree on NULL is safe */
  432. vfree(buffer->bin_buffer);
  433. buffer->bin_buffer = NULL;
  434. buffer->bin_buffer_size = 0;
  435. buffer->needs_read_fill = 1;
  436. }
  437. ret = configfs_release(inode, filp);
  438. if (len < 0)
  439. return len;
  440. return ret;
  441. }
  442. const struct file_operations configfs_file_operations = {
  443. .read = configfs_read_file,
  444. .write = configfs_write_file,
  445. .llseek = generic_file_llseek,
  446. .open = configfs_open_file,
  447. .release = configfs_release,
  448. };
  449. const struct file_operations configfs_bin_file_operations = {
  450. .read = configfs_read_bin_file,
  451. .write = configfs_write_bin_file,
  452. .llseek = NULL, /* bin file is not seekable */
  453. .open = configfs_open_bin_file,
  454. .release = configfs_release_bin_file,
  455. };
  456. /**
  457. * configfs_create_file - create an attribute file for an item.
  458. * @item: item we're creating for.
  459. * @attr: atrribute descriptor.
  460. */
  461. int configfs_create_file(struct config_item * item, const struct configfs_attribute * attr)
  462. {
  463. struct dentry *dir = item->ci_dentry;
  464. struct configfs_dirent *parent_sd = dir->d_fsdata;
  465. umode_t mode = (attr->ca_mode & S_IALLUGO) | S_IFREG;
  466. int error = 0;
  467. inode_lock_nested(d_inode(dir), I_MUTEX_NORMAL);
  468. error = configfs_make_dirent(parent_sd, NULL, (void *) attr, mode,
  469. CONFIGFS_ITEM_ATTR);
  470. inode_unlock(d_inode(dir));
  471. return error;
  472. }
  473. /**
  474. * configfs_create_bin_file - create a binary attribute file for an item.
  475. * @item: item we're creating for.
  476. * @attr: atrribute descriptor.
  477. */
  478. int configfs_create_bin_file(struct config_item *item,
  479. const struct configfs_bin_attribute *bin_attr)
  480. {
  481. struct dentry *dir = item->ci_dentry;
  482. struct configfs_dirent *parent_sd = dir->d_fsdata;
  483. umode_t mode = (bin_attr->cb_attr.ca_mode & S_IALLUGO) | S_IFREG;
  484. int error = 0;
  485. inode_lock_nested(dir->d_inode, I_MUTEX_NORMAL);
  486. error = configfs_make_dirent(parent_sd, NULL, (void *) bin_attr, mode,
  487. CONFIGFS_ITEM_BIN_ATTR);
  488. inode_unlock(dir->d_inode);
  489. return error;
  490. }