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