node.c 43 KB

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