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