Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * JFFS2 -- Journalling Flash File System, Version 2. |
| 3 | * |
| 4 | * Copyright (C) 2001-2003 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 | * |
David Woodhouse | 265489f | 2005-07-06 13:13:13 +0100 | [diff] [blame] | 10 | * $Id: file.c,v 1.102 2005/07/06 12:13:09 dwmw2 Exp $ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * |
| 12 | */ |
| 13 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/kernel.h> |
| 15 | #include <linux/slab.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 | extern int generic_file_open(struct inode *, struct file *) __attribute__((weak)); |
| 25 | extern loff_t generic_file_llseek(struct file *file, loff_t offset, int origin) __attribute__((weak)); |
| 26 | |
| 27 | static int jffs2_commit_write (struct file *filp, struct page *pg, |
| 28 | unsigned start, unsigned end); |
| 29 | static int jffs2_prepare_write (struct file *filp, struct page *pg, |
| 30 | unsigned start, unsigned end); |
| 31 | static int jffs2_readpage (struct file *filp, struct page *pg); |
| 32 | |
| 33 | int jffs2_fsync(struct file *filp, struct dentry *dentry, int datasync) |
| 34 | { |
| 35 | struct inode *inode = dentry->d_inode; |
| 36 | struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb); |
| 37 | |
| 38 | /* Trigger GC to flush any pending writes for this inode */ |
| 39 | jffs2_flush_wbuf_gc(c, inode->i_ino); |
| 40 | |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | struct file_operations jffs2_file_operations = |
| 45 | { |
| 46 | .llseek = generic_file_llseek, |
| 47 | .open = generic_file_open, |
| 48 | .read = generic_file_read, |
| 49 | .write = generic_file_write, |
| 50 | .ioctl = jffs2_ioctl, |
| 51 | .mmap = generic_file_readonly_mmap, |
| 52 | .fsync = jffs2_fsync, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | .sendfile = generic_file_sendfile |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | /* jffs2_file_inode_operations */ |
| 57 | |
| 58 | struct inode_operations jffs2_file_inode_operations = |
| 59 | { |
| 60 | .setattr = jffs2_setattr |
| 61 | }; |
| 62 | |
| 63 | struct address_space_operations jffs2_file_address_operations = |
| 64 | { |
| 65 | .readpage = jffs2_readpage, |
| 66 | .prepare_write =jffs2_prepare_write, |
| 67 | .commit_write = jffs2_commit_write |
| 68 | }; |
| 69 | |
| 70 | static int jffs2_do_readpage_nolock (struct inode *inode, struct page *pg) |
| 71 | { |
| 72 | struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode); |
| 73 | struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb); |
| 74 | unsigned char *pg_buf; |
| 75 | int ret; |
| 76 | |
| 77 | D2(printk(KERN_DEBUG "jffs2_do_readpage_nolock(): ino #%lu, page at offset 0x%lx\n", inode->i_ino, pg->index << PAGE_CACHE_SHIFT)); |
| 78 | |
Matt Mackall | cd7619d | 2005-05-01 08:59:01 -0700 | [diff] [blame] | 79 | BUG_ON(!PageLocked(pg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
| 81 | pg_buf = kmap(pg); |
| 82 | /* FIXME: Can kmap fail? */ |
| 83 | |
| 84 | ret = jffs2_read_inode_range(c, f, pg_buf, pg->index << PAGE_CACHE_SHIFT, PAGE_CACHE_SIZE); |
| 85 | |
| 86 | if (ret) { |
| 87 | ClearPageUptodate(pg); |
| 88 | SetPageError(pg); |
| 89 | } else { |
| 90 | SetPageUptodate(pg); |
| 91 | ClearPageError(pg); |
| 92 | } |
| 93 | |
| 94 | flush_dcache_page(pg); |
| 95 | kunmap(pg); |
| 96 | |
| 97 | D2(printk(KERN_DEBUG "readpage finished\n")); |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | int jffs2_do_readpage_unlock(struct inode *inode, struct page *pg) |
| 102 | { |
| 103 | int ret = jffs2_do_readpage_nolock(inode, pg); |
| 104 | unlock_page(pg); |
| 105 | return ret; |
| 106 | } |
| 107 | |
| 108 | |
| 109 | static int jffs2_readpage (struct file *filp, struct page *pg) |
| 110 | { |
| 111 | struct jffs2_inode_info *f = JFFS2_INODE_INFO(pg->mapping->host); |
| 112 | int ret; |
| 113 | |
| 114 | down(&f->sem); |
| 115 | ret = jffs2_do_readpage_unlock(pg->mapping->host, pg); |
| 116 | up(&f->sem); |
| 117 | return ret; |
| 118 | } |
| 119 | |
| 120 | static int jffs2_prepare_write (struct file *filp, struct page *pg, |
| 121 | unsigned start, unsigned end) |
| 122 | { |
| 123 | struct inode *inode = pg->mapping->host; |
| 124 | struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode); |
| 125 | uint32_t pageofs = pg->index << PAGE_CACHE_SHIFT; |
| 126 | int ret = 0; |
| 127 | |
| 128 | D1(printk(KERN_DEBUG "jffs2_prepare_write()\n")); |
| 129 | |
| 130 | if (pageofs > inode->i_size) { |
| 131 | /* Make new hole frag from old EOF to new page */ |
| 132 | struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb); |
| 133 | struct jffs2_raw_inode ri; |
| 134 | struct jffs2_full_dnode *fn; |
| 135 | uint32_t phys_ofs, alloc_len; |
| 136 | |
| 137 | D1(printk(KERN_DEBUG "Writing new hole frag 0x%x-0x%x between current EOF and new page\n", |
| 138 | (unsigned int)inode->i_size, pageofs)); |
| 139 | |
| 140 | ret = jffs2_reserve_space(c, sizeof(ri), &phys_ofs, &alloc_len, ALLOC_NORMAL); |
| 141 | if (ret) |
| 142 | return ret; |
| 143 | |
| 144 | down(&f->sem); |
| 145 | memset(&ri, 0, sizeof(ri)); |
| 146 | |
| 147 | ri.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK); |
| 148 | ri.nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE); |
| 149 | ri.totlen = cpu_to_je32(sizeof(ri)); |
| 150 | ri.hdr_crc = cpu_to_je32(crc32(0, &ri, sizeof(struct jffs2_unknown_node)-4)); |
| 151 | |
| 152 | ri.ino = cpu_to_je32(f->inocache->ino); |
| 153 | ri.version = cpu_to_je32(++f->highest_version); |
| 154 | ri.mode = cpu_to_jemode(inode->i_mode); |
| 155 | ri.uid = cpu_to_je16(inode->i_uid); |
| 156 | ri.gid = cpu_to_je16(inode->i_gid); |
| 157 | ri.isize = cpu_to_je32(max((uint32_t)inode->i_size, pageofs)); |
| 158 | ri.atime = ri.ctime = ri.mtime = cpu_to_je32(get_seconds()); |
| 159 | ri.offset = cpu_to_je32(inode->i_size); |
| 160 | ri.dsize = cpu_to_je32(pageofs - inode->i_size); |
| 161 | ri.csize = cpu_to_je32(0); |
| 162 | ri.compr = JFFS2_COMPR_ZERO; |
| 163 | ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8)); |
| 164 | ri.data_crc = cpu_to_je32(0); |
| 165 | |
| 166 | fn = jffs2_write_dnode(c, f, &ri, NULL, 0, phys_ofs, ALLOC_NORMAL); |
| 167 | |
| 168 | if (IS_ERR(fn)) { |
| 169 | ret = PTR_ERR(fn); |
| 170 | jffs2_complete_reservation(c); |
| 171 | up(&f->sem); |
| 172 | return ret; |
| 173 | } |
| 174 | ret = jffs2_add_full_dnode_to_inode(c, f, fn); |
| 175 | if (f->metadata) { |
| 176 | jffs2_mark_node_obsolete(c, f->metadata->raw); |
| 177 | jffs2_free_full_dnode(f->metadata); |
| 178 | f->metadata = NULL; |
| 179 | } |
| 180 | if (ret) { |
| 181 | D1(printk(KERN_DEBUG "Eep. add_full_dnode_to_inode() failed in prepare_write, returned %d\n", ret)); |
| 182 | jffs2_mark_node_obsolete(c, fn->raw); |
| 183 | jffs2_free_full_dnode(fn); |
| 184 | jffs2_complete_reservation(c); |
| 185 | up(&f->sem); |
| 186 | return ret; |
| 187 | } |
| 188 | jffs2_complete_reservation(c); |
| 189 | inode->i_size = pageofs; |
| 190 | up(&f->sem); |
| 191 | } |
| 192 | |
| 193 | /* Read in the page if it wasn't already present, unless it's a whole page */ |
| 194 | if (!PageUptodate(pg) && (start || end < PAGE_CACHE_SIZE)) { |
| 195 | down(&f->sem); |
| 196 | ret = jffs2_do_readpage_nolock(inode, pg); |
| 197 | up(&f->sem); |
| 198 | } |
| 199 | D1(printk(KERN_DEBUG "end prepare_write(). pg->flags %lx\n", pg->flags)); |
| 200 | return ret; |
| 201 | } |
| 202 | |
| 203 | static int jffs2_commit_write (struct file *filp, struct page *pg, |
| 204 | unsigned start, unsigned end) |
| 205 | { |
| 206 | /* Actually commit the write from the page cache page we're looking at. |
| 207 | * For now, we write the full page out each time. It sucks, but it's simple |
| 208 | */ |
| 209 | struct inode *inode = pg->mapping->host; |
| 210 | struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode); |
| 211 | struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb); |
| 212 | struct jffs2_raw_inode *ri; |
| 213 | unsigned aligned_start = start & ~3; |
| 214 | int ret = 0; |
| 215 | uint32_t writtenlen = 0; |
| 216 | |
| 217 | D1(printk(KERN_DEBUG "jffs2_commit_write(): ino #%lu, page at 0x%lx, range %d-%d, flags %lx\n", |
| 218 | inode->i_ino, pg->index << PAGE_CACHE_SHIFT, start, end, pg->flags)); |
| 219 | |
| 220 | if (!start && end == PAGE_CACHE_SIZE) { |
| 221 | /* We need to avoid deadlock with page_cache_read() in |
| 222 | jffs2_garbage_collect_pass(). So we have to mark the |
| 223 | page up to date, to prevent page_cache_read() from |
| 224 | trying to re-lock it. */ |
| 225 | SetPageUptodate(pg); |
| 226 | } |
| 227 | |
| 228 | ri = jffs2_alloc_raw_inode(); |
| 229 | |
| 230 | if (!ri) { |
| 231 | D1(printk(KERN_DEBUG "jffs2_commit_write(): Allocation of raw inode failed\n")); |
| 232 | return -ENOMEM; |
| 233 | } |
| 234 | |
| 235 | /* Set the fields that the generic jffs2_write_inode_range() code can't find */ |
| 236 | ri->ino = cpu_to_je32(inode->i_ino); |
| 237 | ri->mode = cpu_to_jemode(inode->i_mode); |
| 238 | ri->uid = cpu_to_je16(inode->i_uid); |
| 239 | ri->gid = cpu_to_je16(inode->i_gid); |
| 240 | ri->isize = cpu_to_je32((uint32_t)inode->i_size); |
| 241 | ri->atime = ri->ctime = ri->mtime = cpu_to_je32(get_seconds()); |
| 242 | |
| 243 | /* In 2.4, it was already kmapped by generic_file_write(). Doesn't |
| 244 | hurt to do it again. The alternative is ifdefs, which are ugly. */ |
| 245 | kmap(pg); |
| 246 | |
| 247 | ret = jffs2_write_inode_range(c, f, ri, page_address(pg) + aligned_start, |
| 248 | (pg->index << PAGE_CACHE_SHIFT) + aligned_start, |
| 249 | end - aligned_start, &writtenlen); |
| 250 | |
| 251 | kunmap(pg); |
| 252 | |
| 253 | if (ret) { |
| 254 | /* There was an error writing. */ |
| 255 | SetPageError(pg); |
| 256 | } |
| 257 | |
| 258 | /* Adjust writtenlen for the padding we did, so we don't confuse our caller */ |
| 259 | if (writtenlen < (start&3)) |
| 260 | writtenlen = 0; |
| 261 | else |
| 262 | writtenlen -= (start&3); |
| 263 | |
| 264 | if (writtenlen) { |
| 265 | if (inode->i_size < (pg->index << PAGE_CACHE_SHIFT) + start + writtenlen) { |
| 266 | inode->i_size = (pg->index << PAGE_CACHE_SHIFT) + start + writtenlen; |
| 267 | inode->i_blocks = (inode->i_size + 511) >> 9; |
| 268 | |
| 269 | inode->i_ctime = inode->i_mtime = ITIME(je32_to_cpu(ri->ctime)); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | jffs2_free_raw_inode(ri); |
| 274 | |
| 275 | if (start+writtenlen < end) { |
| 276 | /* generic_file_write has written more to the page cache than we've |
| 277 | actually written to the medium. Mark the page !Uptodate so that |
| 278 | it gets reread */ |
| 279 | D1(printk(KERN_DEBUG "jffs2_commit_write(): Not all bytes written. Marking page !uptodate\n")); |
| 280 | SetPageError(pg); |
| 281 | ClearPageUptodate(pg); |
| 282 | } |
| 283 | |
| 284 | D1(printk(KERN_DEBUG "jffs2_commit_write() returning %d\n",writtenlen?writtenlen:ret)); |
| 285 | return writtenlen?writtenlen:ret; |
| 286 | } |