checkpoint.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199
  1. /*
  2. * fs/f2fs/checkpoint.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
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/fs.h>
  12. #include <linux/bio.h>
  13. #include <linux/mpage.h>
  14. #include <linux/writeback.h>
  15. #include <linux/blkdev.h>
  16. #include <linux/f2fs_fs.h>
  17. #include <linux/pagevec.h>
  18. #include <linux/swap.h>
  19. #include "f2fs.h"
  20. #include "node.h"
  21. #include "segment.h"
  22. #include "trace.h"
  23. #include <trace/events/f2fs.h>
  24. static struct kmem_cache *ino_entry_slab;
  25. struct kmem_cache *inode_entry_slab;
  26. /*
  27. * We guarantee no failure on the returned page.
  28. */
  29. struct page *grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
  30. {
  31. struct address_space *mapping = META_MAPPING(sbi);
  32. struct page *page = NULL;
  33. repeat:
  34. page = grab_cache_page(mapping, index);
  35. if (!page) {
  36. cond_resched();
  37. goto repeat;
  38. }
  39. f2fs_wait_on_page_writeback(page, META);
  40. SetPageUptodate(page);
  41. return page;
  42. }
  43. /*
  44. * We guarantee no failure on the returned page.
  45. */
  46. static struct page *__get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index,
  47. bool is_meta)
  48. {
  49. struct address_space *mapping = META_MAPPING(sbi);
  50. struct page *page;
  51. struct f2fs_io_info fio = {
  52. .sbi = sbi,
  53. .type = META,
  54. .rw = READ_SYNC | REQ_META | REQ_PRIO,
  55. .blk_addr = index,
  56. .encrypted_page = NULL,
  57. .is_meta = is_meta,
  58. };
  59. if (unlikely(!is_meta))
  60. fio.rw &= ~REQ_META;
  61. repeat:
  62. page = grab_cache_page(mapping, index);
  63. if (!page) {
  64. cond_resched();
  65. goto repeat;
  66. }
  67. if (PageUptodate(page))
  68. goto out;
  69. fio.page = page;
  70. if (f2fs_submit_page_bio(&fio)) {
  71. memset(page_address(page), 0, PAGE_SIZE);
  72. f2fs_stop_checkpoint(sbi);
  73. f2fs_bug_on(sbi, 1);
  74. return page;
  75. }
  76. lock_page(page);
  77. if (unlikely(page->mapping != mapping)) {
  78. f2fs_put_page(page, 1);
  79. goto repeat;
  80. }
  81. /*
  82. * if there is any IO error when accessing device, make our filesystem
  83. * readonly and make sure do not write checkpoint with non-uptodate
  84. * meta page.
  85. */
  86. if (unlikely(!PageUptodate(page)))
  87. f2fs_stop_checkpoint(sbi);
  88. out:
  89. return page;
  90. }
  91. struct page *get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index)
  92. {
  93. return __get_meta_page(sbi, index, true);
  94. }
  95. /* for POR only */
  96. struct page *get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index)
  97. {
  98. return __get_meta_page(sbi, index, false);
  99. }
  100. bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
  101. block_t blkaddr, int type)
  102. {
  103. switch (type) {
  104. case META_NAT:
  105. break;
  106. case META_SIT:
  107. if (unlikely(blkaddr >= SIT_BLK_CNT(sbi)))
  108. return false;
  109. break;
  110. case META_SSA:
  111. if (unlikely(blkaddr >= MAIN_BLKADDR(sbi) ||
  112. blkaddr < SM_I(sbi)->ssa_blkaddr))
  113. return false;
  114. break;
  115. case META_CP:
  116. if (unlikely(blkaddr >= SIT_I(sbi)->sit_base_addr ||
  117. blkaddr < __start_cp_addr(sbi)))
  118. return false;
  119. break;
  120. case META_POR:
  121. case DATA_GENERIC:
  122. if (unlikely(blkaddr >= MAX_BLKADDR(sbi) ||
  123. blkaddr < MAIN_BLKADDR(sbi))) {
  124. if (type == DATA_GENERIC) {
  125. f2fs_msg(sbi->sb, KERN_WARNING,
  126. "access invalid blkaddr:%u", blkaddr);
  127. WARN_ON(1);
  128. }
  129. return false;
  130. }
  131. break;
  132. case META_GENERIC:
  133. if (unlikely(blkaddr < SEG0_BLKADDR(sbi) ||
  134. blkaddr >= MAIN_BLKADDR(sbi)))
  135. return false;
  136. break;
  137. default:
  138. BUG();
  139. }
  140. return true;
  141. }
  142. /*
  143. * Readahead CP/NAT/SIT/SSA pages
  144. */
  145. int ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
  146. int type, bool sync)
  147. {
  148. block_t prev_blk_addr = 0;
  149. struct page *page;
  150. block_t blkno = start;
  151. struct f2fs_io_info fio = {
  152. .sbi = sbi,
  153. .type = META,
  154. .rw = sync ? (READ_SYNC | REQ_META | REQ_PRIO) : READA,
  155. .encrypted_page = NULL,
  156. .is_meta = (type != META_POR),
  157. };
  158. if (unlikely(type == META_POR))
  159. fio.rw &= ~REQ_META;
  160. for (; nrpages-- > 0; blkno++) {
  161. if (!f2fs_is_valid_blkaddr(sbi, blkno, type))
  162. goto out;
  163. switch (type) {
  164. case META_NAT:
  165. if (unlikely(blkno >=
  166. NAT_BLOCK_OFFSET(NM_I(sbi)->max_nid)))
  167. blkno = 0;
  168. /* get nat block addr */
  169. fio.blk_addr = current_nat_addr(sbi,
  170. blkno * NAT_ENTRY_PER_BLOCK);
  171. break;
  172. case META_SIT:
  173. /* get sit block addr */
  174. fio.blk_addr = current_sit_addr(sbi,
  175. blkno * SIT_ENTRY_PER_BLOCK);
  176. if (blkno != start && prev_blk_addr + 1 != fio.blk_addr)
  177. goto out;
  178. prev_blk_addr = fio.blk_addr;
  179. break;
  180. case META_SSA:
  181. case META_CP:
  182. case META_POR:
  183. fio.blk_addr = blkno;
  184. break;
  185. default:
  186. BUG();
  187. }
  188. page = grab_cache_page(META_MAPPING(sbi), fio.blk_addr);
  189. if (!page)
  190. continue;
  191. if (PageUptodate(page)) {
  192. f2fs_put_page(page, 1);
  193. continue;
  194. }
  195. fio.page = page;
  196. f2fs_submit_page_mbio(&fio);
  197. f2fs_put_page(page, 0);
  198. }
  199. out:
  200. f2fs_submit_merged_bio(sbi, META, READ);
  201. return blkno - start;
  202. }
  203. void ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index)
  204. {
  205. struct page *page;
  206. bool readahead = false;
  207. page = find_get_page(META_MAPPING(sbi), index);
  208. if (!page || (page && !PageUptodate(page)))
  209. readahead = true;
  210. f2fs_put_page(page, 0);
  211. if (readahead)
  212. ra_meta_pages(sbi, index, MAX_BIO_BLOCKS(sbi), META_POR, true);
  213. }
  214. static int f2fs_write_meta_page(struct page *page,
  215. struct writeback_control *wbc)
  216. {
  217. struct f2fs_sb_info *sbi = F2FS_P_SB(page);
  218. trace_f2fs_writepage(page, META);
  219. if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
  220. goto redirty_out;
  221. if (wbc->for_reclaim && page->index < GET_SUM_BLOCK(sbi, 0))
  222. goto redirty_out;
  223. if (unlikely(f2fs_cp_error(sbi)))
  224. goto redirty_out;
  225. f2fs_wait_on_page_writeback(page, META);
  226. write_meta_page(sbi, page);
  227. dec_page_count(sbi, F2FS_DIRTY_META);
  228. unlock_page(page);
  229. if (wbc->for_reclaim)
  230. f2fs_submit_merged_bio(sbi, META, WRITE);
  231. return 0;
  232. redirty_out:
  233. redirty_page_for_writepage(wbc, page);
  234. return AOP_WRITEPAGE_ACTIVATE;
  235. }
  236. static int f2fs_write_meta_pages(struct address_space *mapping,
  237. struct writeback_control *wbc)
  238. {
  239. struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
  240. long diff, written;
  241. trace_f2fs_writepages(mapping->host, wbc, META);
  242. /* collect a number of dirty meta pages and write together */
  243. if (wbc->for_kupdate ||
  244. get_pages(sbi, F2FS_DIRTY_META) < nr_pages_to_skip(sbi, META))
  245. goto skip_write;
  246. /* if mounting is failed, skip writing node pages */
  247. mutex_lock(&sbi->cp_mutex);
  248. diff = nr_pages_to_write(sbi, META, wbc);
  249. written = sync_meta_pages(sbi, META, wbc->nr_to_write);
  250. mutex_unlock(&sbi->cp_mutex);
  251. wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff);
  252. return 0;
  253. skip_write:
  254. wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_META);
  255. return 0;
  256. }
  257. long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
  258. long nr_to_write)
  259. {
  260. struct address_space *mapping = META_MAPPING(sbi);
  261. pgoff_t index = 0, end = LONG_MAX, prev = LONG_MAX;
  262. struct pagevec pvec;
  263. long nwritten = 0;
  264. struct writeback_control wbc = {
  265. .for_reclaim = 0,
  266. };
  267. pagevec_init(&pvec, 0);
  268. while (index <= end) {
  269. int i, nr_pages;
  270. nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
  271. PAGECACHE_TAG_DIRTY,
  272. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
  273. if (unlikely(nr_pages == 0))
  274. break;
  275. for (i = 0; i < nr_pages; i++) {
  276. struct page *page = pvec.pages[i];
  277. if (prev == LONG_MAX)
  278. prev = page->index - 1;
  279. if (nr_to_write != LONG_MAX && page->index != prev + 1) {
  280. pagevec_release(&pvec);
  281. goto stop;
  282. }
  283. lock_page(page);
  284. if (unlikely(page->mapping != mapping)) {
  285. continue_unlock:
  286. unlock_page(page);
  287. continue;
  288. }
  289. if (!PageDirty(page)) {
  290. /* someone wrote it for us */
  291. goto continue_unlock;
  292. }
  293. if (!clear_page_dirty_for_io(page))
  294. goto continue_unlock;
  295. if (mapping->a_ops->writepage(page, &wbc)) {
  296. unlock_page(page);
  297. break;
  298. }
  299. nwritten++;
  300. prev = page->index;
  301. if (unlikely(nwritten >= nr_to_write))
  302. break;
  303. }
  304. pagevec_release(&pvec);
  305. cond_resched();
  306. }
  307. stop:
  308. if (nwritten)
  309. f2fs_submit_merged_bio(sbi, type, WRITE);
  310. return nwritten;
  311. }
  312. static int f2fs_set_meta_page_dirty(struct page *page)
  313. {
  314. trace_f2fs_set_page_dirty(page, META);
  315. SetPageUptodate(page);
  316. if (!PageDirty(page)) {
  317. __set_page_dirty_nobuffers(page);
  318. inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_META);
  319. SetPagePrivate(page);
  320. f2fs_trace_pid(page);
  321. return 1;
  322. }
  323. return 0;
  324. }
  325. const struct address_space_operations f2fs_meta_aops = {
  326. .writepage = f2fs_write_meta_page,
  327. .writepages = f2fs_write_meta_pages,
  328. .set_page_dirty = f2fs_set_meta_page_dirty,
  329. .invalidatepage = f2fs_invalidate_page,
  330. .releasepage = f2fs_release_page,
  331. };
  332. static void __add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
  333. {
  334. struct inode_management *im = &sbi->im[type];
  335. struct ino_entry *e, *tmp;
  336. tmp = f2fs_kmem_cache_alloc(ino_entry_slab, GFP_NOFS);
  337. retry:
  338. radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
  339. spin_lock(&im->ino_lock);
  340. e = radix_tree_lookup(&im->ino_root, ino);
  341. if (!e) {
  342. e = tmp;
  343. if (radix_tree_insert(&im->ino_root, ino, e)) {
  344. spin_unlock(&im->ino_lock);
  345. radix_tree_preload_end();
  346. goto retry;
  347. }
  348. memset(e, 0, sizeof(struct ino_entry));
  349. e->ino = ino;
  350. list_add_tail(&e->list, &im->ino_list);
  351. if (type != ORPHAN_INO)
  352. im->ino_num++;
  353. }
  354. spin_unlock(&im->ino_lock);
  355. radix_tree_preload_end();
  356. if (e != tmp)
  357. kmem_cache_free(ino_entry_slab, tmp);
  358. }
  359. static void __remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
  360. {
  361. struct inode_management *im = &sbi->im[type];
  362. struct ino_entry *e;
  363. spin_lock(&im->ino_lock);
  364. e = radix_tree_lookup(&im->ino_root, ino);
  365. if (e) {
  366. list_del(&e->list);
  367. radix_tree_delete(&im->ino_root, ino);
  368. im->ino_num--;
  369. spin_unlock(&im->ino_lock);
  370. kmem_cache_free(ino_entry_slab, e);
  371. return;
  372. }
  373. spin_unlock(&im->ino_lock);
  374. }
  375. void add_dirty_inode(struct f2fs_sb_info *sbi, nid_t ino, int type)
  376. {
  377. /* add new dirty ino entry into list */
  378. __add_ino_entry(sbi, ino, type);
  379. }
  380. void remove_dirty_inode(struct f2fs_sb_info *sbi, nid_t ino, int type)
  381. {
  382. /* remove dirty ino entry from list */
  383. __remove_ino_entry(sbi, ino, type);
  384. }
  385. /* mode should be APPEND_INO or UPDATE_INO */
  386. bool exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode)
  387. {
  388. struct inode_management *im = &sbi->im[mode];
  389. struct ino_entry *e;
  390. spin_lock(&im->ino_lock);
  391. e = radix_tree_lookup(&im->ino_root, ino);
  392. spin_unlock(&im->ino_lock);
  393. return e ? true : false;
  394. }
  395. void release_dirty_inode(struct f2fs_sb_info *sbi)
  396. {
  397. struct ino_entry *e, *tmp;
  398. int i;
  399. for (i = APPEND_INO; i <= UPDATE_INO; i++) {
  400. struct inode_management *im = &sbi->im[i];
  401. spin_lock(&im->ino_lock);
  402. list_for_each_entry_safe(e, tmp, &im->ino_list, list) {
  403. list_del(&e->list);
  404. radix_tree_delete(&im->ino_root, e->ino);
  405. kmem_cache_free(ino_entry_slab, e);
  406. im->ino_num--;
  407. }
  408. spin_unlock(&im->ino_lock);
  409. }
  410. }
  411. int acquire_orphan_inode(struct f2fs_sb_info *sbi)
  412. {
  413. struct inode_management *im = &sbi->im[ORPHAN_INO];
  414. int err = 0;
  415. spin_lock(&im->ino_lock);
  416. if (unlikely(im->ino_num >= sbi->max_orphans))
  417. err = -ENOSPC;
  418. else
  419. im->ino_num++;
  420. spin_unlock(&im->ino_lock);
  421. return err;
  422. }
  423. void release_orphan_inode(struct f2fs_sb_info *sbi)
  424. {
  425. struct inode_management *im = &sbi->im[ORPHAN_INO];
  426. spin_lock(&im->ino_lock);
  427. f2fs_bug_on(sbi, im->ino_num == 0);
  428. im->ino_num--;
  429. spin_unlock(&im->ino_lock);
  430. }
  431. void add_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
  432. {
  433. /* add new orphan ino entry into list */
  434. __add_ino_entry(sbi, ino, ORPHAN_INO);
  435. }
  436. void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
  437. {
  438. /* remove orphan entry from orphan list */
  439. __remove_ino_entry(sbi, ino, ORPHAN_INO);
  440. }
  441. static int recover_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
  442. {
  443. struct inode *inode;
  444. inode = f2fs_iget(sbi->sb, ino);
  445. if (IS_ERR(inode)) {
  446. /*
  447. * there should be a bug that we can't find the entry
  448. * to orphan inode.
  449. */
  450. f2fs_bug_on(sbi, PTR_ERR(inode) == -ENOENT);
  451. return PTR_ERR(inode);
  452. }
  453. clear_nlink(inode);
  454. /* truncate all the data during iput */
  455. iput(inode);
  456. return 0;
  457. }
  458. int recover_orphan_inodes(struct f2fs_sb_info *sbi)
  459. {
  460. block_t start_blk, orphan_blocks, i, j;
  461. int err;
  462. if (!is_set_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG))
  463. return 0;
  464. start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi);
  465. orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi);
  466. ra_meta_pages(sbi, start_blk, orphan_blocks, META_CP, true);
  467. for (i = 0; i < orphan_blocks; i++) {
  468. struct page *page = get_meta_page(sbi, start_blk + i);
  469. struct f2fs_orphan_block *orphan_blk;
  470. orphan_blk = (struct f2fs_orphan_block *)page_address(page);
  471. for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) {
  472. nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
  473. err = recover_orphan_inode(sbi, ino);
  474. if (err) {
  475. f2fs_put_page(page, 1);
  476. return err;
  477. }
  478. }
  479. f2fs_put_page(page, 1);
  480. }
  481. /* clear Orphan Flag */
  482. clear_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG);
  483. return 0;
  484. }
  485. static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk)
  486. {
  487. struct list_head *head;
  488. struct f2fs_orphan_block *orphan_blk = NULL;
  489. unsigned int nentries = 0;
  490. unsigned short index = 1;
  491. unsigned short orphan_blocks;
  492. struct page *page = NULL;
  493. struct ino_entry *orphan = NULL;
  494. struct inode_management *im = &sbi->im[ORPHAN_INO];
  495. orphan_blocks = GET_ORPHAN_BLOCKS(im->ino_num);
  496. /*
  497. * we don't need to do spin_lock(&im->ino_lock) here, since all the
  498. * orphan inode operations are covered under f2fs_lock_op().
  499. * And, spin_lock should be avoided due to page operations below.
  500. */
  501. head = &im->ino_list;
  502. /* loop for each orphan inode entry and write them in Jornal block */
  503. list_for_each_entry(orphan, head, list) {
  504. if (!page) {
  505. page = grab_meta_page(sbi, start_blk++);
  506. orphan_blk =
  507. (struct f2fs_orphan_block *)page_address(page);
  508. memset(orphan_blk, 0, sizeof(*orphan_blk));
  509. }
  510. orphan_blk->ino[nentries++] = cpu_to_le32(orphan->ino);
  511. if (nentries == F2FS_ORPHANS_PER_BLOCK) {
  512. /*
  513. * an orphan block is full of 1020 entries,
  514. * then we need to flush current orphan blocks
  515. * and bring another one in memory
  516. */
  517. orphan_blk->blk_addr = cpu_to_le16(index);
  518. orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
  519. orphan_blk->entry_count = cpu_to_le32(nentries);
  520. set_page_dirty(page);
  521. f2fs_put_page(page, 1);
  522. index++;
  523. nentries = 0;
  524. page = NULL;
  525. }
  526. }
  527. if (page) {
  528. orphan_blk->blk_addr = cpu_to_le16(index);
  529. orphan_blk->blk_count = cpu_to_le16(orphan_blocks);
  530. orphan_blk->entry_count = cpu_to_le32(nentries);
  531. set_page_dirty(page);
  532. f2fs_put_page(page, 1);
  533. }
  534. }
  535. static int get_checkpoint_version(struct f2fs_sb_info *sbi, block_t cp_addr,
  536. struct f2fs_checkpoint **cp_block, struct page **cp_page,
  537. unsigned long long *version)
  538. {
  539. unsigned long blk_size = sbi->blocksize;
  540. size_t crc_offset = 0;
  541. __u32 crc = 0;
  542. *cp_page = get_meta_page(sbi, cp_addr);
  543. *cp_block = (struct f2fs_checkpoint *)page_address(*cp_page);
  544. crc_offset = le32_to_cpu((*cp_block)->checksum_offset);
  545. if (crc_offset >= blk_size) {
  546. f2fs_put_page(*cp_page, 1);
  547. f2fs_msg(sbi->sb, KERN_WARNING,
  548. "invalid crc_offset: %zu", crc_offset);
  549. return -EINVAL;
  550. }
  551. crc = le32_to_cpu(*((__le32 *)((unsigned char *)*cp_block
  552. + crc_offset)));
  553. if (!f2fs_crc_valid(crc, *cp_block, crc_offset)) {
  554. f2fs_put_page(*cp_page, 1);
  555. f2fs_msg(sbi->sb, KERN_WARNING, "invalid crc value");
  556. return -EINVAL;
  557. }
  558. *version = cur_cp_version(*cp_block);
  559. return 0;
  560. }
  561. static struct page *validate_checkpoint(struct f2fs_sb_info *sbi,
  562. block_t cp_addr, unsigned long long *version)
  563. {
  564. struct page *cp_page_1 = NULL, *cp_page_2 = NULL;
  565. struct f2fs_checkpoint *cp_block = NULL;
  566. unsigned long long cur_version = 0, pre_version = 0;
  567. int err;
  568. err = get_checkpoint_version(sbi, cp_addr, &cp_block,
  569. &cp_page_1, version);
  570. if (err)
  571. return NULL;
  572. if (le32_to_cpu(cp_block->cp_pack_total_block_count) >
  573. sbi->blocks_per_seg) {
  574. f2fs_msg(sbi->sb, KERN_WARNING,
  575. "invalid cp_pack_total_block_count:%u",
  576. le32_to_cpu(cp_block->cp_pack_total_block_count));
  577. goto invalid_cp;
  578. }
  579. pre_version = *version;
  580. cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1;
  581. err = get_checkpoint_version(sbi, cp_addr, &cp_block,
  582. &cp_page_2, version);
  583. if (err)
  584. goto invalid_cp;
  585. cur_version = *version;
  586. if (cur_version == pre_version) {
  587. *version = cur_version;
  588. f2fs_put_page(cp_page_2, 1);
  589. return cp_page_1;
  590. }
  591. f2fs_put_page(cp_page_2, 1);
  592. invalid_cp:
  593. f2fs_put_page(cp_page_1, 1);
  594. return NULL;
  595. }
  596. int get_valid_checkpoint(struct f2fs_sb_info *sbi)
  597. {
  598. struct f2fs_checkpoint *cp_block;
  599. struct f2fs_super_block *fsb = sbi->raw_super;
  600. struct page *cp1, *cp2, *cur_page;
  601. unsigned long blk_size = sbi->blocksize;
  602. unsigned long long cp1_version = 0, cp2_version = 0;
  603. unsigned long long cp_start_blk_no;
  604. unsigned int cp_blks = 1 + __cp_payload(sbi);
  605. block_t cp_blk_no;
  606. int i;
  607. sbi->ckpt = kzalloc(cp_blks * blk_size, GFP_KERNEL);
  608. if (!sbi->ckpt)
  609. return -ENOMEM;
  610. /*
  611. * Finding out valid cp block involves read both
  612. * sets( cp pack1 and cp pack 2)
  613. */
  614. cp_start_blk_no = le32_to_cpu(fsb->cp_blkaddr);
  615. cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version);
  616. /* The second checkpoint pack should start at the next segment */
  617. cp_start_blk_no += ((unsigned long long)1) <<
  618. le32_to_cpu(fsb->log_blocks_per_seg);
  619. cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version);
  620. if (cp1 && cp2) {
  621. if (ver_after(cp2_version, cp1_version))
  622. cur_page = cp2;
  623. else
  624. cur_page = cp1;
  625. } else if (cp1) {
  626. cur_page = cp1;
  627. } else if (cp2) {
  628. cur_page = cp2;
  629. } else {
  630. goto fail_no_cp;
  631. }
  632. cp_block = (struct f2fs_checkpoint *)page_address(cur_page);
  633. memcpy(sbi->ckpt, cp_block, blk_size);
  634. if (cur_page == cp1)
  635. sbi->cur_cp_pack = 1;
  636. else
  637. sbi->cur_cp_pack = 2;
  638. /* Sanity checking of checkpoint */
  639. if (sanity_check_ckpt(sbi))
  640. goto free_fail_no_cp;
  641. if (cp_blks <= 1)
  642. goto done;
  643. cp_blk_no = le32_to_cpu(fsb->cp_blkaddr);
  644. if (cur_page == cp2)
  645. cp_blk_no += 1 << le32_to_cpu(fsb->log_blocks_per_seg);
  646. for (i = 1; i < cp_blks; i++) {
  647. void *sit_bitmap_ptr;
  648. unsigned char *ckpt = (unsigned char *)sbi->ckpt;
  649. cur_page = get_meta_page(sbi, cp_blk_no + i);
  650. sit_bitmap_ptr = page_address(cur_page);
  651. memcpy(ckpt + i * blk_size, sit_bitmap_ptr, blk_size);
  652. f2fs_put_page(cur_page, 1);
  653. }
  654. done:
  655. f2fs_put_page(cp1, 1);
  656. f2fs_put_page(cp2, 1);
  657. return 0;
  658. free_fail_no_cp:
  659. f2fs_put_page(cp1, 1);
  660. f2fs_put_page(cp2, 1);
  661. fail_no_cp:
  662. kfree(sbi->ckpt);
  663. return -EINVAL;
  664. }
  665. static int __add_dirty_inode(struct inode *inode, struct inode_entry *new)
  666. {
  667. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  668. if (is_inode_flag_set(F2FS_I(inode), FI_DIRTY_DIR))
  669. return -EEXIST;
  670. set_inode_flag(F2FS_I(inode), FI_DIRTY_DIR);
  671. F2FS_I(inode)->dirty_dir = new;
  672. list_add_tail(&new->list, &sbi->dir_inode_list);
  673. stat_inc_dirty_dir(sbi);
  674. return 0;
  675. }
  676. void update_dirty_page(struct inode *inode, struct page *page)
  677. {
  678. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  679. struct inode_entry *new;
  680. int ret = 0;
  681. if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
  682. !S_ISLNK(inode->i_mode))
  683. return;
  684. if (!S_ISDIR(inode->i_mode)) {
  685. inode_inc_dirty_pages(inode);
  686. goto out;
  687. }
  688. new = f2fs_kmem_cache_alloc(inode_entry_slab, GFP_NOFS);
  689. new->inode = inode;
  690. INIT_LIST_HEAD(&new->list);
  691. spin_lock(&sbi->dir_inode_lock);
  692. ret = __add_dirty_inode(inode, new);
  693. inode_inc_dirty_pages(inode);
  694. spin_unlock(&sbi->dir_inode_lock);
  695. if (ret)
  696. kmem_cache_free(inode_entry_slab, new);
  697. out:
  698. SetPagePrivate(page);
  699. f2fs_trace_pid(page);
  700. }
  701. void remove_dirty_dir_inode(struct inode *inode)
  702. {
  703. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  704. struct inode_entry *entry;
  705. if (!S_ISDIR(inode->i_mode))
  706. return;
  707. spin_lock(&sbi->dir_inode_lock);
  708. if (get_dirty_pages(inode) ||
  709. !is_inode_flag_set(F2FS_I(inode), FI_DIRTY_DIR)) {
  710. spin_unlock(&sbi->dir_inode_lock);
  711. return;
  712. }
  713. entry = F2FS_I(inode)->dirty_dir;
  714. list_del(&entry->list);
  715. F2FS_I(inode)->dirty_dir = NULL;
  716. clear_inode_flag(F2FS_I(inode), FI_DIRTY_DIR);
  717. stat_dec_dirty_dir(sbi);
  718. spin_unlock(&sbi->dir_inode_lock);
  719. kmem_cache_free(inode_entry_slab, entry);
  720. }
  721. void sync_dirty_dir_inodes(struct f2fs_sb_info *sbi)
  722. {
  723. struct list_head *head;
  724. struct inode_entry *entry;
  725. struct inode *inode;
  726. retry:
  727. if (unlikely(f2fs_cp_error(sbi)))
  728. return;
  729. spin_lock(&sbi->dir_inode_lock);
  730. head = &sbi->dir_inode_list;
  731. if (list_empty(head)) {
  732. spin_unlock(&sbi->dir_inode_lock);
  733. return;
  734. }
  735. entry = list_entry(head->next, struct inode_entry, list);
  736. inode = igrab(entry->inode);
  737. spin_unlock(&sbi->dir_inode_lock);
  738. if (inode) {
  739. filemap_fdatawrite(inode->i_mapping);
  740. iput(inode);
  741. } else {
  742. /*
  743. * We should submit bio, since it exists several
  744. * wribacking dentry pages in the freeing inode.
  745. */
  746. f2fs_submit_merged_bio(sbi, DATA, WRITE);
  747. cond_resched();
  748. }
  749. goto retry;
  750. }
  751. /*
  752. * Freeze all the FS-operations for checkpoint.
  753. */
  754. static int block_operations(struct f2fs_sb_info *sbi)
  755. {
  756. struct writeback_control wbc = {
  757. .sync_mode = WB_SYNC_ALL,
  758. .nr_to_write = LONG_MAX,
  759. .for_reclaim = 0,
  760. };
  761. struct blk_plug plug;
  762. int err = 0;
  763. blk_start_plug(&plug);
  764. retry_flush_dents:
  765. f2fs_lock_all(sbi);
  766. /* write all the dirty dentry pages */
  767. if (get_pages(sbi, F2FS_DIRTY_DENTS)) {
  768. f2fs_unlock_all(sbi);
  769. sync_dirty_dir_inodes(sbi);
  770. if (unlikely(f2fs_cp_error(sbi))) {
  771. err = -EIO;
  772. goto out;
  773. }
  774. goto retry_flush_dents;
  775. }
  776. /*
  777. * POR: we should ensure that there are no dirty node pages
  778. * until finishing nat/sit flush.
  779. */
  780. retry_flush_nodes:
  781. down_write(&sbi->node_write);
  782. if (get_pages(sbi, F2FS_DIRTY_NODES)) {
  783. up_write(&sbi->node_write);
  784. sync_node_pages(sbi, 0, &wbc);
  785. if (unlikely(f2fs_cp_error(sbi))) {
  786. f2fs_unlock_all(sbi);
  787. err = -EIO;
  788. goto out;
  789. }
  790. goto retry_flush_nodes;
  791. }
  792. out:
  793. blk_finish_plug(&plug);
  794. return err;
  795. }
  796. static void unblock_operations(struct f2fs_sb_info *sbi)
  797. {
  798. up_write(&sbi->node_write);
  799. f2fs_unlock_all(sbi);
  800. }
  801. static void wait_on_all_pages_writeback(struct f2fs_sb_info *sbi)
  802. {
  803. DEFINE_WAIT(wait);
  804. for (;;) {
  805. prepare_to_wait(&sbi->cp_wait, &wait, TASK_UNINTERRUPTIBLE);
  806. if (!get_pages(sbi, F2FS_WRITEBACK))
  807. break;
  808. io_schedule();
  809. }
  810. finish_wait(&sbi->cp_wait, &wait);
  811. }
  812. static void do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
  813. {
  814. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  815. struct f2fs_nm_info *nm_i = NM_I(sbi);
  816. unsigned long orphan_num = sbi->im[ORPHAN_INO].ino_num;
  817. nid_t last_nid = nm_i->next_scan_nid;
  818. block_t start_blk;
  819. unsigned int data_sum_blocks, orphan_blocks;
  820. __u32 crc32 = 0;
  821. int i;
  822. int cp_payload_blks = __cp_payload(sbi);
  823. /* Flush all the NAT/SIT pages */
  824. while (get_pages(sbi, F2FS_DIRTY_META)) {
  825. sync_meta_pages(sbi, META, LONG_MAX);
  826. if (unlikely(f2fs_cp_error(sbi)))
  827. return;
  828. }
  829. next_free_nid(sbi, &last_nid);
  830. /*
  831. * modify checkpoint
  832. * version number is already updated
  833. */
  834. ckpt->elapsed_time = cpu_to_le64(get_mtime(sbi));
  835. ckpt->valid_block_count = cpu_to_le64(valid_user_blocks(sbi));
  836. ckpt->free_segment_count = cpu_to_le32(free_segments(sbi));
  837. for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
  838. ckpt->cur_node_segno[i] =
  839. cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_NODE));
  840. ckpt->cur_node_blkoff[i] =
  841. cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_NODE));
  842. ckpt->alloc_type[i + CURSEG_HOT_NODE] =
  843. curseg_alloc_type(sbi, i + CURSEG_HOT_NODE);
  844. }
  845. for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
  846. ckpt->cur_data_segno[i] =
  847. cpu_to_le32(curseg_segno(sbi, i + CURSEG_HOT_DATA));
  848. ckpt->cur_data_blkoff[i] =
  849. cpu_to_le16(curseg_blkoff(sbi, i + CURSEG_HOT_DATA));
  850. ckpt->alloc_type[i + CURSEG_HOT_DATA] =
  851. curseg_alloc_type(sbi, i + CURSEG_HOT_DATA);
  852. }
  853. ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi));
  854. ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi));
  855. ckpt->next_free_nid = cpu_to_le32(last_nid);
  856. /* 2 cp + n data seg summary + orphan inode blocks */
  857. data_sum_blocks = npages_for_summary_flush(sbi, false);
  858. if (data_sum_blocks < NR_CURSEG_DATA_TYPE)
  859. set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
  860. else
  861. clear_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
  862. orphan_blocks = GET_ORPHAN_BLOCKS(orphan_num);
  863. ckpt->cp_pack_start_sum = cpu_to_le32(1 + cp_payload_blks +
  864. orphan_blocks);
  865. if (__remain_node_summaries(cpc->reason))
  866. ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS+
  867. cp_payload_blks + data_sum_blocks +
  868. orphan_blocks + NR_CURSEG_NODE_TYPE);
  869. else
  870. ckpt->cp_pack_total_block_count = cpu_to_le32(F2FS_CP_PACKS +
  871. cp_payload_blks + data_sum_blocks +
  872. orphan_blocks);
  873. if (cpc->reason == CP_UMOUNT)
  874. set_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
  875. else
  876. clear_ckpt_flags(ckpt, CP_UMOUNT_FLAG);
  877. if (cpc->reason == CP_FASTBOOT)
  878. set_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
  879. else
  880. clear_ckpt_flags(ckpt, CP_FASTBOOT_FLAG);
  881. if (orphan_num)
  882. set_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
  883. else
  884. clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
  885. if (is_sbi_flag_set(sbi, SBI_NEED_FSCK))
  886. set_ckpt_flags(ckpt, CP_FSCK_FLAG);
  887. /* set this flag to activate crc|cp_ver for recovery */
  888. set_ckpt_flags(ckpt, CP_CRC_RECOVERY_FLAG);
  889. /* update SIT/NAT bitmap */
  890. get_sit_bitmap(sbi, __bitmap_ptr(sbi, SIT_BITMAP));
  891. get_nat_bitmap(sbi, __bitmap_ptr(sbi, NAT_BITMAP));
  892. crc32 = f2fs_crc32(ckpt, le32_to_cpu(ckpt->checksum_offset));
  893. *((__le32 *)((unsigned char *)ckpt +
  894. le32_to_cpu(ckpt->checksum_offset)))
  895. = cpu_to_le32(crc32);
  896. start_blk = __start_cp_next_addr(sbi);
  897. /* need to wait for end_io results */
  898. wait_on_all_pages_writeback(sbi);
  899. if (unlikely(f2fs_cp_error(sbi)))
  900. return;
  901. /* write out checkpoint buffer at block 0 */
  902. update_meta_page(sbi, ckpt, start_blk++);
  903. for (i = 1; i < 1 + cp_payload_blks; i++)
  904. update_meta_page(sbi, (char *)ckpt + i * F2FS_BLKSIZE,
  905. start_blk++);
  906. if (orphan_num) {
  907. write_orphan_inodes(sbi, start_blk);
  908. start_blk += orphan_blocks;
  909. }
  910. write_data_summaries(sbi, start_blk);
  911. start_blk += data_sum_blocks;
  912. if (__remain_node_summaries(cpc->reason)) {
  913. write_node_summaries(sbi, start_blk);
  914. start_blk += NR_CURSEG_NODE_TYPE;
  915. }
  916. /* writeout checkpoint block */
  917. update_meta_page(sbi, ckpt, start_blk);
  918. /* wait for previous submitted node/meta pages writeback */
  919. wait_on_all_pages_writeback(sbi);
  920. if (unlikely(f2fs_cp_error(sbi)))
  921. return;
  922. filemap_fdatawait_range(NODE_MAPPING(sbi), 0, LONG_MAX);
  923. filemap_fdatawait_range(META_MAPPING(sbi), 0, LONG_MAX);
  924. /* update user_block_counts */
  925. sbi->last_valid_block_count = sbi->total_valid_block_count;
  926. sbi->alloc_valid_block_count = 0;
  927. /* Here, we only have one bio having CP pack */
  928. sync_meta_pages(sbi, META_FLUSH, LONG_MAX);
  929. /* wait for previous submitted meta pages writeback */
  930. wait_on_all_pages_writeback(sbi);
  931. release_dirty_inode(sbi);
  932. if (unlikely(f2fs_cp_error(sbi)))
  933. return;
  934. clear_prefree_segments(sbi, cpc);
  935. clear_sbi_flag(sbi, SBI_IS_DIRTY);
  936. __set_cp_next_pack(sbi);
  937. }
  938. /*
  939. * We guarantee that this checkpoint procedure will not fail.
  940. */
  941. void write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
  942. {
  943. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  944. unsigned long long ckpt_ver;
  945. mutex_lock(&sbi->cp_mutex);
  946. if (!is_sbi_flag_set(sbi, SBI_IS_DIRTY) &&
  947. (cpc->reason == CP_FASTBOOT || cpc->reason == CP_SYNC ||
  948. (cpc->reason == CP_DISCARD && !sbi->discard_blks)))
  949. goto out;
  950. if (unlikely(f2fs_cp_error(sbi)))
  951. goto out;
  952. if (f2fs_readonly(sbi->sb))
  953. goto out;
  954. trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "start block_ops");
  955. if (block_operations(sbi))
  956. goto out;
  957. trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish block_ops");
  958. f2fs_submit_merged_bio(sbi, DATA, WRITE);
  959. f2fs_submit_merged_bio(sbi, NODE, WRITE);
  960. f2fs_submit_merged_bio(sbi, META, WRITE);
  961. /*
  962. * update checkpoint pack index
  963. * Increase the version number so that
  964. * SIT entries and seg summaries are written at correct place
  965. */
  966. ckpt_ver = cur_cp_version(ckpt);
  967. ckpt->checkpoint_ver = cpu_to_le64(++ckpt_ver);
  968. /* write cached NAT/SIT entries to NAT/SIT area */
  969. flush_nat_entries(sbi);
  970. flush_sit_entries(sbi, cpc);
  971. /* unlock all the fs_lock[] in do_checkpoint() */
  972. do_checkpoint(sbi, cpc);
  973. unblock_operations(sbi);
  974. stat_inc_cp_count(sbi->stat_info);
  975. if (cpc->reason == CP_RECOVERY)
  976. f2fs_msg(sbi->sb, KERN_NOTICE,
  977. "checkpoint: version = %llx", ckpt_ver);
  978. /* do checkpoint periodically */
  979. sbi->cp_expires = round_jiffies_up(jiffies + HZ * sbi->cp_interval);
  980. out:
  981. mutex_unlock(&sbi->cp_mutex);
  982. trace_f2fs_write_checkpoint(sbi->sb, cpc->reason, "finish checkpoint");
  983. }
  984. void init_ino_entry_info(struct f2fs_sb_info *sbi)
  985. {
  986. int i;
  987. for (i = 0; i < MAX_INO_ENTRY; i++) {
  988. struct inode_management *im = &sbi->im[i];
  989. INIT_RADIX_TREE(&im->ino_root, GFP_ATOMIC);
  990. spin_lock_init(&im->ino_lock);
  991. INIT_LIST_HEAD(&im->ino_list);
  992. im->ino_num = 0;
  993. }
  994. sbi->max_orphans = (sbi->blocks_per_seg - F2FS_CP_PACKS -
  995. NR_CURSEG_TYPE - __cp_payload(sbi)) *
  996. F2FS_ORPHANS_PER_BLOCK;
  997. }
  998. int __init create_checkpoint_caches(void)
  999. {
  1000. ino_entry_slab = f2fs_kmem_cache_create("f2fs_ino_entry",
  1001. sizeof(struct ino_entry));
  1002. if (!ino_entry_slab)
  1003. return -ENOMEM;
  1004. inode_entry_slab = f2fs_kmem_cache_create("f2fs_inode_entry",
  1005. sizeof(struct inode_entry));
  1006. if (!inode_entry_slab) {
  1007. kmem_cache_destroy(ino_entry_slab);
  1008. return -ENOMEM;
  1009. }
  1010. return 0;
  1011. }
  1012. void destroy_checkpoint_caches(void)
  1013. {
  1014. kmem_cache_destroy(ino_entry_slab);
  1015. kmem_cache_destroy(inode_entry_slab);
  1016. }