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