node.c (e8c8ce54807b19e90ac84e609b13f7d4e337eab1) node.c (300e129c15f0ed2f94482900a4cb65b28eb09d94)
1/*
2 * fs/f2fs/node.c
3 *
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as

--- 818 unchanged lines hidden (view full) ---

827 unsigned int nofs = 0;
828 struct f2fs_inode *ri;
829 struct dnode_of_data dn;
830 struct page *page;
831
832 trace_f2fs_truncate_inode_blocks_enter(inode, from);
833
834 level = get_node_path(inode, from, offset, noffset);
1/*
2 * fs/f2fs/node.c
3 *
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as

--- 818 unchanged lines hidden (view full) ---

827 unsigned int nofs = 0;
828 struct f2fs_inode *ri;
829 struct dnode_of_data dn;
830 struct page *page;
831
832 trace_f2fs_truncate_inode_blocks_enter(inode, from);
833
834 level = get_node_path(inode, from, offset, noffset);
835restart:
835
836 page = get_node_page(sbi, inode->i_ino);
837 if (IS_ERR(page)) {
838 trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(page));
839 return PTR_ERR(page);
840 }
841
842 set_new_dnode(&dn, inode, page, NULL, 0);
843 unlock_page(page);

--- 47 unchanged lines hidden (view full) ---

891 default:
892 BUG();
893 }
894 if (err < 0 && err != -ENOENT)
895 goto fail;
896 if (offset[1] == 0 &&
897 ri->i_nid[offset[0] - NODE_DIR1_BLOCK]) {
898 lock_page(page);
836 page = get_node_page(sbi, inode->i_ino);
837 if (IS_ERR(page)) {
838 trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(page));
839 return PTR_ERR(page);
840 }
841
842 set_new_dnode(&dn, inode, page, NULL, 0);
843 unlock_page(page);

--- 47 unchanged lines hidden (view full) ---

891 default:
892 BUG();
893 }
894 if (err < 0 && err != -ENOENT)
895 goto fail;
896 if (offset[1] == 0 &&
897 ri->i_nid[offset[0] - NODE_DIR1_BLOCK]) {
898 lock_page(page);
899 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
900 f2fs_put_page(page, 1);
901 goto restart;
902 }
899 BUG_ON(page->mapping != NODE_MAPPING(sbi));
903 f2fs_wait_on_page_writeback(page, NODE, true);
904 ri->i_nid[offset[0] - NODE_DIR1_BLOCK] = 0;
905 set_page_dirty(page);
906 unlock_page(page);
907 }
908 offset[1] = 0;
909 offset[0]++;
910 nofs += err;

--- 82 unchanged lines hidden (view full) ---

993 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
994 struct node_info old_ni, new_ni;
995 struct page *page;
996 int err;
997
998 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
999 return ERR_PTR(-EPERM);
1000
900 f2fs_wait_on_page_writeback(page, NODE, true);
901 ri->i_nid[offset[0] - NODE_DIR1_BLOCK] = 0;
902 set_page_dirty(page);
903 unlock_page(page);
904 }
905 offset[1] = 0;
906 offset[0]++;
907 nofs += err;

--- 82 unchanged lines hidden (view full) ---

990 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
991 struct node_info old_ni, new_ni;
992 struct page *page;
993 int err;
994
995 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
996 return ERR_PTR(-EPERM);
997
1001 page = grab_cache_page(NODE_MAPPING(sbi), dn->nid);
998 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), dn->nid, false);
1002 if (!page)
1003 return ERR_PTR(-ENOMEM);
1004
1005 if (unlikely(!inc_valid_node_count(sbi, dn->inode))) {
1006 err = -ENOSPC;
1007 goto fail;
1008 }
1009

--- 75 unchanged lines hidden (view full) ---

1085 f2fs_bug_on(sbi, check_nid_range(sbi, nid));
1086
1087 rcu_read_lock();
1088 apage = radix_tree_lookup(&NODE_MAPPING(sbi)->page_tree, nid);
1089 rcu_read_unlock();
1090 if (apage)
1091 return;
1092
999 if (!page)
1000 return ERR_PTR(-ENOMEM);
1001
1002 if (unlikely(!inc_valid_node_count(sbi, dn->inode))) {
1003 err = -ENOSPC;
1004 goto fail;
1005 }
1006

--- 75 unchanged lines hidden (view full) ---

1082 f2fs_bug_on(sbi, check_nid_range(sbi, nid));
1083
1084 rcu_read_lock();
1085 apage = radix_tree_lookup(&NODE_MAPPING(sbi)->page_tree, nid);
1086 rcu_read_unlock();
1087 if (apage)
1088 return;
1089
1093 apage = grab_cache_page(NODE_MAPPING(sbi), nid);
1090 apage = f2fs_grab_cache_page(NODE_MAPPING(sbi), nid, false);
1094 if (!apage)
1095 return;
1096
1097 err = read_node_page(apage, READA);
1098 f2fs_put_page(apage, err ? 1 : 0);
1099}
1100
1101/*

--- 24 unchanged lines hidden (view full) ---

1126{
1127 struct page *page;
1128 int err;
1129
1130 if (!nid)
1131 return ERR_PTR(-ENOENT);
1132 f2fs_bug_on(sbi, check_nid_range(sbi, nid));
1133repeat:
1091 if (!apage)
1092 return;
1093
1094 err = read_node_page(apage, READA);
1095 f2fs_put_page(apage, err ? 1 : 0);
1096}
1097
1098/*

--- 24 unchanged lines hidden (view full) ---

1123{
1124 struct page *page;
1125 int err;
1126
1127 if (!nid)
1128 return ERR_PTR(-ENOENT);
1129 f2fs_bug_on(sbi, check_nid_range(sbi, nid));
1130repeat:
1134 page = grab_cache_page(NODE_MAPPING(sbi), nid);
1131 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), nid, false);
1135 if (!page)
1136 return ERR_PTR(-ENOMEM);
1137
1138 err = read_node_page(page, READ_SYNC);
1139 if (err < 0) {
1140 f2fs_put_page(page, 1);
1141 return ERR_PTR(err);
1142 } else if (err == LOCKED_PAGE) {

--- 54 unchanged lines hidden (view full) ---

1197 struct inode *inode;
1198 struct page *page;
1199
1200 /* should flush inline_data before evict_inode */
1201 inode = ilookup(sbi->sb, ino);
1202 if (!inode)
1203 return;
1204
1132 if (!page)
1133 return ERR_PTR(-ENOMEM);
1134
1135 err = read_node_page(page, READ_SYNC);
1136 if (err < 0) {
1137 f2fs_put_page(page, 1);
1138 return ERR_PTR(err);
1139 } else if (err == LOCKED_PAGE) {

--- 54 unchanged lines hidden (view full) ---

1194 struct inode *inode;
1195 struct page *page;
1196
1197 /* should flush inline_data before evict_inode */
1198 inode = ilookup(sbi->sb, ino);
1199 if (!inode)
1200 return;
1201
1205 page = pagecache_get_page(inode->i_mapping, 0, FGP_NOWAIT, 0);
1202 page = pagecache_get_page(inode->i_mapping, 0, FGP_LOCK|FGP_NOWAIT, 0);
1206 if (!page)
1207 goto iput_out;
1208
1203 if (!page)
1204 goto iput_out;
1205
1209 if (!trylock_page(page))
1210 goto release_out;
1211
1212 if (!PageUptodate(page))
1213 goto page_out;
1214
1215 if (!PageDirty(page))
1216 goto page_out;
1217
1218 if (!clear_page_dirty_for_io(page))
1219 goto page_out;
1220
1221 if (!f2fs_write_inline_data(inode, page))
1222 inode_dec_dirty_pages(inode);
1223 else
1224 set_page_dirty(page);
1225page_out:
1206 if (!PageUptodate(page))
1207 goto page_out;
1208
1209 if (!PageDirty(page))
1210 goto page_out;
1211
1212 if (!clear_page_dirty_for_io(page))
1213 goto page_out;
1214
1215 if (!f2fs_write_inline_data(inode, page))
1216 inode_dec_dirty_pages(inode);
1217 else
1218 set_page_dirty(page);
1219page_out:
1226 unlock_page(page);
1227release_out:
1228 f2fs_put_page(page, 0);
1220 f2fs_put_page(page, 1);
1229iput_out:
1230 iput(inode);
1231}
1232
1221iput_out:
1222 iput(inode);
1223}
1224
1233int sync_node_pages(struct f2fs_sb_info *sbi, nid_t ino,
1234 struct writeback_control *wbc)
1225void move_node_page(struct page *node_page, int gc_type)
1235{
1226{
1227 if (gc_type == FG_GC) {
1228 struct f2fs_sb_info *sbi = F2FS_P_SB(node_page);
1229 struct writeback_control wbc = {
1230 .sync_mode = WB_SYNC_ALL,
1231 .nr_to_write = 1,
1232 .for_reclaim = 0,
1233 };
1234
1235 set_page_dirty(node_page);
1236 f2fs_wait_on_page_writeback(node_page, NODE, true);
1237
1238 f2fs_bug_on(sbi, PageWriteback(node_page));
1239 if (!clear_page_dirty_for_io(node_page))
1240 goto out_page;
1241
1242 if (NODE_MAPPING(sbi)->a_ops->writepage(node_page, &wbc))
1243 unlock_page(node_page);
1244 goto release_page;
1245 } else {
1246 /* set page dirty and write it */
1247 if (!PageWriteback(node_page))
1248 set_page_dirty(node_page);
1249 }
1250out_page:
1251 unlock_page(node_page);
1252release_page:
1253 f2fs_put_page(node_page, 0);
1254}
1255
1256static struct page *last_fsync_dnode(struct f2fs_sb_info *sbi, nid_t ino)
1257{
1236 pgoff_t index, end;
1237 struct pagevec pvec;
1258 pgoff_t index, end;
1259 struct pagevec pvec;
1238 int step = ino ? 2 : 0;
1260 struct page *last_page = NULL;
1261
1262 pagevec_init(&pvec, 0);
1263 index = 0;
1264 end = ULONG_MAX;
1265
1266 while (index <= end) {
1267 int i, nr_pages;
1268 nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
1269 PAGECACHE_TAG_DIRTY,
1270 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1271 if (nr_pages == 0)
1272 break;
1273
1274 for (i = 0; i < nr_pages; i++) {
1275 struct page *page = pvec.pages[i];
1276
1277 if (unlikely(f2fs_cp_error(sbi))) {
1278 f2fs_put_page(last_page, 0);
1279 pagevec_release(&pvec);
1280 return ERR_PTR(-EIO);
1281 }
1282
1283 if (!IS_DNODE(page) || !is_cold_node(page))
1284 continue;
1285 if (ino_of_node(page) != ino)
1286 continue;
1287
1288 lock_page(page);
1289
1290 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
1291continue_unlock:
1292 unlock_page(page);
1293 continue;
1294 }
1295 if (ino_of_node(page) != ino)
1296 goto continue_unlock;
1297
1298 if (!PageDirty(page)) {
1299 /* someone wrote it for us */
1300 goto continue_unlock;
1301 }
1302
1303 if (last_page)
1304 f2fs_put_page(last_page, 0);
1305
1306 get_page(page);
1307 last_page = page;
1308 unlock_page(page);
1309 }
1310 pagevec_release(&pvec);
1311 cond_resched();
1312 }
1313 return last_page;
1314}
1315
1316int fsync_node_pages(struct f2fs_sb_info *sbi, nid_t ino,
1317 struct writeback_control *wbc, bool atomic)
1318{
1319 pgoff_t index, end;
1320 struct pagevec pvec;
1321 int ret = 0;
1322 struct page *last_page = NULL;
1323 bool marked = false;
1324
1325 if (atomic) {
1326 last_page = last_fsync_dnode(sbi, ino);
1327 if (IS_ERR_OR_NULL(last_page))
1328 return PTR_ERR_OR_ZERO(last_page);
1329 }
1330retry:
1331 pagevec_init(&pvec, 0);
1332 index = 0;
1333 end = ULONG_MAX;
1334
1335 while (index <= end) {
1336 int i, nr_pages;
1337 nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
1338 PAGECACHE_TAG_DIRTY,
1339 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1340 if (nr_pages == 0)
1341 break;
1342
1343 for (i = 0; i < nr_pages; i++) {
1344 struct page *page = pvec.pages[i];
1345
1346 if (unlikely(f2fs_cp_error(sbi))) {
1347 f2fs_put_page(last_page, 0);
1348 pagevec_release(&pvec);
1349 return -EIO;
1350 }
1351
1352 if (!IS_DNODE(page) || !is_cold_node(page))
1353 continue;
1354 if (ino_of_node(page) != ino)
1355 continue;
1356
1357 lock_page(page);
1358
1359 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
1360continue_unlock:
1361 unlock_page(page);
1362 continue;
1363 }
1364 if (ino_of_node(page) != ino)
1365 goto continue_unlock;
1366
1367 if (!PageDirty(page) && page != last_page) {
1368 /* someone wrote it for us */
1369 goto continue_unlock;
1370 }
1371
1372 f2fs_wait_on_page_writeback(page, NODE, true);
1373 BUG_ON(PageWriteback(page));
1374
1375 if (!atomic || page == last_page) {
1376 set_fsync_mark(page, 1);
1377 if (IS_INODE(page))
1378 set_dentry_mark(page,
1379 need_dentry_mark(sbi, ino));
1380 /* may be written by other thread */
1381 if (!PageDirty(page))
1382 set_page_dirty(page);
1383 }
1384
1385 if (!clear_page_dirty_for_io(page))
1386 goto continue_unlock;
1387
1388 ret = NODE_MAPPING(sbi)->a_ops->writepage(page, wbc);
1389 if (ret) {
1390 unlock_page(page);
1391 f2fs_put_page(last_page, 0);
1392 break;
1393 }
1394 if (page == last_page) {
1395 f2fs_put_page(page, 0);
1396 marked = true;
1397 break;
1398 }
1399 }
1400 pagevec_release(&pvec);
1401 cond_resched();
1402
1403 if (ret || marked)
1404 break;
1405 }
1406 if (!ret && atomic && !marked) {
1407 f2fs_msg(sbi->sb, KERN_DEBUG,
1408 "Retry to write fsync mark: ino=%u, idx=%lx",
1409 ino, last_page->index);
1410 lock_page(last_page);
1411 set_page_dirty(last_page);
1412 unlock_page(last_page);
1413 goto retry;
1414 }
1415 return ret ? -EIO: 0;
1416}
1417
1418int sync_node_pages(struct f2fs_sb_info *sbi, struct writeback_control *wbc)
1419{
1420 pgoff_t index, end;
1421 struct pagevec pvec;
1422 int step = 0;
1239 int nwritten = 0;
1240
1241 pagevec_init(&pvec, 0);
1242
1243next_step:
1244 index = 0;
1245 end = ULONG_MAX;
1246

--- 22 unchanged lines hidden (view full) ---

1269 if (step == 0 && IS_DNODE(page))
1270 continue;
1271 if (step == 1 && (!IS_DNODE(page) ||
1272 is_cold_node(page)))
1273 continue;
1274 if (step == 2 && (!IS_DNODE(page) ||
1275 !is_cold_node(page)))
1276 continue;
1423 int nwritten = 0;
1424
1425 pagevec_init(&pvec, 0);
1426
1427next_step:
1428 index = 0;
1429 end = ULONG_MAX;
1430

--- 22 unchanged lines hidden (view full) ---

1453 if (step == 0 && IS_DNODE(page))
1454 continue;
1455 if (step == 1 && (!IS_DNODE(page) ||
1456 is_cold_node(page)))
1457 continue;
1458 if (step == 2 && (!IS_DNODE(page) ||
1459 !is_cold_node(page)))
1460 continue;
1277
1278 /*
1279 * If an fsync mode,
1280 * we should not skip writing node pages.
1281 */
1282lock_node:
1461lock_node:
1283 if (ino && ino_of_node(page) == ino)
1284 lock_page(page);
1285 else if (!trylock_page(page))
1462 if (!trylock_page(page))
1286 continue;
1287
1288 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
1289continue_unlock:
1290 unlock_page(page);
1291 continue;
1292 }
1463 continue;
1464
1465 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
1466continue_unlock:
1467 unlock_page(page);
1468 continue;
1469 }
1293 if (ino && ino_of_node(page) != ino)
1294 goto continue_unlock;
1295
1296 if (!PageDirty(page)) {
1297 /* someone wrote it for us */
1298 goto continue_unlock;
1299 }
1300
1301 /* flush inline_data */
1470
1471 if (!PageDirty(page)) {
1472 /* someone wrote it for us */
1473 goto continue_unlock;
1474 }
1475
1476 /* flush inline_data */
1302 if (!ino && is_inline_node(page)) {
1477 if (is_inline_node(page)) {
1303 clear_inline_node(page);
1304 unlock_page(page);
1305 flush_inline_data(sbi, ino_of_node(page));
1306 goto lock_node;
1307 }
1308
1309 f2fs_wait_on_page_writeback(page, NODE, true);
1310
1311 BUG_ON(PageWriteback(page));
1312 if (!clear_page_dirty_for_io(page))
1313 goto continue_unlock;
1314
1478 clear_inline_node(page);
1479 unlock_page(page);
1480 flush_inline_data(sbi, ino_of_node(page));
1481 goto lock_node;
1482 }
1483
1484 f2fs_wait_on_page_writeback(page, NODE, true);
1485
1486 BUG_ON(PageWriteback(page));
1487 if (!clear_page_dirty_for_io(page))
1488 goto continue_unlock;
1489
1315 /* called by fsync() */
1316 if (ino && IS_DNODE(page)) {
1317 set_fsync_mark(page, 1);
1318 if (IS_INODE(page))
1319 set_dentry_mark(page,
1320 need_dentry_mark(sbi, ino));
1321 nwritten++;
1322 } else {
1323 set_fsync_mark(page, 0);
1324 set_dentry_mark(page, 0);
1325 }
1490 set_fsync_mark(page, 0);
1491 set_dentry_mark(page, 0);
1326
1327 if (NODE_MAPPING(sbi)->a_ops->writepage(page, wbc))
1328 unlock_page(page);
1329
1330 if (--wbc->nr_to_write == 0)
1331 break;
1332 }
1333 pagevec_release(&pvec);

