Linux 3.2.60
[linux/fpc-iii.git] / fs / jffs2 / file.c
blob0095a70a3426c7de4073b47259e53c8c343f4a2f
1 /*
2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright © 2001-2007 Red Hat, Inc.
5 * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
7 * Created by David Woodhouse <dwmw2@infradead.org>
9 * For licensing information, see the file 'LICENCE' in this directory.
13 #include <linux/kernel.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"
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);
28 static int jffs2_readpage (struct file *filp, struct page *pg);
30 int jffs2_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
32 struct inode *inode = filp->f_mapping->host;
33 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
34 int ret;
36 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
37 if (ret)
38 return ret;
40 mutex_lock(&inode->i_mutex);
41 /* Trigger GC to flush any pending writes for this inode */
42 jffs2_flush_wbuf_gc(c, inode->i_ino);
43 mutex_unlock(&inode->i_mutex);
45 return 0;
48 const struct file_operations jffs2_file_operations =
50 .llseek = generic_file_llseek,
51 .open = generic_file_open,
52 .read = do_sync_read,
53 .aio_read = generic_file_aio_read,
54 .write = do_sync_write,
55 .aio_write = generic_file_aio_write,
56 .unlocked_ioctl=jffs2_ioctl,
57 .mmap = generic_file_readonly_mmap,
58 .fsync = jffs2_fsync,
59 .splice_read = generic_file_splice_read,
62 /* jffs2_file_inode_operations */
64 const struct inode_operations jffs2_file_inode_operations =
66 .get_acl = jffs2_get_acl,
67 .setattr = jffs2_setattr,
68 .setxattr = jffs2_setxattr,
69 .getxattr = jffs2_getxattr,
70 .listxattr = jffs2_listxattr,
71 .removexattr = jffs2_removexattr
74 const struct address_space_operations jffs2_file_address_operations =
76 .readpage = jffs2_readpage,
77 .write_begin = jffs2_write_begin,
78 .write_end = jffs2_write_end,
81 static int jffs2_do_readpage_nolock (struct inode *inode, struct page *pg)
83 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
84 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
85 unsigned char *pg_buf;
86 int ret;
88 D2(printk(KERN_DEBUG "jffs2_do_readpage_nolock(): ino #%lu, page at offset 0x%lx\n", inode->i_ino, pg->index << PAGE_CACHE_SHIFT));
90 BUG_ON(!PageLocked(pg));
92 pg_buf = kmap(pg);
93 /* FIXME: Can kmap fail? */
95 ret = jffs2_read_inode_range(c, f, pg_buf, pg->index << PAGE_CACHE_SHIFT, PAGE_CACHE_SIZE);
97 if (ret) {
98 ClearPageUptodate(pg);
99 SetPageError(pg);
100 } else {
101 SetPageUptodate(pg);
102 ClearPageError(pg);
105 flush_dcache_page(pg);
106 kunmap(pg);
108 D2(printk(KERN_DEBUG "readpage finished\n"));
109 return ret;
112 int jffs2_do_readpage_unlock(struct inode *inode, struct page *pg)
114 int ret = jffs2_do_readpage_nolock(inode, pg);
115 unlock_page(pg);
116 return ret;
120 static int jffs2_readpage (struct file *filp, struct page *pg)
122 struct jffs2_inode_info *f = JFFS2_INODE_INFO(pg->mapping->host);
123 int ret;
125 mutex_lock(&f->sem);
126 ret = jffs2_do_readpage_unlock(pg->mapping->host, pg);
127 mutex_unlock(&f->sem);
128 return ret;
131 static int jffs2_write_begin(struct file *filp, struct address_space *mapping,
132 loff_t pos, unsigned len, unsigned flags,
133 struct page **pagep, void **fsdata)
135 struct page *pg;
136 struct inode *inode = mapping->host;
137 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
138 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
139 struct jffs2_raw_inode ri;
140 uint32_t alloc_len = 0;
141 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
142 uint32_t pageofs = index << PAGE_CACHE_SHIFT;
143 int ret = 0;
145 D1(printk(KERN_DEBUG "%s()\n", __func__));
147 if (pageofs > inode->i_size) {
148 ret = jffs2_reserve_space(c, sizeof(ri), &alloc_len,
149 ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
150 if (ret)
151 return ret;
154 mutex_lock(&f->sem);
155 pg = grab_cache_page_write_begin(mapping, index, flags);
156 if (!pg) {
157 if (alloc_len)
158 jffs2_complete_reservation(c);
159 mutex_unlock(&f->sem);
160 return -ENOMEM;
162 *pagep = pg;
164 if (alloc_len) {
165 /* Make new hole frag from old EOF to new page */
166 struct jffs2_full_dnode *fn;
168 D1(printk(KERN_DEBUG "Writing new hole frag 0x%x-0x%x between current EOF and new page\n",
169 (unsigned int)inode->i_size, pageofs));
171 memset(&ri, 0, sizeof(ri));
173 ri.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
174 ri.nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
175 ri.totlen = cpu_to_je32(sizeof(ri));
176 ri.hdr_crc = cpu_to_je32(crc32(0, &ri, sizeof(struct jffs2_unknown_node)-4));
178 ri.ino = cpu_to_je32(f->inocache->ino);
179 ri.version = cpu_to_je32(++f->highest_version);
180 ri.mode = cpu_to_jemode(inode->i_mode);
181 ri.uid = cpu_to_je16(inode->i_uid);
182 ri.gid = cpu_to_je16(inode->i_gid);
183 ri.isize = cpu_to_je32(max((uint32_t)inode->i_size, pageofs));
184 ri.atime = ri.ctime = ri.mtime = cpu_to_je32(get_seconds());
185 ri.offset = cpu_to_je32(inode->i_size);
186 ri.dsize = cpu_to_je32(pageofs - inode->i_size);
187 ri.csize = cpu_to_je32(0);
188 ri.compr = JFFS2_COMPR_ZERO;
189 ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8));
190 ri.data_crc = cpu_to_je32(0);
192 fn = jffs2_write_dnode(c, f, &ri, NULL, 0, ALLOC_NORMAL);
194 if (IS_ERR(fn)) {
195 ret = PTR_ERR(fn);
196 jffs2_complete_reservation(c);
197 goto out_page;
199 ret = jffs2_add_full_dnode_to_inode(c, f, fn);
200 if (f->metadata) {
201 jffs2_mark_node_obsolete(c, f->metadata->raw);
202 jffs2_free_full_dnode(f->metadata);
203 f->metadata = NULL;
205 if (ret) {
206 D1(printk(KERN_DEBUG "Eep. add_full_dnode_to_inode() failed in write_begin, returned %d\n", ret));
207 jffs2_mark_node_obsolete(c, fn->raw);
208 jffs2_free_full_dnode(fn);
209 jffs2_complete_reservation(c);
210 goto out_page;
212 jffs2_complete_reservation(c);
213 inode->i_size = pageofs;
217 * Read in the page if it wasn't already present. Cannot optimize away
218 * the whole page write case until jffs2_write_end can handle the
219 * case of a short-copy.
221 if (!PageUptodate(pg)) {
222 ret = jffs2_do_readpage_nolock(inode, pg);
223 if (ret)
224 goto out_page;
226 mutex_unlock(&f->sem);
227 D1(printk(KERN_DEBUG "end write_begin(). pg->flags %lx\n", pg->flags));
228 return ret;
230 out_page:
231 unlock_page(pg);
232 page_cache_release(pg);
233 mutex_unlock(&f->sem);
234 return ret;
237 static int jffs2_write_end(struct file *filp, struct address_space *mapping,
238 loff_t pos, unsigned len, unsigned copied,
239 struct page *pg, void *fsdata)
241 /* Actually commit the write from the page cache page we're looking at.
242 * For now, we write the full page out each time. It sucks, but it's simple
244 struct inode *inode = mapping->host;
245 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
246 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
247 struct jffs2_raw_inode *ri;
248 unsigned start = pos & (PAGE_CACHE_SIZE - 1);
249 unsigned end = start + copied;
250 unsigned aligned_start = start & ~3;
251 int ret = 0;
252 uint32_t writtenlen = 0;
254 D1(printk(KERN_DEBUG "jffs2_write_end(): ino #%lu, page at 0x%lx, range %d-%d, flags %lx\n",
255 inode->i_ino, pg->index << PAGE_CACHE_SHIFT, start, end, pg->flags));
257 /* We need to avoid deadlock with page_cache_read() in
258 jffs2_garbage_collect_pass(). So the page must be
259 up to date to prevent page_cache_read() from trying
260 to re-lock it. */
261 BUG_ON(!PageUptodate(pg));
263 if (end == PAGE_CACHE_SIZE) {
264 /* When writing out the end of a page, write out the
265 _whole_ page. This helps to reduce the number of
266 nodes in files which have many short writes, like
267 syslog files. */
268 aligned_start = 0;
271 ri = jffs2_alloc_raw_inode();
273 if (!ri) {
274 D1(printk(KERN_DEBUG "jffs2_write_end(): Allocation of raw inode failed\n"));
275 unlock_page(pg);
276 page_cache_release(pg);
277 return -ENOMEM;
280 /* Set the fields that the generic jffs2_write_inode_range() code can't find */
281 ri->ino = cpu_to_je32(inode->i_ino);
282 ri->mode = cpu_to_jemode(inode->i_mode);
283 ri->uid = cpu_to_je16(inode->i_uid);
284 ri->gid = cpu_to_je16(inode->i_gid);
285 ri->isize = cpu_to_je32((uint32_t)inode->i_size);
286 ri->atime = ri->ctime = ri->mtime = cpu_to_je32(get_seconds());
288 /* In 2.4, it was already kmapped by generic_file_write(). Doesn't
289 hurt to do it again. The alternative is ifdefs, which are ugly. */
290 kmap(pg);
292 ret = jffs2_write_inode_range(c, f, ri, page_address(pg) + aligned_start,
293 (pg->index << PAGE_CACHE_SHIFT) + aligned_start,
294 end - aligned_start, &writtenlen);
296 kunmap(pg);
298 if (ret) {
299 /* There was an error writing. */
300 SetPageError(pg);
303 /* Adjust writtenlen for the padding we did, so we don't confuse our caller */
304 writtenlen -= min(writtenlen, (start - aligned_start));
306 if (writtenlen) {
307 if (inode->i_size < pos + writtenlen) {
308 inode->i_size = pos + writtenlen;
309 inode->i_blocks = (inode->i_size + 511) >> 9;
311 inode->i_ctime = inode->i_mtime = ITIME(je32_to_cpu(ri->ctime));
315 jffs2_free_raw_inode(ri);
317 if (start+writtenlen < end) {
318 /* generic_file_write has written more to the page cache than we've
319 actually written to the medium. Mark the page !Uptodate so that
320 it gets reread */
321 D1(printk(KERN_DEBUG "jffs2_write_end(): Not all bytes written. Marking page !uptodate\n"));
322 SetPageError(pg);
323 ClearPageUptodate(pg);
326 D1(printk(KERN_DEBUG "jffs2_write_end() returning %d\n",
327 writtenlen > 0 ? writtenlen : ret));
328 unlock_page(pg);
329 page_cache_release(pg);
330 return writtenlen > 0 ? writtenlen : ret;