file.c 9.1 KB

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