node.c 67 KB

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