node.c 68 KB

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