node.c 50 KB

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