node.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912
  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/events/f2fs.h>
  22. static struct kmem_cache *nat_entry_slab;
  23. static struct kmem_cache *free_nid_slab;
  24. static void clear_node_page_dirty(struct page *page)
  25. {
  26. struct address_space *mapping = page->mapping;
  27. struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
  28. unsigned int long flags;
  29. if (PageDirty(page)) {
  30. spin_lock_irqsave(&mapping->tree_lock, flags);
  31. radix_tree_tag_clear(&mapping->page_tree,
  32. page_index(page),
  33. PAGECACHE_TAG_DIRTY);
  34. spin_unlock_irqrestore(&mapping->tree_lock, flags);
  35. clear_page_dirty_for_io(page);
  36. dec_page_count(sbi, F2FS_DIRTY_NODES);
  37. }
  38. ClearPageUptodate(page);
  39. }
  40. static struct page *get_current_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
  41. {
  42. pgoff_t index = current_nat_addr(sbi, nid);
  43. return get_meta_page(sbi, index);
  44. }
  45. static struct page *get_next_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
  46. {
  47. struct page *src_page;
  48. struct page *dst_page;
  49. pgoff_t src_off;
  50. pgoff_t dst_off;
  51. void *src_addr;
  52. void *dst_addr;
  53. struct f2fs_nm_info *nm_i = NM_I(sbi);
  54. src_off = current_nat_addr(sbi, nid);
  55. dst_off = next_nat_addr(sbi, src_off);
  56. /* get current nat block page with lock */
  57. src_page = get_meta_page(sbi, src_off);
  58. /* Dirty src_page means that it is already the new target NAT page. */
  59. if (PageDirty(src_page))
  60. return src_page;
  61. dst_page = grab_meta_page(sbi, dst_off);
  62. src_addr = page_address(src_page);
  63. dst_addr = page_address(dst_page);
  64. memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
  65. set_page_dirty(dst_page);
  66. f2fs_put_page(src_page, 1);
  67. set_to_next_nat(nm_i, nid);
  68. return dst_page;
  69. }
  70. /*
  71. * Readahead NAT pages
  72. */
  73. static void ra_nat_pages(struct f2fs_sb_info *sbi, int nid)
  74. {
  75. struct address_space *mapping = META_MAPPING(sbi);
  76. struct f2fs_nm_info *nm_i = NM_I(sbi);
  77. struct page *page;
  78. pgoff_t index;
  79. int i;
  80. struct f2fs_io_info fio = {
  81. .type = META,
  82. .rw = READ_SYNC | REQ_META | REQ_PRIO
  83. };
  84. for (i = 0; i < FREE_NID_PAGES; i++, nid += NAT_ENTRY_PER_BLOCK) {
  85. if (unlikely(nid >= nm_i->max_nid))
  86. nid = 0;
  87. index = current_nat_addr(sbi, nid);
  88. page = grab_cache_page(mapping, index);
  89. if (!page)
  90. continue;
  91. if (PageUptodate(page)) {
  92. mark_page_accessed(page);
  93. f2fs_put_page(page, 1);
  94. continue;
  95. }
  96. f2fs_submit_page_mbio(sbi, page, index, &fio);
  97. mark_page_accessed(page);
  98. f2fs_put_page(page, 0);
  99. }
  100. f2fs_submit_merged_bio(sbi, META, READ);
  101. }
  102. static struct nat_entry *__lookup_nat_cache(struct f2fs_nm_info *nm_i, nid_t n)
  103. {
  104. return radix_tree_lookup(&nm_i->nat_root, n);
  105. }
  106. static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info *nm_i,
  107. nid_t start, unsigned int nr, struct nat_entry **ep)
  108. {
  109. return radix_tree_gang_lookup(&nm_i->nat_root, (void **)ep, start, nr);
  110. }
  111. static void __del_from_nat_cache(struct f2fs_nm_info *nm_i, struct nat_entry *e)
  112. {
  113. list_del(&e->list);
  114. radix_tree_delete(&nm_i->nat_root, nat_get_nid(e));
  115. nm_i->nat_cnt--;
  116. kmem_cache_free(nat_entry_slab, e);
  117. }
  118. int is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid)
  119. {
  120. struct f2fs_nm_info *nm_i = NM_I(sbi);
  121. struct nat_entry *e;
  122. int is_cp = 1;
  123. read_lock(&nm_i->nat_tree_lock);
  124. e = __lookup_nat_cache(nm_i, nid);
  125. if (e && !e->checkpointed)
  126. is_cp = 0;
  127. read_unlock(&nm_i->nat_tree_lock);
  128. return is_cp;
  129. }
  130. static struct nat_entry *grab_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid)
  131. {
  132. struct nat_entry *new;
  133. new = kmem_cache_alloc(nat_entry_slab, GFP_ATOMIC);
  134. if (!new)
  135. return NULL;
  136. if (radix_tree_insert(&nm_i->nat_root, nid, new)) {
  137. kmem_cache_free(nat_entry_slab, new);
  138. return NULL;
  139. }
  140. memset(new, 0, sizeof(struct nat_entry));
  141. nat_set_nid(new, nid);
  142. list_add_tail(&new->list, &nm_i->nat_entries);
  143. nm_i->nat_cnt++;
  144. return new;
  145. }
  146. static void cache_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid,
  147. struct f2fs_nat_entry *ne)
  148. {
  149. struct nat_entry *e;
  150. retry:
  151. write_lock(&nm_i->nat_tree_lock);
  152. e = __lookup_nat_cache(nm_i, nid);
  153. if (!e) {
  154. e = grab_nat_entry(nm_i, nid);
  155. if (!e) {
  156. write_unlock(&nm_i->nat_tree_lock);
  157. goto retry;
  158. }
  159. nat_set_blkaddr(e, le32_to_cpu(ne->block_addr));
  160. nat_set_ino(e, le32_to_cpu(ne->ino));
  161. nat_set_version(e, ne->version);
  162. e->checkpointed = true;
  163. }
  164. write_unlock(&nm_i->nat_tree_lock);
  165. }
  166. static void set_node_addr(struct f2fs_sb_info *sbi, struct node_info *ni,
  167. block_t new_blkaddr)
  168. {
  169. struct f2fs_nm_info *nm_i = NM_I(sbi);
  170. struct nat_entry *e;
  171. retry:
  172. write_lock(&nm_i->nat_tree_lock);
  173. e = __lookup_nat_cache(nm_i, ni->nid);
  174. if (!e) {
  175. e = grab_nat_entry(nm_i, ni->nid);
  176. if (!e) {
  177. write_unlock(&nm_i->nat_tree_lock);
  178. goto retry;
  179. }
  180. e->ni = *ni;
  181. e->checkpointed = true;
  182. f2fs_bug_on(ni->blk_addr == NEW_ADDR);
  183. } else if (new_blkaddr == NEW_ADDR) {
  184. /*
  185. * when nid is reallocated,
  186. * previous nat entry can be remained in nat cache.
  187. * So, reinitialize it with new information.
  188. */
  189. e->ni = *ni;
  190. f2fs_bug_on(ni->blk_addr != NULL_ADDR);
  191. }
  192. if (new_blkaddr == NEW_ADDR)
  193. e->checkpointed = false;
  194. /* sanity check */
  195. f2fs_bug_on(nat_get_blkaddr(e) != ni->blk_addr);
  196. f2fs_bug_on(nat_get_blkaddr(e) == NULL_ADDR &&
  197. new_blkaddr == NULL_ADDR);
  198. f2fs_bug_on(nat_get_blkaddr(e) == NEW_ADDR &&
  199. new_blkaddr == NEW_ADDR);
  200. f2fs_bug_on(nat_get_blkaddr(e) != NEW_ADDR &&
  201. nat_get_blkaddr(e) != NULL_ADDR &&
  202. new_blkaddr == NEW_ADDR);
  203. /* increament version no as node is removed */
  204. if (nat_get_blkaddr(e) != NEW_ADDR && new_blkaddr == NULL_ADDR) {
  205. unsigned char version = nat_get_version(e);
  206. nat_set_version(e, inc_node_version(version));
  207. }
  208. /* change address */
  209. nat_set_blkaddr(e, new_blkaddr);
  210. __set_nat_cache_dirty(nm_i, e);
  211. write_unlock(&nm_i->nat_tree_lock);
  212. }
  213. int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink)
  214. {
  215. struct f2fs_nm_info *nm_i = NM_I(sbi);
  216. if (nm_i->nat_cnt <= NM_WOUT_THRESHOLD)
  217. return 0;
  218. write_lock(&nm_i->nat_tree_lock);
  219. while (nr_shrink && !list_empty(&nm_i->nat_entries)) {
  220. struct nat_entry *ne;
  221. ne = list_first_entry(&nm_i->nat_entries,
  222. struct nat_entry, list);
  223. __del_from_nat_cache(nm_i, ne);
  224. nr_shrink--;
  225. }
  226. write_unlock(&nm_i->nat_tree_lock);
  227. return nr_shrink;
  228. }
  229. /*
  230. * This function returns always success
  231. */
  232. void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni)
  233. {
  234. struct f2fs_nm_info *nm_i = NM_I(sbi);
  235. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
  236. struct f2fs_summary_block *sum = curseg->sum_blk;
  237. nid_t start_nid = START_NID(nid);
  238. struct f2fs_nat_block *nat_blk;
  239. struct page *page = NULL;
  240. struct f2fs_nat_entry ne;
  241. struct nat_entry *e;
  242. int i;
  243. memset(&ne, 0, sizeof(struct f2fs_nat_entry));
  244. ni->nid = nid;
  245. /* Check nat cache */
  246. read_lock(&nm_i->nat_tree_lock);
  247. e = __lookup_nat_cache(nm_i, nid);
  248. if (e) {
  249. ni->ino = nat_get_ino(e);
  250. ni->blk_addr = nat_get_blkaddr(e);
  251. ni->version = nat_get_version(e);
  252. }
  253. read_unlock(&nm_i->nat_tree_lock);
  254. if (e)
  255. return;
  256. /* Check current segment summary */
  257. mutex_lock(&curseg->curseg_mutex);
  258. i = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 0);
  259. if (i >= 0) {
  260. ne = nat_in_journal(sum, i);
  261. node_info_from_raw_nat(ni, &ne);
  262. }
  263. mutex_unlock(&curseg->curseg_mutex);
  264. if (i >= 0)
  265. goto cache;
  266. /* Fill node_info from nat page */
  267. page = get_current_nat_page(sbi, start_nid);
  268. nat_blk = (struct f2fs_nat_block *)page_address(page);
  269. ne = nat_blk->entries[nid - start_nid];
  270. node_info_from_raw_nat(ni, &ne);
  271. f2fs_put_page(page, 1);
  272. cache:
  273. /* cache nat entry */
  274. cache_nat_entry(NM_I(sbi), nid, &ne);
  275. }
  276. /*
  277. * The maximum depth is four.
  278. * Offset[0] will have raw inode offset.
  279. */
  280. static int get_node_path(struct f2fs_inode_info *fi, long block,
  281. int offset[4], unsigned int noffset[4])
  282. {
  283. const long direct_index = ADDRS_PER_INODE(fi);
  284. const long direct_blks = ADDRS_PER_BLOCK;
  285. const long dptrs_per_blk = NIDS_PER_BLOCK;
  286. const long indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
  287. const long dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
  288. int n = 0;
  289. int level = 0;
  290. noffset[0] = 0;
  291. if (block < direct_index) {
  292. offset[n] = block;
  293. goto got;
  294. }
  295. block -= direct_index;
  296. if (block < direct_blks) {
  297. offset[n++] = NODE_DIR1_BLOCK;
  298. noffset[n] = 1;
  299. offset[n] = block;
  300. level = 1;
  301. goto got;
  302. }
  303. block -= direct_blks;
  304. if (block < direct_blks) {
  305. offset[n++] = NODE_DIR2_BLOCK;
  306. noffset[n] = 2;
  307. offset[n] = block;
  308. level = 1;
  309. goto got;
  310. }
  311. block -= direct_blks;
  312. if (block < indirect_blks) {
  313. offset[n++] = NODE_IND1_BLOCK;
  314. noffset[n] = 3;
  315. offset[n++] = block / direct_blks;
  316. noffset[n] = 4 + offset[n - 1];
  317. offset[n] = block % direct_blks;
  318. level = 2;
  319. goto got;
  320. }
  321. block -= indirect_blks;
  322. if (block < indirect_blks) {
  323. offset[n++] = NODE_IND2_BLOCK;
  324. noffset[n] = 4 + dptrs_per_blk;
  325. offset[n++] = block / direct_blks;
  326. noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
  327. offset[n] = block % direct_blks;
  328. level = 2;
  329. goto got;
  330. }
  331. block -= indirect_blks;
  332. if (block < dindirect_blks) {
  333. offset[n++] = NODE_DIND_BLOCK;
  334. noffset[n] = 5 + (dptrs_per_blk * 2);
  335. offset[n++] = block / indirect_blks;
  336. noffset[n] = 6 + (dptrs_per_blk * 2) +
  337. offset[n - 1] * (dptrs_per_blk + 1);
  338. offset[n++] = (block / direct_blks) % dptrs_per_blk;
  339. noffset[n] = 7 + (dptrs_per_blk * 2) +
  340. offset[n - 2] * (dptrs_per_blk + 1) +
  341. offset[n - 1];
  342. offset[n] = block % direct_blks;
  343. level = 3;
  344. goto got;
  345. } else {
  346. BUG();
  347. }
  348. got:
  349. return level;
  350. }
  351. /*
  352. * Caller should call f2fs_put_dnode(dn).
  353. * Also, it should grab and release a rwsem by calling f2fs_lock_op() and
  354. * f2fs_unlock_op() only if ro is not set RDONLY_NODE.
  355. * In the case of RDONLY_NODE, we don't need to care about mutex.
  356. */
  357. int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode)
  358. {
  359. struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
  360. struct page *npage[4];
  361. struct page *parent;
  362. int offset[4];
  363. unsigned int noffset[4];
  364. nid_t nids[4];
  365. int level, i;
  366. int err = 0;
  367. level = get_node_path(F2FS_I(dn->inode), index, offset, noffset);
  368. nids[0] = dn->inode->i_ino;
  369. npage[0] = dn->inode_page;
  370. if (!npage[0]) {
  371. npage[0] = get_node_page(sbi, nids[0]);
  372. if (IS_ERR(npage[0]))
  373. return PTR_ERR(npage[0]);
  374. }
  375. parent = npage[0];
  376. if (level != 0)
  377. nids[1] = get_nid(parent, offset[0], true);
  378. dn->inode_page = npage[0];
  379. dn->inode_page_locked = true;
  380. /* get indirect or direct nodes */
  381. for (i = 1; i <= level; i++) {
  382. bool done = false;
  383. if (!nids[i] && mode == ALLOC_NODE) {
  384. /* alloc new node */
  385. if (!alloc_nid(sbi, &(nids[i]))) {
  386. err = -ENOSPC;
  387. goto release_pages;
  388. }
  389. dn->nid = nids[i];
  390. npage[i] = new_node_page(dn, noffset[i], NULL);
  391. if (IS_ERR(npage[i])) {
  392. alloc_nid_failed(sbi, nids[i]);
  393. err = PTR_ERR(npage[i]);
  394. goto release_pages;
  395. }
  396. set_nid(parent, offset[i - 1], nids[i], i == 1);
  397. alloc_nid_done(sbi, nids[i]);
  398. done = true;
  399. } else if (mode == LOOKUP_NODE_RA && i == level && level > 1) {
  400. npage[i] = get_node_page_ra(parent, offset[i - 1]);
  401. if (IS_ERR(npage[i])) {
  402. err = PTR_ERR(npage[i]);
  403. goto release_pages;
  404. }
  405. done = true;
  406. }
  407. if (i == 1) {
  408. dn->inode_page_locked = false;
  409. unlock_page(parent);
  410. } else {
  411. f2fs_put_page(parent, 1);
  412. }
  413. if (!done) {
  414. npage[i] = get_node_page(sbi, nids[i]);
  415. if (IS_ERR(npage[i])) {
  416. err = PTR_ERR(npage[i]);
  417. f2fs_put_page(npage[0], 0);
  418. goto release_out;
  419. }
  420. }
  421. if (i < level) {
  422. parent = npage[i];
  423. nids[i + 1] = get_nid(parent, offset[i], false);
  424. }
  425. }
  426. dn->nid = nids[level];
  427. dn->ofs_in_node = offset[level];
  428. dn->node_page = npage[level];
  429. dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
  430. return 0;
  431. release_pages:
  432. f2fs_put_page(parent, 1);
  433. if (i > 1)
  434. f2fs_put_page(npage[0], 0);
  435. release_out:
  436. dn->inode_page = NULL;
  437. dn->node_page = NULL;
  438. return err;
  439. }
  440. static void truncate_node(struct dnode_of_data *dn)
  441. {
  442. struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
  443. struct node_info ni;
  444. get_node_info(sbi, dn->nid, &ni);
  445. if (dn->inode->i_blocks == 0) {
  446. f2fs_bug_on(ni.blk_addr != NULL_ADDR);
  447. goto invalidate;
  448. }
  449. f2fs_bug_on(ni.blk_addr == NULL_ADDR);
  450. /* Deallocate node address */
  451. invalidate_blocks(sbi, ni.blk_addr);
  452. dec_valid_node_count(sbi, dn->inode);
  453. set_node_addr(sbi, &ni, NULL_ADDR);
  454. if (dn->nid == dn->inode->i_ino) {
  455. remove_orphan_inode(sbi, dn->nid);
  456. dec_valid_inode_count(sbi);
  457. } else {
  458. sync_inode_page(dn);
  459. }
  460. invalidate:
  461. clear_node_page_dirty(dn->node_page);
  462. F2FS_SET_SB_DIRT(sbi);
  463. f2fs_put_page(dn->node_page, 1);
  464. invalidate_mapping_pages(NODE_MAPPING(sbi),
  465. dn->node_page->index, dn->node_page->index);
  466. dn->node_page = NULL;
  467. trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr);
  468. }
  469. static int truncate_dnode(struct dnode_of_data *dn)
  470. {
  471. struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
  472. struct page *page;
  473. if (dn->nid == 0)
  474. return 1;
  475. /* get direct node */
  476. page = get_node_page(sbi, dn->nid);
  477. if (IS_ERR(page) && PTR_ERR(page) == -ENOENT)
  478. return 1;
  479. else if (IS_ERR(page))
  480. return PTR_ERR(page);
  481. /* Make dnode_of_data for parameter */
  482. dn->node_page = page;
  483. dn->ofs_in_node = 0;
  484. truncate_data_blocks(dn);
  485. truncate_node(dn);
  486. return 1;
  487. }
  488. static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs,
  489. int ofs, int depth)
  490. {
  491. struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
  492. struct dnode_of_data rdn = *dn;
  493. struct page *page;
  494. struct f2fs_node *rn;
  495. nid_t child_nid;
  496. unsigned int child_nofs;
  497. int freed = 0;
  498. int i, ret;
  499. if (dn->nid == 0)
  500. return NIDS_PER_BLOCK + 1;
  501. trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr);
  502. page = get_node_page(sbi, dn->nid);
  503. if (IS_ERR(page)) {
  504. trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page));
  505. return PTR_ERR(page);
  506. }
  507. rn = F2FS_NODE(page);
  508. if (depth < 3) {
  509. for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) {
  510. child_nid = le32_to_cpu(rn->in.nid[i]);
  511. if (child_nid == 0)
  512. continue;
  513. rdn.nid = child_nid;
  514. ret = truncate_dnode(&rdn);
  515. if (ret < 0)
  516. goto out_err;
  517. set_nid(page, i, 0, false);
  518. }
  519. } else {
  520. child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1;
  521. for (i = ofs; i < NIDS_PER_BLOCK; i++) {
  522. child_nid = le32_to_cpu(rn->in.nid[i]);
  523. if (child_nid == 0) {
  524. child_nofs += NIDS_PER_BLOCK + 1;
  525. continue;
  526. }
  527. rdn.nid = child_nid;
  528. ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1);
  529. if (ret == (NIDS_PER_BLOCK + 1)) {
  530. set_nid(page, i, 0, false);
  531. child_nofs += ret;
  532. } else if (ret < 0 && ret != -ENOENT) {
  533. goto out_err;
  534. }
  535. }
  536. freed = child_nofs;
  537. }
  538. if (!ofs) {
  539. /* remove current indirect node */
  540. dn->node_page = page;
  541. truncate_node(dn);
  542. freed++;
  543. } else {
  544. f2fs_put_page(page, 1);
  545. }
  546. trace_f2fs_truncate_nodes_exit(dn->inode, freed);
  547. return freed;
  548. out_err:
  549. f2fs_put_page(page, 1);
  550. trace_f2fs_truncate_nodes_exit(dn->inode, ret);
  551. return ret;
  552. }
  553. static int truncate_partial_nodes(struct dnode_of_data *dn,
  554. struct f2fs_inode *ri, int *offset, int depth)
  555. {
  556. struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
  557. struct page *pages[2];
  558. nid_t nid[3];
  559. nid_t child_nid;
  560. int err = 0;
  561. int i;
  562. int idx = depth - 2;
  563. nid[0] = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
  564. if (!nid[0])
  565. return 0;
  566. /* get indirect nodes in the path */
  567. for (i = 0; i < idx + 1; i++) {
  568. /* refernece count'll be increased */
  569. pages[i] = get_node_page(sbi, nid[i]);
  570. if (IS_ERR(pages[i])) {
  571. err = PTR_ERR(pages[i]);
  572. idx = i - 1;
  573. goto fail;
  574. }
  575. nid[i + 1] = get_nid(pages[i], offset[i + 1], false);
  576. }
  577. /* free direct nodes linked to a partial indirect node */
  578. for (i = offset[idx + 1]; i < NIDS_PER_BLOCK; i++) {
  579. child_nid = get_nid(pages[idx], i, false);
  580. if (!child_nid)
  581. continue;
  582. dn->nid = child_nid;
  583. err = truncate_dnode(dn);
  584. if (err < 0)
  585. goto fail;
  586. set_nid(pages[idx], i, 0, false);
  587. }
  588. if (offset[idx + 1] == 0) {
  589. dn->node_page = pages[idx];
  590. dn->nid = nid[idx];
  591. truncate_node(dn);
  592. } else {
  593. f2fs_put_page(pages[idx], 1);
  594. }
  595. offset[idx]++;
  596. offset[idx + 1] = 0;
  597. idx--;
  598. fail:
  599. for (i = idx; i >= 0; i--)
  600. f2fs_put_page(pages[i], 1);
  601. trace_f2fs_truncate_partial_nodes(dn->inode, nid, depth, err);
  602. return err;
  603. }
  604. /*
  605. * All the block addresses of data and nodes should be nullified.
  606. */
  607. int truncate_inode_blocks(struct inode *inode, pgoff_t from)
  608. {
  609. struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
  610. int err = 0, cont = 1;
  611. int level, offset[4], noffset[4];
  612. unsigned int nofs = 0;
  613. struct f2fs_inode *ri;
  614. struct dnode_of_data dn;
  615. struct page *page;
  616. trace_f2fs_truncate_inode_blocks_enter(inode, from);
  617. level = get_node_path(F2FS_I(inode), from, offset, noffset);
  618. restart:
  619. page = get_node_page(sbi, inode->i_ino);
  620. if (IS_ERR(page)) {
  621. trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(page));
  622. return PTR_ERR(page);
  623. }
  624. set_new_dnode(&dn, inode, page, NULL, 0);
  625. unlock_page(page);
  626. ri = F2FS_INODE(page);
  627. switch (level) {
  628. case 0:
  629. case 1:
  630. nofs = noffset[1];
  631. break;
  632. case 2:
  633. nofs = noffset[1];
  634. if (!offset[level - 1])
  635. goto skip_partial;
  636. err = truncate_partial_nodes(&dn, ri, offset, level);
  637. if (err < 0 && err != -ENOENT)
  638. goto fail;
  639. nofs += 1 + NIDS_PER_BLOCK;
  640. break;
  641. case 3:
  642. nofs = 5 + 2 * NIDS_PER_BLOCK;
  643. if (!offset[level - 1])
  644. goto skip_partial;
  645. err = truncate_partial_nodes(&dn, ri, offset, level);
  646. if (err < 0 && err != -ENOENT)
  647. goto fail;
  648. break;
  649. default:
  650. BUG();
  651. }
  652. skip_partial:
  653. while (cont) {
  654. dn.nid = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
  655. switch (offset[0]) {
  656. case NODE_DIR1_BLOCK:
  657. case NODE_DIR2_BLOCK:
  658. err = truncate_dnode(&dn);
  659. break;
  660. case NODE_IND1_BLOCK:
  661. case NODE_IND2_BLOCK:
  662. err = truncate_nodes(&dn, nofs, offset[1], 2);
  663. break;
  664. case NODE_DIND_BLOCK:
  665. err = truncate_nodes(&dn, nofs, offset[1], 3);
  666. cont = 0;
  667. break;
  668. default:
  669. BUG();
  670. }
  671. if (err < 0 && err != -ENOENT)
  672. goto fail;
  673. if (offset[1] == 0 &&
  674. ri->i_nid[offset[0] - NODE_DIR1_BLOCK]) {
  675. lock_page(page);
  676. if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
  677. f2fs_put_page(page, 1);
  678. goto restart;
  679. }
  680. wait_on_page_writeback(page);
  681. ri->i_nid[offset[0] - NODE_DIR1_BLOCK] = 0;
  682. set_page_dirty(page);
  683. unlock_page(page);
  684. }
  685. offset[1] = 0;
  686. offset[0]++;
  687. nofs += err;
  688. }
  689. fail:
  690. f2fs_put_page(page, 0);
  691. trace_f2fs_truncate_inode_blocks_exit(inode, err);
  692. return err > 0 ? 0 : err;
  693. }
  694. int truncate_xattr_node(struct inode *inode, struct page *page)
  695. {
  696. struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
  697. nid_t nid = F2FS_I(inode)->i_xattr_nid;
  698. struct dnode_of_data dn;
  699. struct page *npage;
  700. if (!nid)
  701. return 0;
  702. npage = get_node_page(sbi, nid);
  703. if (IS_ERR(npage))
  704. return PTR_ERR(npage);
  705. F2FS_I(inode)->i_xattr_nid = 0;
  706. /* need to do checkpoint during fsync */
  707. F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi));
  708. set_new_dnode(&dn, inode, page, npage, nid);
  709. if (page)
  710. dn.inode_page_locked = true;
  711. truncate_node(&dn);
  712. return 0;
  713. }
  714. /*
  715. * Caller should grab and release a rwsem by calling f2fs_lock_op() and
  716. * f2fs_unlock_op().
  717. */
  718. void remove_inode_page(struct inode *inode)
  719. {
  720. struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
  721. struct page *page;
  722. nid_t ino = inode->i_ino;
  723. struct dnode_of_data dn;
  724. page = get_node_page(sbi, ino);
  725. if (IS_ERR(page))
  726. return;
  727. if (truncate_xattr_node(inode, page)) {
  728. f2fs_put_page(page, 1);
  729. return;
  730. }
  731. /* 0 is possible, after f2fs_new_inode() is failed */
  732. f2fs_bug_on(inode->i_blocks != 0 && inode->i_blocks != 1);
  733. set_new_dnode(&dn, inode, page, page, ino);
  734. truncate_node(&dn);
  735. }
  736. struct page *new_inode_page(struct inode *inode, const struct qstr *name)
  737. {
  738. struct dnode_of_data dn;
  739. /* allocate inode page for new inode */
  740. set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
  741. /* caller should f2fs_put_page(page, 1); */
  742. return new_node_page(&dn, 0, NULL);
  743. }
  744. struct page *new_node_page(struct dnode_of_data *dn,
  745. unsigned int ofs, struct page *ipage)
  746. {
  747. struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
  748. struct node_info old_ni, new_ni;
  749. struct page *page;
  750. int err;
  751. if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
  752. return ERR_PTR(-EPERM);
  753. page = grab_cache_page(NODE_MAPPING(sbi), dn->nid);
  754. if (!page)
  755. return ERR_PTR(-ENOMEM);
  756. if (unlikely(!inc_valid_node_count(sbi, dn->inode))) {
  757. err = -ENOSPC;
  758. goto fail;
  759. }
  760. get_node_info(sbi, dn->nid, &old_ni);
  761. /* Reinitialize old_ni with new node page */
  762. f2fs_bug_on(old_ni.blk_addr != NULL_ADDR);
  763. new_ni = old_ni;
  764. new_ni.ino = dn->inode->i_ino;
  765. set_node_addr(sbi, &new_ni, NEW_ADDR);
  766. fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
  767. set_cold_node(dn->inode, page);
  768. SetPageUptodate(page);
  769. set_page_dirty(page);
  770. if (ofs == XATTR_NODE_OFFSET)
  771. F2FS_I(dn->inode)->i_xattr_nid = dn->nid;
  772. dn->node_page = page;
  773. if (ipage)
  774. update_inode(dn->inode, ipage);
  775. else
  776. sync_inode_page(dn);
  777. if (ofs == 0)
  778. inc_valid_inode_count(sbi);
  779. return page;
  780. fail:
  781. clear_node_page_dirty(page);
  782. f2fs_put_page(page, 1);
  783. return ERR_PTR(err);
  784. }
  785. /*
  786. * Caller should do after getting the following values.
  787. * 0: f2fs_put_page(page, 0)
  788. * LOCKED_PAGE: f2fs_put_page(page, 1)
  789. * error: nothing
  790. */
  791. static int read_node_page(struct page *page, int rw)
  792. {
  793. struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
  794. struct node_info ni;
  795. get_node_info(sbi, page->index, &ni);
  796. if (unlikely(ni.blk_addr == NULL_ADDR)) {
  797. f2fs_put_page(page, 1);
  798. return -ENOENT;
  799. }
  800. if (PageUptodate(page))
  801. return LOCKED_PAGE;
  802. return f2fs_submit_page_bio(sbi, page, ni.blk_addr, rw);
  803. }
  804. /*
  805. * Readahead a node page
  806. */
  807. void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid)
  808. {
  809. struct page *apage;
  810. int err;
  811. apage = find_get_page(NODE_MAPPING(sbi), nid);
  812. if (apage && PageUptodate(apage)) {
  813. f2fs_put_page(apage, 0);
  814. return;
  815. }
  816. f2fs_put_page(apage, 0);
  817. apage = grab_cache_page(NODE_MAPPING(sbi), nid);
  818. if (!apage)
  819. return;
  820. err = read_node_page(apage, READA);
  821. if (err == 0)
  822. f2fs_put_page(apage, 0);
  823. else if (err == LOCKED_PAGE)
  824. f2fs_put_page(apage, 1);
  825. }
  826. struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid)
  827. {
  828. struct page *page;
  829. int err;
  830. repeat:
  831. page = grab_cache_page(NODE_MAPPING(sbi), nid);
  832. if (!page)
  833. return ERR_PTR(-ENOMEM);
  834. err = read_node_page(page, READ_SYNC);
  835. if (err < 0)
  836. return ERR_PTR(err);
  837. else if (err == LOCKED_PAGE)
  838. goto got_it;
  839. lock_page(page);
  840. if (unlikely(!PageUptodate(page))) {
  841. f2fs_put_page(page, 1);
  842. return ERR_PTR(-EIO);
  843. }
  844. if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
  845. f2fs_put_page(page, 1);
  846. goto repeat;
  847. }
  848. got_it:
  849. f2fs_bug_on(nid != nid_of_node(page));
  850. mark_page_accessed(page);
  851. return page;
  852. }
  853. /*
  854. * Return a locked page for the desired node page.
  855. * And, readahead MAX_RA_NODE number of node pages.
  856. */
  857. struct page *get_node_page_ra(struct page *parent, int start)
  858. {
  859. struct f2fs_sb_info *sbi = F2FS_SB(parent->mapping->host->i_sb);
  860. struct blk_plug plug;
  861. struct page *page;
  862. int err, i, end;
  863. nid_t nid;
  864. /* First, try getting the desired direct node. */
  865. nid = get_nid(parent, start, false);
  866. if (!nid)
  867. return ERR_PTR(-ENOENT);
  868. repeat:
  869. page = grab_cache_page(NODE_MAPPING(sbi), nid);
  870. if (!page)
  871. return ERR_PTR(-ENOMEM);
  872. err = read_node_page(page, READ_SYNC);
  873. if (err < 0)
  874. return ERR_PTR(err);
  875. else if (err == LOCKED_PAGE)
  876. goto page_hit;
  877. blk_start_plug(&plug);
  878. /* Then, try readahead for siblings of the desired node */
  879. end = start + MAX_RA_NODE;
  880. end = min(end, NIDS_PER_BLOCK);
  881. for (i = start + 1; i < end; i++) {
  882. nid = get_nid(parent, i, false);
  883. if (!nid)
  884. continue;
  885. ra_node_page(sbi, nid);
  886. }
  887. blk_finish_plug(&plug);
  888. lock_page(page);
  889. if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
  890. f2fs_put_page(page, 1);
  891. goto repeat;
  892. }
  893. page_hit:
  894. if (unlikely(!PageUptodate(page))) {
  895. f2fs_put_page(page, 1);
  896. return ERR_PTR(-EIO);
  897. }
  898. mark_page_accessed(page);
  899. return page;
  900. }
  901. void sync_inode_page(struct dnode_of_data *dn)
  902. {
  903. if (IS_INODE(dn->node_page) || dn->inode_page == dn->node_page) {
  904. update_inode(dn->inode, dn->node_page);
  905. } else if (dn->inode_page) {
  906. if (!dn->inode_page_locked)
  907. lock_page(dn->inode_page);
  908. update_inode(dn->inode, dn->inode_page);
  909. if (!dn->inode_page_locked)
  910. unlock_page(dn->inode_page);
  911. } else {
  912. update_inode_page(dn->inode);
  913. }
  914. }
  915. int sync_node_pages(struct f2fs_sb_info *sbi, nid_t ino,
  916. struct writeback_control *wbc)
  917. {
  918. pgoff_t index, end;
  919. struct pagevec pvec;
  920. int step = ino ? 2 : 0;
  921. int nwritten = 0, wrote = 0;
  922. pagevec_init(&pvec, 0);
  923. next_step:
  924. index = 0;
  925. end = LONG_MAX;
  926. while (index <= end) {
  927. int i, nr_pages;
  928. nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
  929. PAGECACHE_TAG_DIRTY,
  930. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
  931. if (nr_pages == 0)
  932. break;
  933. for (i = 0; i < nr_pages; i++) {
  934. struct page *page = pvec.pages[i];
  935. /*
  936. * flushing sequence with step:
  937. * 0. indirect nodes
  938. * 1. dentry dnodes
  939. * 2. file dnodes
  940. */
  941. if (step == 0 && IS_DNODE(page))
  942. continue;
  943. if (step == 1 && (!IS_DNODE(page) ||
  944. is_cold_node(page)))
  945. continue;
  946. if (step == 2 && (!IS_DNODE(page) ||
  947. !is_cold_node(page)))
  948. continue;
  949. /*
  950. * If an fsync mode,
  951. * we should not skip writing node pages.
  952. */
  953. if (ino && ino_of_node(page) == ino)
  954. lock_page(page);
  955. else if (!trylock_page(page))
  956. continue;
  957. if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
  958. continue_unlock:
  959. unlock_page(page);
  960. continue;
  961. }
  962. if (ino && ino_of_node(page) != ino)
  963. goto continue_unlock;
  964. if (!PageDirty(page)) {
  965. /* someone wrote it for us */
  966. goto continue_unlock;
  967. }
  968. if (!clear_page_dirty_for_io(page))
  969. goto continue_unlock;
  970. /* called by fsync() */
  971. if (ino && IS_DNODE(page)) {
  972. int mark = !is_checkpointed_node(sbi, ino);
  973. set_fsync_mark(page, 1);
  974. if (IS_INODE(page))
  975. set_dentry_mark(page, mark);
  976. nwritten++;
  977. } else {
  978. set_fsync_mark(page, 0);
  979. set_dentry_mark(page, 0);
  980. }
  981. NODE_MAPPING(sbi)->a_ops->writepage(page, wbc);
  982. wrote++;
  983. if (--wbc->nr_to_write == 0)
  984. break;
  985. }
  986. pagevec_release(&pvec);
  987. cond_resched();
  988. if (wbc->nr_to_write == 0) {
  989. step = 2;
  990. break;
  991. }
  992. }
  993. if (step < 2) {
  994. step++;
  995. goto next_step;
  996. }
  997. if (wrote)
  998. f2fs_submit_merged_bio(sbi, NODE, WRITE);
  999. return nwritten;
  1000. }
  1001. int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino)
  1002. {
  1003. pgoff_t index = 0, end = LONG_MAX;
  1004. struct pagevec pvec;
  1005. int ret2 = 0, ret = 0;
  1006. pagevec_init(&pvec, 0);
  1007. while (index <= end) {
  1008. int i, nr_pages;
  1009. nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
  1010. PAGECACHE_TAG_WRITEBACK,
  1011. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
  1012. if (nr_pages == 0)
  1013. break;
  1014. for (i = 0; i < nr_pages; i++) {
  1015. struct page *page = pvec.pages[i];
  1016. /* until radix tree lookup accepts end_index */
  1017. if (unlikely(page->index > end))
  1018. continue;
  1019. if (ino && ino_of_node(page) == ino) {
  1020. wait_on_page_writeback(page);
  1021. if (TestClearPageError(page))
  1022. ret = -EIO;
  1023. }
  1024. }
  1025. pagevec_release(&pvec);
  1026. cond_resched();
  1027. }
  1028. if (unlikely(test_and_clear_bit(AS_ENOSPC, &NODE_MAPPING(sbi)->flags)))
  1029. ret2 = -ENOSPC;
  1030. if (unlikely(test_and_clear_bit(AS_EIO, &NODE_MAPPING(sbi)->flags)))
  1031. ret2 = -EIO;
  1032. if (!ret)
  1033. ret = ret2;
  1034. return ret;
  1035. }
  1036. static int f2fs_write_node_page(struct page *page,
  1037. struct writeback_control *wbc)
  1038. {
  1039. struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
  1040. nid_t nid;
  1041. block_t new_addr;
  1042. struct node_info ni;
  1043. struct f2fs_io_info fio = {
  1044. .type = NODE,
  1045. .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
  1046. };
  1047. if (unlikely(sbi->por_doing))
  1048. goto redirty_out;
  1049. wait_on_page_writeback(page);
  1050. /* get old block addr of this node page */
  1051. nid = nid_of_node(page);
  1052. f2fs_bug_on(page->index != nid);
  1053. get_node_info(sbi, nid, &ni);
  1054. /* This page is already truncated */
  1055. if (unlikely(ni.blk_addr == NULL_ADDR)) {
  1056. dec_page_count(sbi, F2FS_DIRTY_NODES);
  1057. unlock_page(page);
  1058. return 0;
  1059. }
  1060. if (wbc->for_reclaim)
  1061. goto redirty_out;
  1062. mutex_lock(&sbi->node_write);
  1063. set_page_writeback(page);
  1064. write_node_page(sbi, page, &fio, nid, ni.blk_addr, &new_addr);
  1065. set_node_addr(sbi, &ni, new_addr);
  1066. dec_page_count(sbi, F2FS_DIRTY_NODES);
  1067. mutex_unlock(&sbi->node_write);
  1068. unlock_page(page);
  1069. return 0;
  1070. redirty_out:
  1071. dec_page_count(sbi, F2FS_DIRTY_NODES);
  1072. wbc->pages_skipped++;
  1073. set_page_dirty(page);
  1074. return AOP_WRITEPAGE_ACTIVATE;
  1075. }
  1076. /*
  1077. * It is very important to gather dirty pages and write at once, so that we can
  1078. * submit a big bio without interfering other data writes.
  1079. * Be default, 512 pages (2MB) * 3 node types, is more reasonable.
  1080. */
  1081. #define COLLECT_DIRTY_NODES 1536
  1082. static int f2fs_write_node_pages(struct address_space *mapping,
  1083. struct writeback_control *wbc)
  1084. {
  1085. struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
  1086. long nr_to_write = wbc->nr_to_write;
  1087. /* balancing f2fs's metadata in background */
  1088. f2fs_balance_fs_bg(sbi);
  1089. /* collect a number of dirty node pages and write together */
  1090. if (get_pages(sbi, F2FS_DIRTY_NODES) < COLLECT_DIRTY_NODES)
  1091. return 0;
  1092. /* if mounting is failed, skip writing node pages */
  1093. wbc->nr_to_write = 3 * max_hw_blocks(sbi);
  1094. wbc->sync_mode = WB_SYNC_NONE;
  1095. sync_node_pages(sbi, 0, wbc);
  1096. wbc->nr_to_write = nr_to_write - (3 * max_hw_blocks(sbi) -
  1097. wbc->nr_to_write);
  1098. return 0;
  1099. }
  1100. static int f2fs_set_node_page_dirty(struct page *page)
  1101. {
  1102. struct address_space *mapping = page->mapping;
  1103. struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
  1104. trace_f2fs_set_page_dirty(page, NODE);
  1105. SetPageUptodate(page);
  1106. if (!PageDirty(page)) {
  1107. __set_page_dirty_nobuffers(page);
  1108. inc_page_count(sbi, F2FS_DIRTY_NODES);
  1109. SetPagePrivate(page);
  1110. return 1;
  1111. }
  1112. return 0;
  1113. }
  1114. static void f2fs_invalidate_node_page(struct page *page, unsigned int offset,
  1115. unsigned int length)
  1116. {
  1117. struct inode *inode = page->mapping->host;
  1118. struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
  1119. if (PageDirty(page))
  1120. dec_page_count(sbi, F2FS_DIRTY_NODES);
  1121. ClearPagePrivate(page);
  1122. }
  1123. static int f2fs_release_node_page(struct page *page, gfp_t wait)
  1124. {
  1125. ClearPagePrivate(page);
  1126. return 1;
  1127. }
  1128. /*
  1129. * Structure of the f2fs node operations
  1130. */
  1131. const struct address_space_operations f2fs_node_aops = {
  1132. .writepage = f2fs_write_node_page,
  1133. .writepages = f2fs_write_node_pages,
  1134. .set_page_dirty = f2fs_set_node_page_dirty,
  1135. .invalidatepage = f2fs_invalidate_node_page,
  1136. .releasepage = f2fs_release_node_page,
  1137. };
  1138. static struct free_nid *__lookup_free_nid_list(nid_t n, struct list_head *head)
  1139. {
  1140. struct list_head *this;
  1141. struct free_nid *i;
  1142. list_for_each(this, head) {
  1143. i = list_entry(this, struct free_nid, list);
  1144. if (i->nid == n)
  1145. return i;
  1146. }
  1147. return NULL;
  1148. }
  1149. static void __del_from_free_nid_list(struct free_nid *i)
  1150. {
  1151. list_del(&i->list);
  1152. kmem_cache_free(free_nid_slab, i);
  1153. }
  1154. static int add_free_nid(struct f2fs_nm_info *nm_i, nid_t nid, bool build)
  1155. {
  1156. struct free_nid *i;
  1157. struct nat_entry *ne;
  1158. bool allocated = false;
  1159. if (nm_i->fcnt > 2 * MAX_FREE_NIDS)
  1160. return -1;
  1161. /* 0 nid should not be used */
  1162. if (unlikely(nid == 0))
  1163. return 0;
  1164. if (build) {
  1165. /* do not add allocated nids */
  1166. read_lock(&nm_i->nat_tree_lock);
  1167. ne = __lookup_nat_cache(nm_i, nid);
  1168. if (ne && nat_get_blkaddr(ne) != NULL_ADDR)
  1169. allocated = true;
  1170. read_unlock(&nm_i->nat_tree_lock);
  1171. if (allocated)
  1172. return 0;
  1173. }
  1174. i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS);
  1175. i->nid = nid;
  1176. i->state = NID_NEW;
  1177. spin_lock(&nm_i->free_nid_list_lock);
  1178. if (__lookup_free_nid_list(nid, &nm_i->free_nid_list)) {
  1179. spin_unlock(&nm_i->free_nid_list_lock);
  1180. kmem_cache_free(free_nid_slab, i);
  1181. return 0;
  1182. }
  1183. list_add_tail(&i->list, &nm_i->free_nid_list);
  1184. nm_i->fcnt++;
  1185. spin_unlock(&nm_i->free_nid_list_lock);
  1186. return 1;
  1187. }
  1188. static void remove_free_nid(struct f2fs_nm_info *nm_i, nid_t nid)
  1189. {
  1190. struct free_nid *i;
  1191. spin_lock(&nm_i->free_nid_list_lock);
  1192. i = __lookup_free_nid_list(nid, &nm_i->free_nid_list);
  1193. if (i && i->state == NID_NEW) {
  1194. __del_from_free_nid_list(i);
  1195. nm_i->fcnt--;
  1196. }
  1197. spin_unlock(&nm_i->free_nid_list_lock);
  1198. }
  1199. static void scan_nat_page(struct f2fs_nm_info *nm_i,
  1200. struct page *nat_page, nid_t start_nid)
  1201. {
  1202. struct f2fs_nat_block *nat_blk = page_address(nat_page);
  1203. block_t blk_addr;
  1204. int i;
  1205. i = start_nid % NAT_ENTRY_PER_BLOCK;
  1206. for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
  1207. if (unlikely(start_nid >= nm_i->max_nid))
  1208. break;
  1209. blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
  1210. f2fs_bug_on(blk_addr == NEW_ADDR);
  1211. if (blk_addr == NULL_ADDR) {
  1212. if (add_free_nid(nm_i, start_nid, true) < 0)
  1213. break;
  1214. }
  1215. }
  1216. }
  1217. static void build_free_nids(struct f2fs_sb_info *sbi)
  1218. {
  1219. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1220. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
  1221. struct f2fs_summary_block *sum = curseg->sum_blk;
  1222. int i = 0;
  1223. nid_t nid = nm_i->next_scan_nid;
  1224. /* Enough entries */
  1225. if (nm_i->fcnt > NAT_ENTRY_PER_BLOCK)
  1226. return;
  1227. /* readahead nat pages to be scanned */
  1228. ra_nat_pages(sbi, nid);
  1229. while (1) {
  1230. struct page *page = get_current_nat_page(sbi, nid);
  1231. scan_nat_page(nm_i, page, nid);
  1232. f2fs_put_page(page, 1);
  1233. nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK));
  1234. if (unlikely(nid >= nm_i->max_nid))
  1235. nid = 0;
  1236. if (i++ == FREE_NID_PAGES)
  1237. break;
  1238. }
  1239. /* go to the next free nat pages to find free nids abundantly */
  1240. nm_i->next_scan_nid = nid;
  1241. /* find free nids from current sum_pages */
  1242. mutex_lock(&curseg->curseg_mutex);
  1243. for (i = 0; i < nats_in_cursum(sum); i++) {
  1244. block_t addr = le32_to_cpu(nat_in_journal(sum, i).block_addr);
  1245. nid = le32_to_cpu(nid_in_journal(sum, i));
  1246. if (addr == NULL_ADDR)
  1247. add_free_nid(nm_i, nid, true);
  1248. else
  1249. remove_free_nid(nm_i, nid);
  1250. }
  1251. mutex_unlock(&curseg->curseg_mutex);
  1252. }
  1253. /*
  1254. * If this function returns success, caller can obtain a new nid
  1255. * from second parameter of this function.
  1256. * The returned nid could be used ino as well as nid when inode is created.
  1257. */
  1258. bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
  1259. {
  1260. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1261. struct free_nid *i = NULL;
  1262. struct list_head *this;
  1263. retry:
  1264. if (unlikely(sbi->total_valid_node_count + 1 >= nm_i->max_nid))
  1265. return false;
  1266. spin_lock(&nm_i->free_nid_list_lock);
  1267. /* We should not use stale free nids created by build_free_nids */
  1268. if (nm_i->fcnt && !sbi->on_build_free_nids) {
  1269. f2fs_bug_on(list_empty(&nm_i->free_nid_list));
  1270. list_for_each(this, &nm_i->free_nid_list) {
  1271. i = list_entry(this, struct free_nid, list);
  1272. if (i->state == NID_NEW)
  1273. break;
  1274. }
  1275. f2fs_bug_on(i->state != NID_NEW);
  1276. *nid = i->nid;
  1277. i->state = NID_ALLOC;
  1278. nm_i->fcnt--;
  1279. spin_unlock(&nm_i->free_nid_list_lock);
  1280. return true;
  1281. }
  1282. spin_unlock(&nm_i->free_nid_list_lock);
  1283. /* Let's scan nat pages and its caches to get free nids */
  1284. mutex_lock(&nm_i->build_lock);
  1285. sbi->on_build_free_nids = true;
  1286. build_free_nids(sbi);
  1287. sbi->on_build_free_nids = false;
  1288. mutex_unlock(&nm_i->build_lock);
  1289. goto retry;
  1290. }
  1291. /*
  1292. * alloc_nid() should be called prior to this function.
  1293. */
  1294. void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
  1295. {
  1296. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1297. struct free_nid *i;
  1298. spin_lock(&nm_i->free_nid_list_lock);
  1299. i = __lookup_free_nid_list(nid, &nm_i->free_nid_list);
  1300. f2fs_bug_on(!i || i->state != NID_ALLOC);
  1301. __del_from_free_nid_list(i);
  1302. spin_unlock(&nm_i->free_nid_list_lock);
  1303. }
  1304. /*
  1305. * alloc_nid() should be called prior to this function.
  1306. */
  1307. void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
  1308. {
  1309. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1310. struct free_nid *i;
  1311. if (!nid)
  1312. return;
  1313. spin_lock(&nm_i->free_nid_list_lock);
  1314. i = __lookup_free_nid_list(nid, &nm_i->free_nid_list);
  1315. f2fs_bug_on(!i || i->state != NID_ALLOC);
  1316. if (nm_i->fcnt > 2 * MAX_FREE_NIDS) {
  1317. __del_from_free_nid_list(i);
  1318. } else {
  1319. i->state = NID_NEW;
  1320. nm_i->fcnt++;
  1321. }
  1322. spin_unlock(&nm_i->free_nid_list_lock);
  1323. }
  1324. void recover_node_page(struct f2fs_sb_info *sbi, struct page *page,
  1325. struct f2fs_summary *sum, struct node_info *ni,
  1326. block_t new_blkaddr)
  1327. {
  1328. rewrite_node_page(sbi, page, sum, ni->blk_addr, new_blkaddr);
  1329. set_node_addr(sbi, ni, new_blkaddr);
  1330. clear_node_page_dirty(page);
  1331. }
  1332. int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page)
  1333. {
  1334. struct f2fs_inode *src, *dst;
  1335. nid_t ino = ino_of_node(page);
  1336. struct node_info old_ni, new_ni;
  1337. struct page *ipage;
  1338. ipage = grab_cache_page(NODE_MAPPING(sbi), ino);
  1339. if (!ipage)
  1340. return -ENOMEM;
  1341. /* Should not use this inode from free nid list */
  1342. remove_free_nid(NM_I(sbi), ino);
  1343. get_node_info(sbi, ino, &old_ni);
  1344. SetPageUptodate(ipage);
  1345. fill_node_footer(ipage, ino, ino, 0, true);
  1346. src = F2FS_INODE(page);
  1347. dst = F2FS_INODE(ipage);
  1348. memcpy(dst, src, (unsigned long)&src->i_ext - (unsigned long)src);
  1349. dst->i_size = 0;
  1350. dst->i_blocks = cpu_to_le64(1);
  1351. dst->i_links = cpu_to_le32(1);
  1352. dst->i_xattr_nid = 0;
  1353. new_ni = old_ni;
  1354. new_ni.ino = ino;
  1355. if (unlikely(!inc_valid_node_count(sbi, NULL)))
  1356. WARN_ON(1);
  1357. set_node_addr(sbi, &new_ni, NEW_ADDR);
  1358. inc_valid_inode_count(sbi);
  1359. f2fs_put_page(ipage, 1);
  1360. return 0;
  1361. }
  1362. /*
  1363. * ra_sum_pages() merge contiguous pages into one bio and submit.
  1364. * these pre-readed pages are linked in pages list.
  1365. */
  1366. static int ra_sum_pages(struct f2fs_sb_info *sbi, struct list_head *pages,
  1367. int start, int nrpages)
  1368. {
  1369. struct page *page;
  1370. int page_idx = start;
  1371. struct f2fs_io_info fio = {
  1372. .type = META,
  1373. .rw = READ_SYNC | REQ_META | REQ_PRIO
  1374. };
  1375. for (; page_idx < start + nrpages; page_idx++) {
  1376. /* alloc temporal page for read node summary info*/
  1377. page = alloc_page(GFP_F2FS_ZERO);
  1378. if (!page) {
  1379. struct page *tmp;
  1380. list_for_each_entry_safe(page, tmp, pages, lru) {
  1381. list_del(&page->lru);
  1382. unlock_page(page);
  1383. __free_pages(page, 0);
  1384. }
  1385. return -ENOMEM;
  1386. }
  1387. lock_page(page);
  1388. page->index = page_idx;
  1389. list_add_tail(&page->lru, pages);
  1390. }
  1391. list_for_each_entry(page, pages, lru)
  1392. f2fs_submit_page_mbio(sbi, page, page->index, &fio);
  1393. f2fs_submit_merged_bio(sbi, META, READ);
  1394. return 0;
  1395. }
  1396. int restore_node_summary(struct f2fs_sb_info *sbi,
  1397. unsigned int segno, struct f2fs_summary_block *sum)
  1398. {
  1399. struct f2fs_node *rn;
  1400. struct f2fs_summary *sum_entry;
  1401. struct page *page, *tmp;
  1402. block_t addr;
  1403. int bio_blocks = MAX_BIO_BLOCKS(max_hw_blocks(sbi));
  1404. int i, last_offset, nrpages, err = 0;
  1405. LIST_HEAD(page_list);
  1406. /* scan the node segment */
  1407. last_offset = sbi->blocks_per_seg;
  1408. addr = START_BLOCK(sbi, segno);
  1409. sum_entry = &sum->entries[0];
  1410. for (i = 0; i < last_offset; i += nrpages, addr += nrpages) {
  1411. nrpages = min(last_offset - i, bio_blocks);
  1412. /* read ahead node pages */
  1413. err = ra_sum_pages(sbi, &page_list, addr, nrpages);
  1414. if (err)
  1415. return err;
  1416. list_for_each_entry_safe(page, tmp, &page_list, lru) {
  1417. lock_page(page);
  1418. if (unlikely(!PageUptodate(page))) {
  1419. err = -EIO;
  1420. } else {
  1421. rn = F2FS_NODE(page);
  1422. sum_entry->nid = rn->footer.nid;
  1423. sum_entry->version = 0;
  1424. sum_entry->ofs_in_node = 0;
  1425. sum_entry++;
  1426. }
  1427. list_del(&page->lru);
  1428. unlock_page(page);
  1429. __free_pages(page, 0);
  1430. }
  1431. }
  1432. return err;
  1433. }
  1434. static bool flush_nats_in_journal(struct f2fs_sb_info *sbi)
  1435. {
  1436. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1437. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
  1438. struct f2fs_summary_block *sum = curseg->sum_blk;
  1439. int i;
  1440. mutex_lock(&curseg->curseg_mutex);
  1441. if (nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES) {
  1442. mutex_unlock(&curseg->curseg_mutex);
  1443. return false;
  1444. }
  1445. for (i = 0; i < nats_in_cursum(sum); i++) {
  1446. struct nat_entry *ne;
  1447. struct f2fs_nat_entry raw_ne;
  1448. nid_t nid = le32_to_cpu(nid_in_journal(sum, i));
  1449. raw_ne = nat_in_journal(sum, i);
  1450. retry:
  1451. write_lock(&nm_i->nat_tree_lock);
  1452. ne = __lookup_nat_cache(nm_i, nid);
  1453. if (ne) {
  1454. __set_nat_cache_dirty(nm_i, ne);
  1455. write_unlock(&nm_i->nat_tree_lock);
  1456. continue;
  1457. }
  1458. ne = grab_nat_entry(nm_i, nid);
  1459. if (!ne) {
  1460. write_unlock(&nm_i->nat_tree_lock);
  1461. goto retry;
  1462. }
  1463. nat_set_blkaddr(ne, le32_to_cpu(raw_ne.block_addr));
  1464. nat_set_ino(ne, le32_to_cpu(raw_ne.ino));
  1465. nat_set_version(ne, raw_ne.version);
  1466. __set_nat_cache_dirty(nm_i, ne);
  1467. write_unlock(&nm_i->nat_tree_lock);
  1468. }
  1469. update_nats_in_cursum(sum, -i);
  1470. mutex_unlock(&curseg->curseg_mutex);
  1471. return true;
  1472. }
  1473. /*
  1474. * This function is called during the checkpointing process.
  1475. */
  1476. void flush_nat_entries(struct f2fs_sb_info *sbi)
  1477. {
  1478. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1479. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
  1480. struct f2fs_summary_block *sum = curseg->sum_blk;
  1481. struct list_head *cur, *n;
  1482. struct page *page = NULL;
  1483. struct f2fs_nat_block *nat_blk = NULL;
  1484. nid_t start_nid = 0, end_nid = 0;
  1485. bool flushed;
  1486. flushed = flush_nats_in_journal(sbi);
  1487. if (!flushed)
  1488. mutex_lock(&curseg->curseg_mutex);
  1489. /* 1) flush dirty nat caches */
  1490. list_for_each_safe(cur, n, &nm_i->dirty_nat_entries) {
  1491. struct nat_entry *ne;
  1492. nid_t nid;
  1493. struct f2fs_nat_entry raw_ne;
  1494. int offset = -1;
  1495. block_t new_blkaddr;
  1496. ne = list_entry(cur, struct nat_entry, list);
  1497. nid = nat_get_nid(ne);
  1498. if (nat_get_blkaddr(ne) == NEW_ADDR)
  1499. continue;
  1500. if (flushed)
  1501. goto to_nat_page;
  1502. /* if there is room for nat enries in curseg->sumpage */
  1503. offset = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 1);
  1504. if (offset >= 0) {
  1505. raw_ne = nat_in_journal(sum, offset);
  1506. goto flush_now;
  1507. }
  1508. to_nat_page:
  1509. if (!page || (start_nid > nid || nid > end_nid)) {
  1510. if (page) {
  1511. f2fs_put_page(page, 1);
  1512. page = NULL;
  1513. }
  1514. start_nid = START_NID(nid);
  1515. end_nid = start_nid + NAT_ENTRY_PER_BLOCK - 1;
  1516. /*
  1517. * get nat block with dirty flag, increased reference
  1518. * count, mapped and lock
  1519. */
  1520. page = get_next_nat_page(sbi, start_nid);
  1521. nat_blk = page_address(page);
  1522. }
  1523. f2fs_bug_on(!nat_blk);
  1524. raw_ne = nat_blk->entries[nid - start_nid];
  1525. flush_now:
  1526. new_blkaddr = nat_get_blkaddr(ne);
  1527. raw_ne.ino = cpu_to_le32(nat_get_ino(ne));
  1528. raw_ne.block_addr = cpu_to_le32(new_blkaddr);
  1529. raw_ne.version = nat_get_version(ne);
  1530. if (offset < 0) {
  1531. nat_blk->entries[nid - start_nid] = raw_ne;
  1532. } else {
  1533. nat_in_journal(sum, offset) = raw_ne;
  1534. nid_in_journal(sum, offset) = cpu_to_le32(nid);
  1535. }
  1536. if (nat_get_blkaddr(ne) == NULL_ADDR &&
  1537. add_free_nid(NM_I(sbi), nid, false) <= 0) {
  1538. write_lock(&nm_i->nat_tree_lock);
  1539. __del_from_nat_cache(nm_i, ne);
  1540. write_unlock(&nm_i->nat_tree_lock);
  1541. } else {
  1542. write_lock(&nm_i->nat_tree_lock);
  1543. __clear_nat_cache_dirty(nm_i, ne);
  1544. ne->checkpointed = true;
  1545. write_unlock(&nm_i->nat_tree_lock);
  1546. }
  1547. }
  1548. if (!flushed)
  1549. mutex_unlock(&curseg->curseg_mutex);
  1550. f2fs_put_page(page, 1);
  1551. /* 2) shrink nat caches if necessary */
  1552. try_to_free_nats(sbi, nm_i->nat_cnt - NM_WOUT_THRESHOLD);
  1553. }
  1554. static int init_node_manager(struct f2fs_sb_info *sbi)
  1555. {
  1556. struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi);
  1557. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1558. unsigned char *version_bitmap;
  1559. unsigned int nat_segs, nat_blocks;
  1560. nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr);
  1561. /* segment_count_nat includes pair segment so divide to 2. */
  1562. nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1;
  1563. nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg);
  1564. nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nat_blocks;
  1565. nm_i->fcnt = 0;
  1566. nm_i->nat_cnt = 0;
  1567. INIT_LIST_HEAD(&nm_i->free_nid_list);
  1568. INIT_RADIX_TREE(&nm_i->nat_root, GFP_ATOMIC);
  1569. INIT_LIST_HEAD(&nm_i->nat_entries);
  1570. INIT_LIST_HEAD(&nm_i->dirty_nat_entries);
  1571. mutex_init(&nm_i->build_lock);
  1572. spin_lock_init(&nm_i->free_nid_list_lock);
  1573. rwlock_init(&nm_i->nat_tree_lock);
  1574. nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
  1575. nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
  1576. version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
  1577. if (!version_bitmap)
  1578. return -EFAULT;
  1579. nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size,
  1580. GFP_KERNEL);
  1581. if (!nm_i->nat_bitmap)
  1582. return -ENOMEM;
  1583. return 0;
  1584. }
  1585. int build_node_manager(struct f2fs_sb_info *sbi)
  1586. {
  1587. int err;
  1588. sbi->nm_info = kzalloc(sizeof(struct f2fs_nm_info), GFP_KERNEL);
  1589. if (!sbi->nm_info)
  1590. return -ENOMEM;
  1591. err = init_node_manager(sbi);
  1592. if (err)
  1593. return err;
  1594. build_free_nids(sbi);
  1595. return 0;
  1596. }
  1597. void destroy_node_manager(struct f2fs_sb_info *sbi)
  1598. {
  1599. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1600. struct free_nid *i, *next_i;
  1601. struct nat_entry *natvec[NATVEC_SIZE];
  1602. nid_t nid = 0;
  1603. unsigned int found;
  1604. if (!nm_i)
  1605. return;
  1606. /* destroy free nid list */
  1607. spin_lock(&nm_i->free_nid_list_lock);
  1608. list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) {
  1609. f2fs_bug_on(i->state == NID_ALLOC);
  1610. __del_from_free_nid_list(i);
  1611. nm_i->fcnt--;
  1612. }
  1613. f2fs_bug_on(nm_i->fcnt);
  1614. spin_unlock(&nm_i->free_nid_list_lock);
  1615. /* destroy nat cache */
  1616. write_lock(&nm_i->nat_tree_lock);
  1617. while ((found = __gang_lookup_nat_cache(nm_i,
  1618. nid, NATVEC_SIZE, natvec))) {
  1619. unsigned idx;
  1620. for (idx = 0; idx < found; idx++) {
  1621. struct nat_entry *e = natvec[idx];
  1622. nid = nat_get_nid(e) + 1;
  1623. __del_from_nat_cache(nm_i, e);
  1624. }
  1625. }
  1626. f2fs_bug_on(nm_i->nat_cnt);
  1627. write_unlock(&nm_i->nat_tree_lock);
  1628. kfree(nm_i->nat_bitmap);
  1629. sbi->nm_info = NULL;
  1630. kfree(nm_i);
  1631. }
  1632. int __init create_node_manager_caches(void)
  1633. {
  1634. nat_entry_slab = f2fs_kmem_cache_create("nat_entry",
  1635. sizeof(struct nat_entry), NULL);
  1636. if (!nat_entry_slab)
  1637. return -ENOMEM;
  1638. free_nid_slab = f2fs_kmem_cache_create("free_nid",
  1639. sizeof(struct free_nid), NULL);
  1640. if (!free_nid_slab) {
  1641. kmem_cache_destroy(nat_entry_slab);
  1642. return -ENOMEM;
  1643. }
  1644. return 0;
  1645. }
  1646. void destroy_node_manager_caches(void)
  1647. {
  1648. kmem_cache_destroy(free_nid_slab);
  1649. kmem_cache_destroy(nat_entry_slab);
  1650. }