📄 gc.c
字号:
/* * JFFS2 -- Journalling Flash File System, Version 2. * * Copyright (C) 2001 Red Hat, Inc. * * Created by David Woodhouse <dwmw2@cambridge.redhat.com> * * The original JFFS, from which the design for JFFS2 was derived, * was designed and implemented by Axis Communications AB. * * The contents of this file are subject to the Red Hat eCos Public * License Version 1.1 (the "Licence"); you may not use this file * except in compliance with the Licence. You may obtain a copy of * the Licence at http://www.redhat.com/ * * Software distributed under the Licence is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. * See the Licence for the specific language governing rights and * limitations under the Licence. * * The Original Code is JFFS2 - Journalling Flash File System, version 2 * * Alternatively, the contents of this file may be used under the * terms of the GNU General Public License version 2 (the "GPL"), in * which case the provisions of the GPL are applicable instead of the * above. If you wish to allow the use of your version of this file * only under the terms of the GPL and not to allow others to use your * version of this file under the RHEPL, indicate your decision by * deleting the provisions above and replace them with the notice and * other provisions required by the GPL. If you do not delete the * provisions above, a recipient may use your version of this file * under either the RHEPL or the GPL. * * $Id: gc.c,v 1.52.2.3 2002/05/12 17:27:08 dwmw2 Exp $ * */#include <linux/kernel.h>#include <linux/mtd/mtd.h>#include <linux/slab.h>#include <linux/jffs2.h>#include <linux/sched.h>#include <linux/interrupt.h>#include <linux/pagemap.h>#include "nodelist.h"#include "crc32.h"static int jffs2_garbage_collect_metadata(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, struct inode *inode, struct jffs2_full_dnode *fd);static int jffs2_garbage_collect_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, struct inode *inode, struct jffs2_full_dirent *fd);static int jffs2_garbage_collect_deletion_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, struct inode *inode, struct jffs2_full_dirent *fd);static int jffs2_garbage_collect_hole(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, struct inode *indeo, struct jffs2_full_dnode *fn, __u32 start, __u32 end);static int jffs2_garbage_collect_dnode(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, struct inode *inode, struct jffs2_full_dnode *fn, __u32 start, __u32 end);/* Called with erase_completion_lock held */static struct jffs2_eraseblock *jffs2_find_gc_block(struct jffs2_sb_info *c){ struct jffs2_eraseblock *ret; struct list_head *nextlist = NULL; /* Pick an eraseblock to garbage collect next. This is where we'll put the clever wear-levelling algorithms. Eventually. */ if (!list_empty(&c->bad_used_list) && c->nr_free_blocks > JFFS2_RESERVED_BLOCKS_GCBAD) { D1(printk(KERN_DEBUG "Picking block from bad_used_list to GC next\n")); nextlist = &c->bad_used_list; } else if (jiffies % 100 && !list_empty(&c->dirty_list)) { /* Most of the time, pick one off the dirty list */ D1(printk(KERN_DEBUG "Picking block from dirty_list to GC next\n")); nextlist = &c->dirty_list; } else if (!list_empty(&c->clean_list)) { D1(printk(KERN_DEBUG "Picking block from clean_list to GC next\n")); nextlist = &c->clean_list; } else if (!list_empty(&c->dirty_list)) { D1(printk(KERN_DEBUG "Picking block from dirty_list to GC next (clean_list was empty)\n")); nextlist = &c->dirty_list; } else { /* Eep. Both were empty */ printk(KERN_NOTICE "jffs2: No clean _or_ dirty blocks to GC from! Where are they all?\n"); return NULL; } ret = list_entry(nextlist->next, struct jffs2_eraseblock, list); list_del(&ret->list); c->gcblock = ret; ret->gc_node = ret->first_node; if (!ret->gc_node) { printk(KERN_WARNING "Eep. ret->gc_node for block at 0x%08x is NULL\n", ret->offset); BUG(); } return ret;}/* jffs2_garbage_collect_pass * Make a single attempt to progress GC. Move one node, and possibly * start erasing one eraseblock. */int jffs2_garbage_collect_pass(struct jffs2_sb_info *c){ struct jffs2_eraseblock *jeb; struct jffs2_inode_info *f; struct jffs2_raw_node_ref *raw; struct jffs2_node_frag *frag; struct jffs2_full_dnode *fn = NULL; struct jffs2_full_dirent *fd; __u32 start = 0, end = 0, nrfrags = 0; __u32 inum; struct inode *inode; int ret = 0; if (down_interruptible(&c->alloc_sem)) return -EINTR; spin_lock_bh(&c->erase_completion_lock); /* First, work out which block we're garbage-collecting */ jeb = c->gcblock; if (!jeb) jeb = jffs2_find_gc_block(c); if (!jeb) { printk(KERN_NOTICE "jffs2: Couldn't find erase block to garbage collect!\n"); spin_unlock_bh(&c->erase_completion_lock); up(&c->alloc_sem); return -EIO; } D1(printk(KERN_DEBUG "garbage collect from block at phys 0x%08x\n", jeb->offset)); if (!jeb->used_size) goto eraseit; raw = jeb->gc_node; while(raw->flash_offset & 1) { D1(printk(KERN_DEBUG "Node at 0x%08x is obsolete... skipping\n", raw->flash_offset &~3)); jeb->gc_node = raw = raw->next_phys; if (!raw) { printk(KERN_WARNING "eep. End of raw list while still supposedly nodes to GC\n"); printk(KERN_WARNING "erase block at 0x%08x. free_size 0x%08x, dirty_size 0x%08x, used_size 0x%08x\n", jeb->offset, jeb->free_size, jeb->dirty_size, jeb->used_size); spin_unlock_bh(&c->erase_completion_lock); up(&c->alloc_sem); BUG(); } } D1(printk(KERN_DEBUG "Going to garbage collect node at 0x%08x\n", raw->flash_offset &~3)); if (!raw->next_in_ino) { /* Inode-less node. Clean marker, snapshot or something like that */ spin_unlock_bh(&c->erase_completion_lock); jffs2_mark_node_obsolete(c, raw); goto eraseit_lock; } inum = jffs2_raw_ref_to_inum(raw); D1(printk(KERN_DEBUG "Inode number is #%u\n", inum)); spin_unlock_bh(&c->erase_completion_lock); D1(printk(KERN_DEBUG "jffs2_garbage_collect_pass collecting from block @0x%08x. Node @0x%08x, ino #%u\n", jeb->offset, raw->flash_offset&~3, inum)); inode = iget(OFNI_BS_2SFFJ(c), inum); if (is_bad_inode(inode)) { printk(KERN_NOTICE "Eep. read_inode() failed for ino #%u\n", inum); /* NB. This will happen again. We need to do something appropriate here. */ iput(inode); up(&c->alloc_sem); return -EIO; } f = JFFS2_INODE_INFO(inode); down(&f->sem); /* Now we have the lock for this inode. Check that it's still the one at the head of the list. */ if (raw->flash_offset & 1) { D1(printk(KERN_DEBUG "node to be GC'd was obsoleted in the meantime.\n")); /* They'll call again */ goto upnout; } /* OK. Looks safe. And nobody can get us now because we have the semaphore. Move the block */ if (f->metadata && f->metadata->raw == raw) { fn = f->metadata; ret = jffs2_garbage_collect_metadata(c, jeb, inode, fn); goto upnout; } for (frag = f->fraglist; frag; frag = frag->next) { if (frag->node && frag->node->raw == raw) { fn = frag->node; end = frag->ofs + frag->size; if (!nrfrags++) start = frag->ofs; if (nrfrags == frag->node->frags) break; /* We've found them all */ } } if (fn) { /* We found a datanode. Do the GC */ if((start >> PAGE_CACHE_SHIFT) < ((end-1) >> PAGE_CACHE_SHIFT)) { /* It crosses a page boundary. Therefore, it must be a hole. */ ret = jffs2_garbage_collect_hole(c, jeb, inode, fn, start, end); } else { /* It could still be a hole. But we GC the page this way anyway */ ret = jffs2_garbage_collect_dnode(c, jeb, inode, fn, start, end); } goto upnout; } /* Wasn't a dnode. Try dirent */ for (fd = f->dents; fd; fd=fd->next) { if (fd->raw == raw) break; } if (fd && fd->ino) { ret = jffs2_garbage_collect_dirent(c, jeb, inode, fd); } else if (fd) { ret = jffs2_garbage_collect_deletion_dirent(c, jeb, inode, fd); } else { printk(KERN_WARNING "Raw node at 0x%08x wasn't in node lists for ino #%lu\n", raw->flash_offset&~3, inode->i_ino); if (raw->flash_offset & 1) { printk(KERN_WARNING "But it's obsolete so we don't mind too much\n"); } else { ret = -EIO; } } upnout: up(&f->sem); iput(inode); eraseit_lock: /* If we've finished this block, start it erasing */ spin_lock_bh(&c->erase_completion_lock); eraseit: if (c->gcblock && !c->gcblock->used_size) { D1(printk(KERN_DEBUG "Block at 0x%08x completely obsoleted by GC. Moving to erase_pending_list\n", c->gcblock->offset)); /* We're GC'ing an empty block? */ list_add_tail(&c->gcblock->list, &c->erase_pending_list); c->gcblock = NULL; c->nr_erasing_blocks++; jffs2_erase_pending_trigger(c); } spin_unlock_bh(&c->erase_completion_lock); up(&c->alloc_sem); return ret;}static int jffs2_garbage_collect_metadata(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, struct inode *inode, struct jffs2_full_dnode *fn){ struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode); struct jffs2_full_dnode *new_fn; struct jffs2_raw_inode ri; unsigned short dev; char *mdata = NULL, mdatalen = 0; __u32 alloclen, phys_ofs; int ret; if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) { /* For these, we don't actually need to read the old node */ dev = (MAJOR(to_kdev_t(inode->i_rdev)) << 8) | MINOR(to_kdev_t(inode->i_rdev)); mdata = (char *)&dev; mdatalen = sizeof(dev); D1(printk(KERN_DEBUG "jffs2_garbage_collect_metadata(): Writing %d bytes of kdev_t\n", mdatalen)); } else if (S_ISLNK(inode->i_mode)) { mdatalen = fn->size; mdata = kmalloc(fn->size, GFP_KERNEL); if (!mdata) { printk(KERN_WARNING "kmalloc of mdata failed in jffs2_garbage_collect_metadata()\n"); return -ENOMEM; } ret = jffs2_read_dnode(c, fn, mdata, 0, mdatalen); if (ret) { printk(KERN_WARNING "read of old metadata failed in jffs2_garbage_collect_metadata(): %d\n", ret); kfree(mdata); return ret; } D1(printk(KERN_DEBUG "jffs2_garbage_collect_metadata(): Writing %d bites of symlink target\n", mdatalen)); } ret = jffs2_reserve_space_gc(c, sizeof(ri) + mdatalen, &phys_ofs, &alloclen); if (ret) { printk(KERN_WARNING "jffs2_reserve_space_gc of %d bytes for garbage_collect_metadata failed: %d\n", sizeof(ri)+ mdatalen, ret); goto out; } memset(&ri, 0, sizeof(ri)); ri.magic = JFFS2_MAGIC_BITMASK; ri.nodetype = JFFS2_NODETYPE_INODE; ri.totlen = sizeof(ri) + mdatalen; ri.hdr_crc = crc32(0, &ri, sizeof(struct jffs2_unknown_node)-4); ri.ino = inode->i_ino; ri.version = ++f->highest_version; ri.mode = inode->i_mode; ri.uid = inode->i_uid; ri.gid = inode->i_gid; ri.isize = inode->i_size; ri.atime = inode->i_atime; ri.ctime = inode->i_ctime; ri.mtime = inode->i_mtime; ri.offset = 0; ri.csize = mdatalen; ri.dsize = mdatalen; ri.compr = JFFS2_COMPR_NONE; ri.node_crc = crc32(0, &ri, sizeof(ri)-8); ri.data_crc = crc32(0, mdata, mdatalen); new_fn = jffs2_write_dnode(inode, &ri, mdata, mdatalen, phys_ofs, NULL); if (IS_ERR(new_fn)) { printk(KERN_WARNING "Error writing new dnode: %ld\n", PTR_ERR(new_fn)); ret = PTR_ERR(new_fn); goto out; } jffs2_mark_node_obsolete(c, fn->raw); jffs2_free_full_dnode(fn); f->metadata = new_fn; out: if (S_ISLNK(inode->i_mode)) kfree(mdata); return ret;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -