node.c 57 KB

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