compress.c 49 KB

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