node.c 67 KB

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