node.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804
  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->nid_cnt[FREE_NID_LIST] *
  42. sizeof(struct free_nid)) >> 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. if (excess_cached_nats(sbi))
  49. res = false;
  50. } else if (type == DIRTY_DENTS) {
  51. if (sbi->sb->s_bdi->wb.dirty_exceeded)
  52. return false;
  53. mem_size = get_pages(sbi, F2FS_DIRTY_DENTS);
  54. res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
  55. } else if (type == INO_ENTRIES) {
  56. int i;
  57. for (i = 0; i <= UPDATE_INO; i++)
  58. mem_size += sbi->im[i].ino_num *
  59. sizeof(struct ino_entry);
  60. mem_size >>= PAGE_SHIFT;
  61. res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
  62. } else if (type == EXTENT_CACHE) {
  63. mem_size = (atomic_read(&sbi->total_ext_tree) *
  64. sizeof(struct extent_tree) +
  65. atomic_read(&sbi->total_ext_node) *
  66. sizeof(struct extent_node)) >> PAGE_SHIFT;
  67. res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
  68. } else {
  69. if (!sbi->sb->s_bdi->wb.dirty_exceeded)
  70. return true;
  71. }
  72. return res;
  73. }
  74. static void clear_node_page_dirty(struct page *page)
  75. {
  76. struct address_space *mapping = page->mapping;
  77. unsigned int long flags;
  78. if (PageDirty(page)) {
  79. spin_lock_irqsave(&mapping->tree_lock, flags);
  80. radix_tree_tag_clear(&mapping->page_tree,
  81. page_index(page),
  82. PAGECACHE_TAG_DIRTY);
  83. spin_unlock_irqrestore(&mapping->tree_lock, flags);
  84. clear_page_dirty_for_io(page);
  85. dec_page_count(F2FS_M_SB(mapping), F2FS_DIRTY_NODES);
  86. }
  87. ClearPageUptodate(page);
  88. }
  89. static struct page *get_current_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
  90. {
  91. pgoff_t index = current_nat_addr(sbi, nid);
  92. return get_meta_page(sbi, index);
  93. }
  94. static struct page *get_next_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
  95. {
  96. struct page *src_page;
  97. struct page *dst_page;
  98. pgoff_t src_off;
  99. pgoff_t dst_off;
  100. void *src_addr;
  101. void *dst_addr;
  102. struct f2fs_nm_info *nm_i = NM_I(sbi);
  103. src_off = current_nat_addr(sbi, nid);
  104. dst_off = next_nat_addr(sbi, src_off);
  105. /* get current nat block page with lock */
  106. src_page = get_meta_page(sbi, src_off);
  107. dst_page = grab_meta_page(sbi, dst_off);
  108. f2fs_bug_on(sbi, PageDirty(src_page));
  109. src_addr = page_address(src_page);
  110. dst_addr = page_address(dst_page);
  111. memcpy(dst_addr, src_addr, PAGE_SIZE);
  112. set_page_dirty(dst_page);
  113. f2fs_put_page(src_page, 1);
  114. set_to_next_nat(nm_i, nid);
  115. return dst_page;
  116. }
  117. static struct nat_entry *__lookup_nat_cache(struct f2fs_nm_info *nm_i, nid_t n)
  118. {
  119. return radix_tree_lookup(&nm_i->nat_root, n);
  120. }
  121. static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info *nm_i,
  122. nid_t start, unsigned int nr, struct nat_entry **ep)
  123. {
  124. return radix_tree_gang_lookup(&nm_i->nat_root, (void **)ep, start, nr);
  125. }
  126. static void __del_from_nat_cache(struct f2fs_nm_info *nm_i, struct nat_entry *e)
  127. {
  128. list_del(&e->list);
  129. radix_tree_delete(&nm_i->nat_root, nat_get_nid(e));
  130. nm_i->nat_cnt--;
  131. kmem_cache_free(nat_entry_slab, e);
  132. }
  133. static void __set_nat_cache_dirty(struct f2fs_nm_info *nm_i,
  134. struct nat_entry *ne)
  135. {
  136. nid_t set = NAT_BLOCK_OFFSET(ne->ni.nid);
  137. struct nat_entry_set *head;
  138. head = radix_tree_lookup(&nm_i->nat_set_root, set);
  139. if (!head) {
  140. head = f2fs_kmem_cache_alloc(nat_entry_set_slab, GFP_NOFS);
  141. INIT_LIST_HEAD(&head->entry_list);
  142. INIT_LIST_HEAD(&head->set_list);
  143. head->set = set;
  144. head->entry_cnt = 0;
  145. f2fs_radix_tree_insert(&nm_i->nat_set_root, set, head);
  146. }
  147. if (get_nat_flag(ne, IS_DIRTY))
  148. goto refresh_list;
  149. nm_i->dirty_nat_cnt++;
  150. head->entry_cnt++;
  151. set_nat_flag(ne, IS_DIRTY, true);
  152. refresh_list:
  153. if (nat_get_blkaddr(ne) == NEW_ADDR)
  154. list_del_init(&ne->list);
  155. else
  156. list_move_tail(&ne->list, &head->entry_list);
  157. }
  158. static void __clear_nat_cache_dirty(struct f2fs_nm_info *nm_i,
  159. struct nat_entry_set *set, struct nat_entry *ne)
  160. {
  161. list_move_tail(&ne->list, &nm_i->nat_entries);
  162. set_nat_flag(ne, IS_DIRTY, false);
  163. set->entry_cnt--;
  164. nm_i->dirty_nat_cnt--;
  165. }
  166. static unsigned int __gang_lookup_nat_set(struct f2fs_nm_info *nm_i,
  167. nid_t start, unsigned int nr, struct nat_entry_set **ep)
  168. {
  169. return radix_tree_gang_lookup(&nm_i->nat_set_root, (void **)ep,
  170. start, nr);
  171. }
  172. int need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid)
  173. {
  174. struct f2fs_nm_info *nm_i = NM_I(sbi);
  175. struct nat_entry *e;
  176. bool need = false;
  177. down_read(&nm_i->nat_tree_lock);
  178. e = __lookup_nat_cache(nm_i, nid);
  179. if (e) {
  180. if (!get_nat_flag(e, IS_CHECKPOINTED) &&
  181. !get_nat_flag(e, HAS_FSYNCED_INODE))
  182. need = true;
  183. }
  184. up_read(&nm_i->nat_tree_lock);
  185. return need;
  186. }
  187. bool is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid)
  188. {
  189. struct f2fs_nm_info *nm_i = NM_I(sbi);
  190. struct nat_entry *e;
  191. bool is_cp = true;
  192. down_read(&nm_i->nat_tree_lock);
  193. e = __lookup_nat_cache(nm_i, nid);
  194. if (e && !get_nat_flag(e, IS_CHECKPOINTED))
  195. is_cp = false;
  196. up_read(&nm_i->nat_tree_lock);
  197. return is_cp;
  198. }
  199. bool need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino)
  200. {
  201. struct f2fs_nm_info *nm_i = NM_I(sbi);
  202. struct nat_entry *e;
  203. bool need_update = true;
  204. down_read(&nm_i->nat_tree_lock);
  205. e = __lookup_nat_cache(nm_i, ino);
  206. if (e && get_nat_flag(e, HAS_LAST_FSYNC) &&
  207. (get_nat_flag(e, IS_CHECKPOINTED) ||
  208. get_nat_flag(e, HAS_FSYNCED_INODE)))
  209. need_update = false;
  210. up_read(&nm_i->nat_tree_lock);
  211. return need_update;
  212. }
  213. static struct nat_entry *grab_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid,
  214. bool no_fail)
  215. {
  216. struct nat_entry *new;
  217. if (no_fail) {
  218. new = f2fs_kmem_cache_alloc(nat_entry_slab, GFP_NOFS);
  219. f2fs_radix_tree_insert(&nm_i->nat_root, nid, new);
  220. } else {
  221. new = kmem_cache_alloc(nat_entry_slab, GFP_NOFS);
  222. if (!new)
  223. return NULL;
  224. if (radix_tree_insert(&nm_i->nat_root, nid, new)) {
  225. kmem_cache_free(nat_entry_slab, new);
  226. return NULL;
  227. }
  228. }
  229. memset(new, 0, sizeof(struct nat_entry));
  230. nat_set_nid(new, nid);
  231. nat_reset_flag(new);
  232. list_add_tail(&new->list, &nm_i->nat_entries);
  233. nm_i->nat_cnt++;
  234. return new;
  235. }
  236. static void cache_nat_entry(struct f2fs_sb_info *sbi, nid_t nid,
  237. struct f2fs_nat_entry *ne)
  238. {
  239. struct f2fs_nm_info *nm_i = NM_I(sbi);
  240. struct nat_entry *e;
  241. e = __lookup_nat_cache(nm_i, nid);
  242. if (!e) {
  243. e = grab_nat_entry(nm_i, nid, false);
  244. if (e)
  245. node_info_from_raw_nat(&e->ni, ne);
  246. } else {
  247. f2fs_bug_on(sbi, nat_get_ino(e) != le32_to_cpu(ne->ino) ||
  248. nat_get_blkaddr(e) !=
  249. le32_to_cpu(ne->block_addr) ||
  250. nat_get_version(e) != ne->version);
  251. }
  252. }
  253. static void set_node_addr(struct f2fs_sb_info *sbi, struct node_info *ni,
  254. block_t new_blkaddr, bool fsync_done)
  255. {
  256. struct f2fs_nm_info *nm_i = NM_I(sbi);
  257. struct nat_entry *e;
  258. down_write(&nm_i->nat_tree_lock);
  259. e = __lookup_nat_cache(nm_i, ni->nid);
  260. if (!e) {
  261. e = grab_nat_entry(nm_i, ni->nid, true);
  262. copy_node_info(&e->ni, ni);
  263. f2fs_bug_on(sbi, ni->blk_addr == NEW_ADDR);
  264. } else if (new_blkaddr == NEW_ADDR) {
  265. /*
  266. * when nid is reallocated,
  267. * previous nat entry can be remained in nat cache.
  268. * So, reinitialize it with new information.
  269. */
  270. copy_node_info(&e->ni, ni);
  271. f2fs_bug_on(sbi, ni->blk_addr != NULL_ADDR);
  272. }
  273. /* sanity check */
  274. f2fs_bug_on(sbi, nat_get_blkaddr(e) != ni->blk_addr);
  275. f2fs_bug_on(sbi, nat_get_blkaddr(e) == NULL_ADDR &&
  276. new_blkaddr == NULL_ADDR);
  277. f2fs_bug_on(sbi, nat_get_blkaddr(e) == NEW_ADDR &&
  278. new_blkaddr == NEW_ADDR);
  279. f2fs_bug_on(sbi, nat_get_blkaddr(e) != NEW_ADDR &&
  280. nat_get_blkaddr(e) != NULL_ADDR &&
  281. new_blkaddr == NEW_ADDR);
  282. /* increment version no as node is removed */
  283. if (nat_get_blkaddr(e) != NEW_ADDR && new_blkaddr == NULL_ADDR) {
  284. unsigned char version = nat_get_version(e);
  285. nat_set_version(e, inc_node_version(version));
  286. /* in order to reuse the nid */
  287. if (nm_i->next_scan_nid > ni->nid)
  288. nm_i->next_scan_nid = ni->nid;
  289. }
  290. /* change address */
  291. nat_set_blkaddr(e, new_blkaddr);
  292. if (new_blkaddr == NEW_ADDR || new_blkaddr == NULL_ADDR)
  293. set_nat_flag(e, IS_CHECKPOINTED, false);
  294. __set_nat_cache_dirty(nm_i, e);
  295. /* update fsync_mark if its inode nat entry is still alive */
  296. if (ni->nid != ni->ino)
  297. e = __lookup_nat_cache(nm_i, ni->ino);
  298. if (e) {
  299. if (fsync_done && ni->nid == ni->ino)
  300. set_nat_flag(e, HAS_FSYNCED_INODE, true);
  301. set_nat_flag(e, HAS_LAST_FSYNC, fsync_done);
  302. }
  303. up_write(&nm_i->nat_tree_lock);
  304. }
  305. int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink)
  306. {
  307. struct f2fs_nm_info *nm_i = NM_I(sbi);
  308. int nr = nr_shrink;
  309. if (!down_write_trylock(&nm_i->nat_tree_lock))
  310. return 0;
  311. while (nr_shrink && !list_empty(&nm_i->nat_entries)) {
  312. struct nat_entry *ne;
  313. ne = list_first_entry(&nm_i->nat_entries,
  314. struct nat_entry, list);
  315. __del_from_nat_cache(nm_i, ne);
  316. nr_shrink--;
  317. }
  318. up_write(&nm_i->nat_tree_lock);
  319. return nr - nr_shrink;
  320. }
  321. /*
  322. * This function always returns success
  323. */
  324. void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni)
  325. {
  326. struct f2fs_nm_info *nm_i = NM_I(sbi);
  327. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
  328. struct f2fs_journal *journal = curseg->journal;
  329. nid_t start_nid = START_NID(nid);
  330. struct f2fs_nat_block *nat_blk;
  331. struct page *page = NULL;
  332. struct f2fs_nat_entry ne;
  333. struct nat_entry *e;
  334. pgoff_t index;
  335. int i;
  336. ni->nid = nid;
  337. /* Check nat cache */
  338. down_read(&nm_i->nat_tree_lock);
  339. e = __lookup_nat_cache(nm_i, nid);
  340. if (e) {
  341. ni->ino = nat_get_ino(e);
  342. ni->blk_addr = nat_get_blkaddr(e);
  343. ni->version = nat_get_version(e);
  344. up_read(&nm_i->nat_tree_lock);
  345. return;
  346. }
  347. memset(&ne, 0, sizeof(struct f2fs_nat_entry));
  348. /* Check current segment summary */
  349. down_read(&curseg->journal_rwsem);
  350. i = lookup_journal_in_cursum(journal, NAT_JOURNAL, nid, 0);
  351. if (i >= 0) {
  352. ne = nat_in_journal(journal, i);
  353. node_info_from_raw_nat(ni, &ne);
  354. }
  355. up_read(&curseg->journal_rwsem);
  356. if (i >= 0) {
  357. up_read(&nm_i->nat_tree_lock);
  358. goto cache;
  359. }
  360. /* Fill node_info from nat page */
  361. index = current_nat_addr(sbi, nid);
  362. up_read(&nm_i->nat_tree_lock);
  363. page = get_meta_page(sbi, index);
  364. nat_blk = (struct f2fs_nat_block *)page_address(page);
  365. ne = nat_blk->entries[nid - start_nid];
  366. node_info_from_raw_nat(ni, &ne);
  367. f2fs_put_page(page, 1);
  368. cache:
  369. /* cache nat entry */
  370. down_write(&nm_i->nat_tree_lock);
  371. cache_nat_entry(sbi, nid, &ne);
  372. up_write(&nm_i->nat_tree_lock);
  373. }
  374. /*
  375. * readahead MAX_RA_NODE number of node pages.
  376. */
  377. static void ra_node_pages(struct page *parent, int start, int n)
  378. {
  379. struct f2fs_sb_info *sbi = F2FS_P_SB(parent);
  380. struct blk_plug plug;
  381. int i, end;
  382. nid_t nid;
  383. blk_start_plug(&plug);
  384. /* Then, try readahead for siblings of the desired node */
  385. end = start + n;
  386. end = min(end, NIDS_PER_BLOCK);
  387. for (i = start; i < end; i++) {
  388. nid = get_nid(parent, i, false);
  389. ra_node_page(sbi, nid);
  390. }
  391. blk_finish_plug(&plug);
  392. }
  393. pgoff_t get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs)
  394. {
  395. const long direct_index = ADDRS_PER_INODE(dn->inode);
  396. const long direct_blks = ADDRS_PER_BLOCK;
  397. const long indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
  398. unsigned int skipped_unit = ADDRS_PER_BLOCK;
  399. int cur_level = dn->cur_level;
  400. int max_level = dn->max_level;
  401. pgoff_t base = 0;
  402. if (!dn->max_level)
  403. return pgofs + 1;
  404. while (max_level-- > cur_level)
  405. skipped_unit *= NIDS_PER_BLOCK;
  406. switch (dn->max_level) {
  407. case 3:
  408. base += 2 * indirect_blks;
  409. case 2:
  410. base += 2 * direct_blks;
  411. case 1:
  412. base += direct_index;
  413. break;
  414. default:
  415. f2fs_bug_on(F2FS_I_SB(dn->inode), 1);
  416. }
  417. return ((pgofs - base) / skipped_unit + 1) * skipped_unit + base;
  418. }
  419. /*
  420. * The maximum depth is four.
  421. * Offset[0] will have raw inode offset.
  422. */
  423. static int get_node_path(struct inode *inode, long block,
  424. int offset[4], unsigned int noffset[4])
  425. {
  426. const long direct_index = ADDRS_PER_INODE(inode);
  427. const long direct_blks = ADDRS_PER_BLOCK;
  428. const long dptrs_per_blk = NIDS_PER_BLOCK;
  429. const long indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
  430. const long dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
  431. int n = 0;
  432. int level = 0;
  433. noffset[0] = 0;
  434. if (block < direct_index) {
  435. offset[n] = block;
  436. goto got;
  437. }
  438. block -= direct_index;
  439. if (block < direct_blks) {
  440. offset[n++] = NODE_DIR1_BLOCK;
  441. noffset[n] = 1;
  442. offset[n] = block;
  443. level = 1;
  444. goto got;
  445. }
  446. block -= direct_blks;
  447. if (block < direct_blks) {
  448. offset[n++] = NODE_DIR2_BLOCK;
  449. noffset[n] = 2;
  450. offset[n] = block;
  451. level = 1;
  452. goto got;
  453. }
  454. block -= direct_blks;
  455. if (block < indirect_blks) {
  456. offset[n++] = NODE_IND1_BLOCK;
  457. noffset[n] = 3;
  458. offset[n++] = block / direct_blks;
  459. noffset[n] = 4 + offset[n - 1];
  460. offset[n] = block % direct_blks;
  461. level = 2;
  462. goto got;
  463. }
  464. block -= indirect_blks;
  465. if (block < indirect_blks) {
  466. offset[n++] = NODE_IND2_BLOCK;
  467. noffset[n] = 4 + dptrs_per_blk;
  468. offset[n++] = block / direct_blks;
  469. noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
  470. offset[n] = block % direct_blks;
  471. level = 2;
  472. goto got;
  473. }
  474. block -= indirect_blks;
  475. if (block < dindirect_blks) {
  476. offset[n++] = NODE_DIND_BLOCK;
  477. noffset[n] = 5 + (dptrs_per_blk * 2);
  478. offset[n++] = block / indirect_blks;
  479. noffset[n] = 6 + (dptrs_per_blk * 2) +
  480. offset[n - 1] * (dptrs_per_blk + 1);
  481. offset[n++] = (block / direct_blks) % dptrs_per_blk;
  482. noffset[n] = 7 + (dptrs_per_blk * 2) +
  483. offset[n - 2] * (dptrs_per_blk + 1) +
  484. offset[n - 1];
  485. offset[n] = block % direct_blks;
  486. level = 3;
  487. goto got;
  488. } else {
  489. BUG();
  490. }
  491. got:
  492. return level;
  493. }
  494. /*
  495. * Caller should call f2fs_put_dnode(dn).
  496. * Also, it should grab and release a rwsem by calling f2fs_lock_op() and
  497. * f2fs_unlock_op() only if ro is not set RDONLY_NODE.
  498. * In the case of RDONLY_NODE, we don't need to care about mutex.
  499. */
  500. int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode)
  501. {
  502. struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
  503. struct page *npage[4];
  504. struct page *parent = NULL;
  505. int offset[4];
  506. unsigned int noffset[4];
  507. nid_t nids[4];
  508. int level, i = 0;
  509. int err = 0;
  510. level = get_node_path(dn->inode, index, offset, noffset);
  511. nids[0] = dn->inode->i_ino;
  512. npage[0] = dn->inode_page;
  513. if (!npage[0]) {
  514. npage[0] = get_node_page(sbi, nids[0]);
  515. if (IS_ERR(npage[0]))
  516. return PTR_ERR(npage[0]);
  517. }
  518. /* if inline_data is set, should not report any block indices */
  519. if (f2fs_has_inline_data(dn->inode) && index) {
  520. err = -ENOENT;
  521. f2fs_put_page(npage[0], 1);
  522. goto release_out;
  523. }
  524. parent = npage[0];
  525. if (level != 0)
  526. nids[1] = get_nid(parent, offset[0], true);
  527. dn->inode_page = npage[0];
  528. dn->inode_page_locked = true;
  529. /* get indirect or direct nodes */
  530. for (i = 1; i <= level; i++) {
  531. bool done = false;
  532. if (!nids[i] && mode == ALLOC_NODE) {
  533. /* alloc new node */
  534. if (!alloc_nid(sbi, &(nids[i]))) {
  535. err = -ENOSPC;
  536. goto release_pages;
  537. }
  538. dn->nid = nids[i];
  539. npage[i] = new_node_page(dn, noffset[i], NULL);
  540. if (IS_ERR(npage[i])) {
  541. alloc_nid_failed(sbi, nids[i]);
  542. err = PTR_ERR(npage[i]);
  543. goto release_pages;
  544. }
  545. set_nid(parent, offset[i - 1], nids[i], i == 1);
  546. alloc_nid_done(sbi, nids[i]);
  547. done = true;
  548. } else if (mode == LOOKUP_NODE_RA && i == level && level > 1) {
  549. npage[i] = get_node_page_ra(parent, offset[i - 1]);
  550. if (IS_ERR(npage[i])) {
  551. err = PTR_ERR(npage[i]);
  552. goto release_pages;
  553. }
  554. done = true;
  555. }
  556. if (i == 1) {
  557. dn->inode_page_locked = false;
  558. unlock_page(parent);
  559. } else {
  560. f2fs_put_page(parent, 1);
  561. }
  562. if (!done) {
  563. npage[i] = get_node_page(sbi, nids[i]);
  564. if (IS_ERR(npage[i])) {
  565. err = PTR_ERR(npage[i]);
  566. f2fs_put_page(npage[0], 0);
  567. goto release_out;
  568. }
  569. }
  570. if (i < level) {
  571. parent = npage[i];
  572. nids[i + 1] = get_nid(parent, offset[i], false);
  573. }
  574. }
  575. dn->nid = nids[level];
  576. dn->ofs_in_node = offset[level];
  577. dn->node_page = npage[level];
  578. dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
  579. return 0;
  580. release_pages:
  581. f2fs_put_page(parent, 1);
  582. if (i > 1)
  583. f2fs_put_page(npage[0], 0);
  584. release_out:
  585. dn->inode_page = NULL;
  586. dn->node_page = NULL;
  587. if (err == -ENOENT) {
  588. dn->cur_level = i;
  589. dn->max_level = level;
  590. dn->ofs_in_node = offset[level];
  591. }
  592. return err;
  593. }
  594. static void truncate_node(struct dnode_of_data *dn)
  595. {
  596. struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
  597. struct node_info ni;
  598. get_node_info(sbi, dn->nid, &ni);
  599. if (dn->inode->i_blocks == 0) {
  600. f2fs_bug_on(sbi, ni.blk_addr != NULL_ADDR);
  601. goto invalidate;
  602. }
  603. f2fs_bug_on(sbi, ni.blk_addr == NULL_ADDR);
  604. /* Deallocate node address */
  605. invalidate_blocks(sbi, ni.blk_addr);
  606. dec_valid_node_count(sbi, dn->inode);
  607. set_node_addr(sbi, &ni, NULL_ADDR, false);
  608. if (dn->nid == dn->inode->i_ino) {
  609. remove_orphan_inode(sbi, dn->nid);
  610. dec_valid_inode_count(sbi);
  611. f2fs_inode_synced(dn->inode);
  612. }
  613. invalidate:
  614. clear_node_page_dirty(dn->node_page);
  615. set_sbi_flag(sbi, SBI_IS_DIRTY);
  616. f2fs_put_page(dn->node_page, 1);
  617. invalidate_mapping_pages(NODE_MAPPING(sbi),
  618. dn->node_page->index, dn->node_page->index);
  619. dn->node_page = NULL;
  620. trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr);
  621. }
  622. static int truncate_dnode(struct dnode_of_data *dn)
  623. {
  624. struct page *page;
  625. if (dn->nid == 0)
  626. return 1;
  627. /* get direct node */
  628. page = get_node_page(F2FS_I_SB(dn->inode), dn->nid);
  629. if (IS_ERR(page) && PTR_ERR(page) == -ENOENT)
  630. return 1;
  631. else if (IS_ERR(page))
  632. return PTR_ERR(page);
  633. /* Make dnode_of_data for parameter */
  634. dn->node_page = page;
  635. dn->ofs_in_node = 0;
  636. truncate_data_blocks(dn);
  637. truncate_node(dn);
  638. return 1;
  639. }
  640. static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs,
  641. int ofs, int depth)
  642. {
  643. struct dnode_of_data rdn = *dn;
  644. struct page *page;
  645. struct f2fs_node *rn;
  646. nid_t child_nid;
  647. unsigned int child_nofs;
  648. int freed = 0;
  649. int i, ret;
  650. if (dn->nid == 0)
  651. return NIDS_PER_BLOCK + 1;
  652. trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr);
  653. page = get_node_page(F2FS_I_SB(dn->inode), dn->nid);
  654. if (IS_ERR(page)) {
  655. trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page));
  656. return PTR_ERR(page);
  657. }
  658. ra_node_pages(page, ofs, NIDS_PER_BLOCK);
  659. rn = F2FS_NODE(page);
  660. if (depth < 3) {
  661. for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) {
  662. child_nid = le32_to_cpu(rn->in.nid[i]);
  663. if (child_nid == 0)
  664. continue;
  665. rdn.nid = child_nid;
  666. ret = truncate_dnode(&rdn);
  667. if (ret < 0)
  668. goto out_err;
  669. if (set_nid(page, i, 0, false))
  670. dn->node_changed = true;
  671. }
  672. } else {
  673. child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1;
  674. for (i = ofs; i < NIDS_PER_BLOCK; i++) {
  675. child_nid = le32_to_cpu(rn->in.nid[i]);
  676. if (child_nid == 0) {
  677. child_nofs += NIDS_PER_BLOCK + 1;
  678. continue;
  679. }
  680. rdn.nid = child_nid;
  681. ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1);
  682. if (ret == (NIDS_PER_BLOCK + 1)) {
  683. if (set_nid(page, i, 0, false))
  684. dn->node_changed = true;
  685. child_nofs += ret;
  686. } else if (ret < 0 && ret != -ENOENT) {
  687. goto out_err;
  688. }
  689. }
  690. freed = child_nofs;
  691. }
  692. if (!ofs) {
  693. /* remove current indirect node */
  694. dn->node_page = page;
  695. truncate_node(dn);
  696. freed++;
  697. } else {
  698. f2fs_put_page(page, 1);
  699. }
  700. trace_f2fs_truncate_nodes_exit(dn->inode, freed);
  701. return freed;
  702. out_err:
  703. f2fs_put_page(page, 1);
  704. trace_f2fs_truncate_nodes_exit(dn->inode, ret);
  705. return ret;
  706. }
  707. static int truncate_partial_nodes(struct dnode_of_data *dn,
  708. struct f2fs_inode *ri, int *offset, int depth)
  709. {
  710. struct page *pages[2];
  711. nid_t nid[3];
  712. nid_t child_nid;
  713. int err = 0;
  714. int i;
  715. int idx = depth - 2;
  716. nid[0] = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
  717. if (!nid[0])
  718. return 0;
  719. /* get indirect nodes in the path */
  720. for (i = 0; i < idx + 1; i++) {
  721. /* reference count'll be increased */
  722. pages[i] = get_node_page(F2FS_I_SB(dn->inode), nid[i]);
  723. if (IS_ERR(pages[i])) {
  724. err = PTR_ERR(pages[i]);
  725. idx = i - 1;
  726. goto fail;
  727. }
  728. nid[i + 1] = get_nid(pages[i], offset[i + 1], false);
  729. }
  730. ra_node_pages(pages[idx], offset[idx + 1], NIDS_PER_BLOCK);
  731. /* free direct nodes linked to a partial indirect node */
  732. for (i = offset[idx + 1]; i < NIDS_PER_BLOCK; i++) {
  733. child_nid = get_nid(pages[idx], i, false);
  734. if (!child_nid)
  735. continue;
  736. dn->nid = child_nid;
  737. err = truncate_dnode(dn);
  738. if (err < 0)
  739. goto fail;
  740. if (set_nid(pages[idx], i, 0, false))
  741. dn->node_changed = true;
  742. }
  743. if (offset[idx + 1] == 0) {
  744. dn->node_page = pages[idx];
  745. dn->nid = nid[idx];
  746. truncate_node(dn);
  747. } else {
  748. f2fs_put_page(pages[idx], 1);
  749. }
  750. offset[idx]++;
  751. offset[idx + 1] = 0;
  752. idx--;
  753. fail:
  754. for (i = idx; i >= 0; i--)
  755. f2fs_put_page(pages[i], 1);
  756. trace_f2fs_truncate_partial_nodes(dn->inode, nid, depth, err);
  757. return err;
  758. }
  759. /*
  760. * All the block addresses of data and nodes should be nullified.
  761. */
  762. int truncate_inode_blocks(struct inode *inode, pgoff_t from)
  763. {
  764. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  765. int err = 0, cont = 1;
  766. int level, offset[4], noffset[4];
  767. unsigned int nofs = 0;
  768. struct f2fs_inode *ri;
  769. struct dnode_of_data dn;
  770. struct page *page;
  771. trace_f2fs_truncate_inode_blocks_enter(inode, from);
  772. level = get_node_path(inode, from, offset, noffset);
  773. page = get_node_page(sbi, inode->i_ino);
  774. if (IS_ERR(page)) {
  775. trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(page));
  776. return PTR_ERR(page);
  777. }
  778. set_new_dnode(&dn, inode, page, NULL, 0);
  779. unlock_page(page);
  780. ri = F2FS_INODE(page);
  781. switch (level) {
  782. case 0:
  783. case 1:
  784. nofs = noffset[1];
  785. break;
  786. case 2:
  787. nofs = noffset[1];
  788. if (!offset[level - 1])
  789. goto skip_partial;
  790. err = truncate_partial_nodes(&dn, ri, offset, level);
  791. if (err < 0 && err != -ENOENT)
  792. goto fail;
  793. nofs += 1 + NIDS_PER_BLOCK;
  794. break;
  795. case 3:
  796. nofs = 5 + 2 * NIDS_PER_BLOCK;
  797. if (!offset[level - 1])
  798. goto skip_partial;
  799. err = truncate_partial_nodes(&dn, ri, offset, level);
  800. if (err < 0 && err != -ENOENT)
  801. goto fail;
  802. break;
  803. default:
  804. BUG();
  805. }
  806. skip_partial:
  807. while (cont) {
  808. dn.nid = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
  809. switch (offset[0]) {
  810. case NODE_DIR1_BLOCK:
  811. case NODE_DIR2_BLOCK:
  812. err = truncate_dnode(&dn);
  813. break;
  814. case NODE_IND1_BLOCK:
  815. case NODE_IND2_BLOCK:
  816. err = truncate_nodes(&dn, nofs, offset[1], 2);
  817. break;
  818. case NODE_DIND_BLOCK:
  819. err = truncate_nodes(&dn, nofs, offset[1], 3);
  820. cont = 0;
  821. break;
  822. default:
  823. BUG();
  824. }
  825. if (err < 0 && err != -ENOENT)
  826. goto fail;
  827. if (offset[1] == 0 &&
  828. ri->i_nid[offset[0] - NODE_DIR1_BLOCK]) {
  829. lock_page(page);
  830. BUG_ON(page->mapping != NODE_MAPPING(sbi));
  831. f2fs_wait_on_page_writeback(page, NODE, true);
  832. ri->i_nid[offset[0] - NODE_DIR1_BLOCK] = 0;
  833. set_page_dirty(page);
  834. unlock_page(page);
  835. }
  836. offset[1] = 0;
  837. offset[0]++;
  838. nofs += err;
  839. }
  840. fail:
  841. f2fs_put_page(page, 0);
  842. trace_f2fs_truncate_inode_blocks_exit(inode, err);
  843. return err > 0 ? 0 : err;
  844. }
  845. int truncate_xattr_node(struct inode *inode, struct page *page)
  846. {
  847. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  848. nid_t nid = F2FS_I(inode)->i_xattr_nid;
  849. struct dnode_of_data dn;
  850. struct page *npage;
  851. if (!nid)
  852. return 0;
  853. npage = get_node_page(sbi, nid);
  854. if (IS_ERR(npage))
  855. return PTR_ERR(npage);
  856. f2fs_i_xnid_write(inode, 0);
  857. set_new_dnode(&dn, inode, page, npage, nid);
  858. if (page)
  859. dn.inode_page_locked = true;
  860. truncate_node(&dn);
  861. return 0;
  862. }
  863. /*
  864. * Caller should grab and release a rwsem by calling f2fs_lock_op() and
  865. * f2fs_unlock_op().
  866. */
  867. int remove_inode_page(struct inode *inode)
  868. {
  869. struct dnode_of_data dn;
  870. int err;
  871. set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
  872. err = get_dnode_of_data(&dn, 0, LOOKUP_NODE);
  873. if (err)
  874. return err;
  875. err = truncate_xattr_node(inode, dn.inode_page);
  876. if (err) {
  877. f2fs_put_dnode(&dn);
  878. return err;
  879. }
  880. /* remove potential inline_data blocks */
  881. if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  882. S_ISLNK(inode->i_mode))
  883. truncate_data_blocks_range(&dn, 1);
  884. /* 0 is possible, after f2fs_new_inode() has failed */
  885. f2fs_bug_on(F2FS_I_SB(inode),
  886. inode->i_blocks != 0 && inode->i_blocks != 8);
  887. /* will put inode & node pages */
  888. truncate_node(&dn);
  889. return 0;
  890. }
  891. struct page *new_inode_page(struct inode *inode)
  892. {
  893. struct dnode_of_data dn;
  894. /* allocate inode page for new inode */
  895. set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
  896. /* caller should f2fs_put_page(page, 1); */
  897. return new_node_page(&dn, 0, NULL);
  898. }
  899. struct page *new_node_page(struct dnode_of_data *dn,
  900. unsigned int ofs, struct page *ipage)
  901. {
  902. struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
  903. struct node_info new_ni;
  904. struct page *page;
  905. int err;
  906. if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
  907. return ERR_PTR(-EPERM);
  908. page = f2fs_grab_cache_page(NODE_MAPPING(sbi), dn->nid, false);
  909. if (!page)
  910. return ERR_PTR(-ENOMEM);
  911. if (unlikely(!inc_valid_node_count(sbi, dn->inode))) {
  912. err = -ENOSPC;
  913. goto fail;
  914. }
  915. #ifdef CONFIG_F2FS_CHECK_FS
  916. get_node_info(sbi, dn->nid, &new_ni);
  917. f2fs_bug_on(sbi, new_ni.blk_addr != NULL_ADDR);
  918. #endif
  919. new_ni.nid = dn->nid;
  920. new_ni.ino = dn->inode->i_ino;
  921. new_ni.blk_addr = NULL_ADDR;
  922. new_ni.flag = 0;
  923. new_ni.version = 0;
  924. set_node_addr(sbi, &new_ni, NEW_ADDR, false);
  925. f2fs_wait_on_page_writeback(page, NODE, true);
  926. fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
  927. set_cold_node(dn->inode, page);
  928. if (!PageUptodate(page))
  929. SetPageUptodate(page);
  930. if (set_page_dirty(page))
  931. dn->node_changed = true;
  932. if (f2fs_has_xattr_block(ofs))
  933. f2fs_i_xnid_write(dn->inode, dn->nid);
  934. if (ofs == 0)
  935. inc_valid_inode_count(sbi);
  936. return page;
  937. fail:
  938. clear_node_page_dirty(page);
  939. f2fs_put_page(page, 1);
  940. return ERR_PTR(err);
  941. }
  942. /*
  943. * Caller should do after getting the following values.
  944. * 0: f2fs_put_page(page, 0)
  945. * LOCKED_PAGE or error: f2fs_put_page(page, 1)
  946. */
  947. static int read_node_page(struct page *page, int op_flags)
  948. {
  949. struct f2fs_sb_info *sbi = F2FS_P_SB(page);
  950. struct node_info ni;
  951. struct f2fs_io_info fio = {
  952. .sbi = sbi,
  953. .type = NODE,
  954. .op = REQ_OP_READ,
  955. .op_flags = op_flags,
  956. .page = page,
  957. .encrypted_page = NULL,
  958. };
  959. if (PageUptodate(page))
  960. return LOCKED_PAGE;
  961. get_node_info(sbi, page->index, &ni);
  962. if (unlikely(ni.blk_addr == NULL_ADDR)) {
  963. ClearPageUptodate(page);
  964. return -ENOENT;
  965. }
  966. fio.new_blkaddr = fio.old_blkaddr = ni.blk_addr;
  967. return f2fs_submit_page_bio(&fio);
  968. }
  969. /*
  970. * Readahead a node page
  971. */
  972. void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid)
  973. {
  974. struct page *apage;
  975. int err;
  976. if (!nid)
  977. return;
  978. f2fs_bug_on(sbi, check_nid_range(sbi, nid));
  979. rcu_read_lock();
  980. apage = radix_tree_lookup(&NODE_MAPPING(sbi)->page_tree, nid);
  981. rcu_read_unlock();
  982. if (apage)
  983. return;
  984. apage = f2fs_grab_cache_page(NODE_MAPPING(sbi), nid, false);
  985. if (!apage)
  986. return;
  987. err = read_node_page(apage, REQ_RAHEAD);
  988. f2fs_put_page(apage, err ? 1 : 0);
  989. }
  990. static struct page *__get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid,
  991. struct page *parent, int start)
  992. {
  993. struct page *page;
  994. int err;
  995. if (!nid)
  996. return ERR_PTR(-ENOENT);
  997. f2fs_bug_on(sbi, check_nid_range(sbi, nid));
  998. repeat:
  999. page = f2fs_grab_cache_page(NODE_MAPPING(sbi), nid, false);
  1000. if (!page)
  1001. return ERR_PTR(-ENOMEM);
  1002. err = read_node_page(page, 0);
  1003. if (err < 0) {
  1004. f2fs_put_page(page, 1);
  1005. return ERR_PTR(err);
  1006. } else if (err == LOCKED_PAGE) {
  1007. err = 0;
  1008. goto page_hit;
  1009. }
  1010. if (parent)
  1011. ra_node_pages(parent, start + 1, MAX_RA_NODE);
  1012. lock_page(page);
  1013. if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
  1014. f2fs_put_page(page, 1);
  1015. goto repeat;
  1016. }
  1017. if (unlikely(!PageUptodate(page))) {
  1018. err = -EIO;
  1019. goto out_err;
  1020. }
  1021. page_hit:
  1022. if(unlikely(nid != nid_of_node(page))) {
  1023. f2fs_msg(sbi->sb, KERN_WARNING, "inconsistent node block, "
  1024. "nid:%lu, node_footer[nid:%u,ino:%u,ofs:%u,cpver:%llu,blkaddr:%u]",
  1025. nid, nid_of_node(page), ino_of_node(page),
  1026. ofs_of_node(page), cpver_of_node(page),
  1027. next_blkaddr_of_node(page));
  1028. ClearPageUptodate(page);
  1029. err = -EINVAL;
  1030. out_err:
  1031. f2fs_put_page(page, 1);
  1032. return ERR_PTR(err);
  1033. }
  1034. return page;
  1035. }
  1036. struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid)
  1037. {
  1038. return __get_node_page(sbi, nid, NULL, 0);
  1039. }
  1040. struct page *get_node_page_ra(struct page *parent, int start)
  1041. {
  1042. struct f2fs_sb_info *sbi = F2FS_P_SB(parent);
  1043. nid_t nid = get_nid(parent, start, false);
  1044. return __get_node_page(sbi, nid, parent, start);
  1045. }
  1046. static void flush_inline_data(struct f2fs_sb_info *sbi, nid_t ino)
  1047. {
  1048. struct inode *inode;
  1049. struct page *page;
  1050. int ret;
  1051. /* should flush inline_data before evict_inode */
  1052. inode = ilookup(sbi->sb, ino);
  1053. if (!inode)
  1054. return;
  1055. page = pagecache_get_page(inode->i_mapping, 0, FGP_LOCK|FGP_NOWAIT, 0);
  1056. if (!page)
  1057. goto iput_out;
  1058. if (!PageUptodate(page))
  1059. goto page_out;
  1060. if (!PageDirty(page))
  1061. goto page_out;
  1062. if (!clear_page_dirty_for_io(page))
  1063. goto page_out;
  1064. ret = f2fs_write_inline_data(inode, page);
  1065. inode_dec_dirty_pages(inode);
  1066. remove_dirty_inode(inode);
  1067. if (ret)
  1068. set_page_dirty(page);
  1069. page_out:
  1070. f2fs_put_page(page, 1);
  1071. iput_out:
  1072. iput(inode);
  1073. }
  1074. void move_node_page(struct page *node_page, int gc_type)
  1075. {
  1076. if (gc_type == FG_GC) {
  1077. struct f2fs_sb_info *sbi = F2FS_P_SB(node_page);
  1078. struct writeback_control wbc = {
  1079. .sync_mode = WB_SYNC_ALL,
  1080. .nr_to_write = 1,
  1081. .for_reclaim = 0,
  1082. };
  1083. set_page_dirty(node_page);
  1084. f2fs_wait_on_page_writeback(node_page, NODE, true);
  1085. f2fs_bug_on(sbi, PageWriteback(node_page));
  1086. if (!clear_page_dirty_for_io(node_page))
  1087. goto out_page;
  1088. if (NODE_MAPPING(sbi)->a_ops->writepage(node_page, &wbc))
  1089. unlock_page(node_page);
  1090. goto release_page;
  1091. } else {
  1092. /* set page dirty and write it */
  1093. if (!PageWriteback(node_page))
  1094. set_page_dirty(node_page);
  1095. }
  1096. out_page:
  1097. unlock_page(node_page);
  1098. release_page:
  1099. f2fs_put_page(node_page, 0);
  1100. }
  1101. static struct page *last_fsync_dnode(struct f2fs_sb_info *sbi, nid_t ino)
  1102. {
  1103. pgoff_t index, end;
  1104. struct pagevec pvec;
  1105. struct page *last_page = NULL;
  1106. pagevec_init(&pvec, 0);
  1107. index = 0;
  1108. end = ULONG_MAX;
  1109. while (index <= end) {
  1110. int i, nr_pages;
  1111. nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
  1112. PAGECACHE_TAG_DIRTY,
  1113. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
  1114. if (nr_pages == 0)
  1115. break;
  1116. for (i = 0; i < nr_pages; i++) {
  1117. struct page *page = pvec.pages[i];
  1118. if (unlikely(f2fs_cp_error(sbi))) {
  1119. f2fs_put_page(last_page, 0);
  1120. pagevec_release(&pvec);
  1121. return ERR_PTR(-EIO);
  1122. }
  1123. if (!IS_DNODE(page) || !is_cold_node(page))
  1124. continue;
  1125. if (ino_of_node(page) != ino)
  1126. continue;
  1127. lock_page(page);
  1128. if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
  1129. continue_unlock:
  1130. unlock_page(page);
  1131. continue;
  1132. }
  1133. if (ino_of_node(page) != ino)
  1134. goto continue_unlock;
  1135. if (!PageDirty(page)) {
  1136. /* someone wrote it for us */
  1137. goto continue_unlock;
  1138. }
  1139. if (last_page)
  1140. f2fs_put_page(last_page, 0);
  1141. get_page(page);
  1142. last_page = page;
  1143. unlock_page(page);
  1144. }
  1145. pagevec_release(&pvec);
  1146. cond_resched();
  1147. }
  1148. return last_page;
  1149. }
  1150. static int __write_node_page(struct page *page, bool atomic, bool *submitted,
  1151. struct writeback_control *wbc)
  1152. {
  1153. struct f2fs_sb_info *sbi = F2FS_P_SB(page);
  1154. nid_t nid;
  1155. struct node_info ni;
  1156. struct f2fs_io_info fio = {
  1157. .sbi = sbi,
  1158. .type = NODE,
  1159. .op = REQ_OP_WRITE,
  1160. .op_flags = wbc_to_write_flags(wbc),
  1161. .page = page,
  1162. .encrypted_page = NULL,
  1163. .submitted = false,
  1164. };
  1165. trace_f2fs_writepage(page, NODE);
  1166. if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
  1167. goto redirty_out;
  1168. if (unlikely(f2fs_cp_error(sbi)))
  1169. goto redirty_out;
  1170. /* get old block addr of this node page */
  1171. nid = nid_of_node(page);
  1172. f2fs_bug_on(sbi, page->index != nid);
  1173. if (wbc->for_reclaim) {
  1174. if (!down_read_trylock(&sbi->node_write))
  1175. goto redirty_out;
  1176. } else {
  1177. down_read(&sbi->node_write);
  1178. }
  1179. get_node_info(sbi, nid, &ni);
  1180. /* This page is already truncated */
  1181. if (unlikely(ni.blk_addr == NULL_ADDR)) {
  1182. ClearPageUptodate(page);
  1183. dec_page_count(sbi, F2FS_DIRTY_NODES);
  1184. up_read(&sbi->node_write);
  1185. unlock_page(page);
  1186. return 0;
  1187. }
  1188. if (atomic && !test_opt(sbi, NOBARRIER))
  1189. fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
  1190. set_page_writeback(page);
  1191. fio.old_blkaddr = ni.blk_addr;
  1192. write_node_page(nid, &fio);
  1193. set_node_addr(sbi, &ni, fio.new_blkaddr, is_fsync_dnode(page));
  1194. dec_page_count(sbi, F2FS_DIRTY_NODES);
  1195. up_read(&sbi->node_write);
  1196. if (wbc->for_reclaim) {
  1197. f2fs_submit_merged_write_cond(sbi, page->mapping->host, 0,
  1198. page->index, NODE);
  1199. submitted = NULL;
  1200. }
  1201. unlock_page(page);
  1202. if (unlikely(f2fs_cp_error(sbi))) {
  1203. f2fs_submit_merged_write(sbi, NODE);
  1204. submitted = NULL;
  1205. }
  1206. if (submitted)
  1207. *submitted = fio.submitted;
  1208. return 0;
  1209. redirty_out:
  1210. redirty_page_for_writepage(wbc, page);
  1211. return AOP_WRITEPAGE_ACTIVATE;
  1212. }
  1213. static int f2fs_write_node_page(struct page *page,
  1214. struct writeback_control *wbc)
  1215. {
  1216. return __write_node_page(page, false, NULL, wbc);
  1217. }
  1218. int fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
  1219. struct writeback_control *wbc, bool atomic)
  1220. {
  1221. pgoff_t index, end;
  1222. pgoff_t last_idx = ULONG_MAX;
  1223. struct pagevec pvec;
  1224. int ret = 0;
  1225. struct page *last_page = NULL;
  1226. bool marked = false;
  1227. nid_t ino = inode->i_ino;
  1228. if (atomic) {
  1229. last_page = last_fsync_dnode(sbi, ino);
  1230. if (IS_ERR_OR_NULL(last_page))
  1231. return PTR_ERR_OR_ZERO(last_page);
  1232. }
  1233. retry:
  1234. pagevec_init(&pvec, 0);
  1235. index = 0;
  1236. end = ULONG_MAX;
  1237. while (index <= end) {
  1238. int i, nr_pages;
  1239. nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
  1240. PAGECACHE_TAG_DIRTY,
  1241. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
  1242. if (nr_pages == 0)
  1243. break;
  1244. for (i = 0; i < nr_pages; i++) {
  1245. struct page *page = pvec.pages[i];
  1246. bool submitted = false;
  1247. if (unlikely(f2fs_cp_error(sbi))) {
  1248. f2fs_put_page(last_page, 0);
  1249. pagevec_release(&pvec);
  1250. ret = -EIO;
  1251. goto out;
  1252. }
  1253. if (!IS_DNODE(page) || !is_cold_node(page))
  1254. continue;
  1255. if (ino_of_node(page) != ino)
  1256. continue;
  1257. lock_page(page);
  1258. if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
  1259. continue_unlock:
  1260. unlock_page(page);
  1261. continue;
  1262. }
  1263. if (ino_of_node(page) != ino)
  1264. goto continue_unlock;
  1265. if (!PageDirty(page) && page != last_page) {
  1266. /* someone wrote it for us */
  1267. goto continue_unlock;
  1268. }
  1269. f2fs_wait_on_page_writeback(page, NODE, true);
  1270. BUG_ON(PageWriteback(page));
  1271. set_fsync_mark(page, 0);
  1272. set_dentry_mark(page, 0);
  1273. if (!atomic || page == last_page) {
  1274. set_fsync_mark(page, 1);
  1275. if (IS_INODE(page)) {
  1276. if (is_inode_flag_set(inode,
  1277. FI_DIRTY_INODE))
  1278. update_inode(inode, page);
  1279. set_dentry_mark(page,
  1280. need_dentry_mark(sbi, ino));
  1281. }
  1282. /* may be written by other thread */
  1283. if (!PageDirty(page))
  1284. set_page_dirty(page);
  1285. }
  1286. if (!clear_page_dirty_for_io(page))
  1287. goto continue_unlock;
  1288. ret = __write_node_page(page, atomic &&
  1289. page == last_page,
  1290. &submitted, wbc);
  1291. if (ret) {
  1292. unlock_page(page);
  1293. f2fs_put_page(last_page, 0);
  1294. break;
  1295. } else if (submitted) {
  1296. last_idx = page->index;
  1297. }
  1298. if (page == last_page) {
  1299. f2fs_put_page(page, 0);
  1300. marked = true;
  1301. break;
  1302. }
  1303. }
  1304. pagevec_release(&pvec);
  1305. cond_resched();
  1306. if (ret || marked)
  1307. break;
  1308. }
  1309. if (!ret && atomic && !marked) {
  1310. f2fs_msg(sbi->sb, KERN_DEBUG,
  1311. "Retry to write fsync mark: ino=%u, idx=%lx",
  1312. ino, last_page->index);
  1313. lock_page(last_page);
  1314. f2fs_wait_on_page_writeback(last_page, NODE, true);
  1315. set_page_dirty(last_page);
  1316. unlock_page(last_page);
  1317. goto retry;
  1318. }
  1319. out:
  1320. if (last_idx != ULONG_MAX)
  1321. f2fs_submit_merged_write_cond(sbi, NULL, ino, last_idx, NODE);
  1322. return ret ? -EIO: 0;
  1323. }
  1324. int sync_node_pages(struct f2fs_sb_info *sbi, struct writeback_control *wbc)
  1325. {
  1326. pgoff_t index, end;
  1327. struct pagevec pvec;
  1328. int step = 0;
  1329. int nwritten = 0;
  1330. int ret = 0;
  1331. pagevec_init(&pvec, 0);
  1332. next_step:
  1333. index = 0;
  1334. end = ULONG_MAX;
  1335. while (index <= end) {
  1336. int i, nr_pages;
  1337. nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
  1338. PAGECACHE_TAG_DIRTY,
  1339. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
  1340. if (nr_pages == 0)
  1341. break;
  1342. for (i = 0; i < nr_pages; i++) {
  1343. struct page *page = pvec.pages[i];
  1344. bool submitted = false;
  1345. if (unlikely(f2fs_cp_error(sbi))) {
  1346. pagevec_release(&pvec);
  1347. ret = -EIO;
  1348. goto out;
  1349. }
  1350. /*
  1351. * flushing sequence with step:
  1352. * 0. indirect nodes
  1353. * 1. dentry dnodes
  1354. * 2. file dnodes
  1355. */
  1356. if (step == 0 && IS_DNODE(page))
  1357. continue;
  1358. if (step == 1 && (!IS_DNODE(page) ||
  1359. is_cold_node(page)))
  1360. continue;
  1361. if (step == 2 && (!IS_DNODE(page) ||
  1362. !is_cold_node(page)))
  1363. continue;
  1364. lock_node:
  1365. if (!trylock_page(page))
  1366. continue;
  1367. if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
  1368. continue_unlock:
  1369. unlock_page(page);
  1370. continue;
  1371. }
  1372. if (!PageDirty(page)) {
  1373. /* someone wrote it for us */
  1374. goto continue_unlock;
  1375. }
  1376. /* flush inline_data */
  1377. if (is_inline_node(page)) {
  1378. clear_inline_node(page);
  1379. unlock_page(page);
  1380. flush_inline_data(sbi, ino_of_node(page));
  1381. goto lock_node;
  1382. }
  1383. f2fs_wait_on_page_writeback(page, NODE, true);
  1384. BUG_ON(PageWriteback(page));
  1385. if (!clear_page_dirty_for_io(page))
  1386. goto continue_unlock;
  1387. set_fsync_mark(page, 0);
  1388. set_dentry_mark(page, 0);
  1389. ret = __write_node_page(page, false, &submitted, wbc);
  1390. if (ret)
  1391. unlock_page(page);
  1392. else if (submitted)
  1393. nwritten++;
  1394. if (--wbc->nr_to_write == 0)
  1395. break;
  1396. }
  1397. pagevec_release(&pvec);
  1398. cond_resched();
  1399. if (wbc->nr_to_write == 0) {
  1400. step = 2;
  1401. break;
  1402. }
  1403. }
  1404. if (step < 2) {
  1405. step++;
  1406. goto next_step;
  1407. }
  1408. out:
  1409. if (nwritten)
  1410. f2fs_submit_merged_write(sbi, NODE);
  1411. return ret;
  1412. }
  1413. int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino)
  1414. {
  1415. pgoff_t index = 0, end = ULONG_MAX;
  1416. struct pagevec pvec;
  1417. int ret2, ret = 0;
  1418. pagevec_init(&pvec, 0);
  1419. while (index <= end) {
  1420. int i, nr_pages;
  1421. nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
  1422. PAGECACHE_TAG_WRITEBACK,
  1423. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
  1424. if (nr_pages == 0)
  1425. break;
  1426. for (i = 0; i < nr_pages; i++) {
  1427. struct page *page = pvec.pages[i];
  1428. /* until radix tree lookup accepts end_index */
  1429. if (unlikely(page->index > end))
  1430. continue;
  1431. if (ino && ino_of_node(page) == ino) {
  1432. f2fs_wait_on_page_writeback(page, NODE, true);
  1433. if (TestClearPageError(page))
  1434. ret = -EIO;
  1435. }
  1436. }
  1437. pagevec_release(&pvec);
  1438. cond_resched();
  1439. }
  1440. ret2 = filemap_check_errors(NODE_MAPPING(sbi));
  1441. if (!ret)
  1442. ret = ret2;
  1443. return ret;
  1444. }
  1445. static int f2fs_write_node_pages(struct address_space *mapping,
  1446. struct writeback_control *wbc)
  1447. {
  1448. struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
  1449. struct blk_plug plug;
  1450. long diff;
  1451. /* balancing f2fs's metadata in background */
  1452. f2fs_balance_fs_bg(sbi);
  1453. /* collect a number of dirty node pages and write together */
  1454. if (get_pages(sbi, F2FS_DIRTY_NODES) < nr_pages_to_skip(sbi, NODE))
  1455. goto skip_write;
  1456. trace_f2fs_writepages(mapping->host, wbc, NODE);
  1457. diff = nr_pages_to_write(sbi, NODE, wbc);
  1458. wbc->sync_mode = WB_SYNC_NONE;
  1459. blk_start_plug(&plug);
  1460. sync_node_pages(sbi, wbc);
  1461. blk_finish_plug(&plug);
  1462. wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
  1463. return 0;
  1464. skip_write:
  1465. wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_NODES);
  1466. trace_f2fs_writepages(mapping->host, wbc, NODE);
  1467. return 0;
  1468. }
  1469. static int f2fs_set_node_page_dirty(struct page *page)
  1470. {
  1471. trace_f2fs_set_page_dirty(page, NODE);
  1472. if (!PageUptodate(page))
  1473. SetPageUptodate(page);
  1474. if (!PageDirty(page)) {
  1475. f2fs_set_page_dirty_nobuffers(page);
  1476. inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_NODES);
  1477. SetPagePrivate(page);
  1478. f2fs_trace_pid(page);
  1479. return 1;
  1480. }
  1481. return 0;
  1482. }
  1483. /*
  1484. * Structure of the f2fs node operations
  1485. */
  1486. const struct address_space_operations f2fs_node_aops = {
  1487. .writepage = f2fs_write_node_page,
  1488. .writepages = f2fs_write_node_pages,
  1489. .set_page_dirty = f2fs_set_node_page_dirty,
  1490. .invalidatepage = f2fs_invalidate_page,
  1491. .releasepage = f2fs_release_page,
  1492. #ifdef CONFIG_MIGRATION
  1493. .migratepage = f2fs_migrate_page,
  1494. #endif
  1495. };
  1496. static struct free_nid *__lookup_free_nid_list(struct f2fs_nm_info *nm_i,
  1497. nid_t n)
  1498. {
  1499. return radix_tree_lookup(&nm_i->free_nid_root, n);
  1500. }
  1501. static int __insert_nid_to_list(struct f2fs_sb_info *sbi,
  1502. struct free_nid *i, enum nid_list list, bool new)
  1503. {
  1504. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1505. if (new) {
  1506. int err = radix_tree_insert(&nm_i->free_nid_root, i->nid, i);
  1507. if (err)
  1508. return err;
  1509. }
  1510. f2fs_bug_on(sbi, list == FREE_NID_LIST ? i->state != NID_NEW :
  1511. i->state != NID_ALLOC);
  1512. nm_i->nid_cnt[list]++;
  1513. list_add_tail(&i->list, &nm_i->nid_list[list]);
  1514. return 0;
  1515. }
  1516. static void __remove_nid_from_list(struct f2fs_sb_info *sbi,
  1517. struct free_nid *i, enum nid_list list, bool reuse)
  1518. {
  1519. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1520. f2fs_bug_on(sbi, list == FREE_NID_LIST ? i->state != NID_NEW :
  1521. i->state != NID_ALLOC);
  1522. nm_i->nid_cnt[list]--;
  1523. list_del(&i->list);
  1524. if (!reuse)
  1525. radix_tree_delete(&nm_i->free_nid_root, i->nid);
  1526. }
  1527. /* return if the nid is recognized as free */
  1528. static bool add_free_nid(struct f2fs_sb_info *sbi, nid_t nid, bool build)
  1529. {
  1530. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1531. struct free_nid *i, *e;
  1532. struct nat_entry *ne;
  1533. int err = -EINVAL;
  1534. bool ret = false;
  1535. /* 0 nid should not be used */
  1536. if (unlikely(nid == 0))
  1537. return false;
  1538. i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS);
  1539. i->nid = nid;
  1540. i->state = NID_NEW;
  1541. if (radix_tree_preload(GFP_NOFS))
  1542. goto err;
  1543. spin_lock(&nm_i->nid_list_lock);
  1544. if (build) {
  1545. /*
  1546. * Thread A Thread B
  1547. * - f2fs_create
  1548. * - f2fs_new_inode
  1549. * - alloc_nid
  1550. * - __insert_nid_to_list(ALLOC_NID_LIST)
  1551. * - f2fs_balance_fs_bg
  1552. * - build_free_nids
  1553. * - __build_free_nids
  1554. * - scan_nat_page
  1555. * - add_free_nid
  1556. * - __lookup_nat_cache
  1557. * - f2fs_add_link
  1558. * - init_inode_metadata
  1559. * - new_inode_page
  1560. * - new_node_page
  1561. * - set_node_addr
  1562. * - alloc_nid_done
  1563. * - __remove_nid_from_list(ALLOC_NID_LIST)
  1564. * - __insert_nid_to_list(FREE_NID_LIST)
  1565. */
  1566. ne = __lookup_nat_cache(nm_i, nid);
  1567. if (ne && (!get_nat_flag(ne, IS_CHECKPOINTED) ||
  1568. nat_get_blkaddr(ne) != NULL_ADDR))
  1569. goto err_out;
  1570. e = __lookup_free_nid_list(nm_i, nid);
  1571. if (e) {
  1572. if (e->state == NID_NEW)
  1573. ret = true;
  1574. goto err_out;
  1575. }
  1576. }
  1577. ret = true;
  1578. err = __insert_nid_to_list(sbi, i, FREE_NID_LIST, true);
  1579. err_out:
  1580. spin_unlock(&nm_i->nid_list_lock);
  1581. radix_tree_preload_end();
  1582. err:
  1583. if (err)
  1584. kmem_cache_free(free_nid_slab, i);
  1585. return ret;
  1586. }
  1587. static void remove_free_nid(struct f2fs_sb_info *sbi, nid_t nid)
  1588. {
  1589. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1590. struct free_nid *i;
  1591. bool need_free = false;
  1592. spin_lock(&nm_i->nid_list_lock);
  1593. i = __lookup_free_nid_list(nm_i, nid);
  1594. if (i && i->state == NID_NEW) {
  1595. __remove_nid_from_list(sbi, i, FREE_NID_LIST, false);
  1596. need_free = true;
  1597. }
  1598. spin_unlock(&nm_i->nid_list_lock);
  1599. if (need_free)
  1600. kmem_cache_free(free_nid_slab, i);
  1601. }
  1602. static void update_free_nid_bitmap(struct f2fs_sb_info *sbi, nid_t nid,
  1603. bool set, bool build)
  1604. {
  1605. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1606. unsigned int nat_ofs = NAT_BLOCK_OFFSET(nid);
  1607. unsigned int nid_ofs = nid - START_NID(nid);
  1608. if (!test_bit_le(nat_ofs, nm_i->nat_block_bitmap))
  1609. return;
  1610. if (set)
  1611. __set_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]);
  1612. else
  1613. __clear_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]);
  1614. if (set)
  1615. nm_i->free_nid_count[nat_ofs]++;
  1616. else if (!build)
  1617. nm_i->free_nid_count[nat_ofs]--;
  1618. }
  1619. static void scan_nat_page(struct f2fs_sb_info *sbi,
  1620. struct page *nat_page, nid_t start_nid)
  1621. {
  1622. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1623. struct f2fs_nat_block *nat_blk = page_address(nat_page);
  1624. block_t blk_addr;
  1625. unsigned int nat_ofs = NAT_BLOCK_OFFSET(start_nid);
  1626. int i;
  1627. if (test_bit_le(nat_ofs, nm_i->nat_block_bitmap))
  1628. return;
  1629. __set_bit_le(nat_ofs, nm_i->nat_block_bitmap);
  1630. i = start_nid % NAT_ENTRY_PER_BLOCK;
  1631. for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
  1632. bool freed = false;
  1633. if (unlikely(start_nid >= nm_i->max_nid))
  1634. break;
  1635. blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
  1636. f2fs_bug_on(sbi, blk_addr == NEW_ADDR);
  1637. if (blk_addr == NULL_ADDR)
  1638. freed = add_free_nid(sbi, start_nid, true);
  1639. spin_lock(&NM_I(sbi)->nid_list_lock);
  1640. update_free_nid_bitmap(sbi, start_nid, freed, true);
  1641. spin_unlock(&NM_I(sbi)->nid_list_lock);
  1642. }
  1643. }
  1644. static void scan_free_nid_bits(struct f2fs_sb_info *sbi)
  1645. {
  1646. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1647. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
  1648. struct f2fs_journal *journal = curseg->journal;
  1649. unsigned int i, idx;
  1650. down_read(&nm_i->nat_tree_lock);
  1651. for (i = 0; i < nm_i->nat_blocks; i++) {
  1652. if (!test_bit_le(i, nm_i->nat_block_bitmap))
  1653. continue;
  1654. if (!nm_i->free_nid_count[i])
  1655. continue;
  1656. for (idx = 0; idx < NAT_ENTRY_PER_BLOCK; idx++) {
  1657. nid_t nid;
  1658. if (!test_bit_le(idx, nm_i->free_nid_bitmap[i]))
  1659. continue;
  1660. nid = i * NAT_ENTRY_PER_BLOCK + idx;
  1661. add_free_nid(sbi, nid, true);
  1662. if (nm_i->nid_cnt[FREE_NID_LIST] >= MAX_FREE_NIDS)
  1663. goto out;
  1664. }
  1665. }
  1666. out:
  1667. down_read(&curseg->journal_rwsem);
  1668. for (i = 0; i < nats_in_cursum(journal); i++) {
  1669. block_t addr;
  1670. nid_t nid;
  1671. addr = le32_to_cpu(nat_in_journal(journal, i).block_addr);
  1672. nid = le32_to_cpu(nid_in_journal(journal, i));
  1673. if (addr == NULL_ADDR)
  1674. add_free_nid(sbi, nid, true);
  1675. else
  1676. remove_free_nid(sbi, nid);
  1677. }
  1678. up_read(&curseg->journal_rwsem);
  1679. up_read(&nm_i->nat_tree_lock);
  1680. }
  1681. static void __build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount)
  1682. {
  1683. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1684. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
  1685. struct f2fs_journal *journal = curseg->journal;
  1686. int i = 0;
  1687. nid_t nid = nm_i->next_scan_nid;
  1688. if (unlikely(nid >= nm_i->max_nid))
  1689. nid = 0;
  1690. /* Enough entries */
  1691. if (nm_i->nid_cnt[FREE_NID_LIST] >= NAT_ENTRY_PER_BLOCK)
  1692. return;
  1693. if (!sync && !available_free_memory(sbi, FREE_NIDS))
  1694. return;
  1695. if (!mount) {
  1696. /* try to find free nids in free_nid_bitmap */
  1697. scan_free_nid_bits(sbi);
  1698. if (nm_i->nid_cnt[FREE_NID_LIST])
  1699. return;
  1700. }
  1701. /* readahead nat pages to be scanned */
  1702. ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), FREE_NID_PAGES,
  1703. META_NAT, true);
  1704. down_read(&nm_i->nat_tree_lock);
  1705. while (1) {
  1706. struct page *page = get_current_nat_page(sbi, nid);
  1707. scan_nat_page(sbi, page, nid);
  1708. f2fs_put_page(page, 1);
  1709. nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK));
  1710. if (unlikely(nid >= nm_i->max_nid))
  1711. nid = 0;
  1712. if (++i >= FREE_NID_PAGES)
  1713. break;
  1714. }
  1715. /* go to the next free nat pages to find free nids abundantly */
  1716. nm_i->next_scan_nid = nid;
  1717. /* find free nids from current sum_pages */
  1718. down_read(&curseg->journal_rwsem);
  1719. for (i = 0; i < nats_in_cursum(journal); i++) {
  1720. block_t addr;
  1721. addr = le32_to_cpu(nat_in_journal(journal, i).block_addr);
  1722. nid = le32_to_cpu(nid_in_journal(journal, i));
  1723. if (addr == NULL_ADDR)
  1724. add_free_nid(sbi, nid, true);
  1725. else
  1726. remove_free_nid(sbi, nid);
  1727. }
  1728. up_read(&curseg->journal_rwsem);
  1729. up_read(&nm_i->nat_tree_lock);
  1730. ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nm_i->next_scan_nid),
  1731. nm_i->ra_nid_pages, META_NAT, false);
  1732. }
  1733. void build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount)
  1734. {
  1735. mutex_lock(&NM_I(sbi)->build_lock);
  1736. __build_free_nids(sbi, sync, mount);
  1737. mutex_unlock(&NM_I(sbi)->build_lock);
  1738. }
  1739. /*
  1740. * If this function returns success, caller can obtain a new nid
  1741. * from second parameter of this function.
  1742. * The returned nid could be used ino as well as nid when inode is created.
  1743. */
  1744. bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
  1745. {
  1746. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1747. struct free_nid *i = NULL;
  1748. retry:
  1749. #ifdef CONFIG_F2FS_FAULT_INJECTION
  1750. if (time_to_inject(sbi, FAULT_ALLOC_NID)) {
  1751. f2fs_show_injection_info(FAULT_ALLOC_NID);
  1752. return false;
  1753. }
  1754. #endif
  1755. spin_lock(&nm_i->nid_list_lock);
  1756. if (unlikely(nm_i->available_nids == 0)) {
  1757. spin_unlock(&nm_i->nid_list_lock);
  1758. return false;
  1759. }
  1760. /* We should not use stale free nids created by build_free_nids */
  1761. if (nm_i->nid_cnt[FREE_NID_LIST] && !on_build_free_nids(nm_i)) {
  1762. f2fs_bug_on(sbi, list_empty(&nm_i->nid_list[FREE_NID_LIST]));
  1763. i = list_first_entry(&nm_i->nid_list[FREE_NID_LIST],
  1764. struct free_nid, list);
  1765. *nid = i->nid;
  1766. __remove_nid_from_list(sbi, i, FREE_NID_LIST, true);
  1767. i->state = NID_ALLOC;
  1768. __insert_nid_to_list(sbi, i, ALLOC_NID_LIST, false);
  1769. nm_i->available_nids--;
  1770. update_free_nid_bitmap(sbi, *nid, false, false);
  1771. spin_unlock(&nm_i->nid_list_lock);
  1772. return true;
  1773. }
  1774. spin_unlock(&nm_i->nid_list_lock);
  1775. /* Let's scan nat pages and its caches to get free nids */
  1776. build_free_nids(sbi, true, false);
  1777. goto retry;
  1778. }
  1779. /*
  1780. * alloc_nid() should be called prior to this function.
  1781. */
  1782. void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
  1783. {
  1784. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1785. struct free_nid *i;
  1786. spin_lock(&nm_i->nid_list_lock);
  1787. i = __lookup_free_nid_list(nm_i, nid);
  1788. f2fs_bug_on(sbi, !i);
  1789. __remove_nid_from_list(sbi, i, ALLOC_NID_LIST, false);
  1790. spin_unlock(&nm_i->nid_list_lock);
  1791. kmem_cache_free(free_nid_slab, i);
  1792. }
  1793. /*
  1794. * alloc_nid() should be called prior to this function.
  1795. */
  1796. void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
  1797. {
  1798. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1799. struct free_nid *i;
  1800. bool need_free = false;
  1801. if (!nid)
  1802. return;
  1803. spin_lock(&nm_i->nid_list_lock);
  1804. i = __lookup_free_nid_list(nm_i, nid);
  1805. f2fs_bug_on(sbi, !i);
  1806. if (!available_free_memory(sbi, FREE_NIDS)) {
  1807. __remove_nid_from_list(sbi, i, ALLOC_NID_LIST, false);
  1808. need_free = true;
  1809. } else {
  1810. __remove_nid_from_list(sbi, i, ALLOC_NID_LIST, true);
  1811. i->state = NID_NEW;
  1812. __insert_nid_to_list(sbi, i, FREE_NID_LIST, false);
  1813. }
  1814. nm_i->available_nids++;
  1815. update_free_nid_bitmap(sbi, nid, true, false);
  1816. spin_unlock(&nm_i->nid_list_lock);
  1817. if (need_free)
  1818. kmem_cache_free(free_nid_slab, i);
  1819. }
  1820. int try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink)
  1821. {
  1822. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1823. struct free_nid *i, *next;
  1824. int nr = nr_shrink;
  1825. if (nm_i->nid_cnt[FREE_NID_LIST] <= MAX_FREE_NIDS)
  1826. return 0;
  1827. if (!mutex_trylock(&nm_i->build_lock))
  1828. return 0;
  1829. spin_lock(&nm_i->nid_list_lock);
  1830. list_for_each_entry_safe(i, next, &nm_i->nid_list[FREE_NID_LIST],
  1831. list) {
  1832. if (nr_shrink <= 0 ||
  1833. nm_i->nid_cnt[FREE_NID_LIST] <= MAX_FREE_NIDS)
  1834. break;
  1835. __remove_nid_from_list(sbi, i, FREE_NID_LIST, false);
  1836. kmem_cache_free(free_nid_slab, i);
  1837. nr_shrink--;
  1838. }
  1839. spin_unlock(&nm_i->nid_list_lock);
  1840. mutex_unlock(&nm_i->build_lock);
  1841. return nr - nr_shrink;
  1842. }
  1843. void recover_inline_xattr(struct inode *inode, struct page *page)
  1844. {
  1845. void *src_addr, *dst_addr;
  1846. size_t inline_size;
  1847. struct page *ipage;
  1848. struct f2fs_inode *ri;
  1849. ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
  1850. f2fs_bug_on(F2FS_I_SB(inode), IS_ERR(ipage));
  1851. ri = F2FS_INODE(page);
  1852. if (!(ri->i_inline & F2FS_INLINE_XATTR)) {
  1853. clear_inode_flag(inode, FI_INLINE_XATTR);
  1854. goto update_inode;
  1855. }
  1856. dst_addr = inline_xattr_addr(ipage);
  1857. src_addr = inline_xattr_addr(page);
  1858. inline_size = inline_xattr_size(inode);
  1859. f2fs_wait_on_page_writeback(ipage, NODE, true);
  1860. memcpy(dst_addr, src_addr, inline_size);
  1861. update_inode:
  1862. update_inode(inode, ipage);
  1863. f2fs_put_page(ipage, 1);
  1864. }
  1865. int recover_xattr_data(struct inode *inode, struct page *page, block_t blkaddr)
  1866. {
  1867. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  1868. nid_t prev_xnid = F2FS_I(inode)->i_xattr_nid;
  1869. nid_t new_xnid = nid_of_node(page);
  1870. struct node_info ni;
  1871. struct page *xpage;
  1872. if (!prev_xnid)
  1873. goto recover_xnid;
  1874. /* 1: invalidate the previous xattr nid */
  1875. get_node_info(sbi, prev_xnid, &ni);
  1876. f2fs_bug_on(sbi, ni.blk_addr == NULL_ADDR);
  1877. invalidate_blocks(sbi, ni.blk_addr);
  1878. dec_valid_node_count(sbi, inode);
  1879. set_node_addr(sbi, &ni, NULL_ADDR, false);
  1880. recover_xnid:
  1881. /* 2: update xattr nid in inode */
  1882. remove_free_nid(sbi, new_xnid);
  1883. f2fs_i_xnid_write(inode, new_xnid);
  1884. if (unlikely(!inc_valid_node_count(sbi, inode)))
  1885. f2fs_bug_on(sbi, 1);
  1886. update_inode_page(inode);
  1887. /* 3: update and set xattr node page dirty */
  1888. xpage = grab_cache_page(NODE_MAPPING(sbi), new_xnid);
  1889. if (!xpage)
  1890. return -ENOMEM;
  1891. memcpy(F2FS_NODE(xpage), F2FS_NODE(page), PAGE_SIZE);
  1892. get_node_info(sbi, new_xnid, &ni);
  1893. ni.ino = inode->i_ino;
  1894. set_node_addr(sbi, &ni, NEW_ADDR, false);
  1895. set_page_dirty(xpage);
  1896. f2fs_put_page(xpage, 1);
  1897. return 0;
  1898. }
  1899. int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page)
  1900. {
  1901. struct f2fs_inode *src, *dst;
  1902. nid_t ino = ino_of_node(page);
  1903. struct node_info old_ni, new_ni;
  1904. struct page *ipage;
  1905. get_node_info(sbi, ino, &old_ni);
  1906. if (unlikely(old_ni.blk_addr != NULL_ADDR))
  1907. return -EINVAL;
  1908. retry:
  1909. ipage = f2fs_grab_cache_page(NODE_MAPPING(sbi), ino, false);
  1910. if (!ipage) {
  1911. congestion_wait(BLK_RW_ASYNC, HZ/50);
  1912. goto retry;
  1913. }
  1914. /* Should not use this inode from free nid list */
  1915. remove_free_nid(sbi, ino);
  1916. if (!PageUptodate(ipage))
  1917. SetPageUptodate(ipage);
  1918. fill_node_footer(ipage, ino, ino, 0, true);
  1919. src = F2FS_INODE(page);
  1920. dst = F2FS_INODE(ipage);
  1921. memcpy(dst, src, (unsigned long)&src->i_ext - (unsigned long)src);
  1922. dst->i_size = 0;
  1923. dst->i_blocks = cpu_to_le64(1);
  1924. dst->i_links = cpu_to_le32(1);
  1925. dst->i_xattr_nid = 0;
  1926. dst->i_inline = src->i_inline & F2FS_INLINE_XATTR;
  1927. new_ni = old_ni;
  1928. new_ni.ino = ino;
  1929. if (unlikely(!inc_valid_node_count(sbi, NULL)))
  1930. WARN_ON(1);
  1931. set_node_addr(sbi, &new_ni, NEW_ADDR, false);
  1932. inc_valid_inode_count(sbi);
  1933. set_page_dirty(ipage);
  1934. f2fs_put_page(ipage, 1);
  1935. return 0;
  1936. }
  1937. int restore_node_summary(struct f2fs_sb_info *sbi,
  1938. unsigned int segno, struct f2fs_summary_block *sum)
  1939. {
  1940. struct f2fs_node *rn;
  1941. struct f2fs_summary *sum_entry;
  1942. block_t addr;
  1943. int i, idx, last_offset, nrpages;
  1944. /* scan the node segment */
  1945. last_offset = sbi->blocks_per_seg;
  1946. addr = START_BLOCK(sbi, segno);
  1947. sum_entry = &sum->entries[0];
  1948. for (i = 0; i < last_offset; i += nrpages, addr += nrpages) {
  1949. nrpages = min(last_offset - i, BIO_MAX_PAGES);
  1950. /* readahead node pages */
  1951. ra_meta_pages(sbi, addr, nrpages, META_POR, true);
  1952. for (idx = addr; idx < addr + nrpages; idx++) {
  1953. struct page *page = get_tmp_page(sbi, idx);
  1954. rn = F2FS_NODE(page);
  1955. sum_entry->nid = rn->footer.nid;
  1956. sum_entry->version = 0;
  1957. sum_entry->ofs_in_node = 0;
  1958. sum_entry++;
  1959. f2fs_put_page(page, 1);
  1960. }
  1961. invalidate_mapping_pages(META_MAPPING(sbi), addr,
  1962. addr + nrpages);
  1963. }
  1964. return 0;
  1965. }
  1966. static void remove_nats_in_journal(struct f2fs_sb_info *sbi)
  1967. {
  1968. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1969. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
  1970. struct f2fs_journal *journal = curseg->journal;
  1971. int i;
  1972. down_write(&curseg->journal_rwsem);
  1973. for (i = 0; i < nats_in_cursum(journal); i++) {
  1974. struct nat_entry *ne;
  1975. struct f2fs_nat_entry raw_ne;
  1976. nid_t nid = le32_to_cpu(nid_in_journal(journal, i));
  1977. raw_ne = nat_in_journal(journal, i);
  1978. ne = __lookup_nat_cache(nm_i, nid);
  1979. if (!ne) {
  1980. ne = grab_nat_entry(nm_i, nid, true);
  1981. node_info_from_raw_nat(&ne->ni, &raw_ne);
  1982. }
  1983. /*
  1984. * if a free nat in journal has not been used after last
  1985. * checkpoint, we should remove it from available nids,
  1986. * since later we will add it again.
  1987. */
  1988. if (!get_nat_flag(ne, IS_DIRTY) &&
  1989. le32_to_cpu(raw_ne.block_addr) == NULL_ADDR) {
  1990. spin_lock(&nm_i->nid_list_lock);
  1991. nm_i->available_nids--;
  1992. spin_unlock(&nm_i->nid_list_lock);
  1993. }
  1994. __set_nat_cache_dirty(nm_i, ne);
  1995. }
  1996. update_nats_in_cursum(journal, -i);
  1997. up_write(&curseg->journal_rwsem);
  1998. }
  1999. static void __adjust_nat_entry_set(struct nat_entry_set *nes,
  2000. struct list_head *head, int max)
  2001. {
  2002. struct nat_entry_set *cur;
  2003. if (nes->entry_cnt >= max)
  2004. goto add_out;
  2005. list_for_each_entry(cur, head, set_list) {
  2006. if (cur->entry_cnt >= nes->entry_cnt) {
  2007. list_add(&nes->set_list, cur->set_list.prev);
  2008. return;
  2009. }
  2010. }
  2011. add_out:
  2012. list_add_tail(&nes->set_list, head);
  2013. }
  2014. static void __update_nat_bits(struct f2fs_sb_info *sbi, nid_t start_nid,
  2015. struct page *page)
  2016. {
  2017. struct f2fs_nm_info *nm_i = NM_I(sbi);
  2018. unsigned int nat_index = start_nid / NAT_ENTRY_PER_BLOCK;
  2019. struct f2fs_nat_block *nat_blk = page_address(page);
  2020. int valid = 0;
  2021. int i;
  2022. if (!enabled_nat_bits(sbi, NULL))
  2023. return;
  2024. for (i = 0; i < NAT_ENTRY_PER_BLOCK; i++) {
  2025. if (start_nid == 0 && i == 0)
  2026. valid++;
  2027. if (nat_blk->entries[i].block_addr)
  2028. valid++;
  2029. }
  2030. if (valid == 0) {
  2031. __set_bit_le(nat_index, nm_i->empty_nat_bits);
  2032. __clear_bit_le(nat_index, nm_i->full_nat_bits);
  2033. return;
  2034. }
  2035. __clear_bit_le(nat_index, nm_i->empty_nat_bits);
  2036. if (valid == NAT_ENTRY_PER_BLOCK)
  2037. __set_bit_le(nat_index, nm_i->full_nat_bits);
  2038. else
  2039. __clear_bit_le(nat_index, nm_i->full_nat_bits);
  2040. }
  2041. static void __flush_nat_entry_set(struct f2fs_sb_info *sbi,
  2042. struct nat_entry_set *set, struct cp_control *cpc)
  2043. {
  2044. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
  2045. struct f2fs_journal *journal = curseg->journal;
  2046. nid_t start_nid = set->set * NAT_ENTRY_PER_BLOCK;
  2047. bool to_journal = true;
  2048. struct f2fs_nat_block *nat_blk;
  2049. struct nat_entry *ne, *cur;
  2050. struct page *page = NULL;
  2051. /*
  2052. * there are two steps to flush nat entries:
  2053. * #1, flush nat entries to journal in current hot data summary block.
  2054. * #2, flush nat entries to nat page.
  2055. */
  2056. if (enabled_nat_bits(sbi, cpc) ||
  2057. !__has_cursum_space(journal, set->entry_cnt, NAT_JOURNAL))
  2058. to_journal = false;
  2059. if (to_journal) {
  2060. down_write(&curseg->journal_rwsem);
  2061. } else {
  2062. page = get_next_nat_page(sbi, start_nid);
  2063. nat_blk = page_address(page);
  2064. f2fs_bug_on(sbi, !nat_blk);
  2065. }
  2066. /* flush dirty nats in nat entry set */
  2067. list_for_each_entry_safe(ne, cur, &set->entry_list, list) {
  2068. struct f2fs_nat_entry *raw_ne;
  2069. nid_t nid = nat_get_nid(ne);
  2070. int offset;
  2071. f2fs_bug_on(sbi, nat_get_blkaddr(ne) == NEW_ADDR);
  2072. if (to_journal) {
  2073. offset = lookup_journal_in_cursum(journal,
  2074. NAT_JOURNAL, nid, 1);
  2075. f2fs_bug_on(sbi, offset < 0);
  2076. raw_ne = &nat_in_journal(journal, offset);
  2077. nid_in_journal(journal, offset) = cpu_to_le32(nid);
  2078. } else {
  2079. raw_ne = &nat_blk->entries[nid - start_nid];
  2080. }
  2081. raw_nat_from_node_info(raw_ne, &ne->ni);
  2082. nat_reset_flag(ne);
  2083. __clear_nat_cache_dirty(NM_I(sbi), set, ne);
  2084. if (nat_get_blkaddr(ne) == NULL_ADDR) {
  2085. add_free_nid(sbi, nid, false);
  2086. spin_lock(&NM_I(sbi)->nid_list_lock);
  2087. NM_I(sbi)->available_nids++;
  2088. update_free_nid_bitmap(sbi, nid, true, false);
  2089. spin_unlock(&NM_I(sbi)->nid_list_lock);
  2090. } else {
  2091. spin_lock(&NM_I(sbi)->nid_list_lock);
  2092. update_free_nid_bitmap(sbi, nid, false, false);
  2093. spin_unlock(&NM_I(sbi)->nid_list_lock);
  2094. }
  2095. }
  2096. if (to_journal) {
  2097. up_write(&curseg->journal_rwsem);
  2098. } else {
  2099. __update_nat_bits(sbi, start_nid, page);
  2100. f2fs_put_page(page, 1);
  2101. }
  2102. /* Allow dirty nats by node block allocation in write_begin */
  2103. if (!set->entry_cnt) {
  2104. radix_tree_delete(&NM_I(sbi)->nat_set_root, set->set);
  2105. kmem_cache_free(nat_entry_set_slab, set);
  2106. }
  2107. }
  2108. /*
  2109. * This function is called during the checkpointing process.
  2110. */
  2111. void flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
  2112. {
  2113. struct f2fs_nm_info *nm_i = NM_I(sbi);
  2114. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
  2115. struct f2fs_journal *journal = curseg->journal;
  2116. struct nat_entry_set *setvec[SETVEC_SIZE];
  2117. struct nat_entry_set *set, *tmp;
  2118. unsigned int found;
  2119. nid_t set_idx = 0;
  2120. LIST_HEAD(sets);
  2121. if (!nm_i->dirty_nat_cnt)
  2122. return;
  2123. down_write(&nm_i->nat_tree_lock);
  2124. /*
  2125. * if there are no enough space in journal to store dirty nat
  2126. * entries, remove all entries from journal and merge them
  2127. * into nat entry set.
  2128. */
  2129. if (enabled_nat_bits(sbi, cpc) ||
  2130. !__has_cursum_space(journal, nm_i->dirty_nat_cnt, NAT_JOURNAL))
  2131. remove_nats_in_journal(sbi);
  2132. while ((found = __gang_lookup_nat_set(nm_i,
  2133. set_idx, SETVEC_SIZE, setvec))) {
  2134. unsigned idx;
  2135. set_idx = setvec[found - 1]->set + 1;
  2136. for (idx = 0; idx < found; idx++)
  2137. __adjust_nat_entry_set(setvec[idx], &sets,
  2138. MAX_NAT_JENTRIES(journal));
  2139. }
  2140. /* flush dirty nats in nat entry set */
  2141. list_for_each_entry_safe(set, tmp, &sets, set_list)
  2142. __flush_nat_entry_set(sbi, set, cpc);
  2143. up_write(&nm_i->nat_tree_lock);
  2144. /* Allow dirty nats by node block allocation in write_begin */
  2145. }
  2146. static int __get_nat_bitmaps(struct f2fs_sb_info *sbi)
  2147. {
  2148. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  2149. struct f2fs_nm_info *nm_i = NM_I(sbi);
  2150. unsigned int nat_bits_bytes = nm_i->nat_blocks / BITS_PER_BYTE;
  2151. unsigned int i;
  2152. __u64 cp_ver = cur_cp_version(ckpt);
  2153. block_t nat_bits_addr;
  2154. if (!enabled_nat_bits(sbi, NULL))
  2155. return 0;
  2156. nm_i->nat_bits_blocks = F2FS_BYTES_TO_BLK((nat_bits_bytes << 1) + 8 +
  2157. F2FS_BLKSIZE - 1);
  2158. nm_i->nat_bits = kzalloc(nm_i->nat_bits_blocks << F2FS_BLKSIZE_BITS,
  2159. GFP_KERNEL);
  2160. if (!nm_i->nat_bits)
  2161. return -ENOMEM;
  2162. nat_bits_addr = __start_cp_addr(sbi) + sbi->blocks_per_seg -
  2163. nm_i->nat_bits_blocks;
  2164. for (i = 0; i < nm_i->nat_bits_blocks; i++) {
  2165. struct page *page = get_meta_page(sbi, nat_bits_addr++);
  2166. memcpy(nm_i->nat_bits + (i << F2FS_BLKSIZE_BITS),
  2167. page_address(page), F2FS_BLKSIZE);
  2168. f2fs_put_page(page, 1);
  2169. }
  2170. cp_ver |= (cur_cp_crc(ckpt) << 32);
  2171. if (cpu_to_le64(cp_ver) != *(__le64 *)nm_i->nat_bits) {
  2172. disable_nat_bits(sbi, true);
  2173. return 0;
  2174. }
  2175. nm_i->full_nat_bits = nm_i->nat_bits + 8;
  2176. nm_i->empty_nat_bits = nm_i->full_nat_bits + nat_bits_bytes;
  2177. f2fs_msg(sbi->sb, KERN_NOTICE, "Found nat_bits in checkpoint");
  2178. return 0;
  2179. }
  2180. static inline void load_free_nid_bitmap(struct f2fs_sb_info *sbi)
  2181. {
  2182. struct f2fs_nm_info *nm_i = NM_I(sbi);
  2183. unsigned int i = 0;
  2184. nid_t nid, last_nid;
  2185. if (!enabled_nat_bits(sbi, NULL))
  2186. return;
  2187. for (i = 0; i < nm_i->nat_blocks; i++) {
  2188. i = find_next_bit_le(nm_i->empty_nat_bits, nm_i->nat_blocks, i);
  2189. if (i >= nm_i->nat_blocks)
  2190. break;
  2191. __set_bit_le(i, nm_i->nat_block_bitmap);
  2192. nid = i * NAT_ENTRY_PER_BLOCK;
  2193. last_nid = (i + 1) * NAT_ENTRY_PER_BLOCK;
  2194. spin_lock(&NM_I(sbi)->nid_list_lock);
  2195. for (; nid < last_nid; nid++)
  2196. update_free_nid_bitmap(sbi, nid, true, true);
  2197. spin_unlock(&NM_I(sbi)->nid_list_lock);
  2198. }
  2199. for (i = 0; i < nm_i->nat_blocks; i++) {
  2200. i = find_next_bit_le(nm_i->full_nat_bits, nm_i->nat_blocks, i);
  2201. if (i >= nm_i->nat_blocks)
  2202. break;
  2203. __set_bit_le(i, nm_i->nat_block_bitmap);
  2204. }
  2205. }
  2206. static int init_node_manager(struct f2fs_sb_info *sbi)
  2207. {
  2208. struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi);
  2209. struct f2fs_nm_info *nm_i = NM_I(sbi);
  2210. unsigned char *version_bitmap;
  2211. unsigned int nat_segs;
  2212. int err;
  2213. nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr);
  2214. /* segment_count_nat includes pair segment so divide to 2. */
  2215. nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1;
  2216. nm_i->nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg);
  2217. nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nm_i->nat_blocks;
  2218. /* not used nids: 0, node, meta, (and root counted as valid node) */
  2219. nm_i->available_nids = nm_i->max_nid - sbi->total_valid_node_count -
  2220. F2FS_RESERVED_NODE_NUM;
  2221. nm_i->nid_cnt[FREE_NID_LIST] = 0;
  2222. nm_i->nid_cnt[ALLOC_NID_LIST] = 0;
  2223. nm_i->nat_cnt = 0;
  2224. nm_i->ram_thresh = DEF_RAM_THRESHOLD;
  2225. nm_i->ra_nid_pages = DEF_RA_NID_PAGES;
  2226. nm_i->dirty_nats_ratio = DEF_DIRTY_NAT_RATIO_THRESHOLD;
  2227. INIT_RADIX_TREE(&nm_i->free_nid_root, GFP_ATOMIC);
  2228. INIT_LIST_HEAD(&nm_i->nid_list[FREE_NID_LIST]);
  2229. INIT_LIST_HEAD(&nm_i->nid_list[ALLOC_NID_LIST]);
  2230. INIT_RADIX_TREE(&nm_i->nat_root, GFP_NOIO);
  2231. INIT_RADIX_TREE(&nm_i->nat_set_root, GFP_NOIO);
  2232. INIT_LIST_HEAD(&nm_i->nat_entries);
  2233. mutex_init(&nm_i->build_lock);
  2234. spin_lock_init(&nm_i->nid_list_lock);
  2235. init_rwsem(&nm_i->nat_tree_lock);
  2236. nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
  2237. nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
  2238. version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
  2239. if (!version_bitmap)
  2240. return -EFAULT;
  2241. nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size,
  2242. GFP_KERNEL);
  2243. if (!nm_i->nat_bitmap)
  2244. return -ENOMEM;
  2245. err = __get_nat_bitmaps(sbi);
  2246. if (err)
  2247. return err;
  2248. #ifdef CONFIG_F2FS_CHECK_FS
  2249. nm_i->nat_bitmap_mir = kmemdup(version_bitmap, nm_i->bitmap_size,
  2250. GFP_KERNEL);
  2251. if (!nm_i->nat_bitmap_mir)
  2252. return -ENOMEM;
  2253. #endif
  2254. return 0;
  2255. }
  2256. static int init_free_nid_cache(struct f2fs_sb_info *sbi)
  2257. {
  2258. struct f2fs_nm_info *nm_i = NM_I(sbi);
  2259. nm_i->free_nid_bitmap = f2fs_kvzalloc(nm_i->nat_blocks *
  2260. NAT_ENTRY_BITMAP_SIZE, GFP_KERNEL);
  2261. if (!nm_i->free_nid_bitmap)
  2262. return -ENOMEM;
  2263. nm_i->nat_block_bitmap = f2fs_kvzalloc(nm_i->nat_blocks / 8,
  2264. GFP_KERNEL);
  2265. if (!nm_i->nat_block_bitmap)
  2266. return -ENOMEM;
  2267. nm_i->free_nid_count = f2fs_kvzalloc(nm_i->nat_blocks *
  2268. sizeof(unsigned short), GFP_KERNEL);
  2269. if (!nm_i->free_nid_count)
  2270. return -ENOMEM;
  2271. return 0;
  2272. }
  2273. int build_node_manager(struct f2fs_sb_info *sbi)
  2274. {
  2275. int err;
  2276. sbi->nm_info = kzalloc(sizeof(struct f2fs_nm_info), GFP_KERNEL);
  2277. if (!sbi->nm_info)
  2278. return -ENOMEM;
  2279. err = init_node_manager(sbi);
  2280. if (err)
  2281. return err;
  2282. err = init_free_nid_cache(sbi);
  2283. if (err)
  2284. return err;
  2285. /* load free nid status from nat_bits table */
  2286. load_free_nid_bitmap(sbi);
  2287. build_free_nids(sbi, true, true);
  2288. return 0;
  2289. }
  2290. void destroy_node_manager(struct f2fs_sb_info *sbi)
  2291. {
  2292. struct f2fs_nm_info *nm_i = NM_I(sbi);
  2293. struct free_nid *i, *next_i;
  2294. struct nat_entry *natvec[NATVEC_SIZE];
  2295. struct nat_entry_set *setvec[SETVEC_SIZE];
  2296. nid_t nid = 0;
  2297. unsigned int found;
  2298. if (!nm_i)
  2299. return;
  2300. /* destroy free nid list */
  2301. spin_lock(&nm_i->nid_list_lock);
  2302. list_for_each_entry_safe(i, next_i, &nm_i->nid_list[FREE_NID_LIST],
  2303. list) {
  2304. __remove_nid_from_list(sbi, i, FREE_NID_LIST, false);
  2305. spin_unlock(&nm_i->nid_list_lock);
  2306. kmem_cache_free(free_nid_slab, i);
  2307. spin_lock(&nm_i->nid_list_lock);
  2308. }
  2309. f2fs_bug_on(sbi, nm_i->nid_cnt[FREE_NID_LIST]);
  2310. f2fs_bug_on(sbi, nm_i->nid_cnt[ALLOC_NID_LIST]);
  2311. f2fs_bug_on(sbi, !list_empty(&nm_i->nid_list[ALLOC_NID_LIST]));
  2312. spin_unlock(&nm_i->nid_list_lock);
  2313. /* destroy nat cache */
  2314. down_write(&nm_i->nat_tree_lock);
  2315. while ((found = __gang_lookup_nat_cache(nm_i,
  2316. nid, NATVEC_SIZE, natvec))) {
  2317. unsigned idx;
  2318. nid = nat_get_nid(natvec[found - 1]) + 1;
  2319. for (idx = 0; idx < found; idx++)
  2320. __del_from_nat_cache(nm_i, natvec[idx]);
  2321. }
  2322. f2fs_bug_on(sbi, nm_i->nat_cnt);
  2323. /* destroy nat set cache */
  2324. nid = 0;
  2325. while ((found = __gang_lookup_nat_set(nm_i,
  2326. nid, SETVEC_SIZE, setvec))) {
  2327. unsigned idx;
  2328. nid = setvec[found - 1]->set + 1;
  2329. for (idx = 0; idx < found; idx++) {
  2330. /* entry_cnt is not zero, when cp_error was occurred */
  2331. f2fs_bug_on(sbi, !list_empty(&setvec[idx]->entry_list));
  2332. radix_tree_delete(&nm_i->nat_set_root, setvec[idx]->set);
  2333. kmem_cache_free(nat_entry_set_slab, setvec[idx]);
  2334. }
  2335. }
  2336. up_write(&nm_i->nat_tree_lock);
  2337. kvfree(nm_i->nat_block_bitmap);
  2338. kvfree(nm_i->free_nid_bitmap);
  2339. kvfree(nm_i->free_nid_count);
  2340. kfree(nm_i->nat_bitmap);
  2341. kfree(nm_i->nat_bits);
  2342. #ifdef CONFIG_F2FS_CHECK_FS
  2343. kfree(nm_i->nat_bitmap_mir);
  2344. #endif
  2345. sbi->nm_info = NULL;
  2346. kfree(nm_i);
  2347. }
  2348. int __init create_node_manager_caches(void)
  2349. {
  2350. nat_entry_slab = f2fs_kmem_cache_create("nat_entry",
  2351. sizeof(struct nat_entry));
  2352. if (!nat_entry_slab)
  2353. goto fail;
  2354. free_nid_slab = f2fs_kmem_cache_create("free_nid",
  2355. sizeof(struct free_nid));
  2356. if (!free_nid_slab)
  2357. goto destroy_nat_entry;
  2358. nat_entry_set_slab = f2fs_kmem_cache_create("nat_entry_set",
  2359. sizeof(struct nat_entry_set));
  2360. if (!nat_entry_set_slab)
  2361. goto destroy_free_nid;
  2362. return 0;
  2363. destroy_free_nid:
  2364. kmem_cache_destroy(free_nid_slab);
  2365. destroy_nat_entry:
  2366. kmem_cache_destroy(nat_entry_slab);
  2367. fail:
  2368. return -ENOMEM;
  2369. }
  2370. void destroy_node_manager_caches(void)
  2371. {
  2372. kmem_cache_destroy(nat_entry_set_slab);
  2373. kmem_cache_destroy(free_nid_slab);
  2374. kmem_cache_destroy(nat_entry_slab);
  2375. }