node.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386
  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. }
  576. return err;
  577. }
  578. static void truncate_node(struct dnode_of_data *dn)
  579. {
  580. struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
  581. struct node_info ni;
  582. get_node_info(sbi, dn->nid, &ni);
  583. if (dn->inode->i_blocks == 0) {
  584. f2fs_bug_on(sbi, ni.blk_addr != NULL_ADDR);
  585. goto invalidate;
  586. }
  587. f2fs_bug_on(sbi, ni.blk_addr == NULL_ADDR);
  588. /* Deallocate node address */
  589. invalidate_blocks(sbi, ni.blk_addr);
  590. dec_valid_node_count(sbi, dn->inode);
  591. set_node_addr(sbi, &ni, NULL_ADDR, false);
  592. if (dn->nid == dn->inode->i_ino) {
  593. remove_orphan_inode(sbi, dn->nid);
  594. dec_valid_inode_count(sbi);
  595. f2fs_inode_synced(dn->inode);
  596. }
  597. invalidate:
  598. clear_node_page_dirty(dn->node_page);
  599. set_sbi_flag(sbi, SBI_IS_DIRTY);
  600. f2fs_put_page(dn->node_page, 1);
  601. invalidate_mapping_pages(NODE_MAPPING(sbi),
  602. dn->node_page->index, dn->node_page->index);
  603. dn->node_page = NULL;
  604. trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr);
  605. }
  606. static int truncate_dnode(struct dnode_of_data *dn)
  607. {
  608. struct page *page;
  609. if (dn->nid == 0)
  610. return 1;
  611. /* get direct node */
  612. page = get_node_page(F2FS_I_SB(dn->inode), dn->nid);
  613. if (IS_ERR(page) && PTR_ERR(page) == -ENOENT)
  614. return 1;
  615. else if (IS_ERR(page))
  616. return PTR_ERR(page);
  617. /* Make dnode_of_data for parameter */
  618. dn->node_page = page;
  619. dn->ofs_in_node = 0;
  620. truncate_data_blocks(dn);
  621. truncate_node(dn);
  622. return 1;
  623. }
  624. static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs,
  625. int ofs, int depth)
  626. {
  627. struct dnode_of_data rdn = *dn;
  628. struct page *page;
  629. struct f2fs_node *rn;
  630. nid_t child_nid;
  631. unsigned int child_nofs;
  632. int freed = 0;
  633. int i, ret;
  634. if (dn->nid == 0)
  635. return NIDS_PER_BLOCK + 1;
  636. trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr);
  637. page = get_node_page(F2FS_I_SB(dn->inode), dn->nid);
  638. if (IS_ERR(page)) {
  639. trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page));
  640. return PTR_ERR(page);
  641. }
  642. ra_node_pages(page, ofs, NIDS_PER_BLOCK);
  643. rn = F2FS_NODE(page);
  644. if (depth < 3) {
  645. for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) {
  646. child_nid = le32_to_cpu(rn->in.nid[i]);
  647. if (child_nid == 0)
  648. continue;
  649. rdn.nid = child_nid;
  650. ret = truncate_dnode(&rdn);
  651. if (ret < 0)
  652. goto out_err;
  653. if (set_nid(page, i, 0, false))
  654. dn->node_changed = true;
  655. }
  656. } else {
  657. child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1;
  658. for (i = ofs; i < NIDS_PER_BLOCK; i++) {
  659. child_nid = le32_to_cpu(rn->in.nid[i]);
  660. if (child_nid == 0) {
  661. child_nofs += NIDS_PER_BLOCK + 1;
  662. continue;
  663. }
  664. rdn.nid = child_nid;
  665. ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1);
  666. if (ret == (NIDS_PER_BLOCK + 1)) {
  667. if (set_nid(page, i, 0, false))
  668. dn->node_changed = true;
  669. child_nofs += ret;
  670. } else if (ret < 0 && ret != -ENOENT) {
  671. goto out_err;
  672. }
  673. }
  674. freed = child_nofs;
  675. }
  676. if (!ofs) {
  677. /* remove current indirect node */
  678. dn->node_page = page;
  679. truncate_node(dn);
  680. freed++;
  681. } else {
  682. f2fs_put_page(page, 1);
  683. }
  684. trace_f2fs_truncate_nodes_exit(dn->inode, freed);
  685. return freed;
  686. out_err:
  687. f2fs_put_page(page, 1);
  688. trace_f2fs_truncate_nodes_exit(dn->inode, ret);
  689. return ret;
  690. }
  691. static int truncate_partial_nodes(struct dnode_of_data *dn,
  692. struct f2fs_inode *ri, int *offset, int depth)
  693. {
  694. struct page *pages[2];
  695. nid_t nid[3];
  696. nid_t child_nid;
  697. int err = 0;
  698. int i;
  699. int idx = depth - 2;
  700. nid[0] = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
  701. if (!nid[0])
  702. return 0;
  703. /* get indirect nodes in the path */
  704. for (i = 0; i < idx + 1; i++) {
  705. /* reference count'll be increased */
  706. pages[i] = get_node_page(F2FS_I_SB(dn->inode), nid[i]);
  707. if (IS_ERR(pages[i])) {
  708. err = PTR_ERR(pages[i]);
  709. idx = i - 1;
  710. goto fail;
  711. }
  712. nid[i + 1] = get_nid(pages[i], offset[i + 1], false);
  713. }
  714. ra_node_pages(pages[idx], offset[idx + 1], NIDS_PER_BLOCK);
  715. /* free direct nodes linked to a partial indirect node */
  716. for (i = offset[idx + 1]; i < NIDS_PER_BLOCK; i++) {
  717. child_nid = get_nid(pages[idx], i, false);
  718. if (!child_nid)
  719. continue;
  720. dn->nid = child_nid;
  721. err = truncate_dnode(dn);
  722. if (err < 0)
  723. goto fail;
  724. if (set_nid(pages[idx], i, 0, false))
  725. dn->node_changed = true;
  726. }
  727. if (offset[idx + 1] == 0) {
  728. dn->node_page = pages[idx];
  729. dn->nid = nid[idx];
  730. truncate_node(dn);
  731. } else {
  732. f2fs_put_page(pages[idx], 1);
  733. }
  734. offset[idx]++;
  735. offset[idx + 1] = 0;
  736. idx--;
  737. fail:
  738. for (i = idx; i >= 0; i--)
  739. f2fs_put_page(pages[i], 1);
  740. trace_f2fs_truncate_partial_nodes(dn->inode, nid, depth, err);
  741. return err;
  742. }
  743. /*
  744. * All the block addresses of data and nodes should be nullified.
  745. */
  746. int truncate_inode_blocks(struct inode *inode, pgoff_t from)
  747. {
  748. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  749. int err = 0, cont = 1;
  750. int level, offset[4], noffset[4];
  751. unsigned int nofs = 0;
  752. struct f2fs_inode *ri;
  753. struct dnode_of_data dn;
  754. struct page *page;
  755. trace_f2fs_truncate_inode_blocks_enter(inode, from);
  756. level = get_node_path(inode, from, offset, noffset);
  757. page = get_node_page(sbi, inode->i_ino);
  758. if (IS_ERR(page)) {
  759. trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(page));
  760. return PTR_ERR(page);
  761. }
  762. set_new_dnode(&dn, inode, page, NULL, 0);
  763. unlock_page(page);
  764. ri = F2FS_INODE(page);
  765. switch (level) {
  766. case 0:
  767. case 1:
  768. nofs = noffset[1];
  769. break;
  770. case 2:
  771. nofs = noffset[1];
  772. if (!offset[level - 1])
  773. goto skip_partial;
  774. err = truncate_partial_nodes(&dn, ri, offset, level);
  775. if (err < 0 && err != -ENOENT)
  776. goto fail;
  777. nofs += 1 + NIDS_PER_BLOCK;
  778. break;
  779. case 3:
  780. nofs = 5 + 2 * NIDS_PER_BLOCK;
  781. if (!offset[level - 1])
  782. goto skip_partial;
  783. err = truncate_partial_nodes(&dn, ri, offset, level);
  784. if (err < 0 && err != -ENOENT)
  785. goto fail;
  786. break;
  787. default:
  788. BUG();
  789. }
  790. skip_partial:
  791. while (cont) {
  792. dn.nid = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
  793. switch (offset[0]) {
  794. case NODE_DIR1_BLOCK:
  795. case NODE_DIR2_BLOCK:
  796. err = truncate_dnode(&dn);
  797. break;
  798. case NODE_IND1_BLOCK:
  799. case NODE_IND2_BLOCK:
  800. err = truncate_nodes(&dn, nofs, offset[1], 2);
  801. break;
  802. case NODE_DIND_BLOCK:
  803. err = truncate_nodes(&dn, nofs, offset[1], 3);
  804. cont = 0;
  805. break;
  806. default:
  807. BUG();
  808. }
  809. if (err < 0 && err != -ENOENT)
  810. goto fail;
  811. if (offset[1] == 0 &&
  812. ri->i_nid[offset[0] - NODE_DIR1_BLOCK]) {
  813. lock_page(page);
  814. BUG_ON(page->mapping != NODE_MAPPING(sbi));
  815. f2fs_wait_on_page_writeback(page, NODE, true);
  816. ri->i_nid[offset[0] - NODE_DIR1_BLOCK] = 0;
  817. set_page_dirty(page);
  818. unlock_page(page);
  819. }
  820. offset[1] = 0;
  821. offset[0]++;
  822. nofs += err;
  823. }
  824. fail:
  825. f2fs_put_page(page, 0);
  826. trace_f2fs_truncate_inode_blocks_exit(inode, err);
  827. return err > 0 ? 0 : err;
  828. }
  829. int truncate_xattr_node(struct inode *inode, struct page *page)
  830. {
  831. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  832. nid_t nid = F2FS_I(inode)->i_xattr_nid;
  833. struct dnode_of_data dn;
  834. struct page *npage;
  835. if (!nid)
  836. return 0;
  837. npage = get_node_page(sbi, nid);
  838. if (IS_ERR(npage))
  839. return PTR_ERR(npage);
  840. f2fs_i_xnid_write(inode, 0);
  841. /* need to do checkpoint during fsync */
  842. F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi));
  843. set_new_dnode(&dn, inode, page, npage, nid);
  844. if (page)
  845. dn.inode_page_locked = true;
  846. truncate_node(&dn);
  847. return 0;
  848. }
  849. /*
  850. * Caller should grab and release a rwsem by calling f2fs_lock_op() and
  851. * f2fs_unlock_op().
  852. */
  853. int remove_inode_page(struct inode *inode)
  854. {
  855. struct dnode_of_data dn;
  856. int err;
  857. set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
  858. err = get_dnode_of_data(&dn, 0, LOOKUP_NODE);
  859. if (err)
  860. return err;
  861. err = truncate_xattr_node(inode, dn.inode_page);
  862. if (err) {
  863. f2fs_put_dnode(&dn);
  864. return err;
  865. }
  866. /* remove potential inline_data blocks */
  867. if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  868. S_ISLNK(inode->i_mode))
  869. truncate_data_blocks_range(&dn, 1);
  870. /* 0 is possible, after f2fs_new_inode() has failed */
  871. f2fs_bug_on(F2FS_I_SB(inode),
  872. inode->i_blocks != 0 && inode->i_blocks != 1);
  873. /* will put inode & node pages */
  874. truncate_node(&dn);
  875. return 0;
  876. }
  877. struct page *new_inode_page(struct inode *inode)
  878. {
  879. struct dnode_of_data dn;
  880. /* allocate inode page for new inode */
  881. set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
  882. /* caller should f2fs_put_page(page, 1); */
  883. return new_node_page(&dn, 0, NULL);
  884. }
  885. struct page *new_node_page(struct dnode_of_data *dn,
  886. unsigned int ofs, struct page *ipage)
  887. {
  888. struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
  889. struct node_info old_ni, new_ni;
  890. struct page *page;
  891. int err;
  892. if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
  893. return ERR_PTR(-EPERM);
  894. page = f2fs_grab_cache_page(NODE_MAPPING(sbi), dn->nid, false);
  895. if (!page)
  896. return ERR_PTR(-ENOMEM);
  897. if (unlikely(!inc_valid_node_count(sbi, dn->inode))) {
  898. err = -ENOSPC;
  899. goto fail;
  900. }
  901. get_node_info(sbi, dn->nid, &old_ni);
  902. /* Reinitialize old_ni with new node page */
  903. f2fs_bug_on(sbi, old_ni.blk_addr != NULL_ADDR);
  904. new_ni = old_ni;
  905. new_ni.ino = dn->inode->i_ino;
  906. set_node_addr(sbi, &new_ni, NEW_ADDR, false);
  907. f2fs_wait_on_page_writeback(page, NODE, true);
  908. fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
  909. set_cold_node(dn->inode, page);
  910. if (!PageUptodate(page))
  911. SetPageUptodate(page);
  912. if (set_page_dirty(page))
  913. dn->node_changed = true;
  914. if (f2fs_has_xattr_block(ofs))
  915. f2fs_i_xnid_write(dn->inode, dn->nid);
  916. if (ofs == 0)
  917. inc_valid_inode_count(sbi);
  918. return page;
  919. fail:
  920. clear_node_page_dirty(page);
  921. f2fs_put_page(page, 1);
  922. return ERR_PTR(err);
  923. }
  924. /*
  925. * Caller should do after getting the following values.
  926. * 0: f2fs_put_page(page, 0)
  927. * LOCKED_PAGE or error: f2fs_put_page(page, 1)
  928. */
  929. static int read_node_page(struct page *page, int rw)
  930. {
  931. struct f2fs_sb_info *sbi = F2FS_P_SB(page);
  932. struct node_info ni;
  933. struct f2fs_io_info fio = {
  934. .sbi = sbi,
  935. .type = NODE,
  936. .rw = rw,
  937. .page = page,
  938. .encrypted_page = NULL,
  939. };
  940. get_node_info(sbi, page->index, &ni);
  941. if (unlikely(ni.blk_addr == NULL_ADDR)) {
  942. ClearPageUptodate(page);
  943. return -ENOENT;
  944. }
  945. if (PageUptodate(page))
  946. return LOCKED_PAGE;
  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, READA);
  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. if (atomic) {
  1132. last_page = last_fsync_dnode(sbi, ino);
  1133. if (IS_ERR_OR_NULL(last_page))
  1134. return PTR_ERR_OR_ZERO(last_page);
  1135. }
  1136. retry:
  1137. pagevec_init(&pvec, 0);
  1138. index = 0;
  1139. end = ULONG_MAX;
  1140. while (index <= end) {
  1141. int i, nr_pages;
  1142. nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
  1143. PAGECACHE_TAG_DIRTY,
  1144. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
  1145. if (nr_pages == 0)
  1146. break;
  1147. for (i = 0; i < nr_pages; i++) {
  1148. struct page *page = pvec.pages[i];
  1149. if (unlikely(f2fs_cp_error(sbi))) {
  1150. f2fs_put_page(last_page, 0);
  1151. pagevec_release(&pvec);
  1152. return -EIO;
  1153. }
  1154. if (!IS_DNODE(page) || !is_cold_node(page))
  1155. continue;
  1156. if (ino_of_node(page) != ino)
  1157. continue;
  1158. lock_page(page);
  1159. if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
  1160. continue_unlock:
  1161. unlock_page(page);
  1162. continue;
  1163. }
  1164. if (ino_of_node(page) != ino)
  1165. goto continue_unlock;
  1166. if (!PageDirty(page) && page != last_page) {
  1167. /* someone wrote it for us */
  1168. goto continue_unlock;
  1169. }
  1170. f2fs_wait_on_page_writeback(page, NODE, true);
  1171. BUG_ON(PageWriteback(page));
  1172. if (!atomic || page == last_page) {
  1173. set_fsync_mark(page, 1);
  1174. if (IS_INODE(page)) {
  1175. if (is_inode_flag_set(inode,
  1176. FI_DIRTY_INODE))
  1177. update_inode(inode, page);
  1178. set_dentry_mark(page,
  1179. need_dentry_mark(sbi, ino));
  1180. }
  1181. /* may be written by other thread */
  1182. if (!PageDirty(page))
  1183. set_page_dirty(page);
  1184. }
  1185. if (!clear_page_dirty_for_io(page))
  1186. goto continue_unlock;
  1187. ret = NODE_MAPPING(sbi)->a_ops->writepage(page, wbc);
  1188. if (ret) {
  1189. unlock_page(page);
  1190. f2fs_put_page(last_page, 0);
  1191. break;
  1192. }
  1193. if (page == last_page) {
  1194. f2fs_put_page(page, 0);
  1195. marked = true;
  1196. break;
  1197. }
  1198. }
  1199. pagevec_release(&pvec);
  1200. cond_resched();
  1201. if (ret || marked)
  1202. break;
  1203. }
  1204. if (!ret && atomic && !marked) {
  1205. f2fs_msg(sbi->sb, KERN_DEBUG,
  1206. "Retry to write fsync mark: ino=%u, idx=%lx",
  1207. ino, last_page->index);
  1208. lock_page(last_page);
  1209. set_page_dirty(last_page);
  1210. unlock_page(last_page);
  1211. goto retry;
  1212. }
  1213. return ret ? -EIO: 0;
  1214. }
  1215. int sync_node_pages(struct f2fs_sb_info *sbi, struct writeback_control *wbc)
  1216. {
  1217. pgoff_t index, end;
  1218. struct pagevec pvec;
  1219. int step = 0;
  1220. int nwritten = 0;
  1221. pagevec_init(&pvec, 0);
  1222. next_step:
  1223. index = 0;
  1224. end = ULONG_MAX;
  1225. while (index <= end) {
  1226. int i, nr_pages;
  1227. nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
  1228. PAGECACHE_TAG_DIRTY,
  1229. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
  1230. if (nr_pages == 0)
  1231. break;
  1232. for (i = 0; i < nr_pages; i++) {
  1233. struct page *page = pvec.pages[i];
  1234. if (unlikely(f2fs_cp_error(sbi))) {
  1235. pagevec_release(&pvec);
  1236. return -EIO;
  1237. }
  1238. /*
  1239. * flushing sequence with step:
  1240. * 0. indirect nodes
  1241. * 1. dentry dnodes
  1242. * 2. file dnodes
  1243. */
  1244. if (step == 0 && IS_DNODE(page))
  1245. continue;
  1246. if (step == 1 && (!IS_DNODE(page) ||
  1247. is_cold_node(page)))
  1248. continue;
  1249. if (step == 2 && (!IS_DNODE(page) ||
  1250. !is_cold_node(page)))
  1251. continue;
  1252. lock_node:
  1253. if (!trylock_page(page))
  1254. continue;
  1255. if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
  1256. continue_unlock:
  1257. unlock_page(page);
  1258. continue;
  1259. }
  1260. if (!PageDirty(page)) {
  1261. /* someone wrote it for us */
  1262. goto continue_unlock;
  1263. }
  1264. /* flush inline_data */
  1265. if (is_inline_node(page)) {
  1266. clear_inline_node(page);
  1267. unlock_page(page);
  1268. flush_inline_data(sbi, ino_of_node(page));
  1269. goto lock_node;
  1270. }
  1271. f2fs_wait_on_page_writeback(page, NODE, true);
  1272. BUG_ON(PageWriteback(page));
  1273. if (!clear_page_dirty_for_io(page))
  1274. goto continue_unlock;
  1275. set_fsync_mark(page, 0);
  1276. set_dentry_mark(page, 0);
  1277. if (NODE_MAPPING(sbi)->a_ops->writepage(page, wbc))
  1278. unlock_page(page);
  1279. if (--wbc->nr_to_write == 0)
  1280. break;
  1281. }
  1282. pagevec_release(&pvec);
  1283. cond_resched();
  1284. if (wbc->nr_to_write == 0) {
  1285. step = 2;
  1286. break;
  1287. }
  1288. }
  1289. if (step < 2) {
  1290. step++;
  1291. goto next_step;
  1292. }
  1293. return nwritten;
  1294. }
  1295. int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino)
  1296. {
  1297. pgoff_t index = 0, end = ULONG_MAX;
  1298. struct pagevec pvec;
  1299. int ret2 = 0, ret = 0;
  1300. pagevec_init(&pvec, 0);
  1301. while (index <= end) {
  1302. int i, nr_pages;
  1303. nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
  1304. PAGECACHE_TAG_WRITEBACK,
  1305. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
  1306. if (nr_pages == 0)
  1307. break;
  1308. for (i = 0; i < nr_pages; i++) {
  1309. struct page *page = pvec.pages[i];
  1310. /* until radix tree lookup accepts end_index */
  1311. if (unlikely(page->index > end))
  1312. continue;
  1313. if (ino && ino_of_node(page) == ino) {
  1314. f2fs_wait_on_page_writeback(page, NODE, true);
  1315. if (TestClearPageError(page))
  1316. ret = -EIO;
  1317. }
  1318. }
  1319. pagevec_release(&pvec);
  1320. cond_resched();
  1321. }
  1322. if (unlikely(test_and_clear_bit(AS_ENOSPC, &NODE_MAPPING(sbi)->flags)))
  1323. ret2 = -ENOSPC;
  1324. if (unlikely(test_and_clear_bit(AS_EIO, &NODE_MAPPING(sbi)->flags)))
  1325. ret2 = -EIO;
  1326. if (!ret)
  1327. ret = ret2;
  1328. return ret;
  1329. }
  1330. static int f2fs_write_node_page(struct page *page,
  1331. struct writeback_control *wbc)
  1332. {
  1333. struct f2fs_sb_info *sbi = F2FS_P_SB(page);
  1334. nid_t nid;
  1335. struct node_info ni;
  1336. struct f2fs_io_info fio = {
  1337. .sbi = sbi,
  1338. .type = NODE,
  1339. .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
  1340. .page = page,
  1341. .encrypted_page = NULL,
  1342. };
  1343. trace_f2fs_writepage(page, NODE);
  1344. if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
  1345. goto redirty_out;
  1346. if (unlikely(f2fs_cp_error(sbi)))
  1347. goto redirty_out;
  1348. /* get old block addr of this node page */
  1349. nid = nid_of_node(page);
  1350. f2fs_bug_on(sbi, page->index != nid);
  1351. if (wbc->for_reclaim) {
  1352. if (!down_read_trylock(&sbi->node_write))
  1353. goto redirty_out;
  1354. } else {
  1355. down_read(&sbi->node_write);
  1356. }
  1357. get_node_info(sbi, nid, &ni);
  1358. /* This page is already truncated */
  1359. if (unlikely(ni.blk_addr == NULL_ADDR)) {
  1360. ClearPageUptodate(page);
  1361. dec_page_count(sbi, F2FS_DIRTY_NODES);
  1362. up_read(&sbi->node_write);
  1363. unlock_page(page);
  1364. return 0;
  1365. }
  1366. set_page_writeback(page);
  1367. fio.old_blkaddr = ni.blk_addr;
  1368. write_node_page(nid, &fio);
  1369. set_node_addr(sbi, &ni, fio.new_blkaddr, is_fsync_dnode(page));
  1370. dec_page_count(sbi, F2FS_DIRTY_NODES);
  1371. up_read(&sbi->node_write);
  1372. if (wbc->for_reclaim)
  1373. f2fs_submit_merged_bio_cond(sbi, NULL, page, 0, NODE, WRITE);
  1374. unlock_page(page);
  1375. if (unlikely(f2fs_cp_error(sbi)))
  1376. f2fs_submit_merged_bio(sbi, NODE, WRITE);
  1377. return 0;
  1378. redirty_out:
  1379. redirty_page_for_writepage(wbc, page);
  1380. return AOP_WRITEPAGE_ACTIVATE;
  1381. }
  1382. static int f2fs_write_node_pages(struct address_space *mapping,
  1383. struct writeback_control *wbc)
  1384. {
  1385. struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
  1386. long diff;
  1387. /* balancing f2fs's metadata in background */
  1388. f2fs_balance_fs_bg(sbi);
  1389. /* collect a number of dirty node pages and write together */
  1390. if (get_pages(sbi, F2FS_DIRTY_NODES) < nr_pages_to_skip(sbi, NODE))
  1391. goto skip_write;
  1392. trace_f2fs_writepages(mapping->host, wbc, NODE);
  1393. diff = nr_pages_to_write(sbi, NODE, wbc);
  1394. wbc->sync_mode = WB_SYNC_NONE;
  1395. sync_node_pages(sbi, wbc);
  1396. wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
  1397. return 0;
  1398. skip_write:
  1399. wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_NODES);
  1400. trace_f2fs_writepages(mapping->host, wbc, NODE);
  1401. return 0;
  1402. }
  1403. static int f2fs_set_node_page_dirty(struct page *page)
  1404. {
  1405. trace_f2fs_set_page_dirty(page, NODE);
  1406. if (!PageUptodate(page))
  1407. SetPageUptodate(page);
  1408. if (!PageDirty(page)) {
  1409. f2fs_set_page_dirty_nobuffers(page);
  1410. inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_NODES);
  1411. SetPagePrivate(page);
  1412. f2fs_trace_pid(page);
  1413. return 1;
  1414. }
  1415. return 0;
  1416. }
  1417. /*
  1418. * Structure of the f2fs node operations
  1419. */
  1420. const struct address_space_operations f2fs_node_aops = {
  1421. .writepage = f2fs_write_node_page,
  1422. .writepages = f2fs_write_node_pages,
  1423. .set_page_dirty = f2fs_set_node_page_dirty,
  1424. .invalidatepage = f2fs_invalidate_page,
  1425. .releasepage = f2fs_release_page,
  1426. };
  1427. static struct free_nid *__lookup_free_nid_list(struct f2fs_nm_info *nm_i,
  1428. nid_t n)
  1429. {
  1430. return radix_tree_lookup(&nm_i->free_nid_root, n);
  1431. }
  1432. static void __del_from_free_nid_list(struct f2fs_nm_info *nm_i,
  1433. struct free_nid *i)
  1434. {
  1435. list_del(&i->list);
  1436. radix_tree_delete(&nm_i->free_nid_root, i->nid);
  1437. }
  1438. static int add_free_nid(struct f2fs_sb_info *sbi, nid_t nid, bool build)
  1439. {
  1440. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1441. struct free_nid *i;
  1442. struct nat_entry *ne;
  1443. if (!available_free_memory(sbi, FREE_NIDS))
  1444. return -1;
  1445. /* 0 nid should not be used */
  1446. if (unlikely(nid == 0))
  1447. return 0;
  1448. if (build) {
  1449. /* do not add allocated nids */
  1450. ne = __lookup_nat_cache(nm_i, nid);
  1451. if (ne && (!get_nat_flag(ne, IS_CHECKPOINTED) ||
  1452. nat_get_blkaddr(ne) != NULL_ADDR))
  1453. return 0;
  1454. }
  1455. i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS);
  1456. i->nid = nid;
  1457. i->state = NID_NEW;
  1458. if (radix_tree_preload(GFP_NOFS)) {
  1459. kmem_cache_free(free_nid_slab, i);
  1460. return 0;
  1461. }
  1462. spin_lock(&nm_i->free_nid_list_lock);
  1463. if (radix_tree_insert(&nm_i->free_nid_root, i->nid, i)) {
  1464. spin_unlock(&nm_i->free_nid_list_lock);
  1465. radix_tree_preload_end();
  1466. kmem_cache_free(free_nid_slab, i);
  1467. return 0;
  1468. }
  1469. list_add_tail(&i->list, &nm_i->free_nid_list);
  1470. nm_i->fcnt++;
  1471. spin_unlock(&nm_i->free_nid_list_lock);
  1472. radix_tree_preload_end();
  1473. return 1;
  1474. }
  1475. static void remove_free_nid(struct f2fs_nm_info *nm_i, nid_t nid)
  1476. {
  1477. struct free_nid *i;
  1478. bool need_free = false;
  1479. spin_lock(&nm_i->free_nid_list_lock);
  1480. i = __lookup_free_nid_list(nm_i, nid);
  1481. if (i && i->state == NID_NEW) {
  1482. __del_from_free_nid_list(nm_i, i);
  1483. nm_i->fcnt--;
  1484. need_free = true;
  1485. }
  1486. spin_unlock(&nm_i->free_nid_list_lock);
  1487. if (need_free)
  1488. kmem_cache_free(free_nid_slab, i);
  1489. }
  1490. static void scan_nat_page(struct f2fs_sb_info *sbi,
  1491. struct page *nat_page, nid_t start_nid)
  1492. {
  1493. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1494. struct f2fs_nat_block *nat_blk = page_address(nat_page);
  1495. block_t blk_addr;
  1496. int i;
  1497. i = start_nid % NAT_ENTRY_PER_BLOCK;
  1498. for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
  1499. if (unlikely(start_nid >= nm_i->max_nid))
  1500. break;
  1501. blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
  1502. f2fs_bug_on(sbi, blk_addr == NEW_ADDR);
  1503. if (blk_addr == NULL_ADDR) {
  1504. if (add_free_nid(sbi, start_nid, true) < 0)
  1505. break;
  1506. }
  1507. }
  1508. }
  1509. void build_free_nids(struct f2fs_sb_info *sbi)
  1510. {
  1511. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1512. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
  1513. struct f2fs_journal *journal = curseg->journal;
  1514. int i = 0;
  1515. nid_t nid = nm_i->next_scan_nid;
  1516. /* Enough entries */
  1517. if (nm_i->fcnt >= NAT_ENTRY_PER_BLOCK)
  1518. return;
  1519. /* readahead nat pages to be scanned */
  1520. ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), FREE_NID_PAGES,
  1521. META_NAT, true);
  1522. down_read(&nm_i->nat_tree_lock);
  1523. while (1) {
  1524. struct page *page = get_current_nat_page(sbi, nid);
  1525. scan_nat_page(sbi, page, nid);
  1526. f2fs_put_page(page, 1);
  1527. nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK));
  1528. if (unlikely(nid >= nm_i->max_nid))
  1529. nid = 0;
  1530. if (++i >= FREE_NID_PAGES)
  1531. break;
  1532. }
  1533. /* go to the next free nat pages to find free nids abundantly */
  1534. nm_i->next_scan_nid = nid;
  1535. /* find free nids from current sum_pages */
  1536. down_read(&curseg->journal_rwsem);
  1537. for (i = 0; i < nats_in_cursum(journal); i++) {
  1538. block_t addr;
  1539. addr = le32_to_cpu(nat_in_journal(journal, i).block_addr);
  1540. nid = le32_to_cpu(nid_in_journal(journal, i));
  1541. if (addr == NULL_ADDR)
  1542. add_free_nid(sbi, nid, true);
  1543. else
  1544. remove_free_nid(nm_i, nid);
  1545. }
  1546. up_read(&curseg->journal_rwsem);
  1547. up_read(&nm_i->nat_tree_lock);
  1548. ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nm_i->next_scan_nid),
  1549. nm_i->ra_nid_pages, META_NAT, false);
  1550. }
  1551. /*
  1552. * If this function returns success, caller can obtain a new nid
  1553. * from second parameter of this function.
  1554. * The returned nid could be used ino as well as nid when inode is created.
  1555. */
  1556. bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
  1557. {
  1558. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1559. struct free_nid *i = NULL;
  1560. retry:
  1561. #ifdef CONFIG_F2FS_FAULT_INJECTION
  1562. if (time_to_inject(FAULT_ALLOC_NID))
  1563. return false;
  1564. #endif
  1565. if (unlikely(sbi->total_valid_node_count + 1 > nm_i->available_nids))
  1566. return false;
  1567. spin_lock(&nm_i->free_nid_list_lock);
  1568. /* We should not use stale free nids created by build_free_nids */
  1569. if (nm_i->fcnt && !on_build_free_nids(nm_i)) {
  1570. f2fs_bug_on(sbi, list_empty(&nm_i->free_nid_list));
  1571. list_for_each_entry(i, &nm_i->free_nid_list, list)
  1572. if (i->state == NID_NEW)
  1573. break;
  1574. f2fs_bug_on(sbi, i->state != NID_NEW);
  1575. *nid = i->nid;
  1576. i->state = NID_ALLOC;
  1577. nm_i->fcnt--;
  1578. spin_unlock(&nm_i->free_nid_list_lock);
  1579. return true;
  1580. }
  1581. spin_unlock(&nm_i->free_nid_list_lock);
  1582. /* Let's scan nat pages and its caches to get free nids */
  1583. mutex_lock(&nm_i->build_lock);
  1584. build_free_nids(sbi);
  1585. mutex_unlock(&nm_i->build_lock);
  1586. goto retry;
  1587. }
  1588. /*
  1589. * alloc_nid() should be called prior to this function.
  1590. */
  1591. void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
  1592. {
  1593. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1594. struct free_nid *i;
  1595. spin_lock(&nm_i->free_nid_list_lock);
  1596. i = __lookup_free_nid_list(nm_i, nid);
  1597. f2fs_bug_on(sbi, !i || i->state != NID_ALLOC);
  1598. __del_from_free_nid_list(nm_i, i);
  1599. spin_unlock(&nm_i->free_nid_list_lock);
  1600. kmem_cache_free(free_nid_slab, i);
  1601. }
  1602. /*
  1603. * alloc_nid() should be called prior to this function.
  1604. */
  1605. void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
  1606. {
  1607. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1608. struct free_nid *i;
  1609. bool need_free = false;
  1610. if (!nid)
  1611. return;
  1612. spin_lock(&nm_i->free_nid_list_lock);
  1613. i = __lookup_free_nid_list(nm_i, nid);
  1614. f2fs_bug_on(sbi, !i || i->state != NID_ALLOC);
  1615. if (!available_free_memory(sbi, FREE_NIDS)) {
  1616. __del_from_free_nid_list(nm_i, i);
  1617. need_free = true;
  1618. } else {
  1619. i->state = NID_NEW;
  1620. nm_i->fcnt++;
  1621. }
  1622. spin_unlock(&nm_i->free_nid_list_lock);
  1623. if (need_free)
  1624. kmem_cache_free(free_nid_slab, i);
  1625. }
  1626. int try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink)
  1627. {
  1628. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1629. struct free_nid *i, *next;
  1630. int nr = nr_shrink;
  1631. if (nm_i->fcnt <= MAX_FREE_NIDS)
  1632. return 0;
  1633. if (!mutex_trylock(&nm_i->build_lock))
  1634. return 0;
  1635. spin_lock(&nm_i->free_nid_list_lock);
  1636. list_for_each_entry_safe(i, next, &nm_i->free_nid_list, list) {
  1637. if (nr_shrink <= 0 || nm_i->fcnt <= MAX_FREE_NIDS)
  1638. break;
  1639. if (i->state == NID_ALLOC)
  1640. continue;
  1641. __del_from_free_nid_list(nm_i, i);
  1642. kmem_cache_free(free_nid_slab, i);
  1643. nm_i->fcnt--;
  1644. nr_shrink--;
  1645. }
  1646. spin_unlock(&nm_i->free_nid_list_lock);
  1647. mutex_unlock(&nm_i->build_lock);
  1648. return nr - nr_shrink;
  1649. }
  1650. void recover_inline_xattr(struct inode *inode, struct page *page)
  1651. {
  1652. void *src_addr, *dst_addr;
  1653. size_t inline_size;
  1654. struct page *ipage;
  1655. struct f2fs_inode *ri;
  1656. ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
  1657. f2fs_bug_on(F2FS_I_SB(inode), IS_ERR(ipage));
  1658. ri = F2FS_INODE(page);
  1659. if (!(ri->i_inline & F2FS_INLINE_XATTR)) {
  1660. clear_inode_flag(inode, FI_INLINE_XATTR);
  1661. goto update_inode;
  1662. }
  1663. dst_addr = inline_xattr_addr(ipage);
  1664. src_addr = inline_xattr_addr(page);
  1665. inline_size = inline_xattr_size(inode);
  1666. f2fs_wait_on_page_writeback(ipage, NODE, true);
  1667. memcpy(dst_addr, src_addr, inline_size);
  1668. update_inode:
  1669. update_inode(inode, ipage);
  1670. f2fs_put_page(ipage, 1);
  1671. }
  1672. void recover_xattr_data(struct inode *inode, struct page *page, block_t blkaddr)
  1673. {
  1674. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  1675. nid_t prev_xnid = F2FS_I(inode)->i_xattr_nid;
  1676. nid_t new_xnid = nid_of_node(page);
  1677. struct node_info ni;
  1678. /* 1: invalidate the previous xattr nid */
  1679. if (!prev_xnid)
  1680. goto recover_xnid;
  1681. /* Deallocate node address */
  1682. get_node_info(sbi, prev_xnid, &ni);
  1683. f2fs_bug_on(sbi, ni.blk_addr == NULL_ADDR);
  1684. invalidate_blocks(sbi, ni.blk_addr);
  1685. dec_valid_node_count(sbi, inode);
  1686. set_node_addr(sbi, &ni, NULL_ADDR, false);
  1687. recover_xnid:
  1688. /* 2: allocate new xattr nid */
  1689. if (unlikely(!inc_valid_node_count(sbi, inode)))
  1690. f2fs_bug_on(sbi, 1);
  1691. remove_free_nid(NM_I(sbi), new_xnid);
  1692. get_node_info(sbi, new_xnid, &ni);
  1693. ni.ino = inode->i_ino;
  1694. set_node_addr(sbi, &ni, NEW_ADDR, false);
  1695. f2fs_i_xnid_write(inode, new_xnid);
  1696. /* 3: update xattr blkaddr */
  1697. refresh_sit_entry(sbi, NEW_ADDR, blkaddr);
  1698. set_node_addr(sbi, &ni, blkaddr, false);
  1699. }
  1700. int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page)
  1701. {
  1702. struct f2fs_inode *src, *dst;
  1703. nid_t ino = ino_of_node(page);
  1704. struct node_info old_ni, new_ni;
  1705. struct page *ipage;
  1706. get_node_info(sbi, ino, &old_ni);
  1707. if (unlikely(old_ni.blk_addr != NULL_ADDR))
  1708. return -EINVAL;
  1709. ipage = f2fs_grab_cache_page(NODE_MAPPING(sbi), ino, false);
  1710. if (!ipage)
  1711. return -ENOMEM;
  1712. /* Should not use this inode from free nid list */
  1713. remove_free_nid(NM_I(sbi), ino);
  1714. if (!PageUptodate(ipage))
  1715. SetPageUptodate(ipage);
  1716. fill_node_footer(ipage, ino, ino, 0, true);
  1717. src = F2FS_INODE(page);
  1718. dst = F2FS_INODE(ipage);
  1719. memcpy(dst, src, (unsigned long)&src->i_ext - (unsigned long)src);
  1720. dst->i_size = 0;
  1721. dst->i_blocks = cpu_to_le64(1);
  1722. dst->i_links = cpu_to_le32(1);
  1723. dst->i_xattr_nid = 0;
  1724. dst->i_inline = src->i_inline & F2FS_INLINE_XATTR;
  1725. new_ni = old_ni;
  1726. new_ni.ino = ino;
  1727. if (unlikely(!inc_valid_node_count(sbi, NULL)))
  1728. WARN_ON(1);
  1729. set_node_addr(sbi, &new_ni, NEW_ADDR, false);
  1730. inc_valid_inode_count(sbi);
  1731. set_page_dirty(ipage);
  1732. f2fs_put_page(ipage, 1);
  1733. return 0;
  1734. }
  1735. int restore_node_summary(struct f2fs_sb_info *sbi,
  1736. unsigned int segno, struct f2fs_summary_block *sum)
  1737. {
  1738. struct f2fs_node *rn;
  1739. struct f2fs_summary *sum_entry;
  1740. block_t addr;
  1741. int bio_blocks = MAX_BIO_BLOCKS(sbi);
  1742. int i, idx, last_offset, nrpages;
  1743. /* scan the node segment */
  1744. last_offset = sbi->blocks_per_seg;
  1745. addr = START_BLOCK(sbi, segno);
  1746. sum_entry = &sum->entries[0];
  1747. for (i = 0; i < last_offset; i += nrpages, addr += nrpages) {
  1748. nrpages = min(last_offset - i, bio_blocks);
  1749. /* readahead node pages */
  1750. ra_meta_pages(sbi, addr, nrpages, META_POR, true);
  1751. for (idx = addr; idx < addr + nrpages; idx++) {
  1752. struct page *page = get_tmp_page(sbi, idx);
  1753. rn = F2FS_NODE(page);
  1754. sum_entry->nid = rn->footer.nid;
  1755. sum_entry->version = 0;
  1756. sum_entry->ofs_in_node = 0;
  1757. sum_entry++;
  1758. f2fs_put_page(page, 1);
  1759. }
  1760. invalidate_mapping_pages(META_MAPPING(sbi), addr,
  1761. addr + nrpages);
  1762. }
  1763. return 0;
  1764. }
  1765. static void remove_nats_in_journal(struct f2fs_sb_info *sbi)
  1766. {
  1767. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1768. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
  1769. struct f2fs_journal *journal = curseg->journal;
  1770. int i;
  1771. down_write(&curseg->journal_rwsem);
  1772. for (i = 0; i < nats_in_cursum(journal); i++) {
  1773. struct nat_entry *ne;
  1774. struct f2fs_nat_entry raw_ne;
  1775. nid_t nid = le32_to_cpu(nid_in_journal(journal, i));
  1776. raw_ne = nat_in_journal(journal, i);
  1777. ne = __lookup_nat_cache(nm_i, nid);
  1778. if (!ne) {
  1779. ne = grab_nat_entry(nm_i, nid);
  1780. node_info_from_raw_nat(&ne->ni, &raw_ne);
  1781. }
  1782. __set_nat_cache_dirty(nm_i, ne);
  1783. }
  1784. update_nats_in_cursum(journal, -i);
  1785. up_write(&curseg->journal_rwsem);
  1786. }
  1787. static void __adjust_nat_entry_set(struct nat_entry_set *nes,
  1788. struct list_head *head, int max)
  1789. {
  1790. struct nat_entry_set *cur;
  1791. if (nes->entry_cnt >= max)
  1792. goto add_out;
  1793. list_for_each_entry(cur, head, set_list) {
  1794. if (cur->entry_cnt >= nes->entry_cnt) {
  1795. list_add(&nes->set_list, cur->set_list.prev);
  1796. return;
  1797. }
  1798. }
  1799. add_out:
  1800. list_add_tail(&nes->set_list, head);
  1801. }
  1802. static void __flush_nat_entry_set(struct f2fs_sb_info *sbi,
  1803. struct nat_entry_set *set)
  1804. {
  1805. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
  1806. struct f2fs_journal *journal = curseg->journal;
  1807. nid_t start_nid = set->set * NAT_ENTRY_PER_BLOCK;
  1808. bool to_journal = true;
  1809. struct f2fs_nat_block *nat_blk;
  1810. struct nat_entry *ne, *cur;
  1811. struct page *page = NULL;
  1812. /*
  1813. * there are two steps to flush nat entries:
  1814. * #1, flush nat entries to journal in current hot data summary block.
  1815. * #2, flush nat entries to nat page.
  1816. */
  1817. if (!__has_cursum_space(journal, set->entry_cnt, NAT_JOURNAL))
  1818. to_journal = false;
  1819. if (to_journal) {
  1820. down_write(&curseg->journal_rwsem);
  1821. } else {
  1822. page = get_next_nat_page(sbi, start_nid);
  1823. nat_blk = page_address(page);
  1824. f2fs_bug_on(sbi, !nat_blk);
  1825. }
  1826. /* flush dirty nats in nat entry set */
  1827. list_for_each_entry_safe(ne, cur, &set->entry_list, list) {
  1828. struct f2fs_nat_entry *raw_ne;
  1829. nid_t nid = nat_get_nid(ne);
  1830. int offset;
  1831. if (nat_get_blkaddr(ne) == NEW_ADDR)
  1832. continue;
  1833. if (to_journal) {
  1834. offset = lookup_journal_in_cursum(journal,
  1835. NAT_JOURNAL, nid, 1);
  1836. f2fs_bug_on(sbi, offset < 0);
  1837. raw_ne = &nat_in_journal(journal, offset);
  1838. nid_in_journal(journal, offset) = cpu_to_le32(nid);
  1839. } else {
  1840. raw_ne = &nat_blk->entries[nid - start_nid];
  1841. }
  1842. raw_nat_from_node_info(raw_ne, &ne->ni);
  1843. nat_reset_flag(ne);
  1844. __clear_nat_cache_dirty(NM_I(sbi), ne);
  1845. if (nat_get_blkaddr(ne) == NULL_ADDR)
  1846. add_free_nid(sbi, nid, false);
  1847. }
  1848. if (to_journal)
  1849. up_write(&curseg->journal_rwsem);
  1850. else
  1851. f2fs_put_page(page, 1);
  1852. f2fs_bug_on(sbi, set->entry_cnt);
  1853. radix_tree_delete(&NM_I(sbi)->nat_set_root, set->set);
  1854. kmem_cache_free(nat_entry_set_slab, set);
  1855. }
  1856. /*
  1857. * This function is called during the checkpointing process.
  1858. */
  1859. void flush_nat_entries(struct f2fs_sb_info *sbi)
  1860. {
  1861. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1862. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
  1863. struct f2fs_journal *journal = curseg->journal;
  1864. struct nat_entry_set *setvec[SETVEC_SIZE];
  1865. struct nat_entry_set *set, *tmp;
  1866. unsigned int found;
  1867. nid_t set_idx = 0;
  1868. LIST_HEAD(sets);
  1869. if (!nm_i->dirty_nat_cnt)
  1870. return;
  1871. down_write(&nm_i->nat_tree_lock);
  1872. /*
  1873. * if there are no enough space in journal to store dirty nat
  1874. * entries, remove all entries from journal and merge them
  1875. * into nat entry set.
  1876. */
  1877. if (!__has_cursum_space(journal, nm_i->dirty_nat_cnt, NAT_JOURNAL))
  1878. remove_nats_in_journal(sbi);
  1879. while ((found = __gang_lookup_nat_set(nm_i,
  1880. set_idx, SETVEC_SIZE, setvec))) {
  1881. unsigned idx;
  1882. set_idx = setvec[found - 1]->set + 1;
  1883. for (idx = 0; idx < found; idx++)
  1884. __adjust_nat_entry_set(setvec[idx], &sets,
  1885. MAX_NAT_JENTRIES(journal));
  1886. }
  1887. /* flush dirty nats in nat entry set */
  1888. list_for_each_entry_safe(set, tmp, &sets, set_list)
  1889. __flush_nat_entry_set(sbi, set);
  1890. up_write(&nm_i->nat_tree_lock);
  1891. f2fs_bug_on(sbi, nm_i->dirty_nat_cnt);
  1892. }
  1893. static int init_node_manager(struct f2fs_sb_info *sbi)
  1894. {
  1895. struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi);
  1896. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1897. unsigned char *version_bitmap;
  1898. unsigned int nat_segs, nat_blocks;
  1899. nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr);
  1900. /* segment_count_nat includes pair segment so divide to 2. */
  1901. nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1;
  1902. nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg);
  1903. nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nat_blocks;
  1904. /* not used nids: 0, node, meta, (and root counted as valid node) */
  1905. nm_i->available_nids = nm_i->max_nid - F2FS_RESERVED_NODE_NUM;
  1906. nm_i->fcnt = 0;
  1907. nm_i->nat_cnt = 0;
  1908. nm_i->ram_thresh = DEF_RAM_THRESHOLD;
  1909. nm_i->ra_nid_pages = DEF_RA_NID_PAGES;
  1910. nm_i->dirty_nats_ratio = DEF_DIRTY_NAT_RATIO_THRESHOLD;
  1911. INIT_RADIX_TREE(&nm_i->free_nid_root, GFP_ATOMIC);
  1912. INIT_LIST_HEAD(&nm_i->free_nid_list);
  1913. INIT_RADIX_TREE(&nm_i->nat_root, GFP_NOIO);
  1914. INIT_RADIX_TREE(&nm_i->nat_set_root, GFP_NOIO);
  1915. INIT_LIST_HEAD(&nm_i->nat_entries);
  1916. mutex_init(&nm_i->build_lock);
  1917. spin_lock_init(&nm_i->free_nid_list_lock);
  1918. init_rwsem(&nm_i->nat_tree_lock);
  1919. nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
  1920. nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
  1921. version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
  1922. if (!version_bitmap)
  1923. return -EFAULT;
  1924. nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size,
  1925. GFP_KERNEL);
  1926. if (!nm_i->nat_bitmap)
  1927. return -ENOMEM;
  1928. return 0;
  1929. }
  1930. int build_node_manager(struct f2fs_sb_info *sbi)
  1931. {
  1932. int err;
  1933. sbi->nm_info = kzalloc(sizeof(struct f2fs_nm_info), GFP_KERNEL);
  1934. if (!sbi->nm_info)
  1935. return -ENOMEM;
  1936. err = init_node_manager(sbi);
  1937. if (err)
  1938. return err;
  1939. build_free_nids(sbi);
  1940. return 0;
  1941. }
  1942. void destroy_node_manager(struct f2fs_sb_info *sbi)
  1943. {
  1944. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1945. struct free_nid *i, *next_i;
  1946. struct nat_entry *natvec[NATVEC_SIZE];
  1947. struct nat_entry_set *setvec[SETVEC_SIZE];
  1948. nid_t nid = 0;
  1949. unsigned int found;
  1950. if (!nm_i)
  1951. return;
  1952. /* destroy free nid list */
  1953. spin_lock(&nm_i->free_nid_list_lock);
  1954. list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) {
  1955. f2fs_bug_on(sbi, i->state == NID_ALLOC);
  1956. __del_from_free_nid_list(nm_i, i);
  1957. nm_i->fcnt--;
  1958. spin_unlock(&nm_i->free_nid_list_lock);
  1959. kmem_cache_free(free_nid_slab, i);
  1960. spin_lock(&nm_i->free_nid_list_lock);
  1961. }
  1962. f2fs_bug_on(sbi, nm_i->fcnt);
  1963. spin_unlock(&nm_i->free_nid_list_lock);
  1964. /* destroy nat cache */
  1965. down_write(&nm_i->nat_tree_lock);
  1966. while ((found = __gang_lookup_nat_cache(nm_i,
  1967. nid, NATVEC_SIZE, natvec))) {
  1968. unsigned idx;
  1969. nid = nat_get_nid(natvec[found - 1]) + 1;
  1970. for (idx = 0; idx < found; idx++)
  1971. __del_from_nat_cache(nm_i, natvec[idx]);
  1972. }
  1973. f2fs_bug_on(sbi, nm_i->nat_cnt);
  1974. /* destroy nat set cache */
  1975. nid = 0;
  1976. while ((found = __gang_lookup_nat_set(nm_i,
  1977. nid, SETVEC_SIZE, setvec))) {
  1978. unsigned idx;
  1979. nid = setvec[found - 1]->set + 1;
  1980. for (idx = 0; idx < found; idx++) {
  1981. /* entry_cnt is not zero, when cp_error was occurred */
  1982. f2fs_bug_on(sbi, !list_empty(&setvec[idx]->entry_list));
  1983. radix_tree_delete(&nm_i->nat_set_root, setvec[idx]->set);
  1984. kmem_cache_free(nat_entry_set_slab, setvec[idx]);
  1985. }
  1986. }
  1987. up_write(&nm_i->nat_tree_lock);
  1988. kfree(nm_i->nat_bitmap);
  1989. sbi->nm_info = NULL;
  1990. kfree(nm_i);
  1991. }
  1992. int __init create_node_manager_caches(void)
  1993. {
  1994. nat_entry_slab = f2fs_kmem_cache_create("nat_entry",
  1995. sizeof(struct nat_entry));
  1996. if (!nat_entry_slab)
  1997. goto fail;
  1998. free_nid_slab = f2fs_kmem_cache_create("free_nid",
  1999. sizeof(struct free_nid));
  2000. if (!free_nid_slab)
  2001. goto destroy_nat_entry;
  2002. nat_entry_set_slab = f2fs_kmem_cache_create("nat_entry_set",
  2003. sizeof(struct nat_entry_set));
  2004. if (!nat_entry_set_slab)
  2005. goto destroy_free_nid;
  2006. return 0;
  2007. destroy_free_nid:
  2008. kmem_cache_destroy(free_nid_slab);
  2009. destroy_nat_entry:
  2010. kmem_cache_destroy(nat_entry_slab);
  2011. fail:
  2012. return -ENOMEM;
  2013. }
  2014. void destroy_node_manager_caches(void)
  2015. {
  2016. kmem_cache_destroy(nat_entry_set_slab);
  2017. kmem_cache_destroy(free_nid_slab);
  2018. kmem_cache_destroy(nat_entry_slab);
  2019. }