node.c 57 KB

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