file.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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/filelock.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_write_end(const struct kiocb *iocb,
  23. struct address_space *mapping,
  24. loff_t pos, unsigned len, unsigned copied,
  25. struct folio *folio, void *fsdata);
  26. static int jffs2_write_begin(const struct kiocb *iocb,
  27. struct address_space *mapping,
  28. loff_t pos, unsigned len,
  29. struct folio **foliop, void **fsdata);
  30. static int jffs2_read_folio(struct file *filp, struct folio *folio);
  31. int jffs2_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
  32. {
  33. struct inode *inode = filp->f_mapping->host;
  34. struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
  35. int ret;
  36. ret = file_write_and_wait_range(filp, start, end);
  37. if (ret)
  38. return ret;
  39. inode_lock(inode);
  40. /* Trigger GC to flush any pending writes for this inode */
  41. jffs2_flush_wbuf_gc(c, inode->i_ino);
  42. inode_unlock(inode);
  43. return 0;
  44. }
  45. const struct file_operations jffs2_file_operations =
  46. {
  47. .llseek = generic_file_llseek,
  48. .open = generic_file_open,
  49. .read_iter = generic_file_read_iter,
  50. .write_iter = generic_file_write_iter,
  51. .unlocked_ioctl=jffs2_ioctl,
  52. .mmap_prepare = generic_file_readonly_mmap_prepare,
  53. .fsync = jffs2_fsync,
  54. .splice_read = filemap_splice_read,
  55. .splice_write = iter_file_splice_write,
  56. .setlease = generic_setlease,
  57. };
  58. /* jffs2_file_inode_operations */
  59. const struct inode_operations jffs2_file_inode_operations =
  60. {
  61. .get_inode_acl = jffs2_get_acl,
  62. .set_acl = jffs2_set_acl,
  63. .setattr = jffs2_setattr,
  64. .listxattr = jffs2_listxattr,
  65. };
  66. const struct address_space_operations jffs2_file_address_operations =
  67. {
  68. .read_folio = jffs2_read_folio,
  69. .write_begin = jffs2_write_begin,
  70. .write_end = jffs2_write_end,
  71. };
  72. static int jffs2_do_readpage_nolock(struct inode *inode, struct folio *folio)
  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 *kaddr;
  77. int ret;
  78. jffs2_dbg(2, "%s(): ino #%lu, page at offset 0x%lx\n",
  79. __func__, inode->i_ino, folio->index << PAGE_SHIFT);
  80. BUG_ON(!folio_test_locked(folio));
  81. kaddr = kmap_local_folio(folio, 0);
  82. ret = jffs2_read_inode_range(c, f, kaddr, folio->index << PAGE_SHIFT,
  83. PAGE_SIZE);
  84. kunmap_local(kaddr);
  85. if (!ret)
  86. folio_mark_uptodate(folio);
  87. flush_dcache_folio(folio);
  88. jffs2_dbg(2, "readpage finished\n");
  89. return ret;
  90. }
  91. int __jffs2_read_folio(struct file *file, struct folio *folio)
  92. {
  93. int ret = jffs2_do_readpage_nolock(folio->mapping->host, folio);
  94. folio_unlock(folio);
  95. return ret;
  96. }
  97. static int jffs2_read_folio(struct file *file, struct folio *folio)
  98. {
  99. struct jffs2_inode_info *f = JFFS2_INODE_INFO(folio->mapping->host);
  100. int ret;
  101. mutex_lock(&f->sem);
  102. ret = __jffs2_read_folio(file, folio);
  103. mutex_unlock(&f->sem);
  104. return ret;
  105. }
  106. static int jffs2_write_begin(const struct kiocb *iocb,
  107. struct address_space *mapping,
  108. loff_t pos, unsigned len,
  109. struct folio **foliop, void **fsdata)
  110. {
  111. struct folio *folio;
  112. struct inode *inode = mapping->host;
  113. struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
  114. struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
  115. pgoff_t index = pos >> PAGE_SHIFT;
  116. int ret = 0;
  117. jffs2_dbg(1, "%s()\n", __func__);
  118. if (pos > inode->i_size) {
  119. /* Make new hole frag from old EOF to new position */
  120. struct jffs2_raw_inode ri;
  121. struct jffs2_full_dnode *fn;
  122. uint32_t alloc_len;
  123. jffs2_dbg(1, "Writing new hole frag 0x%x-0x%x between current EOF and new position\n",
  124. (unsigned int)inode->i_size, (uint32_t)pos);
  125. ret = jffs2_reserve_space(c, sizeof(ri), &alloc_len,
  126. ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
  127. if (ret)
  128. goto out_err;
  129. mutex_lock(&f->sem);
  130. memset(&ri, 0, sizeof(ri));
  131. ri.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
  132. ri.nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
  133. ri.totlen = cpu_to_je32(sizeof(ri));
  134. ri.hdr_crc = cpu_to_je32(crc32(0, &ri, sizeof(struct jffs2_unknown_node)-4));
  135. ri.ino = cpu_to_je32(f->inocache->ino);
  136. ri.version = cpu_to_je32(++f->highest_version);
  137. ri.mode = cpu_to_jemode(inode->i_mode);
  138. ri.uid = cpu_to_je16(i_uid_read(inode));
  139. ri.gid = cpu_to_je16(i_gid_read(inode));
  140. ri.isize = cpu_to_je32((uint32_t)pos);
  141. ri.atime = ri.ctime = ri.mtime = cpu_to_je32(JFFS2_NOW());
  142. ri.offset = cpu_to_je32(inode->i_size);
  143. ri.dsize = cpu_to_je32((uint32_t)pos - inode->i_size);
  144. ri.csize = cpu_to_je32(0);
  145. ri.compr = JFFS2_COMPR_ZERO;
  146. ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8));
  147. ri.data_crc = cpu_to_je32(0);
  148. fn = jffs2_write_dnode(c, f, &ri, NULL, 0, ALLOC_NORMAL);
  149. if (IS_ERR(fn)) {
  150. ret = PTR_ERR(fn);
  151. jffs2_complete_reservation(c);
  152. mutex_unlock(&f->sem);
  153. goto out_err;
  154. }
  155. ret = jffs2_add_full_dnode_to_inode(c, f, fn);
  156. if (f->metadata) {
  157. jffs2_mark_node_obsolete(c, f->metadata->raw);
  158. jffs2_free_full_dnode(f->metadata);
  159. f->metadata = NULL;
  160. }
  161. if (ret) {
  162. jffs2_dbg(1, "Eep. add_full_dnode_to_inode() failed in write_begin, returned %d\n",
  163. ret);
  164. jffs2_mark_node_obsolete(c, fn->raw);
  165. jffs2_free_full_dnode(fn);
  166. jffs2_complete_reservation(c);
  167. mutex_unlock(&f->sem);
  168. goto out_err;
  169. }
  170. jffs2_complete_reservation(c);
  171. inode->i_size = pos;
  172. mutex_unlock(&f->sem);
  173. }
  174. /*
  175. * While getting a page and reading data in, lock c->alloc_sem until
  176. * the page is Uptodate. Otherwise GC task may attempt to read the same
  177. * page in read_cache_page(), which causes a deadlock.
  178. */
  179. mutex_lock(&c->alloc_sem);
  180. folio = __filemap_get_folio(mapping, index, FGP_WRITEBEGIN,
  181. mapping_gfp_mask(mapping));
  182. if (IS_ERR(folio)) {
  183. ret = PTR_ERR(folio);
  184. goto release_sem;
  185. }
  186. *foliop = folio;
  187. /*
  188. * Read in the folio if it wasn't already present. Cannot optimize away
  189. * the whole folio write case until jffs2_write_end can handle the
  190. * case of a short-copy.
  191. */
  192. if (!folio_test_uptodate(folio)) {
  193. mutex_lock(&f->sem);
  194. ret = jffs2_do_readpage_nolock(inode, folio);
  195. mutex_unlock(&f->sem);
  196. if (ret) {
  197. folio_unlock(folio);
  198. folio_put(folio);
  199. goto release_sem;
  200. }
  201. }
  202. jffs2_dbg(1, "end write_begin(). folio->flags %lx\n", folio->flags.f);
  203. release_sem:
  204. mutex_unlock(&c->alloc_sem);
  205. out_err:
  206. return ret;
  207. }
  208. static int jffs2_write_end(const struct kiocb *iocb,
  209. struct address_space *mapping,
  210. loff_t pos, unsigned len, unsigned copied,
  211. struct folio *folio, void *fsdata)
  212. {
  213. /* Actually commit the write from the page cache page we're looking at.
  214. * For now, we write the full page out each time. It sucks, but it's simple
  215. */
  216. struct inode *inode = mapping->host;
  217. struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
  218. struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
  219. struct jffs2_raw_inode *ri;
  220. unsigned start = pos & (PAGE_SIZE - 1);
  221. unsigned end = start + copied;
  222. unsigned aligned_start = start & ~3;
  223. int ret = 0;
  224. uint32_t writtenlen = 0;
  225. void *buf;
  226. jffs2_dbg(1, "%s(): ino #%lu, page at 0x%llx, range %d-%d, flags %lx\n",
  227. __func__, inode->i_ino, folio_pos(folio),
  228. start, end, folio->flags.f);
  229. /* We need to avoid deadlock with page_cache_read() in
  230. jffs2_garbage_collect_pass(). So the folio must be
  231. up to date to prevent page_cache_read() from trying
  232. to re-lock it. */
  233. BUG_ON(!folio_test_uptodate(folio));
  234. if (end == PAGE_SIZE) {
  235. /* When writing out the end of a page, write out the
  236. _whole_ page. This helps to reduce the number of
  237. nodes in files which have many short writes, like
  238. syslog files. */
  239. aligned_start = 0;
  240. }
  241. ri = jffs2_alloc_raw_inode();
  242. if (!ri) {
  243. jffs2_dbg(1, "%s(): Allocation of raw inode failed\n",
  244. __func__);
  245. folio_unlock(folio);
  246. folio_put(folio);
  247. return -ENOMEM;
  248. }
  249. /* Set the fields that the generic jffs2_write_inode_range() code can't find */
  250. ri->ino = cpu_to_je32(inode->i_ino);
  251. ri->mode = cpu_to_jemode(inode->i_mode);
  252. ri->uid = cpu_to_je16(i_uid_read(inode));
  253. ri->gid = cpu_to_je16(i_gid_read(inode));
  254. ri->isize = cpu_to_je32((uint32_t)inode->i_size);
  255. ri->atime = ri->ctime = ri->mtime = cpu_to_je32(JFFS2_NOW());
  256. buf = kmap_local_folio(folio, aligned_start);
  257. ret = jffs2_write_inode_range(c, f, ri, buf,
  258. folio_pos(folio) + aligned_start,
  259. end - aligned_start, &writtenlen);
  260. kunmap_local(buf);
  261. if (ret)
  262. mapping_set_error(mapping, ret);
  263. /* Adjust writtenlen for the padding we did, so we don't confuse our caller */
  264. writtenlen -= min(writtenlen, (start - aligned_start));
  265. if (writtenlen) {
  266. if (inode->i_size < pos + writtenlen) {
  267. inode->i_size = pos + writtenlen;
  268. inode->i_blocks = (inode->i_size + 511) >> 9;
  269. inode_set_mtime_to_ts(inode,
  270. inode_set_ctime_to_ts(inode, ITIME(je32_to_cpu(ri->ctime))));
  271. }
  272. }
  273. jffs2_free_raw_inode(ri);
  274. if (start+writtenlen < end) {
  275. /* generic_file_write has written more to the page cache than we've
  276. actually written to the medium. Mark the page !Uptodate so that
  277. it gets reread */
  278. jffs2_dbg(1, "%s(): Not all bytes written. Marking page !uptodate\n",
  279. __func__);
  280. folio_clear_uptodate(folio);
  281. }
  282. jffs2_dbg(1, "%s() returning %d\n",
  283. __func__, writtenlen > 0 ? writtenlen : ret);
  284. folio_unlock(folio);
  285. folio_put(folio);
  286. return writtenlen > 0 ? writtenlen : ret;
  287. }