xref: /linux/fs/jffs2/file.c (revision 3630400697a3d334a391c1dba1b601d852145f2c)
1 /*
2  * JFFS2 -- Journalling Flash File System, Version 2.
3  *
4  * Copyright © 2001-2007 Red Hat, Inc.
5  * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
6  *
7  * Created by David Woodhouse <dwmw2@infradead.org>
8  *
9  * For licensing information, see the file 'LICENCE' in this directory.
10  *
11  */
12 
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 
15 #include <linux/kernel.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_write_end(struct file *filp, struct address_space *mapping,
25 			loff_t pos, unsigned len, unsigned copied,
26 			struct folio *folio, void *fsdata);
27 static int jffs2_write_begin(struct file *filp, struct address_space *mapping,
28 			loff_t pos, unsigned len,
29 			struct folio **foliop, void **fsdata);
30 static int jffs2_read_folio(struct file *filp, struct folio *folio);
31 
32 int jffs2_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
33 {
34 	struct inode *inode = filp->f_mapping->host;
35 	struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
36 	int ret;
37 
38 	ret = file_write_and_wait_range(filp, start, end);
39 	if (ret)
40 		return ret;
41 
42 	inode_lock(inode);
43 	/* Trigger GC to flush any pending writes for this inode */
44 	jffs2_flush_wbuf_gc(c, inode->i_ino);
45 	inode_unlock(inode);
46 
47 	return 0;
48 }
49 
50 const struct file_operations jffs2_file_operations =
51 {
52 	.llseek =	generic_file_llseek,
53 	.open =		generic_file_open,
54  	.read_iter =	generic_file_read_iter,
55  	.write_iter =	generic_file_write_iter,
56 	.unlocked_ioctl=jffs2_ioctl,
57 	.mmap =		generic_file_readonly_mmap,
58 	.fsync =	jffs2_fsync,
59 	.splice_read =	filemap_splice_read,
60 	.splice_write = iter_file_splice_write,
61 };
62 
63 /* jffs2_file_inode_operations */
64 
65 const struct inode_operations jffs2_file_inode_operations =
66 {
67 	.get_inode_acl =	jffs2_get_acl,
68 	.set_acl =	jffs2_set_acl,
69 	.setattr =	jffs2_setattr,
70 	.listxattr =	jffs2_listxattr,
71 };
72 
73 const struct address_space_operations jffs2_file_address_operations =
74 {
75 	.read_folio =	jffs2_read_folio,
76 	.write_begin =	jffs2_write_begin,
77 	.write_end =	jffs2_write_end,
78 };
79 
80 static int jffs2_do_readpage_nolock(struct inode *inode, struct folio *folio)
81 {
82 	struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
83 	struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
84 	unsigned char *kaddr;
85 	int ret;
86 
87 	jffs2_dbg(2, "%s(): ino #%lu, page at offset 0x%lx\n",
88 		  __func__, inode->i_ino, folio->index << PAGE_SHIFT);
89 
90 	BUG_ON(!folio_test_locked(folio));
91 
92 	kaddr = kmap_local_folio(folio, 0);
93 	ret = jffs2_read_inode_range(c, f, kaddr, folio->index << PAGE_SHIFT,
94 				     PAGE_SIZE);
95 	kunmap_local(kaddr);
96 
97 	if (!ret)
98 		folio_mark_uptodate(folio);
99 
100 	flush_dcache_folio(folio);
101 
102 	jffs2_dbg(2, "readpage finished\n");
103 	return ret;
104 }
105 
106 int __jffs2_read_folio(struct file *file, struct folio *folio)
107 {
108 	int ret = jffs2_do_readpage_nolock(folio->mapping->host, folio);
109 	folio_unlock(folio);
110 	return ret;
111 }
112 
113 static int jffs2_read_folio(struct file *file, struct folio *folio)
114 {
115 	struct jffs2_inode_info *f = JFFS2_INODE_INFO(folio->mapping->host);
116 	int ret;
117 
118 	mutex_lock(&f->sem);
119 	ret = __jffs2_read_folio(file, folio);
120 	mutex_unlock(&f->sem);
121 	return ret;
122 }
123 
124 static int jffs2_write_begin(struct file *filp, struct address_space *mapping,
125 			loff_t pos, unsigned len,
126 			struct folio **foliop, void **fsdata)
127 {
128 	struct folio *folio;
129 	struct inode *inode = mapping->host;
130 	struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
131 	struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
132 	pgoff_t index = pos >> PAGE_SHIFT;
133 	int ret = 0;
134 
135 	jffs2_dbg(1, "%s()\n", __func__);
136 
137 	if (pos > inode->i_size) {
138 		/* Make new hole frag from old EOF to new position */
139 		struct jffs2_raw_inode ri;
140 		struct jffs2_full_dnode *fn;
141 		uint32_t alloc_len;
142 
143 		jffs2_dbg(1, "Writing new hole frag 0x%x-0x%x between current EOF and new position\n",
144 			  (unsigned int)inode->i_size, (uint32_t)pos);
145 
146 		ret = jffs2_reserve_space(c, sizeof(ri), &alloc_len,
147 					  ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
148 		if (ret)
149 			goto out_err;
150 
151 		mutex_lock(&f->sem);
152 		memset(&ri, 0, sizeof(ri));
153 
154 		ri.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
155 		ri.nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
156 		ri.totlen = cpu_to_je32(sizeof(ri));
157 		ri.hdr_crc = cpu_to_je32(crc32(0, &ri, sizeof(struct jffs2_unknown_node)-4));
158 
159 		ri.ino = cpu_to_je32(f->inocache->ino);
160 		ri.version = cpu_to_je32(++f->highest_version);
161 		ri.mode = cpu_to_jemode(inode->i_mode);
162 		ri.uid = cpu_to_je16(i_uid_read(inode));
163 		ri.gid = cpu_to_je16(i_gid_read(inode));
164 		ri.isize = cpu_to_je32((uint32_t)pos);
165 		ri.atime = ri.ctime = ri.mtime = cpu_to_je32(JFFS2_NOW());
166 		ri.offset = cpu_to_je32(inode->i_size);
167 		ri.dsize = cpu_to_je32((uint32_t)pos - inode->i_size);
168 		ri.csize = cpu_to_je32(0);
169 		ri.compr = JFFS2_COMPR_ZERO;
170 		ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8));
171 		ri.data_crc = cpu_to_je32(0);
172 
173 		fn = jffs2_write_dnode(c, f, &ri, NULL, 0, ALLOC_NORMAL);
174 
175 		if (IS_ERR(fn)) {
176 			ret = PTR_ERR(fn);
177 			jffs2_complete_reservation(c);
178 			mutex_unlock(&f->sem);
179 			goto out_err;
180 		}
181 		ret = jffs2_add_full_dnode_to_inode(c, f, fn);
182 		if (f->metadata) {
183 			jffs2_mark_node_obsolete(c, f->metadata->raw);
184 			jffs2_free_full_dnode(f->metadata);
185 			f->metadata = NULL;
186 		}
187 		if (ret) {
188 			jffs2_dbg(1, "Eep. add_full_dnode_to_inode() failed in write_begin, returned %d\n",
189 				  ret);
190 			jffs2_mark_node_obsolete(c, fn->raw);
191 			jffs2_free_full_dnode(fn);
192 			jffs2_complete_reservation(c);
193 			mutex_unlock(&f->sem);
194 			goto out_err;
195 		}
196 		jffs2_complete_reservation(c);
197 		inode->i_size = pos;
198 		mutex_unlock(&f->sem);
199 	}
200 
201 	/*
202 	 * While getting a page and reading data in, lock c->alloc_sem until
203 	 * the page is Uptodate. Otherwise GC task may attempt to read the same
204 	 * page in read_cache_page(), which causes a deadlock.
205 	 */
206 	mutex_lock(&c->alloc_sem);
207 	folio = __filemap_get_folio(mapping, index, FGP_WRITEBEGIN,
208 			mapping_gfp_mask(mapping));
209 	if (IS_ERR(folio)) {
210 		ret = PTR_ERR(folio);
211 		goto release_sem;
212 	}
213 	*foliop = folio;
214 
215 	/*
216 	 * Read in the folio if it wasn't already present. Cannot optimize away
217 	 * the whole folio write case until jffs2_write_end can handle the
218 	 * case of a short-copy.
219 	 */
220 	if (!folio_test_uptodate(folio)) {
221 		mutex_lock(&f->sem);
222 		ret = jffs2_do_readpage_nolock(inode, folio);
223 		mutex_unlock(&f->sem);
224 		if (ret) {
225 			folio_unlock(folio);
226 			folio_put(folio);
227 			goto release_sem;
228 		}
229 	}
230 	jffs2_dbg(1, "end write_begin(). folio->flags %lx\n", folio->flags);
231 
232 release_sem:
233 	mutex_unlock(&c->alloc_sem);
234 out_err:
235 	return ret;
236 }
237 
238 static int jffs2_write_end(struct file *filp, struct address_space *mapping,
239 			loff_t pos, unsigned len, unsigned copied,
240 			struct folio *folio, void *fsdata)
241 {
242 	/* Actually commit the write from the page cache page we're looking at.
243 	 * For now, we write the full page out each time. It sucks, but it's simple
244 	 */
245 	struct inode *inode = mapping->host;
246 	struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
247 	struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
248 	struct jffs2_raw_inode *ri;
249 	unsigned start = pos & (PAGE_SIZE - 1);
250 	unsigned end = start + copied;
251 	unsigned aligned_start = start & ~3;
252 	int ret = 0;
253 	uint32_t writtenlen = 0;
254 	void *buf;
255 
256 	jffs2_dbg(1, "%s(): ino #%lu, page at 0x%llx, range %d-%d, flags %lx\n",
257 		  __func__, inode->i_ino, folio_pos(folio),
258 		  start, end, folio->flags);
259 
260 	/* We need to avoid deadlock with page_cache_read() in
261 	   jffs2_garbage_collect_pass(). So the folio must be
262 	   up to date to prevent page_cache_read() from trying
263 	   to re-lock it. */
264 	BUG_ON(!folio_test_uptodate(folio));
265 
266 	if (end == PAGE_SIZE) {
267 		/* When writing out the end of a page, write out the
268 		   _whole_ page. This helps to reduce the number of
269 		   nodes in files which have many short writes, like
270 		   syslog files. */
271 		aligned_start = 0;
272 	}
273 
274 	ri = jffs2_alloc_raw_inode();
275 
276 	if (!ri) {
277 		jffs2_dbg(1, "%s(): Allocation of raw inode failed\n",
278 			  __func__);
279 		folio_unlock(folio);
280 		folio_put(folio);
281 		return -ENOMEM;
282 	}
283 
284 	/* Set the fields that the generic jffs2_write_inode_range() code can't find */
285 	ri->ino = cpu_to_je32(inode->i_ino);
286 	ri->mode = cpu_to_jemode(inode->i_mode);
287 	ri->uid = cpu_to_je16(i_uid_read(inode));
288 	ri->gid = cpu_to_je16(i_gid_read(inode));
289 	ri->isize = cpu_to_je32((uint32_t)inode->i_size);
290 	ri->atime = ri->ctime = ri->mtime = cpu_to_je32(JFFS2_NOW());
291 
292 	buf = kmap_local_folio(folio, aligned_start);
293 	ret = jffs2_write_inode_range(c, f, ri, buf,
294 				      folio_pos(folio) + aligned_start,
295 				      end - aligned_start, &writtenlen);
296 	kunmap_local(buf);
297 
298 	if (ret)
299 		mapping_set_error(mapping, ret);
300 
301 	/* Adjust writtenlen for the padding we did, so we don't confuse our caller */
302 	writtenlen -= min(writtenlen, (start - aligned_start));
303 
304 	if (writtenlen) {
305 		if (inode->i_size < pos + writtenlen) {
306 			inode->i_size = pos + writtenlen;
307 			inode->i_blocks = (inode->i_size + 511) >> 9;
308 
309 			inode_set_mtime_to_ts(inode,
310 					      inode_set_ctime_to_ts(inode, ITIME(je32_to_cpu(ri->ctime))));
311 		}
312 	}
313 
314 	jffs2_free_raw_inode(ri);
315 
316 	if (start+writtenlen < end) {
317 		/* generic_file_write has written more to the page cache than we've
318 		   actually written to the medium. Mark the page !Uptodate so that
319 		   it gets reread */
320 		jffs2_dbg(1, "%s(): Not all bytes written. Marking page !uptodate\n",
321 			__func__);
322 		folio_clear_uptodate(folio);
323 	}
324 
325 	jffs2_dbg(1, "%s() returning %d\n",
326 		  __func__, writtenlen > 0 ? writtenlen : ret);
327 	folio_unlock(folio);
328 	folio_put(folio);
329 	return writtenlen > 0 ? writtenlen : ret;
330 }
331