node.c 57 KB

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