node.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830
  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. /* need to do checkpoint during fsync */
  702. F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi));
  703. set_new_dnode(&dn, inode, page, npage, nid);
  704. if (page)
  705. dn.inode_page_locked = 1;
  706. truncate_node(&dn);
  707. return 0;
  708. }
  709. /*
  710. * Caller should grab and release a mutex by calling mutex_lock_op() and
  711. * mutex_unlock_op().
  712. */
  713. int remove_inode_page(struct inode *inode)
  714. {
  715. struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
  716. struct page *page;
  717. nid_t ino = inode->i_ino;
  718. struct dnode_of_data dn;
  719. int err;
  720. page = get_node_page(sbi, ino);
  721. if (IS_ERR(page))
  722. return PTR_ERR(page);
  723. err = truncate_xattr_node(inode, page);
  724. if (err) {
  725. f2fs_put_page(page, 1);
  726. return err;
  727. }
  728. /* 0 is possible, after f2fs_new_inode() is failed */
  729. BUG_ON(inode->i_blocks != 0 && inode->i_blocks != 1);
  730. set_new_dnode(&dn, inode, page, page, ino);
  731. truncate_node(&dn);
  732. return 0;
  733. }
  734. struct page *new_inode_page(struct inode *inode, const struct qstr *name)
  735. {
  736. struct dnode_of_data dn;
  737. /* allocate inode page for new inode */
  738. set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
  739. /* caller should f2fs_put_page(page, 1); */
  740. return new_node_page(&dn, 0, NULL);
  741. }
  742. struct page *new_node_page(struct dnode_of_data *dn,
  743. unsigned int ofs, struct page *ipage)
  744. {
  745. struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
  746. struct address_space *mapping = sbi->node_inode->i_mapping;
  747. struct node_info old_ni, new_ni;
  748. struct page *page;
  749. int err;
  750. if (is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC))
  751. return ERR_PTR(-EPERM);
  752. page = grab_cache_page(mapping, dn->nid);
  753. if (!page)
  754. return ERR_PTR(-ENOMEM);
  755. if (!inc_valid_node_count(sbi, dn->inode, 1)) {
  756. err = -ENOSPC;
  757. goto fail;
  758. }
  759. get_node_info(sbi, dn->nid, &old_ni);
  760. /* Reinitialize old_ni with new node page */
  761. BUG_ON(old_ni.blk_addr != NULL_ADDR);
  762. new_ni = old_ni;
  763. new_ni.ino = dn->inode->i_ino;
  764. set_node_addr(sbi, &new_ni, NEW_ADDR);
  765. fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
  766. set_cold_node(dn->inode, page);
  767. SetPageUptodate(page);
  768. set_page_dirty(page);
  769. if (ofs == XATTR_NODE_OFFSET)
  770. F2FS_I(dn->inode)->i_xattr_nid = dn->nid;
  771. dn->node_page = page;
  772. if (ipage)
  773. update_inode(dn->inode, ipage);
  774. else
  775. sync_inode_page(dn);
  776. if (ofs == 0)
  777. inc_valid_inode_count(sbi);
  778. return page;
  779. fail:
  780. clear_node_page_dirty(page);
  781. f2fs_put_page(page, 1);
  782. return ERR_PTR(err);
  783. }
  784. /*
  785. * Caller should do after getting the following values.
  786. * 0: f2fs_put_page(page, 0)
  787. * LOCKED_PAGE: f2fs_put_page(page, 1)
  788. * error: nothing
  789. */
  790. static int read_node_page(struct page *page, int type)
  791. {
  792. struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
  793. struct node_info ni;
  794. get_node_info(sbi, page->index, &ni);
  795. if (ni.blk_addr == NULL_ADDR) {
  796. f2fs_put_page(page, 1);
  797. return -ENOENT;
  798. }
  799. if (PageUptodate(page))
  800. return LOCKED_PAGE;
  801. return f2fs_readpage(sbi, page, ni.blk_addr, type);
  802. }
  803. /*
  804. * Readahead a node page
  805. */
  806. void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid)
  807. {
  808. struct address_space *mapping = sbi->node_inode->i_mapping;
  809. struct page *apage;
  810. int err;
  811. apage = find_get_page(mapping, 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(mapping, 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 address_space *mapping = sbi->node_inode->i_mapping;
  829. struct page *page;
  830. int err;
  831. repeat:
  832. page = grab_cache_page(mapping, nid);
  833. if (!page)
  834. return ERR_PTR(-ENOMEM);
  835. err = read_node_page(page, READ_SYNC);
  836. if (err < 0)
  837. return ERR_PTR(err);
  838. else if (err == LOCKED_PAGE)
  839. goto got_it;
  840. lock_page(page);
  841. if (!PageUptodate(page)) {
  842. f2fs_put_page(page, 1);
  843. return ERR_PTR(-EIO);
  844. }
  845. if (page->mapping != mapping) {
  846. f2fs_put_page(page, 1);
  847. goto repeat;
  848. }
  849. got_it:
  850. BUG_ON(nid != nid_of_node(page));
  851. mark_page_accessed(page);
  852. return page;
  853. }
  854. /*
  855. * Return a locked page for the desired node page.
  856. * And, readahead MAX_RA_NODE number of node pages.
  857. */
  858. struct page *get_node_page_ra(struct page *parent, int start)
  859. {
  860. struct f2fs_sb_info *sbi = F2FS_SB(parent->mapping->host->i_sb);
  861. struct address_space *mapping = sbi->node_inode->i_mapping;
  862. struct blk_plug plug;
  863. struct page *page;
  864. int err, i, end;
  865. nid_t nid;
  866. /* First, try getting the desired direct node. */
  867. nid = get_nid(parent, start, false);
  868. if (!nid)
  869. return ERR_PTR(-ENOENT);
  870. repeat:
  871. page = grab_cache_page(mapping, nid);
  872. if (!page)
  873. return ERR_PTR(-ENOMEM);
  874. err = read_node_page(page, READ_SYNC);
  875. if (err < 0)
  876. return ERR_PTR(err);
  877. else if (err == LOCKED_PAGE)
  878. goto page_hit;
  879. blk_start_plug(&plug);
  880. /* Then, try readahead for siblings of the desired node */
  881. end = start + MAX_RA_NODE;
  882. end = min(end, NIDS_PER_BLOCK);
  883. for (i = start + 1; i < end; i++) {
  884. nid = get_nid(parent, i, false);
  885. if (!nid)
  886. continue;
  887. ra_node_page(sbi, nid);
  888. }
  889. blk_finish_plug(&plug);
  890. lock_page(page);
  891. if (page->mapping != mapping) {
  892. f2fs_put_page(page, 1);
  893. goto repeat;
  894. }
  895. page_hit:
  896. if (!PageUptodate(page)) {
  897. f2fs_put_page(page, 1);
  898. return ERR_PTR(-EIO);
  899. }
  900. mark_page_accessed(page);
  901. return page;
  902. }
  903. void sync_inode_page(struct dnode_of_data *dn)
  904. {
  905. if (IS_INODE(dn->node_page) || dn->inode_page == dn->node_page) {
  906. update_inode(dn->inode, dn->node_page);
  907. } else if (dn->inode_page) {
  908. if (!dn->inode_page_locked)
  909. lock_page(dn->inode_page);
  910. update_inode(dn->inode, dn->inode_page);
  911. if (!dn->inode_page_locked)
  912. unlock_page(dn->inode_page);
  913. } else {
  914. update_inode_page(dn->inode);
  915. }
  916. }
  917. int sync_node_pages(struct f2fs_sb_info *sbi, nid_t ino,
  918. struct writeback_control *wbc)
  919. {
  920. struct address_space *mapping = sbi->node_inode->i_mapping;
  921. pgoff_t index, end;
  922. struct pagevec pvec;
  923. int step = ino ? 2 : 0;
  924. int nwritten = 0, wrote = 0;
  925. pagevec_init(&pvec, 0);
  926. next_step:
  927. index = 0;
  928. end = LONG_MAX;
  929. while (index <= end) {
  930. int i, nr_pages;
  931. nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
  932. PAGECACHE_TAG_DIRTY,
  933. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
  934. if (nr_pages == 0)
  935. break;
  936. for (i = 0; i < nr_pages; i++) {
  937. struct page *page = pvec.pages[i];
  938. /*
  939. * flushing sequence with step:
  940. * 0. indirect nodes
  941. * 1. dentry dnodes
  942. * 2. file dnodes
  943. */
  944. if (step == 0 && IS_DNODE(page))
  945. continue;
  946. if (step == 1 && (!IS_DNODE(page) ||
  947. is_cold_node(page)))
  948. continue;
  949. if (step == 2 && (!IS_DNODE(page) ||
  950. !is_cold_node(page)))
  951. continue;
  952. /*
  953. * If an fsync mode,
  954. * we should not skip writing node pages.
  955. */
  956. if (ino && ino_of_node(page) == ino)
  957. lock_page(page);
  958. else if (!trylock_page(page))
  959. continue;
  960. if (unlikely(page->mapping != mapping)) {
  961. continue_unlock:
  962. unlock_page(page);
  963. continue;
  964. }
  965. if (ino && ino_of_node(page) != ino)
  966. goto continue_unlock;
  967. if (!PageDirty(page)) {
  968. /* someone wrote it for us */
  969. goto continue_unlock;
  970. }
  971. if (!clear_page_dirty_for_io(page))
  972. goto continue_unlock;
  973. /* called by fsync() */
  974. if (ino && IS_DNODE(page)) {
  975. int mark = !is_checkpointed_node(sbi, ino);
  976. set_fsync_mark(page, 1);
  977. if (IS_INODE(page))
  978. set_dentry_mark(page, mark);
  979. nwritten++;
  980. } else {
  981. set_fsync_mark(page, 0);
  982. set_dentry_mark(page, 0);
  983. }
  984. mapping->a_ops->writepage(page, wbc);
  985. wrote++;
  986. if (--wbc->nr_to_write == 0)
  987. break;
  988. }
  989. pagevec_release(&pvec);
  990. cond_resched();
  991. if (wbc->nr_to_write == 0) {
  992. step = 2;
  993. break;
  994. }
  995. }
  996. if (step < 2) {
  997. step++;
  998. goto next_step;
  999. }
  1000. if (wrote)
  1001. f2fs_submit_bio(sbi, NODE, wbc->sync_mode == WB_SYNC_ALL);
  1002. return nwritten;
  1003. }
  1004. static int f2fs_write_node_page(struct page *page,
  1005. struct writeback_control *wbc)
  1006. {
  1007. struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
  1008. nid_t nid;
  1009. block_t new_addr;
  1010. struct node_info ni;
  1011. wait_on_page_writeback(page);
  1012. /* get old block addr of this node page */
  1013. nid = nid_of_node(page);
  1014. BUG_ON(page->index != nid);
  1015. get_node_info(sbi, nid, &ni);
  1016. /* This page is already truncated */
  1017. if (ni.blk_addr == NULL_ADDR) {
  1018. dec_page_count(sbi, F2FS_DIRTY_NODES);
  1019. unlock_page(page);
  1020. return 0;
  1021. }
  1022. if (wbc->for_reclaim) {
  1023. dec_page_count(sbi, F2FS_DIRTY_NODES);
  1024. wbc->pages_skipped++;
  1025. set_page_dirty(page);
  1026. return AOP_WRITEPAGE_ACTIVATE;
  1027. }
  1028. mutex_lock(&sbi->node_write);
  1029. set_page_writeback(page);
  1030. write_node_page(sbi, page, nid, ni.blk_addr, &new_addr);
  1031. set_node_addr(sbi, &ni, new_addr);
  1032. dec_page_count(sbi, F2FS_DIRTY_NODES);
  1033. mutex_unlock(&sbi->node_write);
  1034. unlock_page(page);
  1035. return 0;
  1036. }
  1037. /*
  1038. * It is very important to gather dirty pages and write at once, so that we can
  1039. * submit a big bio without interfering other data writes.
  1040. * Be default, 512 pages (2MB) * 3 node types, is more reasonable.
  1041. */
  1042. #define COLLECT_DIRTY_NODES 1536
  1043. static int f2fs_write_node_pages(struct address_space *mapping,
  1044. struct writeback_control *wbc)
  1045. {
  1046. struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
  1047. long nr_to_write = wbc->nr_to_write;
  1048. /* First check balancing cached NAT entries */
  1049. if (try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK)) {
  1050. f2fs_sync_fs(sbi->sb, true);
  1051. return 0;
  1052. }
  1053. /* collect a number of dirty node pages and write together */
  1054. if (get_pages(sbi, F2FS_DIRTY_NODES) < COLLECT_DIRTY_NODES)
  1055. return 0;
  1056. /* if mounting is failed, skip writing node pages */
  1057. wbc->nr_to_write = 3 * max_hw_blocks(sbi);
  1058. sync_node_pages(sbi, 0, wbc);
  1059. wbc->nr_to_write = nr_to_write - (3 * max_hw_blocks(sbi) -
  1060. wbc->nr_to_write);
  1061. return 0;
  1062. }
  1063. static int f2fs_set_node_page_dirty(struct page *page)
  1064. {
  1065. struct address_space *mapping = page->mapping;
  1066. struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
  1067. SetPageUptodate(page);
  1068. if (!PageDirty(page)) {
  1069. __set_page_dirty_nobuffers(page);
  1070. inc_page_count(sbi, F2FS_DIRTY_NODES);
  1071. SetPagePrivate(page);
  1072. return 1;
  1073. }
  1074. return 0;
  1075. }
  1076. static void f2fs_invalidate_node_page(struct page *page, unsigned int offset,
  1077. unsigned int length)
  1078. {
  1079. struct inode *inode = page->mapping->host;
  1080. struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
  1081. if (PageDirty(page))
  1082. dec_page_count(sbi, F2FS_DIRTY_NODES);
  1083. ClearPagePrivate(page);
  1084. }
  1085. static int f2fs_release_node_page(struct page *page, gfp_t wait)
  1086. {
  1087. ClearPagePrivate(page);
  1088. return 1;
  1089. }
  1090. /*
  1091. * Structure of the f2fs node operations
  1092. */
  1093. const struct address_space_operations f2fs_node_aops = {
  1094. .writepage = f2fs_write_node_page,
  1095. .writepages = f2fs_write_node_pages,
  1096. .set_page_dirty = f2fs_set_node_page_dirty,
  1097. .invalidatepage = f2fs_invalidate_node_page,
  1098. .releasepage = f2fs_release_node_page,
  1099. };
  1100. static struct free_nid *__lookup_free_nid_list(nid_t n, struct list_head *head)
  1101. {
  1102. struct list_head *this;
  1103. struct free_nid *i;
  1104. list_for_each(this, head) {
  1105. i = list_entry(this, struct free_nid, list);
  1106. if (i->nid == n)
  1107. return i;
  1108. }
  1109. return NULL;
  1110. }
  1111. static void __del_from_free_nid_list(struct free_nid *i)
  1112. {
  1113. list_del(&i->list);
  1114. kmem_cache_free(free_nid_slab, i);
  1115. }
  1116. static int add_free_nid(struct f2fs_nm_info *nm_i, nid_t nid, bool build)
  1117. {
  1118. struct free_nid *i;
  1119. struct nat_entry *ne;
  1120. bool allocated = false;
  1121. if (nm_i->fcnt > 2 * MAX_FREE_NIDS)
  1122. return -1;
  1123. /* 0 nid should not be used */
  1124. if (nid == 0)
  1125. return 0;
  1126. if (!build)
  1127. goto retry;
  1128. /* do not add allocated nids */
  1129. read_lock(&nm_i->nat_tree_lock);
  1130. ne = __lookup_nat_cache(nm_i, nid);
  1131. if (ne && nat_get_blkaddr(ne) != NULL_ADDR)
  1132. allocated = true;
  1133. read_unlock(&nm_i->nat_tree_lock);
  1134. if (allocated)
  1135. return 0;
  1136. retry:
  1137. i = kmem_cache_alloc(free_nid_slab, GFP_NOFS);
  1138. if (!i) {
  1139. cond_resched();
  1140. goto retry;
  1141. }
  1142. i->nid = nid;
  1143. i->state = NID_NEW;
  1144. spin_lock(&nm_i->free_nid_list_lock);
  1145. if (__lookup_free_nid_list(nid, &nm_i->free_nid_list)) {
  1146. spin_unlock(&nm_i->free_nid_list_lock);
  1147. kmem_cache_free(free_nid_slab, i);
  1148. return 0;
  1149. }
  1150. list_add_tail(&i->list, &nm_i->free_nid_list);
  1151. nm_i->fcnt++;
  1152. spin_unlock(&nm_i->free_nid_list_lock);
  1153. return 1;
  1154. }
  1155. static void remove_free_nid(struct f2fs_nm_info *nm_i, nid_t nid)
  1156. {
  1157. struct free_nid *i;
  1158. spin_lock(&nm_i->free_nid_list_lock);
  1159. i = __lookup_free_nid_list(nid, &nm_i->free_nid_list);
  1160. if (i && i->state == NID_NEW) {
  1161. __del_from_free_nid_list(i);
  1162. nm_i->fcnt--;
  1163. }
  1164. spin_unlock(&nm_i->free_nid_list_lock);
  1165. }
  1166. static void scan_nat_page(struct f2fs_nm_info *nm_i,
  1167. struct page *nat_page, nid_t start_nid)
  1168. {
  1169. struct f2fs_nat_block *nat_blk = page_address(nat_page);
  1170. block_t blk_addr;
  1171. int i;
  1172. i = start_nid % NAT_ENTRY_PER_BLOCK;
  1173. for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
  1174. if (start_nid >= nm_i->max_nid)
  1175. break;
  1176. blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
  1177. BUG_ON(blk_addr == NEW_ADDR);
  1178. if (blk_addr == NULL_ADDR) {
  1179. if (add_free_nid(nm_i, start_nid, true) < 0)
  1180. break;
  1181. }
  1182. }
  1183. }
  1184. static void build_free_nids(struct f2fs_sb_info *sbi)
  1185. {
  1186. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1187. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
  1188. struct f2fs_summary_block *sum = curseg->sum_blk;
  1189. int i = 0;
  1190. nid_t nid = nm_i->next_scan_nid;
  1191. /* Enough entries */
  1192. if (nm_i->fcnt > NAT_ENTRY_PER_BLOCK)
  1193. return;
  1194. /* readahead nat pages to be scanned */
  1195. ra_nat_pages(sbi, nid);
  1196. while (1) {
  1197. struct page *page = get_current_nat_page(sbi, nid);
  1198. scan_nat_page(nm_i, page, nid);
  1199. f2fs_put_page(page, 1);
  1200. nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK));
  1201. if (nid >= nm_i->max_nid)
  1202. nid = 0;
  1203. if (i++ == FREE_NID_PAGES)
  1204. break;
  1205. }
  1206. /* go to the next free nat pages to find free nids abundantly */
  1207. nm_i->next_scan_nid = nid;
  1208. /* find free nids from current sum_pages */
  1209. mutex_lock(&curseg->curseg_mutex);
  1210. for (i = 0; i < nats_in_cursum(sum); i++) {
  1211. block_t addr = le32_to_cpu(nat_in_journal(sum, i).block_addr);
  1212. nid = le32_to_cpu(nid_in_journal(sum, i));
  1213. if (addr == NULL_ADDR)
  1214. add_free_nid(nm_i, nid, true);
  1215. else
  1216. remove_free_nid(nm_i, nid);
  1217. }
  1218. mutex_unlock(&curseg->curseg_mutex);
  1219. }
  1220. /*
  1221. * If this function returns success, caller can obtain a new nid
  1222. * from second parameter of this function.
  1223. * The returned nid could be used ino as well as nid when inode is created.
  1224. */
  1225. bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
  1226. {
  1227. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1228. struct free_nid *i = NULL;
  1229. struct list_head *this;
  1230. retry:
  1231. if (sbi->total_valid_node_count + 1 >= nm_i->max_nid)
  1232. return false;
  1233. spin_lock(&nm_i->free_nid_list_lock);
  1234. /* We should not use stale free nids created by build_free_nids */
  1235. if (nm_i->fcnt && !sbi->on_build_free_nids) {
  1236. BUG_ON(list_empty(&nm_i->free_nid_list));
  1237. list_for_each(this, &nm_i->free_nid_list) {
  1238. i = list_entry(this, struct free_nid, list);
  1239. if (i->state == NID_NEW)
  1240. break;
  1241. }
  1242. BUG_ON(i->state != NID_NEW);
  1243. *nid = i->nid;
  1244. i->state = NID_ALLOC;
  1245. nm_i->fcnt--;
  1246. spin_unlock(&nm_i->free_nid_list_lock);
  1247. return true;
  1248. }
  1249. spin_unlock(&nm_i->free_nid_list_lock);
  1250. /* Let's scan nat pages and its caches to get free nids */
  1251. mutex_lock(&nm_i->build_lock);
  1252. sbi->on_build_free_nids = 1;
  1253. build_free_nids(sbi);
  1254. sbi->on_build_free_nids = 0;
  1255. mutex_unlock(&nm_i->build_lock);
  1256. goto retry;
  1257. }
  1258. /*
  1259. * alloc_nid() should be called prior to this function.
  1260. */
  1261. void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
  1262. {
  1263. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1264. struct free_nid *i;
  1265. spin_lock(&nm_i->free_nid_list_lock);
  1266. i = __lookup_free_nid_list(nid, &nm_i->free_nid_list);
  1267. BUG_ON(!i || i->state != NID_ALLOC);
  1268. __del_from_free_nid_list(i);
  1269. spin_unlock(&nm_i->free_nid_list_lock);
  1270. }
  1271. /*
  1272. * alloc_nid() should be called prior to this function.
  1273. */
  1274. void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
  1275. {
  1276. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1277. struct free_nid *i;
  1278. if (!nid)
  1279. return;
  1280. spin_lock(&nm_i->free_nid_list_lock);
  1281. i = __lookup_free_nid_list(nid, &nm_i->free_nid_list);
  1282. BUG_ON(!i || i->state != NID_ALLOC);
  1283. if (nm_i->fcnt > 2 * MAX_FREE_NIDS) {
  1284. __del_from_free_nid_list(i);
  1285. } else {
  1286. i->state = NID_NEW;
  1287. nm_i->fcnt++;
  1288. }
  1289. spin_unlock(&nm_i->free_nid_list_lock);
  1290. }
  1291. void recover_node_page(struct f2fs_sb_info *sbi, struct page *page,
  1292. struct f2fs_summary *sum, struct node_info *ni,
  1293. block_t new_blkaddr)
  1294. {
  1295. rewrite_node_page(sbi, page, sum, ni->blk_addr, new_blkaddr);
  1296. set_node_addr(sbi, ni, new_blkaddr);
  1297. clear_node_page_dirty(page);
  1298. }
  1299. int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page)
  1300. {
  1301. struct address_space *mapping = sbi->node_inode->i_mapping;
  1302. struct f2fs_node *src, *dst;
  1303. nid_t ino = ino_of_node(page);
  1304. struct node_info old_ni, new_ni;
  1305. struct page *ipage;
  1306. ipage = grab_cache_page(mapping, ino);
  1307. if (!ipage)
  1308. return -ENOMEM;
  1309. /* Should not use this inode from free nid list */
  1310. remove_free_nid(NM_I(sbi), ino);
  1311. get_node_info(sbi, ino, &old_ni);
  1312. SetPageUptodate(ipage);
  1313. fill_node_footer(ipage, ino, ino, 0, true);
  1314. src = F2FS_NODE(page);
  1315. dst = F2FS_NODE(ipage);
  1316. memcpy(dst, src, (unsigned long)&src->i.i_ext - (unsigned long)&src->i);
  1317. dst->i.i_size = 0;
  1318. dst->i.i_blocks = cpu_to_le64(1);
  1319. dst->i.i_links = cpu_to_le32(1);
  1320. dst->i.i_xattr_nid = 0;
  1321. new_ni = old_ni;
  1322. new_ni.ino = ino;
  1323. if (!inc_valid_node_count(sbi, NULL, 1))
  1324. WARN_ON(1);
  1325. set_node_addr(sbi, &new_ni, NEW_ADDR);
  1326. inc_valid_inode_count(sbi);
  1327. f2fs_put_page(ipage, 1);
  1328. return 0;
  1329. }
  1330. int restore_node_summary(struct f2fs_sb_info *sbi,
  1331. unsigned int segno, struct f2fs_summary_block *sum)
  1332. {
  1333. struct f2fs_node *rn;
  1334. struct f2fs_summary *sum_entry;
  1335. struct page *page;
  1336. block_t addr;
  1337. int i, last_offset;
  1338. /* alloc temporal page for read node */
  1339. page = alloc_page(GFP_NOFS | __GFP_ZERO);
  1340. if (!page)
  1341. return -ENOMEM;
  1342. lock_page(page);
  1343. /* scan the node segment */
  1344. last_offset = sbi->blocks_per_seg;
  1345. addr = START_BLOCK(sbi, segno);
  1346. sum_entry = &sum->entries[0];
  1347. for (i = 0; i < last_offset; i++, sum_entry++) {
  1348. /*
  1349. * In order to read next node page,
  1350. * we must clear PageUptodate flag.
  1351. */
  1352. ClearPageUptodate(page);
  1353. if (f2fs_readpage(sbi, page, addr, READ_SYNC))
  1354. goto out;
  1355. lock_page(page);
  1356. rn = F2FS_NODE(page);
  1357. sum_entry->nid = rn->footer.nid;
  1358. sum_entry->version = 0;
  1359. sum_entry->ofs_in_node = 0;
  1360. addr++;
  1361. }
  1362. unlock_page(page);
  1363. out:
  1364. __free_pages(page, 0);
  1365. return 0;
  1366. }
  1367. static bool flush_nats_in_journal(struct f2fs_sb_info *sbi)
  1368. {
  1369. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1370. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
  1371. struct f2fs_summary_block *sum = curseg->sum_blk;
  1372. int i;
  1373. mutex_lock(&curseg->curseg_mutex);
  1374. if (nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES) {
  1375. mutex_unlock(&curseg->curseg_mutex);
  1376. return false;
  1377. }
  1378. for (i = 0; i < nats_in_cursum(sum); i++) {
  1379. struct nat_entry *ne;
  1380. struct f2fs_nat_entry raw_ne;
  1381. nid_t nid = le32_to_cpu(nid_in_journal(sum, i));
  1382. raw_ne = nat_in_journal(sum, i);
  1383. retry:
  1384. write_lock(&nm_i->nat_tree_lock);
  1385. ne = __lookup_nat_cache(nm_i, nid);
  1386. if (ne) {
  1387. __set_nat_cache_dirty(nm_i, ne);
  1388. write_unlock(&nm_i->nat_tree_lock);
  1389. continue;
  1390. }
  1391. ne = grab_nat_entry(nm_i, nid);
  1392. if (!ne) {
  1393. write_unlock(&nm_i->nat_tree_lock);
  1394. goto retry;
  1395. }
  1396. nat_set_blkaddr(ne, le32_to_cpu(raw_ne.block_addr));
  1397. nat_set_ino(ne, le32_to_cpu(raw_ne.ino));
  1398. nat_set_version(ne, raw_ne.version);
  1399. __set_nat_cache_dirty(nm_i, ne);
  1400. write_unlock(&nm_i->nat_tree_lock);
  1401. }
  1402. update_nats_in_cursum(sum, -i);
  1403. mutex_unlock(&curseg->curseg_mutex);
  1404. return true;
  1405. }
  1406. /*
  1407. * This function is called during the checkpointing process.
  1408. */
  1409. void flush_nat_entries(struct f2fs_sb_info *sbi)
  1410. {
  1411. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1412. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
  1413. struct f2fs_summary_block *sum = curseg->sum_blk;
  1414. struct list_head *cur, *n;
  1415. struct page *page = NULL;
  1416. struct f2fs_nat_block *nat_blk = NULL;
  1417. nid_t start_nid = 0, end_nid = 0;
  1418. bool flushed;
  1419. flushed = flush_nats_in_journal(sbi);
  1420. if (!flushed)
  1421. mutex_lock(&curseg->curseg_mutex);
  1422. /* 1) flush dirty nat caches */
  1423. list_for_each_safe(cur, n, &nm_i->dirty_nat_entries) {
  1424. struct nat_entry *ne;
  1425. nid_t nid;
  1426. struct f2fs_nat_entry raw_ne;
  1427. int offset = -1;
  1428. block_t new_blkaddr;
  1429. ne = list_entry(cur, struct nat_entry, list);
  1430. nid = nat_get_nid(ne);
  1431. if (nat_get_blkaddr(ne) == NEW_ADDR)
  1432. continue;
  1433. if (flushed)
  1434. goto to_nat_page;
  1435. /* if there is room for nat enries in curseg->sumpage */
  1436. offset = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 1);
  1437. if (offset >= 0) {
  1438. raw_ne = nat_in_journal(sum, offset);
  1439. goto flush_now;
  1440. }
  1441. to_nat_page:
  1442. if (!page || (start_nid > nid || nid > end_nid)) {
  1443. if (page) {
  1444. f2fs_put_page(page, 1);
  1445. page = NULL;
  1446. }
  1447. start_nid = START_NID(nid);
  1448. end_nid = start_nid + NAT_ENTRY_PER_BLOCK - 1;
  1449. /*
  1450. * get nat block with dirty flag, increased reference
  1451. * count, mapped and lock
  1452. */
  1453. page = get_next_nat_page(sbi, start_nid);
  1454. nat_blk = page_address(page);
  1455. }
  1456. BUG_ON(!nat_blk);
  1457. raw_ne = nat_blk->entries[nid - start_nid];
  1458. flush_now:
  1459. new_blkaddr = nat_get_blkaddr(ne);
  1460. raw_ne.ino = cpu_to_le32(nat_get_ino(ne));
  1461. raw_ne.block_addr = cpu_to_le32(new_blkaddr);
  1462. raw_ne.version = nat_get_version(ne);
  1463. if (offset < 0) {
  1464. nat_blk->entries[nid - start_nid] = raw_ne;
  1465. } else {
  1466. nat_in_journal(sum, offset) = raw_ne;
  1467. nid_in_journal(sum, offset) = cpu_to_le32(nid);
  1468. }
  1469. if (nat_get_blkaddr(ne) == NULL_ADDR &&
  1470. add_free_nid(NM_I(sbi), nid, false) <= 0) {
  1471. write_lock(&nm_i->nat_tree_lock);
  1472. __del_from_nat_cache(nm_i, ne);
  1473. write_unlock(&nm_i->nat_tree_lock);
  1474. } else {
  1475. write_lock(&nm_i->nat_tree_lock);
  1476. __clear_nat_cache_dirty(nm_i, ne);
  1477. ne->checkpointed = true;
  1478. write_unlock(&nm_i->nat_tree_lock);
  1479. }
  1480. }
  1481. if (!flushed)
  1482. mutex_unlock(&curseg->curseg_mutex);
  1483. f2fs_put_page(page, 1);
  1484. /* 2) shrink nat caches if necessary */
  1485. try_to_free_nats(sbi, nm_i->nat_cnt - NM_WOUT_THRESHOLD);
  1486. }
  1487. static int init_node_manager(struct f2fs_sb_info *sbi)
  1488. {
  1489. struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi);
  1490. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1491. unsigned char *version_bitmap;
  1492. unsigned int nat_segs, nat_blocks;
  1493. nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr);
  1494. /* segment_count_nat includes pair segment so divide to 2. */
  1495. nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1;
  1496. nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg);
  1497. nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nat_blocks;
  1498. nm_i->fcnt = 0;
  1499. nm_i->nat_cnt = 0;
  1500. INIT_LIST_HEAD(&nm_i->free_nid_list);
  1501. INIT_RADIX_TREE(&nm_i->nat_root, GFP_ATOMIC);
  1502. INIT_LIST_HEAD(&nm_i->nat_entries);
  1503. INIT_LIST_HEAD(&nm_i->dirty_nat_entries);
  1504. mutex_init(&nm_i->build_lock);
  1505. spin_lock_init(&nm_i->free_nid_list_lock);
  1506. rwlock_init(&nm_i->nat_tree_lock);
  1507. nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
  1508. nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
  1509. version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
  1510. if (!version_bitmap)
  1511. return -EFAULT;
  1512. nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size,
  1513. GFP_KERNEL);
  1514. if (!nm_i->nat_bitmap)
  1515. return -ENOMEM;
  1516. return 0;
  1517. }
  1518. int build_node_manager(struct f2fs_sb_info *sbi)
  1519. {
  1520. int err;
  1521. sbi->nm_info = kzalloc(sizeof(struct f2fs_nm_info), GFP_KERNEL);
  1522. if (!sbi->nm_info)
  1523. return -ENOMEM;
  1524. err = init_node_manager(sbi);
  1525. if (err)
  1526. return err;
  1527. build_free_nids(sbi);
  1528. return 0;
  1529. }
  1530. void destroy_node_manager(struct f2fs_sb_info *sbi)
  1531. {
  1532. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1533. struct free_nid *i, *next_i;
  1534. struct nat_entry *natvec[NATVEC_SIZE];
  1535. nid_t nid = 0;
  1536. unsigned int found;
  1537. if (!nm_i)
  1538. return;
  1539. /* destroy free nid list */
  1540. spin_lock(&nm_i->free_nid_list_lock);
  1541. list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) {
  1542. BUG_ON(i->state == NID_ALLOC);
  1543. __del_from_free_nid_list(i);
  1544. nm_i->fcnt--;
  1545. }
  1546. BUG_ON(nm_i->fcnt);
  1547. spin_unlock(&nm_i->free_nid_list_lock);
  1548. /* destroy nat cache */
  1549. write_lock(&nm_i->nat_tree_lock);
  1550. while ((found = __gang_lookup_nat_cache(nm_i,
  1551. nid, NATVEC_SIZE, natvec))) {
  1552. unsigned idx;
  1553. for (idx = 0; idx < found; idx++) {
  1554. struct nat_entry *e = natvec[idx];
  1555. nid = nat_get_nid(e) + 1;
  1556. __del_from_nat_cache(nm_i, e);
  1557. }
  1558. }
  1559. BUG_ON(nm_i->nat_cnt);
  1560. write_unlock(&nm_i->nat_tree_lock);
  1561. kfree(nm_i->nat_bitmap);
  1562. sbi->nm_info = NULL;
  1563. kfree(nm_i);
  1564. }
  1565. int __init create_node_manager_caches(void)
  1566. {
  1567. nat_entry_slab = f2fs_kmem_cache_create("nat_entry",
  1568. sizeof(struct nat_entry), NULL);
  1569. if (!nat_entry_slab)
  1570. return -ENOMEM;
  1571. free_nid_slab = f2fs_kmem_cache_create("free_nid",
  1572. sizeof(struct free_nid), NULL);
  1573. if (!free_nid_slab) {
  1574. kmem_cache_destroy(nat_entry_slab);
  1575. return -ENOMEM;
  1576. }
  1577. return 0;
  1578. }
  1579. void destroy_node_manager_caches(void)
  1580. {
  1581. kmem_cache_destroy(free_nid_slab);
  1582. kmem_cache_destroy(nat_entry_slab);
  1583. }