file.c 8.1 KB

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