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