0095a70a3426c7de4073b47259e53c8c343f4a2f
[pandora-kernel.git] / fs / jffs2 / file.c
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 #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"
21
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);
29
30 int jffs2_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
31 {
32         struct inode *inode = filp->f_mapping->host;
33         struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
34         int ret;
35
36         ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
37         if (ret)
38                 return ret;
39
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);
44
45         return 0;
46 }
47
48 const struct file_operations jffs2_file_operations =
49 {
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,
60 };
61
62 /* jffs2_file_inode_operations */
63
64 const struct inode_operations jffs2_file_inode_operations =
65 {
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
72 };
73
74 const struct address_space_operations jffs2_file_address_operations =
75 {
76         .readpage =     jffs2_readpage,
77         .write_begin =  jffs2_write_begin,
78         .write_end =    jffs2_write_end,
79 };
80
81 static int jffs2_do_readpage_nolock (struct inode *inode, struct page *pg)
82 {
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;
87
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));
89
90         BUG_ON(!PageLocked(pg));
91
92         pg_buf = kmap(pg);
93         /* FIXME: Can kmap fail? */
94
95         ret = jffs2_read_inode_range(c, f, pg_buf, pg->index << PAGE_CACHE_SHIFT, PAGE_CACHE_SIZE);
96
97         if (ret) {
98                 ClearPageUptodate(pg);
99                 SetPageError(pg);
100         } else {
101                 SetPageUptodate(pg);
102                 ClearPageError(pg);
103         }
104
105         flush_dcache_page(pg);
106         kunmap(pg);
107
108         D2(printk(KERN_DEBUG "readpage finished\n"));
109         return ret;
110 }
111
112 int jffs2_do_readpage_unlock(struct inode *inode, struct page *pg)
113 {
114         int ret = jffs2_do_readpage_nolock(inode, pg);
115         unlock_page(pg);
116         return ret;
117 }
118
119
120 static int jffs2_readpage (struct file *filp, struct page *pg)
121 {
122         struct jffs2_inode_info *f = JFFS2_INODE_INFO(pg->mapping->host);
123         int ret;
124
125         mutex_lock(&f->sem);
126         ret = jffs2_do_readpage_unlock(pg->mapping->host, pg);
127         mutex_unlock(&f->sem);
128         return ret;
129 }
130
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)
134 {
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;
144
145         D1(printk(KERN_DEBUG "%s()\n", __func__));
146
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;
152         }
153
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;
161         }
162         *pagep = pg;
163
164         if (alloc_len) {
165                 /* Make new hole frag from old EOF to new page */
166                 struct jffs2_full_dnode *fn;
167
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));
170
171                 memset(&ri, 0, sizeof(ri));
172
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));
177
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);
191
192                 fn = jffs2_write_dnode(c, f, &ri, NULL, 0, ALLOC_NORMAL);
193
194                 if (IS_ERR(fn)) {
195                         ret = PTR_ERR(fn);
196                         jffs2_complete_reservation(c);
197                         goto out_page;
198                 }
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;
204                 }
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;
211                 }
212                 jffs2_complete_reservation(c);
213                 inode->i_size = pageofs;
214         }
215
216         /*
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.
220          */
221         if (!PageUptodate(pg)) {
222                 ret = jffs2_do_readpage_nolock(inode, pg);
223                 if (ret)
224                         goto out_page;
225         }
226         mutex_unlock(&f->sem);
227         D1(printk(KERN_DEBUG "end write_begin(). pg->flags %lx\n", pg->flags));
228         return ret;
229
230 out_page:
231         unlock_page(pg);
232         page_cache_release(pg);
233         mutex_unlock(&f->sem);
234         return ret;
235 }
236
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)
240 {
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
243          */
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;
253
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));
256
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));
262
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;
269         }
270
271         ri = jffs2_alloc_raw_inode();
272
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;
278         }
279
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());
287
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);
291
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);
295
296         kunmap(pg);
297
298         if (ret) {
299                 /* There was an error writing. */
300                 SetPageError(pg);
301         }
302
303         /* Adjust writtenlen for the padding we did, so we don't confuse our caller */
304         writtenlen -= min(writtenlen, (start - aligned_start));
305
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;
310
311                         inode->i_ctime = inode->i_mtime = ITIME(je32_to_cpu(ri->ctime));
312                 }
313         }
314
315         jffs2_free_raw_inode(ri);
316
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);
324         }
325
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;
331 }