Revert "f2fs: resync with upstream 3.4 branch (forced update)"

This reverts commit 6a1be380100e402aacca6a0a3ac829c059768d79.
diff --git a/fs/f2fs/checkpoint.c b/fs/f2fs/checkpoint.c
index 6aeed5b..0b4710c 100644
--- a/fs/f2fs/checkpoint.c
+++ b/fs/f2fs/checkpoint.c
@@ -22,7 +22,7 @@
 #include "segment.h"
 #include <trace/events/f2fs.h>
 
-static struct kmem_cache *ino_entry_slab;
+static struct kmem_cache *orphan_entry_slab;
 static struct kmem_cache *inode_entry_slab;
 
 /*
@@ -282,120 +282,72 @@
 	.set_page_dirty	= f2fs_set_meta_page_dirty,
 };
 
-static void __add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
-{
-	struct ino_entry *e;
-retry:
-	spin_lock(&sbi->ino_lock[type]);
-
-	e = radix_tree_lookup(&sbi->ino_root[type], ino);
-	if (!e) {
-		e = kmem_cache_alloc(ino_entry_slab, GFP_ATOMIC);
-		if (!e) {
-			spin_unlock(&sbi->ino_lock[type]);
-			goto retry;
-		}
-		if (radix_tree_insert(&sbi->ino_root[type], ino, e)) {
-			spin_unlock(&sbi->ino_lock[type]);
-			kmem_cache_free(ino_entry_slab, e);
-			goto retry;
-		}
-		memset(e, 0, sizeof(struct ino_entry));
-		e->ino = ino;
-
-		list_add_tail(&e->list, &sbi->ino_list[type]);
-	}
-	spin_unlock(&sbi->ino_lock[type]);
-}
-
-static void __remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
-{
-	struct ino_entry *e;
-
-	spin_lock(&sbi->ino_lock[type]);
-	e = radix_tree_lookup(&sbi->ino_root[type], ino);
-	if (e) {
-		list_del(&e->list);
-		radix_tree_delete(&sbi->ino_root[type], ino);
-		if (type == ORPHAN_INO)
-			sbi->n_orphans--;
-		spin_unlock(&sbi->ino_lock[type]);
-		kmem_cache_free(ino_entry_slab, e);
-		return;
-	}
-	spin_unlock(&sbi->ino_lock[type]);
-}
-
-void add_dirty_inode(struct f2fs_sb_info *sbi, nid_t ino, int type)
-{
-	/* add new dirty ino entry into list */
-	__add_ino_entry(sbi, ino, type);
-}
-
-void remove_dirty_inode(struct f2fs_sb_info *sbi, nid_t ino, int type)
-{
-	/* remove dirty ino entry from list */
-	__remove_ino_entry(sbi, ino, type);
-}
-
-/* mode should be APPEND_INO or UPDATE_INO */
-bool exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode)
-{
-	struct ino_entry *e;
-	spin_lock(&sbi->ino_lock[mode]);
-	e = radix_tree_lookup(&sbi->ino_root[mode], ino);
-	spin_unlock(&sbi->ino_lock[mode]);
-	return e ? true : false;
-}
-
-static void release_dirty_inode(struct f2fs_sb_info *sbi)
-{
-	struct ino_entry *e, *tmp;
-	int i;
-
-	for (i = APPEND_INO; i <= UPDATE_INO; i++) {
-		spin_lock(&sbi->ino_lock[i]);
-		list_for_each_entry_safe(e, tmp, &sbi->ino_list[i], list) {
-			list_del(&e->list);
-			radix_tree_delete(&sbi->ino_root[i], e->ino);
-			kmem_cache_free(ino_entry_slab, e);
-		}
-		spin_unlock(&sbi->ino_lock[i]);
-	}
-}
-
 int acquire_orphan_inode(struct f2fs_sb_info *sbi)
 {
 	int err = 0;
 
-	spin_lock(&sbi->ino_lock[ORPHAN_INO]);
+	spin_lock(&sbi->orphan_inode_lock);
 	if (unlikely(sbi->n_orphans >= sbi->max_orphans))
 		err = -ENOSPC;
 	else
 		sbi->n_orphans++;
-	spin_unlock(&sbi->ino_lock[ORPHAN_INO]);
+	spin_unlock(&sbi->orphan_inode_lock);
 
 	return err;
 }
 
 void release_orphan_inode(struct f2fs_sb_info *sbi)
 {
-	spin_lock(&sbi->ino_lock[ORPHAN_INO]);
+	spin_lock(&sbi->orphan_inode_lock);
 	f2fs_bug_on(sbi->n_orphans == 0);
 	sbi->n_orphans--;
-	spin_unlock(&sbi->ino_lock[ORPHAN_INO]);
+	spin_unlock(&sbi->orphan_inode_lock);
 }
 
 void add_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
 {
-	/* add new orphan ino entry into list */
-	__add_ino_entry(sbi, ino, ORPHAN_INO);
+	struct list_head *head;
+	struct orphan_inode_entry *new, *orphan;
+
+	new = f2fs_kmem_cache_alloc(orphan_entry_slab, GFP_ATOMIC);
+	new->ino = ino;
+
+	spin_lock(&sbi->orphan_inode_lock);
+	head = &sbi->orphan_inode_list;
+	list_for_each_entry(orphan, head, list) {
+		if (orphan->ino == ino) {
+			spin_unlock(&sbi->orphan_inode_lock);
+			kmem_cache_free(orphan_entry_slab, new);
+			return;
+		}
+
+		if (orphan->ino > ino)
+			break;
+	}
+
+	/* add new orphan entry into list which is sorted by inode number */
+	list_add_tail(&new->list, &orphan->list);
+	spin_unlock(&sbi->orphan_inode_lock);
 }
 
 void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
 {
-	/* remove orphan entry from orphan list */
-	__remove_ino_entry(sbi, ino, ORPHAN_INO);
+	struct list_head *head;
+	struct orphan_inode_entry *orphan;
+
+	spin_lock(&sbi->orphan_inode_lock);
+	head = &sbi->orphan_inode_list;
+	list_for_each_entry(orphan, head, list) {
+		if (orphan->ino == ino) {
+			list_del(&orphan->list);
+			f2fs_bug_on(sbi->n_orphans == 0);
+			sbi->n_orphans--;
+			spin_unlock(&sbi->orphan_inode_lock);
+			kmem_cache_free(orphan_entry_slab, orphan);
+			return;
+		}
+	}
+	spin_unlock(&sbi->orphan_inode_lock);
 }
 
 static void recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
