Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * JFFS2 -- Journalling Flash File System, Version 2.
0003  *
0004  * Copyright © 2001-2007 Red Hat, Inc.
0005  * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
0006  *
0007  * Created by David Woodhouse <dwmw2@infradead.org>
0008  *
0009  * For licensing information, see the file 'LICENCE' in this directory.
0010  *
0011  */
0012 
0013 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0014 
0015 #include <linux/kernel.h>
0016 #include <linux/mtd/mtd.h>
0017 #include <linux/slab.h>
0018 #include <linux/pagemap.h>
0019 #include <linux/crc32.h>
0020 #include <linux/compiler.h>
0021 #include <linux/stat.h>
0022 #include "nodelist.h"
0023 #include "compr.h"
0024 
0025 static int jffs2_garbage_collect_pristine(struct jffs2_sb_info *c,
0026                       struct jffs2_inode_cache *ic,
0027                       struct jffs2_raw_node_ref *raw);
0028 static int jffs2_garbage_collect_metadata(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
0029                     struct jffs2_inode_info *f, struct jffs2_full_dnode *fd);
0030 static int jffs2_garbage_collect_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
0031                     struct jffs2_inode_info *f, struct jffs2_full_dirent *fd);
0032 static int jffs2_garbage_collect_deletion_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
0033                     struct jffs2_inode_info *f, struct jffs2_full_dirent *fd);
0034 static int jffs2_garbage_collect_hole(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
0035                       struct jffs2_inode_info *f, struct jffs2_full_dnode *fn,
0036                       uint32_t start, uint32_t end);
0037 static int jffs2_garbage_collect_dnode(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
0038                        struct jffs2_inode_info *f, struct jffs2_full_dnode *fn,
0039                        uint32_t start, uint32_t end);
0040 static int jffs2_garbage_collect_live(struct jffs2_sb_info *c,  struct jffs2_eraseblock *jeb,
0041                    struct jffs2_raw_node_ref *raw, struct jffs2_inode_info *f);
0042 
0043 /* Called with erase_completion_lock held */
0044 static struct jffs2_eraseblock *jffs2_find_gc_block(struct jffs2_sb_info *c)
0045 {
0046     struct jffs2_eraseblock *ret;
0047     struct list_head *nextlist = NULL;
0048     int n = jiffies % 128;
0049 
0050     /* Pick an eraseblock to garbage collect next. This is where we'll
0051        put the clever wear-levelling algorithms. Eventually.  */
0052     /* We possibly want to favour the dirtier blocks more when the
0053        number of free blocks is low. */
0054 again:
0055     if (!list_empty(&c->bad_used_list) && c->nr_free_blocks > c->resv_blocks_gcbad) {
0056         jffs2_dbg(1, "Picking block from bad_used_list to GC next\n");
0057         nextlist = &c->bad_used_list;
0058     } else if (n < 50 && !list_empty(&c->erasable_list)) {
0059         /* Note that most of them will have gone directly to be erased.
0060            So don't favour the erasable_list _too_ much. */
0061         jffs2_dbg(1, "Picking block from erasable_list to GC next\n");
0062         nextlist = &c->erasable_list;
0063     } else if (n < 110 && !list_empty(&c->very_dirty_list)) {
0064         /* Most of the time, pick one off the very_dirty list */
0065         jffs2_dbg(1, "Picking block from very_dirty_list to GC next\n");
0066         nextlist = &c->very_dirty_list;
0067     } else if (n < 126 && !list_empty(&c->dirty_list)) {
0068         jffs2_dbg(1, "Picking block from dirty_list to GC next\n");
0069         nextlist = &c->dirty_list;
0070     } else if (!list_empty(&c->clean_list)) {
0071         jffs2_dbg(1, "Picking block from clean_list to GC next\n");
0072         nextlist = &c->clean_list;
0073     } else if (!list_empty(&c->dirty_list)) {
0074         jffs2_dbg(1, "Picking block from dirty_list to GC next (clean_list was empty)\n");
0075 
0076         nextlist = &c->dirty_list;
0077     } else if (!list_empty(&c->very_dirty_list)) {
0078         jffs2_dbg(1, "Picking block from very_dirty_list to GC next (clean_list and dirty_list were empty)\n");
0079         nextlist = &c->very_dirty_list;
0080     } else if (!list_empty(&c->erasable_list)) {
0081         jffs2_dbg(1, "Picking block from erasable_list to GC next (clean_list and {very_,}dirty_list were empty)\n");
0082 
0083         nextlist = &c->erasable_list;
0084     } else if (!list_empty(&c->erasable_pending_wbuf_list)) {
0085         /* There are blocks are wating for the wbuf sync */
0086         jffs2_dbg(1, "Synching wbuf in order to reuse erasable_pending_wbuf_list blocks\n");
0087         spin_unlock(&c->erase_completion_lock);
0088         jffs2_flush_wbuf_pad(c);
0089         spin_lock(&c->erase_completion_lock);
0090         goto again;
0091     } else {
0092         /* Eep. All were empty */
0093         jffs2_dbg(1, "No clean, dirty _or_ erasable blocks to GC from! Where are they all?\n");
0094         return NULL;
0095     }
0096 
0097     ret = list_entry(nextlist->next, struct jffs2_eraseblock, list);
0098     list_del(&ret->list);
0099     c->gcblock = ret;
0100     ret->gc_node = ret->first_node;
0101     if (!ret->gc_node) {
0102         pr_warn("Eep. ret->gc_node for block at 0x%08x is NULL\n",
0103             ret->offset);
0104         BUG();
0105     }
0106 
0107     /* Have we accidentally picked a clean block with wasted space ? */
0108     if (ret->wasted_size) {
0109         jffs2_dbg(1, "Converting wasted_size %08x to dirty_size\n",
0110               ret->wasted_size);
0111         ret->dirty_size += ret->wasted_size;
0112         c->wasted_size -= ret->wasted_size;
0113         c->dirty_size += ret->wasted_size;
0114         ret->wasted_size = 0;
0115     }
0116 
0117     return ret;
0118 }
0119 
0120 /* jffs2_garbage_collect_pass
0121  * Make a single attempt to progress GC. Move one node, and possibly
0122  * start erasing one eraseblock.
0123  */
0124 int jffs2_garbage_collect_pass(struct jffs2_sb_info *c)
0125 {
0126     struct jffs2_inode_info *f;
0127     struct jffs2_inode_cache *ic;
0128     struct jffs2_eraseblock *jeb;
0129     struct jffs2_raw_node_ref *raw;
0130     uint32_t gcblock_dirty;
0131     int ret = 0, inum, nlink;
0132     int xattr = 0;
0133 
0134     if (mutex_lock_interruptible(&c->alloc_sem))
0135         return -EINTR;
0136 
0137 
0138     for (;;) {
0139         /* We can't start doing GC until we've finished checking
0140            the node CRCs etc. */
0141         int bucket, want_ino;
0142 
0143         spin_lock(&c->erase_completion_lock);
0144         if (!c->unchecked_size)
0145             break;
0146         spin_unlock(&c->erase_completion_lock);
0147 
0148         if (!xattr)
0149             xattr = jffs2_verify_xattr(c);
0150 
0151         spin_lock(&c->inocache_lock);
0152         /* Instead of doing the inodes in numeric order, doing a lookup
0153          * in the hash for each possible number, just walk the hash
0154          * buckets of *existing* inodes. This means that we process
0155          * them out-of-order, but it can be a lot faster if there's
0156          * a sparse inode# space. Which there often is. */
0157         want_ino = c->check_ino;
0158         for (bucket = c->check_ino % c->inocache_hashsize ; bucket < c->inocache_hashsize; bucket++) {
0159             for (ic = c->inocache_list[bucket]; ic; ic = ic->next) {
0160                 if (ic->ino < want_ino)
0161                     continue;
0162 
0163                 if (ic->state != INO_STATE_CHECKEDABSENT &&
0164                     ic->state != INO_STATE_PRESENT)
0165                     goto got_next; /* with inocache_lock held */
0166 
0167                 jffs2_dbg(1, "Skipping ino #%u already checked\n",
0168                       ic->ino);
0169             }
0170             want_ino = 0;
0171         }
0172 
0173         /* Point c->check_ino past the end of the last bucket. */
0174         c->check_ino = ((c->highest_ino + c->inocache_hashsize + 1) &
0175                 ~c->inocache_hashsize) - 1;
0176 
0177         spin_unlock(&c->inocache_lock);
0178 
0179         pr_crit("Checked all inodes but still 0x%x bytes of unchecked space?\n",
0180             c->unchecked_size);
0181         jffs2_dbg_dump_block_lists_nolock(c);
0182         mutex_unlock(&c->alloc_sem);
0183         return -ENOSPC;
0184 
0185     got_next:
0186         /* For next time round the loop, we want c->checked_ino to indicate
0187          * the *next* one we want to check. And since we're walking the
0188          * buckets rather than doing it sequentially, it's: */
0189         c->check_ino = ic->ino + c->inocache_hashsize;
0190 
0191         if (!ic->pino_nlink) {
0192             jffs2_dbg(1, "Skipping check of ino #%d with nlink/pino zero\n",
0193                   ic->ino);
0194             spin_unlock(&c->inocache_lock);
0195             jffs2_xattr_delete_inode(c, ic);
0196             continue;
0197         }
0198         switch(ic->state) {
0199         case INO_STATE_CHECKEDABSENT:
0200         case INO_STATE_PRESENT:
0201             spin_unlock(&c->inocache_lock);
0202             continue;
0203 
0204         case INO_STATE_GC:
0205         case INO_STATE_CHECKING:
0206             pr_warn("Inode #%u is in state %d during CRC check phase!\n",
0207                 ic->ino, ic->state);
0208             spin_unlock(&c->inocache_lock);
0209             BUG();
0210 
0211         case INO_STATE_READING:
0212             /* We need to wait for it to finish, lest we move on
0213                and trigger the BUG() above while we haven't yet
0214                finished checking all its nodes */
0215             jffs2_dbg(1, "Waiting for ino #%u to finish reading\n",
0216                   ic->ino);
0217             /* We need to come back again for the _same_ inode. We've
0218              made no progress in this case, but that should be OK */
0219             c->check_ino = ic->ino;
0220 
0221             mutex_unlock(&c->alloc_sem);
0222             sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock);
0223             return 0;
0224 
0225         default:
0226             BUG();
0227 
0228         case INO_STATE_UNCHECKED:
0229             ;
0230         }
0231         ic->state = INO_STATE_CHECKING;
0232         spin_unlock(&c->inocache_lock);
0233 
0234         jffs2_dbg(1, "%s(): triggering inode scan of ino#%u\n",
0235               __func__, ic->ino);
0236 
0237         ret = jffs2_do_crccheck_inode(c, ic);
0238         if (ret)
0239             pr_warn("Returned error for crccheck of ino #%u. Expect badness...\n",
0240                 ic->ino);
0241 
0242         jffs2_set_inocache_state(c, ic, INO_STATE_CHECKEDABSENT);
0243         mutex_unlock(&c->alloc_sem);
0244         return ret;
0245     }
0246 
0247     /* If there are any blocks which need erasing, erase them now */
0248     if (!list_empty(&c->erase_complete_list) ||
0249         !list_empty(&c->erase_pending_list)) {
0250         spin_unlock(&c->erase_completion_lock);
0251         mutex_unlock(&c->alloc_sem);
0252         jffs2_dbg(1, "%s(): erasing pending blocks\n", __func__);
0253         if (jffs2_erase_pending_blocks(c, 1))
0254             return 0;
0255 
0256         jffs2_dbg(1, "No progress from erasing block; doing GC anyway\n");
0257         mutex_lock(&c->alloc_sem);
0258         spin_lock(&c->erase_completion_lock);
0259     }
0260 
0261     /* First, work out which block we're garbage-collecting */
0262     jeb = c->gcblock;
0263 
0264     if (!jeb)
0265         jeb = jffs2_find_gc_block(c);
0266 
0267     if (!jeb) {
0268         /* Couldn't find a free block. But maybe we can just erase one and make 'progress'? */
0269         if (c->nr_erasing_blocks) {
0270             spin_unlock(&c->erase_completion_lock);
0271             mutex_unlock(&c->alloc_sem);
0272             return -EAGAIN;
0273         }
0274         jffs2_dbg(1, "Couldn't find erase block to garbage collect!\n");
0275         spin_unlock(&c->erase_completion_lock);
0276         mutex_unlock(&c->alloc_sem);
0277         return -EIO;
0278     }
0279 
0280     jffs2_dbg(1, "GC from block %08x, used_size %08x, dirty_size %08x, free_size %08x\n",
0281           jeb->offset, jeb->used_size, jeb->dirty_size, jeb->free_size);
0282     D1(if (c->nextblock)
0283        printk(KERN_DEBUG "Nextblock at  %08x, used_size %08x, dirty_size %08x, wasted_size %08x, free_size %08x\n", c->nextblock->offset, c->nextblock->used_size, c->nextblock->dirty_size, c->nextblock->wasted_size, c->nextblock->free_size));
0284 
0285     if (!jeb->used_size) {
0286         mutex_unlock(&c->alloc_sem);
0287         goto eraseit;
0288     }
0289 
0290     raw = jeb->gc_node;
0291     gcblock_dirty = jeb->dirty_size;
0292 
0293     while(ref_obsolete(raw)) {
0294         jffs2_dbg(1, "Node at 0x%08x is obsolete... skipping\n",
0295               ref_offset(raw));
0296         raw = ref_next(raw);
0297         if (unlikely(!raw)) {
0298             pr_warn("eep. End of raw list while still supposedly nodes to GC\n");
0299             pr_warn("erase block at 0x%08x. free_size 0x%08x, dirty_size 0x%08x, used_size 0x%08x\n",
0300                 jeb->offset, jeb->free_size,
0301                 jeb->dirty_size, jeb->used_size);
0302             jeb->gc_node = raw;
0303             spin_unlock(&c->erase_completion_lock);
0304             mutex_unlock(&c->alloc_sem);
0305             BUG();
0306         }
0307     }
0308     jeb->gc_node = raw;
0309 
0310     jffs2_dbg(1, "Going to garbage collect node at 0x%08x\n",
0311           ref_offset(raw));
0312 
0313     if (!raw->next_in_ino) {
0314         /* Inode-less node. Clean marker, snapshot or something like that */
0315         spin_unlock(&c->erase_completion_lock);
0316         if (ref_flags(raw) == REF_PRISTINE) {
0317             /* It's an unknown node with JFFS2_FEATURE_RWCOMPAT_COPY */
0318             jffs2_garbage_collect_pristine(c, NULL, raw);
0319         } else {
0320             /* Just mark it obsolete */
0321             jffs2_mark_node_obsolete(c, raw);
0322         }
0323         mutex_unlock(&c->alloc_sem);
0324         goto eraseit_lock;
0325     }
0326 
0327     ic = jffs2_raw_ref_to_ic(raw);
0328 
0329 #ifdef CONFIG_JFFS2_FS_XATTR
0330     /* When 'ic' refers xattr_datum/xattr_ref, this node is GCed as xattr.
0331      * We can decide whether this node is inode or xattr by ic->class.     */
0332     if (ic->class == RAWNODE_CLASS_XATTR_DATUM
0333         || ic->class == RAWNODE_CLASS_XATTR_REF) {
0334         spin_unlock(&c->erase_completion_lock);
0335 
0336         if (ic->class == RAWNODE_CLASS_XATTR_DATUM) {
0337             ret = jffs2_garbage_collect_xattr_datum(c, (struct jffs2_xattr_datum *)ic, raw);
0338         } else {
0339             ret = jffs2_garbage_collect_xattr_ref(c, (struct jffs2_xattr_ref *)ic, raw);
0340         }
0341         goto test_gcnode;
0342     }
0343 #endif
0344 
0345     /* We need to hold the inocache. Either the erase_completion_lock or
0346        the inocache_lock are sufficient; we trade down since the inocache_lock
0347        causes less contention. */
0348     spin_lock(&c->inocache_lock);
0349 
0350     spin_unlock(&c->erase_completion_lock);
0351 
0352     jffs2_dbg(1, "%s(): collecting from block @0x%08x. Node @0x%08x(%d), ino #%u\n",
0353           __func__, jeb->offset, ref_offset(raw), ref_flags(raw),
0354           ic->ino);
0355 
0356     /* Three possibilities:
0357        1. Inode is already in-core. We must iget it and do proper
0358           updating to its fragtree, etc.
0359        2. Inode is not in-core, node is REF_PRISTINE. We lock the
0360           inocache to prevent a read_inode(), copy the node intact.
0361        3. Inode is not in-core, node is not pristine. We must iget()
0362           and take the slow path.
0363     */
0364 
0365     switch(ic->state) {
0366     case INO_STATE_CHECKEDABSENT:
0367         /* It's been checked, but it's not currently in-core.
0368            We can just copy any pristine nodes, but have
0369            to prevent anyone else from doing read_inode() while
0370            we're at it, so we set the state accordingly */
0371         if (ref_flags(raw) == REF_PRISTINE)
0372             ic->state = INO_STATE_GC;
0373         else {
0374             jffs2_dbg(1, "Ino #%u is absent but node not REF_PRISTINE. Reading.\n",
0375                   ic->ino);
0376         }
0377         break;
0378 
0379     case INO_STATE_PRESENT:
0380         /* It's in-core. GC must iget() it. */
0381         break;
0382 
0383     case INO_STATE_UNCHECKED:
0384     case INO_STATE_CHECKING:
0385     case INO_STATE_GC:
0386         /* Should never happen. We should have finished checking
0387            by the time we actually start doing any GC, and since
0388            we're holding the alloc_sem, no other garbage collection
0389            can happen.
0390         */
0391         pr_crit("Inode #%u already in state %d in jffs2_garbage_collect_pass()!\n",
0392             ic->ino, ic->state);
0393         mutex_unlock(&c->alloc_sem);
0394         spin_unlock(&c->inocache_lock);
0395         BUG();
0396 
0397     case INO_STATE_READING:
0398         /* Someone's currently trying to read it. We must wait for
0399            them to finish and then go through the full iget() route
0400            to do the GC. However, sometimes read_inode() needs to get
0401            the alloc_sem() (for marking nodes invalid) so we must
0402            drop the alloc_sem before sleeping. */
0403 
0404         mutex_unlock(&c->alloc_sem);
0405         jffs2_dbg(1, "%s(): waiting for ino #%u in state %d\n",
0406               __func__, ic->ino, ic->state);
0407         sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock);
0408         /* And because we dropped the alloc_sem we must start again from the
0409            beginning. Ponder chance of livelock here -- we're returning success
0410            without actually making any progress.
0411 
0412            Q: What are the chances that the inode is back in INO_STATE_READING
0413            again by the time we next enter this function? And that this happens
0414            enough times to cause a real delay?
0415 
0416            A: Small enough that I don't care :)
0417         */
0418         return 0;
0419     }
0420 
0421     /* OK. Now if the inode is in state INO_STATE_GC, we are going to copy the
0422        node intact, and we don't have to muck about with the fragtree etc.
0423        because we know it's not in-core. If it _was_ in-core, we go through
0424        all the iget() crap anyway */
0425 
0426     if (ic->state == INO_STATE_GC) {
0427         spin_unlock(&c->inocache_lock);
0428 
0429         ret = jffs2_garbage_collect_pristine(c, ic, raw);
0430 
0431         spin_lock(&c->inocache_lock);
0432         ic->state = INO_STATE_CHECKEDABSENT;
0433         wake_up(&c->inocache_wq);
0434 
0435         if (ret != -EBADFD) {
0436             spin_unlock(&c->inocache_lock);
0437             goto test_gcnode;
0438         }
0439 
0440         /* Fall through if it wanted us to, with inocache_lock held */
0441     }
0442 
0443     /* Prevent the fairly unlikely race where the gcblock is
0444        entirely obsoleted by the final close of a file which had
0445        the only valid nodes in the block, followed by erasure,
0446        followed by freeing of the ic because the erased block(s)
0447        held _all_ the nodes of that inode.... never been seen but
0448        it's vaguely possible. */
0449 
0450     inum = ic->ino;
0451     nlink = ic->pino_nlink;
0452     spin_unlock(&c->inocache_lock);
0453 
0454     f = jffs2_gc_fetch_inode(c, inum, !nlink);
0455     if (IS_ERR(f)) {
0456         ret = PTR_ERR(f);
0457         goto release_sem;
0458     }
0459     if (!f) {
0460         ret = 0;
0461         goto release_sem;
0462     }
0463 
0464     ret = jffs2_garbage_collect_live(c, jeb, raw, f);
0465 
0466     jffs2_gc_release_inode(c, f);
0467 
0468  test_gcnode:
0469     if (jeb->dirty_size == gcblock_dirty && !ref_obsolete(jeb->gc_node)) {
0470         /* Eep. This really should never happen. GC is broken */
0471         pr_err("Error garbage collecting node at %08x!\n",
0472                ref_offset(jeb->gc_node));
0473         ret = -ENOSPC;
0474     }
0475  release_sem:
0476     mutex_unlock(&c->alloc_sem);
0477 
0478  eraseit_lock:
0479     /* If we've finished this block, start it erasing */
0480     spin_lock(&c->erase_completion_lock);
0481 
0482  eraseit:
0483     if (c->gcblock && !c->gcblock->used_size) {
0484         jffs2_dbg(1, "Block at 0x%08x completely obsoleted by GC. Moving to erase_pending_list\n",
0485               c->gcblock->offset);
0486         /* We're GC'ing an empty block? */
0487         list_add_tail(&c->gcblock->list, &c->erase_pending_list);
0488         c->gcblock = NULL;
0489         c->nr_erasing_blocks++;
0490         jffs2_garbage_collect_trigger(c);
0491     }
0492     spin_unlock(&c->erase_completion_lock);
0493 
0494     return ret;
0495 }
0496 
0497 static int jffs2_garbage_collect_live(struct jffs2_sb_info *c,  struct jffs2_eraseblock *jeb,
0498                       struct jffs2_raw_node_ref *raw, struct jffs2_inode_info *f)
0499 {
0500     struct jffs2_node_frag *frag;
0501     struct jffs2_full_dnode *fn = NULL;
0502     struct jffs2_full_dirent *fd;
0503     uint32_t start = 0, end = 0, nrfrags = 0;
0504     int ret = 0;
0505 
0506     mutex_lock(&f->sem);
0507 
0508     /* Now we have the lock for this inode. Check that it's still the one at the head
0509        of the list. */
0510 
0511     spin_lock(&c->erase_completion_lock);
0512 
0513     if (c->gcblock != jeb) {
0514         spin_unlock(&c->erase_completion_lock);
0515         jffs2_dbg(1, "GC block is no longer gcblock. Restart\n");
0516         goto upnout;
0517     }
0518     if (ref_obsolete(raw)) {
0519         spin_unlock(&c->erase_completion_lock);
0520         jffs2_dbg(1, "node to be GC'd was obsoleted in the meantime.\n");
0521         /* They'll call again */
0522         goto upnout;
0523     }
0524     spin_unlock(&c->erase_completion_lock);
0525 
0526     /* OK. Looks safe. And nobody can get us now because we have the semaphore. Move the block */
0527     if (f->metadata && f->metadata->raw == raw) {
0528         fn = f->metadata;
0529         ret = jffs2_garbage_collect_metadata(c, jeb, f, fn);
0530         goto upnout;
0531     }
0532 
0533     /* FIXME. Read node and do lookup? */
0534     for (frag = frag_first(&f->fragtree); frag; frag = frag_next(frag)) {
0535         if (frag->node && frag->node->raw == raw) {
0536             fn = frag->node;
0537             end = frag->ofs + frag->size;
0538             if (!nrfrags++)
0539                 start = frag->ofs;
0540             if (nrfrags == frag->node->frags)
0541                 break; /* We've found them all */
0542         }
0543     }
0544     if (fn) {
0545         if (ref_flags(raw) == REF_PRISTINE) {
0546             ret = jffs2_garbage_collect_pristine(c, f->inocache, raw);
0547             if (!ret) {
0548                 /* Urgh. Return it sensibly. */
0549                 frag->node->raw = f->inocache->nodes;
0550             }
0551             if (ret != -EBADFD)
0552                 goto upnout;
0553         }
0554         /* We found a datanode. Do the GC */
0555         if((start >> PAGE_SHIFT) < ((end-1) >> PAGE_SHIFT)) {
0556             /* It crosses a page boundary. Therefore, it must be a hole. */
0557             ret = jffs2_garbage_collect_hole(c, jeb, f, fn, start, end);
0558         } else {
0559             /* It could still be a hole. But we GC the page this way anyway */
0560             ret = jffs2_garbage_collect_dnode(c, jeb, f, fn, start, end);
0561         }
0562         goto upnout;
0563     }
0564 
0565     /* Wasn't a dnode. Try dirent */
0566     for (fd = f->dents; fd; fd=fd->next) {
0567         if (fd->raw == raw)
0568             break;
0569     }
0570 
0571     if (fd && fd->ino) {
0572         ret = jffs2_garbage_collect_dirent(c, jeb, f, fd);
0573     } else if (fd) {
0574         ret = jffs2_garbage_collect_deletion_dirent(c, jeb, f, fd);
0575     } else {
0576         pr_warn("Raw node at 0x%08x wasn't in node lists for ino #%u\n",
0577             ref_offset(raw), f->inocache->ino);
0578         if (ref_obsolete(raw)) {
0579             pr_warn("But it's obsolete so we don't mind too much\n");
0580         } else {
0581             jffs2_dbg_dump_node(c, ref_offset(raw));
0582             BUG();
0583         }
0584     }
0585  upnout:
0586     mutex_unlock(&f->sem);
0587 
0588     return ret;
0589 }
0590 
0591 static int jffs2_garbage_collect_pristine(struct jffs2_sb_info *c,
0592                       struct jffs2_inode_cache *ic,
0593                       struct jffs2_raw_node_ref *raw)
0594 {
0595     union jffs2_node_union *node;
0596     size_t retlen;
0597     int ret;
0598     uint32_t phys_ofs, alloclen;
0599     uint32_t crc, rawlen;
0600     int retried = 0;
0601 
0602     jffs2_dbg(1, "Going to GC REF_PRISTINE node at 0x%08x\n",
0603           ref_offset(raw));
0604 
0605     alloclen = rawlen = ref_totlen(c, c->gcblock, raw);
0606 
0607     /* Ask for a small amount of space (or the totlen if smaller) because we
0608        don't want to force wastage of the end of a block if splitting would
0609        work. */
0610     if (ic && alloclen > sizeof(struct jffs2_raw_inode) + JFFS2_MIN_DATA_LEN)
0611         alloclen = sizeof(struct jffs2_raw_inode) + JFFS2_MIN_DATA_LEN;
0612 
0613     ret = jffs2_reserve_space_gc(c, alloclen, &alloclen, rawlen);
0614     /* 'rawlen' is not the exact summary size; it is only an upper estimation */
0615 
0616     if (ret)
0617         return ret;
0618 
0619     if (alloclen < rawlen) {
0620         /* Doesn't fit untouched. We'll go the old route and split it */
0621         return -EBADFD;
0622     }
0623 
0624     node = kmalloc(rawlen, GFP_KERNEL);
0625     if (!node)
0626         return -ENOMEM;
0627 
0628     ret = jffs2_flash_read(c, ref_offset(raw), rawlen, &retlen, (char *)node);
0629     if (!ret && retlen != rawlen)
0630         ret = -EIO;
0631     if (ret)
0632         goto out_node;
0633 
0634     crc = crc32(0, node, sizeof(struct jffs2_unknown_node)-4);
0635     if (je32_to_cpu(node->u.hdr_crc) != crc) {
0636         pr_warn("Header CRC failed on REF_PRISTINE node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
0637             ref_offset(raw), je32_to_cpu(node->u.hdr_crc), crc);
0638         goto bail;
0639     }
0640 
0641     switch(je16_to_cpu(node->u.nodetype)) {
0642     case JFFS2_NODETYPE_INODE:
0643         crc = crc32(0, node, sizeof(node->i)-8);
0644         if (je32_to_cpu(node->i.node_crc) != crc) {
0645             pr_warn("Node CRC failed on REF_PRISTINE data node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
0646                 ref_offset(raw), je32_to_cpu(node->i.node_crc),
0647                 crc);
0648             goto bail;
0649         }
0650 
0651         if (je32_to_cpu(node->i.dsize)) {
0652             crc = crc32(0, node->i.data, je32_to_cpu(node->i.csize));
0653             if (je32_to_cpu(node->i.data_crc) != crc) {
0654                 pr_warn("Data CRC failed on REF_PRISTINE data node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
0655                     ref_offset(raw),
0656                     je32_to_cpu(node->i.data_crc), crc);
0657                 goto bail;
0658             }
0659         }
0660         break;
0661 
0662     case JFFS2_NODETYPE_DIRENT:
0663         crc = crc32(0, node, sizeof(node->d)-8);
0664         if (je32_to_cpu(node->d.node_crc) != crc) {
0665             pr_warn("Node CRC failed on REF_PRISTINE dirent node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
0666                 ref_offset(raw),
0667                 je32_to_cpu(node->d.node_crc), crc);
0668             goto bail;
0669         }
0670 
0671         if (strnlen(node->d.name, node->d.nsize) != node->d.nsize) {
0672             pr_warn("Name in dirent node at 0x%08x contains zeroes\n",
0673                 ref_offset(raw));
0674             goto bail;
0675         }
0676 
0677         if (node->d.nsize) {
0678             crc = crc32(0, node->d.name, node->d.nsize);
0679             if (je32_to_cpu(node->d.name_crc) != crc) {
0680                 pr_warn("Name CRC failed on REF_PRISTINE dirent node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
0681                     ref_offset(raw),
0682                     je32_to_cpu(node->d.name_crc), crc);
0683                 goto bail;
0684             }
0685         }
0686         break;
0687     default:
0688         /* If it's inode-less, we don't _know_ what it is. Just copy it intact */
0689         if (ic) {
0690             pr_warn("Unknown node type for REF_PRISTINE node at 0x%08x: 0x%04x\n",
0691                 ref_offset(raw), je16_to_cpu(node->u.nodetype));
0692             goto bail;
0693         }
0694     }
0695 
0696     /* OK, all the CRCs are good; this node can just be copied as-is. */
0697  retry:
0698     phys_ofs = write_ofs(c);
0699 
0700     ret = jffs2_flash_write(c, phys_ofs, rawlen, &retlen, (char *)node);
0701 
0702     if (ret || (retlen != rawlen)) {
0703         pr_notice("Write of %d bytes at 0x%08x failed. returned %d, retlen %zd\n",
0704               rawlen, phys_ofs, ret, retlen);
0705         if (retlen) {
0706             jffs2_add_physical_node_ref(c, phys_ofs | REF_OBSOLETE, rawlen, NULL);
0707         } else {
0708             pr_notice("Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n",
0709                   phys_ofs);
0710         }
0711         if (!retried) {
0712             /* Try to reallocate space and retry */
0713             uint32_t dummy;
0714             struct jffs2_eraseblock *jeb = &c->blocks[phys_ofs / c->sector_size];
0715 
0716             retried = 1;
0717 
0718             jffs2_dbg(1, "Retrying failed write of REF_PRISTINE node.\n");
0719 
0720             jffs2_dbg_acct_sanity_check(c,jeb);
0721             jffs2_dbg_acct_paranoia_check(c, jeb);
0722 
0723             ret = jffs2_reserve_space_gc(c, rawlen, &dummy, rawlen);
0724                         /* this is not the exact summary size of it,
0725                             it is only an upper estimation */
0726 
0727             if (!ret) {
0728                 jffs2_dbg(1, "Allocated space at 0x%08x to retry failed write.\n",
0729                       phys_ofs);
0730 
0731                 jffs2_dbg_acct_sanity_check(c,jeb);
0732                 jffs2_dbg_acct_paranoia_check(c, jeb);
0733 
0734                 goto retry;
0735             }
0736             jffs2_dbg(1, "Failed to allocate space to retry failed write: %d!\n",
0737                   ret);
0738         }
0739 
0740         if (!ret)
0741             ret = -EIO;
0742         goto out_node;
0743     }
0744     jffs2_add_physical_node_ref(c, phys_ofs | REF_PRISTINE, rawlen, ic);
0745 
0746     jffs2_mark_node_obsolete(c, raw);
0747     jffs2_dbg(1, "WHEEE! GC REF_PRISTINE node at 0x%08x succeeded\n",
0748           ref_offset(raw));
0749 
0750  out_node:
0751     kfree(node);
0752     return ret;
0753  bail:
0754     ret = -EBADFD;
0755     goto out_node;
0756 }
0757 
0758 static int jffs2_garbage_collect_metadata(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
0759                     struct jffs2_inode_info *f, struct jffs2_full_dnode *fn)
0760 {
0761     struct jffs2_full_dnode *new_fn;
0762     struct jffs2_raw_inode ri;
0763     struct jffs2_node_frag *last_frag;
0764     union jffs2_device_node dev;
0765     char *mdata = NULL;
0766     int mdatalen = 0;
0767     uint32_t alloclen, ilen;
0768     int ret;
0769 
0770     if (S_ISBLK(JFFS2_F_I_MODE(f)) ||
0771         S_ISCHR(JFFS2_F_I_MODE(f)) ) {
0772         /* For these, we don't actually need to read the old node */
0773         mdatalen = jffs2_encode_dev(&dev, JFFS2_F_I_RDEV(f));
0774         mdata = (char *)&dev;
0775         jffs2_dbg(1, "%s(): Writing %d bytes of kdev_t\n",
0776               __func__, mdatalen);
0777     } else if (S_ISLNK(JFFS2_F_I_MODE(f))) {
0778         mdatalen = fn->size;
0779         mdata = kmalloc(fn->size, GFP_KERNEL);
0780         if (!mdata) {
0781             pr_warn("kmalloc of mdata failed in jffs2_garbage_collect_metadata()\n");
0782             return -ENOMEM;
0783         }
0784         ret = jffs2_read_dnode(c, f, fn, mdata, 0, mdatalen);
0785         if (ret) {
0786             pr_warn("read of old metadata failed in jffs2_garbage_collect_metadata(): %d\n",
0787                 ret);
0788             kfree(mdata);
0789             return ret;
0790         }
0791         jffs2_dbg(1, "%s(): Writing %d bites of symlink target\n",
0792               __func__, mdatalen);
0793 
0794     }
0795 
0796     ret = jffs2_reserve_space_gc(c, sizeof(ri) + mdatalen, &alloclen,
0797                 JFFS2_SUMMARY_INODE_SIZE);
0798     if (ret) {
0799         pr_warn("jffs2_reserve_space_gc of %zd bytes for garbage_collect_metadata failed: %d\n",
0800             sizeof(ri) + mdatalen, ret);
0801         goto out;
0802     }
0803 
0804     last_frag = frag_last(&f->fragtree);
0805     if (last_frag)
0806         /* Fetch the inode length from the fragtree rather then
0807          * from i_size since i_size may have not been updated yet */
0808         ilen = last_frag->ofs + last_frag->size;
0809     else
0810         ilen = JFFS2_F_I_SIZE(f);
0811 
0812     memset(&ri, 0, sizeof(ri));
0813     ri.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
0814     ri.nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
0815     ri.totlen = cpu_to_je32(sizeof(ri) + mdatalen);
0816     ri.hdr_crc = cpu_to_je32(crc32(0, &ri, sizeof(struct jffs2_unknown_node)-4));
0817 
0818     ri.ino = cpu_to_je32(f->inocache->ino);
0819     ri.version = cpu_to_je32(++f->highest_version);
0820     ri.mode = cpu_to_jemode(JFFS2_F_I_MODE(f));
0821     ri.uid = cpu_to_je16(JFFS2_F_I_UID(f));
0822     ri.gid = cpu_to_je16(JFFS2_F_I_GID(f));
0823     ri.isize = cpu_to_je32(ilen);
0824     ri.atime = cpu_to_je32(JFFS2_F_I_ATIME(f));
0825     ri.ctime = cpu_to_je32(JFFS2_F_I_CTIME(f));
0826     ri.mtime = cpu_to_je32(JFFS2_F_I_MTIME(f));
0827     ri.offset = cpu_to_je32(0);
0828     ri.csize = cpu_to_je32(mdatalen);
0829     ri.dsize = cpu_to_je32(mdatalen);
0830     ri.compr = JFFS2_COMPR_NONE;
0831     ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8));
0832     ri.data_crc = cpu_to_je32(crc32(0, mdata, mdatalen));
0833 
0834     new_fn = jffs2_write_dnode(c, f, &ri, mdata, mdatalen, ALLOC_GC);
0835 
0836     if (IS_ERR(new_fn)) {
0837         pr_warn("Error writing new dnode: %ld\n", PTR_ERR(new_fn));
0838         ret = PTR_ERR(new_fn);
0839         goto out;
0840     }
0841     jffs2_mark_node_obsolete(c, fn->raw);
0842     jffs2_free_full_dnode(fn);
0843     f->metadata = new_fn;
0844  out:
0845     if (S_ISLNK(JFFS2_F_I_MODE(f)))
0846         kfree(mdata);
0847     return ret;
0848 }
0849 
0850 static int jffs2_garbage_collect_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
0851                     struct jffs2_inode_info *f, struct jffs2_full_dirent *fd)
0852 {
0853     struct jffs2_full_dirent *new_fd;
0854     struct jffs2_raw_dirent rd;
0855     uint32_t alloclen;
0856     int ret;
0857 
0858     rd.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
0859     rd.nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
0860     rd.nsize = strlen(fd->name);
0861     rd.totlen = cpu_to_je32(sizeof(rd) + rd.nsize);
0862     rd.hdr_crc = cpu_to_je32(crc32(0, &rd, sizeof(struct jffs2_unknown_node)-4));
0863 
0864     rd.pino = cpu_to_je32(f->inocache->ino);
0865     rd.version = cpu_to_je32(++f->highest_version);
0866     rd.ino = cpu_to_je32(fd->ino);
0867     /* If the times on this inode were set by explicit utime() they can be different,
0868        so refrain from splatting them. */
0869     if (JFFS2_F_I_MTIME(f) == JFFS2_F_I_CTIME(f))
0870         rd.mctime = cpu_to_je32(JFFS2_F_I_MTIME(f));
0871     else
0872         rd.mctime = cpu_to_je32(0);
0873     rd.type = fd->type;
0874     rd.node_crc = cpu_to_je32(crc32(0, &rd, sizeof(rd)-8));
0875     rd.name_crc = cpu_to_je32(crc32(0, fd->name, rd.nsize));
0876 
0877     ret = jffs2_reserve_space_gc(c, sizeof(rd)+rd.nsize, &alloclen,
0878                 JFFS2_SUMMARY_DIRENT_SIZE(rd.nsize));
0879     if (ret) {
0880         pr_warn("jffs2_reserve_space_gc of %zd bytes for garbage_collect_dirent failed: %d\n",
0881             sizeof(rd)+rd.nsize, ret);
0882         return ret;
0883     }
0884     new_fd = jffs2_write_dirent(c, f, &rd, fd->name, rd.nsize, ALLOC_GC);
0885 
0886     if (IS_ERR(new_fd)) {
0887         pr_warn("jffs2_write_dirent in garbage_collect_dirent failed: %ld\n",
0888             PTR_ERR(new_fd));
0889         return PTR_ERR(new_fd);
0890     }
0891     jffs2_add_fd_to_list(c, new_fd, &f->dents);
0892     return 0;
0893 }
0894 
0895 static int jffs2_garbage_collect_deletion_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
0896                     struct jffs2_inode_info *f, struct jffs2_full_dirent *fd)
0897 {
0898     struct jffs2_full_dirent **fdp = &f->dents;
0899     int found = 0;
0900 
0901     /* On a medium where we can't actually mark nodes obsolete
0902        pernamently, such as NAND flash, we need to work out
0903        whether this deletion dirent is still needed to actively
0904        delete a 'real' dirent with the same name that's still
0905        somewhere else on the flash. */
0906     if (!jffs2_can_mark_obsolete(c)) {
0907         struct jffs2_raw_dirent *rd;
0908         struct jffs2_raw_node_ref *raw;
0909         int ret;
0910         size_t retlen;
0911         int name_len = strlen(fd->name);
0912         uint32_t name_crc = crc32(0, fd->name, name_len);
0913         uint32_t rawlen = ref_totlen(c, jeb, fd->raw);
0914 
0915         rd = kmalloc(rawlen, GFP_KERNEL);
0916         if (!rd)
0917             return -ENOMEM;
0918 
0919         /* Prevent the erase code from nicking the obsolete node refs while
0920            we're looking at them. I really don't like this extra lock but
0921            can't see any alternative. Suggestions on a postcard to... */
0922         mutex_lock(&c->erase_free_sem);
0923 
0924         for (raw = f->inocache->nodes; raw != (void *)f->inocache; raw = raw->next_in_ino) {
0925 
0926             cond_resched();
0927 
0928             /* We only care about obsolete ones */
0929             if (!(ref_obsolete(raw)))
0930                 continue;
0931 
0932             /* Any dirent with the same name is going to have the same length... */
0933             if (ref_totlen(c, NULL, raw) != rawlen)
0934                 continue;
0935 
0936             /* Doesn't matter if there's one in the same erase block. We're going to
0937                delete it too at the same time. */
0938             if (SECTOR_ADDR(raw->flash_offset) == SECTOR_ADDR(fd->raw->flash_offset))
0939                 continue;
0940 
0941             jffs2_dbg(1, "Check potential deletion dirent at %08x\n",
0942                   ref_offset(raw));
0943 
0944             /* This is an obsolete node belonging to the same directory, and it's of the right
0945                length. We need to take a closer look...*/
0946             ret = jffs2_flash_read(c, ref_offset(raw), rawlen, &retlen, (char *)rd);
0947             if (ret) {
0948                 pr_warn("%s(): Read error (%d) reading obsolete node at %08x\n",
0949                     __func__, ret, ref_offset(raw));
0950                 /* If we can't read it, we don't need to continue to obsolete it. Continue */
0951                 continue;
0952             }
0953             if (retlen != rawlen) {
0954                 pr_warn("%s(): Short read (%zd not %u) reading header from obsolete node at %08x\n",
0955                     __func__, retlen, rawlen,
0956                     ref_offset(raw));
0957                 continue;
0958             }
0959 
0960             if (je16_to_cpu(rd->nodetype) != JFFS2_NODETYPE_DIRENT)
0961                 continue;
0962 
0963             /* If the name CRC doesn't match, skip */
0964             if (je32_to_cpu(rd->name_crc) != name_crc)
0965                 continue;
0966 
0967             /* If the name length doesn't match, or it's another deletion dirent, skip */
0968             if (rd->nsize != name_len || !je32_to_cpu(rd->ino))
0969                 continue;
0970 
0971             /* OK, check the actual name now */
0972             if (memcmp(rd->name, fd->name, name_len))
0973                 continue;
0974 
0975             /* OK. The name really does match. There really is still an older node on
0976                the flash which our deletion dirent obsoletes. So we have to write out
0977                a new deletion dirent to replace it */
0978             mutex_unlock(&c->erase_free_sem);
0979 
0980             jffs2_dbg(1, "Deletion dirent at %08x still obsoletes real dirent \"%s\" at %08x for ino #%u\n",
0981                   ref_offset(fd->raw), fd->name,
0982                   ref_offset(raw), je32_to_cpu(rd->ino));
0983             kfree(rd);
0984 
0985             return jffs2_garbage_collect_dirent(c, jeb, f, fd);
0986         }
0987 
0988         mutex_unlock(&c->erase_free_sem);
0989         kfree(rd);
0990     }
0991 
0992     /* FIXME: If we're deleting a dirent which contains the current mtime and ctime,
0993        we should update the metadata node with those times accordingly */
0994 
0995     /* No need for it any more. Just mark it obsolete and remove it from the list */
0996     while (*fdp) {
0997         if ((*fdp) == fd) {
0998             found = 1;
0999             *fdp = fd->next;
1000             break;
1001         }
1002         fdp = &(*fdp)->next;
1003     }
1004     if (!found) {
1005         pr_warn("Deletion dirent \"%s\" not found in list for ino #%u\n",
1006             fd->name, f->inocache->ino);
1007     }
1008     jffs2_mark_node_obsolete(c, fd->raw);
1009     jffs2_free_full_dirent(fd);
1010     return 0;
1011 }
1012 
1013 static int jffs2_garbage_collect_hole(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
1014                       struct jffs2_inode_info *f, struct jffs2_full_dnode *fn,
1015                       uint32_t start, uint32_t end)
1016 {
1017     struct jffs2_raw_inode ri;
1018     struct jffs2_node_frag *frag;
1019     struct jffs2_full_dnode *new_fn;
1020     uint32_t alloclen, ilen;
1021     int ret;
1022 
1023     jffs2_dbg(1, "Writing replacement hole node for ino #%u from offset 0x%x to 0x%x\n",
1024           f->inocache->ino, start, end);
1025 
1026     memset(&ri, 0, sizeof(ri));
1027 
1028     if(fn->frags > 1) {
1029         size_t readlen;
1030         uint32_t crc;
1031         /* It's partially obsoleted by a later write. So we have to
1032            write it out again with the _same_ version as before */
1033         ret = jffs2_flash_read(c, ref_offset(fn->raw), sizeof(ri), &readlen, (char *)&ri);
1034         if (readlen != sizeof(ri) || ret) {
1035             pr_warn("Node read failed in jffs2_garbage_collect_hole. Ret %d, retlen %zd. Data will be lost by writing new hole node\n",
1036                 ret, readlen);
1037             goto fill;
1038         }
1039         if (je16_to_cpu(ri.nodetype) != JFFS2_NODETYPE_INODE) {
1040             pr_warn("%s(): Node at 0x%08x had node type 0x%04x instead of JFFS2_NODETYPE_INODE(0x%04x)\n",
1041                 __func__, ref_offset(fn->raw),
1042                 je16_to_cpu(ri.nodetype), JFFS2_NODETYPE_INODE);
1043             return -EIO;
1044         }
1045         if (je32_to_cpu(ri.totlen) != sizeof(ri)) {
1046             pr_warn("%s(): Node at 0x%08x had totlen 0x%x instead of expected 0x%zx\n",
1047                 __func__, ref_offset(fn->raw),
1048                 je32_to_cpu(ri.totlen), sizeof(ri));
1049             return -EIO;
1050         }
1051         crc = crc32(0, &ri, sizeof(ri)-8);
1052         if (crc != je32_to_cpu(ri.node_crc)) {
1053             pr_warn("%s: Node at 0x%08x had CRC 0x%08x which doesn't match calculated CRC 0x%08x\n",
1054                 __func__, ref_offset(fn->raw),
1055                 je32_to_cpu(ri.node_crc), crc);
1056             /* FIXME: We could possibly deal with this by writing new holes for each frag */
1057             pr_warn("Data in the range 0x%08x to 0x%08x of inode #%u will be lost\n",
1058                 start, end, f->inocache->ino);
1059             goto fill;
1060         }
1061         if (ri.compr != JFFS2_COMPR_ZERO) {
1062             pr_warn("%s(): Node 0x%08x wasn't a hole node!\n",
1063                 __func__, ref_offset(fn->raw));
1064             pr_warn("Data in the range 0x%08x to 0x%08x of inode #%u will be lost\n",
1065                 start, end, f->inocache->ino);
1066             goto fill;
1067         }
1068     } else {
1069     fill:
1070         ri.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
1071         ri.nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
1072         ri.totlen = cpu_to_je32(sizeof(ri));
1073         ri.hdr_crc = cpu_to_je32(crc32(0, &ri, sizeof(struct jffs2_unknown_node)-4));
1074 
1075         ri.ino = cpu_to_je32(f->inocache->ino);
1076         ri.version = cpu_to_je32(++f->highest_version);
1077         ri.offset = cpu_to_je32(start);
1078         ri.dsize = cpu_to_je32(end - start);
1079         ri.csize = cpu_to_je32(0);
1080         ri.compr = JFFS2_COMPR_ZERO;
1081     }
1082 
1083     frag = frag_last(&f->fragtree);
1084     if (frag)
1085         /* Fetch the inode length from the fragtree rather then
1086          * from i_size since i_size may have not been updated yet */
1087         ilen = frag->ofs + frag->size;
1088     else
1089         ilen = JFFS2_F_I_SIZE(f);
1090 
1091     ri.mode = cpu_to_jemode(JFFS2_F_I_MODE(f));
1092     ri.uid = cpu_to_je16(JFFS2_F_I_UID(f));
1093     ri.gid = cpu_to_je16(JFFS2_F_I_GID(f));
1094     ri.isize = cpu_to_je32(ilen);
1095     ri.atime = cpu_to_je32(JFFS2_F_I_ATIME(f));
1096     ri.ctime = cpu_to_je32(JFFS2_F_I_CTIME(f));
1097     ri.mtime = cpu_to_je32(JFFS2_F_I_MTIME(f));
1098     ri.data_crc = cpu_to_je32(0);
1099     ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8));
1100 
1101     ret = jffs2_reserve_space_gc(c, sizeof(ri), &alloclen,
1102                      JFFS2_SUMMARY_INODE_SIZE);
1103     if (ret) {
1104         pr_warn("jffs2_reserve_space_gc of %zd bytes for garbage_collect_hole failed: %d\n",
1105             sizeof(ri), ret);
1106         return ret;
1107     }
1108     new_fn = jffs2_write_dnode(c, f, &ri, NULL, 0, ALLOC_GC);
1109 
1110     if (IS_ERR(new_fn)) {
1111         pr_warn("Error writing new hole node: %ld\n", PTR_ERR(new_fn));
1112         return PTR_ERR(new_fn);
1113     }
1114     if (je32_to_cpu(ri.version) == f->highest_version) {
1115         jffs2_add_full_dnode_to_inode(c, f, new_fn);
1116         if (f->metadata) {
1117             jffs2_mark_node_obsolete(c, f->metadata->raw);
1118             jffs2_free_full_dnode(f->metadata);
1119             f->metadata = NULL;
1120         }
1121         return 0;
1122     }
1123 
1124     /*
1125      * We should only get here in the case where the node we are
1126      * replacing had more than one frag, so we kept the same version
1127      * number as before. (Except in case of error -- see 'goto fill;'
1128      * above.)
1129      */
1130     D1(if(unlikely(fn->frags <= 1)) {
1131             pr_warn("%s(): Replacing fn with %d frag(s) but new ver %d != highest_version %d of ino #%d\n",
1132                 __func__, fn->frags, je32_to_cpu(ri.version),
1133                 f->highest_version, je32_to_cpu(ri.ino));
1134     });
1135 
1136     /* This is a partially-overlapped hole node. Mark it REF_NORMAL not REF_PRISTINE */
1137     mark_ref_normal(new_fn->raw);
1138 
1139     for (frag = jffs2_lookup_node_frag(&f->fragtree, fn->ofs);
1140          frag; frag = frag_next(frag)) {
1141         if (frag->ofs > fn->size + fn->ofs)
1142             break;
1143         if (frag->node == fn) {
1144             frag->node = new_fn;
1145             new_fn->frags++;
1146             fn->frags--;
1147         }
1148     }
1149     if (fn->frags) {
1150         pr_warn("%s(): Old node still has frags!\n", __func__);
1151         BUG();
1152     }
1153     if (!new_fn->frags) {
1154         pr_warn("%s(): New node has no frags!\n", __func__);
1155         BUG();
1156     }
1157 
1158     jffs2_mark_node_obsolete(c, fn->raw);
1159     jffs2_free_full_dnode(fn);
1160 
1161     return 0;
1162 }
1163 
1164 static int jffs2_garbage_collect_dnode(struct jffs2_sb_info *c, struct jffs2_eraseblock *orig_jeb,
1165                        struct jffs2_inode_info *f, struct jffs2_full_dnode *fn,
1166                        uint32_t start, uint32_t end)
1167 {
1168     struct inode *inode = OFNI_EDONI_2SFFJ(f);
1169     struct jffs2_full_dnode *new_fn;
1170     struct jffs2_raw_inode ri;
1171     uint32_t alloclen, offset, orig_end, orig_start;
1172     int ret = 0;
1173     unsigned char *comprbuf = NULL, *writebuf;
1174     struct page *page;
1175     unsigned char *pg_ptr;
1176 
1177     memset(&ri, 0, sizeof(ri));
1178 
1179     jffs2_dbg(1, "Writing replacement dnode for ino #%u from offset 0x%x to 0x%x\n",
1180           f->inocache->ino, start, end);
1181 
1182     orig_end = end;
1183     orig_start = start;
1184 
1185     if (c->nr_free_blocks + c->nr_erasing_blocks > c->resv_blocks_gcmerge) {
1186         /* Attempt to do some merging. But only expand to cover logically
1187            adjacent frags if the block containing them is already considered
1188            to be dirty. Otherwise we end up with GC just going round in
1189            circles dirtying the nodes it already wrote out, especially
1190            on NAND where we have small eraseblocks and hence a much higher
1191            chance of nodes having to be split to cross boundaries. */
1192 
1193         struct jffs2_node_frag *frag;
1194         uint32_t min, max;
1195 
1196         min = start & ~(PAGE_SIZE-1);
1197         max = min + PAGE_SIZE;
1198 
1199         frag = jffs2_lookup_node_frag(&f->fragtree, start);
1200 
1201         /* BUG_ON(!frag) but that'll happen anyway... */
1202 
1203         BUG_ON(frag->ofs != start);
1204 
1205         /* First grow down... */
1206         while((frag = frag_prev(frag)) && frag->ofs >= min) {
1207 
1208             /* If the previous frag doesn't even reach the beginning, there's
1209                excessive fragmentation. Just merge. */
1210             if (frag->ofs > min) {
1211                 jffs2_dbg(1, "Expanding down to cover partial frag (0x%x-0x%x)\n",
1212                       frag->ofs, frag->ofs+frag->size);
1213                 start = frag->ofs;
1214                 continue;
1215             }
1216             /* OK. This frag holds the first byte of the page. */
1217             if (!frag->node || !frag->node->raw) {
1218                 jffs2_dbg(1, "First frag in page is hole (0x%x-0x%x). Not expanding down.\n",
1219                       frag->ofs, frag->ofs+frag->size);
1220                 break;
1221             } else {
1222 
1223                 /* OK, it's a frag which extends to the beginning of the page. Does it live
1224                    in a block which is still considered clean? If so, don't obsolete it.
1225                    If not, cover it anyway. */
1226 
1227                 struct jffs2_raw_node_ref *raw = frag->node->raw;
1228                 struct jffs2_eraseblock *jeb;
1229 
1230                 jeb = &c->blocks[raw->flash_offset / c->sector_size];
1231 
1232                 if (jeb == c->gcblock) {
1233                     jffs2_dbg(1, "Expanding down to cover frag (0x%x-0x%x) in gcblock at %08x\n",
1234                           frag->ofs,
1235                           frag->ofs + frag->size,
1236                           ref_offset(raw));
1237                     start = frag->ofs;
1238                     break;
1239                 }
1240                 if (!ISDIRTY(jeb->dirty_size + jeb->wasted_size)) {
1241                     jffs2_dbg(1, "Not expanding down to cover frag (0x%x-0x%x) in clean block %08x\n",
1242                           frag->ofs,
1243                           frag->ofs + frag->size,
1244                           jeb->offset);
1245                     break;
1246                 }
1247 
1248                 jffs2_dbg(1, "Expanding down to cover frag (0x%x-0x%x) in dirty block %08x\n",
1249                       frag->ofs,
1250                       frag->ofs + frag->size,
1251                       jeb->offset);
1252                 start = frag->ofs;
1253                 break;
1254             }
1255         }
1256 
1257         /* ... then up */
1258 
1259         /* Find last frag which is actually part of the node we're to GC. */
1260         frag = jffs2_lookup_node_frag(&f->fragtree, end-1);
1261 
1262         while((frag = frag_next(frag)) && frag->ofs+frag->size <= max) {
1263 
1264             /* If the previous frag doesn't even reach the beginning, there's lots
1265                of fragmentation. Just merge. */
1266             if (frag->ofs+frag->size < max) {
1267                 jffs2_dbg(1, "Expanding up to cover partial frag (0x%x-0x%x)\n",
1268                       frag->ofs, frag->ofs+frag->size);
1269                 end = frag->ofs + frag->size;
1270                 continue;
1271             }
1272 
1273             if (!frag->node || !frag->node->raw) {
1274                 jffs2_dbg(1, "Last frag in page is hole (0x%x-0x%x). Not expanding up.\n",
1275                       frag->ofs, frag->ofs+frag->size);
1276                 break;
1277             } else {
1278 
1279                 /* OK, it's a frag which extends to the beginning of the page. Does it live
1280                    in a block which is still considered clean? If so, don't obsolete it.
1281                    If not, cover it anyway. */
1282 
1283                 struct jffs2_raw_node_ref *raw = frag->node->raw;
1284                 struct jffs2_eraseblock *jeb;
1285 
1286                 jeb = &c->blocks[raw->flash_offset / c->sector_size];
1287 
1288                 if (jeb == c->gcblock) {
1289                     jffs2_dbg(1, "Expanding up to cover frag (0x%x-0x%x) in gcblock at %08x\n",
1290                           frag->ofs,
1291                           frag->ofs + frag->size,
1292                           ref_offset(raw));
1293                     end = frag->ofs + frag->size;
1294                     break;
1295                 }
1296                 if (!ISDIRTY(jeb->dirty_size + jeb->wasted_size)) {
1297                     jffs2_dbg(1, "Not expanding up to cover frag (0x%x-0x%x) in clean block %08x\n",
1298                           frag->ofs,
1299                           frag->ofs + frag->size,
1300                           jeb->offset);
1301                     break;
1302                 }
1303 
1304                 jffs2_dbg(1, "Expanding up to cover frag (0x%x-0x%x) in dirty block %08x\n",
1305                       frag->ofs,
1306                       frag->ofs + frag->size,
1307                       jeb->offset);
1308                 end = frag->ofs + frag->size;
1309                 break;
1310             }
1311         }
1312         jffs2_dbg(1, "Expanded dnode to write from (0x%x-0x%x) to (0x%x-0x%x)\n",
1313               orig_start, orig_end, start, end);
1314 
1315         D1(BUG_ON(end > frag_last(&f->fragtree)->ofs + frag_last(&f->fragtree)->size));
1316         BUG_ON(end < orig_end);
1317         BUG_ON(start > orig_start);
1318     }
1319 
1320     /* The rules state that we must obtain the page lock *before* f->sem, so
1321      * drop f->sem temporarily. Since we also hold c->alloc_sem, nothing's
1322      * actually going to *change* so we're safe; we only allow reading.
1323      *
1324      * It is important to note that jffs2_write_begin() will ensure that its
1325      * page is marked Uptodate before allocating space. That means that if we
1326      * end up here trying to GC the *same* page that jffs2_write_begin() is
1327      * trying to write out, read_cache_page() will not deadlock. */
1328     mutex_unlock(&f->sem);
1329     page = read_cache_page(inode->i_mapping, start >> PAGE_SHIFT,
1330                    __jffs2_read_folio, NULL);
1331     if (IS_ERR(page)) {
1332         pr_warn("read_cache_page() returned error: %ld\n",
1333             PTR_ERR(page));
1334         mutex_lock(&f->sem);
1335         return PTR_ERR(page);
1336     }
1337 
1338     pg_ptr = kmap(page);
1339     mutex_lock(&f->sem);
1340 
1341     offset = start;
1342     while(offset < orig_end) {
1343         uint32_t datalen;
1344         uint32_t cdatalen;
1345         uint16_t comprtype = JFFS2_COMPR_NONE;
1346 
1347         ret = jffs2_reserve_space_gc(c, sizeof(ri) + JFFS2_MIN_DATA_LEN,
1348                     &alloclen, JFFS2_SUMMARY_INODE_SIZE);
1349 
1350         if (ret) {
1351             pr_warn("jffs2_reserve_space_gc of %zd bytes for garbage_collect_dnode failed: %d\n",
1352                 sizeof(ri) + JFFS2_MIN_DATA_LEN, ret);
1353             break;
1354         }
1355         cdatalen = min_t(uint32_t, alloclen - sizeof(ri), end - offset);
1356         datalen = end - offset;
1357 
1358         writebuf = pg_ptr + (offset & (PAGE_SIZE -1));
1359 
1360         comprtype = jffs2_compress(c, f, writebuf, &comprbuf, &datalen, &cdatalen);
1361 
1362         ri.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
1363         ri.nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
1364         ri.totlen = cpu_to_je32(sizeof(ri) + cdatalen);
1365         ri.hdr_crc = cpu_to_je32(crc32(0, &ri, sizeof(struct jffs2_unknown_node)-4));
1366 
1367         ri.ino = cpu_to_je32(f->inocache->ino);
1368         ri.version = cpu_to_je32(++f->highest_version);
1369         ri.mode = cpu_to_jemode(JFFS2_F_I_MODE(f));
1370         ri.uid = cpu_to_je16(JFFS2_F_I_UID(f));
1371         ri.gid = cpu_to_je16(JFFS2_F_I_GID(f));
1372         ri.isize = cpu_to_je32(JFFS2_F_I_SIZE(f));
1373         ri.atime = cpu_to_je32(JFFS2_F_I_ATIME(f));
1374         ri.ctime = cpu_to_je32(JFFS2_F_I_CTIME(f));
1375         ri.mtime = cpu_to_je32(JFFS2_F_I_MTIME(f));
1376         ri.offset = cpu_to_je32(offset);
1377         ri.csize = cpu_to_je32(cdatalen);
1378         ri.dsize = cpu_to_je32(datalen);
1379         ri.compr = comprtype & 0xff;
1380         ri.usercompr = (comprtype >> 8) & 0xff;
1381         ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8));
1382         ri.data_crc = cpu_to_je32(crc32(0, comprbuf, cdatalen));
1383 
1384         new_fn = jffs2_write_dnode(c, f, &ri, comprbuf, cdatalen, ALLOC_GC);
1385 
1386         jffs2_free_comprbuf(comprbuf, writebuf);
1387 
1388         if (IS_ERR(new_fn)) {
1389             pr_warn("Error writing new dnode: %ld\n",
1390                 PTR_ERR(new_fn));
1391             ret = PTR_ERR(new_fn);
1392             break;
1393         }
1394         ret = jffs2_add_full_dnode_to_inode(c, f, new_fn);
1395         offset += datalen;
1396         if (f->metadata) {
1397             jffs2_mark_node_obsolete(c, f->metadata->raw);
1398             jffs2_free_full_dnode(f->metadata);
1399             f->metadata = NULL;
1400         }
1401     }
1402 
1403     kunmap(page);
1404     put_page(page);
1405     return ret;
1406 }