]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/fs/jffs2/v2_0/src/file-ecos.c
RedBoot TX53 Release 2012-02-15
[karo-tx-redboot.git] / packages / fs / jffs2 / v2_0 / src / file-ecos.c
1 /*
2  * JFFS2 -- Journalling Flash File System, Version 2.
3  *
4  * Copyright (C) 2001, 2002 Red Hat, Inc.
5  *
6  * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
7  *
8  * For licensing information, see the file 'LICENCE' in this directory.
9  *
10  * $Id$
11  *
12  */
13
14 #include <linux/kernel.h>
15 #include <linux/pagemap.h>
16 #include <linux/crc32.h>
17 #include "nodelist.h"
18
19 int jffs2_do_readpage_nolock (struct inode *inode, struct page *pg)
20 {
21         struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
22         struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
23         unsigned char *pg_buf;
24         int ret;
25
26         D1(printk(KERN_DEBUG "jffs2_do_readpage_nolock(): ino #%lu, page at offset 0x%lx\n", inode->i_ino, pg->index << PAGE_CACHE_SHIFT));
27
28         if (!PageLocked(pg))
29                 PAGE_BUG(pg);
30
31         pg_buf = (char *)kmap(pg);
32         /* FIXME: Can kmap fail? */
33
34         ret = jffs2_read_inode_range(c, f, pg_buf, pg->index << PAGE_CACHE_SHIFT, PAGE_CACHE_SIZE);
35
36         if (ret) {
37                 ClearPageUptodate(pg);
38                 SetPageError(pg);
39         } else {
40                 SetPageUptodate(pg);
41                 ClearPageError(pg);
42         }
43
44         flush_dcache_page(pg);
45         kunmap(pg);
46
47         D1(printk(KERN_DEBUG "readpage finished\n"));
48         return 0;
49 }
50
51 int jffs2_do_readpage_unlock(struct inode *inode, struct page *pg)
52 {
53         int ret = jffs2_do_readpage_nolock(inode, pg);
54         UnlockPage(pg);
55         return ret;
56 }
57
58
59 //int jffs2_readpage (struct file *filp, struct page *pg)
60 int jffs2_readpage (struct inode *d_inode, struct page *pg)
61 {
62         //      struct jffs2_inode_info *f = JFFS2_INODE_INFO(d_inode);
63         int ret;
64         
65         //      down(&f->sem);
66         ret = jffs2_do_readpage_unlock(d_inode, pg);
67         //      up(&f->sem);
68         return ret;
69 }
70
71 //int jffs2_prepare_write (struct file *filp, struct page *pg, unsigned start, unsigned end)
72 int jffs2_prepare_write (struct inode *d_inode, struct page *pg, unsigned start, unsigned end)
73 {
74         struct inode *inode = d_inode;
75         struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
76         uint32_t pageofs = pg->index << PAGE_CACHE_SHIFT;
77         int ret = 0;
78
79         D1(printk(KERN_DEBUG "jffs2_prepare_write()\n"));
80
81         if (pageofs > inode->i_size) {
82                 /* Make new hole frag from old EOF to new page */
83                 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
84                 struct jffs2_raw_inode ri;
85                 struct jffs2_full_dnode *fn;
86                 uint32_t phys_ofs, alloc_len;
87                 
88                 D1(printk(KERN_DEBUG "Writing new hole frag 0x%x-0x%x between current EOF and new page\n",
89                           (unsigned int)inode->i_size, pageofs));
90
91                 ret = jffs2_reserve_space(c, sizeof(ri), &phys_ofs, &alloc_len, ALLOC_NORMAL);
92                 if (ret)
93                         return ret;
94
95                 down(&f->sem);
96                 memset(&ri, 0, sizeof(ri));
97
98                 ri.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
99                 ri.nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
100                 ri.totlen = cpu_to_je32(sizeof(ri));
101                 ri.hdr_crc = cpu_to_je32(crc32(0, &ri, sizeof(struct jffs2_unknown_node)-4));
102
103                 ri.ino = cpu_to_je32(f->inocache->ino);
104                 ri.version = cpu_to_je32(++f->highest_version);
105                 ri.mode = cpu_to_jemode(inode->i_mode);
106                 ri.uid = cpu_to_je16(inode->i_uid);
107                 ri.gid = cpu_to_je16(inode->i_gid);
108
109                 ri.isize = cpu_to_je32(max((uint32_t)inode->i_size, pageofs));
110                 ri.atime = ri.ctime = ri.mtime = cpu_to_je32(cyg_timestamp());
111                 ri.offset = cpu_to_je32(inode->i_size);
112                 ri.dsize = cpu_to_je32(pageofs - inode->i_size);
113                 ri.csize = cpu_to_je32(0);
114                 ri.compr = JFFS2_COMPR_ZERO;
115                 ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8));
116                 ri.data_crc = cpu_to_je32(0);
117                 
118                 fn = jffs2_write_dnode(c, f, &ri, NULL, 0, phys_ofs, NULL);
119                 jffs2_complete_reservation(c);
120                 if (IS_ERR(fn)) {
121                         ret = PTR_ERR(fn);
122                         up(&f->sem);
123                         return ret;
124                 }
125                 ret = jffs2_add_full_dnode_to_inode(c, f, fn);
126                 if (f->metadata) {
127                         jffs2_mark_node_obsolete(c, f->metadata->raw);
128                         jffs2_free_full_dnode(f->metadata);
129                         f->metadata = NULL;
130                 }
131                 if (ret) {
132                         D1(printk(KERN_DEBUG "Eep. add_full_dnode_to_inode() failed in prepare_write, returned %d\n", ret));
133                         jffs2_mark_node_obsolete(c, fn->raw);
134                         jffs2_free_full_dnode(fn);
135                         up(&f->sem);
136                         return ret;
137                 }
138                 inode->i_size = pageofs;
139                 up(&f->sem);
140         }
141         
142
143         /* Read in the page if it wasn't already present, unless it's a whole page */
144         // eCos has no concept of uptodate and by default always reads pages afresh
145         if (!Page_Uptodate(pg) && (start || end < PAGE_CACHE_SIZE)) {
146                 down(&f->sem);
147                 ret = jffs2_do_readpage_nolock(inode, pg);
148                 up(&f->sem);
149         }
150         D1(printk(KERN_DEBUG "end prepare_write()\n"));
151         return ret;
152 }
153
154 //int jffs2_commit_write (struct file *filp, struct page *pg, unsigned start, unsigned end)
155 int jffs2_commit_write (struct inode *d_inode, struct page *pg, unsigned start, unsigned end)
156 {
157         /* Actually commit the write from the page cache page we're looking at.
158          * For now, we write the full page out each time. It sucks, but it's simple
159          */
160         struct inode *inode = d_inode;
161         struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
162         struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
163         struct jffs2_raw_inode *ri;
164         int ret = 0;
165         uint32_t writtenlen = 0;
166
167         D1(printk(KERN_DEBUG "jffs2_commit_write(): ino #%lu, page at 0x%lx, range %d-%d\n",
168                   inode->i_ino, pg->index << PAGE_CACHE_SHIFT, start, end));
169
170
171         ri = jffs2_alloc_raw_inode();
172         if (!ri) {
173                 D1(printk(KERN_DEBUG "jffs2_commit_write(): Allocation of raw inode failed\n"));
174                 return -ENOMEM;
175         }
176
177         /* Set the fields that the generic jffs2_write_inode_range() code can't find */
178         ri->ino = cpu_to_je32(inode->i_ino);
179         ri->mode = cpu_to_jemode(inode->i_mode);
180         ri->uid = cpu_to_je16(inode->i_uid);
181         ri->gid = cpu_to_je16(inode->i_gid);
182         ri->isize = cpu_to_je32((uint32_t)inode->i_size);
183         ri->atime = ri->ctime = ri->mtime = cpu_to_je32(cyg_timestamp());
184
185         ret = jffs2_write_inode_range(c, f, ri, page_address(pg) + start,
186                                       (pg->index << PAGE_CACHE_SHIFT) + start,
187                                       end - start, &writtenlen);
188
189         if (ret) {
190                 /* There was an error writing. */
191                 SetPageError(pg);
192         }
193
194         if (writtenlen) {
195                 if (inode->i_size < (pg->index << PAGE_CACHE_SHIFT) + start + writtenlen) {
196                         inode->i_size = (pg->index << PAGE_CACHE_SHIFT) + start + writtenlen;
197                         inode->i_ctime = inode->i_mtime = je32_to_cpu(ri->ctime);
198                 }
199         }
200
201         jffs2_free_raw_inode(ri);
202
203         if (start+writtenlen < end) {
204                 /* generic_file_write has written more to the page cache than we've
205                    actually written to the medium. Mark the page !Uptodate so that 
206                    it gets reread */
207                 D1(printk(KERN_DEBUG "jffs2_commit_write(): Not all bytes written. Marking page !uptodate\n"));
208                 SetPageError(pg);
209                 ClearPageUptodate(pg);
210         }
211
212         D1(printk(KERN_DEBUG "jffs2_commit_write() returning %d\n",writtenlen?writtenlen:ret));
213         return writtenlen?writtenlen:ret;
214 }