node.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132
  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
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/fs.h>
  12. #include <linux/f2fs_fs.h>
  13. #include <linux/mpage.h>
  14. #include <linux/backing-dev.h>
  15. #include <linux/blkdev.h>
  16. #include <linux/pagevec.h>
  17. #include <linux/swap.h>
  18. #include "f2fs.h"
  19. #include "node.h"
  20. #include "segment.h"
  21. #include "trace.h"
  22. #include <trace/events/f2fs.h>
  23. #define on_build_free_nids(nmi) mutex_is_locked(&nm_i->build_lock)
  24. static struct kmem_cache *nat_entry_slab;
  25. static struct kmem_cache *free_nid_slab;
  26. static struct kmem_cache *nat_entry_set_slab;
  27. bool available_free_memory(struct f2fs_sb_info *sbi, int type)
  28. {
  29. struct f2fs_nm_info *nm_i = NM_I(sbi);
  30. struct sysinfo val;
  31. unsigned long avail_ram;
  32. unsigned long mem_size = 0;
  33. bool res = false;
  34. si_meminfo(&val);
  35. /* only uses low memory */
  36. avail_ram = val.totalram - val.totalhigh;
  37. /*
  38. * give 25%, 25%, 50%, 50%, 50% memory for each components respectively
  39. */
  40. if (type == FREE_NIDS) {
  41. mem_size = (nm_i->fcnt * sizeof(struct free_nid)) >>
  42. PAGE_CACHE_SHIFT;
  43. res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2);
  44. } else if (type == NAT_ENTRIES) {
  45. mem_size = (nm_i->nat_cnt * sizeof(struct nat_entry)) >>
  46. PAGE_CACHE_SHIFT;
  47. res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2);
  48. } else if (type == DIRTY_DENTS) {
  49. if (sbi->sb->s_bdi->wb.dirty_exceeded)
  50. return false;
  51. mem_size = get_pages(sbi, F2FS_DIRTY_DENTS);
  52. res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
  53. } else if (type == INO_ENTRIES) {
  54. int i;
  55. for (i = 0; i <= UPDATE_INO; i++)
  56. mem_size += (sbi->im[i].ino_num *
  57. sizeof(struct ino_entry)) >> PAGE_CACHE_SHIFT;
  58. res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
  59. } else if (type == EXTENT_CACHE) {
  60. mem_size = (atomic_read(&sbi->total_ext_tree) *
  61. sizeof(struct extent_tree) +
  62. atomic_read(&sbi->total_ext_node) *
  63. sizeof(struct extent_node)) >> PAGE_CACHE_SHIFT;
  64. res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
  65. } else {
  66. if (!sbi->sb->s_bdi->wb.dirty_exceeded)
  67. return true;
  68. }
  69. return res;
  70. }
  71. static void clear_node_page_dirty(struct page *page)
  72. {
  73. struct address_space *mapping = page->mapping;
  74. unsigned int long flags;
  75. if (PageDirty(page)) {
  76. spin_lock_irqsave(&mapping->tree_lock, flags);
  77. radix_tree_tag_clear(&mapping->page_tree,
  78. page_index(page),
  79. PAGECACHE_TAG_DIRTY);
  80. spin_unlock_irqrestore(&mapping->tree_lock, flags);
  81. clear_page_dirty_for_io(page);
  82. dec_page_count(F2FS_M_SB(mapping), F2FS_DIRTY_NODES);
  83. }
  84. ClearPageUptodate(page);
  85. }
  86. static struct page *get_current_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
  87. {
  88. pgoff_t index = current_nat_addr(sbi, nid);
  89. return get_meta_page(sbi, index);
  90. }
  91. static struct page *get_next_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
  92. {
  93. struct page *src_page;
  94. struct page *dst_page;
  95. pgoff_t src_off;
  96. pgoff_t dst_off;
  97. void *src_addr;
  98. void *dst_addr;
  99. struct f2fs_nm_info *nm_i = NM_I(sbi);
  100. src_off = current_nat_addr(sbi, nid);
  101. dst_off = next_nat_addr(sbi, src_off);
  102. /* get current nat block page with lock */
  103. src_page = get_meta_page(sbi, src_off);
  104. dst_page = grab_meta_page(sbi, dst_off);
  105. f2fs_bug_on(sbi, PageDirty(src_page));
  106. src_addr = page_address(src_page);
  107. dst_addr = page_address(dst_page);
  108. memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
  109. set_page_dirty(dst_page);
  110. f2fs_put_page(src_page, 1);
  111. set_to_next_nat(nm_i, nid);
  112. return dst_page;
  113. }
  114. static struct nat_entry *__lookup_nat_cache(struct f2fs_nm_info *nm_i, nid_t n)
  115. {
  116. return radix_tree_lookup(&nm_i->nat_root, n);
  117. }
  118. static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info *nm_i,
  119. nid_t start, unsigned int nr, struct nat_entry **ep)
  120. {
  121. return radix_tree_gang_lookup(&nm_i->nat_root, (void **)ep, start, nr);
  122. }
  123. static void __del_from_nat_cache(struct f2fs_nm_info *nm_i, struct nat_entry *e)
  124. {
  125. list_del(&e->list);
  126. radix_tree_delete(&nm_i->nat_root, nat_get_nid(e));
  127. nm_i->nat_cnt--;
  128. kmem_cache_free(nat_entry_slab, e);
  129. }
  130. static void __set_nat_cache_dirty(struct f2fs_nm_info *nm_i,
  131. struct nat_entry *ne)
  132. {
  133. nid_t set = NAT_BLOCK_OFFSET(ne->ni.nid);
  134. struct nat_entry_set *head;
  135. if (get_nat_flag(ne, IS_DIRTY))
  136. return;
  137. head = radix_tree_lookup(&nm_i->nat_set_root, set);
  138. if (!head) {
  139. head = f2fs_kmem_cache_alloc(nat_entry_set_slab, GFP_NOFS);
  140. INIT_LIST_HEAD(&head->entry_list);
  141. INIT_LIST_HEAD(&head->set_list);
  142. head->set = set;
  143. head->entry_cnt = 0;
  144. f2fs_radix_tree_insert(&nm_i->nat_set_root, set, head);
  145. }
  146. list_move_tail(&ne->list, &head->entry_list);
  147. nm_i->dirty_nat_cnt++;
  148. head->entry_cnt++;
  149. set_nat_flag(ne, IS_DIRTY, true);
  150. }
  151. static void __clear_nat_cache_dirty(struct f2fs_nm_info *nm_i,
  152. struct nat_entry *ne)
  153. {
  154. nid_t set = NAT_BLOCK_OFFSET(ne->ni.nid);
  155. struct nat_entry_set *head;
  156. head = radix_tree_lookup(&nm_i->nat_set_root, set);
  157. if (head) {
  158. list_move_tail(&ne->list, &nm_i->nat_entries);
  159. set_nat_flag(ne, IS_DIRTY, false);
  160. head->entry_cnt--;
  161. nm_i->dirty_nat_cnt--;
  162. }
  163. }
  164. static unsigned int __gang_lookup_nat_set(struct f2fs_nm_info *nm_i,
  165. nid_t start, unsigned int nr, struct nat_entry_set **ep)
  166. {
  167. return radix_tree_gang_lookup(&nm_i->nat_set_root, (void **)ep,
  168. start, nr);
  169. }
  170. int need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid)
  171. {
  172. struct f2fs_nm_info *nm_i = NM_I(sbi);
  173. struct nat_entry *e;
  174. bool need = false;
  175. down_read(&nm_i->nat_tree_lock);
  176. e = __lookup_nat_cache(nm_i, nid);
  177. if (e) {
  178. if (!get_nat_flag(e, IS_CHECKPOINTED) &&
  179. !get_nat_flag(e, HAS_FSYNCED_INODE))
  180. need = true;
  181. }
  182. up_read(&nm_i->nat_tree_lock);
  183. return need;
  184. }
  185. bool is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid)
  186. {
  187. struct f2fs_nm_info *nm_i = NM_I(sbi);
  188. struct nat_entry *e;
  189. bool is_cp = true;
  190. down_read(&nm_i->nat_tree_lock);
  191. e = __lookup_nat_cache(nm_i, nid);
  192. if (e && !get_nat_flag(e, IS_CHECKPOINTED))
  193. is_cp = false;
  194. up_read(&nm_i->nat_tree_lock);
  195. return is_cp;
  196. }
  197. bool need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino)
  198. {
  199. struct f2fs_nm_info *nm_i = NM_I(sbi);
  200. struct nat_entry *e;
  201. bool need_update = true;
  202. down_read(&nm_i->nat_tree_lock);
  203. e = __lookup_nat_cache(nm_i, ino);
  204. if (e && get_nat_flag(e, HAS_LAST_FSYNC) &&
  205. (get_nat_flag(e, IS_CHECKPOINTED) ||
  206. get_nat_flag(e, HAS_FSYNCED_INODE)))
  207. need_update = false;
  208. up_read(&nm_i->nat_tree_lock);
  209. return need_update;
  210. }
  211. static struct nat_entry *grab_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid)
  212. {
  213. struct nat_entry *new;
  214. new = f2fs_kmem_cache_alloc(nat_entry_slab, GFP_NOFS);
  215. f2fs_radix_tree_insert(&nm_i->nat_root, nid, new);
  216. memset(new, 0, sizeof(struct nat_entry));
  217. nat_set_nid(new, nid);
  218. nat_reset_flag(new);
  219. list_add_tail(&new->list, &nm_i->nat_entries);
  220. nm_i->nat_cnt++;
  221. return new;
  222. }
  223. static void cache_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid,
  224. struct f2fs_nat_entry *ne)
  225. {
  226. struct nat_entry *e;
  227. e = __lookup_nat_cache(nm_i, nid);
  228. if (!e) {
  229. e = grab_nat_entry(nm_i, nid);
  230. node_info_from_raw_nat(&e->ni, ne);
  231. }
  232. }
  233. static void set_node_addr(struct f2fs_sb_info *sbi, struct node_info *ni,
  234. block_t new_blkaddr, bool fsync_done)
  235. {
  236. struct f2fs_nm_info *nm_i = NM_I(sbi);
  237. struct nat_entry *e;
  238. down_write(&nm_i->nat_tree_lock);
  239. e = __lookup_nat_cache(nm_i, ni->nid);
  240. if (!e) {
  241. e = grab_nat_entry(nm_i, ni->nid);
  242. copy_node_info(&e->ni, ni);
  243. f2fs_bug_on(sbi, ni->blk_addr == NEW_ADDR);
  244. } else if (new_blkaddr == NEW_ADDR) {
  245. /*
  246. * when nid is reallocated,
  247. * previous nat entry can be remained in nat cache.
  248. * So, reinitialize it with new information.
  249. */
  250. copy_node_info(&e->ni, ni);
  251. f2fs_bug_on(sbi, ni->blk_addr != NULL_ADDR);
  252. }
  253. /* sanity check */
  254. f2fs_bug_on(sbi, nat_get_blkaddr(e) != ni->blk_addr);
  255. f2fs_bug_on(sbi, nat_get_blkaddr(e) == NULL_ADDR &&
  256. new_blkaddr == NULL_ADDR);
  257. f2fs_bug_on(sbi, nat_get_blkaddr(e) == NEW_ADDR &&
  258. new_blkaddr == NEW_ADDR);
  259. f2fs_bug_on(sbi, nat_get_blkaddr(e) != NEW_ADDR &&
  260. nat_get_blkaddr(e) != NULL_ADDR &&
  261. new_blkaddr == NEW_ADDR);
  262. /* increment version no as node is removed */
  263. if (nat_get_blkaddr(e) != NEW_ADDR && new_blkaddr == NULL_ADDR) {
  264. unsigned char version = nat_get_version(e);
  265. nat_set_version(e, inc_node_version(version));
  266. /* in order to reuse the nid */
  267. if (nm_i->next_scan_nid > ni->nid)
  268. nm_i->next_scan_nid = ni->nid;
  269. }
  270. /* change address */
  271. nat_set_blkaddr(e, new_blkaddr);
  272. if (new_blkaddr == NEW_ADDR || new_blkaddr == NULL_ADDR)
  273. set_nat_flag(e, IS_CHECKPOINTED, false);
  274. __set_nat_cache_dirty(nm_i, e);
  275. /* update fsync_mark if its inode nat entry is still alive */
  276. if (ni->nid != ni->ino)
  277. e = __lookup_nat_cache(nm_i, ni->ino);
  278. if (e) {
  279. if (fsync_done && ni->nid == ni->ino)
  280. set_nat_flag(e, HAS_FSYNCED_INODE, true);
  281. set_nat_flag(e, HAS_LAST_FSYNC, fsync_done);
  282. }
  283. up_write(&nm_i->nat_tree_lock);
  284. }
  285. int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink)
  286. {
  287. struct f2fs_nm_info *nm_i = NM_I(sbi);
  288. int nr = nr_shrink;
  289. if (!down_write_trylock(&nm_i->nat_tree_lock))
  290. return 0;
  291. while (nr_shrink && !list_empty(&nm_i->nat_entries)) {
  292. struct nat_entry *ne;
  293. ne = list_first_entry(&nm_i->nat_entries,
  294. struct nat_entry, list);
  295. __del_from_nat_cache(nm_i, ne);
  296. nr_shrink--;
  297. }
  298. up_write(&nm_i->nat_tree_lock);
  299. return nr - nr_shrink;
  300. }
  301. /*
  302. * This function always returns success
  303. */
  304. void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni)
  305. {
  306. struct f2fs_nm_info *nm_i = NM_I(sbi);
  307. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
  308. struct f2fs_summary_block *sum = curseg->sum_blk;
  309. nid_t start_nid = START_NID(nid);
  310. struct f2fs_nat_block *nat_blk;
  311. struct page *page = NULL;
  312. struct f2fs_nat_entry ne;
  313. struct nat_entry *e;
  314. int i;
  315. ni->nid = nid;
  316. /* Check nat cache */
  317. down_read(&nm_i->nat_tree_lock);
  318. e = __lookup_nat_cache(nm_i, nid);
  319. if (e) {
  320. ni->ino = nat_get_ino(e);
  321. ni->blk_addr = nat_get_blkaddr(e);
  322. ni->version = nat_get_version(e);
  323. }
  324. up_read(&nm_i->nat_tree_lock);
  325. if (e)
  326. return;
  327. memset(&ne, 0, sizeof(struct f2fs_nat_entry));
  328. down_write(&nm_i->nat_tree_lock);
  329. /* Check current segment summary */
  330. mutex_lock(&curseg->curseg_mutex);
  331. i = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 0);
  332. if (i >= 0) {
  333. ne = nat_in_journal(sum, i);
  334. node_info_from_raw_nat(ni, &ne);
  335. }
  336. mutex_unlock(&curseg->curseg_mutex);
  337. if (i >= 0)
  338. goto cache;
  339. /* Fill node_info from nat page */
  340. page = get_current_nat_page(sbi, start_nid);
  341. nat_blk = (struct f2fs_nat_block *)page_address(page);
  342. ne = nat_blk->entries[nid - start_nid];
  343. node_info_from_raw_nat(ni, &ne);
  344. f2fs_put_page(page, 1);
  345. cache:
  346. /* cache nat entry */
  347. cache_nat_entry(NM_I(sbi), nid, &ne);
  348. up_write(&nm_i->nat_tree_lock);
  349. }
  350. /*
  351. * The maximum depth is four.
  352. * Offset[0] will have raw inode offset.
  353. */
  354. static int get_node_path(struct f2fs_inode_info *fi, long block,
  355. int offset[4], unsigned int noffset[4])
  356. {
  357. const long direct_index = ADDRS_PER_INODE(fi);
  358. const long direct_blks = ADDRS_PER_BLOCK;
  359. const long dptrs_per_blk = NIDS_PER_BLOCK;
  360. const long indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
  361. const long dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
  362. int n = 0;
  363. int level = 0;
  364. noffset[0] = 0;
  365. if (block < direct_index) {
  366. offset[n] = block;
  367. goto got;
  368. }
  369. block -= direct_index;
  370. if (block < direct_blks) {
  371. offset[n++] = NODE_DIR1_BLOCK;
  372. noffset[n] = 1;
  373. offset[n] = block;
  374. level = 1;
  375. goto got;
  376. }
  377. block -= direct_blks;
  378. if (block < direct_blks) {
  379. offset[n++] = NODE_DIR2_BLOCK;
  380. noffset[n] = 2;
  381. offset[n] = block;
  382. level = 1;
  383. goto got;
  384. }
  385. block -= direct_blks;
  386. if (block < indirect_blks) {
  387. offset[n++] = NODE_IND1_BLOCK;
  388. noffset[n] = 3;
  389. offset[n++] = block / direct_blks;
  390. noffset[n] = 4 + offset[n - 1];
  391. offset[n] = block % direct_blks;
  392. level = 2;
  393. goto got;
  394. }
  395. block -= indirect_blks;
  396. if (block < indirect_blks) {
  397. offset[n++] = NODE_IND2_BLOCK;
  398. noffset[n] = 4 + dptrs_per_blk;
  399. offset[n++] = block / direct_blks;
  400. noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
  401. offset[n] = block % direct_blks;
  402. level = 2;
  403. goto got;
  404. }
  405. block -= indirect_blks;
  406. if (block < dindirect_blks) {
  407. offset[n++] = NODE_DIND_BLOCK;
  408. noffset[n] = 5 + (dptrs_per_blk * 2);
  409. offset[n++] = block / indirect_blks;
  410. noffset[n] = 6 + (dptrs_per_blk * 2) +
  411. offset[n - 1] * (dptrs_per_blk + 1);
  412. offset[n++] = (block / direct_blks) % dptrs_per_blk;
  413. noffset[n] = 7 + (dptrs_per_blk * 2) +
  414. offset[n - 2] * (dptrs_per_blk + 1) +
  415. offset[n - 1];
  416. offset[n] = block % direct_blks;
  417. level = 3;
  418. goto got;
  419. } else {
  420. BUG();
  421. }
  422. got:
  423. return level;
  424. }
  425. /*
  426. * Caller should call f2fs_put_dnode(dn).
  427. * Also, it should grab and release a rwsem by calling f2fs_lock_op() and
  428. * f2fs_unlock_op() only if ro is not set RDONLY_NODE.
  429. * In the case of RDONLY_NODE, we don't need to care about mutex.
  430. */
  431. int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode)
  432. {
  433. struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
  434. struct page *npage[4];
  435. struct page *parent = NULL;
  436. int offset[4];
  437. unsigned int noffset[4];
  438. nid_t nids[4];
  439. int level, i;
  440. int err = 0;
  441. level = get_node_path(F2FS_I(dn->inode), index, offset, noffset);
  442. nids[0] = dn->inode->i_ino;
  443. npage[0] = dn->inode_page;
  444. if (!npage[0]) {
  445. npage[0] = get_node_page(sbi, nids[0]);
  446. if (IS_ERR(npage[0]))
  447. return PTR_ERR(npage[0]);
  448. }
  449. /* if inline_data is set, should not report any block indices */
  450. if (f2fs_has_inline_data(dn->inode) && index) {
  451. err = -ENOENT;
  452. f2fs_put_page(npage[0], 1);
  453. goto release_out;
  454. }
  455. parent = npage[0];
  456. if (level != 0)
  457. nids[1] = get_nid(parent, offset[0], true);
  458. dn->inode_page = npage[0];
  459. dn->inode_page_locked = true;
  460. /* get indirect or direct nodes */
  461. for (i = 1; i <= level; i++) {
  462. bool done = false;
  463. if (!nids[i] && mode == ALLOC_NODE) {
  464. /* alloc new node */
  465. if (!alloc_nid(sbi, &(nids[i]))) {
  466. err = -ENOSPC;
  467. goto release_pages;
  468. }
  469. dn->nid = nids[i];
  470. npage[i] = new_node_page(dn, noffset[i], NULL);
  471. if (IS_ERR(npage[i])) {
  472. alloc_nid_failed(sbi, nids[i]);
  473. err = PTR_ERR(npage[i]);
  474. goto release_pages;
  475. }
  476. set_nid(parent, offset[i - 1], nids[i], i == 1);
  477. alloc_nid_done(sbi, nids[i]);
  478. done = true;
  479. } else if (mode == LOOKUP_NODE_RA && i == level && level > 1) {
  480. npage[i] = get_node_page_ra(parent, offset[i - 1]);
  481. if (IS_ERR(npage[i])) {
  482. err = PTR_ERR(npage[i]);
  483. goto release_pages;
  484. }
  485. done = true;
  486. }
  487. if (i == 1) {
  488. dn->inode_page_locked = false;
  489. unlock_page(parent);
  490. } else {
  491. f2fs_put_page(parent, 1);
  492. }
  493. if (!done) {
  494. npage[i] = get_node_page(sbi, nids[i]);
  495. if (IS_ERR(npage[i])) {
  496. err = PTR_ERR(npage[i]);
  497. f2fs_put_page(npage[0], 0);
  498. goto release_out;
  499. }
  500. }
  501. if (i < level) {
  502. parent = npage[i];
  503. nids[i + 1] = get_nid(parent, offset[i], false);
  504. }
  505. }
  506. dn->nid = nids[level];
  507. dn->ofs_in_node = offset[level];
  508. dn->node_page = npage[level];
  509. dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
  510. return 0;
  511. release_pages:
  512. f2fs_put_page(parent, 1);
  513. if (i > 1)
  514. f2fs_put_page(npage[0], 0);
  515. release_out:
  516. dn->inode_page = NULL;
  517. dn->node_page = NULL;
  518. return err;
  519. }
  520. static void truncate_node(struct dnode_of_data *dn)
  521. {
  522. struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
  523. struct node_info ni;
  524. get_node_info(sbi, dn->nid, &ni);
  525. if (dn->inode->i_blocks == 0) {
  526. f2fs_bug_on(sbi, ni.blk_addr != NULL_ADDR);
  527. goto invalidate;
  528. }
  529. f2fs_bug_on(sbi, ni.blk_addr == NULL_ADDR);
  530. /* Deallocate node address */
  531. invalidate_blocks(sbi, ni.blk_addr);
  532. dec_valid_node_count(sbi, dn->inode);
  533. set_node_addr(sbi, &ni, NULL_ADDR, false);
  534. if (dn->nid == dn->inode->i_ino) {
  535. remove_orphan_inode(sbi, dn->nid);
  536. dec_valid_inode_count(sbi);
  537. } else {
  538. sync_inode_page(dn);
  539. }
  540. invalidate:
  541. clear_node_page_dirty(dn->node_page);
  542. set_sbi_flag(sbi, SBI_IS_DIRTY);
  543. f2fs_put_page(dn->node_page, 1);
  544. invalidate_mapping_pages(NODE_MAPPING(sbi),
  545. dn->node_page->index, dn->node_page->index);
  546. dn->node_page = NULL;
  547. trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr);
  548. }
  549. static int truncate_dnode(struct dnode_of_data *dn)
  550. {
  551. struct page *page;
  552. if (dn->nid == 0)
  553. return 1;
  554. /* get direct node */
  555. page = get_node_page(F2FS_I_SB(dn->inode), dn->nid);
  556. if (IS_ERR(page) && PTR_ERR(page) == -ENOENT)
  557. return 1;
  558. else if (IS_ERR(page))
  559. return PTR_ERR(page);
  560. /* Make dnode_of_data for parameter */
  561. dn->node_page = page;
  562. dn->ofs_in_node = 0;
  563. truncate_data_blocks(dn);
  564. truncate_node(dn);
  565. return 1;
  566. }
  567. static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs,
  568. int ofs, int depth)
  569. {
  570. struct dnode_of_data rdn = *dn;
  571. struct page *page;
  572. struct f2fs_node *rn;
  573. nid_t child_nid;
  574. unsigned int child_nofs;
  575. int freed = 0;
  576. int i, ret;
  577. if (dn->nid == 0)
  578. return NIDS_PER_BLOCK + 1;
  579. trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr);
  580. page = get_node_page(F2FS_I_SB(dn->inode), dn->nid);
  581. if (IS_ERR(page)) {
  582. trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page));
  583. return PTR_ERR(page);
  584. }
  585. rn = F2FS_NODE(page);
  586. if (depth < 3) {
  587. for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) {
  588. child_nid = le32_to_cpu(rn->in.nid[i]);
  589. if (child_nid == 0)
  590. continue;
  591. rdn.nid = child_nid;
  592. ret = truncate_dnode(&rdn);
  593. if (ret < 0)
  594. goto out_err;
  595. if (set_nid(page, i, 0, false))
  596. dn->node_changed = true;
  597. }
  598. } else {
  599. child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1;
  600. for (i = ofs; i < NIDS_PER_BLOCK; i++) {
  601. child_nid = le32_to_cpu(rn->in.nid[i]);
  602. if (child_nid == 0) {
  603. child_nofs += NIDS_PER_BLOCK + 1;
  604. continue;
  605. }
  606. rdn.nid = child_nid;
  607. ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1);
  608. if (ret == (NIDS_PER_BLOCK + 1)) {
  609. if (set_nid(page, i, 0, false))
  610. dn->node_changed = true;
  611. child_nofs += ret;
  612. } else if (ret < 0 && ret != -ENOENT) {
  613. goto out_err;
  614. }
  615. }
  616. freed = child_nofs;
  617. }
  618. if (!ofs) {
  619. /* remove current indirect node */
  620. dn->node_page = page;
  621. truncate_node(dn);
  622. freed++;
  623. } else {
  624. f2fs_put_page(page, 1);
  625. }
  626. trace_f2fs_truncate_nodes_exit(dn->inode, freed);
  627. return freed;
  628. out_err:
  629. f2fs_put_page(page, 1);
  630. trace_f2fs_truncate_nodes_exit(dn->inode, ret);
  631. return ret;
  632. }
  633. static int truncate_partial_nodes(struct dnode_of_data *dn,
  634. struct f2fs_inode *ri, int *offset, int depth)
  635. {
  636. struct page *pages[2];
  637. nid_t nid[3];
  638. nid_t child_nid;
  639. int err = 0;
  640. int i;
  641. int idx = depth - 2;
  642. nid[0] = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
  643. if (!nid[0])
  644. return 0;
  645. /* get indirect nodes in the path */
  646. for (i = 0; i < idx + 1; i++) {
  647. /* reference count'll be increased */
  648. pages[i] = get_node_page(F2FS_I_SB(dn->inode), nid[i]);
  649. if (IS_ERR(pages[i])) {
  650. err = PTR_ERR(pages[i]);
  651. idx = i - 1;
  652. goto fail;
  653. }
  654. nid[i + 1] = get_nid(pages[i], offset[i + 1], false);
  655. }
  656. /* free direct nodes linked to a partial indirect node */
  657. for (i = offset[idx + 1]; i < NIDS_PER_BLOCK; i++) {
  658. child_nid = get_nid(pages[idx], i, false);
  659. if (!child_nid)
  660. continue;
  661. dn->nid = child_nid;
  662. err = truncate_dnode(dn);
  663. if (err < 0)
  664. goto fail;
  665. if (set_nid(pages[idx], i, 0, false))
  666. dn->node_changed = true;
  667. }
  668. if (offset[idx + 1] == 0) {
  669. dn->node_page = pages[idx];
  670. dn->nid = nid[idx];
  671. truncate_node(dn);
  672. } else {
  673. f2fs_put_page(pages[idx], 1);
  674. }
  675. offset[idx]++;
  676. offset[idx + 1] = 0;
  677. idx--;
  678. fail:
  679. for (i = idx; i >= 0; i--)
  680. f2fs_put_page(pages[i], 1);
  681. trace_f2fs_truncate_partial_nodes(dn->inode, nid, depth, err);
  682. return err;
  683. }
  684. /*
  685. * All the block addresses of data and nodes should be nullified.
  686. */
  687. int truncate_inode_blocks(struct inode *inode, pgoff_t from)
  688. {
  689. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  690. int err = 0, cont = 1;
  691. int level, offset[4], noffset[4];
  692. unsigned int nofs = 0;
  693. struct f2fs_inode *ri;
  694. struct dnode_of_data dn;
  695. struct page *page;
  696. trace_f2fs_truncate_inode_blocks_enter(inode, from);
  697. level = get_node_path(F2FS_I(inode), from, offset, noffset);
  698. restart:
  699. page = get_node_page(sbi, inode->i_ino);
  700. if (IS_ERR(page)) {
  701. trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(page));
  702. return PTR_ERR(page);
  703. }
  704. set_new_dnode(&dn, inode, page, NULL, 0);
  705. unlock_page(page);
  706. ri = F2FS_INODE(page);
  707. switch (level) {
  708. case 0:
  709. case 1:
  710. nofs = noffset[1];
  711. break;
  712. case 2:
  713. nofs = noffset[1];
  714. if (!offset[level - 1])
  715. goto skip_partial;
  716. err = truncate_partial_nodes(&dn, ri, offset, level);
  717. if (err < 0 && err != -ENOENT)
  718. goto fail;
  719. nofs += 1 + NIDS_PER_BLOCK;
  720. break;
  721. case 3:
  722. nofs = 5 + 2 * NIDS_PER_BLOCK;
  723. if (!offset[level - 1])
  724. goto skip_partial;
  725. err = truncate_partial_nodes(&dn, ri, offset, level);
  726. if (err < 0 && err != -ENOENT)
  727. goto fail;
  728. break;
  729. default:
  730. BUG();
  731. }
  732. skip_partial:
  733. while (cont) {
  734. dn.nid = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
  735. switch (offset[0]) {
  736. case NODE_DIR1_BLOCK:
  737. case NODE_DIR2_BLOCK:
  738. err = truncate_dnode(&dn);
  739. break;
  740. case NODE_IND1_BLOCK:
  741. case NODE_IND2_BLOCK:
  742. err = truncate_nodes(&dn, nofs, offset[1], 2);
  743. break;
  744. case NODE_DIND_BLOCK:
  745. err = truncate_nodes(&dn, nofs, offset[1], 3);
  746. cont = 0;
  747. break;
  748. default:
  749. BUG();
  750. }
  751. if (err < 0 && err != -ENOENT)
  752. goto fail;
  753. if (offset[1] == 0 &&
  754. ri->i_nid[offset[0] - NODE_DIR1_BLOCK]) {
  755. lock_page(page);
  756. if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
  757. f2fs_put_page(page, 1);
  758. goto restart;
  759. }
  760. f2fs_wait_on_page_writeback(page, NODE);
  761. ri->i_nid[offset[0] - NODE_DIR1_BLOCK] = 0;
  762. set_page_dirty(page);
  763. unlock_page(page);
  764. }
  765. offset[1] = 0;
  766. offset[0]++;
  767. nofs += err;
  768. }
  769. fail:
  770. f2fs_put_page(page, 0);
  771. trace_f2fs_truncate_inode_blocks_exit(inode, err);
  772. return err > 0 ? 0 : err;
  773. }
  774. int truncate_xattr_node(struct inode *inode, struct page *page)
  775. {
  776. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  777. nid_t nid = F2FS_I(inode)->i_xattr_nid;
  778. struct dnode_of_data dn;
  779. struct page *npage;
  780. if (!nid)
  781. return 0;
  782. npage = get_node_page(sbi, nid);
  783. if (IS_ERR(npage))
  784. return PTR_ERR(npage);
  785. F2FS_I(inode)->i_xattr_nid = 0;
  786. /* need to do checkpoint during fsync */
  787. F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi));
  788. set_new_dnode(&dn, inode, page, npage, nid);
  789. if (page)
  790. dn.inode_page_locked = true;
  791. truncate_node(&dn);
  792. return 0;
  793. }
  794. /*
  795. * Caller should grab and release a rwsem by calling f2fs_lock_op() and
  796. * f2fs_unlock_op().
  797. */
  798. int remove_inode_page(struct inode *inode)
  799. {
  800. struct dnode_of_data dn;
  801. int err;
  802. set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
  803. err = get_dnode_of_data(&dn, 0, LOOKUP_NODE);
  804. if (err)
  805. return err;
  806. err = truncate_xattr_node(inode, dn.inode_page);
  807. if (err) {
  808. f2fs_put_dnode(&dn);
  809. return err;
  810. }
  811. /* remove potential inline_data blocks */
  812. if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  813. S_ISLNK(inode->i_mode))
  814. truncate_data_blocks_range(&dn, 1);
  815. /* 0 is possible, after f2fs_new_inode() has failed */
  816. f2fs_bug_on(F2FS_I_SB(inode),
  817. inode->i_blocks != 0 && inode->i_blocks != 1);
  818. /* will put inode & node pages */
  819. truncate_node(&dn);
  820. return 0;
  821. }
  822. struct page *new_inode_page(struct inode *inode)
  823. {
  824. struct dnode_of_data dn;
  825. /* allocate inode page for new inode */
  826. set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
  827. /* caller should f2fs_put_page(page, 1); */
  828. return new_node_page(&dn, 0, NULL);
  829. }
  830. struct page *new_node_page(struct dnode_of_data *dn,
  831. unsigned int ofs, struct page *ipage)
  832. {
  833. struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
  834. struct node_info old_ni, new_ni;
  835. struct page *page;
  836. int err;
  837. if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
  838. return ERR_PTR(-EPERM);
  839. page = grab_cache_page(NODE_MAPPING(sbi), dn->nid);
  840. if (!page)
  841. return ERR_PTR(-ENOMEM);
  842. if (unlikely(!inc_valid_node_count(sbi, dn->inode))) {
  843. err = -ENOSPC;
  844. goto fail;
  845. }
  846. get_node_info(sbi, dn->nid, &old_ni);
  847. /* Reinitialize old_ni with new node page */
  848. f2fs_bug_on(sbi, old_ni.blk_addr != NULL_ADDR);
  849. new_ni = old_ni;
  850. new_ni.ino = dn->inode->i_ino;
  851. set_node_addr(sbi, &new_ni, NEW_ADDR, false);
  852. f2fs_wait_on_page_writeback(page, NODE);
  853. fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
  854. set_cold_node(dn->inode, page);
  855. SetPageUptodate(page);
  856. if (set_page_dirty(page))
  857. dn->node_changed = true;
  858. if (f2fs_has_xattr_block(ofs))
  859. F2FS_I(dn->inode)->i_xattr_nid = dn->nid;
  860. dn->node_page = page;
  861. if (ipage)
  862. update_inode(dn->inode, ipage);
  863. else
  864. sync_inode_page(dn);
  865. if (ofs == 0)
  866. inc_valid_inode_count(sbi);
  867. return page;
  868. fail:
  869. clear_node_page_dirty(page);
  870. f2fs_put_page(page, 1);
  871. return ERR_PTR(err);
  872. }
  873. /*
  874. * Caller should do after getting the following values.
  875. * 0: f2fs_put_page(page, 0)
  876. * LOCKED_PAGE or error: f2fs_put_page(page, 1)
  877. */
  878. static int read_node_page(struct page *page, int rw)
  879. {
  880. struct f2fs_sb_info *sbi = F2FS_P_SB(page);
  881. struct node_info ni;
  882. struct f2fs_io_info fio = {
  883. .sbi = sbi,
  884. .type = NODE,
  885. .rw = rw,
  886. .page = page,
  887. .encrypted_page = NULL,
  888. };
  889. get_node_info(sbi, page->index, &ni);
  890. if (unlikely(ni.blk_addr == NULL_ADDR)) {
  891. ClearPageUptodate(page);
  892. return -ENOENT;
  893. }
  894. if (PageUptodate(page))
  895. return LOCKED_PAGE;
  896. fio.blk_addr = ni.blk_addr;
  897. return f2fs_submit_page_bio(&fio);
  898. }
  899. /*
  900. * Readahead a node page
  901. */
  902. void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid)
  903. {
  904. struct page *apage;
  905. int err;
  906. if (!nid)
  907. return;
  908. f2fs_bug_on(sbi, check_nid_range(sbi, nid));
  909. apage = find_get_page(NODE_MAPPING(sbi), nid);
  910. if (apage && PageUptodate(apage)) {
  911. f2fs_put_page(apage, 0);
  912. return;
  913. }
  914. f2fs_put_page(apage, 0);
  915. apage = grab_cache_page(NODE_MAPPING(sbi), nid);
  916. if (!apage)
  917. return;
  918. err = read_node_page(apage, READA);
  919. f2fs_put_page(apage, err ? 1 : 0);
  920. }
  921. /*
  922. * readahead MAX_RA_NODE number of node pages.
  923. */
  924. void ra_node_pages(struct page *parent, int start)
  925. {
  926. struct f2fs_sb_info *sbi = F2FS_P_SB(parent);
  927. struct blk_plug plug;
  928. int i, end;
  929. nid_t nid;
  930. blk_start_plug(&plug);
  931. /* Then, try readahead for siblings of the desired node */
  932. end = start + MAX_RA_NODE;
  933. end = min(end, NIDS_PER_BLOCK);
  934. for (i = start; i < end; i++) {
  935. nid = get_nid(parent, i, false);
  936. ra_node_page(sbi, nid);
  937. }
  938. blk_finish_plug(&plug);
  939. }
  940. struct page *__get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid,
  941. struct page *parent, int start)
  942. {
  943. struct page *page;
  944. int err;
  945. if (!nid)
  946. return ERR_PTR(-ENOENT);
  947. f2fs_bug_on(sbi, check_nid_range(sbi, nid));
  948. repeat:
  949. page = grab_cache_page(NODE_MAPPING(sbi), nid);
  950. if (!page)
  951. return ERR_PTR(-ENOMEM);
  952. err = read_node_page(page, READ_SYNC);
  953. if (err < 0) {
  954. f2fs_put_page(page, 1);
  955. return ERR_PTR(err);
  956. } else if (err == LOCKED_PAGE) {
  957. goto page_hit;
  958. }
  959. if (parent)
  960. ra_node_pages(parent, start + 1);
  961. lock_page(page);
  962. if (unlikely(!PageUptodate(page))) {
  963. f2fs_put_page(page, 1);
  964. return ERR_PTR(-EIO);
  965. }
  966. if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
  967. f2fs_put_page(page, 1);
  968. goto repeat;
  969. }
  970. page_hit:
  971. f2fs_bug_on(sbi, nid != nid_of_node(page));
  972. return page;
  973. }
  974. struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid)
  975. {
  976. return __get_node_page(sbi, nid, NULL, 0);
  977. }
  978. struct page *get_node_page_ra(struct page *parent, int start)
  979. {
  980. struct f2fs_sb_info *sbi = F2FS_P_SB(parent);
  981. nid_t nid = get_nid(parent, start, false);
  982. return __get_node_page(sbi, nid, parent, start);
  983. }
  984. void sync_inode_page(struct dnode_of_data *dn)
  985. {
  986. int ret = 0;
  987. if (IS_INODE(dn->node_page) || dn->inode_page == dn->node_page) {
  988. ret = update_inode(dn->inode, dn->node_page);
  989. } else if (dn->inode_page) {
  990. if (!dn->inode_page_locked)
  991. lock_page(dn->inode_page);
  992. ret = update_inode(dn->inode, dn->inode_page);
  993. if (!dn->inode_page_locked)
  994. unlock_page(dn->inode_page);
  995. } else {
  996. ret = update_inode_page(dn->inode);
  997. }
  998. dn->node_changed = ret ? true: false;
  999. }
  1000. int sync_node_pages(struct f2fs_sb_info *sbi, nid_t ino,
  1001. struct writeback_control *wbc)
  1002. {
  1003. pgoff_t index, end;
  1004. struct pagevec pvec;
  1005. int step = ino ? 2 : 0;
  1006. int nwritten = 0, wrote = 0;
  1007. pagevec_init(&pvec, 0);
  1008. next_step:
  1009. index = 0;
  1010. end = LONG_MAX;
  1011. while (index <= end) {
  1012. int i, nr_pages;
  1013. nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
  1014. PAGECACHE_TAG_DIRTY,
  1015. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
  1016. if (nr_pages == 0)
  1017. break;
  1018. for (i = 0; i < nr_pages; i++) {
  1019. struct page *page = pvec.pages[i];
  1020. if (unlikely(f2fs_cp_error(sbi))) {
  1021. pagevec_release(&pvec);
  1022. return -EIO;
  1023. }
  1024. /*
  1025. * flushing sequence with step:
  1026. * 0. indirect nodes
  1027. * 1. dentry dnodes
  1028. * 2. file dnodes
  1029. */
  1030. if (step == 0 && IS_DNODE(page))
  1031. continue;
  1032. if (step == 1 && (!IS_DNODE(page) ||
  1033. is_cold_node(page)))
  1034. continue;
  1035. if (step == 2 && (!IS_DNODE(page) ||
  1036. !is_cold_node(page)))
  1037. continue;
  1038. /*
  1039. * If an fsync mode,
  1040. * we should not skip writing node pages.
  1041. */
  1042. if (ino && ino_of_node(page) == ino)
  1043. lock_page(page);
  1044. else if (!trylock_page(page))
  1045. continue;
  1046. if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
  1047. continue_unlock:
  1048. unlock_page(page);
  1049. continue;
  1050. }
  1051. if (ino && ino_of_node(page) != ino)
  1052. goto continue_unlock;
  1053. if (!PageDirty(page)) {
  1054. /* someone wrote it for us */
  1055. goto continue_unlock;
  1056. }
  1057. if (!clear_page_dirty_for_io(page))
  1058. goto continue_unlock;
  1059. /* called by fsync() */
  1060. if (ino && IS_DNODE(page)) {
  1061. set_fsync_mark(page, 1);
  1062. if (IS_INODE(page))
  1063. set_dentry_mark(page,
  1064. need_dentry_mark(sbi, ino));
  1065. nwritten++;
  1066. } else {
  1067. set_fsync_mark(page, 0);
  1068. set_dentry_mark(page, 0);
  1069. }
  1070. if (NODE_MAPPING(sbi)->a_ops->writepage(page, wbc))
  1071. unlock_page(page);
  1072. else
  1073. wrote++;
  1074. if (--wbc->nr_to_write == 0)
  1075. break;
  1076. }
  1077. pagevec_release(&pvec);
  1078. cond_resched();
  1079. if (wbc->nr_to_write == 0) {
  1080. step = 2;
  1081. break;
  1082. }
  1083. }
  1084. if (step < 2) {
  1085. step++;
  1086. goto next_step;
  1087. }
  1088. if (wrote)
  1089. f2fs_submit_merged_bio(sbi, NODE, WRITE);
  1090. return nwritten;
  1091. }
  1092. int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino)
  1093. {
  1094. pgoff_t index = 0, end = LONG_MAX;
  1095. struct pagevec pvec;
  1096. int ret2 = 0, ret = 0;
  1097. pagevec_init(&pvec, 0);
  1098. while (index <= end) {
  1099. int i, nr_pages;
  1100. nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
  1101. PAGECACHE_TAG_WRITEBACK,
  1102. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
  1103. if (nr_pages == 0)
  1104. break;
  1105. for (i = 0; i < nr_pages; i++) {
  1106. struct page *page = pvec.pages[i];
  1107. /* until radix tree lookup accepts end_index */
  1108. if (unlikely(page->index > end))
  1109. continue;
  1110. if (ino && ino_of_node(page) == ino) {
  1111. f2fs_wait_on_page_writeback(page, NODE);
  1112. if (TestClearPageError(page))
  1113. ret = -EIO;
  1114. }
  1115. }
  1116. pagevec_release(&pvec);
  1117. cond_resched();
  1118. }
  1119. if (unlikely(test_and_clear_bit(AS_ENOSPC, &NODE_MAPPING(sbi)->flags)))
  1120. ret2 = -ENOSPC;
  1121. if (unlikely(test_and_clear_bit(AS_EIO, &NODE_MAPPING(sbi)->flags)))
  1122. ret2 = -EIO;
  1123. if (!ret)
  1124. ret = ret2;
  1125. return ret;
  1126. }
  1127. static int f2fs_write_node_page(struct page *page,
  1128. struct writeback_control *wbc)
  1129. {
  1130. struct f2fs_sb_info *sbi = F2FS_P_SB(page);
  1131. nid_t nid;
  1132. struct node_info ni;
  1133. struct f2fs_io_info fio = {
  1134. .sbi = sbi,
  1135. .type = NODE,
  1136. .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
  1137. .page = page,
  1138. .encrypted_page = NULL,
  1139. };
  1140. trace_f2fs_writepage(page, NODE);
  1141. if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
  1142. goto redirty_out;
  1143. if (unlikely(f2fs_cp_error(sbi)))
  1144. goto redirty_out;
  1145. f2fs_wait_on_page_writeback(page, NODE);
  1146. /* get old block addr of this node page */
  1147. nid = nid_of_node(page);
  1148. f2fs_bug_on(sbi, page->index != nid);
  1149. if (wbc->for_reclaim) {
  1150. if (!down_read_trylock(&sbi->node_write))
  1151. goto redirty_out;
  1152. } else {
  1153. down_read(&sbi->node_write);
  1154. }
  1155. get_node_info(sbi, nid, &ni);
  1156. /* This page is already truncated */
  1157. if (unlikely(ni.blk_addr == NULL_ADDR)) {
  1158. ClearPageUptodate(page);
  1159. dec_page_count(sbi, F2FS_DIRTY_NODES);
  1160. up_read(&sbi->node_write);
  1161. unlock_page(page);
  1162. return 0;
  1163. }
  1164. set_page_writeback(page);
  1165. fio.blk_addr = ni.blk_addr;
  1166. write_node_page(nid, &fio);
  1167. set_node_addr(sbi, &ni, fio.blk_addr, is_fsync_dnode(page));
  1168. dec_page_count(sbi, F2FS_DIRTY_NODES);
  1169. up_read(&sbi->node_write);
  1170. unlock_page(page);
  1171. if (wbc->for_reclaim || unlikely(f2fs_cp_error(sbi)))
  1172. f2fs_submit_merged_bio(sbi, NODE, WRITE);
  1173. return 0;
  1174. redirty_out:
  1175. redirty_page_for_writepage(wbc, page);
  1176. return AOP_WRITEPAGE_ACTIVATE;
  1177. }
  1178. static int f2fs_write_node_pages(struct address_space *mapping,
  1179. struct writeback_control *wbc)
  1180. {
  1181. struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
  1182. long diff;
  1183. trace_f2fs_writepages(mapping->host, wbc, NODE);
  1184. /* balancing f2fs's metadata in background */
  1185. f2fs_balance_fs_bg(sbi);
  1186. /* collect a number of dirty node pages and write together */
  1187. if (get_pages(sbi, F2FS_DIRTY_NODES) < nr_pages_to_skip(sbi, NODE))
  1188. goto skip_write;
  1189. diff = nr_pages_to_write(sbi, NODE, wbc);
  1190. wbc->sync_mode = WB_SYNC_NONE;
  1191. sync_node_pages(sbi, 0, wbc);
  1192. wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
  1193. return 0;
  1194. skip_write:
  1195. wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_NODES);
  1196. return 0;
  1197. }
  1198. static int f2fs_set_node_page_dirty(struct page *page)
  1199. {
  1200. trace_f2fs_set_page_dirty(page, NODE);
  1201. SetPageUptodate(page);
  1202. if (!PageDirty(page)) {
  1203. __set_page_dirty_nobuffers(page);
  1204. inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_NODES);
  1205. SetPagePrivate(page);
  1206. f2fs_trace_pid(page);
  1207. return 1;
  1208. }
  1209. return 0;
  1210. }
  1211. /*
  1212. * Structure of the f2fs node operations
  1213. */
  1214. const struct address_space_operations f2fs_node_aops = {
  1215. .writepage = f2fs_write_node_page,
  1216. .writepages = f2fs_write_node_pages,
  1217. .set_page_dirty = f2fs_set_node_page_dirty,
  1218. .invalidatepage = f2fs_invalidate_page,
  1219. .releasepage = f2fs_release_page,
  1220. };
  1221. static struct free_nid *__lookup_free_nid_list(struct f2fs_nm_info *nm_i,
  1222. nid_t n)
  1223. {
  1224. return radix_tree_lookup(&nm_i->free_nid_root, n);
  1225. }
  1226. static void __del_from_free_nid_list(struct f2fs_nm_info *nm_i,
  1227. struct free_nid *i)
  1228. {
  1229. list_del(&i->list);
  1230. radix_tree_delete(&nm_i->free_nid_root, i->nid);
  1231. }
  1232. static int add_free_nid(struct f2fs_sb_info *sbi, nid_t nid, bool build)
  1233. {
  1234. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1235. struct free_nid *i;
  1236. struct nat_entry *ne;
  1237. bool allocated = false;
  1238. if (!available_free_memory(sbi, FREE_NIDS))
  1239. return -1;
  1240. /* 0 nid should not be used */
  1241. if (unlikely(nid == 0))
  1242. return 0;
  1243. if (build) {
  1244. /* do not add allocated nids */
  1245. ne = __lookup_nat_cache(nm_i, nid);
  1246. if (ne && (!get_nat_flag(ne, IS_CHECKPOINTED) ||
  1247. nat_get_blkaddr(ne) != NULL_ADDR))
  1248. allocated = true;
  1249. if (allocated)
  1250. return 0;
  1251. }
  1252. i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS);
  1253. i->nid = nid;
  1254. i->state = NID_NEW;
  1255. if (radix_tree_preload(GFP_NOFS)) {
  1256. kmem_cache_free(free_nid_slab, i);
  1257. return 0;
  1258. }
  1259. spin_lock(&nm_i->free_nid_list_lock);
  1260. if (radix_tree_insert(&nm_i->free_nid_root, i->nid, i)) {
  1261. spin_unlock(&nm_i->free_nid_list_lock);
  1262. radix_tree_preload_end();
  1263. kmem_cache_free(free_nid_slab, i);
  1264. return 0;
  1265. }
  1266. list_add_tail(&i->list, &nm_i->free_nid_list);
  1267. nm_i->fcnt++;
  1268. spin_unlock(&nm_i->free_nid_list_lock);
  1269. radix_tree_preload_end();
  1270. return 1;
  1271. }
  1272. static void remove_free_nid(struct f2fs_nm_info *nm_i, nid_t nid)
  1273. {
  1274. struct free_nid *i;
  1275. bool need_free = false;
  1276. spin_lock(&nm_i->free_nid_list_lock);
  1277. i = __lookup_free_nid_list(nm_i, nid);
  1278. if (i && i->state == NID_NEW) {
  1279. __del_from_free_nid_list(nm_i, i);
  1280. nm_i->fcnt--;
  1281. need_free = true;
  1282. }
  1283. spin_unlock(&nm_i->free_nid_list_lock);
  1284. if (need_free)
  1285. kmem_cache_free(free_nid_slab, i);
  1286. }
  1287. static void scan_nat_page(struct f2fs_sb_info *sbi,
  1288. struct page *nat_page, nid_t start_nid)
  1289. {
  1290. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1291. struct f2fs_nat_block *nat_blk = page_address(nat_page);
  1292. block_t blk_addr;
  1293. int i;
  1294. i = start_nid % NAT_ENTRY_PER_BLOCK;
  1295. for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
  1296. if (unlikely(start_nid >= nm_i->max_nid))
  1297. break;
  1298. blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
  1299. f2fs_bug_on(sbi, blk_addr == NEW_ADDR);
  1300. if (blk_addr == NULL_ADDR) {
  1301. if (add_free_nid(sbi, start_nid, true) < 0)
  1302. break;
  1303. }
  1304. }
  1305. }
  1306. static void build_free_nids(struct f2fs_sb_info *sbi)
  1307. {
  1308. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1309. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
  1310. struct f2fs_summary_block *sum = curseg->sum_blk;
  1311. int i = 0;
  1312. nid_t nid = nm_i->next_scan_nid;
  1313. /* Enough entries */
  1314. if (nm_i->fcnt > NAT_ENTRY_PER_BLOCK)
  1315. return;
  1316. /* readahead nat pages to be scanned */
  1317. ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), FREE_NID_PAGES,
  1318. META_NAT, true);
  1319. down_read(&nm_i->nat_tree_lock);
  1320. while (1) {
  1321. struct page *page = get_current_nat_page(sbi, nid);
  1322. scan_nat_page(sbi, page, nid);
  1323. f2fs_put_page(page, 1);
  1324. nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK));
  1325. if (unlikely(nid >= nm_i->max_nid))
  1326. nid = 0;
  1327. if (++i >= FREE_NID_PAGES)
  1328. break;
  1329. }
  1330. /* go to the next free nat pages to find free nids abundantly */
  1331. nm_i->next_scan_nid = nid;
  1332. /* find free nids from current sum_pages */
  1333. mutex_lock(&curseg->curseg_mutex);
  1334. for (i = 0; i < nats_in_cursum(sum); i++) {
  1335. block_t addr = le32_to_cpu(nat_in_journal(sum, i).block_addr);
  1336. nid = le32_to_cpu(nid_in_journal(sum, i));
  1337. if (addr == NULL_ADDR)
  1338. add_free_nid(sbi, nid, true);
  1339. else
  1340. remove_free_nid(nm_i, nid);
  1341. }
  1342. mutex_unlock(&curseg->curseg_mutex);
  1343. up_read(&nm_i->nat_tree_lock);
  1344. ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nm_i->next_scan_nid),
  1345. nm_i->ra_nid_pages, META_NAT, false);
  1346. }
  1347. /*
  1348. * If this function returns success, caller can obtain a new nid
  1349. * from second parameter of this function.
  1350. * The returned nid could be used ino as well as nid when inode is created.
  1351. */
  1352. bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
  1353. {
  1354. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1355. struct free_nid *i = NULL;
  1356. retry:
  1357. if (unlikely(sbi->total_valid_node_count + 1 > nm_i->available_nids))
  1358. return false;
  1359. spin_lock(&nm_i->free_nid_list_lock);
  1360. /* We should not use stale free nids created by build_free_nids */
  1361. if (nm_i->fcnt && !on_build_free_nids(nm_i)) {
  1362. f2fs_bug_on(sbi, list_empty(&nm_i->free_nid_list));
  1363. list_for_each_entry(i, &nm_i->free_nid_list, list)
  1364. if (i->state == NID_NEW)
  1365. break;
  1366. f2fs_bug_on(sbi, i->state != NID_NEW);
  1367. *nid = i->nid;
  1368. i->state = NID_ALLOC;
  1369. nm_i->fcnt--;
  1370. spin_unlock(&nm_i->free_nid_list_lock);
  1371. return true;
  1372. }
  1373. spin_unlock(&nm_i->free_nid_list_lock);
  1374. /* Let's scan nat pages and its caches to get free nids */
  1375. mutex_lock(&nm_i->build_lock);
  1376. build_free_nids(sbi);
  1377. mutex_unlock(&nm_i->build_lock);
  1378. goto retry;
  1379. }
  1380. /*
  1381. * alloc_nid() should be called prior to this function.
  1382. */
  1383. void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
  1384. {
  1385. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1386. struct free_nid *i;
  1387. spin_lock(&nm_i->free_nid_list_lock);
  1388. i = __lookup_free_nid_list(nm_i, nid);
  1389. f2fs_bug_on(sbi, !i || i->state != NID_ALLOC);
  1390. __del_from_free_nid_list(nm_i, i);
  1391. spin_unlock(&nm_i->free_nid_list_lock);
  1392. kmem_cache_free(free_nid_slab, i);
  1393. }
  1394. /*
  1395. * alloc_nid() should be called prior to this function.
  1396. */
  1397. void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
  1398. {
  1399. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1400. struct free_nid *i;
  1401. bool need_free = false;
  1402. if (!nid)
  1403. return;
  1404. spin_lock(&nm_i->free_nid_list_lock);
  1405. i = __lookup_free_nid_list(nm_i, nid);
  1406. f2fs_bug_on(sbi, !i || i->state != NID_ALLOC);
  1407. if (!available_free_memory(sbi, FREE_NIDS)) {
  1408. __del_from_free_nid_list(nm_i, i);
  1409. need_free = true;
  1410. } else {
  1411. i->state = NID_NEW;
  1412. nm_i->fcnt++;
  1413. }
  1414. spin_unlock(&nm_i->free_nid_list_lock);
  1415. if (need_free)
  1416. kmem_cache_free(free_nid_slab, i);
  1417. }
  1418. int try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink)
  1419. {
  1420. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1421. struct free_nid *i, *next;
  1422. int nr = nr_shrink;
  1423. if (!mutex_trylock(&nm_i->build_lock))
  1424. return 0;
  1425. spin_lock(&nm_i->free_nid_list_lock);
  1426. list_for_each_entry_safe(i, next, &nm_i->free_nid_list, list) {
  1427. if (nr_shrink <= 0 || nm_i->fcnt <= NAT_ENTRY_PER_BLOCK)
  1428. break;
  1429. if (i->state == NID_ALLOC)
  1430. continue;
  1431. __del_from_free_nid_list(nm_i, i);
  1432. kmem_cache_free(free_nid_slab, i);
  1433. nm_i->fcnt--;
  1434. nr_shrink--;
  1435. }
  1436. spin_unlock(&nm_i->free_nid_list_lock);
  1437. mutex_unlock(&nm_i->build_lock);
  1438. return nr - nr_shrink;
  1439. }
  1440. void recover_inline_xattr(struct inode *inode, struct page *page)
  1441. {
  1442. void *src_addr, *dst_addr;
  1443. size_t inline_size;
  1444. struct page *ipage;
  1445. struct f2fs_inode *ri;
  1446. ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
  1447. f2fs_bug_on(F2FS_I_SB(inode), IS_ERR(ipage));
  1448. ri = F2FS_INODE(page);
  1449. if (!(ri->i_inline & F2FS_INLINE_XATTR)) {
  1450. clear_inode_flag(F2FS_I(inode), FI_INLINE_XATTR);
  1451. goto update_inode;
  1452. }
  1453. dst_addr = inline_xattr_addr(ipage);
  1454. src_addr = inline_xattr_addr(page);
  1455. inline_size = inline_xattr_size(inode);
  1456. f2fs_wait_on_page_writeback(ipage, NODE);
  1457. memcpy(dst_addr, src_addr, inline_size);
  1458. update_inode:
  1459. update_inode(inode, ipage);
  1460. f2fs_put_page(ipage, 1);
  1461. }
  1462. void recover_xattr_data(struct inode *inode, struct page *page, block_t blkaddr)
  1463. {
  1464. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  1465. nid_t prev_xnid = F2FS_I(inode)->i_xattr_nid;
  1466. nid_t new_xnid = nid_of_node(page);
  1467. struct node_info ni;
  1468. /* 1: invalidate the previous xattr nid */
  1469. if (!prev_xnid)
  1470. goto recover_xnid;
  1471. /* Deallocate node address */
  1472. get_node_info(sbi, prev_xnid, &ni);
  1473. f2fs_bug_on(sbi, ni.blk_addr == NULL_ADDR);
  1474. invalidate_blocks(sbi, ni.blk_addr);
  1475. dec_valid_node_count(sbi, inode);
  1476. set_node_addr(sbi, &ni, NULL_ADDR, false);
  1477. recover_xnid:
  1478. /* 2: allocate new xattr nid */
  1479. if (unlikely(!inc_valid_node_count(sbi, inode)))
  1480. f2fs_bug_on(sbi, 1);
  1481. remove_free_nid(NM_I(sbi), new_xnid);
  1482. get_node_info(sbi, new_xnid, &ni);
  1483. ni.ino = inode->i_ino;
  1484. set_node_addr(sbi, &ni, NEW_ADDR, false);
  1485. F2FS_I(inode)->i_xattr_nid = new_xnid;
  1486. /* 3: update xattr blkaddr */
  1487. refresh_sit_entry(sbi, NEW_ADDR, blkaddr);
  1488. set_node_addr(sbi, &ni, blkaddr, false);
  1489. update_inode_page(inode);
  1490. }
  1491. int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page)
  1492. {
  1493. struct f2fs_inode *src, *dst;
  1494. nid_t ino = ino_of_node(page);
  1495. struct node_info old_ni, new_ni;
  1496. struct page *ipage;
  1497. get_node_info(sbi, ino, &old_ni);
  1498. if (unlikely(old_ni.blk_addr != NULL_ADDR))
  1499. return -EINVAL;
  1500. ipage = grab_cache_page(NODE_MAPPING(sbi), ino);
  1501. if (!ipage)
  1502. return -ENOMEM;
  1503. /* Should not use this inode from free nid list */
  1504. remove_free_nid(NM_I(sbi), ino);
  1505. SetPageUptodate(ipage);
  1506. fill_node_footer(ipage, ino, ino, 0, true);
  1507. src = F2FS_INODE(page);
  1508. dst = F2FS_INODE(ipage);
  1509. memcpy(dst, src, (unsigned long)&src->i_ext - (unsigned long)src);
  1510. dst->i_size = 0;
  1511. dst->i_blocks = cpu_to_le64(1);
  1512. dst->i_links = cpu_to_le32(1);
  1513. dst->i_xattr_nid = 0;
  1514. dst->i_inline = src->i_inline & F2FS_INLINE_XATTR;
  1515. new_ni = old_ni;
  1516. new_ni.ino = ino;
  1517. if (unlikely(!inc_valid_node_count(sbi, NULL)))
  1518. WARN_ON(1);
  1519. set_node_addr(sbi, &new_ni, NEW_ADDR, false);
  1520. inc_valid_inode_count(sbi);
  1521. set_page_dirty(ipage);
  1522. f2fs_put_page(ipage, 1);
  1523. return 0;
  1524. }
  1525. int restore_node_summary(struct f2fs_sb_info *sbi,
  1526. unsigned int segno, struct f2fs_summary_block *sum)
  1527. {
  1528. struct f2fs_node *rn;
  1529. struct f2fs_summary *sum_entry;
  1530. block_t addr;
  1531. int bio_blocks = MAX_BIO_BLOCKS(sbi);
  1532. int i, idx, last_offset, nrpages;
  1533. /* scan the node segment */
  1534. last_offset = sbi->blocks_per_seg;
  1535. addr = START_BLOCK(sbi, segno);
  1536. sum_entry = &sum->entries[0];
  1537. for (i = 0; i < last_offset; i += nrpages, addr += nrpages) {
  1538. nrpages = min(last_offset - i, bio_blocks);
  1539. /* readahead node pages */
  1540. ra_meta_pages(sbi, addr, nrpages, META_POR, true);
  1541. for (idx = addr; idx < addr + nrpages; idx++) {
  1542. struct page *page = get_tmp_page(sbi, idx);
  1543. rn = F2FS_NODE(page);
  1544. sum_entry->nid = rn->footer.nid;
  1545. sum_entry->version = 0;
  1546. sum_entry->ofs_in_node = 0;
  1547. sum_entry++;
  1548. f2fs_put_page(page, 1);
  1549. }
  1550. invalidate_mapping_pages(META_MAPPING(sbi), addr,
  1551. addr + nrpages);
  1552. }
  1553. return 0;
  1554. }
  1555. static void remove_nats_in_journal(struct f2fs_sb_info *sbi)
  1556. {
  1557. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1558. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
  1559. struct f2fs_summary_block *sum = curseg->sum_blk;
  1560. int i;
  1561. mutex_lock(&curseg->curseg_mutex);
  1562. for (i = 0; i < nats_in_cursum(sum); i++) {
  1563. struct nat_entry *ne;
  1564. struct f2fs_nat_entry raw_ne;
  1565. nid_t nid = le32_to_cpu(nid_in_journal(sum, i));
  1566. raw_ne = nat_in_journal(sum, i);
  1567. ne = __lookup_nat_cache(nm_i, nid);
  1568. if (!ne) {
  1569. ne = grab_nat_entry(nm_i, nid);
  1570. node_info_from_raw_nat(&ne->ni, &raw_ne);
  1571. }
  1572. __set_nat_cache_dirty(nm_i, ne);
  1573. }
  1574. update_nats_in_cursum(sum, -i);
  1575. mutex_unlock(&curseg->curseg_mutex);
  1576. }
  1577. static void __adjust_nat_entry_set(struct nat_entry_set *nes,
  1578. struct list_head *head, int max)
  1579. {
  1580. struct nat_entry_set *cur;
  1581. if (nes->entry_cnt >= max)
  1582. goto add_out;
  1583. list_for_each_entry(cur, head, set_list) {
  1584. if (cur->entry_cnt >= nes->entry_cnt) {
  1585. list_add(&nes->set_list, cur->set_list.prev);
  1586. return;
  1587. }
  1588. }
  1589. add_out:
  1590. list_add_tail(&nes->set_list, head);
  1591. }
  1592. static void __flush_nat_entry_set(struct f2fs_sb_info *sbi,
  1593. struct nat_entry_set *set)
  1594. {
  1595. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
  1596. struct f2fs_summary_block *sum = curseg->sum_blk;
  1597. nid_t start_nid = set->set * NAT_ENTRY_PER_BLOCK;
  1598. bool to_journal = true;
  1599. struct f2fs_nat_block *nat_blk;
  1600. struct nat_entry *ne, *cur;
  1601. struct page *page = NULL;
  1602. /*
  1603. * there are two steps to flush nat entries:
  1604. * #1, flush nat entries to journal in current hot data summary block.
  1605. * #2, flush nat entries to nat page.
  1606. */
  1607. if (!__has_cursum_space(sum, set->entry_cnt, NAT_JOURNAL))
  1608. to_journal = false;
  1609. if (to_journal) {
  1610. mutex_lock(&curseg->curseg_mutex);
  1611. } else {
  1612. page = get_next_nat_page(sbi, start_nid);
  1613. nat_blk = page_address(page);
  1614. f2fs_bug_on(sbi, !nat_blk);
  1615. }
  1616. /* flush dirty nats in nat entry set */
  1617. list_for_each_entry_safe(ne, cur, &set->entry_list, list) {
  1618. struct f2fs_nat_entry *raw_ne;
  1619. nid_t nid = nat_get_nid(ne);
  1620. int offset;
  1621. if (nat_get_blkaddr(ne) == NEW_ADDR)
  1622. continue;
  1623. if (to_journal) {
  1624. offset = lookup_journal_in_cursum(sum,
  1625. NAT_JOURNAL, nid, 1);
  1626. f2fs_bug_on(sbi, offset < 0);
  1627. raw_ne = &nat_in_journal(sum, offset);
  1628. nid_in_journal(sum, offset) = cpu_to_le32(nid);
  1629. } else {
  1630. raw_ne = &nat_blk->entries[nid - start_nid];
  1631. }
  1632. raw_nat_from_node_info(raw_ne, &ne->ni);
  1633. nat_reset_flag(ne);
  1634. __clear_nat_cache_dirty(NM_I(sbi), ne);
  1635. if (nat_get_blkaddr(ne) == NULL_ADDR)
  1636. add_free_nid(sbi, nid, false);
  1637. }
  1638. if (to_journal)
  1639. mutex_unlock(&curseg->curseg_mutex);
  1640. else
  1641. f2fs_put_page(page, 1);
  1642. f2fs_bug_on(sbi, set->entry_cnt);
  1643. radix_tree_delete(&NM_I(sbi)->nat_set_root, set->set);
  1644. kmem_cache_free(nat_entry_set_slab, set);
  1645. }
  1646. /*
  1647. * This function is called during the checkpointing process.
  1648. */
  1649. void flush_nat_entries(struct f2fs_sb_info *sbi)
  1650. {
  1651. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1652. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
  1653. struct f2fs_summary_block *sum = curseg->sum_blk;
  1654. struct nat_entry_set *setvec[SETVEC_SIZE];
  1655. struct nat_entry_set *set, *tmp;
  1656. unsigned int found;
  1657. nid_t set_idx = 0;
  1658. LIST_HEAD(sets);
  1659. if (!nm_i->dirty_nat_cnt)
  1660. return;
  1661. down_write(&nm_i->nat_tree_lock);
  1662. /*
  1663. * if there are no enough space in journal to store dirty nat
  1664. * entries, remove all entries from journal and merge them
  1665. * into nat entry set.
  1666. */
  1667. if (!__has_cursum_space(sum, nm_i->dirty_nat_cnt, NAT_JOURNAL))
  1668. remove_nats_in_journal(sbi);
  1669. while ((found = __gang_lookup_nat_set(nm_i,
  1670. set_idx, SETVEC_SIZE, setvec))) {
  1671. unsigned idx;
  1672. set_idx = setvec[found - 1]->set + 1;
  1673. for (idx = 0; idx < found; idx++)
  1674. __adjust_nat_entry_set(setvec[idx], &sets,
  1675. MAX_NAT_JENTRIES(sum));
  1676. }
  1677. /* flush dirty nats in nat entry set */
  1678. list_for_each_entry_safe(set, tmp, &sets, set_list)
  1679. __flush_nat_entry_set(sbi, set);
  1680. up_write(&nm_i->nat_tree_lock);
  1681. f2fs_bug_on(sbi, nm_i->dirty_nat_cnt);
  1682. }
  1683. static int init_node_manager(struct f2fs_sb_info *sbi)
  1684. {
  1685. struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi);
  1686. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1687. unsigned char *version_bitmap;
  1688. unsigned int nat_segs, nat_blocks;
  1689. nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr);
  1690. /* segment_count_nat includes pair segment so divide to 2. */
  1691. nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1;
  1692. nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg);
  1693. nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nat_blocks;
  1694. /* not used nids: 0, node, meta, (and root counted as valid node) */
  1695. nm_i->available_nids = nm_i->max_nid - F2FS_RESERVED_NODE_NUM;
  1696. nm_i->fcnt = 0;
  1697. nm_i->nat_cnt = 0;
  1698. nm_i->ram_thresh = DEF_RAM_THRESHOLD;
  1699. nm_i->ra_nid_pages = DEF_RA_NID_PAGES;
  1700. INIT_RADIX_TREE(&nm_i->free_nid_root, GFP_ATOMIC);
  1701. INIT_LIST_HEAD(&nm_i->free_nid_list);
  1702. INIT_RADIX_TREE(&nm_i->nat_root, GFP_NOIO);
  1703. INIT_RADIX_TREE(&nm_i->nat_set_root, GFP_NOIO);
  1704. INIT_LIST_HEAD(&nm_i->nat_entries);
  1705. mutex_init(&nm_i->build_lock);
  1706. spin_lock_init(&nm_i->free_nid_list_lock);
  1707. init_rwsem(&nm_i->nat_tree_lock);
  1708. nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
  1709. nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
  1710. version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
  1711. if (!version_bitmap)
  1712. return -EFAULT;
  1713. nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size,
  1714. GFP_KERNEL);
  1715. if (!nm_i->nat_bitmap)
  1716. return -ENOMEM;
  1717. return 0;
  1718. }
  1719. int build_node_manager(struct f2fs_sb_info *sbi)
  1720. {
  1721. int err;
  1722. sbi->nm_info = kzalloc(sizeof(struct f2fs_nm_info), GFP_KERNEL);
  1723. if (!sbi->nm_info)
  1724. return -ENOMEM;
  1725. err = init_node_manager(sbi);
  1726. if (err)
  1727. return err;
  1728. build_free_nids(sbi);
  1729. return 0;
  1730. }
  1731. void destroy_node_manager(struct f2fs_sb_info *sbi)
  1732. {
  1733. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1734. struct free_nid *i, *next_i;
  1735. struct nat_entry *natvec[NATVEC_SIZE];
  1736. struct nat_entry_set *setvec[SETVEC_SIZE];
  1737. nid_t nid = 0;
  1738. unsigned int found;
  1739. if (!nm_i)
  1740. return;
  1741. /* destroy free nid list */
  1742. spin_lock(&nm_i->free_nid_list_lock);
  1743. list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) {
  1744. f2fs_bug_on(sbi, i->state == NID_ALLOC);
  1745. __del_from_free_nid_list(nm_i, i);
  1746. nm_i->fcnt--;
  1747. spin_unlock(&nm_i->free_nid_list_lock);
  1748. kmem_cache_free(free_nid_slab, i);
  1749. spin_lock(&nm_i->free_nid_list_lock);
  1750. }
  1751. f2fs_bug_on(sbi, nm_i->fcnt);
  1752. spin_unlock(&nm_i->free_nid_list_lock);
  1753. /* destroy nat cache */
  1754. down_write(&nm_i->nat_tree_lock);
  1755. while ((found = __gang_lookup_nat_cache(nm_i,
  1756. nid, NATVEC_SIZE, natvec))) {
  1757. unsigned idx;
  1758. nid = nat_get_nid(natvec[found - 1]) + 1;
  1759. for (idx = 0; idx < found; idx++)
  1760. __del_from_nat_cache(nm_i, natvec[idx]);
  1761. }
  1762. f2fs_bug_on(sbi, nm_i->nat_cnt);
  1763. /* destroy nat set cache */
  1764. nid = 0;
  1765. while ((found = __gang_lookup_nat_set(nm_i,
  1766. nid, SETVEC_SIZE, setvec))) {
  1767. unsigned idx;
  1768. nid = setvec[found - 1]->set + 1;
  1769. for (idx = 0; idx < found; idx++) {
  1770. /* entry_cnt is not zero, when cp_error was occurred */
  1771. f2fs_bug_on(sbi, !list_empty(&setvec[idx]->entry_list));
  1772. radix_tree_delete(&nm_i->nat_set_root, setvec[idx]->set);
  1773. kmem_cache_free(nat_entry_set_slab, setvec[idx]);
  1774. }
  1775. }
  1776. up_write(&nm_i->nat_tree_lock);
  1777. kfree(nm_i->nat_bitmap);
  1778. sbi->nm_info = NULL;
  1779. kfree(nm_i);
  1780. }
  1781. int __init create_node_manager_caches(void)
  1782. {
  1783. nat_entry_slab = f2fs_kmem_cache_create("nat_entry",
  1784. sizeof(struct nat_entry));
  1785. if (!nat_entry_slab)
  1786. goto fail;
  1787. free_nid_slab = f2fs_kmem_cache_create("free_nid",
  1788. sizeof(struct free_nid));
  1789. if (!free_nid_slab)
  1790. goto destroy_nat_entry;
  1791. nat_entry_set_slab = f2fs_kmem_cache_create("nat_entry_set",
  1792. sizeof(struct nat_entry_set));
  1793. if (!nat_entry_set_slab)
  1794. goto destroy_free_nid;
  1795. return 0;
  1796. destroy_free_nid:
  1797. kmem_cache_destroy(free_nid_slab);
  1798. destroy_nat_entry:
  1799. kmem_cache_destroy(nat_entry_slab);
  1800. fail:
  1801. return -ENOMEM;
  1802. }
  1803. void destroy_node_manager_caches(void)
  1804. {
  1805. kmem_cache_destroy(nat_entry_set_slab);
  1806. kmem_cache_destroy(free_nid_slab);
  1807. kmem_cache_destroy(nat_entry_slab);
  1808. }