compress.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * f2fs compress support
  4. *
  5. * Copyright (c) 2019 Chao Yu <[email protected]>
  6. */
  7. #include <linux/fs.h>
  8. #include <linux/f2fs_fs.h>
  9. #include <linux/moduleparam.h>
  10. #include <linux/writeback.h>
  11. #include <linux/backing-dev.h>
  12. #include <linux/lzo.h>
  13. #include <linux/lz4.h>
  14. #include <linux/zstd.h>
  15. #include <linux/pagevec.h>
  16. #include "f2fs.h"
  17. #include "node.h"
  18. #include "segment.h"
  19. #include <trace/events/f2fs.h>
  20. static struct kmem_cache *cic_entry_slab;
  21. static struct kmem_cache *dic_entry_slab;
  22. static void *page_array_alloc(struct inode *inode, int nr)
  23. {
  24. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  25. unsigned int size = sizeof(struct page *) * nr;
  26. if (likely(size <= sbi->page_array_slab_size))
  27. return f2fs_kmem_cache_alloc(sbi->page_array_slab,
  28. GFP_F2FS_ZERO, false, F2FS_I_SB(inode));
  29. return f2fs_kzalloc(sbi, size, GFP_NOFS);
  30. }
  31. static void page_array_free(struct inode *inode, void *pages, int nr)
  32. {
  33. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  34. unsigned int size = sizeof(struct page *) * nr;
  35. if (!pages)
  36. return;
  37. if (likely(size <= sbi->page_array_slab_size))
  38. kmem_cache_free(sbi->page_array_slab, pages);
  39. else
  40. kfree(pages);
  41. }
  42. struct f2fs_compress_ops {
  43. int (*init_compress_ctx)(struct compress_ctx *cc);
  44. void (*destroy_compress_ctx)(struct compress_ctx *cc);
  45. int (*compress_pages)(struct compress_ctx *cc);
  46. int (*init_decompress_ctx)(struct decompress_io_ctx *dic);
  47. void (*destroy_decompress_ctx)(struct decompress_io_ctx *dic);
  48. int (*decompress_pages)(struct decompress_io_ctx *dic);
  49. };
  50. static unsigned int offset_in_cluster(struct compress_ctx *cc, pgoff_t index)
  51. {
  52. return index & (cc->cluster_size - 1);
  53. }
  54. static pgoff_t cluster_idx(struct compress_ctx *cc, pgoff_t index)
  55. {
  56. return index >> cc->log_cluster_size;
  57. }
  58. static pgoff_t start_idx_of_cluster(struct compress_ctx *cc)
  59. {
  60. return cc->cluster_idx << cc->log_cluster_size;
  61. }
  62. bool f2fs_is_compressed_page(struct page *page)
  63. {
  64. if (!PagePrivate(page))
  65. return false;
  66. if (!page_private(page))
  67. return false;
  68. if (page_private_nonpointer(page))
  69. return false;
  70. f2fs_bug_on(F2FS_M_SB(page->mapping),
  71. *((u32 *)page_private(page)) != F2FS_COMPRESSED_PAGE_MAGIC);
  72. return true;
  73. }
  74. static void f2fs_set_compressed_page(struct page *page,
  75. struct inode *inode, pgoff_t index, void *data)
  76. {
  77. attach_page_private(page, (void *)data);
  78. /* i_crypto_info and iv index */
  79. page->index = index;
  80. page->mapping = inode->i_mapping;
  81. }
  82. static void f2fs_drop_rpages(struct compress_ctx *cc, int len, bool unlock)
  83. {
  84. int i;
  85. for (i = 0; i < len; i++) {
  86. if (!cc->rpages[i])
  87. continue;
  88. if (unlock)
  89. unlock_page(cc->rpages[i]);
  90. else
  91. put_page(cc->rpages[i]);
  92. }
  93. }
  94. static void f2fs_put_rpages(struct compress_ctx *cc)
  95. {
  96. f2fs_drop_rpages(cc, cc->cluster_size, false);
  97. }
  98. static void f2fs_unlock_rpages(struct compress_ctx *cc, int len)
  99. {
  100. f2fs_drop_rpages(cc, len, true);
  101. }
  102. static void f2fs_put_rpages_wbc(struct compress_ctx *cc,
  103. struct writeback_control *wbc, bool redirty, int unlock)
  104. {
  105. unsigned int i;
  106. for (i = 0; i < cc->cluster_size; i++) {
  107. if (!cc->rpages[i])
  108. continue;
  109. if (redirty)
  110. redirty_page_for_writepage(wbc, cc->rpages[i]);
  111. f2fs_put_page(cc->rpages[i], unlock);
  112. }
  113. }
  114. struct page *f2fs_compress_control_page(struct page *page)
  115. {
  116. return ((struct compress_io_ctx *)page_private(page))->rpages[0];
  117. }
  118. int f2fs_init_compress_ctx(struct compress_ctx *cc)
  119. {
  120. if (cc->rpages)
  121. return 0;
  122. cc->rpages = page_array_alloc(cc->inode, cc->cluster_size);
  123. return cc->rpages ? 0 : -ENOMEM;
  124. }
  125. void f2fs_destroy_compress_ctx(struct compress_ctx *cc, bool reuse)
  126. {
  127. page_array_free(cc->inode, cc->rpages, cc->cluster_size);
  128. cc->rpages = NULL;
  129. cc->nr_rpages = 0;
  130. cc->nr_cpages = 0;
  131. cc->valid_nr_cpages = 0;
  132. if (!reuse)
  133. cc->cluster_idx = NULL_CLUSTER;
  134. }
  135. void f2fs_compress_ctx_add_page(struct compress_ctx *cc, struct page *page)
  136. {
  137. unsigned int cluster_ofs;
  138. if (!f2fs_cluster_can_merge_page(cc, page->index))
  139. f2fs_bug_on(F2FS_I_SB(cc->inode), 1);
  140. cluster_ofs = offset_in_cluster(cc, page->index);
  141. cc->rpages[cluster_ofs] = page;
  142. cc->nr_rpages++;
  143. cc->cluster_idx = cluster_idx(cc, page->index);
  144. }
  145. #ifdef CONFIG_F2FS_FS_LZO
  146. static int lzo_init_compress_ctx(struct compress_ctx *cc)
  147. {
  148. cc->private = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
  149. LZO1X_MEM_COMPRESS, GFP_NOFS);
  150. if (!cc->private)
  151. return -ENOMEM;
  152. cc->clen = lzo1x_worst_compress(PAGE_SIZE << cc->log_cluster_size);
  153. return 0;
  154. }
  155. static void lzo_destroy_compress_ctx(struct compress_ctx *cc)
  156. {
  157. kvfree(cc->private);
  158. cc->private = NULL;
  159. }
  160. static int lzo_compress_pages(struct compress_ctx *cc)
  161. {
  162. int ret;
  163. ret = lzo1x_1_compress(cc->rbuf, cc->rlen, cc->cbuf->cdata,
  164. &cc->clen, cc->private);
  165. if (ret != LZO_E_OK) {
  166. printk_ratelimited("%sF2FS-fs (%s): lzo compress failed, ret:%d\n",
  167. KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id, ret);
  168. return -EIO;
  169. }
  170. return 0;
  171. }
  172. static int lzo_decompress_pages(struct decompress_io_ctx *dic)
  173. {
  174. int ret;
  175. ret = lzo1x_decompress_safe(dic->cbuf->cdata, dic->clen,
  176. dic->rbuf, &dic->rlen);
  177. if (ret != LZO_E_OK) {
  178. printk_ratelimited("%sF2FS-fs (%s): lzo decompress failed, ret:%d\n",
  179. KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id, ret);
  180. return -EIO;
  181. }
  182. if (dic->rlen != PAGE_SIZE << dic->log_cluster_size) {
  183. printk_ratelimited("%sF2FS-fs (%s): lzo invalid rlen:%zu, "
  184. "expected:%lu\n", KERN_ERR,
  185. F2FS_I_SB(dic->inode)->sb->s_id,
  186. dic->rlen,
  187. PAGE_SIZE << dic->log_cluster_size);
  188. return -EIO;
  189. }
  190. return 0;
  191. }
  192. static const struct f2fs_compress_ops f2fs_lzo_ops = {
  193. .init_compress_ctx = lzo_init_compress_ctx,
  194. .destroy_compress_ctx = lzo_destroy_compress_ctx,
  195. .compress_pages = lzo_compress_pages,
  196. .decompress_pages = lzo_decompress_pages,
  197. };
  198. #endif
  199. #ifdef CONFIG_F2FS_FS_LZ4
  200. static int lz4_init_compress_ctx(struct compress_ctx *cc)
  201. {
  202. unsigned int size = LZ4_MEM_COMPRESS;
  203. #ifdef CONFIG_F2FS_FS_LZ4HC
  204. if (F2FS_I(cc->inode)->i_compress_level)
  205. size = LZ4HC_MEM_COMPRESS;
  206. #endif
  207. cc->private = f2fs_kvmalloc(F2FS_I_SB(cc->inode), size, GFP_NOFS);
  208. if (!cc->private)
  209. return -ENOMEM;
  210. /*
  211. * we do not change cc->clen to LZ4_compressBound(inputsize) to
  212. * adapt worst compress case, because lz4 compressor can handle
  213. * output budget properly.
  214. */
  215. cc->clen = cc->rlen - PAGE_SIZE - COMPRESS_HEADER_SIZE;
  216. return 0;
  217. }
  218. static void lz4_destroy_compress_ctx(struct compress_ctx *cc)
  219. {
  220. kvfree(cc->private);
  221. cc->private = NULL;
  222. }
  223. static int lz4_compress_pages(struct compress_ctx *cc)
  224. {
  225. int len = -EINVAL;
  226. unsigned char level = F2FS_I(cc->inode)->i_compress_level;
  227. if (!level)
  228. len = LZ4_compress_default(cc->rbuf, cc->cbuf->cdata, cc->rlen,
  229. cc->clen, cc->private);
  230. #ifdef CONFIG_F2FS_FS_LZ4HC
  231. else
  232. len = LZ4_compress_HC(cc->rbuf, cc->cbuf->cdata, cc->rlen,
  233. cc->clen, level, cc->private);
  234. #endif
  235. if (len < 0)
  236. return len;
  237. if (!len)
  238. return -EAGAIN;
  239. cc->clen = len;
  240. return 0;
  241. }
  242. static int lz4_decompress_pages(struct decompress_io_ctx *dic)
  243. {
  244. int ret;
  245. ret = LZ4_decompress_safe(dic->cbuf->cdata, dic->rbuf,
  246. dic->clen, dic->rlen);
  247. if (ret < 0) {
  248. printk_ratelimited("%sF2FS-fs (%s): lz4 decompress failed, ret:%d\n",
  249. KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id, ret);
  250. return -EIO;
  251. }
  252. if (ret != PAGE_SIZE << dic->log_cluster_size) {
  253. printk_ratelimited("%sF2FS-fs (%s): lz4 invalid ret:%d, "
  254. "expected:%lu\n", KERN_ERR,
  255. F2FS_I_SB(dic->inode)->sb->s_id, ret,
  256. PAGE_SIZE << dic->log_cluster_size);
  257. return -EIO;
  258. }
  259. return 0;
  260. }
  261. static const struct f2fs_compress_ops f2fs_lz4_ops = {
  262. .init_compress_ctx = lz4_init_compress_ctx,
  263. .destroy_compress_ctx = lz4_destroy_compress_ctx,
  264. .compress_pages = lz4_compress_pages,
  265. .decompress_pages = lz4_decompress_pages,
  266. };
  267. #endif
  268. #ifdef CONFIG_F2FS_FS_ZSTD
  269. #define F2FS_ZSTD_DEFAULT_CLEVEL 1
  270. static int zstd_init_compress_ctx(struct compress_ctx *cc)
  271. {
  272. zstd_parameters params;
  273. zstd_cstream *stream;
  274. void *workspace;
  275. unsigned int workspace_size;
  276. unsigned char level = F2FS_I(cc->inode)->i_compress_level;
  277. if (!level)
  278. level = F2FS_ZSTD_DEFAULT_CLEVEL;
  279. params = zstd_get_params(level, cc->rlen);
  280. workspace_size = zstd_cstream_workspace_bound(&params.cParams);
  281. workspace = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
  282. workspace_size, GFP_NOFS);
  283. if (!workspace)
  284. return -ENOMEM;
  285. stream = zstd_init_cstream(&params, 0, workspace, workspace_size);
  286. if (!stream) {
  287. printk_ratelimited("%sF2FS-fs (%s): %s zstd_init_cstream failed\n",
  288. KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
  289. __func__);
  290. kvfree(workspace);
  291. return -EIO;
  292. }
  293. cc->private = workspace;
  294. cc->private2 = stream;
  295. cc->clen = cc->rlen - PAGE_SIZE - COMPRESS_HEADER_SIZE;
  296. return 0;
  297. }
  298. static void zstd_destroy_compress_ctx(struct compress_ctx *cc)
  299. {
  300. kvfree(cc->private);
  301. cc->private = NULL;
  302. cc->private2 = NULL;
  303. }
  304. static int zstd_compress_pages(struct compress_ctx *cc)
  305. {
  306. zstd_cstream *stream = cc->private2;
  307. zstd_in_buffer inbuf;
  308. zstd_out_buffer outbuf;
  309. int src_size = cc->rlen;
  310. int dst_size = src_size - PAGE_SIZE - COMPRESS_HEADER_SIZE;
  311. int ret;
  312. inbuf.pos = 0;
  313. inbuf.src = cc->rbuf;
  314. inbuf.size = src_size;
  315. outbuf.pos = 0;
  316. outbuf.dst = cc->cbuf->cdata;
  317. outbuf.size = dst_size;
  318. ret = zstd_compress_stream(stream, &outbuf, &inbuf);
  319. if (zstd_is_error(ret)) {
  320. printk_ratelimited("%sF2FS-fs (%s): %s zstd_compress_stream failed, ret: %d\n",
  321. KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
  322. __func__, zstd_get_error_code(ret));
  323. return -EIO;
  324. }
  325. ret = zstd_end_stream(stream, &outbuf);
  326. if (zstd_is_error(ret)) {
  327. printk_ratelimited("%sF2FS-fs (%s): %s zstd_end_stream returned %d\n",
  328. KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
  329. __func__, zstd_get_error_code(ret));
  330. return -EIO;
  331. }
  332. /*
  333. * there is compressed data remained in intermediate buffer due to
  334. * no more space in cbuf.cdata
  335. */
  336. if (ret)
  337. return -EAGAIN;
  338. cc->clen = outbuf.pos;
  339. return 0;
  340. }
  341. static int zstd_init_decompress_ctx(struct decompress_io_ctx *dic)
  342. {
  343. zstd_dstream *stream;
  344. void *workspace;
  345. unsigned int workspace_size;
  346. unsigned int max_window_size =
  347. MAX_COMPRESS_WINDOW_SIZE(dic->log_cluster_size);
  348. workspace_size = zstd_dstream_workspace_bound(max_window_size);
  349. workspace = f2fs_kvmalloc(F2FS_I_SB(dic->inode),
  350. workspace_size, GFP_NOFS);
  351. if (!workspace)
  352. return -ENOMEM;
  353. stream = zstd_init_dstream(max_window_size, workspace, workspace_size);
  354. if (!stream) {
  355. printk_ratelimited("%sF2FS-fs (%s): %s zstd_init_dstream failed\n",
  356. KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id,
  357. __func__);
  358. kvfree(workspace);
  359. return -EIO;
  360. }
  361. dic->private = workspace;
  362. dic->private2 = stream;
  363. return 0;
  364. }
  365. static void zstd_destroy_decompress_ctx(struct decompress_io_ctx *dic)
  366. {
  367. kvfree(dic->private);
  368. dic->private = NULL;
  369. dic->private2 = NULL;
  370. }
  371. static int zstd_decompress_pages(struct decompress_io_ctx *dic)
  372. {
  373. zstd_dstream *stream = dic->private2;
  374. zstd_in_buffer inbuf;
  375. zstd_out_buffer outbuf;
  376. int ret;
  377. inbuf.pos = 0;
  378. inbuf.src = dic->cbuf->cdata;
  379. inbuf.size = dic->clen;
  380. outbuf.pos = 0;
  381. outbuf.dst = dic->rbuf;
  382. outbuf.size = dic->rlen;
  383. ret = zstd_decompress_stream(stream, &outbuf, &inbuf);
  384. if (zstd_is_error(ret)) {
  385. printk_ratelimited("%sF2FS-fs (%s): %s zstd_decompress_stream failed, ret: %d\n",
  386. KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id,
  387. __func__, zstd_get_error_code(ret));
  388. return -EIO;
  389. }
  390. if (dic->rlen != outbuf.pos) {
  391. printk_ratelimited("%sF2FS-fs (%s): %s ZSTD invalid rlen:%zu, "
  392. "expected:%lu\n", KERN_ERR,
  393. F2FS_I_SB(dic->inode)->sb->s_id,
  394. __func__, dic->rlen,
  395. PAGE_SIZE << dic->log_cluster_size);
  396. return -EIO;
  397. }
  398. return 0;
  399. }
  400. static const struct f2fs_compress_ops f2fs_zstd_ops = {
  401. .init_compress_ctx = zstd_init_compress_ctx,
  402. .destroy_compress_ctx = zstd_destroy_compress_ctx,
  403. .compress_pages = zstd_compress_pages,
  404. .init_decompress_ctx = zstd_init_decompress_ctx,
  405. .destroy_decompress_ctx = zstd_destroy_decompress_ctx,
  406. .decompress_pages = zstd_decompress_pages,
  407. };
  408. #endif
  409. #ifdef CONFIG_F2FS_FS_LZO
  410. #ifdef CONFIG_F2FS_FS_LZORLE
  411. static int lzorle_compress_pages(struct compress_ctx *cc)
  412. {
  413. int ret;
  414. ret = lzorle1x_1_compress(cc->rbuf, cc->rlen, cc->cbuf->cdata,
  415. &cc->clen, cc->private);
  416. if (ret != LZO_E_OK) {
  417. printk_ratelimited("%sF2FS-fs (%s): lzo-rle compress failed, ret:%d\n",
  418. KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id, ret);
  419. return -EIO;
  420. }
  421. return 0;
  422. }
  423. static const struct f2fs_compress_ops f2fs_lzorle_ops = {
  424. .init_compress_ctx = lzo_init_compress_ctx,
  425. .destroy_compress_ctx = lzo_destroy_compress_ctx,
  426. .compress_pages = lzorle_compress_pages,
  427. .decompress_pages = lzo_decompress_pages,
  428. };
  429. #endif
  430. #endif
  431. static const struct f2fs_compress_ops *f2fs_cops[COMPRESS_MAX] = {
  432. #ifdef CONFIG_F2FS_FS_LZO
  433. &f2fs_lzo_ops,
  434. #else
  435. NULL,
  436. #endif
  437. #ifdef CONFIG_F2FS_FS_LZ4
  438. &f2fs_lz4_ops,
  439. #else
  440. NULL,
  441. #endif
  442. #ifdef CONFIG_F2FS_FS_ZSTD
  443. &f2fs_zstd_ops,
  444. #else
  445. NULL,
  446. #endif
  447. #if defined(CONFIG_F2FS_FS_LZO) && defined(CONFIG_F2FS_FS_LZORLE)
  448. &f2fs_lzorle_ops,
  449. #else
  450. NULL,
  451. #endif
  452. };
  453. bool f2fs_is_compress_backend_ready(struct inode *inode)
  454. {
  455. if (!f2fs_compressed_file(inode))
  456. return true;
  457. return f2fs_cops[F2FS_I(inode)->i_compress_algorithm];
  458. }
  459. static mempool_t *compress_page_pool;
  460. static int num_compress_pages = 512;
  461. module_param(num_compress_pages, uint, 0444);
  462. MODULE_PARM_DESC(num_compress_pages,
  463. "Number of intermediate compress pages to preallocate");
  464. int __init f2fs_init_compress_mempool(void)
  465. {
  466. compress_page_pool = mempool_create_page_pool(num_compress_pages, 0);
  467. return compress_page_pool ? 0 : -ENOMEM;
  468. }
  469. void f2fs_destroy_compress_mempool(void)
  470. {
  471. mempool_destroy(compress_page_pool);
  472. }
  473. static struct page *f2fs_compress_alloc_page(void)
  474. {
  475. struct page *page;
  476. page = mempool_alloc(compress_page_pool, GFP_NOFS);
  477. lock_page(page);
  478. return page;
  479. }
  480. static void f2fs_compress_free_page(struct page *page)
  481. {
  482. if (!page)
  483. return;
  484. detach_page_private(page);
  485. page->mapping = NULL;
  486. unlock_page(page);
  487. mempool_free(page, compress_page_pool);
  488. }
  489. #define MAX_VMAP_RETRIES 3
  490. static void *f2fs_vmap(struct page **pages, unsigned int count)
  491. {
  492. int i;
  493. void *buf = NULL;
  494. for (i = 0; i < MAX_VMAP_RETRIES; i++) {
  495. buf = vm_map_ram(pages, count, -1);
  496. if (buf)
  497. break;
  498. vm_unmap_aliases();
  499. }
  500. return buf;
  501. }
  502. static int f2fs_compress_pages(struct compress_ctx *cc)
  503. {
  504. struct f2fs_inode_info *fi = F2FS_I(cc->inode);
  505. const struct f2fs_compress_ops *cops =
  506. f2fs_cops[fi->i_compress_algorithm];
  507. unsigned int max_len, new_nr_cpages;
  508. u32 chksum = 0;
  509. int i, ret;
  510. trace_f2fs_compress_pages_start(cc->inode, cc->cluster_idx,
  511. cc->cluster_size, fi->i_compress_algorithm);
  512. if (cops->init_compress_ctx) {
  513. ret = cops->init_compress_ctx(cc);
  514. if (ret)
  515. goto out;
  516. }
  517. max_len = COMPRESS_HEADER_SIZE + cc->clen;
  518. cc->nr_cpages = DIV_ROUND_UP(max_len, PAGE_SIZE);
  519. cc->valid_nr_cpages = cc->nr_cpages;
  520. cc->cpages = page_array_alloc(cc->inode, cc->nr_cpages);
  521. if (!cc->cpages) {
  522. ret = -ENOMEM;
  523. goto destroy_compress_ctx;
  524. }
  525. for (i = 0; i < cc->nr_cpages; i++) {
  526. cc->cpages[i] = f2fs_compress_alloc_page();
  527. if (!cc->cpages[i]) {
  528. ret = -ENOMEM;
  529. goto out_free_cpages;
  530. }
  531. }
  532. cc->rbuf = f2fs_vmap(cc->rpages, cc->cluster_size);
  533. if (!cc->rbuf) {
  534. ret = -ENOMEM;
  535. goto out_free_cpages;
  536. }
  537. cc->cbuf = f2fs_vmap(cc->cpages, cc->nr_cpages);
  538. if (!cc->cbuf) {
  539. ret = -ENOMEM;
  540. goto out_vunmap_rbuf;
  541. }
  542. ret = cops->compress_pages(cc);
  543. if (ret)
  544. goto out_vunmap_cbuf;
  545. max_len = PAGE_SIZE * (cc->cluster_size - 1) - COMPRESS_HEADER_SIZE;
  546. if (cc->clen > max_len) {
  547. ret = -EAGAIN;
  548. goto out_vunmap_cbuf;
  549. }
  550. cc->cbuf->clen = cpu_to_le32(cc->clen);
  551. if (fi->i_compress_flag & BIT(COMPRESS_CHKSUM))
  552. chksum = f2fs_crc32(F2FS_I_SB(cc->inode),
  553. cc->cbuf->cdata, cc->clen);
  554. cc->cbuf->chksum = cpu_to_le32(chksum);
  555. for (i = 0; i < COMPRESS_DATA_RESERVED_SIZE; i++)
  556. cc->cbuf->reserved[i] = cpu_to_le32(0);
  557. new_nr_cpages = DIV_ROUND_UP(cc->clen + COMPRESS_HEADER_SIZE, PAGE_SIZE);
  558. /* zero out any unused part of the last page */
  559. memset(&cc->cbuf->cdata[cc->clen], 0,
  560. (new_nr_cpages * PAGE_SIZE) -
  561. (cc->clen + COMPRESS_HEADER_SIZE));
  562. vm_unmap_ram(cc->cbuf, cc->nr_cpages);
  563. vm_unmap_ram(cc->rbuf, cc->cluster_size);
  564. for (i = new_nr_cpages; i < cc->nr_cpages; i++) {
  565. f2fs_compress_free_page(cc->cpages[i]);
  566. cc->cpages[i] = NULL;
  567. }
  568. if (cops->destroy_compress_ctx)
  569. cops->destroy_compress_ctx(cc);
  570. cc->valid_nr_cpages = new_nr_cpages;
  571. trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx,
  572. cc->clen, ret);
  573. return 0;
  574. out_vunmap_cbuf:
  575. vm_unmap_ram(cc->cbuf, cc->nr_cpages);
  576. out_vunmap_rbuf:
  577. vm_unmap_ram(cc->rbuf, cc->cluster_size);
  578. out_free_cpages:
  579. for (i = 0; i < cc->nr_cpages; i++) {
  580. if (cc->cpages[i])
  581. f2fs_compress_free_page(cc->cpages[i]);
  582. }
  583. page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
  584. cc->cpages = NULL;
  585. destroy_compress_ctx:
  586. if (cops->destroy_compress_ctx)
  587. cops->destroy_compress_ctx(cc);
  588. out:
  589. trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx,
  590. cc->clen, ret);
  591. return ret;
  592. }
  593. static int f2fs_prepare_decomp_mem(struct decompress_io_ctx *dic,
  594. bool pre_alloc);
  595. static void f2fs_release_decomp_mem(struct decompress_io_ctx *dic,
  596. bool bypass_destroy_callback, bool pre_alloc);
  597. void f2fs_decompress_cluster(struct decompress_io_ctx *dic, bool in_task)
  598. {
  599. struct f2fs_sb_info *sbi = F2FS_I_SB(dic->inode);
  600. struct f2fs_inode_info *fi = F2FS_I(dic->inode);
  601. const struct f2fs_compress_ops *cops =
  602. f2fs_cops[fi->i_compress_algorithm];
  603. bool bypass_callback = false;
  604. int ret;
  605. trace_f2fs_decompress_pages_start(dic->inode, dic->cluster_idx,
  606. dic->cluster_size, fi->i_compress_algorithm);
  607. if (dic->failed) {
  608. ret = -EIO;
  609. goto out_end_io;
  610. }
  611. ret = f2fs_prepare_decomp_mem(dic, false);
  612. if (ret) {
  613. bypass_callback = true;
  614. goto out_release;
  615. }
  616. dic->clen = le32_to_cpu(dic->cbuf->clen);
  617. dic->rlen = PAGE_SIZE << dic->log_cluster_size;
  618. if (dic->clen > PAGE_SIZE * dic->nr_cpages - COMPRESS_HEADER_SIZE) {
  619. ret = -EFSCORRUPTED;
  620. /* Avoid f2fs_commit_super in irq context */
  621. if (!in_task)
  622. f2fs_save_errors(sbi, ERROR_FAIL_DECOMPRESSION);
  623. else
  624. f2fs_handle_error(sbi, ERROR_FAIL_DECOMPRESSION);
  625. goto out_release;
  626. }
  627. ret = cops->decompress_pages(dic);
  628. if (!ret && (fi->i_compress_flag & BIT(COMPRESS_CHKSUM))) {
  629. u32 provided = le32_to_cpu(dic->cbuf->chksum);
  630. u32 calculated = f2fs_crc32(sbi, dic->cbuf->cdata, dic->clen);
  631. if (provided != calculated) {
  632. if (!is_inode_flag_set(dic->inode, FI_COMPRESS_CORRUPT)) {
  633. set_inode_flag(dic->inode, FI_COMPRESS_CORRUPT);
  634. printk_ratelimited(
  635. "%sF2FS-fs (%s): checksum invalid, nid = %lu, %x vs %x",
  636. KERN_INFO, sbi->sb->s_id, dic->inode->i_ino,
  637. provided, calculated);
  638. }
  639. set_sbi_flag(sbi, SBI_NEED_FSCK);
  640. }
  641. }
  642. out_release:
  643. f2fs_release_decomp_mem(dic, bypass_callback, false);
  644. out_end_io:
  645. trace_f2fs_decompress_pages_end(dic->inode, dic->cluster_idx,
  646. dic->clen, ret);
  647. f2fs_decompress_end_io(dic, ret, in_task);
  648. }
  649. /*
  650. * This is called when a page of a compressed cluster has been read from disk
  651. * (or failed to be read from disk). It checks whether this page was the last
  652. * page being waited on in the cluster, and if so, it decompresses the cluster
  653. * (or in the case of a failure, cleans up without actually decompressing).
  654. */
  655. void f2fs_end_read_compressed_page(struct page *page, bool failed,
  656. block_t blkaddr, bool in_task)
  657. {
  658. struct decompress_io_ctx *dic =
  659. (struct decompress_io_ctx *)page_private(page);
  660. struct f2fs_sb_info *sbi = F2FS_I_SB(dic->inode);
  661. dec_page_count(sbi, F2FS_RD_DATA);
  662. if (failed)
  663. WRITE_ONCE(dic->failed, true);
  664. else if (blkaddr && in_task)
  665. f2fs_cache_compressed_page(sbi, page,
  666. dic->inode->i_ino, blkaddr);
  667. if (atomic_dec_and_test(&dic->remaining_pages))
  668. f2fs_decompress_cluster(dic, in_task);
  669. }
  670. static bool is_page_in_cluster(struct compress_ctx *cc, pgoff_t index)
  671. {
  672. if (cc->cluster_idx == NULL_CLUSTER)
  673. return true;
  674. return cc->cluster_idx == cluster_idx(cc, index);
  675. }
  676. bool f2fs_cluster_is_empty(struct compress_ctx *cc)
  677. {
  678. return cc->nr_rpages == 0;
  679. }
  680. static bool f2fs_cluster_is_full(struct compress_ctx *cc)
  681. {
  682. return cc->cluster_size == cc->nr_rpages;
  683. }
  684. bool f2fs_cluster_can_merge_page(struct compress_ctx *cc, pgoff_t index)
  685. {
  686. if (f2fs_cluster_is_empty(cc))
  687. return true;
  688. return is_page_in_cluster(cc, index);
  689. }
  690. bool f2fs_all_cluster_page_ready(struct compress_ctx *cc, struct page **pages,
  691. int index, int nr_pages, bool uptodate)
  692. {
  693. unsigned long pgidx = pages[index]->index;
  694. int i = uptodate ? 0 : 1;
  695. /*
  696. * when uptodate set to true, try to check all pages in cluster is
  697. * uptodate or not.
  698. */
  699. if (uptodate && (pgidx % cc->cluster_size))
  700. return false;
  701. if (nr_pages - index < cc->cluster_size)
  702. return false;
  703. for (; i < cc->cluster_size; i++) {
  704. if (pages[index + i]->index != pgidx + i)
  705. return false;
  706. if (uptodate && !PageUptodate(pages[index + i]))
  707. return false;
  708. }
  709. return true;
  710. }
  711. static bool cluster_has_invalid_data(struct compress_ctx *cc)
  712. {
  713. loff_t i_size = i_size_read(cc->inode);
  714. unsigned nr_pages = DIV_ROUND_UP(i_size, PAGE_SIZE);
  715. int i;
  716. for (i = 0; i < cc->cluster_size; i++) {
  717. struct page *page = cc->rpages[i];
  718. f2fs_bug_on(F2FS_I_SB(cc->inode), !page);
  719. /* beyond EOF */
  720. if (page->index >= nr_pages)
  721. return true;
  722. }
  723. return false;
  724. }
  725. bool f2fs_sanity_check_cluster(struct dnode_of_data *dn)
  726. {
  727. struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
  728. unsigned int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
  729. bool compressed = dn->data_blkaddr == COMPRESS_ADDR;
  730. int cluster_end = 0;
  731. int i;
  732. char *reason = "";
  733. if (!compressed)
  734. return false;
  735. /* [..., COMPR_ADDR, ...] */
  736. if (dn->ofs_in_node % cluster_size) {
  737. reason = "[*|C|*|*]";
  738. goto out;
  739. }
  740. for (i = 1; i < cluster_size; i++) {
  741. block_t blkaddr = data_blkaddr(dn->inode, dn->node_page,
  742. dn->ofs_in_node + i);
  743. /* [COMPR_ADDR, ..., COMPR_ADDR] */
  744. if (blkaddr == COMPRESS_ADDR) {
  745. reason = "[C|*|C|*]";
  746. goto out;
  747. }
  748. if (!__is_valid_data_blkaddr(blkaddr)) {
  749. if (!cluster_end)
  750. cluster_end = i;
  751. continue;
  752. }
  753. /* [COMPR_ADDR, NULL_ADDR or NEW_ADDR, valid_blkaddr] */
  754. if (cluster_end) {
  755. reason = "[C|N|N|V]";
  756. goto out;
  757. }
  758. }
  759. return false;
  760. out:
  761. f2fs_warn(sbi, "access invalid cluster, ino:%lu, nid:%u, ofs_in_node:%u, reason:%s",
  762. dn->inode->i_ino, dn->nid, dn->ofs_in_node, reason);
  763. set_sbi_flag(sbi, SBI_NEED_FSCK);
  764. return true;
  765. }
  766. static int __f2fs_cluster_blocks(struct inode *inode,
  767. unsigned int cluster_idx, bool compr)
  768. {
  769. struct dnode_of_data dn;
  770. unsigned int cluster_size = F2FS_I(inode)->i_cluster_size;
  771. unsigned int start_idx = cluster_idx <<
  772. F2FS_I(inode)->i_log_cluster_size;
  773. int ret;
  774. set_new_dnode(&dn, inode, NULL, NULL, 0);
  775. ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
  776. if (ret) {
  777. if (ret == -ENOENT)
  778. ret = 0;
  779. goto fail;
  780. }
  781. if (f2fs_sanity_check_cluster(&dn)) {
  782. ret = -EFSCORRUPTED;
  783. f2fs_handle_error(F2FS_I_SB(inode), ERROR_CORRUPTED_CLUSTER);
  784. goto fail;
  785. }
  786. if (dn.data_blkaddr == COMPRESS_ADDR) {
  787. int i;
  788. ret = 1;
  789. for (i = 1; i < cluster_size; i++) {
  790. block_t blkaddr;
  791. blkaddr = data_blkaddr(dn.inode,
  792. dn.node_page, dn.ofs_in_node + i);
  793. if (compr) {
  794. if (__is_valid_data_blkaddr(blkaddr))
  795. ret++;
  796. } else {
  797. if (blkaddr != NULL_ADDR)
  798. ret++;
  799. }
  800. }
  801. f2fs_bug_on(F2FS_I_SB(inode),
  802. !compr && ret != cluster_size &&
  803. !is_inode_flag_set(inode, FI_COMPRESS_RELEASED));
  804. }
  805. fail:
  806. f2fs_put_dnode(&dn);
  807. return ret;
  808. }
  809. /* return # of compressed blocks in compressed cluster */
  810. static int f2fs_compressed_blocks(struct compress_ctx *cc)
  811. {
  812. return __f2fs_cluster_blocks(cc->inode, cc->cluster_idx, true);
  813. }
  814. /* return # of valid blocks in compressed cluster */
  815. int f2fs_is_compressed_cluster(struct inode *inode, pgoff_t index)
  816. {
  817. return __f2fs_cluster_blocks(inode,
  818. index >> F2FS_I(inode)->i_log_cluster_size,
  819. false);
  820. }
  821. static bool cluster_may_compress(struct compress_ctx *cc)
  822. {
  823. if (!f2fs_need_compress_data(cc->inode))
  824. return false;
  825. if (f2fs_is_atomic_file(cc->inode))
  826. return false;
  827. if (!f2fs_cluster_is_full(cc))
  828. return false;
  829. if (unlikely(f2fs_cp_error(F2FS_I_SB(cc->inode))))
  830. return false;
  831. return !cluster_has_invalid_data(cc);
  832. }
  833. static void set_cluster_writeback(struct compress_ctx *cc)
  834. {
  835. int i;
  836. for (i = 0; i < cc->cluster_size; i++) {
  837. if (cc->rpages[i])
  838. set_page_writeback(cc->rpages[i]);
  839. }
  840. }
  841. static void set_cluster_dirty(struct compress_ctx *cc)
  842. {
  843. int i;
  844. for (i = 0; i < cc->cluster_size; i++)
  845. if (cc->rpages[i])
  846. set_page_dirty(cc->rpages[i]);
  847. }
  848. static int prepare_compress_overwrite(struct compress_ctx *cc,
  849. struct page **pagep, pgoff_t index, void **fsdata)
  850. {
  851. struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
  852. struct address_space *mapping = cc->inode->i_mapping;
  853. struct page *page;
  854. sector_t last_block_in_bio;
  855. unsigned fgp_flag = FGP_LOCK | FGP_WRITE | FGP_CREAT;
  856. pgoff_t start_idx = start_idx_of_cluster(cc);
  857. int i, ret;
  858. retry:
  859. ret = f2fs_is_compressed_cluster(cc->inode, start_idx);
  860. if (ret <= 0)
  861. return ret;
  862. ret = f2fs_init_compress_ctx(cc);
  863. if (ret)
  864. return ret;
  865. /* keep page reference to avoid page reclaim */
  866. for (i = 0; i < cc->cluster_size; i++) {
  867. page = f2fs_pagecache_get_page(mapping, start_idx + i,
  868. fgp_flag, GFP_NOFS);
  869. if (!page) {
  870. ret = -ENOMEM;
  871. goto unlock_pages;
  872. }
  873. if (PageUptodate(page))
  874. f2fs_put_page(page, 1);
  875. else
  876. f2fs_compress_ctx_add_page(cc, page);
  877. }
  878. if (!f2fs_cluster_is_empty(cc)) {
  879. struct bio *bio = NULL;
  880. ret = f2fs_read_multi_pages(cc, &bio, cc->cluster_size,
  881. &last_block_in_bio, false, true);
  882. f2fs_put_rpages(cc);
  883. f2fs_destroy_compress_ctx(cc, true);
  884. if (ret)
  885. goto out;
  886. if (bio)
  887. f2fs_submit_read_bio(sbi, bio, DATA);
  888. ret = f2fs_init_compress_ctx(cc);
  889. if (ret)
  890. goto out;
  891. }
  892. for (i = 0; i < cc->cluster_size; i++) {
  893. f2fs_bug_on(sbi, cc->rpages[i]);
  894. page = find_lock_page(mapping, start_idx + i);
  895. if (!page) {
  896. /* page can be truncated */
  897. goto release_and_retry;
  898. }
  899. f2fs_wait_on_page_writeback(page, DATA, true, true);
  900. f2fs_compress_ctx_add_page(cc, page);
  901. if (!PageUptodate(page)) {
  902. release_and_retry:
  903. f2fs_put_rpages(cc);
  904. f2fs_unlock_rpages(cc, i + 1);
  905. f2fs_destroy_compress_ctx(cc, true);
  906. goto retry;
  907. }
  908. }
  909. if (likely(!ret)) {
  910. *fsdata = cc->rpages;
  911. *pagep = cc->rpages[offset_in_cluster(cc, index)];
  912. return cc->cluster_size;
  913. }
  914. unlock_pages:
  915. f2fs_put_rpages(cc);
  916. f2fs_unlock_rpages(cc, i);
  917. f2fs_destroy_compress_ctx(cc, true);
  918. out:
  919. return ret;
  920. }
  921. int f2fs_prepare_compress_overwrite(struct inode *inode,
  922. struct page **pagep, pgoff_t index, void **fsdata)
  923. {
  924. struct compress_ctx cc = {
  925. .inode = inode,
  926. .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
  927. .cluster_size = F2FS_I(inode)->i_cluster_size,
  928. .cluster_idx = index >> F2FS_I(inode)->i_log_cluster_size,
  929. .rpages = NULL,
  930. .nr_rpages = 0,
  931. };
  932. return prepare_compress_overwrite(&cc, pagep, index, fsdata);
  933. }
  934. bool f2fs_compress_write_end(struct inode *inode, void *fsdata,
  935. pgoff_t index, unsigned copied)
  936. {
  937. struct compress_ctx cc = {
  938. .inode = inode,
  939. .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
  940. .cluster_size = F2FS_I(inode)->i_cluster_size,
  941. .rpages = fsdata,
  942. };
  943. bool first_index = (index == cc.rpages[0]->index);
  944. if (copied)
  945. set_cluster_dirty(&cc);
  946. f2fs_put_rpages_wbc(&cc, NULL, false, 1);
  947. f2fs_destroy_compress_ctx(&cc, false);
  948. return first_index;
  949. }
  950. int f2fs_truncate_partial_cluster(struct inode *inode, u64 from, bool lock)
  951. {
  952. void *fsdata = NULL;
  953. struct page *pagep;
  954. int log_cluster_size = F2FS_I(inode)->i_log_cluster_size;
  955. pgoff_t start_idx = from >> (PAGE_SHIFT + log_cluster_size) <<
  956. log_cluster_size;
  957. int err;
  958. err = f2fs_is_compressed_cluster(inode, start_idx);
  959. if (err < 0)
  960. return err;
  961. /* truncate normal cluster */
  962. if (!err)
  963. return f2fs_do_truncate_blocks(inode, from, lock);
  964. /* truncate compressed cluster */
  965. err = f2fs_prepare_compress_overwrite(inode, &pagep,
  966. start_idx, &fsdata);
  967. /* should not be a normal cluster */
  968. f2fs_bug_on(F2FS_I_SB(inode), err == 0);
  969. if (err <= 0)
  970. return err;
  971. if (err > 0) {
  972. struct page **rpages = fsdata;
  973. int cluster_size = F2FS_I(inode)->i_cluster_size;
  974. int i;
  975. for (i = cluster_size - 1; i >= 0; i--) {
  976. loff_t start = rpages[i]->index << PAGE_SHIFT;
  977. if (from <= start) {
  978. zero_user_segment(rpages[i], 0, PAGE_SIZE);
  979. } else {
  980. zero_user_segment(rpages[i], from - start,
  981. PAGE_SIZE);
  982. break;
  983. }
  984. }
  985. f2fs_compress_write_end(inode, fsdata, start_idx, true);
  986. }
  987. return 0;
  988. }
  989. static int f2fs_write_compressed_pages(struct compress_ctx *cc,
  990. int *submitted,
  991. struct writeback_control *wbc,
  992. enum iostat_type io_type)
  993. {
  994. struct inode *inode = cc->inode;
  995. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  996. struct f2fs_inode_info *fi = F2FS_I(inode);
  997. struct f2fs_io_info fio = {
  998. .sbi = sbi,
  999. .ino = cc->inode->i_ino,
  1000. .type = DATA,
  1001. .op = REQ_OP_WRITE,
  1002. .op_flags = wbc_to_write_flags(wbc),
  1003. .old_blkaddr = NEW_ADDR,
  1004. .page = NULL,
  1005. .encrypted_page = NULL,
  1006. .compressed_page = NULL,
  1007. .submitted = 0,
  1008. .io_type = io_type,
  1009. .io_wbc = wbc,
  1010. .encrypted = fscrypt_inode_uses_fs_layer_crypto(cc->inode) ?
  1011. 1 : 0,
  1012. };
  1013. struct dnode_of_data dn;
  1014. struct node_info ni;
  1015. struct compress_io_ctx *cic;
  1016. pgoff_t start_idx = start_idx_of_cluster(cc);
  1017. unsigned int last_index = cc->cluster_size - 1;
  1018. loff_t psize;
  1019. int i, err;
  1020. bool quota_inode = IS_NOQUOTA(inode);
  1021. /* we should bypass data pages to proceed the kworker jobs */
  1022. if (unlikely(f2fs_cp_error(sbi))) {
  1023. mapping_set_error(cc->rpages[0]->mapping, -EIO);
  1024. goto out_free;
  1025. }
  1026. if (quota_inode) {
  1027. /*
  1028. * We need to wait for node_write to avoid block allocation during
  1029. * checkpoint. This can only happen to quota writes which can cause
  1030. * the below discard race condition.
  1031. */
  1032. f2fs_down_read(&sbi->node_write);
  1033. } else if (!f2fs_trylock_op(sbi)) {
  1034. goto out_free;
  1035. }
  1036. set_new_dnode(&dn, cc->inode, NULL, NULL, 0);
  1037. err = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
  1038. if (err)
  1039. goto out_unlock_op;
  1040. for (i = 0; i < cc->cluster_size; i++) {
  1041. if (data_blkaddr(dn.inode, dn.node_page,
  1042. dn.ofs_in_node + i) == NULL_ADDR)
  1043. goto out_put_dnode;
  1044. }
  1045. psize = (loff_t)(cc->rpages[last_index]->index + 1) << PAGE_SHIFT;
  1046. err = f2fs_get_node_info(fio.sbi, dn.nid, &ni, false);
  1047. if (err)
  1048. goto out_put_dnode;
  1049. fio.version = ni.version;
  1050. cic = f2fs_kmem_cache_alloc(cic_entry_slab, GFP_F2FS_ZERO, false, sbi);
  1051. if (!cic)
  1052. goto out_put_dnode;
  1053. cic->magic = F2FS_COMPRESSED_PAGE_MAGIC;
  1054. cic->inode = inode;
  1055. atomic_set(&cic->pending_pages, cc->valid_nr_cpages);
  1056. cic->rpages = page_array_alloc(cc->inode, cc->cluster_size);
  1057. if (!cic->rpages)
  1058. goto out_put_cic;
  1059. cic->nr_rpages = cc->cluster_size;
  1060. for (i = 0; i < cc->valid_nr_cpages; i++) {
  1061. f2fs_set_compressed_page(cc->cpages[i], inode,
  1062. cc->rpages[i + 1]->index, cic);
  1063. fio.compressed_page = cc->cpages[i];
  1064. fio.old_blkaddr = data_blkaddr(dn.inode, dn.node_page,
  1065. dn.ofs_in_node + i + 1);
  1066. /* wait for GCed page writeback via META_MAPPING */
  1067. f2fs_wait_on_block_writeback(inode, fio.old_blkaddr);
  1068. if (fio.encrypted) {
  1069. fio.page = cc->rpages[i + 1];
  1070. err = f2fs_encrypt_one_page(&fio);
  1071. if (err)
  1072. goto out_destroy_crypt;
  1073. cc->cpages[i] = fio.encrypted_page;
  1074. }
  1075. }
  1076. set_cluster_writeback(cc);
  1077. for (i = 0; i < cc->cluster_size; i++)
  1078. cic->rpages[i] = cc->rpages[i];
  1079. for (i = 0; i < cc->cluster_size; i++, dn.ofs_in_node++) {
  1080. block_t blkaddr;
  1081. blkaddr = f2fs_data_blkaddr(&dn);
  1082. fio.page = cc->rpages[i];
  1083. fio.old_blkaddr = blkaddr;
  1084. /* cluster header */
  1085. if (i == 0) {
  1086. if (blkaddr == COMPRESS_ADDR)
  1087. fio.compr_blocks++;
  1088. if (__is_valid_data_blkaddr(blkaddr))
  1089. f2fs_invalidate_blocks(sbi, blkaddr);
  1090. f2fs_update_data_blkaddr(&dn, COMPRESS_ADDR);
  1091. goto unlock_continue;
  1092. }
  1093. if (fio.compr_blocks && __is_valid_data_blkaddr(blkaddr))
  1094. fio.compr_blocks++;
  1095. if (i > cc->valid_nr_cpages) {
  1096. if (__is_valid_data_blkaddr(blkaddr)) {
  1097. f2fs_invalidate_blocks(sbi, blkaddr);
  1098. f2fs_update_data_blkaddr(&dn, NEW_ADDR);
  1099. }
  1100. goto unlock_continue;
  1101. }
  1102. f2fs_bug_on(fio.sbi, blkaddr == NULL_ADDR);
  1103. if (fio.encrypted)
  1104. fio.encrypted_page = cc->cpages[i - 1];
  1105. else
  1106. fio.compressed_page = cc->cpages[i - 1];
  1107. cc->cpages[i - 1] = NULL;
  1108. f2fs_outplace_write_data(&dn, &fio);
  1109. (*submitted)++;
  1110. unlock_continue:
  1111. inode_dec_dirty_pages(cc->inode);
  1112. unlock_page(fio.page);
  1113. }
  1114. if (fio.compr_blocks)
  1115. f2fs_i_compr_blocks_update(inode, fio.compr_blocks - 1, false);
  1116. f2fs_i_compr_blocks_update(inode, cc->valid_nr_cpages, true);
  1117. add_compr_block_stat(inode, cc->valid_nr_cpages);
  1118. set_inode_flag(cc->inode, FI_APPEND_WRITE);
  1119. if (cc->cluster_idx == 0)
  1120. set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
  1121. f2fs_put_dnode(&dn);
  1122. if (quota_inode)
  1123. f2fs_up_read(&sbi->node_write);
  1124. else
  1125. f2fs_unlock_op(sbi);
  1126. spin_lock(&fi->i_size_lock);
  1127. if (fi->last_disk_size < psize)
  1128. fi->last_disk_size = psize;
  1129. spin_unlock(&fi->i_size_lock);
  1130. f2fs_put_rpages(cc);
  1131. page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
  1132. cc->cpages = NULL;
  1133. f2fs_destroy_compress_ctx(cc, false);
  1134. return 0;
  1135. out_destroy_crypt:
  1136. page_array_free(cc->inode, cic->rpages, cc->cluster_size);
  1137. for (--i; i >= 0; i--)
  1138. fscrypt_finalize_bounce_page(&cc->cpages[i]);
  1139. out_put_cic:
  1140. kmem_cache_free(cic_entry_slab, cic);
  1141. out_put_dnode:
  1142. f2fs_put_dnode(&dn);
  1143. out_unlock_op:
  1144. if (quota_inode)
  1145. f2fs_up_read(&sbi->node_write);
  1146. else
  1147. f2fs_unlock_op(sbi);
  1148. out_free:
  1149. for (i = 0; i < cc->valid_nr_cpages; i++) {
  1150. f2fs_compress_free_page(cc->cpages[i]);
  1151. cc->cpages[i] = NULL;
  1152. }
  1153. page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
  1154. cc->cpages = NULL;
  1155. return -EAGAIN;
  1156. }
  1157. void f2fs_compress_write_end_io(struct bio *bio, struct page *page)
  1158. {
  1159. struct f2fs_sb_info *sbi = bio->bi_private;
  1160. struct compress_io_ctx *cic =
  1161. (struct compress_io_ctx *)page_private(page);
  1162. enum count_type type = WB_DATA_TYPE(page,
  1163. f2fs_is_compressed_page(page));
  1164. int i;
  1165. if (unlikely(bio->bi_status))
  1166. mapping_set_error(cic->inode->i_mapping, -EIO);
  1167. f2fs_compress_free_page(page);
  1168. dec_page_count(sbi, type);
  1169. if (atomic_dec_return(&cic->pending_pages))
  1170. return;
  1171. for (i = 0; i < cic->nr_rpages; i++) {
  1172. WARN_ON(!cic->rpages[i]);
  1173. clear_page_private_gcing(cic->rpages[i]);
  1174. end_page_writeback(cic->rpages[i]);
  1175. }
  1176. page_array_free(cic->inode, cic->rpages, cic->nr_rpages);
  1177. kmem_cache_free(cic_entry_slab, cic);
  1178. }
  1179. static int f2fs_write_raw_pages(struct compress_ctx *cc,
  1180. int *submitted_p,
  1181. struct writeback_control *wbc,
  1182. enum iostat_type io_type)
  1183. {
  1184. struct address_space *mapping = cc->inode->i_mapping;
  1185. struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
  1186. int submitted, compr_blocks, i;
  1187. int ret = 0;
  1188. compr_blocks = f2fs_compressed_blocks(cc);
  1189. for (i = 0; i < cc->cluster_size; i++) {
  1190. if (!cc->rpages[i])
  1191. continue;
  1192. redirty_page_for_writepage(wbc, cc->rpages[i]);
  1193. unlock_page(cc->rpages[i]);
  1194. }
  1195. if (compr_blocks < 0)
  1196. return compr_blocks;
  1197. /* overwrite compressed cluster w/ normal cluster */
  1198. if (compr_blocks > 0)
  1199. f2fs_lock_op(sbi);
  1200. for (i = 0; i < cc->cluster_size; i++) {
  1201. if (!cc->rpages[i])
  1202. continue;
  1203. retry_write:
  1204. lock_page(cc->rpages[i]);
  1205. if (cc->rpages[i]->mapping != mapping) {
  1206. continue_unlock:
  1207. unlock_page(cc->rpages[i]);
  1208. continue;
  1209. }
  1210. if (!PageDirty(cc->rpages[i]))
  1211. goto continue_unlock;
  1212. if (PageWriteback(cc->rpages[i])) {
  1213. if (wbc->sync_mode == WB_SYNC_NONE)
  1214. goto continue_unlock;
  1215. f2fs_wait_on_page_writeback(cc->rpages[i], DATA, true, true);
  1216. }
  1217. if (!clear_page_dirty_for_io(cc->rpages[i]))
  1218. goto continue_unlock;
  1219. ret = f2fs_write_single_data_page(cc->rpages[i], &submitted,
  1220. NULL, NULL, wbc, io_type,
  1221. compr_blocks, false);
  1222. if (ret) {
  1223. if (ret == AOP_WRITEPAGE_ACTIVATE) {
  1224. unlock_page(cc->rpages[i]);
  1225. ret = 0;
  1226. } else if (ret == -EAGAIN) {
  1227. ret = 0;
  1228. /*
  1229. * for quota file, just redirty left pages to
  1230. * avoid deadlock caused by cluster update race
  1231. * from foreground operation.
  1232. */
  1233. if (IS_NOQUOTA(cc->inode))
  1234. goto out;
  1235. f2fs_io_schedule_timeout(DEFAULT_IO_TIMEOUT);
  1236. goto retry_write;
  1237. }
  1238. goto out;
  1239. }
  1240. *submitted_p += submitted;
  1241. }
  1242. out:
  1243. if (compr_blocks > 0)
  1244. f2fs_unlock_op(sbi);
  1245. f2fs_balance_fs(sbi, true);
  1246. return ret;
  1247. }
  1248. int f2fs_write_multi_pages(struct compress_ctx *cc,
  1249. int *submitted,
  1250. struct writeback_control *wbc,
  1251. enum iostat_type io_type)
  1252. {
  1253. int err;
  1254. *submitted = 0;
  1255. if (cluster_may_compress(cc)) {
  1256. err = f2fs_compress_pages(cc);
  1257. if (err == -EAGAIN) {
  1258. add_compr_block_stat(cc->inode, cc->cluster_size);
  1259. goto write;
  1260. } else if (err) {
  1261. f2fs_put_rpages_wbc(cc, wbc, true, 1);
  1262. goto destroy_out;
  1263. }
  1264. err = f2fs_write_compressed_pages(cc, submitted,
  1265. wbc, io_type);
  1266. if (!err)
  1267. return 0;
  1268. f2fs_bug_on(F2FS_I_SB(cc->inode), err != -EAGAIN);
  1269. }
  1270. write:
  1271. f2fs_bug_on(F2FS_I_SB(cc->inode), *submitted);
  1272. err = f2fs_write_raw_pages(cc, submitted, wbc, io_type);
  1273. f2fs_put_rpages_wbc(cc, wbc, false, 0);
  1274. destroy_out:
  1275. f2fs_destroy_compress_ctx(cc, false);
  1276. return err;
  1277. }
  1278. static inline bool allow_memalloc_for_decomp(struct f2fs_sb_info *sbi,
  1279. bool pre_alloc)
  1280. {
  1281. return pre_alloc ^ f2fs_low_mem_mode(sbi);
  1282. }
  1283. static int f2fs_prepare_decomp_mem(struct decompress_io_ctx *dic,
  1284. bool pre_alloc)
  1285. {
  1286. const struct f2fs_compress_ops *cops =
  1287. f2fs_cops[F2FS_I(dic->inode)->i_compress_algorithm];
  1288. int i;
  1289. if (!allow_memalloc_for_decomp(F2FS_I_SB(dic->inode), pre_alloc))
  1290. return 0;
  1291. dic->tpages = page_array_alloc(dic->inode, dic->cluster_size);
  1292. if (!dic->tpages)
  1293. return -ENOMEM;
  1294. for (i = 0; i < dic->cluster_size; i++) {
  1295. if (dic->rpages[i]) {
  1296. dic->tpages[i] = dic->rpages[i];
  1297. continue;
  1298. }
  1299. dic->tpages[i] = f2fs_compress_alloc_page();
  1300. if (!dic->tpages[i])
  1301. return -ENOMEM;
  1302. }
  1303. dic->rbuf = f2fs_vmap(dic->tpages, dic->cluster_size);
  1304. if (!dic->rbuf)
  1305. return -ENOMEM;
  1306. dic->cbuf = f2fs_vmap(dic->cpages, dic->nr_cpages);
  1307. if (!dic->cbuf)
  1308. return -ENOMEM;
  1309. if (cops->init_decompress_ctx)
  1310. return cops->init_decompress_ctx(dic);
  1311. return 0;
  1312. }
  1313. static void f2fs_release_decomp_mem(struct decompress_io_ctx *dic,
  1314. bool bypass_destroy_callback, bool pre_alloc)
  1315. {
  1316. const struct f2fs_compress_ops *cops =
  1317. f2fs_cops[F2FS_I(dic->inode)->i_compress_algorithm];
  1318. if (!allow_memalloc_for_decomp(F2FS_I_SB(dic->inode), pre_alloc))
  1319. return;
  1320. if (!bypass_destroy_callback && cops->destroy_decompress_ctx)
  1321. cops->destroy_decompress_ctx(dic);
  1322. if (dic->cbuf)
  1323. vm_unmap_ram(dic->cbuf, dic->nr_cpages);
  1324. if (dic->rbuf)
  1325. vm_unmap_ram(dic->rbuf, dic->cluster_size);
  1326. }
  1327. static void f2fs_free_dic(struct decompress_io_ctx *dic,
  1328. bool bypass_destroy_callback);
  1329. struct decompress_io_ctx *f2fs_alloc_dic(struct compress_ctx *cc)
  1330. {
  1331. struct decompress_io_ctx *dic;
  1332. pgoff_t start_idx = start_idx_of_cluster(cc);
  1333. struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
  1334. int i, ret;
  1335. dic = f2fs_kmem_cache_alloc(dic_entry_slab, GFP_F2FS_ZERO, false, sbi);
  1336. if (!dic)
  1337. return ERR_PTR(-ENOMEM);
  1338. dic->rpages = page_array_alloc(cc->inode, cc->cluster_size);
  1339. if (!dic->rpages) {
  1340. kmem_cache_free(dic_entry_slab, dic);
  1341. return ERR_PTR(-ENOMEM);
  1342. }
  1343. dic->magic = F2FS_COMPRESSED_PAGE_MAGIC;
  1344. dic->inode = cc->inode;
  1345. atomic_set(&dic->remaining_pages, cc->nr_cpages);
  1346. dic->cluster_idx = cc->cluster_idx;
  1347. dic->cluster_size = cc->cluster_size;
  1348. dic->log_cluster_size = cc->log_cluster_size;
  1349. dic->nr_cpages = cc->nr_cpages;
  1350. refcount_set(&dic->refcnt, 1);
  1351. dic->failed = false;
  1352. dic->need_verity = f2fs_need_verity(cc->inode, start_idx);
  1353. for (i = 0; i < dic->cluster_size; i++)
  1354. dic->rpages[i] = cc->rpages[i];
  1355. dic->nr_rpages = cc->cluster_size;
  1356. dic->cpages = page_array_alloc(dic->inode, dic->nr_cpages);
  1357. if (!dic->cpages) {
  1358. ret = -ENOMEM;
  1359. goto out_free;
  1360. }
  1361. for (i = 0; i < dic->nr_cpages; i++) {
  1362. struct page *page;
  1363. page = f2fs_compress_alloc_page();
  1364. if (!page) {
  1365. ret = -ENOMEM;
  1366. goto out_free;
  1367. }
  1368. f2fs_set_compressed_page(page, cc->inode,
  1369. start_idx + i + 1, dic);
  1370. dic->cpages[i] = page;
  1371. }
  1372. ret = f2fs_prepare_decomp_mem(dic, true);
  1373. if (ret)
  1374. goto out_free;
  1375. return dic;
  1376. out_free:
  1377. f2fs_free_dic(dic, true);
  1378. return ERR_PTR(ret);
  1379. }
  1380. static void f2fs_free_dic(struct decompress_io_ctx *dic,
  1381. bool bypass_destroy_callback)
  1382. {
  1383. int i;
  1384. f2fs_release_decomp_mem(dic, bypass_destroy_callback, true);
  1385. if (dic->tpages) {
  1386. for (i = 0; i < dic->cluster_size; i++) {
  1387. if (dic->rpages[i])
  1388. continue;
  1389. if (!dic->tpages[i])
  1390. continue;
  1391. f2fs_compress_free_page(dic->tpages[i]);
  1392. }
  1393. page_array_free(dic->inode, dic->tpages, dic->cluster_size);
  1394. }
  1395. if (dic->cpages) {
  1396. for (i = 0; i < dic->nr_cpages; i++) {
  1397. if (!dic->cpages[i])
  1398. continue;
  1399. f2fs_compress_free_page(dic->cpages[i]);
  1400. }
  1401. page_array_free(dic->inode, dic->cpages, dic->nr_cpages);
  1402. }
  1403. page_array_free(dic->inode, dic->rpages, dic->nr_rpages);
  1404. kmem_cache_free(dic_entry_slab, dic);
  1405. }
  1406. static void f2fs_late_free_dic(struct work_struct *work)
  1407. {
  1408. struct decompress_io_ctx *dic =
  1409. container_of(work, struct decompress_io_ctx, free_work);
  1410. f2fs_free_dic(dic, false);
  1411. }
  1412. static void f2fs_put_dic(struct decompress_io_ctx *dic, bool in_task)
  1413. {
  1414. if (refcount_dec_and_test(&dic->refcnt)) {
  1415. if (in_task) {
  1416. f2fs_free_dic(dic, false);
  1417. } else {
  1418. INIT_WORK(&dic->free_work, f2fs_late_free_dic);
  1419. queue_work(F2FS_I_SB(dic->inode)->post_read_wq,
  1420. &dic->free_work);
  1421. }
  1422. }
  1423. }
  1424. static void f2fs_verify_cluster(struct work_struct *work)
  1425. {
  1426. struct decompress_io_ctx *dic =
  1427. container_of(work, struct decompress_io_ctx, verity_work);
  1428. int i;
  1429. /* Verify, update, and unlock the decompressed pages. */
  1430. for (i = 0; i < dic->cluster_size; i++) {
  1431. struct page *rpage = dic->rpages[i];
  1432. if (!rpage)
  1433. continue;
  1434. if (fsverity_verify_page(rpage))
  1435. SetPageUptodate(rpage);
  1436. else
  1437. ClearPageUptodate(rpage);
  1438. unlock_page(rpage);
  1439. }
  1440. f2fs_put_dic(dic, true);
  1441. }
  1442. /*
  1443. * This is called when a compressed cluster has been decompressed
  1444. * (or failed to be read and/or decompressed).
  1445. */
  1446. void f2fs_decompress_end_io(struct decompress_io_ctx *dic, bool failed,
  1447. bool in_task)
  1448. {
  1449. int i;
  1450. if (!failed && dic->need_verity) {
  1451. /*
  1452. * Note that to avoid deadlocks, the verity work can't be done
  1453. * on the decompression workqueue. This is because verifying
  1454. * the data pages can involve reading metadata pages from the
  1455. * file, and these metadata pages may be compressed.
  1456. */
  1457. INIT_WORK(&dic->verity_work, f2fs_verify_cluster);
  1458. fsverity_enqueue_verify_work(&dic->verity_work);
  1459. return;
  1460. }
  1461. /* Update and unlock the cluster's pagecache pages. */
  1462. for (i = 0; i < dic->cluster_size; i++) {
  1463. struct page *rpage = dic->rpages[i];
  1464. if (!rpage)
  1465. continue;
  1466. if (failed)
  1467. ClearPageUptodate(rpage);
  1468. else
  1469. SetPageUptodate(rpage);
  1470. unlock_page(rpage);
  1471. }
  1472. /*
  1473. * Release the reference to the decompress_io_ctx that was being held
  1474. * for I/O completion.
  1475. */
  1476. f2fs_put_dic(dic, in_task);
  1477. }
  1478. /*
  1479. * Put a reference to a compressed page's decompress_io_ctx.
  1480. *
  1481. * This is called when the page is no longer needed and can be freed.
  1482. */
  1483. void f2fs_put_page_dic(struct page *page, bool in_task)
  1484. {
  1485. struct decompress_io_ctx *dic =
  1486. (struct decompress_io_ctx *)page_private(page);
  1487. f2fs_put_dic(dic, in_task);
  1488. }
  1489. /*
  1490. * check whether cluster blocks are contiguous, and add extent cache entry
  1491. * only if cluster blocks are logically and physically contiguous.
  1492. */
  1493. unsigned int f2fs_cluster_blocks_are_contiguous(struct dnode_of_data *dn)
  1494. {
  1495. bool compressed = f2fs_data_blkaddr(dn) == COMPRESS_ADDR;
  1496. int i = compressed ? 1 : 0;
  1497. block_t first_blkaddr = data_blkaddr(dn->inode, dn->node_page,
  1498. dn->ofs_in_node + i);
  1499. for (i += 1; i < F2FS_I(dn->inode)->i_cluster_size; i++) {
  1500. block_t blkaddr = data_blkaddr(dn->inode, dn->node_page,
  1501. dn->ofs_in_node + i);
  1502. if (!__is_valid_data_blkaddr(blkaddr))
  1503. break;
  1504. if (first_blkaddr + i - (compressed ? 1 : 0) != blkaddr)
  1505. return 0;
  1506. }
  1507. return compressed ? i - 1 : i;
  1508. }
  1509. const struct address_space_operations f2fs_compress_aops = {
  1510. .release_folio = f2fs_release_folio,
  1511. .invalidate_folio = f2fs_invalidate_folio,
  1512. .migrate_folio = filemap_migrate_folio,
  1513. };
  1514. struct address_space *COMPRESS_MAPPING(struct f2fs_sb_info *sbi)
  1515. {
  1516. return sbi->compress_inode->i_mapping;
  1517. }
  1518. void f2fs_invalidate_compress_page(struct f2fs_sb_info *sbi, block_t blkaddr)
  1519. {
  1520. if (!sbi->compress_inode)
  1521. return;
  1522. invalidate_mapping_pages(COMPRESS_MAPPING(sbi), blkaddr, blkaddr);
  1523. }
  1524. void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi, struct page *page,
  1525. nid_t ino, block_t blkaddr)
  1526. {
  1527. struct page *cpage;
  1528. int ret;
  1529. if (!test_opt(sbi, COMPRESS_CACHE))
  1530. return;
  1531. if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE_READ))
  1532. return;
  1533. if (!f2fs_available_free_memory(sbi, COMPRESS_PAGE))
  1534. return;
  1535. cpage = find_get_page(COMPRESS_MAPPING(sbi), blkaddr);
  1536. if (cpage) {
  1537. f2fs_put_page(cpage, 0);
  1538. return;
  1539. }
  1540. cpage = alloc_page(__GFP_NOWARN | __GFP_IO);
  1541. if (!cpage)
  1542. return;
  1543. ret = add_to_page_cache_lru(cpage, COMPRESS_MAPPING(sbi),
  1544. blkaddr, GFP_NOFS);
  1545. if (ret) {
  1546. f2fs_put_page(cpage, 0);
  1547. return;
  1548. }
  1549. set_page_private_data(cpage, ino);
  1550. if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE_READ))
  1551. goto out;
  1552. memcpy(page_address(cpage), page_address(page), PAGE_SIZE);
  1553. SetPageUptodate(cpage);
  1554. out:
  1555. f2fs_put_page(cpage, 1);
  1556. }
  1557. bool f2fs_load_compressed_page(struct f2fs_sb_info *sbi, struct page *page,
  1558. block_t blkaddr)
  1559. {
  1560. struct page *cpage;
  1561. bool hitted = false;
  1562. if (!test_opt(sbi, COMPRESS_CACHE))
  1563. return false;
  1564. cpage = f2fs_pagecache_get_page(COMPRESS_MAPPING(sbi),
  1565. blkaddr, FGP_LOCK | FGP_NOWAIT, GFP_NOFS);
  1566. if (cpage) {
  1567. if (PageUptodate(cpage)) {
  1568. atomic_inc(&sbi->compress_page_hit);
  1569. memcpy(page_address(page),
  1570. page_address(cpage), PAGE_SIZE);
  1571. hitted = true;
  1572. }
  1573. f2fs_put_page(cpage, 1);
  1574. }
  1575. return hitted;
  1576. }
  1577. void f2fs_invalidate_compress_pages(struct f2fs_sb_info *sbi, nid_t ino)
  1578. {
  1579. struct address_space *mapping = COMPRESS_MAPPING(sbi);
  1580. struct folio_batch fbatch;
  1581. pgoff_t index = 0;
  1582. pgoff_t end = MAX_BLKADDR(sbi);
  1583. if (!mapping->nrpages)
  1584. return;
  1585. folio_batch_init(&fbatch);
  1586. do {
  1587. unsigned int nr, i;
  1588. nr = filemap_get_folios(mapping, &index, end - 1, &fbatch);
  1589. if (!nr)
  1590. break;
  1591. for (i = 0; i < nr; i++) {
  1592. struct folio *folio = fbatch.folios[i];
  1593. folio_lock(folio);
  1594. if (folio->mapping != mapping) {
  1595. folio_unlock(folio);
  1596. continue;
  1597. }
  1598. if (ino != get_page_private_data(&folio->page)) {
  1599. folio_unlock(folio);
  1600. continue;
  1601. }
  1602. generic_error_remove_page(mapping, &folio->page);
  1603. folio_unlock(folio);
  1604. }
  1605. folio_batch_release(&fbatch);
  1606. cond_resched();
  1607. } while (index < end);
  1608. }
  1609. int f2fs_init_compress_inode(struct f2fs_sb_info *sbi)
  1610. {
  1611. struct inode *inode;
  1612. if (!test_opt(sbi, COMPRESS_CACHE))
  1613. return 0;
  1614. inode = f2fs_iget(sbi->sb, F2FS_COMPRESS_INO(sbi));
  1615. if (IS_ERR(inode))
  1616. return PTR_ERR(inode);
  1617. sbi->compress_inode = inode;
  1618. sbi->compress_percent = COMPRESS_PERCENT;
  1619. sbi->compress_watermark = COMPRESS_WATERMARK;
  1620. atomic_set(&sbi->compress_page_hit, 0);
  1621. return 0;
  1622. }
  1623. void f2fs_destroy_compress_inode(struct f2fs_sb_info *sbi)
  1624. {
  1625. if (!sbi->compress_inode)
  1626. return;
  1627. iput(sbi->compress_inode);
  1628. sbi->compress_inode = NULL;
  1629. }
  1630. int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi)
  1631. {
  1632. dev_t dev = sbi->sb->s_bdev->bd_dev;
  1633. char slab_name[35];
  1634. if (!f2fs_sb_has_compression(sbi))
  1635. return 0;
  1636. sprintf(slab_name, "f2fs_page_array_entry-%u:%u", MAJOR(dev), MINOR(dev));
  1637. sbi->page_array_slab_size = sizeof(struct page *) <<
  1638. F2FS_OPTION(sbi).compress_log_size;
  1639. sbi->page_array_slab = f2fs_kmem_cache_create(slab_name,
  1640. sbi->page_array_slab_size);
  1641. return sbi->page_array_slab ? 0 : -ENOMEM;
  1642. }
  1643. void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi)
  1644. {
  1645. kmem_cache_destroy(sbi->page_array_slab);
  1646. }
  1647. int __init f2fs_init_compress_cache(void)
  1648. {
  1649. cic_entry_slab = f2fs_kmem_cache_create("f2fs_cic_entry",
  1650. sizeof(struct compress_io_ctx));
  1651. if (!cic_entry_slab)
  1652. return -ENOMEM;
  1653. dic_entry_slab = f2fs_kmem_cache_create("f2fs_dic_entry",
  1654. sizeof(struct decompress_io_ctx));
  1655. if (!dic_entry_slab)
  1656. goto free_cic;
  1657. return 0;
  1658. free_cic:
  1659. kmem_cache_destroy(cic_entry_slab);
  1660. return -ENOMEM;
  1661. }
  1662. void f2fs_destroy_compress_cache(void)
  1663. {
  1664. kmem_cache_destroy(dic_entry_slab);
  1665. kmem_cache_destroy(cic_entry_slab);
  1666. }