xref: /openbmc/linux/fs/jffs2/file.c (revision 1da177e4)
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.99 2004/11/16 20:36:11 dwmw2 Exp $
11  *
12  */
13 
14 #include <linux/version.h>
15 #include <linux/kernel.h>
16 #include <linux/slab.h>
17 #include <linux/fs.h>
18 #include <linux/time.h>
19 #include <linux/pagemap.h>
20 #include <linux/highmem.h>
21 #include <linux/crc32.h>
22 #include <linux/jffs2.h>
23 #include "nodelist.h"
24 
25 extern int generic_file_open(struct inode *, struct file *) __attribute__((weak));
26 extern loff_t generic_file_llseek(struct file *file, loff_t offset, int origin) __attribute__((weak));
27 
28 static int jffs2_commit_write (struct file *filp, struct page *pg,
29 			       unsigned start, unsigned end);
30 static int jffs2_prepare_write (struct file *filp, struct page *pg,
31 				unsigned start, unsigned end);
32 static int jffs2_readpage (struct file *filp, struct page *pg);
33 
34 int jffs2_fsync(struct file *filp, struct dentry *dentry, int datasync)
35 {
36 	struct inode *inode = dentry->d_inode;
37 	struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
38 
39 	/* Trigger GC to flush any pending writes for this inode */
40 	jffs2_flush_wbuf_gc(c, inode->i_ino);
41 
42 	return 0;
43 }
44 
45 struct file_operations jffs2_file_operations =
46 {
47 	.llseek =	generic_file_llseek,
48 	.open =		generic_file_open,
49 	.read =		generic_file_read,
50 	.write =	generic_file_write,
51 	.ioctl =	jffs2_ioctl,
52 	.mmap =		generic_file_readonly_mmap,
53 	.fsync =	jffs2_fsync,
54 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,29)
55 	.sendfile =	generic_file_sendfile
56 #endif
57 };
58 
59 /* jffs2_file_inode_operations */
60 
61 struct inode_operations jffs2_file_inode_operations =
62 {
63 	.setattr =	jffs2_setattr
64 };
65 
66 struct address_space_operations jffs2_file_address_operations =
67 {
68 	.readpage =	jffs2_readpage,
69 	.prepare_write =jffs2_prepare_write,
70 	.commit_write =	jffs2_commit_write
71 };
72 
73 static int jffs2_do_readpage_nolock (struct inode *inode, struct page *pg)
74 {
75 	struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
76 	struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
77 	unsigned char *pg_buf;
78 	int ret;
79 
80 	D2(printk(KERN_DEBUG "jffs2_do_readpage_nolock(): ino #%lu, page at offset 0x%lx\n", inode->i_ino, pg->index << PAGE_CACHE_SHIFT));
81 
82 	if (!PageLocked(pg))
83                 PAGE_BUG(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 phys_ofs, 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), &phys_ofs, &alloc_len, ALLOC_NORMAL);
145 		if (ret)
146 			return ret;
147 
148 		down(&f->sem);
149 		memset(&ri, 0, sizeof(ri));
150 
151 		ri.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
152 		ri.nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
153 		ri.totlen = cpu_to_je32(sizeof(ri));
154 		ri.hdr_crc = cpu_to_je32(crc32(0, &ri, sizeof(struct jffs2_unknown_node)-4));
155 
156 		ri.ino = cpu_to_je32(f->inocache->ino);
157 		ri.version = cpu_to_je32(++f->highest_version);
158 		ri.mode = cpu_to_jemode(inode->i_mode);
159 		ri.uid = cpu_to_je16(inode->i_uid);
160 		ri.gid = cpu_to_je16(inode->i_gid);
161 		ri.isize = cpu_to_je32(max((uint32_t)inode->i_size, pageofs));
162 		ri.atime = ri.ctime = ri.mtime = cpu_to_je32(get_seconds());
163 		ri.offset = cpu_to_je32(inode->i_size);
164 		ri.dsize = cpu_to_je32(pageofs - inode->i_size);
165 		ri.csize = cpu_to_je32(0);
166 		ri.compr = JFFS2_COMPR_ZERO;
167 		ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8));
168 		ri.data_crc = cpu_to_je32(0);
169 
170 		fn = jffs2_write_dnode(c, f, &ri, NULL, 0, phys_ofs, ALLOC_NORMAL);
171 
172 		if (IS_ERR(fn)) {
173 			ret = PTR_ERR(fn);
174 			jffs2_complete_reservation(c);
175 			up(&f->sem);
176 			return ret;
177 		}
178 		ret = jffs2_add_full_dnode_to_inode(c, f, fn);
179 		if (f->metadata) {
180 			jffs2_mark_node_obsolete(c, f->metadata->raw);
181 			jffs2_free_full_dnode(f->metadata);
182 			f->metadata = NULL;
183 		}
184 		if (ret) {
185 			D1(printk(KERN_DEBUG "Eep. add_full_dnode_to_inode() failed in prepare_write, returned %d\n", ret));
186 			jffs2_mark_node_obsolete(c, fn->raw);
187 			jffs2_free_full_dnode(fn);
188 			jffs2_complete_reservation(c);
189 			up(&f->sem);
190 			return ret;
191 		}
192 		jffs2_complete_reservation(c);
193 		inode->i_size = pageofs;
194 		up(&f->sem);
195 	}
196 
197 	/* Read in the page if it wasn't already present, unless it's a whole page */
198 	if (!PageUptodate(pg) && (start || end < PAGE_CACHE_SIZE)) {
199 		down(&f->sem);
200 		ret = jffs2_do_readpage_nolock(inode, pg);
201 		up(&f->sem);
202 	}
203 	D1(printk(KERN_DEBUG "end prepare_write(). pg->flags %lx\n", pg->flags));
204 	return ret;
205 }
206 
207 static int jffs2_commit_write (struct file *filp, struct page *pg,
208 			       unsigned start, unsigned end)
209 {
210 	/* Actually commit the write from the page cache page we're looking at.
211 	 * For now, we write the full page out each time. It sucks, but it's simple
212 	 */
213 	struct inode *inode = pg->mapping->host;
214 	struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
215 	struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
216 	struct jffs2_raw_inode *ri;
217 	unsigned aligned_start = start & ~3;
218 	int ret = 0;
219 	uint32_t writtenlen = 0;
220 
221 	D1(printk(KERN_DEBUG "jffs2_commit_write(): ino #%lu, page at 0x%lx, range %d-%d, flags %lx\n",
222 		  inode->i_ino, pg->index << PAGE_CACHE_SHIFT, start, end, pg->flags));
223 
224 	if (!start && end == PAGE_CACHE_SIZE) {
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 	}
231 
232 	ri = jffs2_alloc_raw_inode();
233 
234 	if (!ri) {
235 		D1(printk(KERN_DEBUG "jffs2_commit_write(): Allocation of raw inode failed\n"));
236 		return -ENOMEM;
237 	}
238 
239 	/* Set the fields that the generic jffs2_write_inode_range() code can't find */
240 	ri->ino = cpu_to_je32(inode->i_ino);
241 	ri->mode = cpu_to_jemode(inode->i_mode);
242 	ri->uid = cpu_to_je16(inode->i_uid);
243 	ri->gid = cpu_to_je16(inode->i_gid);
244 	ri->isize = cpu_to_je32((uint32_t)inode->i_size);
245 	ri->atime = ri->ctime = ri->mtime = cpu_to_je32(get_seconds());
246 
247 	/* In 2.4, it was already kmapped by generic_file_write(). Doesn't
248 	   hurt to do it again. The alternative is ifdefs, which are ugly. */
249 	kmap(pg);
250 
251 	ret = jffs2_write_inode_range(c, f, ri, page_address(pg) + aligned_start,
252 				      (pg->index << PAGE_CACHE_SHIFT) + aligned_start,
253 				      end - aligned_start, &writtenlen);
254 
255 	kunmap(pg);
256 
257 	if (ret) {
258 		/* There was an error writing. */
259 		SetPageError(pg);
260 	}
261 
262 	/* Adjust writtenlen for the padding we did, so we don't confuse our caller */
263 	if (writtenlen < (start&3))
264 		writtenlen = 0;
265 	else
266 		writtenlen -= (start&3);
267 
268 	if (writtenlen) {
269 		if (inode->i_size < (pg->index << PAGE_CACHE_SHIFT) + start + writtenlen) {
270 			inode->i_size = (pg->index << PAGE_CACHE_SHIFT) + start + writtenlen;
271 			inode->i_blocks = (inode->i_size + 511) >> 9;
272 
273 			inode->i_ctime = inode->i_mtime = ITIME(je32_to_cpu(ri->ctime));
274 		}
275 	}
276 
277 	jffs2_free_raw_inode(ri);
278 
279 	if (start+writtenlen < end) {
280 		/* generic_file_write has written more to the page cache than we've
281 		   actually written to the medium. Mark the page !Uptodate so that
282 		   it gets reread */
283 		D1(printk(KERN_DEBUG "jffs2_commit_write(): Not all bytes written. Marking page !uptodate\n"));
284 		SetPageError(pg);
285 		ClearPageUptodate(pg);
286 	}
287 
288 	D1(printk(KERN_DEBUG "jffs2_commit_write() returning %d\n",writtenlen?writtenlen:ret));
289 	return writtenlen?writtenlen:ret;
290 }
291