file.c 9.4 KB

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