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