--- 131 unchanged lines hidden (view full) ---

1465 /* collect a number of dirty node pages and write together */
1466 if (get_pages(sbi, F2FS_DIRTY_NODES) < nr_pages_to_skip(sbi, NODE))
1467 goto skip_write;
1468
1469 trace_f2fs_writepages(mapping->host, wbc, NODE);
1470
1471 diff = nr_pages_to_write(sbi, NODE, wbc);
1472 wbc->sync_mode = WB_SYNC_NONE;
1492
1493 if (NODE_MAPPING(sbi)->a_ops->writepage(page, wbc))
1494 unlock_page(page);
1495
1496 if (--wbc->nr_to_write == 0)
1497 break;
1498 }
1499 pagevec_release(&pvec);

--- 131 unchanged lines hidden (view full) ---

1631 /* collect a number of dirty node pages and write together */
1632 if (get_pages(sbi, F2FS_DIRTY_NODES) < nr_pages_to_skip(sbi, NODE))
1633 goto skip_write;
1634
1635 trace_f2fs_writepages(mapping->host, wbc, NODE);
1636
1637 diff = nr_pages_to_write(sbi, NODE, wbc);
1638 wbc->sync_mode = WB_SYNC_NONE;
1473 sync_node_pages(sbi, 0, wbc);
1639 sync_node_pages(sbi, wbc);
1474 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
1475 return 0;
1476
1477skip_write:
1478 wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_NODES);
1479 trace_f2fs_writepages(mapping->host, wbc, NODE);
1480 return 0;
1481}

