aboutsummaryrefslogtreecommitdiffstats
path: root/fs/f2fs
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2013-01-03 14:41:43 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2013-01-03 14:41:43 -0500
commit2318aa272072f6906de8e00a332da1485506b3c5 (patch)
treea305711c90dab041e1c53a00403c90f06989b56b /fs/f2fs
parented4e6a94d3053b9900b4a1338b8056d532a564c4 (diff)
parent9836b8b9499cb25ea32cad9fff640eef874c5431 (diff)
Merge tag 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jaegeuk/f2fs
Pull f2fs bug fixes from Jaegeuk Kim: "This patch-set includes two major bug fixes: - incorrect IUsed provided by *df -i*, and - lookup failure of parent inodes in corner cases. [Other Bug Fixes] - Fix error handling routines - Trigger recovery process correctly - Resolve build failures due to missing header files [Etc] - Add a MAINTAINERS entry for f2fs - Fix and clean up variables, functions, and equations - Avoid warnings during compilation" * tag 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jaegeuk/f2fs: f2fs: unify string length declarations and usage f2fs: clean up unused variables and return values f2fs: clean up the start_bidx_of_node function f2fs: remove unneeded variable from f2fs_sync_fs f2fs: fix fsync_inode list addition logic and avoid invalid access to memory f2fs: remove unneeded initialization of nr_dirty in dirty_seglist_info f2fs: handle error from f2fs_iget_nowait f2fs: fix equation of has_not_enough_free_secs() f2fs: add MAINTAINERS entry f2fs: return a default value for non-void function f2fs: invalidate the node page if allocation is failed f2fs: add missing #include <linux/prefetch.h> f2fs: do f2fs_balance_fs in front of dir operations f2fs: should recover orphan and fsync data f2fs: fix handling errors got by f2fs_write_inode f2fs: fix up f2fs_get_parent issue to retrieve correct parent inode number f2fs: fix wrong calculation on f_files in statfs f2fs: remove set_page_dirty for atomic f2fs_end_io_write
Diffstat (limited to 'fs/f2fs')
-rw-r--r--fs/f2fs/data.c1
-rw-r--r--fs/f2fs/dir.c16
-rw-r--r--fs/f2fs/f2fs.h2
-rw-r--r--fs/f2fs/file.c10
-rw-r--r--fs/f2fs/gc.c34
-rw-r--r--fs/f2fs/hash.c18
-rw-r--r--fs/f2fs/inode.c1
-rw-r--r--fs/f2fs/namei.c34
-rw-r--r--fs/f2fs/node.c37
-rw-r--r--fs/f2fs/recovery.c10
-rw-r--r--fs/f2fs/segment.c46
-rw-r--r--fs/f2fs/segment.h15
-rw-r--r--fs/f2fs/super.c15
-rw-r--r--fs/f2fs/xattr.c5
14 files changed, 107 insertions, 137 deletions
diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c
index 655aeabc1dd..3aa5ce7cab8 100644
--- a/fs/f2fs/data.c
+++ b/fs/f2fs/data.c
@@ -16,6 +16,7 @@
16#include <linux/backing-dev.h> 16#include <linux/backing-dev.h>
17#include <linux/blkdev.h> 17#include <linux/blkdev.h>
18#include <linux/bio.h> 18#include <linux/bio.h>
19#include <linux/prefetch.h>
19 20
20#include "f2fs.h" 21#include "f2fs.h"
21#include "node.h" 22#include "node.h"
diff --git a/fs/f2fs/dir.c b/fs/f2fs/dir.c
index b4e24f32b54..951ed52748f 100644
--- a/fs/f2fs/dir.c
+++ b/fs/f2fs/dir.c
@@ -11,6 +11,7 @@
11#include <linux/fs.h> 11#include <linux/fs.h>
12#include <linux/f2fs_fs.h> 12#include <linux/f2fs_fs.h>
13#include "f2fs.h" 13#include "f2fs.h"
14#include "node.h"
14#include "acl.h" 15#include "acl.h"
15 16
16static unsigned long dir_blocks(struct inode *inode) 17static unsigned long dir_blocks(struct inode *inode)
@@ -74,7 +75,7 @@ static unsigned long dir_block_index(unsigned int level, unsigned int idx)
74 return bidx; 75 return bidx;
75} 76}
76 77
77static bool early_match_name(const char *name, int namelen, 78static bool early_match_name(const char *name, size_t namelen,
78 f2fs_hash_t namehash, struct f2fs_dir_entry *de) 79 f2fs_hash_t namehash, struct f2fs_dir_entry *de)
79{ 80{
80 if (le16_to_cpu(de->name_len) != namelen) 81 if (le16_to_cpu(de->name_len) != namelen)
@@ -87,7 +88,7 @@ static bool early_match_name(const char *name, int namelen,
87} 88}
88 89
89static struct f2fs_dir_entry *find_in_block(struct page *dentry_page, 90static struct f2fs_dir_entry *find_in_block(struct page *dentry_page,
90 const char *name, int namelen, int *max_slots, 91 const char *name, size_t namelen, int *max_slots,
91 f2fs_hash_t namehash, struct page **res_page) 92 f2fs_hash_t namehash, struct page **res_page)
92{ 93{
93 struct f2fs_dir_entry *de; 94 struct f2fs_dir_entry *de;
@@ -126,7 +127,7 @@ found:
126} 127}
127 128
128static struct f2fs_dir_entry *find_in_level(struct inode *dir, 129static struct f2fs_dir_entry *find_in_level(struct inode *dir,
129 unsigned int level, const char *name, int namelen, 130 unsigned int level, const char *name, size_t namelen,
130 f2fs_hash_t namehash, struct page **res_page) 131 f2fs_hash_t namehash, struct page **res_page)
131{ 132{
132 int s = GET_DENTRY_SLOTS(namelen); 133 int s = GET_DENTRY_SLOTS(namelen);
@@ -181,7 +182,7 @@ struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
181 struct qstr *child, struct page **res_page) 182 struct qstr *child, struct page **res_page)
182{ 183{
183 const char *name = child->name; 184 const char *name = child->name;
184 int namelen = child->len; 185 size_t namelen = child->len;
185 unsigned long npages = dir_blocks(dir); 186 unsigned long npages = dir_blocks(dir);
186 struct f2fs_dir_entry *de = NULL; 187 struct f2fs_dir_entry *de = NULL;
187 f2fs_hash_t name_hash; 188 f2fs_hash_t name_hash;
@@ -308,6 +309,7 @@ static int init_inode_metadata(struct inode *inode, struct dentry *dentry)
308 ipage = get_node_page(F2FS_SB(dir->i_sb), inode->i_ino); 309 ipage = get_node_page(F2FS_SB(dir->i_sb), inode->i_ino);
309 if (IS_ERR(ipage)) 310 if (IS_ERR(ipage))
310 return PTR_ERR(ipage); 311 return PTR_ERR(ipage);
312 set_cold_node(inode, ipage);
311 init_dent_inode(dentry, ipage); 313 init_dent_inode(dentry, ipage);
312 f2fs_put_page(ipage, 1); 314 f2fs_put_page(ipage, 1);
313 } 315 }
@@ -381,7 +383,7 @@ int f2fs_add_link(struct dentry *dentry, struct inode *inode)
381 struct inode *dir = dentry->d_parent->d_inode; 383 struct inode *dir = dentry->d_parent->d_inode;
382 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb); 384 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
383 const char *name = dentry->d_name.name; 385 const char *name = dentry->d_name.name;
384 int namelen = dentry->d_name.len; 386 size_t namelen = dentry->d_name.len;
385 struct page *dentry_page = NULL; 387 struct page *dentry_page = NULL;
386 struct f2fs_dentry_block *dentry_blk = NULL; 388 struct f2fs_dentry_block *dentry_blk = NULL;
387 int slots = GET_DENTRY_SLOTS(namelen); 389 int slots = GET_DENTRY_SLOTS(namelen);
@@ -540,13 +542,13 @@ int f2fs_make_empty(struct inode *inode, struct inode *parent)
540 542
541 de = &dentry_blk->dentry[0]; 543 de = &dentry_blk->dentry[0];
542 de->name_len = cpu_to_le16(1); 544 de->name_len = cpu_to_le16(1);
543 de->hash_code = 0; 545 de->hash_code = f2fs_dentry_hash(".", 1);
544 de->ino = cpu_to_le32(inode->i_ino); 546 de->ino = cpu_to_le32(inode->i_ino);
545 memcpy(dentry_blk->filename[0], ".", 1); 547 memcpy(dentry_blk->filename[0], ".", 1);
546 set_de_type(de, inode); 548 set_de_type(de, inode);
547 549
548 de = &dentry_blk->dentry[1]; 550 de = &dentry_blk->dentry[1];
549 de->hash_code = 0; 551 de->hash_code = f2fs_dentry_hash("..", 2);
550 de->name_len = cpu_to_le16(2); 552 de->name_len = cpu_to_le16(2);
551 de->ino = cpu_to_le32(parent->i_ino); 553 de->ino = cpu_to_le32(parent->i_ino);
552 memcpy(dentry_blk->filename[1], "..", 2); 554 memcpy(dentry_blk->filename[1], "..", 2);
diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index a18d63db2fb..13c6dfbb718 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -881,7 +881,7 @@ int f2fs_sync_fs(struct super_block *, int);
881/* 881/*
882 * hash.c 882 * hash.c
883 */ 883 */
884f2fs_hash_t f2fs_dentry_hash(const char *, int); 884f2fs_hash_t f2fs_dentry_hash(const char *, size_t);
885 885
886/* 886/*
887 * node.c 887 * node.c
diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
index f9e085dfb1f..7f9ea9271eb 100644
--- a/fs/f2fs/file.c
+++ b/fs/f2fs/file.c
@@ -160,15 +160,17 @@ int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
160 if (need_to_sync_dir(sbi, inode)) 160 if (need_to_sync_dir(sbi, inode))
161 need_cp = true; 161 need_cp = true;
162 162
163 f2fs_write_inode(inode, NULL);
164
165 if (need_cp) { 163 if (need_cp) {
166 /* all the dirty node pages should be flushed for POR */ 164 /* all the dirty node pages should be flushed for POR */
167 ret = f2fs_sync_fs(inode->i_sb, 1); 165 ret = f2fs_sync_fs(inode->i_sb, 1);
168 clear_inode_flag(F2FS_I(inode), FI_NEED_CP); 166 clear_inode_flag(F2FS_I(inode), FI_NEED_CP);
169 } else { 167 } else {
170 while (sync_node_pages(sbi, inode->i_ino, &wbc) == 0) 168 /* if there is no written node page, write its inode page */
171 f2fs_write_inode(inode, NULL); 169 while (!sync_node_pages(sbi, inode->i_ino, &wbc)) {
170 ret = f2fs_write_inode(inode, NULL);
171 if (ret)
172 goto out;
173 }
172 filemap_fdatawait_range(sbi->node_inode->i_mapping, 174 filemap_fdatawait_range(sbi->node_inode->i_mapping,
173 0, LONG_MAX); 175 0, LONG_MAX);
174 } 176 }
diff --git a/fs/f2fs/gc.c b/fs/f2fs/gc.c
index 644aa380827..b0ec721e984 100644
--- a/fs/f2fs/gc.c
+++ b/fs/f2fs/gc.c
@@ -390,9 +390,7 @@ next_step:
390 } 390 }
391 391
392 err = check_valid_map(sbi, segno, off); 392 err = check_valid_map(sbi, segno, off);
393 if (err == GC_ERROR) 393 if (err == GC_NEXT)
394 return err;
395 else if (err == GC_NEXT)
396 continue; 394 continue;
397 395
398 if (initial) { 396 if (initial) {
@@ -430,28 +428,22 @@ next_step:
430 */ 428 */
431block_t start_bidx_of_node(unsigned int node_ofs) 429block_t start_bidx_of_node(unsigned int node_ofs)
432{ 430{
433 block_t start_bidx; 431 unsigned int indirect_blks = 2 * NIDS_PER_BLOCK + 4;
434 unsigned int bidx, indirect_blks; 432 unsigned int bidx;
435 int dec;
436 433
437 indirect_blks = 2 * NIDS_PER_BLOCK + 4; 434 if (node_ofs == 0)
435 return 0;
438 436
439 start_bidx = 1; 437 if (node_ofs <= 2) {
440 if (node_ofs == 0) {
441 start_bidx = 0;
442 } else if (node_ofs <= 2) {
443 bidx = node_ofs - 1; 438 bidx = node_ofs - 1;
444 } else if (node_ofs <= indirect_blks) { 439 } else if (node_ofs <= indirect_blks) {
445 dec = (node_ofs - 4) / (NIDS_PER_BLOCK + 1); 440 int dec = (node_ofs - 4) / (NIDS_PER_BLOCK + 1);
446 bidx = node_ofs - 2 - dec; 441 bidx = node_ofs - 2 - dec;
447 } else { 442 } else {
448 dec = (node_ofs - indirect_blks - 3) / (NIDS_PER_BLOCK + 1); 443 int dec = (node_ofs - indirect_blks - 3) / (NIDS_PER_BLOCK + 1);
449 bidx = node_ofs - 5 - dec; 444 bidx = node_ofs - 5 - dec;
450 } 445 }
451 446 return bidx * ADDRS_PER_BLOCK + ADDRS_PER_INODE;
452 if (start_bidx)
453 start_bidx = bidx * ADDRS_PER_BLOCK + ADDRS_PER_INODE;
454 return start_bidx;
455} 447}
456 448
457static int check_dnode(struct f2fs_sb_info *sbi, struct f2fs_summary *sum, 449static int check_dnode(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
@@ -556,9 +548,7 @@ next_step:
556 } 548 }
557 549
558 err = check_valid_map(sbi, segno, off); 550 err = check_valid_map(sbi, segno, off);
559 if (err == GC_ERROR) 551 if (err == GC_NEXT)
560 goto stop;
561 else if (err == GC_NEXT)
562 continue; 552 continue;
563 553
564 if (phase == 0) { 554 if (phase == 0) {
@@ -568,9 +558,7 @@ next_step:
568 558
569 /* Get an inode by ino with checking validity */ 559 /* Get an inode by ino with checking validity */
570 err = check_dnode(sbi, entry, &dni, start_addr + off, &nofs); 560 err = check_dnode(sbi, entry, &dni, start_addr + off, &nofs);
571 if (err == GC_ERROR) 561 if (err == GC_NEXT)
572 goto stop;
573 else if (err == GC_NEXT)
574 continue; 562 continue;
575 563
576 if (phase == 1) { 564 if (phase == 1) {
diff --git a/fs/f2fs/hash.c b/fs/f2fs/hash.c
index a60f04200f8..6eb8d269b53 100644
--- a/fs/f2fs/hash.c
+++ b/fs/f2fs/hash.c
@@ -42,7 +42,7 @@ static void TEA_transform(unsigned int buf[4], unsigned int const in[])
42 buf[1] += b1; 42 buf[1] += b1;
43} 43}
44 44
45static void str2hashbuf(const char *msg, int len, unsigned int *buf, int num) 45static void str2hashbuf(const char *msg, size_t len, unsigned int *buf, int num)
46{ 46{
47 unsigned pad, val; 47 unsigned pad, val;
48 int i; 48 int i;
@@ -69,13 +69,17 @@ static void str2hashbuf(const char *msg, int len, unsigned int *buf, int num)
69 *buf++ = pad; 69 *buf++ = pad;
70} 70}
71 71
72f2fs_hash_t f2fs_dentry_hash(const char *name, int len) 72f2fs_hash_t f2fs_dentry_hash(const char *name, size_t len)
73{ 73{
74 __u32 hash, minor_hash; 74 __u32 hash;
75 f2fs_hash_t f2fs_hash; 75 f2fs_hash_t f2fs_hash;
76 const char *p; 76 const char *p;
77 __u32 in[8], buf[4]; 77 __u32 in[8], buf[4];
78 78
79 if ((len <= 2) && (name[0] == '.') &&
80 (name[1] == '.' || name[1] == '\0'))
81 return 0;
82
79 /* Initialize the default seed for the hash checksum functions */ 83 /* Initialize the default seed for the hash checksum functions */
80 buf[0] = 0x67452301; 84 buf[0] = 0x67452301;
81 buf[1] = 0xefcdab89; 85 buf[1] = 0xefcdab89;
@@ -83,15 +87,15 @@ f2fs_hash_t f2fs_dentry_hash(const char *name, int len)
83 buf[3] = 0x10325476; 87 buf[3] = 0x10325476;
84 88
85 p = name; 89 p = name;
86 while (len > 0) { 90 while (1) {
87 str2hashbuf(p, len, in, 4); 91 str2hashbuf(p, len, in, 4);
88 TEA_transform(buf, in); 92 TEA_transform(buf, in);
89 len -= 16;
90 p += 16; 93 p += 16;
94 if (len <= 16)
95 break;
96 len -= 16;
91 } 97 }
92 hash = buf[0]; 98 hash = buf[0];
93 minor_hash = buf[1];
94
95 f2fs_hash = cpu_to_le32(hash & ~F2FS_HASH_COL_BIT); 99 f2fs_hash = cpu_to_le32(hash & ~F2FS_HASH_COL_BIT);
96 return f2fs_hash; 100 return f2fs_hash;
97} 101}
diff --git a/fs/f2fs/inode.c b/fs/f2fs/inode.c
index df5fb381ebf..bf20b4d0321 100644
--- a/fs/f2fs/inode.c
+++ b/fs/f2fs/inode.c
@@ -203,6 +203,7 @@ void update_inode(struct inode *inode, struct page *node_page)
203 ri->i_flags = cpu_to_le32(F2FS_I(inode)->i_flags); 203 ri->i_flags = cpu_to_le32(F2FS_I(inode)->i_flags);
204 ri->i_pino = cpu_to_le32(F2FS_I(inode)->i_pino); 204 ri->i_pino = cpu_to_le32(F2FS_I(inode)->i_pino);
205 ri->i_generation = cpu_to_le32(inode->i_generation); 205 ri->i_generation = cpu_to_le32(inode->i_generation);
206 set_cold_node(inode, node_page);
206 set_page_dirty(node_page); 207 set_page_dirty(node_page);
207} 208}
208 209
diff --git a/fs/f2fs/namei.c b/fs/f2fs/namei.c
index 89b7675dc37..1a49b881bac 100644
--- a/fs/f2fs/namei.c
+++ b/fs/f2fs/namei.c
@@ -77,8 +77,8 @@ fail:
77 77
78static int is_multimedia_file(const unsigned char *s, const char *sub) 78static int is_multimedia_file(const unsigned char *s, const char *sub)
79{ 79{
80 int slen = strlen(s); 80 size_t slen = strlen(s);
81 int sublen = strlen(sub); 81 size_t sublen = strlen(sub);
82 int ret; 82 int ret;
83 83
84 if (sublen > slen) 84 if (sublen > slen)
@@ -123,6 +123,8 @@ static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
123 nid_t ino = 0; 123 nid_t ino = 0;
124 int err; 124 int err;
125 125
126 f2fs_balance_fs(sbi);
127
126 inode = f2fs_new_inode(dir, mode); 128 inode = f2fs_new_inode(dir, mode);
127 if (IS_ERR(inode)) 129 if (IS_ERR(inode))
128 return PTR_ERR(inode); 130 return PTR_ERR(inode);
@@ -144,8 +146,6 @@ static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
144 if (!sbi->por_doing) 146 if (!sbi->por_doing)
145 d_instantiate(dentry, inode); 147 d_instantiate(dentry, inode);
146 unlock_new_inode(inode); 148 unlock_new_inode(inode);
147
148 f2fs_balance_fs(sbi);
149 return 0; 149 return 0;
150out: 150out:
151 clear_nlink(inode); 151 clear_nlink(inode);
@@ -163,6 +163,8 @@ static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
163 struct f2fs_sb_info *sbi = F2FS_SB(sb); 163 struct f2fs_sb_info *sbi = F2FS_SB(sb);
164 int err; 164 int err;
165 165
166 f2fs_balance_fs(sbi);
167
166 inode->i_ctime = CURRENT_TIME; 168 inode->i_ctime = CURRENT_TIME;
167 atomic_inc(&inode->i_count); 169 atomic_inc(&inode->i_count);
168 170
@@ -172,8 +174,6 @@ static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
172 goto out; 174 goto out;
173 175
174 d_instantiate(dentry, inode); 176 d_instantiate(dentry, inode);
175
176 f2fs_balance_fs(sbi);
177 return 0; 177 return 0;
178out: 178out:
179 clear_inode_flag(F2FS_I(inode), FI_INC_LINK); 179 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
@@ -223,6 +223,8 @@ static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
223 struct page *page; 223 struct page *page;
224 int err = -ENOENT; 224 int err = -ENOENT;
225 225
226 f2fs_balance_fs(sbi);
227
226 de = f2fs_find_entry(dir, &dentry->d_name, &page); 228 de = f2fs_find_entry(dir, &dentry->d_name, &page);
227 if (!de) 229 if (!de)
228 goto fail; 230 goto fail;
@@ -238,7 +240,6 @@ static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
238 240
239 /* In order to evict this inode, we set it dirty */ 241 /* In order to evict this inode, we set it dirty */
240 mark_inode_dirty(inode); 242 mark_inode_dirty(inode);
241 f2fs_balance_fs(sbi);
242fail: 243fail:
243 return err; 244 return err;
244} 245}
@@ -249,9 +250,11 @@ static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
249 struct super_block *sb = dir->i_sb; 250 struct super_block *sb = dir->i_sb;
250 struct f2fs_sb_info *sbi = F2FS_SB(sb); 251 struct f2fs_sb_info *sbi = F2FS_SB(sb);
251 struct inode *inode; 252 struct inode *inode;
252 unsigned symlen = strlen(symname) + 1; 253 size_t symlen = strlen(symname) + 1;
253 int err; 254 int err;
254 255
256 f2fs_balance_fs(sbi);
257
255 inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO); 258 inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
256 if (IS_ERR(inode)) 259 if (IS_ERR(inode))
257 return PTR_ERR(inode); 260 return PTR_ERR(inode);
@@ -268,9 +271,6 @@ static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
268 271
269 d_instantiate(dentry, inode); 272 d_instantiate(dentry, inode);
270 unlock_new_inode(inode); 273 unlock_new_inode(inode);
271
272 f2fs_balance_fs(sbi);
273
274 return err; 274 return err;
275out: 275out:
276 clear_nlink(inode); 276 clear_nlink(inode);
@@ -286,6 +286,8 @@ static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
286 struct inode *inode; 286 struct inode *inode;
287 int err; 287 int err;
288 288
289 f2fs_balance_fs(sbi);
290
289 inode = f2fs_new_inode(dir, S_IFDIR | mode); 291 inode = f2fs_new_inode(dir, S_IFDIR | mode);
290 if (IS_ERR(inode)) 292 if (IS_ERR(inode))
291 return PTR_ERR(inode); 293 return PTR_ERR(inode);
@@ -305,7 +307,6 @@ static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
305 d_instantiate(dentry, inode); 307 d_instantiate(dentry, inode);
306 unlock_new_inode(inode); 308 unlock_new_inode(inode);
307 309
308 f2fs_balance_fs(sbi);
309 return 0; 310 return 0;
310 311
311out_fail: 312out_fail:
@@ -336,6 +337,8 @@ static int f2fs_mknod(struct inode *dir, struct dentry *dentry,
336 if (!new_valid_dev(rdev)) 337 if (!new_valid_dev(rdev))
337 return -EINVAL; 338 return -EINVAL;
338 339
340 f2fs_balance_fs(sbi);
341
339 inode = f2fs_new_inode(dir, mode); 342 inode = f2fs_new_inode(dir, mode);
340 if (IS_ERR(inode)) 343 if (IS_ERR(inode))
341 return PTR_ERR(inode); 344 return PTR_ERR(inode);
@@ -350,9 +353,6 @@ static int f2fs_mknod(struct inode *dir, struct dentry *dentry,
350 alloc_nid_done(sbi, inode->i_ino); 353 alloc_nid_done(sbi, inode->i_ino);
351 d_instantiate(dentry, inode); 354 d_instantiate(dentry, inode);
352 unlock_new_inode(inode); 355 unlock_new_inode(inode);
353
354 f2fs_balance_fs(sbi);
355
356 return 0; 356 return 0;
357out: 357out:
358 clear_nlink(inode); 358 clear_nlink(inode);
@@ -376,6 +376,8 @@ static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
376 struct f2fs_dir_entry *new_entry; 376 struct f2fs_dir_entry *new_entry;
377 int err = -ENOENT; 377 int err = -ENOENT;
378 378
379 f2fs_balance_fs(sbi);
380
379 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page); 381 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
380 if (!old_entry) 382 if (!old_entry)
381 goto out; 383 goto out;
@@ -441,8 +443,6 @@ static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
441 } 443 }
442 444
443 mutex_unlock_op(sbi, RENAME); 445 mutex_unlock_op(sbi, RENAME);
444
445 f2fs_balance_fs(sbi);
446 return 0; 446 return 0;
447 447
448out_dir: 448out_dir:
diff --git a/fs/f2fs/node.c b/fs/f2fs/node.c
index 19870361497..5066bfd256c 100644
--- a/fs/f2fs/node.c
+++ b/fs/f2fs/node.c
@@ -484,12 +484,14 @@ static void truncate_node(struct dnode_of_data *dn)
484 struct node_info ni; 484 struct node_info ni;
485 485
486 get_node_info(sbi, dn->nid, &ni); 486 get_node_info(sbi, dn->nid, &ni);
487 if (dn->inode->i_blocks == 0) {
488 BUG_ON(ni.blk_addr != NULL_ADDR);
489 goto invalidate;
490 }
487 BUG_ON(ni.blk_addr == NULL_ADDR); 491 BUG_ON(ni.blk_addr == NULL_ADDR);
488 492
489 if (ni.blk_addr != NULL_ADDR)
490 invalidate_blocks(sbi, ni.blk_addr);
491
492 /* Deallocate node address */ 493 /* Deallocate node address */
494 invalidate_blocks(sbi, ni.blk_addr);
493 dec_valid_node_count(sbi, dn->inode, 1); 495 dec_valid_node_count(sbi, dn->inode, 1);
494 set_node_addr(sbi, &ni, NULL_ADDR); 496 set_node_addr(sbi, &ni, NULL_ADDR);
495 497
@@ -499,7 +501,7 @@ static void truncate_node(struct dnode_of_data *dn)
499 } else { 501 } else {
500 sync_inode_page(dn); 502 sync_inode_page(dn);
501 } 503 }
502 504invalidate:
503 clear_node_page_dirty(dn->node_page); 505 clear_node_page_dirty(dn->node_page);
504 F2FS_SET_SB_DIRT(sbi); 506 F2FS_SET_SB_DIRT(sbi);
505 507
@@ -768,20 +770,12 @@ int remove_inode_page(struct inode *inode)
768 dn.inode_page_locked = 1; 770 dn.inode_page_locked = 1;
769 truncate_node(&dn); 771 truncate_node(&dn);
770 } 772 }
771 if (inode->i_blocks == 1) {
772 /* inernally call f2fs_put_page() */
773 set_new_dnode(&dn, inode, page, page, ino);
774 truncate_node(&dn);
775 } else if (inode->i_blocks == 0) {
776 struct node_info ni;
777 get_node_info(sbi, inode->i_ino, &ni);
778 773
779 /* called after f2fs_new_inode() is failed */ 774 /* 0 is possible, after f2fs_new_inode() is failed */
780 BUG_ON(ni.blk_addr != NULL_ADDR); 775 BUG_ON(inode->i_blocks != 0 && inode->i_blocks != 1);
781 f2fs_put_page(page, 1); 776 set_new_dnode(&dn, inode, page, page, ino);
782 } else { 777 truncate_node(&dn);
783 BUG(); 778
784 }
785 mutex_unlock_op(sbi, NODE_TRUNC); 779 mutex_unlock_op(sbi, NODE_TRUNC);
786 return 0; 780 return 0;
787} 781}
@@ -834,17 +828,18 @@ struct page *new_node_page(struct dnode_of_data *dn, unsigned int ofs)
834 goto fail; 828 goto fail;
835 } 829 }
836 set_node_addr(sbi, &new_ni, NEW_ADDR); 830 set_node_addr(sbi, &new_ni, NEW_ADDR);
831 set_cold_node(dn->inode, page);
837 832
838 dn->node_page = page; 833 dn->node_page = page;
839 sync_inode_page(dn); 834 sync_inode_page(dn);
840 set_page_dirty(page); 835 set_page_dirty(page);
841 set_cold_node(dn->inode, page);
842 if (ofs == 0) 836 if (ofs == 0)
843 inc_valid_inode_count(sbi); 837 inc_valid_inode_count(sbi);
844 838
845 return page; 839 return page;
846 840
847fail: 841fail:
842 clear_node_page_dirty(page);
848 f2fs_put_page(page, 1); 843 f2fs_put_page(page, 1);
849 return ERR_PTR(err); 844 return ERR_PTR(err);
850} 845}
@@ -1093,7 +1088,6 @@ static int f2fs_write_node_page(struct page *page,
1093{ 1088{
1094 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb); 1089 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
1095 nid_t nid; 1090 nid_t nid;
1096 unsigned int nofs;
1097 block_t new_addr; 1091 block_t new_addr;
1098 struct node_info ni; 1092 struct node_info ni;
1099 1093
@@ -1110,7 +1104,6 @@ static int f2fs_write_node_page(struct page *page,
1110 1104
1111 /* get old block addr of this node page */ 1105 /* get old block addr of this node page */
1112 nid = nid_of_node(page); 1106 nid = nid_of_node(page);
1113 nofs = ofs_of_node(page);
1114 BUG_ON(page->index != nid); 1107 BUG_ON(page->index != nid);
1115 1108
1116 get_node_info(sbi, nid, &ni); 1109 get_node_info(sbi, nid, &ni);
@@ -1571,7 +1564,7 @@ void flush_nat_entries(struct f2fs_sb_info *sbi)
1571 nid_t nid; 1564 nid_t nid;
1572 struct f2fs_nat_entry raw_ne; 1565 struct f2fs_nat_entry raw_ne;
1573 int offset = -1; 1566 int offset = -1;
1574 block_t old_blkaddr, new_blkaddr; 1567 block_t new_blkaddr;
1575 1568
1576 ne = list_entry(cur, struct nat_entry, list); 1569 ne = list_entry(cur, struct nat_entry, list);
1577 nid = nat_get_nid(ne); 1570 nid = nat_get_nid(ne);
@@ -1585,7 +1578,6 @@ void flush_nat_entries(struct f2fs_sb_info *sbi)
1585 offset = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 1); 1578 offset = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 1);
1586 if (offset >= 0) { 1579 if (offset >= 0) {
1587 raw_ne = nat_in_journal(sum, offset); 1580 raw_ne = nat_in_journal(sum, offset);
1588 old_blkaddr = le32_to_cpu(raw_ne.block_addr);
1589 goto flush_now; 1581 goto flush_now;
1590 } 1582 }
1591to_nat_page: 1583to_nat_page:
@@ -1607,7 +1599,6 @@ to_nat_page:
1607 1599
1608 BUG_ON(!nat_blk); 1600 BUG_ON(!nat_blk);
1609 raw_ne = nat_blk->entries[nid - start_nid]; 1601 raw_ne = nat_blk->entries[nid - start_nid];
1610 old_blkaddr = le32_to_cpu(raw_ne.block_addr);
1611flush_now: 1602flush_now:
1612 new_blkaddr = nat_get_blkaddr(ne); 1603 new_blkaddr = nat_get_blkaddr(ne);
1613 1604
diff --git a/fs/f2fs/recovery.c b/fs/f2fs/recovery.c
index b07e9b6ef37..b571fee677d 100644
--- a/fs/f2fs/recovery.c
+++ b/fs/f2fs/recovery.c
@@ -144,14 +144,15 @@ static int find_fsync_dnodes(struct f2fs_sb_info *sbi, struct list_head *head)
144 goto out; 144 goto out;
145 } 145 }
146 146
147 INIT_LIST_HEAD(&entry->list);
148 list_add_tail(&entry->list, head);
149
150 entry->inode = f2fs_iget(sbi->sb, ino_of_node(page)); 147 entry->inode = f2fs_iget(sbi->sb, ino_of_node(page));
151 if (IS_ERR(entry->inode)) { 148 if (IS_ERR(entry->inode)) {
152 err = PTR_ERR(entry->inode); 149 err = PTR_ERR(entry->inode);
150 kmem_cache_free(fsync_entry_slab, entry);
153 goto out; 151 goto out;
154 } 152 }
153
154 INIT_LIST_HEAD(&entry->list);
155 list_add_tail(&entry->list, head);
155 entry->blkaddr = blkaddr; 156 entry->blkaddr = blkaddr;
156 } 157 }
157 if (IS_INODE(page)) { 158 if (IS_INODE(page)) {
@@ -228,6 +229,9 @@ static void check_index_in_prev_nodes(struct f2fs_sb_info *sbi,
228 229
229 /* Deallocate previous index in the node page */ 230 /* Deallocate previous index in the node page */
230 inode = f2fs_iget_nowait(sbi->sb, ino); 231 inode = f2fs_iget_nowait(sbi->sb, ino);
232 if (IS_ERR(inode))
233 return;
234
231 truncate_hole(inode, bidx, bidx + 1); 235 truncate_hole(inode, bidx, bidx + 1);
232 iput(inode); 236 iput(inode);
233} 237}
diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c
index 1b26e4ea101..de6240922b0 100644
--- a/fs/f2fs/segment.c
+++ b/fs/f2fs/segment.c
@@ -12,54 +12,23 @@
12#include <linux/f2fs_fs.h> 12#include <linux/f2fs_fs.h>
13#include <linux/bio.h> 13#include <linux/bio.h>
14#include <linux/blkdev.h> 14#include <linux/blkdev.h>
15#include <linux/prefetch.h>
15#include <linux/vmalloc.h> 16#include <linux/vmalloc.h>
16 17
17#include "f2fs.h" 18#include "f2fs.h"
18#include "segment.h" 19#include "segment.h"
19#include "node.h" 20#include "node.h"
20 21
21static int need_to_flush(struct f2fs_sb_info *sbi)
22{
23 unsigned int pages_per_sec = (1 << sbi->log_blocks_per_seg) *
24 sbi->segs_per_sec;
25 int node_secs = ((get_pages(sbi, F2FS_DIRTY_NODES) + pages_per_sec - 1)
26 >> sbi->log_blocks_per_seg) / sbi->segs_per_sec;
27 int dent_secs = ((get_pages(sbi, F2FS_DIRTY_DENTS) + pages_per_sec - 1)
28 >> sbi->log_blocks_per_seg) / sbi->segs_per_sec;
29
30 if (sbi->por_doing)
31 return 0;
32
33 if (free_sections(sbi) <= (node_secs + 2 * dent_secs +
34 reserved_sections(sbi)))
35 return 1;
36 return 0;
37}
38
39/* 22/*
40 * This function balances dirty node and dentry pages. 23 * This function balances dirty node and dentry pages.
41 * In addition, it controls garbage collection. 24 * In addition, it controls garbage collection.
42 */ 25 */
43void f2fs_balance_fs(struct f2fs_sb_info *sbi) 26void f2fs_balance_fs(struct f2fs_sb_info *sbi)
44{ 27{
45 struct writeback_control wbc = {
46 .sync_mode = WB_SYNC_ALL,
47 .nr_to_write = LONG_MAX,
48 .for_reclaim = 0,
49 };
50
51 if (sbi->por_doing)
52 return;
53
54 /* 28 /*
55 * We should do checkpoint when there are so many dirty node pages 29 * We should do GC or end up with checkpoint, if there are so many dirty
56 * with enough free segments. After then, we should do GC. 30 * dir/node pages without enough free segments.
57 */ 31 */
58 if (need_to_flush(sbi)) {
59 sync_dirty_dir_inodes(sbi);
60 sync_node_pages(sbi, 0, &wbc);
61 }
62
63 if (has_not_enough_free_secs(sbi)) { 32 if (has_not_enough_free_secs(sbi)) {
64 mutex_lock(&sbi->gc_mutex); 33 mutex_lock(&sbi->gc_mutex);
65 f2fs_gc(sbi, 1); 34 f2fs_gc(sbi, 1);
@@ -631,7 +600,6 @@ static void f2fs_end_io_write(struct bio *bio, int err)
631 if (page->mapping) 600 if (page->mapping)
632 set_bit(AS_EIO, &page->mapping->flags); 601 set_bit(AS_EIO, &page->mapping->flags);
633 set_ckpt_flags(p->sbi->ckpt, CP_ERROR_FLAG); 602 set_ckpt_flags(p->sbi->ckpt, CP_ERROR_FLAG);
634 set_page_dirty(page);
635 } 603 }
636 end_page_writeback(page); 604 end_page_writeback(page);
637 dec_page_count(p->sbi, F2FS_WRITEBACK); 605 dec_page_count(p->sbi, F2FS_WRITEBACK);
@@ -791,11 +759,10 @@ static int __get_segment_type(struct page *page, enum page_type p_type)
791 return __get_segment_type_2(page, p_type); 759 return __get_segment_type_2(page, p_type);
792 case 4: 760 case 4:
793 return __get_segment_type_4(page, p_type); 761 return __get_segment_type_4(page, p_type);
794 case 6:
795 return __get_segment_type_6(page, p_type);
796 default:
797 BUG();
798 } 762 }
763 /* NR_CURSEG_TYPE(6) logs by default */
764 BUG_ON(sbi->active_logs != NR_CURSEG_TYPE);
765 return __get_segment_type_6(page, p_type);
799} 766}
800 767
801static void do_write_page(struct f2fs_sb_info *sbi, struct page *page, 768static void do_write_page(struct f2fs_sb_info *sbi, struct page *page,
@@ -1608,7 +1575,6 @@ static int build_dirty_segmap(struct f2fs_sb_info *sbi)
1608 1575
1609 for (i = 0; i < NR_DIRTY_TYPE; i++) { 1576 for (i = 0; i < NR_DIRTY_TYPE; i++) {
1610 dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL); 1577 dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL);
1611 dirty_i->nr_dirty[i] = 0;
1612 if (!dirty_i->dirty_segmap[i]) 1578 if (!dirty_i->dirty_segmap[i])
1613 return -ENOMEM; 1579 return -ENOMEM;
1614 } 1580 }
diff --git a/fs/f2fs/segment.h b/fs/f2fs/segment.h
index 0948405af6f..66a288a52fd 100644
--- a/fs/f2fs/segment.h
+++ b/fs/f2fs/segment.h
@@ -459,7 +459,20 @@ static inline int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
459 459
460static inline bool has_not_enough_free_secs(struct f2fs_sb_info *sbi) 460static inline bool has_not_enough_free_secs(struct f2fs_sb_info *sbi)
461{ 461{
462 return free_sections(sbi) <= reserved_sections(sbi); 462 unsigned int pages_per_sec = (1 << sbi->log_blocks_per_seg) *
463 sbi->segs_per_sec;
464 int node_secs = ((get_pages(sbi, F2FS_DIRTY_NODES) + pages_per_sec - 1)
465 >> sbi->log_blocks_per_seg) / sbi->segs_per_sec;
466 int dent_secs = ((get_pages(sbi, F2FS_DIRTY_DENTS) + pages_per_sec - 1)
467 >> sbi->log_blocks_per_seg) / sbi->segs_per_sec;
468
469 if (sbi->por_doing)
470 return false;
471
472 if (free_sections(sbi) <= (node_secs + 2 * dent_secs +
473 reserved_sections(sbi)))
474 return true;
475 return false;
463} 476}
464 477
465static inline int utilization(struct f2fs_sb_info *sbi) 478static inline int utilization(struct f2fs_sb_info *sbi)
diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
index 13867322cf5..08a94c814bd 100644
--- a/fs/f2fs/super.c
+++ b/fs/f2fs/super.c
@@ -119,7 +119,6 @@ static void f2fs_put_super(struct super_block *sb)
119int f2fs_sync_fs(struct super_block *sb, int sync) 119int f2fs_sync_fs(struct super_block *sb, int sync)
120{ 120{
121 struct f2fs_sb_info *sbi = F2FS_SB(sb); 121 struct f2fs_sb_info *sbi = F2FS_SB(sb);
122 int ret = 0;
123 122
124 if (!sbi->s_dirty && !get_pages(sbi, F2FS_DIRTY_NODES)) 123 if (!sbi->s_dirty && !get_pages(sbi, F2FS_DIRTY_NODES))
125 return 0; 124 return 0;
@@ -127,7 +126,7 @@ int f2fs_sync_fs(struct super_block *sb, int sync)
127 if (sync) 126 if (sync)
128 write_checkpoint(sbi, false, false); 127 write_checkpoint(sbi, false, false);
129 128
130 return ret; 129 return 0;
131} 130}
132 131
133static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf) 132static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
@@ -148,8 +147,8 @@ static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
148 buf->f_bfree = buf->f_blocks - valid_user_blocks(sbi) - ovp_count; 147 buf->f_bfree = buf->f_blocks - valid_user_blocks(sbi) - ovp_count;
149 buf->f_bavail = user_block_count - valid_user_blocks(sbi); 148 buf->f_bavail = user_block_count - valid_user_blocks(sbi);
150 149
151 buf->f_files = valid_inode_count(sbi); 150 buf->f_files = sbi->total_node_count;
152 buf->f_ffree = sbi->total_node_count - valid_node_count(sbi); 151 buf->f_ffree = sbi->total_node_count - valid_inode_count(sbi);
153 152
154 buf->f_namelen = F2FS_MAX_NAME_LEN; 153 buf->f_namelen = F2FS_MAX_NAME_LEN;
155 buf->f_fsid.val[0] = (u32)id; 154 buf->f_fsid.val[0] = (u32)id;
@@ -302,7 +301,7 @@ static int parse_options(struct f2fs_sb_info *sbi, char *options)
302 case Opt_active_logs: 301 case Opt_active_logs:
303 if (args->from && match_int(args, &arg)) 302 if (args->from && match_int(args, &arg))
304 return -EINVAL; 303 return -EINVAL;
305 if (arg != 2 && arg != 4 && arg != 6) 304 if (arg != 2 && arg != 4 && arg != NR_CURSEG_TYPE)
306 return -EINVAL; 305 return -EINVAL;
307 sbi->active_logs = arg; 306 sbi->active_logs = arg;
308 break; 307 break;
@@ -528,8 +527,7 @@ static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
528 527
529 /* if there are nt orphan nodes free them */ 528 /* if there are nt orphan nodes free them */
530 err = -EINVAL; 529 err = -EINVAL;
531 if (!is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG) && 530 if (recover_orphan_inodes(sbi))
532 recover_orphan_inodes(sbi))
533 goto free_node_inode; 531 goto free_node_inode;
534 532
535 /* read root inode and dentry */ 533 /* read root inode and dentry */
@@ -548,8 +546,7 @@ static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
548 } 546 }
549 547
550 /* recover fsynced data */ 548 /* recover fsynced data */
551 if (!is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG) && 549 if (!test_opt(sbi, DISABLE_ROLL_FORWARD))
552 !test_opt(sbi, DISABLE_ROLL_FORWARD))
553 recover_fsync_data(sbi); 550 recover_fsync_data(sbi);
554 551
555 /* After POR, we can run background GC thread */ 552 /* After POR, we can run background GC thread */
diff --git a/fs/f2fs/xattr.c b/fs/f2fs/xattr.c
index 7d52e8dc0c5..940136a3d3a 100644
--- a/fs/f2fs/xattr.c
+++ b/fs/f2fs/xattr.c
@@ -208,7 +208,7 @@ int f2fs_getxattr(struct inode *inode, int name_index, const char *name,
208 struct page *page; 208 struct page *page;
209 void *base_addr; 209 void *base_addr;
210 int error = 0, found = 0; 210 int error = 0, found = 0;
211 int value_len, name_len; 211 size_t value_len, name_len;
212 212
213 if (name == NULL) 213 if (name == NULL)
214 return -EINVAL; 214 return -EINVAL;
@@ -304,7 +304,8 @@ int f2fs_setxattr(struct inode *inode, int name_index, const char *name,
304 struct f2fs_xattr_entry *here, *last; 304 struct f2fs_xattr_entry *here, *last;
305 struct page *page; 305 struct page *page;
306 void *base_addr; 306 void *base_addr;
307 int error, found, free, name_len, newsize; 307 int error, found, free, newsize;
308 size_t name_len;
308 char *pval; 309 char *pval;
309 310
310 if (name == NULL) 311 if (name == NULL)