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