zdata.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2018 HUAWEI, Inc.
  4. * https://www.huawei.com/
  5. * Copyright (C) 2022 Alibaba Cloud
  6. */
  7. #include "compress.h"
  8. #include <linux/psi.h>
  9. #include <linux/cpuhotplug.h>
  10. #include <trace/events/erofs.h>
  11. #define Z_EROFS_MAX_SYNC_DECOMPRESS_BYTES 12288
  12. #define Z_EROFS_PCLUSTER_MAX_PAGES (Z_EROFS_PCLUSTER_MAX_SIZE / PAGE_SIZE)
  13. #define Z_EROFS_INLINE_BVECS 2
  14. struct z_erofs_bvec {
  15. struct page *page;
  16. int offset;
  17. unsigned int end;
  18. };
  19. #define __Z_EROFS_BVSET(name, total) \
  20. struct name { \
  21. /* point to the next page which contains the following bvecs */ \
  22. struct page *nextpage; \
  23. struct z_erofs_bvec bvec[total]; \
  24. }
  25. __Z_EROFS_BVSET(z_erofs_bvset,);
  26. __Z_EROFS_BVSET(z_erofs_bvset_inline, Z_EROFS_INLINE_BVECS);
  27. /*
  28. * Structure fields follow one of the following exclusion rules.
  29. *
  30. * I: Modifiable by initialization/destruction paths and read-only
  31. * for everyone else;
  32. *
  33. * L: Field should be protected by the pcluster lock;
  34. *
  35. * A: Field should be accessed / updated in atomic for parallelized code.
  36. */
  37. struct z_erofs_pcluster {
  38. struct mutex lock;
  39. struct lockref lockref;
  40. /* A: point to next chained pcluster or TAILs */
  41. struct z_erofs_pcluster *next;
  42. /* I: start physical position of this pcluster */
  43. erofs_off_t pos;
  44. /* L: the maximum decompression size of this round */
  45. unsigned int length;
  46. /* L: total number of bvecs */
  47. unsigned int vcnt;
  48. /* I: pcluster size (compressed size) in bytes */
  49. unsigned int pclustersize;
  50. /* I: page offset of start position of decompression */
  51. unsigned short pageofs_out;
  52. /* I: page offset of inline compressed data */
  53. unsigned short pageofs_in;
  54. union {
  55. /* L: inline a certain number of bvec for bootstrap */
  56. struct z_erofs_bvset_inline bvset;
  57. /* I: can be used to free the pcluster by RCU. */
  58. struct rcu_head rcu;
  59. };
  60. /* I: compression algorithm format */
  61. unsigned char algorithmformat;
  62. /* I: whether compressed data is in-lined or not */
  63. bool from_meta;
  64. /* L: whether partial decompression or not */
  65. bool partial;
  66. /* L: whether extra buffer allocations are best-effort */
  67. bool besteffort;
  68. /* A: compressed bvecs (can be cached or inplaced pages) */
  69. struct z_erofs_bvec compressed_bvecs[];
  70. };
  71. /* the end of a chain of pclusters */
  72. #define Z_EROFS_PCLUSTER_TAIL ((void *) 0x700 + POISON_POINTER_DELTA)
  73. struct z_erofs_decompressqueue {
  74. struct super_block *sb;
  75. struct z_erofs_pcluster *head;
  76. atomic_t pending_bios;
  77. union {
  78. struct completion done;
  79. struct work_struct work;
  80. struct kthread_work kthread_work;
  81. } u;
  82. bool eio, sync;
  83. };
  84. static inline unsigned int z_erofs_pclusterpages(struct z_erofs_pcluster *pcl)
  85. {
  86. return PAGE_ALIGN(pcl->pageofs_in + pcl->pclustersize) >> PAGE_SHIFT;
  87. }
  88. static bool erofs_folio_is_managed(struct erofs_sb_info *sbi, struct folio *fo)
  89. {
  90. return fo->mapping == MNGD_MAPPING(sbi);
  91. }
  92. #define Z_EROFS_ONSTACK_PAGES 32
  93. /*
  94. * since pclustersize is variable for big pcluster feature, introduce slab
  95. * pools implementation for different pcluster sizes.
  96. */
  97. struct z_erofs_pcluster_slab {
  98. struct kmem_cache *slab;
  99. unsigned int maxpages;
  100. char name[48];
  101. };
  102. #define _PCLP(n) { .maxpages = n }
  103. static struct z_erofs_pcluster_slab pcluster_pool[] __read_mostly = {
  104. _PCLP(1), _PCLP(4), _PCLP(16), _PCLP(64), _PCLP(128),
  105. _PCLP(Z_EROFS_PCLUSTER_MAX_PAGES + 1)
  106. };
  107. struct z_erofs_bvec_iter {
  108. struct page *bvpage;
  109. struct z_erofs_bvset *bvset;
  110. unsigned int nr, cur;
  111. };
  112. static struct page *z_erofs_bvec_iter_end(struct z_erofs_bvec_iter *iter)
  113. {
  114. if (iter->bvpage)
  115. kunmap_local(iter->bvset);
  116. return iter->bvpage;
  117. }
  118. static struct page *z_erofs_bvset_flip(struct z_erofs_bvec_iter *iter)
  119. {
  120. unsigned long base = (unsigned long)((struct z_erofs_bvset *)0)->bvec;
  121. /* have to access nextpage in advance, otherwise it will be unmapped */
  122. struct page *nextpage = iter->bvset->nextpage;
  123. struct page *oldpage;
  124. DBG_BUGON(!nextpage);
  125. oldpage = z_erofs_bvec_iter_end(iter);
  126. iter->bvpage = nextpage;
  127. iter->bvset = kmap_local_page(nextpage);
  128. iter->nr = (PAGE_SIZE - base) / sizeof(struct z_erofs_bvec);
  129. iter->cur = 0;
  130. return oldpage;
  131. }
  132. static void z_erofs_bvec_iter_begin(struct z_erofs_bvec_iter *iter,
  133. struct z_erofs_bvset_inline *bvset,
  134. unsigned int bootstrap_nr,
  135. unsigned int cur)
  136. {
  137. *iter = (struct z_erofs_bvec_iter) {
  138. .nr = bootstrap_nr,
  139. .bvset = (struct z_erofs_bvset *)bvset,
  140. };
  141. while (cur > iter->nr) {
  142. cur -= iter->nr;
  143. z_erofs_bvset_flip(iter);
  144. }
  145. iter->cur = cur;
  146. }
  147. static int z_erofs_bvec_enqueue(struct z_erofs_bvec_iter *iter,
  148. struct z_erofs_bvec *bvec,
  149. struct page **candidate_bvpage,
  150. struct page **pagepool)
  151. {
  152. if (iter->cur >= iter->nr) {
  153. struct page *nextpage = *candidate_bvpage;
  154. if (!nextpage) {
  155. nextpage = __erofs_allocpage(pagepool, GFP_KERNEL,
  156. true);
  157. if (!nextpage)
  158. return -ENOMEM;
  159. set_page_private(nextpage, Z_EROFS_SHORTLIVED_PAGE);
  160. }
  161. DBG_BUGON(iter->bvset->nextpage);
  162. iter->bvset->nextpage = nextpage;
  163. z_erofs_bvset_flip(iter);
  164. iter->bvset->nextpage = NULL;
  165. *candidate_bvpage = NULL;
  166. }
  167. iter->bvset->bvec[iter->cur++] = *bvec;
  168. return 0;
  169. }
  170. static void z_erofs_bvec_dequeue(struct z_erofs_bvec_iter *iter,
  171. struct z_erofs_bvec *bvec,
  172. struct page **old_bvpage)
  173. {
  174. if (iter->cur == iter->nr)
  175. *old_bvpage = z_erofs_bvset_flip(iter);
  176. else
  177. *old_bvpage = NULL;
  178. *bvec = iter->bvset->bvec[iter->cur++];
  179. }
  180. static void z_erofs_destroy_pcluster_pool(void)
  181. {
  182. int i;
  183. for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
  184. if (!pcluster_pool[i].slab)
  185. continue;
  186. kmem_cache_destroy(pcluster_pool[i].slab);
  187. pcluster_pool[i].slab = NULL;
  188. }
  189. }
  190. static int z_erofs_create_pcluster_pool(void)
  191. {
  192. struct z_erofs_pcluster_slab *pcs;
  193. struct z_erofs_pcluster *a;
  194. unsigned int size;
  195. for (pcs = pcluster_pool;
  196. pcs < pcluster_pool + ARRAY_SIZE(pcluster_pool); ++pcs) {
  197. size = struct_size(a, compressed_bvecs, pcs->maxpages);
  198. sprintf(pcs->name, "erofs_pcluster-%u", pcs->maxpages);
  199. pcs->slab = kmem_cache_create(pcs->name, size, 0,
  200. SLAB_RECLAIM_ACCOUNT, NULL);
  201. if (pcs->slab)
  202. continue;
  203. z_erofs_destroy_pcluster_pool();
  204. return -ENOMEM;
  205. }
  206. return 0;
  207. }
  208. static struct z_erofs_pcluster *z_erofs_alloc_pcluster(unsigned int size)
  209. {
  210. unsigned int nrpages = PAGE_ALIGN(size) >> PAGE_SHIFT;
  211. struct z_erofs_pcluster_slab *pcs = pcluster_pool;
  212. for (; pcs < pcluster_pool + ARRAY_SIZE(pcluster_pool); ++pcs) {
  213. struct z_erofs_pcluster *pcl;
  214. if (nrpages > pcs->maxpages)
  215. continue;
  216. pcl = kmem_cache_zalloc(pcs->slab, GFP_KERNEL);
  217. if (!pcl)
  218. return ERR_PTR(-ENOMEM);
  219. return pcl;
  220. }
  221. return ERR_PTR(-EINVAL);
  222. }
  223. static void z_erofs_free_pcluster(struct z_erofs_pcluster *pcl)
  224. {
  225. unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
  226. int i;
  227. for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
  228. struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
  229. if (pclusterpages > pcs->maxpages)
  230. continue;
  231. kmem_cache_free(pcs->slab, pcl);
  232. return;
  233. }
  234. DBG_BUGON(1);
  235. }
  236. static struct workqueue_struct *z_erofs_workqueue __read_mostly;
  237. #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
  238. static struct kthread_worker __rcu **z_erofs_pcpu_workers;
  239. static atomic_t erofs_percpu_workers_initialized = ATOMIC_INIT(0);
  240. static void erofs_destroy_percpu_workers(void)
  241. {
  242. struct kthread_worker *worker;
  243. unsigned int cpu;
  244. for_each_possible_cpu(cpu) {
  245. worker = rcu_dereference_protected(
  246. z_erofs_pcpu_workers[cpu], 1);
  247. rcu_assign_pointer(z_erofs_pcpu_workers[cpu], NULL);
  248. if (worker)
  249. kthread_destroy_worker(worker);
  250. }
  251. kfree(z_erofs_pcpu_workers);
  252. }
  253. static struct kthread_worker *erofs_init_percpu_worker(int cpu)
  254. {
  255. struct kthread_worker *worker =
  256. kthread_run_worker_on_cpu(cpu, 0, "erofs_worker/%u");
  257. if (IS_ERR(worker))
  258. return worker;
  259. if (IS_ENABLED(CONFIG_EROFS_FS_PCPU_KTHREAD_HIPRI))
  260. sched_set_fifo_low(worker->task);
  261. return worker;
  262. }
  263. static int erofs_init_percpu_workers(void)
  264. {
  265. struct kthread_worker *worker;
  266. unsigned int cpu;
  267. z_erofs_pcpu_workers = kzalloc_objs(struct kthread_worker *,
  268. num_possible_cpus(), GFP_ATOMIC);
  269. if (!z_erofs_pcpu_workers)
  270. return -ENOMEM;
  271. for_each_online_cpu(cpu) { /* could miss cpu{off,on}line? */
  272. worker = erofs_init_percpu_worker(cpu);
  273. if (!IS_ERR(worker))
  274. rcu_assign_pointer(z_erofs_pcpu_workers[cpu], worker);
  275. }
  276. return 0;
  277. }
  278. #ifdef CONFIG_HOTPLUG_CPU
  279. static DEFINE_SPINLOCK(z_erofs_pcpu_worker_lock);
  280. static enum cpuhp_state erofs_cpuhp_state;
  281. static int erofs_cpu_online(unsigned int cpu)
  282. {
  283. struct kthread_worker *worker, *old;
  284. worker = erofs_init_percpu_worker(cpu);
  285. if (IS_ERR(worker))
  286. return PTR_ERR(worker);
  287. spin_lock(&z_erofs_pcpu_worker_lock);
  288. old = rcu_dereference_protected(z_erofs_pcpu_workers[cpu],
  289. lockdep_is_held(&z_erofs_pcpu_worker_lock));
  290. if (!old)
  291. rcu_assign_pointer(z_erofs_pcpu_workers[cpu], worker);
  292. spin_unlock(&z_erofs_pcpu_worker_lock);
  293. if (old)
  294. kthread_destroy_worker(worker);
  295. return 0;
  296. }
  297. static int erofs_cpu_offline(unsigned int cpu)
  298. {
  299. struct kthread_worker *worker;
  300. spin_lock(&z_erofs_pcpu_worker_lock);
  301. worker = rcu_dereference_protected(z_erofs_pcpu_workers[cpu],
  302. lockdep_is_held(&z_erofs_pcpu_worker_lock));
  303. rcu_assign_pointer(z_erofs_pcpu_workers[cpu], NULL);
  304. spin_unlock(&z_erofs_pcpu_worker_lock);
  305. synchronize_rcu();
  306. if (worker)
  307. kthread_destroy_worker(worker);
  308. return 0;
  309. }
  310. static int erofs_cpu_hotplug_init(void)
  311. {
  312. int state;
  313. state = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
  314. "fs/erofs:online", erofs_cpu_online, erofs_cpu_offline);
  315. if (state < 0)
  316. return state;
  317. erofs_cpuhp_state = state;
  318. return 0;
  319. }
  320. static void erofs_cpu_hotplug_destroy(void)
  321. {
  322. if (erofs_cpuhp_state)
  323. cpuhp_remove_state_nocalls(erofs_cpuhp_state);
  324. }
  325. #else /* !CONFIG_HOTPLUG_CPU */
  326. static inline int erofs_cpu_hotplug_init(void) { return 0; }
  327. static inline void erofs_cpu_hotplug_destroy(void) {}
  328. #endif/* CONFIG_HOTPLUG_CPU */
  329. static int z_erofs_init_pcpu_workers(struct super_block *sb)
  330. {
  331. int err;
  332. if (atomic_xchg(&erofs_percpu_workers_initialized, 1))
  333. return 0;
  334. err = erofs_init_percpu_workers();
  335. if (err) {
  336. erofs_err(sb, "per-cpu workers: failed to allocate.");
  337. goto err_init_percpu_workers;
  338. }
  339. err = erofs_cpu_hotplug_init();
  340. if (err < 0) {
  341. erofs_err(sb, "per-cpu workers: failed CPU hotplug init.");
  342. goto err_cpuhp_init;
  343. }
  344. erofs_info(sb, "initialized per-cpu workers successfully.");
  345. return err;
  346. err_cpuhp_init:
  347. erofs_destroy_percpu_workers();
  348. err_init_percpu_workers:
  349. atomic_set(&erofs_percpu_workers_initialized, 0);
  350. return err;
  351. }
  352. static void z_erofs_destroy_pcpu_workers(void)
  353. {
  354. if (!atomic_xchg(&erofs_percpu_workers_initialized, 0))
  355. return;
  356. erofs_cpu_hotplug_destroy();
  357. erofs_destroy_percpu_workers();
  358. }
  359. #else /* !CONFIG_EROFS_FS_PCPU_KTHREAD */
  360. static inline int z_erofs_init_pcpu_workers(struct super_block *sb) { return 0; }
  361. static inline void z_erofs_destroy_pcpu_workers(void) {}
  362. #endif/* CONFIG_EROFS_FS_PCPU_KTHREAD */
  363. void z_erofs_exit_subsystem(void)
  364. {
  365. z_erofs_destroy_pcpu_workers();
  366. destroy_workqueue(z_erofs_workqueue);
  367. z_erofs_destroy_pcluster_pool();
  368. z_erofs_crypto_disable_all_engines();
  369. z_erofs_exit_decompressor();
  370. }
  371. int __init z_erofs_init_subsystem(void)
  372. {
  373. int err = z_erofs_init_decompressor();
  374. if (err)
  375. goto err_decompressor;
  376. err = z_erofs_create_pcluster_pool();
  377. if (err)
  378. goto err_pcluster_pool;
  379. z_erofs_workqueue = alloc_workqueue("erofs_worker",
  380. WQ_UNBOUND | WQ_HIGHPRI, num_possible_cpus());
  381. if (!z_erofs_workqueue) {
  382. err = -ENOMEM;
  383. goto err_workqueue_init;
  384. }
  385. return err;
  386. err_workqueue_init:
  387. z_erofs_destroy_pcluster_pool();
  388. err_pcluster_pool:
  389. z_erofs_exit_decompressor();
  390. err_decompressor:
  391. return err;
  392. }
  393. enum z_erofs_pclustermode {
  394. /* It has previously been linked into another processing chain */
  395. Z_EROFS_PCLUSTER_INFLIGHT,
  396. /*
  397. * A weaker form of Z_EROFS_PCLUSTER_FOLLOWED; the difference is that it
  398. * may be dispatched to the bypass queue later due to uptodated managed
  399. * folios. All file-backed folios related to this pcluster cannot be
  400. * reused for in-place I/O (or bvpage) since the pcluster may be decoded
  401. * in a separate queue (and thus out of order).
  402. */
  403. Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE,
  404. /*
  405. * The pcluster has just been linked to our processing chain.
  406. * File-backed folios (except for the head page) related to it can be
  407. * used for in-place I/O (or bvpage).
  408. */
  409. Z_EROFS_PCLUSTER_FOLLOWED,
  410. };
  411. struct z_erofs_frontend {
  412. struct inode *inode, *sharedinode;
  413. struct erofs_map_blocks map;
  414. struct z_erofs_bvec_iter biter;
  415. struct page *pagepool;
  416. struct page *candidate_bvpage;
  417. struct z_erofs_pcluster *pcl, *head;
  418. enum z_erofs_pclustermode mode;
  419. erofs_off_t headoffset;
  420. /* a pointer used to pick up inplace I/O pages */
  421. unsigned int icur;
  422. };
  423. #define Z_EROFS_DEFINE_FRONTEND(fe, i, si, ho) struct z_erofs_frontend fe = { \
  424. .inode = i, .sharedinode = si, .head = Z_EROFS_PCLUSTER_TAIL, \
  425. .mode = Z_EROFS_PCLUSTER_FOLLOWED, .headoffset = ho }
  426. static bool z_erofs_should_alloc_cache(struct z_erofs_frontend *fe)
  427. {
  428. unsigned int cachestrategy = EROFS_I_SB(fe->inode)->opt.cache_strategy;
  429. if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
  430. return false;
  431. if (!(fe->map.m_flags & EROFS_MAP_FULL_MAPPED))
  432. return true;
  433. if (cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
  434. fe->map.m_la < fe->headoffset)
  435. return true;
  436. return false;
  437. }
  438. static void z_erofs_bind_cache(struct z_erofs_frontend *fe)
  439. {
  440. struct address_space *mc = MNGD_MAPPING(EROFS_I_SB(fe->inode));
  441. struct z_erofs_pcluster *pcl = fe->pcl;
  442. unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
  443. bool shouldalloc = z_erofs_should_alloc_cache(fe);
  444. pgoff_t poff = pcl->pos >> PAGE_SHIFT;
  445. bool may_bypass = true;
  446. /* Optimistic allocation, as in-place I/O can be used as a fallback */
  447. gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) |
  448. __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
  449. struct folio *folio, *newfolio;
  450. unsigned int i;
  451. if (i_blocksize(fe->inode) != PAGE_SIZE ||
  452. fe->mode < Z_EROFS_PCLUSTER_FOLLOWED)
  453. return;
  454. for (i = 0; i < pclusterpages; ++i) {
  455. /* Inaccurate check w/o locking to avoid unneeded lookups */
  456. if (READ_ONCE(pcl->compressed_bvecs[i].page))
  457. continue;
  458. folio = filemap_get_folio(mc, poff + i);
  459. if (IS_ERR(folio)) {
  460. may_bypass = false;
  461. if (!shouldalloc)
  462. continue;
  463. /*
  464. * Allocate a managed folio for cached I/O, or it may be
  465. * then filled with a file-backed folio for in-place I/O
  466. */
  467. newfolio = filemap_alloc_folio(gfp, 0, NULL);
  468. if (!newfolio)
  469. continue;
  470. newfolio->private = Z_EROFS_PREALLOCATED_FOLIO;
  471. folio = NULL;
  472. }
  473. spin_lock(&pcl->lockref.lock);
  474. if (!pcl->compressed_bvecs[i].page) {
  475. pcl->compressed_bvecs[i].page =
  476. folio_page(folio ?: newfolio, 0);
  477. spin_unlock(&pcl->lockref.lock);
  478. continue;
  479. }
  480. spin_unlock(&pcl->lockref.lock);
  481. folio_put(folio ?: newfolio);
  482. }
  483. /*
  484. * Don't perform in-place I/O if all compressed pages are available in
  485. * the managed cache, as the pcluster can be moved to the bypass queue.
  486. */
  487. if (may_bypass)
  488. fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE;
  489. }
  490. /* (erofs_shrinker) disconnect cached encoded data with pclusters */
  491. static int erofs_try_to_free_all_cached_folios(struct erofs_sb_info *sbi,
  492. struct z_erofs_pcluster *pcl)
  493. {
  494. unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
  495. struct folio *folio;
  496. int i;
  497. DBG_BUGON(pcl->from_meta);
  498. /* Each cached folio contains one page unless bs > ps is supported */
  499. for (i = 0; i < pclusterpages; ++i) {
  500. if (pcl->compressed_bvecs[i].page) {
  501. folio = page_folio(pcl->compressed_bvecs[i].page);
  502. /* Avoid reclaiming or migrating this folio */
  503. if (!folio_trylock(folio))
  504. return -EBUSY;
  505. if (!erofs_folio_is_managed(sbi, folio))
  506. continue;
  507. pcl->compressed_bvecs[i].page = NULL;
  508. folio_detach_private(folio);
  509. folio_unlock(folio);
  510. }
  511. }
  512. return 0;
  513. }
  514. static bool z_erofs_cache_release_folio(struct folio *folio, gfp_t gfp)
  515. {
  516. struct z_erofs_pcluster *pcl = folio_get_private(folio);
  517. struct z_erofs_bvec *bvec = pcl->compressed_bvecs;
  518. struct z_erofs_bvec *end = bvec + z_erofs_pclusterpages(pcl);
  519. bool ret;
  520. if (!folio_test_private(folio))
  521. return true;
  522. ret = false;
  523. spin_lock(&pcl->lockref.lock);
  524. if (pcl->lockref.count <= 0) {
  525. DBG_BUGON(pcl->from_meta);
  526. for (; bvec < end; ++bvec) {
  527. if (bvec->page && page_folio(bvec->page) == folio) {
  528. bvec->page = NULL;
  529. folio_detach_private(folio);
  530. ret = true;
  531. break;
  532. }
  533. }
  534. }
  535. spin_unlock(&pcl->lockref.lock);
  536. return ret;
  537. }
  538. /*
  539. * It will be called only on inode eviction. In case that there are still some
  540. * decompression requests in progress, wait with rescheduling for a bit here.
  541. * An extra lock could be introduced instead but it seems unnecessary.
  542. */
  543. static void z_erofs_cache_invalidate_folio(struct folio *folio,
  544. size_t offset, size_t length)
  545. {
  546. const size_t stop = length + offset;
  547. /* Check for potential overflow in debug mode */
  548. DBG_BUGON(stop > folio_size(folio) || stop < length);
  549. if (offset == 0 && stop == folio_size(folio))
  550. while (!z_erofs_cache_release_folio(folio, 0))
  551. cond_resched();
  552. }
  553. static const struct address_space_operations z_erofs_cache_aops = {
  554. .release_folio = z_erofs_cache_release_folio,
  555. .invalidate_folio = z_erofs_cache_invalidate_folio,
  556. };
  557. int z_erofs_init_super(struct super_block *sb)
  558. {
  559. struct inode *inode;
  560. int err;
  561. err = z_erofs_init_pcpu_workers(sb);
  562. if (err)
  563. return err;
  564. inode = new_inode(sb);
  565. if (!inode)
  566. return -ENOMEM;
  567. set_nlink(inode, 1);
  568. inode->i_size = OFFSET_MAX;
  569. inode->i_mapping->a_ops = &z_erofs_cache_aops;
  570. mapping_set_gfp_mask(inode->i_mapping, GFP_KERNEL);
  571. EROFS_SB(sb)->managed_cache = inode;
  572. xa_init(&EROFS_SB(sb)->managed_pslots);
  573. return 0;
  574. }
  575. /* callers must be with pcluster lock held */
  576. static int z_erofs_attach_page(struct z_erofs_frontend *fe,
  577. struct z_erofs_bvec *bvec, bool exclusive)
  578. {
  579. struct z_erofs_pcluster *pcl = fe->pcl;
  580. int ret;
  581. if (exclusive) {
  582. /* Inplace I/O is limited to one page for uncompressed data */
  583. if (pcl->algorithmformat < Z_EROFS_COMPRESSION_MAX ||
  584. fe->icur <= 1) {
  585. /* Try to prioritize inplace I/O here */
  586. spin_lock(&pcl->lockref.lock);
  587. while (fe->icur > 0) {
  588. if (pcl->compressed_bvecs[--fe->icur].page)
  589. continue;
  590. pcl->compressed_bvecs[fe->icur] = *bvec;
  591. spin_unlock(&pcl->lockref.lock);
  592. return 0;
  593. }
  594. spin_unlock(&pcl->lockref.lock);
  595. }
  596. /* otherwise, check if it can be used as a bvpage */
  597. if (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED &&
  598. !fe->candidate_bvpage)
  599. fe->candidate_bvpage = bvec->page;
  600. }
  601. ret = z_erofs_bvec_enqueue(&fe->biter, bvec, &fe->candidate_bvpage,
  602. &fe->pagepool);
  603. fe->pcl->vcnt += (ret >= 0);
  604. return ret;
  605. }
  606. static bool z_erofs_get_pcluster(struct z_erofs_pcluster *pcl)
  607. {
  608. if (lockref_get_not_zero(&pcl->lockref))
  609. return true;
  610. spin_lock(&pcl->lockref.lock);
  611. if (__lockref_is_dead(&pcl->lockref)) {
  612. spin_unlock(&pcl->lockref.lock);
  613. return false;
  614. }
  615. if (!pcl->lockref.count++)
  616. atomic_long_dec(&erofs_global_shrink_cnt);
  617. spin_unlock(&pcl->lockref.lock);
  618. return true;
  619. }
  620. static int z_erofs_register_pcluster(struct z_erofs_frontend *fe)
  621. {
  622. struct erofs_map_blocks *map = &fe->map;
  623. struct super_block *sb = fe->inode->i_sb;
  624. struct erofs_sb_info *sbi = EROFS_SB(sb);
  625. struct z_erofs_pcluster *pcl, *pre;
  626. unsigned int pageofs_in;
  627. int err;
  628. pageofs_in = erofs_blkoff(sb, map->m_pa);
  629. pcl = z_erofs_alloc_pcluster(pageofs_in + map->m_plen);
  630. if (IS_ERR(pcl))
  631. return PTR_ERR(pcl);
  632. lockref_init(&pcl->lockref); /* one ref for this request */
  633. pcl->algorithmformat = map->m_algorithmformat;
  634. pcl->pclustersize = map->m_plen;
  635. pcl->length = 0;
  636. pcl->partial = true;
  637. pcl->next = fe->head;
  638. pcl->pos = map->m_pa;
  639. pcl->pageofs_in = pageofs_in;
  640. pcl->pageofs_out = map->m_la & ~PAGE_MASK;
  641. pcl->from_meta = map->m_flags & EROFS_MAP_META;
  642. fe->mode = Z_EROFS_PCLUSTER_FOLLOWED;
  643. /*
  644. * lock all primary followed works before visible to others
  645. * and mutex_trylock *never* fails for a new pcluster.
  646. */
  647. mutex_init(&pcl->lock);
  648. DBG_BUGON(!mutex_trylock(&pcl->lock));
  649. if (!pcl->from_meta) {
  650. while (1) {
  651. xa_lock(&sbi->managed_pslots);
  652. pre = __xa_cmpxchg(&sbi->managed_pslots, pcl->pos,
  653. NULL, pcl, GFP_KERNEL);
  654. if (!pre || xa_is_err(pre) || z_erofs_get_pcluster(pre)) {
  655. xa_unlock(&sbi->managed_pslots);
  656. break;
  657. }
  658. /* try to legitimize the current in-tree one */
  659. xa_unlock(&sbi->managed_pslots);
  660. cond_resched();
  661. }
  662. if (xa_is_err(pre)) {
  663. err = xa_err(pre);
  664. goto err_out;
  665. } else if (pre) {
  666. fe->pcl = pre;
  667. err = -EEXIST;
  668. goto err_out;
  669. }
  670. }
  671. fe->head = fe->pcl = pcl;
  672. return 0;
  673. err_out:
  674. mutex_unlock(&pcl->lock);
  675. z_erofs_free_pcluster(pcl);
  676. return err;
  677. }
  678. static int z_erofs_pcluster_begin(struct z_erofs_frontend *fe)
  679. {
  680. struct erofs_map_blocks *map = &fe->map;
  681. struct super_block *sb = fe->inode->i_sb;
  682. struct z_erofs_pcluster *pcl = NULL;
  683. void *ptr = NULL;
  684. int ret;
  685. DBG_BUGON(fe->pcl);
  686. /* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous pcluster */
  687. DBG_BUGON(!fe->head);
  688. if (map->m_flags & EROFS_MAP_META) {
  689. ret = erofs_init_metabuf(&map->buf, sb,
  690. erofs_inode_in_metabox(fe->inode));
  691. if (ret)
  692. return ret;
  693. ptr = erofs_bread(&map->buf, map->m_pa, false);
  694. if (IS_ERR(ptr)) {
  695. erofs_err(sb, "failed to read inline data %pe @ pa %llu of nid %llu",
  696. ptr, map->m_pa, EROFS_I(fe->inode)->nid);
  697. return PTR_ERR(ptr);
  698. }
  699. ptr = map->buf.page;
  700. } else {
  701. while (1) {
  702. rcu_read_lock();
  703. pcl = xa_load(&EROFS_SB(sb)->managed_pslots, map->m_pa);
  704. if (!pcl || z_erofs_get_pcluster(pcl)) {
  705. DBG_BUGON(pcl && map->m_pa != pcl->pos);
  706. rcu_read_unlock();
  707. break;
  708. }
  709. rcu_read_unlock();
  710. }
  711. }
  712. if (pcl) {
  713. fe->pcl = pcl;
  714. ret = -EEXIST;
  715. } else {
  716. ret = z_erofs_register_pcluster(fe);
  717. }
  718. if (ret == -EEXIST) {
  719. mutex_lock(&fe->pcl->lock);
  720. /* check if this pcluster hasn't been linked into any chain. */
  721. if (!cmpxchg(&fe->pcl->next, NULL, fe->head)) {
  722. /* .. so it can be attached to our submission chain */
  723. fe->head = fe->pcl;
  724. fe->mode = Z_EROFS_PCLUSTER_FOLLOWED;
  725. } else { /* otherwise, it belongs to an inflight chain */
  726. fe->mode = Z_EROFS_PCLUSTER_INFLIGHT;
  727. }
  728. } else if (ret) {
  729. return ret;
  730. }
  731. z_erofs_bvec_iter_begin(&fe->biter, &fe->pcl->bvset,
  732. Z_EROFS_INLINE_BVECS, fe->pcl->vcnt);
  733. if (!fe->pcl->from_meta) {
  734. /* bind cache first when cached decompression is preferred */
  735. z_erofs_bind_cache(fe);
  736. } else {
  737. folio_get(page_folio((struct page *)ptr));
  738. WRITE_ONCE(fe->pcl->compressed_bvecs[0].page, ptr);
  739. fe->pcl->pageofs_in = map->m_pa & ~PAGE_MASK;
  740. fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE;
  741. }
  742. /* file-backed inplace I/O pages are traversed in reverse order */
  743. fe->icur = z_erofs_pclusterpages(fe->pcl);
  744. return 0;
  745. }
  746. static void z_erofs_rcu_callback(struct rcu_head *head)
  747. {
  748. z_erofs_free_pcluster(container_of(head, struct z_erofs_pcluster, rcu));
  749. }
  750. static bool __erofs_try_to_release_pcluster(struct erofs_sb_info *sbi,
  751. struct z_erofs_pcluster *pcl)
  752. {
  753. if (pcl->lockref.count)
  754. return false;
  755. /*
  756. * Note that all cached folios should be detached before deleted from
  757. * the XArray. Otherwise some folios could be still attached to the
  758. * orphan old pcluster when the new one is available in the tree.
  759. */
  760. if (erofs_try_to_free_all_cached_folios(sbi, pcl))
  761. return false;
  762. /*
  763. * It's impossible to fail after the pcluster is freezed, but in order
  764. * to avoid some race conditions, add a DBG_BUGON to observe this.
  765. */
  766. DBG_BUGON(__xa_erase(&sbi->managed_pslots, pcl->pos) != pcl);
  767. lockref_mark_dead(&pcl->lockref);
  768. return true;
  769. }
  770. static bool erofs_try_to_release_pcluster(struct erofs_sb_info *sbi,
  771. struct z_erofs_pcluster *pcl)
  772. {
  773. bool free;
  774. spin_lock(&pcl->lockref.lock);
  775. free = __erofs_try_to_release_pcluster(sbi, pcl);
  776. spin_unlock(&pcl->lockref.lock);
  777. if (free) {
  778. atomic_long_dec(&erofs_global_shrink_cnt);
  779. call_rcu(&pcl->rcu, z_erofs_rcu_callback);
  780. }
  781. return free;
  782. }
  783. unsigned long z_erofs_shrink_scan(struct erofs_sb_info *sbi, unsigned long nr)
  784. {
  785. struct z_erofs_pcluster *pcl;
  786. unsigned long index, freed = 0;
  787. xa_lock(&sbi->managed_pslots);
  788. xa_for_each(&sbi->managed_pslots, index, pcl) {
  789. /* try to shrink each valid pcluster */
  790. if (!erofs_try_to_release_pcluster(sbi, pcl))
  791. continue;
  792. xa_unlock(&sbi->managed_pslots);
  793. ++freed;
  794. if (!--nr)
  795. return freed;
  796. xa_lock(&sbi->managed_pslots);
  797. }
  798. xa_unlock(&sbi->managed_pslots);
  799. return freed;
  800. }
  801. static void z_erofs_put_pcluster(struct erofs_sb_info *sbi,
  802. struct z_erofs_pcluster *pcl, bool try_free)
  803. {
  804. bool free = false;
  805. if (lockref_put_or_lock(&pcl->lockref))
  806. return;
  807. DBG_BUGON(__lockref_is_dead(&pcl->lockref));
  808. if (!--pcl->lockref.count) {
  809. if (try_free && xa_trylock(&sbi->managed_pslots)) {
  810. free = __erofs_try_to_release_pcluster(sbi, pcl);
  811. xa_unlock(&sbi->managed_pslots);
  812. }
  813. atomic_long_add(!free, &erofs_global_shrink_cnt);
  814. }
  815. spin_unlock(&pcl->lockref.lock);
  816. if (free)
  817. call_rcu(&pcl->rcu, z_erofs_rcu_callback);
  818. }
  819. static void z_erofs_pcluster_end(struct z_erofs_frontend *fe)
  820. {
  821. struct z_erofs_pcluster *pcl = fe->pcl;
  822. if (!pcl)
  823. return;
  824. z_erofs_bvec_iter_end(&fe->biter);
  825. mutex_unlock(&pcl->lock);
  826. if (fe->candidate_bvpage)
  827. fe->candidate_bvpage = NULL;
  828. /* Drop refcount if it doesn't belong to our processing chain */
  829. if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE)
  830. z_erofs_put_pcluster(EROFS_I_SB(fe->inode), pcl, false);
  831. fe->pcl = NULL;
  832. }
  833. static int z_erofs_read_fragment(struct super_block *sb, struct folio *folio,
  834. unsigned int cur, unsigned int end, erofs_off_t pos)
  835. {
  836. struct inode *packed_inode = EROFS_SB(sb)->packed_inode;
  837. struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
  838. unsigned int cnt;
  839. u8 *src;
  840. if (!packed_inode)
  841. return -EFSCORRUPTED;
  842. buf.mapping = packed_inode->i_mapping;
  843. for (; cur < end; cur += cnt, pos += cnt) {
  844. cnt = min(end - cur, sb->s_blocksize - erofs_blkoff(sb, pos));
  845. src = erofs_bread(&buf, pos, true);
  846. if (IS_ERR(src)) {
  847. erofs_put_metabuf(&buf);
  848. return PTR_ERR(src);
  849. }
  850. memcpy_to_folio(folio, cur, src, cnt);
  851. }
  852. erofs_put_metabuf(&buf);
  853. return 0;
  854. }
  855. static int z_erofs_scan_folio(struct z_erofs_frontend *f,
  856. struct folio *folio, bool ra)
  857. {
  858. struct inode *const inode = f->inode;
  859. struct erofs_map_blocks *const map = &f->map;
  860. const loff_t offset = folio_pos(folio);
  861. const unsigned int bs = i_blocksize(inode);
  862. unsigned int end = folio_size(folio), split = 0, cur, pgs;
  863. bool tight, excl;
  864. int err = 0;
  865. tight = (bs == PAGE_SIZE);
  866. erofs_onlinefolio_init(folio);
  867. do {
  868. if (offset + end - 1 < map->m_la ||
  869. offset + end - 1 >= map->m_la + map->m_llen) {
  870. z_erofs_pcluster_end(f);
  871. map->m_la = offset + end - 1;
  872. map->m_llen = 0;
  873. err = z_erofs_map_blocks_iter(inode, map, 0);
  874. if (err)
  875. break;
  876. }
  877. cur = offset > map->m_la ? 0 : map->m_la - offset;
  878. pgs = round_down(cur, PAGE_SIZE);
  879. /* bump split parts first to avoid several separate cases */
  880. ++split;
  881. if (!(map->m_flags & EROFS_MAP_MAPPED)) {
  882. folio_zero_segment(folio, cur, end);
  883. tight = false;
  884. } else if (map->m_flags & __EROFS_MAP_FRAGMENT) {
  885. erofs_off_t fpos = offset + cur - map->m_la;
  886. err = z_erofs_read_fragment(inode->i_sb, folio, cur,
  887. cur + min(map->m_llen - fpos, end - cur),
  888. EROFS_I(inode)->z_fragmentoff + fpos);
  889. if (err)
  890. break;
  891. tight = false;
  892. } else {
  893. if (!f->pcl) {
  894. err = z_erofs_pcluster_begin(f);
  895. if (err)
  896. break;
  897. f->pcl->besteffort |= !ra;
  898. }
  899. pgs = round_down(end - 1, PAGE_SIZE);
  900. /*
  901. * Ensure this partial page belongs to this submit chain
  902. * rather than other concurrent submit chains or
  903. * noio(bypass) chains since those chains are handled
  904. * asynchronously thus it cannot be used for inplace I/O
  905. * or bvpage (should be processed in the strict order.)
  906. */
  907. tight &= (f->mode >= Z_EROFS_PCLUSTER_FOLLOWED);
  908. excl = false;
  909. if (cur <= pgs) {
  910. excl = (split <= 1) || tight;
  911. cur = pgs;
  912. }
  913. err = z_erofs_attach_page(f, &((struct z_erofs_bvec) {
  914. .page = folio_page(folio, pgs >> PAGE_SHIFT),
  915. .offset = offset + pgs - map->m_la,
  916. .end = end - pgs, }), excl);
  917. if (err)
  918. break;
  919. erofs_onlinefolio_split(folio);
  920. if (f->pcl->length < offset + end - map->m_la) {
  921. f->pcl->length = offset + end - map->m_la;
  922. f->pcl->pageofs_out = map->m_la & ~PAGE_MASK;
  923. }
  924. if ((map->m_flags & EROFS_MAP_FULL_MAPPED) &&
  925. !(map->m_flags & EROFS_MAP_PARTIAL_REF) &&
  926. f->pcl->length == map->m_llen)
  927. f->pcl->partial = false;
  928. }
  929. /* shorten the remaining extent to update progress */
  930. map->m_llen = offset + cur - map->m_la;
  931. map->m_flags &= ~EROFS_MAP_FULL_MAPPED;
  932. if (cur <= pgs) {
  933. split = cur < pgs;
  934. tight = (bs == PAGE_SIZE);
  935. }
  936. } while ((end = cur) > 0);
  937. erofs_onlinefolio_end(folio, err, false);
  938. return err;
  939. }
  940. static bool z_erofs_page_is_invalidated(struct page *page)
  941. {
  942. return !page_folio(page)->mapping && !z_erofs_is_shortlived_page(page);
  943. }
  944. struct z_erofs_backend {
  945. struct page *onstack_pages[Z_EROFS_ONSTACK_PAGES];
  946. struct super_block *sb;
  947. struct z_erofs_pcluster *pcl;
  948. /* pages with the longest decompressed length for deduplication */
  949. struct page **decompressed_pages;
  950. /* pages to keep the compressed data */
  951. struct page **compressed_pages;
  952. struct list_head decompressed_secondary_bvecs;
  953. struct page **pagepool;
  954. unsigned int onstack_used, nr_pages;
  955. /* indicate if temporary copies should be preserved for later use */
  956. bool keepxcpy;
  957. };
  958. struct z_erofs_bvec_item {
  959. struct z_erofs_bvec bvec;
  960. struct list_head list;
  961. };
  962. static void z_erofs_do_decompressed_bvec(struct z_erofs_backend *be,
  963. struct z_erofs_bvec *bvec)
  964. {
  965. int poff = bvec->offset + be->pcl->pageofs_out;
  966. struct z_erofs_bvec_item *item;
  967. struct page **page;
  968. if (!(poff & ~PAGE_MASK) && (bvec->end == PAGE_SIZE ||
  969. bvec->offset + bvec->end == be->pcl->length)) {
  970. DBG_BUGON((poff >> PAGE_SHIFT) >= be->nr_pages);
  971. page = be->decompressed_pages + (poff >> PAGE_SHIFT);
  972. if (!*page) {
  973. *page = bvec->page;
  974. return;
  975. }
  976. } else {
  977. be->keepxcpy = true;
  978. }
  979. /* (cold path) one pcluster is requested multiple times */
  980. item = kmalloc_obj(*item, GFP_KERNEL | __GFP_NOFAIL);
  981. item->bvec = *bvec;
  982. list_add(&item->list, &be->decompressed_secondary_bvecs);
  983. }
  984. static void z_erofs_fill_other_copies(struct z_erofs_backend *be, int err)
  985. {
  986. unsigned int off0 = be->pcl->pageofs_out;
  987. struct list_head *p, *n;
  988. list_for_each_safe(p, n, &be->decompressed_secondary_bvecs) {
  989. struct z_erofs_bvec_item *bvi;
  990. unsigned int end, cur;
  991. void *dst, *src;
  992. bvi = container_of(p, struct z_erofs_bvec_item, list);
  993. cur = bvi->bvec.offset < 0 ? -bvi->bvec.offset : 0;
  994. end = min_t(unsigned int, be->pcl->length - bvi->bvec.offset,
  995. bvi->bvec.end);
  996. dst = kmap_local_page(bvi->bvec.page);
  997. while (cur < end) {
  998. unsigned int pgnr, scur, len;
  999. pgnr = (bvi->bvec.offset + cur + off0) >> PAGE_SHIFT;
  1000. DBG_BUGON(pgnr >= be->nr_pages);
  1001. scur = bvi->bvec.offset + cur -
  1002. ((pgnr << PAGE_SHIFT) - off0);
  1003. len = min_t(unsigned int, end - cur, PAGE_SIZE - scur);
  1004. if (!be->decompressed_pages[pgnr]) {
  1005. err = -EFSCORRUPTED;
  1006. cur += len;
  1007. continue;
  1008. }
  1009. src = kmap_local_page(be->decompressed_pages[pgnr]);
  1010. memcpy(dst + cur, src + scur, len);
  1011. kunmap_local(src);
  1012. cur += len;
  1013. }
  1014. kunmap_local(dst);
  1015. erofs_onlinefolio_end(page_folio(bvi->bvec.page), err, true);
  1016. list_del(p);
  1017. kfree(bvi);
  1018. }
  1019. }
  1020. static void z_erofs_parse_out_bvecs(struct z_erofs_backend *be)
  1021. {
  1022. struct z_erofs_pcluster *pcl = be->pcl;
  1023. struct z_erofs_bvec_iter biter;
  1024. struct page *old_bvpage;
  1025. int i;
  1026. z_erofs_bvec_iter_begin(&biter, &pcl->bvset, Z_EROFS_INLINE_BVECS, 0);
  1027. for (i = 0; i < pcl->vcnt; ++i) {
  1028. struct z_erofs_bvec bvec;
  1029. z_erofs_bvec_dequeue(&biter, &bvec, &old_bvpage);
  1030. if (old_bvpage)
  1031. z_erofs_put_shortlivedpage(be->pagepool, old_bvpage);
  1032. DBG_BUGON(z_erofs_page_is_invalidated(bvec.page));
  1033. z_erofs_do_decompressed_bvec(be, &bvec);
  1034. }
  1035. old_bvpage = z_erofs_bvec_iter_end(&biter);
  1036. if (old_bvpage)
  1037. z_erofs_put_shortlivedpage(be->pagepool, old_bvpage);
  1038. }
  1039. static int z_erofs_parse_in_bvecs(struct z_erofs_backend *be, bool *overlapped)
  1040. {
  1041. struct z_erofs_pcluster *pcl = be->pcl;
  1042. unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
  1043. int i, err = 0;
  1044. *overlapped = false;
  1045. for (i = 0; i < pclusterpages; ++i) {
  1046. struct z_erofs_bvec *bvec = &pcl->compressed_bvecs[i];
  1047. struct page *page = bvec->page;
  1048. /* compressed data ought to be valid when decompressing */
  1049. if (IS_ERR(page) || !page) {
  1050. bvec->page = NULL; /* clear the failure reason */
  1051. err = page ? PTR_ERR(page) : -EIO;
  1052. continue;
  1053. }
  1054. be->compressed_pages[i] = page;
  1055. if (pcl->from_meta ||
  1056. erofs_folio_is_managed(EROFS_SB(be->sb), page_folio(page))) {
  1057. if (!PageUptodate(page))
  1058. err = -EIO;
  1059. continue;
  1060. }
  1061. DBG_BUGON(z_erofs_page_is_invalidated(page));
  1062. if (z_erofs_is_shortlived_page(page))
  1063. continue;
  1064. z_erofs_do_decompressed_bvec(be, bvec);
  1065. *overlapped = true;
  1066. }
  1067. return err;
  1068. }
  1069. static int z_erofs_decompress_pcluster(struct z_erofs_backend *be, bool eio)
  1070. {
  1071. struct erofs_sb_info *const sbi = EROFS_SB(be->sb);
  1072. struct z_erofs_pcluster *pcl = be->pcl;
  1073. unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
  1074. const struct z_erofs_decompressor *alg =
  1075. z_erofs_decomp[pcl->algorithmformat];
  1076. bool try_free = true;
  1077. int i, j, jtop, err2, err = eio ? -EIO : 0;
  1078. struct page *page;
  1079. bool overlapped;
  1080. const char *reason;
  1081. mutex_lock(&pcl->lock);
  1082. be->nr_pages = PAGE_ALIGN(pcl->length + pcl->pageofs_out) >> PAGE_SHIFT;
  1083. /* allocate (de)compressed page arrays if cannot be kept on stack */
  1084. be->decompressed_pages = NULL;
  1085. be->compressed_pages = NULL;
  1086. be->onstack_used = 0;
  1087. if (be->nr_pages <= Z_EROFS_ONSTACK_PAGES) {
  1088. be->decompressed_pages = be->onstack_pages;
  1089. be->onstack_used = be->nr_pages;
  1090. memset(be->decompressed_pages, 0,
  1091. sizeof(struct page *) * be->nr_pages);
  1092. }
  1093. if (pclusterpages + be->onstack_used <= Z_EROFS_ONSTACK_PAGES)
  1094. be->compressed_pages = be->onstack_pages + be->onstack_used;
  1095. if (!be->decompressed_pages)
  1096. be->decompressed_pages =
  1097. kvzalloc_objs(struct page *, be->nr_pages,
  1098. GFP_KERNEL | __GFP_NOFAIL);
  1099. if (!be->compressed_pages)
  1100. be->compressed_pages =
  1101. kvzalloc_objs(struct page *, pclusterpages,
  1102. GFP_KERNEL | __GFP_NOFAIL);
  1103. z_erofs_parse_out_bvecs(be);
  1104. err2 = z_erofs_parse_in_bvecs(be, &overlapped);
  1105. if (err2)
  1106. err = err2;
  1107. if (!err) {
  1108. reason = alg->decompress(&(struct z_erofs_decompress_req) {
  1109. .sb = be->sb,
  1110. .in = be->compressed_pages,
  1111. .out = be->decompressed_pages,
  1112. .inpages = pclusterpages,
  1113. .outpages = be->nr_pages,
  1114. .pageofs_in = pcl->pageofs_in,
  1115. .pageofs_out = pcl->pageofs_out,
  1116. .inputsize = pcl->pclustersize,
  1117. .outputsize = pcl->length,
  1118. .alg = pcl->algorithmformat,
  1119. .inplace_io = overlapped,
  1120. .partial_decoding = pcl->partial,
  1121. .fillgaps = be->keepxcpy,
  1122. .gfp = pcl->besteffort ? GFP_KERNEL :
  1123. GFP_NOWAIT | __GFP_NORETRY
  1124. }, be->pagepool);
  1125. if (IS_ERR(reason)) {
  1126. if (pcl->besteffort || reason != ERR_PTR(-ENOMEM))
  1127. erofs_err(be->sb, "failed to decompress (%s) %pe @ pa %llu size %u => %u",
  1128. alg->name, reason, pcl->pos,
  1129. pcl->pclustersize, pcl->length);
  1130. err = PTR_ERR(reason);
  1131. } else if (unlikely(reason)) {
  1132. erofs_err(be->sb, "failed to decompress (%s) %s @ pa %llu size %u => %u",
  1133. alg->name, reason, pcl->pos,
  1134. pcl->pclustersize, pcl->length);
  1135. err = -EFSCORRUPTED;
  1136. }
  1137. }
  1138. /* must handle all compressed pages before actual file pages */
  1139. if (pcl->from_meta) {
  1140. folio_put(page_folio(pcl->compressed_bvecs[0].page));
  1141. WRITE_ONCE(pcl->compressed_bvecs[0].page, NULL);
  1142. } else {
  1143. /* managed folios are still left in compressed_bvecs[] */
  1144. for (i = 0; i < pclusterpages; ++i) {
  1145. page = be->compressed_pages[i];
  1146. if (!page)
  1147. continue;
  1148. if (erofs_folio_is_managed(sbi, page_folio(page))) {
  1149. try_free = false;
  1150. continue;
  1151. }
  1152. (void)z_erofs_put_shortlivedpage(be->pagepool, page);
  1153. WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
  1154. }
  1155. }
  1156. if (be->compressed_pages < be->onstack_pages ||
  1157. be->compressed_pages >= be->onstack_pages + Z_EROFS_ONSTACK_PAGES)
  1158. kvfree(be->compressed_pages);
  1159. jtop = 0;
  1160. z_erofs_fill_other_copies(be, err);
  1161. for (i = 0; i < be->nr_pages; ++i) {
  1162. page = be->decompressed_pages[i];
  1163. if (!page)
  1164. continue;
  1165. DBG_BUGON(z_erofs_page_is_invalidated(page));
  1166. if (!z_erofs_is_shortlived_page(page)) {
  1167. erofs_onlinefolio_end(page_folio(page), err, true);
  1168. continue;
  1169. }
  1170. if (pcl->algorithmformat != Z_EROFS_COMPRESSION_LZ4) {
  1171. erofs_pagepool_add(be->pagepool, page);
  1172. continue;
  1173. }
  1174. for (j = 0; j < jtop && be->decompressed_pages[j] != page; ++j)
  1175. ;
  1176. if (j >= jtop) /* this bounce page is newly detected */
  1177. be->decompressed_pages[jtop++] = page;
  1178. }
  1179. while (jtop)
  1180. erofs_pagepool_add(be->pagepool,
  1181. be->decompressed_pages[--jtop]);
  1182. if (be->decompressed_pages != be->onstack_pages)
  1183. kvfree(be->decompressed_pages);
  1184. pcl->length = 0;
  1185. pcl->partial = true;
  1186. pcl->besteffort = false;
  1187. pcl->bvset.nextpage = NULL;
  1188. pcl->vcnt = 0;
  1189. /* pcluster lock MUST be taken before the following line */
  1190. WRITE_ONCE(pcl->next, NULL);
  1191. mutex_unlock(&pcl->lock);
  1192. if (pcl->from_meta)
  1193. z_erofs_free_pcluster(pcl);
  1194. else
  1195. z_erofs_put_pcluster(sbi, pcl, try_free);
  1196. return err;
  1197. }
  1198. static int z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io,
  1199. struct page **pagepool)
  1200. {
  1201. struct z_erofs_backend be = {
  1202. .sb = io->sb,
  1203. .pagepool = pagepool,
  1204. .decompressed_secondary_bvecs =
  1205. LIST_HEAD_INIT(be.decompressed_secondary_bvecs),
  1206. .pcl = io->head,
  1207. };
  1208. struct z_erofs_pcluster *next;
  1209. int err = 0;
  1210. for (; be.pcl != Z_EROFS_PCLUSTER_TAIL; be.pcl = next) {
  1211. DBG_BUGON(!be.pcl);
  1212. next = READ_ONCE(be.pcl->next);
  1213. err = z_erofs_decompress_pcluster(&be, io->eio) ?: err;
  1214. }
  1215. return err;
  1216. }
  1217. static void z_erofs_decompressqueue_work(struct work_struct *work)
  1218. {
  1219. struct z_erofs_decompressqueue *bgq =
  1220. container_of(work, struct z_erofs_decompressqueue, u.work);
  1221. struct page *pagepool = NULL;
  1222. DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL);
  1223. z_erofs_decompress_queue(bgq, &pagepool);
  1224. erofs_release_pages(&pagepool);
  1225. kvfree(bgq);
  1226. }
  1227. #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
  1228. static void z_erofs_decompressqueue_kthread_work(struct kthread_work *work)
  1229. {
  1230. z_erofs_decompressqueue_work((struct work_struct *)work);
  1231. }
  1232. #endif
  1233. /* Use (kthread_)work in atomic contexts to minimize scheduling overhead */
  1234. static inline bool z_erofs_in_atomic(void)
  1235. {
  1236. if (IS_ENABLED(CONFIG_PREEMPTION) && rcu_preempt_depth())
  1237. return true;
  1238. if (!IS_ENABLED(CONFIG_PREEMPT_COUNT))
  1239. return true;
  1240. return !preemptible();
  1241. }
  1242. static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io,
  1243. int bios)
  1244. {
  1245. struct erofs_sb_info *const sbi = EROFS_SB(io->sb);
  1246. int gfp_flag;
  1247. /* wake up the caller thread for sync decompression */
  1248. if (io->sync) {
  1249. if (!atomic_add_return(bios, &io->pending_bios))
  1250. complete(&io->u.done);
  1251. return;
  1252. }
  1253. if (atomic_add_return(bios, &io->pending_bios))
  1254. return;
  1255. if (z_erofs_in_atomic()) {
  1256. #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
  1257. struct kthread_worker *worker;
  1258. rcu_read_lock();
  1259. worker = rcu_dereference(
  1260. z_erofs_pcpu_workers[raw_smp_processor_id()]);
  1261. if (!worker) {
  1262. INIT_WORK(&io->u.work, z_erofs_decompressqueue_work);
  1263. queue_work(z_erofs_workqueue, &io->u.work);
  1264. } else {
  1265. kthread_queue_work(worker, &io->u.kthread_work);
  1266. }
  1267. rcu_read_unlock();
  1268. #else
  1269. queue_work(z_erofs_workqueue, &io->u.work);
  1270. #endif
  1271. /* See `sync_decompress` in sysfs-fs-erofs for more details */
  1272. if (sbi->sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO)
  1273. sbi->sync_decompress = EROFS_SYNC_DECOMPRESS_FORCE_ON;
  1274. return;
  1275. }
  1276. gfp_flag = memalloc_noio_save();
  1277. z_erofs_decompressqueue_work(&io->u.work);
  1278. memalloc_noio_restore(gfp_flag);
  1279. }
  1280. static void z_erofs_fill_bio_vec(struct bio_vec *bvec,
  1281. struct z_erofs_frontend *f,
  1282. struct z_erofs_pcluster *pcl,
  1283. unsigned int nr,
  1284. struct address_space *mc)
  1285. {
  1286. gfp_t gfp = mapping_gfp_mask(mc);
  1287. bool tocache = false;
  1288. struct z_erofs_bvec zbv;
  1289. struct address_space *mapping;
  1290. struct folio *folio;
  1291. struct page *page;
  1292. int bs = i_blocksize(f->inode);
  1293. /* Except for inplace folios, the entire folio can be used for I/Os */
  1294. bvec->bv_offset = 0;
  1295. bvec->bv_len = PAGE_SIZE;
  1296. repeat:
  1297. spin_lock(&pcl->lockref.lock);
  1298. zbv = pcl->compressed_bvecs[nr];
  1299. spin_unlock(&pcl->lockref.lock);
  1300. if (!zbv.page)
  1301. goto out_allocfolio;
  1302. bvec->bv_page = zbv.page;
  1303. DBG_BUGON(z_erofs_is_shortlived_page(bvec->bv_page));
  1304. folio = page_folio(zbv.page);
  1305. /* For preallocated managed folios, add them to page cache here */
  1306. if (folio->private == Z_EROFS_PREALLOCATED_FOLIO) {
  1307. tocache = true;
  1308. goto out_tocache;
  1309. }
  1310. mapping = READ_ONCE(folio->mapping);
  1311. /*
  1312. * File-backed folios for inplace I/Os are all locked steady,
  1313. * therefore it is impossible for `mapping` to be NULL.
  1314. */
  1315. if (mapping && mapping != mc) {
  1316. if (zbv.offset < 0)
  1317. bvec->bv_offset = round_up(-zbv.offset, bs);
  1318. bvec->bv_len = round_up(zbv.end, bs) - bvec->bv_offset;
  1319. return;
  1320. }
  1321. folio_lock(folio);
  1322. if (likely(folio->mapping == mc)) {
  1323. /*
  1324. * The cached folio is still in managed cache but without
  1325. * a valid `->private` pcluster hint. Let's reconnect them.
  1326. */
  1327. if (!folio_test_private(folio)) {
  1328. folio_attach_private(folio, pcl);
  1329. /* compressed_bvecs[] already takes a ref before */
  1330. folio_put(folio);
  1331. }
  1332. if (likely(folio->private == pcl)) {
  1333. /* don't submit cache I/Os again if already uptodate */
  1334. if (folio_test_uptodate(folio)) {
  1335. folio_unlock(folio);
  1336. bvec->bv_page = NULL;
  1337. }
  1338. return;
  1339. }
  1340. /*
  1341. * Already linked with another pcluster, which only appears in
  1342. * crafted images by fuzzers for now. But handle this anyway.
  1343. */
  1344. tocache = false; /* use temporary short-lived pages */
  1345. } else {
  1346. DBG_BUGON(1); /* referenced managed folios can't be truncated */
  1347. tocache = true;
  1348. }
  1349. folio_unlock(folio);
  1350. folio_put(folio);
  1351. out_allocfolio:
  1352. page = __erofs_allocpage(&f->pagepool, gfp, true);
  1353. spin_lock(&pcl->lockref.lock);
  1354. if (unlikely(pcl->compressed_bvecs[nr].page != zbv.page)) {
  1355. if (page)
  1356. erofs_pagepool_add(&f->pagepool, page);
  1357. spin_unlock(&pcl->lockref.lock);
  1358. cond_resched();
  1359. goto repeat;
  1360. }
  1361. pcl->compressed_bvecs[nr].page = page ? page : ERR_PTR(-ENOMEM);
  1362. spin_unlock(&pcl->lockref.lock);
  1363. bvec->bv_page = page;
  1364. if (!page)
  1365. return;
  1366. folio = page_folio(page);
  1367. out_tocache:
  1368. if (!tocache || bs != PAGE_SIZE ||
  1369. filemap_add_folio(mc, folio, (pcl->pos >> PAGE_SHIFT) + nr, gfp)) {
  1370. /* turn into a temporary shortlived folio (1 ref) */
  1371. folio->private = (void *)Z_EROFS_SHORTLIVED_PAGE;
  1372. return;
  1373. }
  1374. folio_attach_private(folio, pcl);
  1375. /* drop a refcount added by allocpage (then 2 refs in total here) */
  1376. folio_put(folio);
  1377. }
  1378. static struct z_erofs_decompressqueue *jobqueue_init(struct super_block *sb,
  1379. struct z_erofs_decompressqueue *fgq, bool *fg)
  1380. {
  1381. struct z_erofs_decompressqueue *q;
  1382. if (fg && !*fg) {
  1383. q = kvzalloc_obj(*q, GFP_KERNEL | __GFP_NOWARN);
  1384. if (!q) {
  1385. *fg = true;
  1386. goto fg_out;
  1387. }
  1388. #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
  1389. kthread_init_work(&q->u.kthread_work,
  1390. z_erofs_decompressqueue_kthread_work);
  1391. #else
  1392. INIT_WORK(&q->u.work, z_erofs_decompressqueue_work);
  1393. #endif
  1394. } else {
  1395. fg_out:
  1396. q = fgq;
  1397. init_completion(&fgq->u.done);
  1398. atomic_set(&fgq->pending_bios, 0);
  1399. q->eio = false;
  1400. q->sync = true;
  1401. }
  1402. q->sb = sb;
  1403. q->head = Z_EROFS_PCLUSTER_TAIL;
  1404. return q;
  1405. }
  1406. /* define decompression jobqueue types */
  1407. enum {
  1408. JQ_BYPASS,
  1409. JQ_SUBMIT,
  1410. NR_JOBQUEUES,
  1411. };
  1412. static void z_erofs_move_to_bypass_queue(struct z_erofs_pcluster *pcl,
  1413. struct z_erofs_pcluster *next,
  1414. struct z_erofs_pcluster **qtail[])
  1415. {
  1416. WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL);
  1417. WRITE_ONCE(*qtail[JQ_SUBMIT], next);
  1418. WRITE_ONCE(*qtail[JQ_BYPASS], pcl);
  1419. qtail[JQ_BYPASS] = &pcl->next;
  1420. }
  1421. static void z_erofs_endio(struct bio *bio)
  1422. {
  1423. struct z_erofs_decompressqueue *q = bio->bi_private;
  1424. blk_status_t err = bio->bi_status;
  1425. struct folio_iter fi;
  1426. bio_for_each_folio_all(fi, bio) {
  1427. struct folio *folio = fi.folio;
  1428. DBG_BUGON(folio_test_uptodate(folio));
  1429. DBG_BUGON(z_erofs_page_is_invalidated(&folio->page));
  1430. if (!erofs_folio_is_managed(EROFS_SB(q->sb), folio))
  1431. continue;
  1432. if (!err)
  1433. folio_mark_uptodate(folio);
  1434. folio_unlock(folio);
  1435. }
  1436. if (err)
  1437. q->eio = true;
  1438. z_erofs_decompress_kickoff(q, -1);
  1439. if (bio->bi_bdev)
  1440. bio_put(bio);
  1441. }
  1442. static void z_erofs_submit_queue(struct z_erofs_frontend *f,
  1443. struct z_erofs_decompressqueue *fgq,
  1444. bool *force_fg, bool readahead)
  1445. {
  1446. struct super_block *sb = f->inode->i_sb;
  1447. struct address_space *mc = MNGD_MAPPING(EROFS_SB(sb));
  1448. struct z_erofs_pcluster **qtail[NR_JOBQUEUES];
  1449. struct z_erofs_decompressqueue *q[NR_JOBQUEUES];
  1450. struct z_erofs_pcluster *pcl, *next;
  1451. /* bio is NULL initially, so no need to initialize last_{index,bdev} */
  1452. erofs_off_t last_pa;
  1453. unsigned int nr_bios = 0;
  1454. struct bio *bio = NULL;
  1455. unsigned long pflags;
  1456. int memstall = 0;
  1457. /* No need to read from device for pclusters in the bypass queue. */
  1458. q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL);
  1459. q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, force_fg);
  1460. qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
  1461. qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
  1462. /* by default, all need io submission */
  1463. q[JQ_SUBMIT]->head = next = f->head;
  1464. do {
  1465. struct erofs_map_dev mdev;
  1466. erofs_off_t cur, end;
  1467. struct bio_vec bvec;
  1468. unsigned int i = 0;
  1469. bool bypass = true;
  1470. pcl = next;
  1471. next = READ_ONCE(pcl->next);
  1472. if (pcl->from_meta) {
  1473. z_erofs_move_to_bypass_queue(pcl, next, qtail);
  1474. continue;
  1475. }
  1476. /* no device id here, thus it will always succeed */
  1477. mdev = (struct erofs_map_dev) {
  1478. .m_pa = round_down(pcl->pos, sb->s_blocksize),
  1479. };
  1480. (void)erofs_map_dev(sb, &mdev);
  1481. cur = mdev.m_pa;
  1482. end = round_up(cur + pcl->pageofs_in + pcl->pclustersize,
  1483. sb->s_blocksize);
  1484. do {
  1485. bvec.bv_page = NULL;
  1486. if (bio && (cur != last_pa ||
  1487. bio->bi_bdev != mdev.m_bdev)) {
  1488. drain_io:
  1489. if (erofs_is_fileio_mode(EROFS_SB(sb)))
  1490. erofs_fileio_submit_bio(bio);
  1491. else if (erofs_is_fscache_mode(sb))
  1492. erofs_fscache_submit_bio(bio);
  1493. else
  1494. submit_bio(bio);
  1495. if (memstall) {
  1496. psi_memstall_leave(&pflags);
  1497. memstall = 0;
  1498. }
  1499. bio = NULL;
  1500. }
  1501. if (!bvec.bv_page) {
  1502. z_erofs_fill_bio_vec(&bvec, f, pcl, i++, mc);
  1503. if (!bvec.bv_page)
  1504. continue;
  1505. if (cur + bvec.bv_len > end)
  1506. bvec.bv_len = end - cur;
  1507. DBG_BUGON(bvec.bv_len < sb->s_blocksize);
  1508. }
  1509. if (unlikely(PageWorkingset(bvec.bv_page)) &&
  1510. !memstall) {
  1511. psi_memstall_enter(&pflags);
  1512. memstall = 1;
  1513. }
  1514. if (!bio) {
  1515. if (erofs_is_fileio_mode(EROFS_SB(sb)))
  1516. bio = erofs_fileio_bio_alloc(&mdev);
  1517. else if (erofs_is_fscache_mode(sb))
  1518. bio = erofs_fscache_bio_alloc(&mdev);
  1519. else
  1520. bio = bio_alloc(mdev.m_bdev, BIO_MAX_VECS,
  1521. REQ_OP_READ, GFP_NOIO);
  1522. bio->bi_end_io = z_erofs_endio;
  1523. bio->bi_iter.bi_sector =
  1524. (mdev.m_dif->fsoff + cur) >> 9;
  1525. bio->bi_private = q[JQ_SUBMIT];
  1526. if (readahead)
  1527. bio->bi_opf |= REQ_RAHEAD;
  1528. ++nr_bios;
  1529. }
  1530. if (!bio_add_page(bio, bvec.bv_page, bvec.bv_len,
  1531. bvec.bv_offset))
  1532. goto drain_io;
  1533. last_pa = cur + bvec.bv_len;
  1534. bypass = false;
  1535. } while ((cur += bvec.bv_len) < end);
  1536. if (!bypass)
  1537. qtail[JQ_SUBMIT] = &pcl->next;
  1538. else
  1539. z_erofs_move_to_bypass_queue(pcl, next, qtail);
  1540. } while (next != Z_EROFS_PCLUSTER_TAIL);
  1541. if (bio) {
  1542. if (erofs_is_fileio_mode(EROFS_SB(sb)))
  1543. erofs_fileio_submit_bio(bio);
  1544. else if (erofs_is_fscache_mode(sb))
  1545. erofs_fscache_submit_bio(bio);
  1546. else
  1547. submit_bio(bio);
  1548. }
  1549. if (memstall)
  1550. psi_memstall_leave(&pflags);
  1551. /*
  1552. * although background is preferred, no one is pending for submission.
  1553. * don't issue decompression but drop it directly instead.
  1554. */
  1555. if (!*force_fg && !nr_bios) {
  1556. kvfree(q[JQ_SUBMIT]);
  1557. return;
  1558. }
  1559. z_erofs_decompress_kickoff(q[JQ_SUBMIT], nr_bios);
  1560. }
  1561. static int z_erofs_runqueue(struct z_erofs_frontend *f, unsigned int rabytes)
  1562. {
  1563. struct z_erofs_decompressqueue io[NR_JOBQUEUES];
  1564. struct erofs_sb_info *sbi = EROFS_I_SB(f->inode);
  1565. int syncmode = sbi->sync_decompress;
  1566. bool force_fg;
  1567. int err;
  1568. force_fg = (syncmode == EROFS_SYNC_DECOMPRESS_AUTO && !rabytes) ||
  1569. (syncmode == EROFS_SYNC_DECOMPRESS_FORCE_ON &&
  1570. (rabytes <= Z_EROFS_MAX_SYNC_DECOMPRESS_BYTES));
  1571. if (f->head == Z_EROFS_PCLUSTER_TAIL)
  1572. return 0;
  1573. z_erofs_submit_queue(f, io, &force_fg, !!rabytes);
  1574. /* handle bypass queue (no i/o pclusters) immediately */
  1575. err = z_erofs_decompress_queue(&io[JQ_BYPASS], &f->pagepool);
  1576. if (!force_fg)
  1577. return err;
  1578. /* wait until all bios are completed */
  1579. wait_for_completion_io(&io[JQ_SUBMIT].u.done);
  1580. /* handle synchronous decompress queue in the caller context */
  1581. return z_erofs_decompress_queue(&io[JQ_SUBMIT], &f->pagepool) ?: err;
  1582. }
  1583. /*
  1584. * Since partial uptodate is still unimplemented for now, we have to use
  1585. * approximate readmore strategies as a start.
  1586. */
  1587. static void z_erofs_pcluster_readmore(struct z_erofs_frontend *f,
  1588. struct readahead_control *rac, bool backmost)
  1589. {
  1590. struct inode *inode = f->inode;
  1591. struct erofs_map_blocks *map = &f->map;
  1592. erofs_off_t cur, end, headoffset = f->headoffset;
  1593. int err;
  1594. if (backmost) {
  1595. if (rac)
  1596. end = headoffset + readahead_length(rac) - 1;
  1597. else
  1598. end = headoffset + PAGE_SIZE - 1;
  1599. map->m_la = end;
  1600. err = z_erofs_map_blocks_iter(inode, map,
  1601. EROFS_GET_BLOCKS_READMORE);
  1602. if (err || !(map->m_flags & EROFS_MAP_ENCODED))
  1603. return;
  1604. /* expand ra for the trailing edge if readahead */
  1605. if (rac) {
  1606. cur = round_up(map->m_la + map->m_llen, PAGE_SIZE);
  1607. readahead_expand(rac, headoffset, cur - headoffset);
  1608. return;
  1609. }
  1610. end = round_up(end, PAGE_SIZE);
  1611. } else {
  1612. end = round_up(map->m_la, PAGE_SIZE);
  1613. if (!(map->m_flags & EROFS_MAP_ENCODED) || !map->m_llen)
  1614. return;
  1615. }
  1616. cur = map->m_la + map->m_llen - 1;
  1617. while ((cur >= end) && (cur < i_size_read(inode))) {
  1618. pgoff_t index = cur >> PAGE_SHIFT;
  1619. struct folio *folio;
  1620. folio = erofs_grab_folio_nowait(f->sharedinode->i_mapping, index);
  1621. if (!IS_ERR_OR_NULL(folio)) {
  1622. if (folio_test_uptodate(folio))
  1623. folio_unlock(folio);
  1624. else
  1625. z_erofs_scan_folio(f, folio, !!rac);
  1626. folio_put(folio);
  1627. }
  1628. if (cur < PAGE_SIZE)
  1629. break;
  1630. cur = (index << PAGE_SHIFT) - 1;
  1631. }
  1632. }
  1633. static int z_erofs_read_folio(struct file *file, struct folio *folio)
  1634. {
  1635. struct inode *sharedinode = folio->mapping->host;
  1636. bool need_iput;
  1637. struct inode *realinode = erofs_real_inode(sharedinode, &need_iput);
  1638. Z_EROFS_DEFINE_FRONTEND(f, realinode, sharedinode, folio_pos(folio));
  1639. int err;
  1640. trace_erofs_read_folio(realinode, folio, false);
  1641. z_erofs_pcluster_readmore(&f, NULL, true);
  1642. err = z_erofs_scan_folio(&f, folio, false);
  1643. z_erofs_pcluster_readmore(&f, NULL, false);
  1644. z_erofs_pcluster_end(&f);
  1645. /* if some pclusters are ready, need submit them anyway */
  1646. err = z_erofs_runqueue(&f, 0) ?: err;
  1647. if (err && err != -EINTR)
  1648. erofs_err(realinode->i_sb, "read error %d @ %lu of nid %llu",
  1649. err, folio->index, EROFS_I(realinode)->nid);
  1650. erofs_put_metabuf(&f.map.buf);
  1651. erofs_release_pages(&f.pagepool);
  1652. if (need_iput)
  1653. iput(realinode);
  1654. return err;
  1655. }
  1656. static void z_erofs_readahead(struct readahead_control *rac)
  1657. {
  1658. struct inode *sharedinode = rac->mapping->host;
  1659. bool need_iput;
  1660. struct inode *realinode = erofs_real_inode(sharedinode, &need_iput);
  1661. Z_EROFS_DEFINE_FRONTEND(f, realinode, sharedinode, readahead_pos(rac));
  1662. unsigned int nrpages = readahead_count(rac);
  1663. struct folio *head = NULL, *folio;
  1664. int err;
  1665. trace_erofs_readahead(realinode, readahead_index(rac), nrpages, false);
  1666. z_erofs_pcluster_readmore(&f, rac, true);
  1667. while ((folio = readahead_folio(rac))) {
  1668. folio->private = head;
  1669. head = folio;
  1670. }
  1671. /* traverse in reverse order for best metadata I/O performance */
  1672. while (head) {
  1673. folio = head;
  1674. head = folio_get_private(folio);
  1675. err = z_erofs_scan_folio(&f, folio, true);
  1676. if (err && err != -EINTR)
  1677. erofs_err(realinode->i_sb, "readahead error at folio %lu @ nid %llu",
  1678. folio->index, EROFS_I(realinode)->nid);
  1679. }
  1680. z_erofs_pcluster_readmore(&f, rac, false);
  1681. z_erofs_pcluster_end(&f);
  1682. (void)z_erofs_runqueue(&f, nrpages << PAGE_SHIFT);
  1683. erofs_put_metabuf(&f.map.buf);
  1684. erofs_release_pages(&f.pagepool);
  1685. if (need_iput)
  1686. iput(realinode);
  1687. }
  1688. const struct address_space_operations z_erofs_aops = {
  1689. .read_folio = z_erofs_read_folio,
  1690. .readahead = z_erofs_readahead,
  1691. };