node.c 53 KB

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