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