segment.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260
  1. /*
  2. * fs/f2fs/segment.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/bio.h>
  14. #include <linux/blkdev.h>
  15. #include <linux/prefetch.h>
  16. #include <linux/kthread.h>
  17. #include <linux/vmalloc.h>
  18. #include <linux/swap.h>
  19. #include "f2fs.h"
  20. #include "segment.h"
  21. #include "node.h"
  22. #include <trace/events/f2fs.h>
  23. #define __reverse_ffz(x) __reverse_ffs(~(x))
  24. static struct kmem_cache *discard_entry_slab;
  25. static struct kmem_cache *sit_entry_set_slab;
  26. static struct kmem_cache *inmem_entry_slab;
  27. /*
  28. * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since
  29. * MSB and LSB are reversed in a byte by f2fs_set_bit.
  30. */
  31. static inline unsigned long __reverse_ffs(unsigned long word)
  32. {
  33. int num = 0;
  34. #if BITS_PER_LONG == 64
  35. if ((word & 0xffffffff) == 0) {
  36. num += 32;
  37. word >>= 32;
  38. }
  39. #endif
  40. if ((word & 0xffff) == 0) {
  41. num += 16;
  42. word >>= 16;
  43. }
  44. if ((word & 0xff) == 0) {
  45. num += 8;
  46. word >>= 8;
  47. }
  48. if ((word & 0xf0) == 0)
  49. num += 4;
  50. else
  51. word >>= 4;
  52. if ((word & 0xc) == 0)
  53. num += 2;
  54. else
  55. word >>= 2;
  56. if ((word & 0x2) == 0)
  57. num += 1;
  58. return num;
  59. }
  60. /*
  61. * __find_rev_next(_zero)_bit is copied from lib/find_next_bit.c because
  62. * f2fs_set_bit makes MSB and LSB reversed in a byte.
  63. * Example:
  64. * LSB <--> MSB
  65. * f2fs_set_bit(0, bitmap) => 0000 0001
  66. * f2fs_set_bit(7, bitmap) => 1000 0000
  67. */
  68. static unsigned long __find_rev_next_bit(const unsigned long *addr,
  69. unsigned long size, unsigned long offset)
  70. {
  71. const unsigned long *p = addr + BIT_WORD(offset);
  72. unsigned long result = offset & ~(BITS_PER_LONG - 1);
  73. unsigned long tmp;
  74. unsigned long mask, submask;
  75. unsigned long quot, rest;
  76. if (offset >= size)
  77. return size;
  78. size -= result;
  79. offset %= BITS_PER_LONG;
  80. if (!offset)
  81. goto aligned;
  82. tmp = *(p++);
  83. quot = (offset >> 3) << 3;
  84. rest = offset & 0x7;
  85. mask = ~0UL << quot;
  86. submask = (unsigned char)(0xff << rest) >> rest;
  87. submask <<= quot;
  88. mask &= submask;
  89. tmp &= mask;
  90. if (size < BITS_PER_LONG)
  91. goto found_first;
  92. if (tmp)
  93. goto found_middle;
  94. size -= BITS_PER_LONG;
  95. result += BITS_PER_LONG;
  96. aligned:
  97. while (size & ~(BITS_PER_LONG-1)) {
  98. tmp = *(p++);
  99. if (tmp)
  100. goto found_middle;
  101. result += BITS_PER_LONG;
  102. size -= BITS_PER_LONG;
  103. }
  104. if (!size)
  105. return result;
  106. tmp = *p;
  107. found_first:
  108. tmp &= (~0UL >> (BITS_PER_LONG - size));
  109. if (tmp == 0UL) /* Are any bits set? */
  110. return result + size; /* Nope. */
  111. found_middle:
  112. return result + __reverse_ffs(tmp);
  113. }
  114. static unsigned long __find_rev_next_zero_bit(const unsigned long *addr,
  115. unsigned long size, unsigned long offset)
  116. {
  117. const unsigned long *p = addr + BIT_WORD(offset);
  118. unsigned long result = offset & ~(BITS_PER_LONG - 1);
  119. unsigned long tmp;
  120. unsigned long mask, submask;
  121. unsigned long quot, rest;
  122. if (offset >= size)
  123. return size;
  124. size -= result;
  125. offset %= BITS_PER_LONG;
  126. if (!offset)
  127. goto aligned;
  128. tmp = *(p++);
  129. quot = (offset >> 3) << 3;
  130. rest = offset & 0x7;
  131. mask = ~(~0UL << quot);
  132. submask = (unsigned char)~((unsigned char)(0xff << rest) >> rest);
  133. submask <<= quot;
  134. mask += submask;
  135. tmp |= mask;
  136. if (size < BITS_PER_LONG)
  137. goto found_first;
  138. if (~tmp)
  139. goto found_middle;
  140. size -= BITS_PER_LONG;
  141. result += BITS_PER_LONG;
  142. aligned:
  143. while (size & ~(BITS_PER_LONG - 1)) {
  144. tmp = *(p++);
  145. if (~tmp)
  146. goto found_middle;
  147. result += BITS_PER_LONG;
  148. size -= BITS_PER_LONG;
  149. }
  150. if (!size)
  151. return result;
  152. tmp = *p;
  153. found_first:
  154. tmp |= ~0UL << size;
  155. if (tmp == ~0UL) /* Are any bits zero? */
  156. return result + size; /* Nope. */
  157. found_middle:
  158. return result + __reverse_ffz(tmp);
  159. }
  160. void register_inmem_page(struct inode *inode, struct page *page)
  161. {
  162. struct f2fs_inode_info *fi = F2FS_I(inode);
  163. struct inmem_pages *new;
  164. int err;
  165. SetPagePrivate(page);
  166. new = f2fs_kmem_cache_alloc(inmem_entry_slab, GFP_NOFS);
  167. /* add atomic page indices to the list */
  168. new->page = page;
  169. INIT_LIST_HEAD(&new->list);
  170. retry:
  171. /* increase reference count with clean state */
  172. mutex_lock(&fi->inmem_lock);
  173. err = radix_tree_insert(&fi->inmem_root, page->index, new);
  174. if (err == -EEXIST) {
  175. mutex_unlock(&fi->inmem_lock);
  176. kmem_cache_free(inmem_entry_slab, new);
  177. return;
  178. } else if (err) {
  179. mutex_unlock(&fi->inmem_lock);
  180. goto retry;
  181. }
  182. get_page(page);
  183. list_add_tail(&new->list, &fi->inmem_pages);
  184. inc_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
  185. mutex_unlock(&fi->inmem_lock);
  186. }
  187. void commit_inmem_pages(struct inode *inode, bool abort)
  188. {
  189. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  190. struct f2fs_inode_info *fi = F2FS_I(inode);
  191. struct inmem_pages *cur, *tmp;
  192. bool submit_bio = false;
  193. struct f2fs_io_info fio = {
  194. .type = DATA,
  195. .rw = WRITE_SYNC | REQ_PRIO,
  196. };
  197. /*
  198. * The abort is true only when f2fs_evict_inode is called.
  199. * Basically, the f2fs_evict_inode doesn't produce any data writes, so
  200. * that we don't need to call f2fs_balance_fs.
  201. * Otherwise, f2fs_gc in f2fs_balance_fs can wait forever until this
  202. * inode becomes free by iget_locked in f2fs_iget.
  203. */
  204. if (!abort) {
  205. f2fs_balance_fs(sbi);
  206. f2fs_lock_op(sbi);
  207. }
  208. mutex_lock(&fi->inmem_lock);
  209. list_for_each_entry_safe(cur, tmp, &fi->inmem_pages, list) {
  210. if (!abort) {
  211. lock_page(cur->page);
  212. if (cur->page->mapping == inode->i_mapping) {
  213. f2fs_wait_on_page_writeback(cur->page, DATA);
  214. if (clear_page_dirty_for_io(cur->page))
  215. inode_dec_dirty_pages(inode);
  216. do_write_data_page(cur->page, &fio);
  217. submit_bio = true;
  218. }
  219. f2fs_put_page(cur->page, 1);
  220. } else {
  221. put_page(cur->page);
  222. }
  223. radix_tree_delete(&fi->inmem_root, cur->page->index);
  224. list_del(&cur->list);
  225. kmem_cache_free(inmem_entry_slab, cur);
  226. dec_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
  227. }
  228. mutex_unlock(&fi->inmem_lock);
  229. if (!abort) {
  230. f2fs_unlock_op(sbi);
  231. if (submit_bio)
  232. f2fs_submit_merged_bio(sbi, DATA, WRITE);
  233. }
  234. }
  235. /*
  236. * This function balances dirty node and dentry pages.
  237. * In addition, it controls garbage collection.
  238. */
  239. void f2fs_balance_fs(struct f2fs_sb_info *sbi)
  240. {
  241. /*
  242. * We should do GC or end up with checkpoint, if there are so many dirty
  243. * dir/node pages without enough free segments.
  244. */
  245. if (has_not_enough_free_secs(sbi, 0)) {
  246. mutex_lock(&sbi->gc_mutex);
  247. f2fs_gc(sbi);
  248. }
  249. }
  250. void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi)
  251. {
  252. /* check the # of cached NAT entries and prefree segments */
  253. if (try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK) ||
  254. excess_prefree_segs(sbi) ||
  255. !available_free_memory(sbi, INO_ENTRIES))
  256. f2fs_sync_fs(sbi->sb, true);
  257. }
  258. static int issue_flush_thread(void *data)
  259. {
  260. struct f2fs_sb_info *sbi = data;
  261. struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
  262. wait_queue_head_t *q = &fcc->flush_wait_queue;
  263. repeat:
  264. if (kthread_should_stop())
  265. return 0;
  266. if (!llist_empty(&fcc->issue_list)) {
  267. struct bio *bio = bio_alloc(GFP_NOIO, 0);
  268. struct flush_cmd *cmd, *next;
  269. int ret;
  270. fcc->dispatch_list = llist_del_all(&fcc->issue_list);
  271. fcc->dispatch_list = llist_reverse_order(fcc->dispatch_list);
  272. bio->bi_bdev = sbi->sb->s_bdev;
  273. ret = submit_bio_wait(WRITE_FLUSH, bio);
  274. llist_for_each_entry_safe(cmd, next,
  275. fcc->dispatch_list, llnode) {
  276. cmd->ret = ret;
  277. complete(&cmd->wait);
  278. }
  279. bio_put(bio);
  280. fcc->dispatch_list = NULL;
  281. }
  282. wait_event_interruptible(*q,
  283. kthread_should_stop() || !llist_empty(&fcc->issue_list));
  284. goto repeat;
  285. }
  286. int f2fs_issue_flush(struct f2fs_sb_info *sbi)
  287. {
  288. struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
  289. struct flush_cmd cmd;
  290. trace_f2fs_issue_flush(sbi->sb, test_opt(sbi, NOBARRIER),
  291. test_opt(sbi, FLUSH_MERGE));
  292. if (test_opt(sbi, NOBARRIER))
  293. return 0;
  294. if (!test_opt(sbi, FLUSH_MERGE))
  295. return blkdev_issue_flush(sbi->sb->s_bdev, GFP_KERNEL, NULL);
  296. init_completion(&cmd.wait);
  297. llist_add(&cmd.llnode, &fcc->issue_list);
  298. if (!fcc->dispatch_list)
  299. wake_up(&fcc->flush_wait_queue);
  300. wait_for_completion(&cmd.wait);
  301. return cmd.ret;
  302. }
  303. int create_flush_cmd_control(struct f2fs_sb_info *sbi)
  304. {
  305. dev_t dev = sbi->sb->s_bdev->bd_dev;
  306. struct flush_cmd_control *fcc;
  307. int err = 0;
  308. fcc = kzalloc(sizeof(struct flush_cmd_control), GFP_KERNEL);
  309. if (!fcc)
  310. return -ENOMEM;
  311. init_waitqueue_head(&fcc->flush_wait_queue);
  312. init_llist_head(&fcc->issue_list);
  313. SM_I(sbi)->cmd_control_info = fcc;
  314. fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi,
  315. "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev));
  316. if (IS_ERR(fcc->f2fs_issue_flush)) {
  317. err = PTR_ERR(fcc->f2fs_issue_flush);
  318. kfree(fcc);
  319. SM_I(sbi)->cmd_control_info = NULL;
  320. return err;
  321. }
  322. return err;
  323. }
  324. void destroy_flush_cmd_control(struct f2fs_sb_info *sbi)
  325. {
  326. struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
  327. if (fcc && fcc->f2fs_issue_flush)
  328. kthread_stop(fcc->f2fs_issue_flush);
  329. kfree(fcc);
  330. SM_I(sbi)->cmd_control_info = NULL;
  331. }
  332. static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
  333. enum dirty_type dirty_type)
  334. {
  335. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  336. /* need not be added */
  337. if (IS_CURSEG(sbi, segno))
  338. return;
  339. if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
  340. dirty_i->nr_dirty[dirty_type]++;
  341. if (dirty_type == DIRTY) {
  342. struct seg_entry *sentry = get_seg_entry(sbi, segno);
  343. enum dirty_type t = sentry->type;
  344. if (unlikely(t >= DIRTY)) {
  345. f2fs_bug_on(sbi, 1);
  346. return;
  347. }
  348. if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
  349. dirty_i->nr_dirty[t]++;
  350. }
  351. }
  352. static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
  353. enum dirty_type dirty_type)
  354. {
  355. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  356. if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
  357. dirty_i->nr_dirty[dirty_type]--;
  358. if (dirty_type == DIRTY) {
  359. struct seg_entry *sentry = get_seg_entry(sbi, segno);
  360. enum dirty_type t = sentry->type;
  361. if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
  362. dirty_i->nr_dirty[t]--;
  363. if (get_valid_blocks(sbi, segno, sbi->segs_per_sec) == 0)
  364. clear_bit(GET_SECNO(sbi, segno),
  365. dirty_i->victim_secmap);
  366. }
  367. }
  368. /*
  369. * Should not occur error such as -ENOMEM.
  370. * Adding dirty entry into seglist is not critical operation.
  371. * If a given segment is one of current working segments, it won't be added.
  372. */
  373. static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
  374. {
  375. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  376. unsigned short valid_blocks;
  377. if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
  378. return;
  379. mutex_lock(&dirty_i->seglist_lock);
  380. valid_blocks = get_valid_blocks(sbi, segno, 0);
  381. if (valid_blocks == 0) {
  382. __locate_dirty_segment(sbi, segno, PRE);
  383. __remove_dirty_segment(sbi, segno, DIRTY);
  384. } else if (valid_blocks < sbi->blocks_per_seg) {
  385. __locate_dirty_segment(sbi, segno, DIRTY);
  386. } else {
  387. /* Recovery routine with SSR needs this */
  388. __remove_dirty_segment(sbi, segno, DIRTY);
  389. }
  390. mutex_unlock(&dirty_i->seglist_lock);
  391. }
  392. static int f2fs_issue_discard(struct f2fs_sb_info *sbi,
  393. block_t blkstart, block_t blklen)
  394. {
  395. sector_t start = SECTOR_FROM_BLOCK(blkstart);
  396. sector_t len = SECTOR_FROM_BLOCK(blklen);
  397. trace_f2fs_issue_discard(sbi->sb, blkstart, blklen);
  398. return blkdev_issue_discard(sbi->sb->s_bdev, start, len, GFP_NOFS, 0);
  399. }
  400. void discard_next_dnode(struct f2fs_sb_info *sbi, block_t blkaddr)
  401. {
  402. if (f2fs_issue_discard(sbi, blkaddr, 1)) {
  403. struct page *page = grab_meta_page(sbi, blkaddr);
  404. /* zero-filled page */
  405. set_page_dirty(page);
  406. f2fs_put_page(page, 1);
  407. }
  408. }
  409. static void __add_discard_entry(struct f2fs_sb_info *sbi,
  410. struct cp_control *cpc, unsigned int start, unsigned int end)
  411. {
  412. struct list_head *head = &SM_I(sbi)->discard_list;
  413. struct discard_entry *new, *last;
  414. if (!list_empty(head)) {
  415. last = list_last_entry(head, struct discard_entry, list);
  416. if (START_BLOCK(sbi, cpc->trim_start) + start ==
  417. last->blkaddr + last->len) {
  418. last->len += end - start;
  419. goto done;
  420. }
  421. }
  422. new = f2fs_kmem_cache_alloc(discard_entry_slab, GFP_NOFS);
  423. INIT_LIST_HEAD(&new->list);
  424. new->blkaddr = START_BLOCK(sbi, cpc->trim_start) + start;
  425. new->len = end - start;
  426. list_add_tail(&new->list, head);
  427. done:
  428. SM_I(sbi)->nr_discards += end - start;
  429. cpc->trimmed += end - start;
  430. }
  431. static void add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc)
  432. {
  433. int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
  434. int max_blocks = sbi->blocks_per_seg;
  435. struct seg_entry *se = get_seg_entry(sbi, cpc->trim_start);
  436. unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
  437. unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
  438. unsigned long dmap[entries];
  439. unsigned int start = 0, end = -1;
  440. bool force = (cpc->reason == CP_DISCARD);
  441. int i;
  442. if (!force && (!test_opt(sbi, DISCARD) ||
  443. SM_I(sbi)->nr_discards >= SM_I(sbi)->max_discards))
  444. return;
  445. if (force && !se->valid_blocks) {
  446. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  447. /*
  448. * if this segment is registered in the prefree list, then
  449. * we should skip adding a discard candidate, and let the
  450. * checkpoint do that later.
  451. */
  452. mutex_lock(&dirty_i->seglist_lock);
  453. if (test_bit(cpc->trim_start, dirty_i->dirty_segmap[PRE])) {
  454. mutex_unlock(&dirty_i->seglist_lock);
  455. cpc->trimmed += sbi->blocks_per_seg;
  456. return;
  457. }
  458. mutex_unlock(&dirty_i->seglist_lock);
  459. __add_discard_entry(sbi, cpc, 0, sbi->blocks_per_seg);
  460. return;
  461. }
  462. /* zero block will be discarded through the prefree list */
  463. if (!se->valid_blocks || se->valid_blocks == max_blocks)
  464. return;
  465. /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
  466. for (i = 0; i < entries; i++)
  467. dmap[i] = force ? ~ckpt_map[i] :
  468. (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i];
  469. while (force || SM_I(sbi)->nr_discards <= SM_I(sbi)->max_discards) {
  470. start = __find_rev_next_bit(dmap, max_blocks, end + 1);
  471. if (start >= max_blocks)
  472. break;
  473. end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
  474. if (end - start < cpc->trim_minlen)
  475. continue;
  476. __add_discard_entry(sbi, cpc, start, end);
  477. }
  478. }
  479. void release_discard_addrs(struct f2fs_sb_info *sbi)
  480. {
  481. struct list_head *head = &(SM_I(sbi)->discard_list);
  482. struct discard_entry *entry, *this;
  483. /* drop caches */
  484. list_for_each_entry_safe(entry, this, head, list) {
  485. list_del(&entry->list);
  486. kmem_cache_free(discard_entry_slab, entry);
  487. }
  488. }
  489. /*
  490. * Should call clear_prefree_segments after checkpoint is done.
  491. */
  492. static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
  493. {
  494. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  495. unsigned int segno;
  496. mutex_lock(&dirty_i->seglist_lock);
  497. for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], MAIN_SEGS(sbi))
  498. __set_test_and_free(sbi, segno);
  499. mutex_unlock(&dirty_i->seglist_lock);
  500. }
  501. void clear_prefree_segments(struct f2fs_sb_info *sbi)
  502. {
  503. struct list_head *head = &(SM_I(sbi)->discard_list);
  504. struct discard_entry *entry, *this;
  505. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  506. unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
  507. unsigned int start = 0, end = -1;
  508. mutex_lock(&dirty_i->seglist_lock);
  509. while (1) {
  510. int i;
  511. start = find_next_bit(prefree_map, MAIN_SEGS(sbi), end + 1);
  512. if (start >= MAIN_SEGS(sbi))
  513. break;
  514. end = find_next_zero_bit(prefree_map, MAIN_SEGS(sbi),
  515. start + 1);
  516. for (i = start; i < end; i++)
  517. clear_bit(i, prefree_map);
  518. dirty_i->nr_dirty[PRE] -= end - start;
  519. if (!test_opt(sbi, DISCARD))
  520. continue;
  521. f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
  522. (end - start) << sbi->log_blocks_per_seg);
  523. }
  524. mutex_unlock(&dirty_i->seglist_lock);
  525. /* send small discards */
  526. list_for_each_entry_safe(entry, this, head, list) {
  527. f2fs_issue_discard(sbi, entry->blkaddr, entry->len);
  528. list_del(&entry->list);
  529. SM_I(sbi)->nr_discards -= entry->len;
  530. kmem_cache_free(discard_entry_slab, entry);
  531. }
  532. }
  533. static bool __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
  534. {
  535. struct sit_info *sit_i = SIT_I(sbi);
  536. if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap)) {
  537. sit_i->dirty_sentries++;
  538. return false;
  539. }
  540. return true;
  541. }
  542. static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
  543. unsigned int segno, int modified)
  544. {
  545. struct seg_entry *se = get_seg_entry(sbi, segno);
  546. se->type = type;
  547. if (modified)
  548. __mark_sit_entry_dirty(sbi, segno);
  549. }
  550. static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
  551. {
  552. struct seg_entry *se;
  553. unsigned int segno, offset;
  554. long int new_vblocks;
  555. segno = GET_SEGNO(sbi, blkaddr);
  556. se = get_seg_entry(sbi, segno);
  557. new_vblocks = se->valid_blocks + del;
  558. offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
  559. f2fs_bug_on(sbi, (new_vblocks >> (sizeof(unsigned short) << 3) ||
  560. (new_vblocks > sbi->blocks_per_seg)));
  561. se->valid_blocks = new_vblocks;
  562. se->mtime = get_mtime(sbi);
  563. SIT_I(sbi)->max_mtime = se->mtime;
  564. /* Update valid block bitmap */
  565. if (del > 0) {
  566. if (f2fs_test_and_set_bit(offset, se->cur_valid_map))
  567. f2fs_bug_on(sbi, 1);
  568. } else {
  569. if (!f2fs_test_and_clear_bit(offset, se->cur_valid_map))
  570. f2fs_bug_on(sbi, 1);
  571. }
  572. if (!f2fs_test_bit(offset, se->ckpt_valid_map))
  573. se->ckpt_valid_blocks += del;
  574. __mark_sit_entry_dirty(sbi, segno);
  575. /* update total number of valid blocks to be written in ckpt area */
  576. SIT_I(sbi)->written_valid_blocks += del;
  577. if (sbi->segs_per_sec > 1)
  578. get_sec_entry(sbi, segno)->valid_blocks += del;
  579. }
  580. void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new)
  581. {
  582. update_sit_entry(sbi, new, 1);
  583. if (GET_SEGNO(sbi, old) != NULL_SEGNO)
  584. update_sit_entry(sbi, old, -1);
  585. locate_dirty_segment(sbi, GET_SEGNO(sbi, old));
  586. locate_dirty_segment(sbi, GET_SEGNO(sbi, new));
  587. }
  588. void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
  589. {
  590. unsigned int segno = GET_SEGNO(sbi, addr);
  591. struct sit_info *sit_i = SIT_I(sbi);
  592. f2fs_bug_on(sbi, addr == NULL_ADDR);
  593. if (addr == NEW_ADDR)
  594. return;
  595. /* add it into sit main buffer */
  596. mutex_lock(&sit_i->sentry_lock);
  597. update_sit_entry(sbi, addr, -1);
  598. /* add it into dirty seglist */
  599. locate_dirty_segment(sbi, segno);
  600. mutex_unlock(&sit_i->sentry_lock);
  601. }
  602. /*
  603. * This function should be resided under the curseg_mutex lock
  604. */
  605. static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
  606. struct f2fs_summary *sum)
  607. {
  608. struct curseg_info *curseg = CURSEG_I(sbi, type);
  609. void *addr = curseg->sum_blk;
  610. addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
  611. memcpy(addr, sum, sizeof(struct f2fs_summary));
  612. }
  613. /*
  614. * Calculate the number of current summary pages for writing
  615. */
  616. int npages_for_summary_flush(struct f2fs_sb_info *sbi)
  617. {
  618. int valid_sum_count = 0;
  619. int i, sum_in_page;
  620. for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
  621. if (sbi->ckpt->alloc_type[i] == SSR)
  622. valid_sum_count += sbi->blocks_per_seg;
  623. else
  624. valid_sum_count += curseg_blkoff(sbi, i);
  625. }
  626. sum_in_page = (PAGE_CACHE_SIZE - 2 * SUM_JOURNAL_SIZE -
  627. SUM_FOOTER_SIZE) / SUMMARY_SIZE;
  628. if (valid_sum_count <= sum_in_page)
  629. return 1;
  630. else if ((valid_sum_count - sum_in_page) <=
  631. (PAGE_CACHE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
  632. return 2;
  633. return 3;
  634. }
  635. /*
  636. * Caller should put this summary page
  637. */
  638. struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
  639. {
  640. return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
  641. }
  642. static void write_sum_page(struct f2fs_sb_info *sbi,
  643. struct f2fs_summary_block *sum_blk, block_t blk_addr)
  644. {
  645. struct page *page = grab_meta_page(sbi, blk_addr);
  646. void *kaddr = page_address(page);
  647. memcpy(kaddr, sum_blk, PAGE_CACHE_SIZE);
  648. set_page_dirty(page);
  649. f2fs_put_page(page, 1);
  650. }
  651. static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
  652. {
  653. struct curseg_info *curseg = CURSEG_I(sbi, type);
  654. unsigned int segno = curseg->segno + 1;
  655. struct free_segmap_info *free_i = FREE_I(sbi);
  656. if (segno < MAIN_SEGS(sbi) && segno % sbi->segs_per_sec)
  657. return !test_bit(segno, free_i->free_segmap);
  658. return 0;
  659. }
  660. /*
  661. * Find a new segment from the free segments bitmap to right order
  662. * This function should be returned with success, otherwise BUG
  663. */
  664. static void get_new_segment(struct f2fs_sb_info *sbi,
  665. unsigned int *newseg, bool new_sec, int dir)
  666. {
  667. struct free_segmap_info *free_i = FREE_I(sbi);
  668. unsigned int segno, secno, zoneno;
  669. unsigned int total_zones = MAIN_SECS(sbi) / sbi->secs_per_zone;
  670. unsigned int hint = *newseg / sbi->segs_per_sec;
  671. unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
  672. unsigned int left_start = hint;
  673. bool init = true;
  674. int go_left = 0;
  675. int i;
  676. write_lock(&free_i->segmap_lock);
  677. if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
  678. segno = find_next_zero_bit(free_i->free_segmap,
  679. MAIN_SEGS(sbi), *newseg + 1);
  680. if (segno - *newseg < sbi->segs_per_sec -
  681. (*newseg % sbi->segs_per_sec))
  682. goto got_it;
  683. }
  684. find_other_zone:
  685. secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint);
  686. if (secno >= MAIN_SECS(sbi)) {
  687. if (dir == ALLOC_RIGHT) {
  688. secno = find_next_zero_bit(free_i->free_secmap,
  689. MAIN_SECS(sbi), 0);
  690. f2fs_bug_on(sbi, secno >= MAIN_SECS(sbi));
  691. } else {
  692. go_left = 1;
  693. left_start = hint - 1;
  694. }
  695. }
  696. if (go_left == 0)
  697. goto skip_left;
  698. while (test_bit(left_start, free_i->free_secmap)) {
  699. if (left_start > 0) {
  700. left_start--;
  701. continue;
  702. }
  703. left_start = find_next_zero_bit(free_i->free_secmap,
  704. MAIN_SECS(sbi), 0);
  705. f2fs_bug_on(sbi, left_start >= MAIN_SECS(sbi));
  706. break;
  707. }
  708. secno = left_start;
  709. skip_left:
  710. hint = secno;
  711. segno = secno * sbi->segs_per_sec;
  712. zoneno = secno / sbi->secs_per_zone;
  713. /* give up on finding another zone */
  714. if (!init)
  715. goto got_it;
  716. if (sbi->secs_per_zone == 1)
  717. goto got_it;
  718. if (zoneno == old_zoneno)
  719. goto got_it;
  720. if (dir == ALLOC_LEFT) {
  721. if (!go_left && zoneno + 1 >= total_zones)
  722. goto got_it;
  723. if (go_left && zoneno == 0)
  724. goto got_it;
  725. }
  726. for (i = 0; i < NR_CURSEG_TYPE; i++)
  727. if (CURSEG_I(sbi, i)->zone == zoneno)
  728. break;
  729. if (i < NR_CURSEG_TYPE) {
  730. /* zone is in user, try another */
  731. if (go_left)
  732. hint = zoneno * sbi->secs_per_zone - 1;
  733. else if (zoneno + 1 >= total_zones)
  734. hint = 0;
  735. else
  736. hint = (zoneno + 1) * sbi->secs_per_zone;
  737. init = false;
  738. goto find_other_zone;
  739. }
  740. got_it:
  741. /* set it as dirty segment in free segmap */
  742. f2fs_bug_on(sbi, test_bit(segno, free_i->free_segmap));
  743. __set_inuse(sbi, segno);
  744. *newseg = segno;
  745. write_unlock(&free_i->segmap_lock);
  746. }
  747. static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
  748. {
  749. struct curseg_info *curseg = CURSEG_I(sbi, type);
  750. struct summary_footer *sum_footer;
  751. curseg->segno = curseg->next_segno;
  752. curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
  753. curseg->next_blkoff = 0;
  754. curseg->next_segno = NULL_SEGNO;
  755. sum_footer = &(curseg->sum_blk->footer);
  756. memset(sum_footer, 0, sizeof(struct summary_footer));
  757. if (IS_DATASEG(type))
  758. SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
  759. if (IS_NODESEG(type))
  760. SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
  761. __set_sit_entry_type(sbi, type, curseg->segno, modified);
  762. }
  763. /*
  764. * Allocate a current working segment.
  765. * This function always allocates a free segment in LFS manner.
  766. */
  767. static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
  768. {
  769. struct curseg_info *curseg = CURSEG_I(sbi, type);
  770. unsigned int segno = curseg->segno;
  771. int dir = ALLOC_LEFT;
  772. write_sum_page(sbi, curseg->sum_blk,
  773. GET_SUM_BLOCK(sbi, segno));
  774. if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
  775. dir = ALLOC_RIGHT;
  776. if (test_opt(sbi, NOHEAP))
  777. dir = ALLOC_RIGHT;
  778. get_new_segment(sbi, &segno, new_sec, dir);
  779. curseg->next_segno = segno;
  780. reset_curseg(sbi, type, 1);
  781. curseg->alloc_type = LFS;
  782. }
  783. static void __next_free_blkoff(struct f2fs_sb_info *sbi,
  784. struct curseg_info *seg, block_t start)
  785. {
  786. struct seg_entry *se = get_seg_entry(sbi, seg->segno);
  787. int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
  788. unsigned long target_map[entries];
  789. unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
  790. unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
  791. int i, pos;
  792. for (i = 0; i < entries; i++)
  793. target_map[i] = ckpt_map[i] | cur_map[i];
  794. pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
  795. seg->next_blkoff = pos;
  796. }
  797. /*
  798. * If a segment is written by LFS manner, next block offset is just obtained
  799. * by increasing the current block offset. However, if a segment is written by
  800. * SSR manner, next block offset obtained by calling __next_free_blkoff
  801. */
  802. static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
  803. struct curseg_info *seg)
  804. {
  805. if (seg->alloc_type == SSR)
  806. __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
  807. else
  808. seg->next_blkoff++;
  809. }
  810. /*
  811. * This function always allocates a used segment(from dirty seglist) by SSR
  812. * manner, so it should recover the existing segment information of valid blocks
  813. */
  814. static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
  815. {
  816. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  817. struct curseg_info *curseg = CURSEG_I(sbi, type);
  818. unsigned int new_segno = curseg->next_segno;
  819. struct f2fs_summary_block *sum_node;
  820. struct page *sum_page;
  821. write_sum_page(sbi, curseg->sum_blk,
  822. GET_SUM_BLOCK(sbi, curseg->segno));
  823. __set_test_and_inuse(sbi, new_segno);
  824. mutex_lock(&dirty_i->seglist_lock);
  825. __remove_dirty_segment(sbi, new_segno, PRE);
  826. __remove_dirty_segment(sbi, new_segno, DIRTY);
  827. mutex_unlock(&dirty_i->seglist_lock);
  828. reset_curseg(sbi, type, 1);
  829. curseg->alloc_type = SSR;
  830. __next_free_blkoff(sbi, curseg, 0);
  831. if (reuse) {
  832. sum_page = get_sum_page(sbi, new_segno);
  833. sum_node = (struct f2fs_summary_block *)page_address(sum_page);
  834. memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
  835. f2fs_put_page(sum_page, 1);
  836. }
  837. }
  838. static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
  839. {
  840. struct curseg_info *curseg = CURSEG_I(sbi, type);
  841. const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
  842. if (IS_NODESEG(type) || !has_not_enough_free_secs(sbi, 0))
  843. return v_ops->get_victim(sbi,
  844. &(curseg)->next_segno, BG_GC, type, SSR);
  845. /* For data segments, let's do SSR more intensively */
  846. for (; type >= CURSEG_HOT_DATA; type--)
  847. if (v_ops->get_victim(sbi, &(curseg)->next_segno,
  848. BG_GC, type, SSR))
  849. return 1;
  850. return 0;
  851. }
  852. /*
  853. * flush out current segment and replace it with new segment
  854. * This function should be returned with success, otherwise BUG
  855. */
  856. static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
  857. int type, bool force)
  858. {
  859. struct curseg_info *curseg = CURSEG_I(sbi, type);
  860. if (force)
  861. new_curseg(sbi, type, true);
  862. else if (type == CURSEG_WARM_NODE)
  863. new_curseg(sbi, type, false);
  864. else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
  865. new_curseg(sbi, type, false);
  866. else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
  867. change_curseg(sbi, type, true);
  868. else
  869. new_curseg(sbi, type, false);
  870. stat_inc_seg_type(sbi, curseg);
  871. }
  872. void allocate_new_segments(struct f2fs_sb_info *sbi)
  873. {
  874. struct curseg_info *curseg;
  875. unsigned int old_curseg;
  876. int i;
  877. for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
  878. curseg = CURSEG_I(sbi, i);
  879. old_curseg = curseg->segno;
  880. SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true);
  881. locate_dirty_segment(sbi, old_curseg);
  882. }
  883. }
  884. static const struct segment_allocation default_salloc_ops = {
  885. .allocate_segment = allocate_segment_by_default,
  886. };
  887. int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range)
  888. {
  889. __u64 start = range->start >> sbi->log_blocksize;
  890. __u64 end = start + (range->len >> sbi->log_blocksize) - 1;
  891. unsigned int start_segno, end_segno;
  892. struct cp_control cpc;
  893. if (range->minlen > SEGMENT_SIZE(sbi) || start >= MAX_BLKADDR(sbi) ||
  894. range->len < sbi->blocksize)
  895. return -EINVAL;
  896. cpc.trimmed = 0;
  897. if (end <= MAIN_BLKADDR(sbi))
  898. goto out;
  899. /* start/end segment number in main_area */
  900. start_segno = (start <= MAIN_BLKADDR(sbi)) ? 0 : GET_SEGNO(sbi, start);
  901. end_segno = (end >= MAX_BLKADDR(sbi)) ? MAIN_SEGS(sbi) - 1 :
  902. GET_SEGNO(sbi, end);
  903. cpc.reason = CP_DISCARD;
  904. cpc.trim_start = start_segno;
  905. cpc.trim_end = end_segno;
  906. cpc.trim_minlen = range->minlen >> sbi->log_blocksize;
  907. /* do checkpoint to issue discard commands safely */
  908. mutex_lock(&sbi->gc_mutex);
  909. write_checkpoint(sbi, &cpc);
  910. mutex_unlock(&sbi->gc_mutex);
  911. out:
  912. range->len = cpc.trimmed << sbi->log_blocksize;
  913. return 0;
  914. }
  915. static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
  916. {
  917. struct curseg_info *curseg = CURSEG_I(sbi, type);
  918. if (curseg->next_blkoff < sbi->blocks_per_seg)
  919. return true;
  920. return false;
  921. }
  922. static int __get_segment_type_2(struct page *page, enum page_type p_type)
  923. {
  924. if (p_type == DATA)
  925. return CURSEG_HOT_DATA;
  926. else
  927. return CURSEG_HOT_NODE;
  928. }
  929. static int __get_segment_type_4(struct page *page, enum page_type p_type)
  930. {
  931. if (p_type == DATA) {
  932. struct inode *inode = page->mapping->host;
  933. if (S_ISDIR(inode->i_mode))
  934. return CURSEG_HOT_DATA;
  935. else
  936. return CURSEG_COLD_DATA;
  937. } else {
  938. if (IS_DNODE(page) && is_cold_node(page))
  939. return CURSEG_WARM_NODE;
  940. else
  941. return CURSEG_COLD_NODE;
  942. }
  943. }
  944. static int __get_segment_type_6(struct page *page, enum page_type p_type)
  945. {
  946. if (p_type == DATA) {
  947. struct inode *inode = page->mapping->host;
  948. if (S_ISDIR(inode->i_mode))
  949. return CURSEG_HOT_DATA;
  950. else if (is_cold_data(page) || file_is_cold(inode))
  951. return CURSEG_COLD_DATA;
  952. else
  953. return CURSEG_WARM_DATA;
  954. } else {
  955. if (IS_DNODE(page))
  956. return is_cold_node(page) ? CURSEG_WARM_NODE :
  957. CURSEG_HOT_NODE;
  958. else
  959. return CURSEG_COLD_NODE;
  960. }
  961. }
  962. static int __get_segment_type(struct page *page, enum page_type p_type)
  963. {
  964. switch (F2FS_P_SB(page)->active_logs) {
  965. case 2:
  966. return __get_segment_type_2(page, p_type);
  967. case 4:
  968. return __get_segment_type_4(page, p_type);
  969. }
  970. /* NR_CURSEG_TYPE(6) logs by default */
  971. f2fs_bug_on(F2FS_P_SB(page),
  972. F2FS_P_SB(page)->active_logs != NR_CURSEG_TYPE);
  973. return __get_segment_type_6(page, p_type);
  974. }
  975. void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
  976. block_t old_blkaddr, block_t *new_blkaddr,
  977. struct f2fs_summary *sum, int type)
  978. {
  979. struct sit_info *sit_i = SIT_I(sbi);
  980. struct curseg_info *curseg;
  981. curseg = CURSEG_I(sbi, type);
  982. mutex_lock(&curseg->curseg_mutex);
  983. *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
  984. /*
  985. * __add_sum_entry should be resided under the curseg_mutex
  986. * because, this function updates a summary entry in the
  987. * current summary block.
  988. */
  989. __add_sum_entry(sbi, type, sum);
  990. mutex_lock(&sit_i->sentry_lock);
  991. __refresh_next_blkoff(sbi, curseg);
  992. stat_inc_block_count(sbi, curseg);
  993. if (!__has_curseg_space(sbi, type))
  994. sit_i->s_ops->allocate_segment(sbi, type, false);
  995. /*
  996. * SIT information should be updated before segment allocation,
  997. * since SSR needs latest valid block information.
  998. */
  999. refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
  1000. mutex_unlock(&sit_i->sentry_lock);
  1001. if (page && IS_NODESEG(type))
  1002. fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
  1003. mutex_unlock(&curseg->curseg_mutex);
  1004. }
  1005. static void do_write_page(struct f2fs_sb_info *sbi, struct page *page,
  1006. block_t old_blkaddr, block_t *new_blkaddr,
  1007. struct f2fs_summary *sum, struct f2fs_io_info *fio)
  1008. {
  1009. int type = __get_segment_type(page, fio->type);
  1010. allocate_data_block(sbi, page, old_blkaddr, new_blkaddr, sum, type);
  1011. /* writeout dirty page into bdev */
  1012. f2fs_submit_page_mbio(sbi, page, *new_blkaddr, fio);
  1013. }
  1014. void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
  1015. {
  1016. struct f2fs_io_info fio = {
  1017. .type = META,
  1018. .rw = WRITE_SYNC | REQ_META | REQ_PRIO
  1019. };
  1020. set_page_writeback(page);
  1021. f2fs_submit_page_mbio(sbi, page, page->index, &fio);
  1022. }
  1023. void write_node_page(struct f2fs_sb_info *sbi, struct page *page,
  1024. struct f2fs_io_info *fio,
  1025. unsigned int nid, block_t old_blkaddr, block_t *new_blkaddr)
  1026. {
  1027. struct f2fs_summary sum;
  1028. set_summary(&sum, nid, 0, 0);
  1029. do_write_page(sbi, page, old_blkaddr, new_blkaddr, &sum, fio);
  1030. }
  1031. void write_data_page(struct page *page, struct dnode_of_data *dn,
  1032. block_t *new_blkaddr, struct f2fs_io_info *fio)
  1033. {
  1034. struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
  1035. struct f2fs_summary sum;
  1036. struct node_info ni;
  1037. f2fs_bug_on(sbi, dn->data_blkaddr == NULL_ADDR);
  1038. get_node_info(sbi, dn->nid, &ni);
  1039. set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
  1040. do_write_page(sbi, page, dn->data_blkaddr, new_blkaddr, &sum, fio);
  1041. }
  1042. void rewrite_data_page(struct page *page, block_t old_blkaddr,
  1043. struct f2fs_io_info *fio)
  1044. {
  1045. f2fs_submit_page_mbio(F2FS_P_SB(page), page, old_blkaddr, fio);
  1046. }
  1047. void recover_data_page(struct f2fs_sb_info *sbi,
  1048. struct page *page, struct f2fs_summary *sum,
  1049. block_t old_blkaddr, block_t new_blkaddr)
  1050. {
  1051. struct sit_info *sit_i = SIT_I(sbi);
  1052. struct curseg_info *curseg;
  1053. unsigned int segno, old_cursegno;
  1054. struct seg_entry *se;
  1055. int type;
  1056. segno = GET_SEGNO(sbi, new_blkaddr);
  1057. se = get_seg_entry(sbi, segno);
  1058. type = se->type;
  1059. if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
  1060. if (old_blkaddr == NULL_ADDR)
  1061. type = CURSEG_COLD_DATA;
  1062. else
  1063. type = CURSEG_WARM_DATA;
  1064. }
  1065. curseg = CURSEG_I(sbi, type);
  1066. mutex_lock(&curseg->curseg_mutex);
  1067. mutex_lock(&sit_i->sentry_lock);
  1068. old_cursegno = curseg->segno;
  1069. /* change the current segment */
  1070. if (segno != curseg->segno) {
  1071. curseg->next_segno = segno;
  1072. change_curseg(sbi, type, true);
  1073. }
  1074. curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
  1075. __add_sum_entry(sbi, type, sum);
  1076. refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
  1077. locate_dirty_segment(sbi, old_cursegno);
  1078. mutex_unlock(&sit_i->sentry_lock);
  1079. mutex_unlock(&curseg->curseg_mutex);
  1080. }
  1081. static inline bool is_merged_page(struct f2fs_sb_info *sbi,
  1082. struct page *page, enum page_type type)
  1083. {
  1084. enum page_type btype = PAGE_TYPE_OF_BIO(type);
  1085. struct f2fs_bio_info *io = &sbi->write_io[btype];
  1086. struct bio_vec *bvec;
  1087. int i;
  1088. down_read(&io->io_rwsem);
  1089. if (!io->bio)
  1090. goto out;
  1091. bio_for_each_segment_all(bvec, io->bio, i) {
  1092. if (page == bvec->bv_page) {
  1093. up_read(&io->io_rwsem);
  1094. return true;
  1095. }
  1096. }
  1097. out:
  1098. up_read(&io->io_rwsem);
  1099. return false;
  1100. }
  1101. void f2fs_wait_on_page_writeback(struct page *page,
  1102. enum page_type type)
  1103. {
  1104. if (PageWriteback(page)) {
  1105. struct f2fs_sb_info *sbi = F2FS_P_SB(page);
  1106. if (is_merged_page(sbi, page, type))
  1107. f2fs_submit_merged_bio(sbi, type, WRITE);
  1108. wait_on_page_writeback(page);
  1109. }
  1110. }
  1111. static int read_compacted_summaries(struct f2fs_sb_info *sbi)
  1112. {
  1113. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  1114. struct curseg_info *seg_i;
  1115. unsigned char *kaddr;
  1116. struct page *page;
  1117. block_t start;
  1118. int i, j, offset;
  1119. start = start_sum_block(sbi);
  1120. page = get_meta_page(sbi, start++);
  1121. kaddr = (unsigned char *)page_address(page);
  1122. /* Step 1: restore nat cache */
  1123. seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
  1124. memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
  1125. /* Step 2: restore sit cache */
  1126. seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
  1127. memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
  1128. SUM_JOURNAL_SIZE);
  1129. offset = 2 * SUM_JOURNAL_SIZE;
  1130. /* Step 3: restore summary entries */
  1131. for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
  1132. unsigned short blk_off;
  1133. unsigned int segno;
  1134. seg_i = CURSEG_I(sbi, i);
  1135. segno = le32_to_cpu(ckpt->cur_data_segno[i]);
  1136. blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
  1137. seg_i->next_segno = segno;
  1138. reset_curseg(sbi, i, 0);
  1139. seg_i->alloc_type = ckpt->alloc_type[i];
  1140. seg_i->next_blkoff = blk_off;
  1141. if (seg_i->alloc_type == SSR)
  1142. blk_off = sbi->blocks_per_seg;
  1143. for (j = 0; j < blk_off; j++) {
  1144. struct f2fs_summary *s;
  1145. s = (struct f2fs_summary *)(kaddr + offset);
  1146. seg_i->sum_blk->entries[j] = *s;
  1147. offset += SUMMARY_SIZE;
  1148. if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
  1149. SUM_FOOTER_SIZE)
  1150. continue;
  1151. f2fs_put_page(page, 1);
  1152. page = NULL;
  1153. page = get_meta_page(sbi, start++);
  1154. kaddr = (unsigned char *)page_address(page);
  1155. offset = 0;
  1156. }
  1157. }
  1158. f2fs_put_page(page, 1);
  1159. return 0;
  1160. }
  1161. static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
  1162. {
  1163. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  1164. struct f2fs_summary_block *sum;
  1165. struct curseg_info *curseg;
  1166. struct page *new;
  1167. unsigned short blk_off;
  1168. unsigned int segno = 0;
  1169. block_t blk_addr = 0;
  1170. /* get segment number and block addr */
  1171. if (IS_DATASEG(type)) {
  1172. segno = le32_to_cpu(ckpt->cur_data_segno[type]);
  1173. blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
  1174. CURSEG_HOT_DATA]);
  1175. if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
  1176. blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
  1177. else
  1178. blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
  1179. } else {
  1180. segno = le32_to_cpu(ckpt->cur_node_segno[type -
  1181. CURSEG_HOT_NODE]);
  1182. blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
  1183. CURSEG_HOT_NODE]);
  1184. if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
  1185. blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
  1186. type - CURSEG_HOT_NODE);
  1187. else
  1188. blk_addr = GET_SUM_BLOCK(sbi, segno);
  1189. }
  1190. new = get_meta_page(sbi, blk_addr);
  1191. sum = (struct f2fs_summary_block *)page_address(new);
  1192. if (IS_NODESEG(type)) {
  1193. if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG)) {
  1194. struct f2fs_summary *ns = &sum->entries[0];
  1195. int i;
  1196. for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
  1197. ns->version = 0;
  1198. ns->ofs_in_node = 0;
  1199. }
  1200. } else {
  1201. int err;
  1202. err = restore_node_summary(sbi, segno, sum);
  1203. if (err) {
  1204. f2fs_put_page(new, 1);
  1205. return err;
  1206. }
  1207. }
  1208. }
  1209. /* set uncompleted segment to curseg */
  1210. curseg = CURSEG_I(sbi, type);
  1211. mutex_lock(&curseg->curseg_mutex);
  1212. memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
  1213. curseg->next_segno = segno;
  1214. reset_curseg(sbi, type, 0);
  1215. curseg->alloc_type = ckpt->alloc_type[type];
  1216. curseg->next_blkoff = blk_off;
  1217. mutex_unlock(&curseg->curseg_mutex);
  1218. f2fs_put_page(new, 1);
  1219. return 0;
  1220. }
  1221. static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
  1222. {
  1223. int type = CURSEG_HOT_DATA;
  1224. int err;
  1225. if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
  1226. /* restore for compacted data summary */
  1227. if (read_compacted_summaries(sbi))
  1228. return -EINVAL;
  1229. type = CURSEG_HOT_NODE;
  1230. }
  1231. for (; type <= CURSEG_COLD_NODE; type++) {
  1232. err = read_normal_summaries(sbi, type);
  1233. if (err)
  1234. return err;
  1235. }
  1236. return 0;
  1237. }
  1238. static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
  1239. {
  1240. struct page *page;
  1241. unsigned char *kaddr;
  1242. struct f2fs_summary *summary;
  1243. struct curseg_info *seg_i;
  1244. int written_size = 0;
  1245. int i, j;
  1246. page = grab_meta_page(sbi, blkaddr++);
  1247. kaddr = (unsigned char *)page_address(page);
  1248. /* Step 1: write nat cache */
  1249. seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
  1250. memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
  1251. written_size += SUM_JOURNAL_SIZE;
  1252. /* Step 2: write sit cache */
  1253. seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
  1254. memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
  1255. SUM_JOURNAL_SIZE);
  1256. written_size += SUM_JOURNAL_SIZE;
  1257. /* Step 3: write summary entries */
  1258. for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
  1259. unsigned short blkoff;
  1260. seg_i = CURSEG_I(sbi, i);
  1261. if (sbi->ckpt->alloc_type[i] == SSR)
  1262. blkoff = sbi->blocks_per_seg;
  1263. else
  1264. blkoff = curseg_blkoff(sbi, i);
  1265. for (j = 0; j < blkoff; j++) {
  1266. if (!page) {
  1267. page = grab_meta_page(sbi, blkaddr++);
  1268. kaddr = (unsigned char *)page_address(page);
  1269. written_size = 0;
  1270. }
  1271. summary = (struct f2fs_summary *)(kaddr + written_size);
  1272. *summary = seg_i->sum_blk->entries[j];
  1273. written_size += SUMMARY_SIZE;
  1274. if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
  1275. SUM_FOOTER_SIZE)
  1276. continue;
  1277. set_page_dirty(page);
  1278. f2fs_put_page(page, 1);
  1279. page = NULL;
  1280. }
  1281. }
  1282. if (page) {
  1283. set_page_dirty(page);
  1284. f2fs_put_page(page, 1);
  1285. }
  1286. }
  1287. static void write_normal_summaries(struct f2fs_sb_info *sbi,
  1288. block_t blkaddr, int type)
  1289. {
  1290. int i, end;
  1291. if (IS_DATASEG(type))
  1292. end = type + NR_CURSEG_DATA_TYPE;
  1293. else
  1294. end = type + NR_CURSEG_NODE_TYPE;
  1295. for (i = type; i < end; i++) {
  1296. struct curseg_info *sum = CURSEG_I(sbi, i);
  1297. mutex_lock(&sum->curseg_mutex);
  1298. write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
  1299. mutex_unlock(&sum->curseg_mutex);
  1300. }
  1301. }
  1302. void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
  1303. {
  1304. if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
  1305. write_compacted_summaries(sbi, start_blk);
  1306. else
  1307. write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
  1308. }
  1309. void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
  1310. {
  1311. if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG))
  1312. write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
  1313. }
  1314. int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
  1315. unsigned int val, int alloc)
  1316. {
  1317. int i;
  1318. if (type == NAT_JOURNAL) {
  1319. for (i = 0; i < nats_in_cursum(sum); i++) {
  1320. if (le32_to_cpu(nid_in_journal(sum, i)) == val)
  1321. return i;
  1322. }
  1323. if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES)
  1324. return update_nats_in_cursum(sum, 1);
  1325. } else if (type == SIT_JOURNAL) {
  1326. for (i = 0; i < sits_in_cursum(sum); i++)
  1327. if (le32_to_cpu(segno_in_journal(sum, i)) == val)
  1328. return i;
  1329. if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES)
  1330. return update_sits_in_cursum(sum, 1);
  1331. }
  1332. return -1;
  1333. }
  1334. static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
  1335. unsigned int segno)
  1336. {
  1337. return get_meta_page(sbi, current_sit_addr(sbi, segno));
  1338. }
  1339. static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
  1340. unsigned int start)
  1341. {
  1342. struct sit_info *sit_i = SIT_I(sbi);
  1343. struct page *src_page, *dst_page;
  1344. pgoff_t src_off, dst_off;
  1345. void *src_addr, *dst_addr;
  1346. src_off = current_sit_addr(sbi, start);
  1347. dst_off = next_sit_addr(sbi, src_off);
  1348. /* get current sit block page without lock */
  1349. src_page = get_meta_page(sbi, src_off);
  1350. dst_page = grab_meta_page(sbi, dst_off);
  1351. f2fs_bug_on(sbi, PageDirty(src_page));
  1352. src_addr = page_address(src_page);
  1353. dst_addr = page_address(dst_page);
  1354. memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
  1355. set_page_dirty(dst_page);
  1356. f2fs_put_page(src_page, 1);
  1357. set_to_next_sit(sit_i, start);
  1358. return dst_page;
  1359. }
  1360. static struct sit_entry_set *grab_sit_entry_set(void)
  1361. {
  1362. struct sit_entry_set *ses =
  1363. f2fs_kmem_cache_alloc(sit_entry_set_slab, GFP_ATOMIC);
  1364. ses->entry_cnt = 0;
  1365. INIT_LIST_HEAD(&ses->set_list);
  1366. return ses;
  1367. }
  1368. static void release_sit_entry_set(struct sit_entry_set *ses)
  1369. {
  1370. list_del(&ses->set_list);
  1371. kmem_cache_free(sit_entry_set_slab, ses);
  1372. }
  1373. static void adjust_sit_entry_set(struct sit_entry_set *ses,
  1374. struct list_head *head)
  1375. {
  1376. struct sit_entry_set *next = ses;
  1377. if (list_is_last(&ses->set_list, head))
  1378. return;
  1379. list_for_each_entry_continue(next, head, set_list)
  1380. if (ses->entry_cnt <= next->entry_cnt)
  1381. break;
  1382. list_move_tail(&ses->set_list, &next->set_list);
  1383. }
  1384. static void add_sit_entry(unsigned int segno, struct list_head *head)
  1385. {
  1386. struct sit_entry_set *ses;
  1387. unsigned int start_segno = START_SEGNO(segno);
  1388. list_for_each_entry(ses, head, set_list) {
  1389. if (ses->start_segno == start_segno) {
  1390. ses->entry_cnt++;
  1391. adjust_sit_entry_set(ses, head);
  1392. return;
  1393. }
  1394. }
  1395. ses = grab_sit_entry_set();
  1396. ses->start_segno = start_segno;
  1397. ses->entry_cnt++;
  1398. list_add(&ses->set_list, head);
  1399. }
  1400. static void add_sits_in_set(struct f2fs_sb_info *sbi)
  1401. {
  1402. struct f2fs_sm_info *sm_info = SM_I(sbi);
  1403. struct list_head *set_list = &sm_info->sit_entry_set;
  1404. unsigned long *bitmap = SIT_I(sbi)->dirty_sentries_bitmap;
  1405. unsigned int segno;
  1406. for_each_set_bit(segno, bitmap, MAIN_SEGS(sbi))
  1407. add_sit_entry(segno, set_list);
  1408. }
  1409. static void remove_sits_in_journal(struct f2fs_sb_info *sbi)
  1410. {
  1411. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
  1412. struct f2fs_summary_block *sum = curseg->sum_blk;
  1413. int i;
  1414. for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
  1415. unsigned int segno;
  1416. bool dirtied;
  1417. segno = le32_to_cpu(segno_in_journal(sum, i));
  1418. dirtied = __mark_sit_entry_dirty(sbi, segno);
  1419. if (!dirtied)
  1420. add_sit_entry(segno, &SM_I(sbi)->sit_entry_set);
  1421. }
  1422. update_sits_in_cursum(sum, -sits_in_cursum(sum));
  1423. }
  1424. /*
  1425. * CP calls this function, which flushes SIT entries including sit_journal,
  1426. * and moves prefree segs to free segs.
  1427. */
  1428. void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
  1429. {
  1430. struct sit_info *sit_i = SIT_I(sbi);
  1431. unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
  1432. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
  1433. struct f2fs_summary_block *sum = curseg->sum_blk;
  1434. struct sit_entry_set *ses, *tmp;
  1435. struct list_head *head = &SM_I(sbi)->sit_entry_set;
  1436. bool to_journal = true;
  1437. struct seg_entry *se;
  1438. mutex_lock(&curseg->curseg_mutex);
  1439. mutex_lock(&sit_i->sentry_lock);
  1440. /*
  1441. * add and account sit entries of dirty bitmap in sit entry
  1442. * set temporarily
  1443. */
  1444. add_sits_in_set(sbi);
  1445. /*
  1446. * if there are no enough space in journal to store dirty sit
  1447. * entries, remove all entries from journal and add and account
  1448. * them in sit entry set.
  1449. */
  1450. if (!__has_cursum_space(sum, sit_i->dirty_sentries, SIT_JOURNAL))
  1451. remove_sits_in_journal(sbi);
  1452. if (!sit_i->dirty_sentries)
  1453. goto out;
  1454. /*
  1455. * there are two steps to flush sit entries:
  1456. * #1, flush sit entries to journal in current cold data summary block.
  1457. * #2, flush sit entries to sit page.
  1458. */
  1459. list_for_each_entry_safe(ses, tmp, head, set_list) {
  1460. struct page *page = NULL;
  1461. struct f2fs_sit_block *raw_sit = NULL;
  1462. unsigned int start_segno = ses->start_segno;
  1463. unsigned int end = min(start_segno + SIT_ENTRY_PER_BLOCK,
  1464. (unsigned long)MAIN_SEGS(sbi));
  1465. unsigned int segno = start_segno;
  1466. if (to_journal &&
  1467. !__has_cursum_space(sum, ses->entry_cnt, SIT_JOURNAL))
  1468. to_journal = false;
  1469. if (!to_journal) {
  1470. page = get_next_sit_page(sbi, start_segno);
  1471. raw_sit = page_address(page);
  1472. }
  1473. /* flush dirty sit entries in region of current sit set */
  1474. for_each_set_bit_from(segno, bitmap, end) {
  1475. int offset, sit_offset;
  1476. se = get_seg_entry(sbi, segno);
  1477. /* add discard candidates */
  1478. if (cpc->reason != CP_DISCARD) {
  1479. cpc->trim_start = segno;
  1480. add_discard_addrs(sbi, cpc);
  1481. }
  1482. if (to_journal) {
  1483. offset = lookup_journal_in_cursum(sum,
  1484. SIT_JOURNAL, segno, 1);
  1485. f2fs_bug_on(sbi, offset < 0);
  1486. segno_in_journal(sum, offset) =
  1487. cpu_to_le32(segno);
  1488. seg_info_to_raw_sit(se,
  1489. &sit_in_journal(sum, offset));
  1490. } else {
  1491. sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
  1492. seg_info_to_raw_sit(se,
  1493. &raw_sit->entries[sit_offset]);
  1494. }
  1495. __clear_bit(segno, bitmap);
  1496. sit_i->dirty_sentries--;
  1497. ses->entry_cnt--;
  1498. }
  1499. if (!to_journal)
  1500. f2fs_put_page(page, 1);
  1501. f2fs_bug_on(sbi, ses->entry_cnt);
  1502. release_sit_entry_set(ses);
  1503. }
  1504. f2fs_bug_on(sbi, !list_empty(head));
  1505. f2fs_bug_on(sbi, sit_i->dirty_sentries);
  1506. out:
  1507. if (cpc->reason == CP_DISCARD) {
  1508. for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++)
  1509. add_discard_addrs(sbi, cpc);
  1510. }
  1511. mutex_unlock(&sit_i->sentry_lock);
  1512. mutex_unlock(&curseg->curseg_mutex);
  1513. set_prefree_as_free_segments(sbi);
  1514. }
  1515. static int build_sit_info(struct f2fs_sb_info *sbi)
  1516. {
  1517. struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
  1518. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  1519. struct sit_info *sit_i;
  1520. unsigned int sit_segs, start;
  1521. char *src_bitmap, *dst_bitmap;
  1522. unsigned int bitmap_size;
  1523. /* allocate memory for SIT information */
  1524. sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
  1525. if (!sit_i)
  1526. return -ENOMEM;
  1527. SM_I(sbi)->sit_info = sit_i;
  1528. sit_i->sentries = vzalloc(MAIN_SEGS(sbi) * sizeof(struct seg_entry));
  1529. if (!sit_i->sentries)
  1530. return -ENOMEM;
  1531. bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
  1532. sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
  1533. if (!sit_i->dirty_sentries_bitmap)
  1534. return -ENOMEM;
  1535. for (start = 0; start < MAIN_SEGS(sbi); start++) {
  1536. sit_i->sentries[start].cur_valid_map
  1537. = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
  1538. sit_i->sentries[start].ckpt_valid_map
  1539. = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
  1540. if (!sit_i->sentries[start].cur_valid_map
  1541. || !sit_i->sentries[start].ckpt_valid_map)
  1542. return -ENOMEM;
  1543. }
  1544. if (sbi->segs_per_sec > 1) {
  1545. sit_i->sec_entries = vzalloc(MAIN_SECS(sbi) *
  1546. sizeof(struct sec_entry));
  1547. if (!sit_i->sec_entries)
  1548. return -ENOMEM;
  1549. }
  1550. /* get information related with SIT */
  1551. sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
  1552. /* setup SIT bitmap from ckeckpoint pack */
  1553. bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
  1554. src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
  1555. dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
  1556. if (!dst_bitmap)
  1557. return -ENOMEM;
  1558. /* init SIT information */
  1559. sit_i->s_ops = &default_salloc_ops;
  1560. sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
  1561. sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
  1562. sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
  1563. sit_i->sit_bitmap = dst_bitmap;
  1564. sit_i->bitmap_size = bitmap_size;
  1565. sit_i->dirty_sentries = 0;
  1566. sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
  1567. sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
  1568. sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
  1569. mutex_init(&sit_i->sentry_lock);
  1570. return 0;
  1571. }
  1572. static int build_free_segmap(struct f2fs_sb_info *sbi)
  1573. {
  1574. struct free_segmap_info *free_i;
  1575. unsigned int bitmap_size, sec_bitmap_size;
  1576. /* allocate memory for free segmap information */
  1577. free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
  1578. if (!free_i)
  1579. return -ENOMEM;
  1580. SM_I(sbi)->free_info = free_i;
  1581. bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
  1582. free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL);
  1583. if (!free_i->free_segmap)
  1584. return -ENOMEM;
  1585. sec_bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
  1586. free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL);
  1587. if (!free_i->free_secmap)
  1588. return -ENOMEM;
  1589. /* set all segments as dirty temporarily */
  1590. memset(free_i->free_segmap, 0xff, bitmap_size);
  1591. memset(free_i->free_secmap, 0xff, sec_bitmap_size);
  1592. /* init free segmap information */
  1593. free_i->start_segno = GET_SEGNO_FROM_SEG0(sbi, MAIN_BLKADDR(sbi));
  1594. free_i->free_segments = 0;
  1595. free_i->free_sections = 0;
  1596. rwlock_init(&free_i->segmap_lock);
  1597. return 0;
  1598. }
  1599. static int build_curseg(struct f2fs_sb_info *sbi)
  1600. {
  1601. struct curseg_info *array;
  1602. int i;
  1603. array = kcalloc(NR_CURSEG_TYPE, sizeof(*array), GFP_KERNEL);
  1604. if (!array)
  1605. return -ENOMEM;
  1606. SM_I(sbi)->curseg_array = array;
  1607. for (i = 0; i < NR_CURSEG_TYPE; i++) {
  1608. mutex_init(&array[i].curseg_mutex);
  1609. array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
  1610. if (!array[i].sum_blk)
  1611. return -ENOMEM;
  1612. array[i].segno = NULL_SEGNO;
  1613. array[i].next_blkoff = 0;
  1614. }
  1615. return restore_curseg_summaries(sbi);
  1616. }
  1617. static void build_sit_entries(struct f2fs_sb_info *sbi)
  1618. {
  1619. struct sit_info *sit_i = SIT_I(sbi);
  1620. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
  1621. struct f2fs_summary_block *sum = curseg->sum_blk;
  1622. int sit_blk_cnt = SIT_BLK_CNT(sbi);
  1623. unsigned int i, start, end;
  1624. unsigned int readed, start_blk = 0;
  1625. int nrpages = MAX_BIO_BLOCKS(sbi);
  1626. do {
  1627. readed = ra_meta_pages(sbi, start_blk, nrpages, META_SIT);
  1628. start = start_blk * sit_i->sents_per_block;
  1629. end = (start_blk + readed) * sit_i->sents_per_block;
  1630. for (; start < end && start < MAIN_SEGS(sbi); start++) {
  1631. struct seg_entry *se = &sit_i->sentries[start];
  1632. struct f2fs_sit_block *sit_blk;
  1633. struct f2fs_sit_entry sit;
  1634. struct page *page;
  1635. mutex_lock(&curseg->curseg_mutex);
  1636. for (i = 0; i < sits_in_cursum(sum); i++) {
  1637. if (le32_to_cpu(segno_in_journal(sum, i))
  1638. == start) {
  1639. sit = sit_in_journal(sum, i);
  1640. mutex_unlock(&curseg->curseg_mutex);
  1641. goto got_it;
  1642. }
  1643. }
  1644. mutex_unlock(&curseg->curseg_mutex);
  1645. page = get_current_sit_page(sbi, start);
  1646. sit_blk = (struct f2fs_sit_block *)page_address(page);
  1647. sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
  1648. f2fs_put_page(page, 1);
  1649. got_it:
  1650. check_block_count(sbi, start, &sit);
  1651. seg_info_from_raw_sit(se, &sit);
  1652. if (sbi->segs_per_sec > 1) {
  1653. struct sec_entry *e = get_sec_entry(sbi, start);
  1654. e->valid_blocks += se->valid_blocks;
  1655. }
  1656. }
  1657. start_blk += readed;
  1658. } while (start_blk < sit_blk_cnt);
  1659. }
  1660. static void init_free_segmap(struct f2fs_sb_info *sbi)
  1661. {
  1662. unsigned int start;
  1663. int type;
  1664. for (start = 0; start < MAIN_SEGS(sbi); start++) {
  1665. struct seg_entry *sentry = get_seg_entry(sbi, start);
  1666. if (!sentry->valid_blocks)
  1667. __set_free(sbi, start);
  1668. }
  1669. /* set use the current segments */
  1670. for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
  1671. struct curseg_info *curseg_t = CURSEG_I(sbi, type);
  1672. __set_test_and_inuse(sbi, curseg_t->segno);
  1673. }
  1674. }
  1675. static void init_dirty_segmap(struct f2fs_sb_info *sbi)
  1676. {
  1677. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  1678. struct free_segmap_info *free_i = FREE_I(sbi);
  1679. unsigned int segno = 0, offset = 0;
  1680. unsigned short valid_blocks;
  1681. while (1) {
  1682. /* find dirty segment based on free segmap */
  1683. segno = find_next_inuse(free_i, MAIN_SEGS(sbi), offset);
  1684. if (segno >= MAIN_SEGS(sbi))
  1685. break;
  1686. offset = segno + 1;
  1687. valid_blocks = get_valid_blocks(sbi, segno, 0);
  1688. if (valid_blocks == sbi->blocks_per_seg || !valid_blocks)
  1689. continue;
  1690. if (valid_blocks > sbi->blocks_per_seg) {
  1691. f2fs_bug_on(sbi, 1);
  1692. continue;
  1693. }
  1694. mutex_lock(&dirty_i->seglist_lock);
  1695. __locate_dirty_segment(sbi, segno, DIRTY);
  1696. mutex_unlock(&dirty_i->seglist_lock);
  1697. }
  1698. }
  1699. static int init_victim_secmap(struct f2fs_sb_info *sbi)
  1700. {
  1701. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  1702. unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
  1703. dirty_i->victim_secmap = kzalloc(bitmap_size, GFP_KERNEL);
  1704. if (!dirty_i->victim_secmap)
  1705. return -ENOMEM;
  1706. return 0;
  1707. }
  1708. static int build_dirty_segmap(struct f2fs_sb_info *sbi)
  1709. {
  1710. struct dirty_seglist_info *dirty_i;
  1711. unsigned int bitmap_size, i;
  1712. /* allocate memory for dirty segments list information */
  1713. dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
  1714. if (!dirty_i)
  1715. return -ENOMEM;
  1716. SM_I(sbi)->dirty_info = dirty_i;
  1717. mutex_init(&dirty_i->seglist_lock);
  1718. bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
  1719. for (i = 0; i < NR_DIRTY_TYPE; i++) {
  1720. dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL);
  1721. if (!dirty_i->dirty_segmap[i])
  1722. return -ENOMEM;
  1723. }
  1724. init_dirty_segmap(sbi);
  1725. return init_victim_secmap(sbi);
  1726. }
  1727. /*
  1728. * Update min, max modified time for cost-benefit GC algorithm
  1729. */
  1730. static void init_min_max_mtime(struct f2fs_sb_info *sbi)
  1731. {
  1732. struct sit_info *sit_i = SIT_I(sbi);
  1733. unsigned int segno;
  1734. mutex_lock(&sit_i->sentry_lock);
  1735. sit_i->min_mtime = LLONG_MAX;
  1736. for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) {
  1737. unsigned int i;
  1738. unsigned long long mtime = 0;
  1739. for (i = 0; i < sbi->segs_per_sec; i++)
  1740. mtime += get_seg_entry(sbi, segno + i)->mtime;
  1741. mtime = div_u64(mtime, sbi->segs_per_sec);
  1742. if (sit_i->min_mtime > mtime)
  1743. sit_i->min_mtime = mtime;
  1744. }
  1745. sit_i->max_mtime = get_mtime(sbi);
  1746. mutex_unlock(&sit_i->sentry_lock);
  1747. }
  1748. int build_segment_manager(struct f2fs_sb_info *sbi)
  1749. {
  1750. struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
  1751. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  1752. struct f2fs_sm_info *sm_info;
  1753. int err;
  1754. sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
  1755. if (!sm_info)
  1756. return -ENOMEM;
  1757. /* init sm info */
  1758. sbi->sm_info = sm_info;
  1759. sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
  1760. sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
  1761. sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
  1762. sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
  1763. sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
  1764. sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
  1765. sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
  1766. sm_info->rec_prefree_segments = sm_info->main_segments *
  1767. DEF_RECLAIM_PREFREE_SEGMENTS / 100;
  1768. sm_info->ipu_policy = 1 << F2FS_IPU_FSYNC;
  1769. sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
  1770. sm_info->min_fsync_blocks = DEF_MIN_FSYNC_BLOCKS;
  1771. INIT_LIST_HEAD(&sm_info->discard_list);
  1772. sm_info->nr_discards = 0;
  1773. sm_info->max_discards = 0;
  1774. INIT_LIST_HEAD(&sm_info->sit_entry_set);
  1775. if (test_opt(sbi, FLUSH_MERGE) && !f2fs_readonly(sbi->sb)) {
  1776. err = create_flush_cmd_control(sbi);
  1777. if (err)
  1778. return err;
  1779. }
  1780. err = build_sit_info(sbi);
  1781. if (err)
  1782. return err;
  1783. err = build_free_segmap(sbi);
  1784. if (err)
  1785. return err;
  1786. err = build_curseg(sbi);
  1787. if (err)
  1788. return err;
  1789. /* reinit free segmap based on SIT */
  1790. build_sit_entries(sbi);
  1791. init_free_segmap(sbi);
  1792. err = build_dirty_segmap(sbi);
  1793. if (err)
  1794. return err;
  1795. init_min_max_mtime(sbi);
  1796. return 0;
  1797. }
  1798. static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
  1799. enum dirty_type dirty_type)
  1800. {
  1801. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  1802. mutex_lock(&dirty_i->seglist_lock);
  1803. kfree(dirty_i->dirty_segmap[dirty_type]);
  1804. dirty_i->nr_dirty[dirty_type] = 0;
  1805. mutex_unlock(&dirty_i->seglist_lock);
  1806. }
  1807. static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
  1808. {
  1809. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  1810. kfree(dirty_i->victim_secmap);
  1811. }
  1812. static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
  1813. {
  1814. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  1815. int i;
  1816. if (!dirty_i)
  1817. return;
  1818. /* discard pre-free/dirty segments list */
  1819. for (i = 0; i < NR_DIRTY_TYPE; i++)
  1820. discard_dirty_segmap(sbi, i);
  1821. destroy_victim_secmap(sbi);
  1822. SM_I(sbi)->dirty_info = NULL;
  1823. kfree(dirty_i);
  1824. }
  1825. static void destroy_curseg(struct f2fs_sb_info *sbi)
  1826. {
  1827. struct curseg_info *array = SM_I(sbi)->curseg_array;
  1828. int i;
  1829. if (!array)
  1830. return;
  1831. SM_I(sbi)->curseg_array = NULL;
  1832. for (i = 0; i < NR_CURSEG_TYPE; i++)
  1833. kfree(array[i].sum_blk);
  1834. kfree(array);
  1835. }
  1836. static void destroy_free_segmap(struct f2fs_sb_info *sbi)
  1837. {
  1838. struct free_segmap_info *free_i = SM_I(sbi)->free_info;
  1839. if (!free_i)
  1840. return;
  1841. SM_I(sbi)->free_info = NULL;
  1842. kfree(free_i->free_segmap);
  1843. kfree(free_i->free_secmap);
  1844. kfree(free_i);
  1845. }
  1846. static void destroy_sit_info(struct f2fs_sb_info *sbi)
  1847. {
  1848. struct sit_info *sit_i = SIT_I(sbi);
  1849. unsigned int start;
  1850. if (!sit_i)
  1851. return;
  1852. if (sit_i->sentries) {
  1853. for (start = 0; start < MAIN_SEGS(sbi); start++) {
  1854. kfree(sit_i->sentries[start].cur_valid_map);
  1855. kfree(sit_i->sentries[start].ckpt_valid_map);
  1856. }
  1857. }
  1858. vfree(sit_i->sentries);
  1859. vfree(sit_i->sec_entries);
  1860. kfree(sit_i->dirty_sentries_bitmap);
  1861. SM_I(sbi)->sit_info = NULL;
  1862. kfree(sit_i->sit_bitmap);
  1863. kfree(sit_i);
  1864. }
  1865. void destroy_segment_manager(struct f2fs_sb_info *sbi)
  1866. {
  1867. struct f2fs_sm_info *sm_info = SM_I(sbi);
  1868. if (!sm_info)
  1869. return;
  1870. destroy_flush_cmd_control(sbi);
  1871. destroy_dirty_segmap(sbi);
  1872. destroy_curseg(sbi);
  1873. destroy_free_segmap(sbi);
  1874. destroy_sit_info(sbi);
  1875. sbi->sm_info = NULL;
  1876. kfree(sm_info);
  1877. }
  1878. int __init create_segment_manager_caches(void)
  1879. {
  1880. discard_entry_slab = f2fs_kmem_cache_create("discard_entry",
  1881. sizeof(struct discard_entry));
  1882. if (!discard_entry_slab)
  1883. goto fail;
  1884. sit_entry_set_slab = f2fs_kmem_cache_create("sit_entry_set",
  1885. sizeof(struct sit_entry_set));
  1886. if (!sit_entry_set_slab)
  1887. goto destory_discard_entry;
  1888. inmem_entry_slab = f2fs_kmem_cache_create("inmem_page_entry",
  1889. sizeof(struct inmem_pages));
  1890. if (!inmem_entry_slab)
  1891. goto destroy_sit_entry_set;
  1892. return 0;
  1893. destroy_sit_entry_set:
  1894. kmem_cache_destroy(sit_entry_set_slab);
  1895. destory_discard_entry:
  1896. kmem_cache_destroy(discard_entry_slab);
  1897. fail:
  1898. return -ENOMEM;
  1899. }
  1900. void destroy_segment_manager_caches(void)
  1901. {
  1902. kmem_cache_destroy(sit_entry_set_slab);
  1903. kmem_cache_destroy(discard_entry_slab);
  1904. kmem_cache_destroy(inmem_entry_slab);
  1905. }