node.c 42 KB

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