2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright (C) 2001 Red Hat, Inc.
6 * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
8 * The original JFFS, from which the design for JFFS2 was derived,
9 * was designed and implemented by Axis Communications AB.
11 * The contents of this file are subject to the Red Hat eCos Public
12 * License Version 1.1 (the "Licence"); you may not use this file
13 * except in compliance with the Licence. You may obtain a copy of
14 * the Licence at http://www.redhat.com/
16 * Software distributed under the Licence is distributed on an "AS IS"
17 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
18 * See the Licence for the specific language governing rights and
19 * limitations under the Licence.
21 * The Original Code is JFFS2 - Journalling Flash File System, version 2
23 * Alternatively, the contents of this file may be used under the
24 * terms of the GNU General Public License version 2 (the "GPL"), in
25 * which case the provisions of the GPL are applicable instead of the
26 * above. If you wish to allow the use of your version of this file
27 * only under the terms of the GPL and not to allow others to use your
28 * version of this file under the RHEPL, indicate your decision by
29 * deleting the provisions above and replace them with the notice and
30 * other provisions required by the GPL. If you do not delete the
31 * provisions above, a recipient may use your version of this file
32 * under either the RHEPL or the GPL.
34 * $Id: file.c,v 1.58.2.7 2003/11/02 13:51:17 dwmw2 Exp $
38 #include <linux/kernel.h>
39 #include <linux/mtd/compatmac.h> /* for min() */
40 #include <linux/slab.h>
42 #include <linux/pagemap.h>
43 #include <linux/jffs2.h>
45 #include <linux/crc32.h>
47 extern int generic_file_open(struct inode *, struct file *) __attribute__((weak));
48 extern loff_t generic_file_llseek(struct file *file, loff_t offset, int origin) __attribute__((weak));
51 int jffs2_null_fsync(struct file *filp, struct dentry *dentry, int datasync)
53 /* Move along. Nothing to see here */
57 struct file_operations jffs2_file_operations =
59 llseek: generic_file_llseek,
60 open: generic_file_open,
61 read: generic_file_read,
62 write: generic_file_write,
64 mmap: generic_file_mmap,
65 fsync: jffs2_null_fsync
68 /* jffs2_file_inode_operations */
70 struct inode_operations jffs2_file_inode_operations =
72 setattr: jffs2_setattr
75 struct address_space_operations jffs2_file_address_operations =
77 readpage: jffs2_readpage,
78 prepare_write: jffs2_prepare_write,
79 commit_write: jffs2_commit_write
82 int jffs2_setattr (struct dentry *dentry, struct iattr *iattr)
84 struct jffs2_full_dnode *old_metadata, *new_metadata;
85 struct inode *inode = dentry->d_inode;
86 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
87 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
88 struct jffs2_raw_inode *ri;
90 unsigned char *mdata = NULL;
93 __u32 phys_ofs, alloclen;
95 D1(printk(KERN_DEBUG "jffs2_setattr(): ino #%lu\n", inode->i_ino));
96 ret = inode_change_ok(inode, iattr);
100 /* Special cases - we don't want more than one data node
101 for these types on the medium at any time. So setattr
102 must read the original data associated with the node
103 (i.e. the device numbers or the target name) and write
104 it out again with the appropriate data attached */
105 if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
106 /* For these, we don't actually need to read the old node */
107 dev = (MAJOR(to_kdev_t(dentry->d_inode->i_rdev)) << 8) |
108 MINOR(to_kdev_t(dentry->d_inode->i_rdev));
109 mdata = (char *)&dev;
110 mdatalen = sizeof(dev);
111 D1(printk(KERN_DEBUG "jffs2_setattr(): Writing %d bytes of kdev_t\n", mdatalen));
112 } else if (S_ISLNK(inode->i_mode)) {
113 mdatalen = f->metadata->size;
114 mdata = kmalloc(f->metadata->size, GFP_USER);
117 ret = jffs2_read_dnode(c, f->metadata, mdata, 0, mdatalen);
122 D1(printk(KERN_DEBUG "jffs2_setattr(): Writing %d bytes of symlink target\n", mdatalen));
125 ri = jffs2_alloc_raw_inode();
127 if (S_ISLNK(inode->i_mode))
132 ret = jffs2_reserve_space(c, sizeof(*ri) + mdatalen, &phys_ofs, &alloclen, ALLOC_NORMAL);
134 jffs2_free_raw_inode(ri);
135 if (S_ISLNK(inode->i_mode))
140 ivalid = iattr->ia_valid;
142 ri->magic = JFFS2_MAGIC_BITMASK;
143 ri->nodetype = JFFS2_NODETYPE_INODE;
144 ri->totlen = sizeof(*ri) + mdatalen;
145 ri->hdr_crc = crc32(0, ri, sizeof(struct jffs2_unknown_node)-4);
147 ri->ino = inode->i_ino;
148 ri->version = ++f->highest_version;
150 ri->mode = (ivalid & ATTR_MODE)?iattr->ia_mode:inode->i_mode;
151 ri->uid = (ivalid & ATTR_UID)?iattr->ia_uid:inode->i_uid;
152 ri->gid = (ivalid & ATTR_GID)?iattr->ia_gid:inode->i_gid;
154 if (ivalid & ATTR_MODE && ri->mode & S_ISGID &&
155 !in_group_p(ri->gid) && !capable(CAP_FSETID))
156 ri->mode &= ~S_ISGID;
158 ri->isize = (ivalid & ATTR_SIZE)?iattr->ia_size:inode->i_size;
159 ri->atime = (ivalid & ATTR_ATIME)?iattr->ia_atime:inode->i_atime;
160 ri->mtime = (ivalid & ATTR_MTIME)?iattr->ia_mtime:inode->i_mtime;
161 ri->ctime = (ivalid & ATTR_CTIME)?iattr->ia_ctime:inode->i_ctime;
164 ri->csize = ri->dsize = mdatalen;
165 ri->compr = JFFS2_COMPR_NONE;
166 if (inode->i_size < ri->isize) {
167 /* It's an extension. Make it a hole node */
168 ri->compr = JFFS2_COMPR_ZERO;
169 ri->dsize = ri->isize - inode->i_size;
170 ri->offset = inode->i_size;
172 ri->node_crc = crc32(0, ri, sizeof(*ri)-8);
174 ri->data_crc = crc32(0, mdata, mdatalen);
178 new_metadata = jffs2_write_dnode(inode, ri, mdata, mdatalen, phys_ofs, NULL);
179 if (S_ISLNK(inode->i_mode))
182 jffs2_complete_reservation(c);
184 if (IS_ERR(new_metadata)) {
185 jffs2_free_raw_inode(ri);
187 return PTR_ERR(new_metadata);
189 /* It worked. Update the inode */
190 inode->i_atime = ri->atime;
191 inode->i_ctime = ri->ctime;
192 inode->i_mtime = ri->mtime;
193 inode->i_mode = ri->mode;
194 inode->i_uid = ri->uid;
195 inode->i_gid = ri->gid;
198 old_metadata = f->metadata;
200 if (inode->i_size > ri->isize) {
201 vmtruncate(inode, ri->isize);
202 jffs2_truncate_fraglist (c, &f->fraglist, ri->isize);
205 if (inode->i_size < ri->isize) {
206 jffs2_add_full_dnode_to_inode(c, f, new_metadata);
207 inode->i_size = ri->isize;
210 f->metadata = new_metadata;
213 jffs2_mark_node_obsolete(c, old_metadata->raw);
214 jffs2_free_full_dnode(old_metadata);
216 jffs2_free_raw_inode(ri);
221 int jffs2_do_readpage_nolock (struct inode *inode, struct page *pg)
223 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
224 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
225 struct jffs2_node_frag *frag = f->fraglist;
226 __u32 offset = pg->index << PAGE_CACHE_SHIFT;
227 __u32 end = offset + PAGE_CACHE_SIZE;
228 unsigned char *pg_buf;
231 D1(printk(KERN_DEBUG "jffs2_do_readpage_nolock(): ino #%lu, page at offset 0x%x\n", inode->i_ino, offset));
236 while(frag && frag->ofs + frag->size <= offset) {
237 // D1(printk(KERN_DEBUG "skipping frag %d-%d; before the region we care about\n", frag->ofs, frag->ofs + frag->size));
243 /* XXX FIXME: Where a single physical node actually shows up in two
244 frags, we read it twice. Don't do that. */
245 /* Now we're pointing at the first frag which overlaps our page */
246 while(offset < end) {
247 D2(printk(KERN_DEBUG "jffs2_readpage: offset %d, end %d\n", offset, end));
248 if (!frag || frag->ofs > offset) {
249 __u32 holesize = end - offset;
251 D1(printk(KERN_NOTICE "Eep. Hole in ino %ld fraglist. frag->ofs = 0x%08x, offset = 0x%08x\n", inode->i_ino, frag->ofs, offset));
252 holesize = min(holesize, frag->ofs - offset);
253 D1(jffs2_print_frag_list(f));
255 D1(printk(KERN_DEBUG "Filling non-frag hole from %d-%d\n", offset, offset+holesize));
256 memset(pg_buf, 0, holesize);
260 } else if (frag->ofs < offset && (offset & (PAGE_CACHE_SIZE-1)) != 0) {
261 D1(printk(KERN_NOTICE "Eep. Overlap in ino #%ld fraglist. frag->ofs = 0x%08x, offset = 0x%08x\n",
262 inode->i_ino, frag->ofs, offset));
263 D1(jffs2_print_frag_list(f));
264 memset(pg_buf, 0, end - offset);
265 ClearPageUptodate(pg);
269 } else if (!frag->node) {
270 __u32 holeend = min(end, frag->ofs + frag->size);
271 D1(printk(KERN_DEBUG "Filling frag hole from %d-%d (frag 0x%x 0x%x)\n", offset, holeend, frag->ofs, frag->ofs + frag->size));
272 memset(pg_buf, 0, holeend - offset);
273 pg_buf += holeend - offset;
279 __u32 fragofs; /* offset within the frag to start reading */
281 fragofs = offset - frag->ofs;
282 readlen = min(frag->size - fragofs, end - offset);
283 D1(printk(KERN_DEBUG "Reading %d-%d from node at 0x%x\n", frag->ofs+fragofs,
284 fragofs+frag->ofs+readlen, frag->node->raw->flash_offset & ~3));
285 ret = jffs2_read_dnode(c, frag->node, pg_buf, fragofs + frag->ofs - frag->node->ofs, readlen);
286 D2(printk(KERN_DEBUG "node read done\n"));
288 D1(printk(KERN_DEBUG"jffs2_readpage error %d\n",ret));
289 memset(pg_buf, 0, readlen);
290 ClearPageUptodate(pg);
299 D2(printk(KERN_DEBUG "node read was OK. Looping\n"));
302 D2(printk(KERN_DEBUG "readpage finishing\n"));
306 flush_dcache_page(pg);
309 D1(printk(KERN_DEBUG "readpage finished\n"));
313 int jffs2_do_readpage_unlock(struct inode *inode, struct page *pg)
315 int ret = jffs2_do_readpage_nolock(inode, pg);
321 int jffs2_readpage (struct file *filp, struct page *pg)
323 struct jffs2_inode_info *f = JFFS2_INODE_INFO(pg->mapping->host);
327 ret = jffs2_do_readpage_unlock(pg->mapping->host, pg);
332 int jffs2_prepare_write (struct file *filp, struct page *pg, unsigned start, unsigned end)
334 struct inode *inode = pg->mapping->host;
335 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
336 __u32 pageofs = pg->index << PAGE_CACHE_SHIFT;
339 D1(printk(KERN_DEBUG "jffs2_prepare_write() nrpages %ld\n", inode->i_mapping->nrpages));
341 if (pageofs > inode->i_size) {
342 /* Make new hole frag from old EOF to new page */
343 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
344 struct jffs2_raw_inode ri;
345 struct jffs2_full_dnode *fn;
346 __u32 phys_ofs, alloc_len;
348 D1(printk(KERN_DEBUG "Writing new hole frag 0x%x-0x%x between current EOF and new page\n",
349 (unsigned int)inode->i_size, pageofs));
351 ret = jffs2_reserve_space(c, sizeof(ri), &phys_ofs, &alloc_len, ALLOC_NORMAL);
356 memset(&ri, 0, sizeof(ri));
358 ri.magic = JFFS2_MAGIC_BITMASK;
359 ri.nodetype = JFFS2_NODETYPE_INODE;
360 ri.totlen = sizeof(ri);
361 ri.hdr_crc = crc32(0, &ri, sizeof(struct jffs2_unknown_node)-4);
363 ri.ino = f->inocache->ino;
364 ri.version = ++f->highest_version;
365 ri.mode = inode->i_mode;
366 ri.uid = inode->i_uid;
367 ri.gid = inode->i_gid;
368 ri.isize = max((__u32)inode->i_size, pageofs);
369 ri.atime = ri.ctime = ri.mtime = CURRENT_TIME;
370 ri.offset = inode->i_size;
371 ri.dsize = pageofs - inode->i_size;
373 ri.compr = JFFS2_COMPR_ZERO;
374 ri.node_crc = crc32(0, &ri, sizeof(ri)-8);
377 fn = jffs2_write_dnode(inode, &ri, NULL, 0, phys_ofs, NULL);
378 jffs2_complete_reservation(c);
384 ret = jffs2_add_full_dnode_to_inode(c, f, fn);
386 jffs2_mark_node_obsolete(c, f->metadata->raw);
387 jffs2_free_full_dnode(f->metadata);
391 D1(printk(KERN_DEBUG "Eep. add_full_dnode_to_inode() failed in prepare_write, returned %d\n", ret));
392 jffs2_mark_node_obsolete(c, fn->raw);
393 jffs2_free_full_dnode(fn);
397 inode->i_size = pageofs;
402 /* Read in the page if it wasn't already present, unless it's a whole page */
403 if (!Page_Uptodate(pg) && (start || end < PAGE_CACHE_SIZE)) {
405 ret = jffs2_do_readpage_nolock(inode, pg);
408 D1(printk(KERN_DEBUG "end prepare_write(). pg->flags %lx\n", pg->flags));
412 int jffs2_commit_write (struct file *filp, struct page *pg, unsigned start, unsigned end)
414 /* Actually commit the write from the page cache page we're looking at.
415 * For now, we write the full page out each time. It sucks, but it's simple
417 struct inode *inode = pg->mapping->host;
418 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
419 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
420 __u32 newsize = max_t(__u32, filp->f_dentry->d_inode->i_size, (pg->index << PAGE_CACHE_SHIFT) + end);
421 __u32 file_ofs = (pg->index << PAGE_CACHE_SHIFT);
422 __u32 writelen = min((__u32)PAGE_CACHE_SIZE, newsize - file_ofs);
423 struct jffs2_raw_inode *ri;
425 ssize_t writtenlen = 0;
427 D1(printk(KERN_DEBUG "jffs2_commit_write(): ino #%lu, page at 0x%lx, range %d-%d, flags %lx\n", inode->i_ino, pg->index << PAGE_CACHE_SHIFT, start, end, pg->flags));
429 if (!start && end == PAGE_CACHE_SIZE) {
430 /* We need to avoid deadlock with page_cache_read() in
431 jffs2_garbage_collect_pass(). So we have to mark the
432 page up to date, to prevent page_cache_read() from
433 trying to re-lock it. */
437 ri = jffs2_alloc_raw_inode();
442 struct jffs2_full_dnode *fn;
443 unsigned char *comprbuf = NULL;
444 unsigned char comprtype = JFFS2_COMPR_NONE;
445 __u32 phys_ofs, alloclen;
446 __u32 datalen, cdatalen;
448 D2(printk(KERN_DEBUG "jffs2_commit_write() loop: 0x%x to write to 0x%x\n", writelen, file_ofs));
450 ret = jffs2_reserve_space(c, sizeof(*ri) + JFFS2_MIN_DATA_LEN, &phys_ofs, &alloclen, ALLOC_NORMAL);
453 D1(printk(KERN_DEBUG "jffs2_reserve_space returned %d\n", ret));
458 cdatalen = min(alloclen - sizeof(*ri), writelen);
460 comprbuf = kmalloc(cdatalen, GFP_KERNEL);
462 comprtype = jffs2_compress(page_address(pg)+ (file_ofs & (PAGE_CACHE_SIZE-1)), comprbuf, &datalen, &cdatalen);
464 if (comprtype == JFFS2_COMPR_NONE) {
465 /* Either compression failed, or the allocation of comprbuf failed */
468 comprbuf = page_address(pg) + (file_ofs & (PAGE_CACHE_SIZE -1));
471 /* Now comprbuf points to the data to be written, be it compressed or not.
472 comprtype holds the compression type, and comprtype == JFFS2_COMPR_NONE means
473 that the comprbuf doesn't need to be kfree()d.
476 ri->magic = JFFS2_MAGIC_BITMASK;
477 ri->nodetype = JFFS2_NODETYPE_INODE;
478 ri->totlen = sizeof(*ri) + cdatalen;
479 ri->hdr_crc = crc32(0, ri, sizeof(struct jffs2_unknown_node)-4);
481 ri->ino = inode->i_ino;
482 ri->version = ++f->highest_version;
483 ri->mode = inode->i_mode;
484 ri->uid = inode->i_uid;
485 ri->gid = inode->i_gid;
486 ri->isize = max((__u32)inode->i_size, file_ofs + datalen);
487 ri->atime = ri->ctime = ri->mtime = CURRENT_TIME;
488 ri->offset = file_ofs;
489 ri->csize = cdatalen;
491 ri->compr = comprtype;
492 ri->node_crc = crc32(0, ri, sizeof(*ri)-8);
493 ri->data_crc = crc32(0, comprbuf, cdatalen);
495 fn = jffs2_write_dnode(inode, ri, comprbuf, cdatalen, phys_ofs, NULL);
497 jffs2_complete_reservation(c);
499 if (comprtype != JFFS2_COMPR_NONE)
508 ret = jffs2_add_full_dnode_to_inode(c, f, fn);
510 jffs2_mark_node_obsolete(c, f->metadata->raw);
511 jffs2_free_full_dnode(f->metadata);
517 D1(printk(KERN_DEBUG "Eep. add_full_dnode_to_inode() failed in commit_write, returned %d\n", ret));
518 jffs2_mark_node_obsolete(c, fn->raw);
519 jffs2_free_full_dnode(fn);
523 inode->i_size = ri->isize;
524 inode->i_blocks = (inode->i_size + 511) >> 9;
525 inode->i_ctime = inode->i_mtime = ri->ctime;
527 printk(KERN_WARNING "Eep. We didn't actually write any bloody data\n");
532 D1(printk(KERN_DEBUG "increasing writtenlen by %d\n", datalen));
533 writtenlen += datalen;
538 jffs2_free_raw_inode(ri);
540 if (writtenlen < end) {
541 /* generic_file_write has written more to the page cache than we've
542 actually written to the medium. Mark the page !Uptodate so that
544 D1(printk(KERN_DEBUG "jffs2_commit_write(): Not all bytes written. Marking page !uptodate\n"));
546 ClearPageUptodate(pg);
548 if (writtenlen <= start) {
549 /* We didn't even get to the start of the affected part */
550 ret = ret?ret:-ENOSPC;
551 D1(printk(KERN_DEBUG "jffs2_commit_write(): Only %x bytes written to page. start (%x) not reached, returning %d\n", writtenlen, start, ret));
553 writtenlen = min(end-start, writtenlen-start);
555 D1(printk(KERN_DEBUG "jffs2_commit_write() returning %d. nrpages is %ld\n",writtenlen?writtenlen:ret, inode->i_mapping->nrpages));
556 return writtenlen?writtenlen:ret;