node.c 42 KB

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