--- 359 unchanged lines hidden (view full) ---

1841 struct node_info old_ni, new_ni;
1842 struct page *ipage;
1843
1844 get_node_info(sbi, ino, &old_ni);
1845
1846 if (unlikely(old_ni.blk_addr != NULL_ADDR))
1847 return -EINVAL;
1848
1640 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
1641 return 0;
1642
1643skip_write:
1644 wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_NODES);
1645 trace_f2fs_writepages(mapping->host, wbc, NODE);
1646 return 0;
1647}

--- 359 unchanged lines hidden (view full) ---

2007 struct node_info old_ni, new_ni;
2008 struct page *ipage;
2009
2010 get_node_info(sbi, ino, &old_ni);
2011
2012 if (unlikely(old_ni.blk_addr != NULL_ADDR))
2013 return -EINVAL;
2014
1849 ipage = grab_cache_page(NODE_MAPPING(sbi), ino);
2015 ipage = f2fs_grab_cache_page(NODE_MAPPING(sbi), ino, false);
1850 if (!ipage)
1851 return -ENOMEM;
1852
1853 /* Should not use this inode from free nid list */
1854 remove_free_nid(NM_I(sbi), ino);
1855
1856 SetPageUptodate(ipage);
1857 fill_node_footer(ipage, ino, ino, 0, true);

--- 364 unchanged lines hidden ---
2016 if (!ipage)
2017 return -ENOMEM;
2018
2019 /* Should not use this inode from free nid list */
2020 remove_free_nid(NM_I(sbi), ino);
2021
2022 SetPageUptodate(ipage);
2023 fill_node_footer(ipage, ino, ino, 0, true);

--- 364 unchanged lines hidden ---