node.c 68 KB

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