compress.c 50 KB

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