segment.c 68 KB

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