@@ -449,14 +401,14 @@
 	unsigned short orphan_blocks = (unsigned short)((sbi->n_orphans +
 		(F2FS_ORPHANS_PER_BLOCK - 1)) / F2FS_ORPHANS_PER_BLOCK);
 	struct page *page = NULL;
-	struct ino_entry *orphan = NULL;
+	struct orphan_inode_entry *orphan = NULL;
 
 	for (index = 0; index < orphan_blocks; index++)
 		grab_meta_page(sbi, start_blk + index);
 
 	index = 1;
-	spin_lock(&sbi->ino_lock[ORPHAN_INO]);
-	head = &sbi->ino_list[ORPHAN_INO];
+	spin_lock(&sbi->orphan_inode_lock);
+	head = &sbi->orphan_inode_list;
 
 	/* loop for each orphan inode entry and write them in Jornal block */
 	list_for_each_entry(orphan, head, list) {
@@ -496,7 +448,7 @@
 		f2fs_put_page(page, 1);
 	}
 
-	spin_unlock(&sbi->ino_lock[ORPHAN_INO]);
+	spin_unlock(&sbi->orphan_inode_lock);
 }
 
 static struct page *validate_checkpoint(struct f2fs_sb_info *sbi,
@@ -762,10 +714,10 @@
 	 * until finishing nat/sit flush.
 	 */
 retry_flush_nodes:
-	down_write(&sbi->node_write);
+	mutex_lock(&sbi->node_write);
 
 	if (get_pages(sbi, F2FS_DIRTY_NODES)) {
-		up_write(&sbi->node_write);
+		mutex_unlock(&sbi->node_write);
 		sync_node_pages(sbi, 0, &wbc);
 		goto retry_flush_nodes;
 	}
@@ -774,7 +726,7 @@
 
 static void unblock_operations(struct f2fs_sb_info *sbi)
 {
-	up_write(&sbi->node_write);
+	mutex_unlock(&sbi->node_write);
 	f2fs_unlock_all(sbi);
 }
 
@@ -796,7 +748,6 @@
 static void do_checkpoint(struct f2fs_sb_info *sbi, bool is_umount)
 {
 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
-	struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
 	nid_t last_nid = 0;
 	block_t start_blk;
 	struct page *cp_page;
@@ -810,7 +761,7 @@
 	 * This avoids to conduct wrong roll-forward operations and uses
 	 * metapages, so should be called prior to sync_meta_pages below.
 	 */
-	discard_next_dnode(sbi, NEXT_FREE_BLKADDR(sbi, curseg));
+	discard_next_dnode(sbi);
 
 	/* Flush all the NAT/SIT pages */
 	while (get_pages(sbi, F2FS_DIRTY_META))
@@ -934,9 +885,8 @@
 	/* Here, we only have one bio having CP pack */
 	sync_meta_pages(sbi, META_FLUSH, LONG_MAX);
 
-	if (!is_set_ckpt_flags(ckpt, CP_ERROR_FLAG)) {
+	if (unlikely(!is_set_ckpt_flags(ckpt, CP_ERROR_FLAG))) {
 		clear_prefree_segments(sbi);
-		release_dirty_inode(sbi);
 		F2FS_RESET_SB_DIRT(sbi);
 	}
 }
@@ -982,37 +932,31 @@
 	trace_f2fs_write_checkpoint(sbi->sb, is_umount, "finish checkpoint");
 }
 
-void init_ino_entry_info(struct f2fs_sb_info *sbi)
+void init_orphan_info(struct f2fs_sb_info *sbi)
 {
-	int i;
-
-	for (i = 0; i < MAX_INO_ENTRY; i++) {
-		INIT_RADIX_TREE(&sbi->ino_root[i], GFP_ATOMIC);
-		spin_lock_init(&sbi->ino_lock[i]);
-		INIT_LIST_HEAD(&sbi->ino_list[i]);
-	}
-
+	spin_lock_init(&sbi->orphan_inode_lock);
+	INIT_LIST_HEAD(&sbi->orphan_inode_list);
+	sbi->n_orphans = 0;
 	/*
 	 * considering 512 blocks in a segment 8 blocks are needed for cp
 	 * and log segment summaries. Remaining blocks are used to keep
 	 * orphan entries with the limitation one reserved segment
 	 * for cp pack we can have max 1020*504 orphan entries
 	 */
-	sbi->n_orphans = 0;
 	sbi->max_orphans = (sbi->blocks_per_seg - 2 - NR_CURSEG_TYPE)
 				* F2FS_ORPHANS_PER_BLOCK;
 }
 
 int __init create_checkpoint_caches(void)
 {
-	ino_entry_slab = f2fs_kmem_cache_create("f2fs_ino_entry",
-			sizeof(struct ino_entry));
-	if (!ino_entry_slab)
+	orphan_entry_slab = f2fs_kmem_cache_create("f2fs_orphan_entry",
+			sizeof(struct orphan_inode_entry));
+	if (!orphan_entry_slab)
 		return -ENOMEM;
 	inode_entry_slab = f2fs_kmem_cache_create("f2fs_dirty_dir_entry",
 			sizeof(struct dir_inode_entry));
 	if (!inode_entry_slab) {
-		kmem_cache_destroy(ino_entry_slab);
+		kmem_cache_destroy(orphan_entry_slab);
 		return -ENOMEM;
 	}
 	return 0;
@@ -1020,6 +964,6 @@
 
 void destroy_checkpoint_caches(void)
 {
-	kmem_cache_destroy(ino_entry_slab);
+	kmem_cache_destroy(orphan_entry_slab);
 	kmem_cache_destroy(inode_entry_slab);
 }