file.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * JFFS2 -- Journalling Flash File System, Version 2.
  3. *
  4. * Copyright © 2001-2007 Red Hat, Inc.
  5. *
  6. * Created by David Woodhouse <dwmw2@infradead.org>
  7. *
  8. * For licensing information, see the file 'LICENCE' in this directory.
  9. *
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/slab.h>
  13. #include <linux/fs.h>
  14. #include <linux/time.h>
  15. #include <linux/pagemap.h>
  16. #include <linux/highmem.h>
  17. #include <linux/crc32.h>
  18. #include <linux/jffs2.h>
  19. #include "nodelist.h"
  20. static int jffs2_commit_write (struct file *filp, struct page *pg,
  21. unsigned start, unsigned end);
  22. static int jffs2_prepare_write (struct file *filp, struct page *pg,
  23. unsigned start, unsigned end);
  24. static int jffs2_readpage (struct file *filp, struct page *pg);
  25. int jffs2_fsync(struct file *filp, struct dentry *dentry, int datasync)
  26. {
  27. struct inode *inode = dentry->d_inode;
  28. struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
  29. /* Trigger GC to flush any pending writes for this inode */
  30. jffs2_flush_wbuf_gc(c, inode->i_ino);
  31. return 0;
  32. }
  33. const struct file_operations jffs2_file_operations =
  34. {
  35. .llseek = generic_file_llseek,
  36. .open = generic_file_open,
  37. .read = do_sync_read,
  38. .aio_read = generic_file_aio_read,
  39. .write = do_sync_write,
  40. .aio_write = generic_file_aio_write,
  41. .ioctl = jffs2_ioctl,
  42. .mmap = generic_file_readonly_mmap,
  43. .fsync = jffs2_fsync,
  44. .splice_read = generic_file_splice_read,
  45. };
  46. /* jffs2_file_inode_operations */
  47. const struct inode_operations jffs2_file_inode_operations =
  48. {
  49. .permission = jffs2_permission,
  50. .setattr = jffs2_setattr,
  51. .setxattr = jffs2_setxattr,
  52. .getxattr = jffs2_getxattr,
  53. .listxattr = jffs2_listxattr,
  54. .removexattr = jffs2_removexattr
  55. };
  56. const struct address_space_operations jffs2_file_address_operations =
  57. {
  58. .readpage = jffs2_readpage,
  59. .prepare_write =jffs2_prepare_write,
  60. .commit_write = jffs2_commit_write
  61. };
  62. static int jffs2_do_readpage_nolock (struct inode *inode, struct page *pg)
  63. {
  64. struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
  65. struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
  66. unsigned char *pg_buf;
  67. int ret;
  68. D2(printk(KERN_DEBUG "jffs2_do_readpage_nolock(): ino #%lu, page at offset 0x%lx\n", inode->i_ino, pg->index << PAGE_CACHE_SHIFT));
  69. BUG_ON(!PageLocked(pg));
  70. pg_buf = kmap(pg);
  71. /* FIXME: Can kmap fail? */
  72. ret = jffs2_read_inode_range(c, f, pg_buf, pg->index << PAGE_CACHE_SHIFT, PAGE_CACHE_SIZE);
  73. if (ret) {
  74. ClearPageUptodate(pg);
  75. SetPageError(pg);
  76. } else {
  77. SetPageUptodate(pg);
  78. ClearPageError(pg);
  79. }
  80. flush_dcache_page(pg);
  81. kunmap(pg);
  82. D2(printk(KERN_DEBUG "readpage finished\n"));
  83. return 0;
  84. }
  85. int jffs2_do_readpage_unlock(struct inode *inode, struct page *pg)
  86. {
  87. int ret = jffs2_do_readpage_nolock(inode, pg);
  88. unlock_page(pg);
  89. return ret;
  90. }
  91. static int jffs2_readpage (struct file *filp, struct page *pg)
  92. {
  93. struct jffs2_inode_info *f = JFFS2_INODE_INFO(pg->mapping->host);
  94. int ret;
  95. down(&f->sem);
  96. ret = jffs2_do_readpage_unlock(pg->mapping->host, pg);
  97. up(&f->sem);
  98. return ret;
  99. }
  100. static int jffs2_prepare_write (struct file *filp, struct page *pg,
  101. unsigned start, unsigned end)
  102. {
  103. struct inode *inode = pg->mapping->host;
  104. struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
  105. uint32_t pageofs = pg->index << PAGE_CACHE_SHIFT;
  106. int ret = 0;
  107. D1(printk(KERN_DEBUG "jffs2_prepare_write()\n"));
  108. if (pageofs > inode->i_size) {
  109. /* Make new hole frag from old EOF to new page */
  110. struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
  111. struct jffs2_raw_inode ri;
  112. struct jffs2_full_dnode *fn;
  113. uint32_t alloc_len;
  114. D1(printk(KERN_DEBUG "Writing new hole frag 0x%x-0x%x between current EOF and new page\n",
  115. (unsigned int)inode->i_size, pageofs));
  116. ret = jffs2_reserve_space(c, sizeof(ri), &alloc_len,
  117. ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
  118. if (ret)
  119. return ret;
  120. down(&f->sem);
  121. memset(&ri, 0, sizeof(ri));
  122. ri.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
  123. ri.nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
  124. ri.totlen = cpu_to_je32(sizeof(ri));
  125. ri.hdr_crc = cpu_to_je32(crc32(0, &ri, sizeof(struct jffs2_unknown_node)-4));
  126. ri.ino = cpu_to_je32(f->inocache->ino);
  127. ri.version = cpu_to_je32(++f->highest_version);
  128. ri.mode = cpu_to_jemode(inode->i_mode);
  129. ri.uid = cpu_to_je16(inode->i_uid);
  130. ri.gid = cpu_to_je16(inode->i_gid);
  131. ri.isize = cpu_to_je32(max((uint32_t)inode->i_size, pageofs));
  132. ri.atime = ri.ctime = ri.mtime = cpu_to_je32(get_seconds());
  133. ri.offset = cpu_to_je32(inode->i_size);
  134. ri.dsize = cpu_to_je32(pageofs - inode->i_size);
  135. ri.csize = cpu_to_je32(0);
  136. ri.compr = JFFS2_COMPR_ZERO;
  137. ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8));
  138. ri.data_crc = cpu_to_je32(0);
  139. fn = jffs2_write_dnode(c, f, &ri, NULL, 0, ALLOC_NORMAL);
  140. if (IS_ERR(fn)) {
  141. ret = PTR_ERR(fn);
  142. jffs2_complete_reservation(c);
  143. up(&f->sem);
  144. return ret;
  145. }
  146. ret = jffs2_add_full_dnode_to_inode(c, f, fn);
  147. if (f->metadata) {
  148. jffs2_mark_node_obsolete(c, f->metadata->raw);
  149. jffs2_free_full_dnode(f->metadata);
  150. f->metadata = NULL;
  151. }
  152. if (ret) {
  153. D1(printk(KERN_DEBUG "Eep. add_full_dnode_to_inode() failed in prepare_write, returned %d\n", ret));
  154. jffs2_mark_node_obsolete(c, fn->raw);
  155. jffs2_free_full_dnode(fn);
  156. jffs2_complete_reservation(c);
  157. up(&f->sem);
  158. return ret;
  159. }
  160. jffs2_complete_reservation(c);
  161. inode->i_size = pageofs;
  162. up(&f->sem);
  163. }
  164. /* Read in the page if it wasn't already present, unless it's a whole page */
  165. if (!PageUptodate(pg) && (start || end < PAGE_CACHE_SIZE)) {
  166. down(&f->sem);
  167. ret = jffs2_do_readpage_nolock(inode, pg);
  168. up(&f->sem);
  169. }
  170. D1(printk(KERN_DEBUG "end prepare_write(). pg->flags %lx\n", pg->flags));
  171. return ret;
  172. }
  173. static int jffs2_commit_write (struct file *filp, struct page *pg,
  174. unsigned start, unsigned end)
  175. {
  176. /* Actually commit the write from the page cache page we're looking at.
  177. * For now, we write the full page out each time. It sucks, but it's simple
  178. */
  179. struct inode *inode = pg->mapping->host;
  180. struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
  181. struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
  182. struct jffs2_raw_inode *ri;
  183. unsigned aligned_start = start & ~3;
  184. int ret = 0;
  185. uint32_t writtenlen = 0;
  186. D1(printk(KERN_DEBUG "jffs2_commit_write(): ino #%lu, page at 0x%lx, range %d-%d, flags %lx\n",
  187. inode->i_ino, pg->index << PAGE_CACHE_SHIFT, start, end, pg->flags));
  188. if (end == PAGE_CACHE_SIZE) {
  189. if (!start) {
  190. /* We need to avoid deadlock with page_cache_read() in
  191. jffs2_garbage_collect_pass(). So we have to mark the
  192. page up to date, to prevent page_cache_read() from
  193. trying to re-lock it. */
  194. SetPageUptodate(pg);
  195. } else {
  196. /* When writing out the end of a page, write out the
  197. _whole_ page. This helps to reduce the number of
  198. nodes in files which have many short writes, like
  199. syslog files. */
  200. start = aligned_start = 0;
  201. }
  202. }
  203. ri = jffs2_alloc_raw_inode();
  204. if (!ri) {
  205. D1(printk(KERN_DEBUG "jffs2_commit_write(): Allocation of raw inode failed\n"));
  206. return -ENOMEM;
  207. }
  208. /* Set the fields that the generic jffs2_write_inode_range() code can't find */
  209. ri->ino = cpu_to_je32(inode->i_ino);
  210. ri->mode = cpu_to_jemode(inode->i_mode);
  211. ri->uid = cpu_to_je16(inode->i_uid);
  212. ri->gid = cpu_to_je16(inode->i_gid);
  213. ri->isize = cpu_to_je32((uint32_t)inode->i_size);
  214. ri->atime = ri->ctime = ri->mtime = cpu_to_je32(get_seconds());
  215. /* In 2.4, it was already kmapped by generic_file_write(). Doesn't
  216. hurt to do it again. The alternative is ifdefs, which are ugly. */
  217. kmap(pg);
  218. ret = jffs2_write_inode_range(c, f, ri, page_address(pg) + aligned_start,
  219. (pg->index << PAGE_CACHE_SHIFT) + aligned_start,
  220. end - aligned_start, &writtenlen);
  221. kunmap(pg);
  222. if (ret) {
  223. /* There was an error writing. */
  224. SetPageError(pg);
  225. }
  226. /* Adjust writtenlen for the padding we did, so we don't confuse our caller */
  227. if (writtenlen < (start&3))
  228. writtenlen = 0;
  229. else
  230. writtenlen -= (start&3);
  231. if (writtenlen) {
  232. if (inode->i_size < (pg->index << PAGE_CACHE_SHIFT) + start + writtenlen) {
  233. inode->i_size = (pg->index << PAGE_CACHE_SHIFT) + start + writtenlen;
  234. inode->i_blocks = (inode->i_size + 511) >> 9;
  235. inode->i_ctime = inode->i_mtime = ITIME(je32_to_cpu(ri->ctime));
  236. }
  237. }
  238. jffs2_free_raw_inode(ri);
  239. if (start+writtenlen < end) {
  240. /* generic_file_write has written more to the page cache than we've
  241. actually written to the medium. Mark the page !Uptodate so that
  242. it gets reread */
  243. D1(printk(KERN_DEBUG "jffs2_commit_write(): Not all bytes written. Marking page !uptodate\n"));
  244. SetPageError(pg);
  245. ClearPageUptodate(pg);
  246. }
  247. D1(printk(KERN_DEBUG "jffs2_commit_write() returning %d\n",start+writtenlen==end?0:ret));
  248. return start+writtenlen==end?0:ret;
  249. }