segment.c 70 KB

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