segment.c 58 KB

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