mtdswap.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536
  1. /*
  2. * Swap block device support for MTDs
  3. * Turns an MTD device into a swap device with block wear leveling
  4. *
  5. * Copyright © 2007,2011 Nokia Corporation. All rights reserved.
  6. *
  7. * Authors: Jarkko Lavinen <jarkko.lavinen@nokia.com>
  8. *
  9. * Based on Richard Purdie's earlier implementation in 2007. Background
  10. * support and lock-less operation written by Adrian Hunter.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * version 2 as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  24. * 02110-1301 USA
  25. */
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/mtd/mtd.h>
  29. #include <linux/mtd/blktrans.h>
  30. #include <linux/rbtree.h>
  31. #include <linux/sched.h>
  32. #include <linux/slab.h>
  33. #include <linux/vmalloc.h>
  34. #include <linux/genhd.h>
  35. #include <linux/swap.h>
  36. #include <linux/debugfs.h>
  37. #include <linux/seq_file.h>
  38. #include <linux/device.h>
  39. #include <linux/math64.h>
  40. #define MTDSWAP_PREFIX "mtdswap"
  41. /*
  42. * The number of free eraseblocks when GC should stop
  43. */
  44. #define CLEAN_BLOCK_THRESHOLD 20
  45. /*
  46. * Number of free eraseblocks below which GC can also collect low frag
  47. * blocks.
  48. */
  49. #define LOW_FRAG_GC_THRESHOLD 5
  50. /*
  51. * Wear level cost amortization. We want to do wear leveling on the background
  52. * without disturbing gc too much. This is made by defining max GC frequency.
  53. * Frequency value 6 means 1/6 of the GC passes will pick an erase block based
  54. * on the biggest wear difference rather than the biggest dirtiness.
  55. *
  56. * The lower freq2 should be chosen so that it makes sure the maximum erase
  57. * difference will decrease even if a malicious application is deliberately
  58. * trying to make erase differences large.
  59. */
  60. #define MAX_ERASE_DIFF 4000
  61. #define COLLECT_NONDIRTY_BASE MAX_ERASE_DIFF
  62. #define COLLECT_NONDIRTY_FREQ1 6
  63. #define COLLECT_NONDIRTY_FREQ2 4
  64. #define PAGE_UNDEF UINT_MAX
  65. #define BLOCK_UNDEF UINT_MAX
  66. #define BLOCK_ERROR (UINT_MAX - 1)
  67. #define BLOCK_MAX (UINT_MAX - 2)
  68. #define EBLOCK_BAD (1 << 0)
  69. #define EBLOCK_NOMAGIC (1 << 1)
  70. #define EBLOCK_BITFLIP (1 << 2)
  71. #define EBLOCK_FAILED (1 << 3)
  72. #define EBLOCK_READERR (1 << 4)
  73. #define EBLOCK_IDX_SHIFT 5
  74. struct swap_eb {
  75. struct rb_node rb;
  76. struct rb_root *root;
  77. unsigned int flags;
  78. unsigned int active_count;
  79. unsigned int erase_count;
  80. unsigned int pad; /* speeds up pointer decrement */
  81. };
  82. #define MTDSWAP_ECNT_MIN(rbroot) (rb_entry(rb_first(rbroot), struct swap_eb, \
  83. rb)->erase_count)
  84. #define MTDSWAP_ECNT_MAX(rbroot) (rb_entry(rb_last(rbroot), struct swap_eb, \
  85. rb)->erase_count)
  86. struct mtdswap_tree {
  87. struct rb_root root;
  88. unsigned int count;
  89. };
  90. enum {
  91. MTDSWAP_CLEAN,
  92. MTDSWAP_USED,
  93. MTDSWAP_LOWFRAG,
  94. MTDSWAP_HIFRAG,
  95. MTDSWAP_DIRTY,
  96. MTDSWAP_BITFLIP,
  97. MTDSWAP_FAILING,
  98. MTDSWAP_TREE_CNT,
  99. };
  100. struct mtdswap_dev {
  101. struct mtd_blktrans_dev *mbd_dev;
  102. struct mtd_info *mtd;
  103. struct device *dev;
  104. unsigned int *page_data;
  105. unsigned int *revmap;
  106. unsigned int eblks;
  107. unsigned int spare_eblks;
  108. unsigned int pages_per_eblk;
  109. unsigned int max_erase_count;
  110. struct swap_eb *eb_data;
  111. struct mtdswap_tree trees[MTDSWAP_TREE_CNT];
  112. unsigned long long sect_read_count;
  113. unsigned long long sect_write_count;
  114. unsigned long long mtd_write_count;
  115. unsigned long long mtd_read_count;
  116. unsigned long long discard_count;
  117. unsigned long long discard_page_count;
  118. unsigned int curr_write_pos;
  119. struct swap_eb *curr_write;
  120. char *page_buf;
  121. char *oob_buf;
  122. };
  123. struct mtdswap_oobdata {
  124. __le16 magic;
  125. __le32 count;
  126. } __packed;
  127. #define MTDSWAP_MAGIC_CLEAN 0x2095
  128. #define MTDSWAP_MAGIC_DIRTY (MTDSWAP_MAGIC_CLEAN + 1)
  129. #define MTDSWAP_TYPE_CLEAN 0
  130. #define MTDSWAP_TYPE_DIRTY 1
  131. #define MTDSWAP_OOBSIZE sizeof(struct mtdswap_oobdata)
  132. #define MTDSWAP_ERASE_RETRIES 3 /* Before marking erase block bad */
  133. #define MTDSWAP_IO_RETRIES 3
  134. enum {
  135. MTDSWAP_SCANNED_CLEAN,
  136. MTDSWAP_SCANNED_DIRTY,
  137. MTDSWAP_SCANNED_BITFLIP,
  138. MTDSWAP_SCANNED_BAD,
  139. };
  140. /*
  141. * In the worst case mtdswap_writesect() has allocated the last clean
  142. * page from the current block and is then pre-empted by the GC
  143. * thread. The thread can consume a full erase block when moving a
  144. * block.
  145. */
  146. #define MIN_SPARE_EBLOCKS 2
  147. #define MIN_ERASE_BLOCKS (MIN_SPARE_EBLOCKS + 1)
  148. #define TREE_ROOT(d, name) (&d->trees[MTDSWAP_ ## name].root)
  149. #define TREE_EMPTY(d, name) (TREE_ROOT(d, name)->rb_node == NULL)
  150. #define TREE_NONEMPTY(d, name) (!TREE_EMPTY(d, name))
  151. #define TREE_COUNT(d, name) (d->trees[MTDSWAP_ ## name].count)
  152. #define MTDSWAP_MBD_TO_MTDSWAP(dev) ((struct mtdswap_dev *)dev->priv)
  153. static char partitions[128] = "";
  154. module_param_string(partitions, partitions, sizeof(partitions), 0444);
  155. MODULE_PARM_DESC(partitions, "MTD partition numbers to use as swap "
  156. "partitions=\"1,3,5\"");
  157. static unsigned int spare_eblocks = 10;
  158. module_param(spare_eblocks, uint, 0444);
  159. MODULE_PARM_DESC(spare_eblocks, "Percentage of spare erase blocks for "
  160. "garbage collection (default 10%)");
  161. static bool header; /* false */
  162. module_param(header, bool, 0444);
  163. MODULE_PARM_DESC(header,
  164. "Include builtin swap header (default 0, without header)");
  165. static int mtdswap_gc(struct mtdswap_dev *d, unsigned int background);
  166. static loff_t mtdswap_eb_offset(struct mtdswap_dev *d, struct swap_eb *eb)
  167. {
  168. return (loff_t)(eb - d->eb_data) * d->mtd->erasesize;
  169. }
  170. static void mtdswap_eb_detach(struct mtdswap_dev *d, struct swap_eb *eb)
  171. {
  172. unsigned int oldidx;
  173. struct mtdswap_tree *tp;
  174. if (eb->root) {
  175. tp = container_of(eb->root, struct mtdswap_tree, root);
  176. oldidx = tp - &d->trees[0];
  177. d->trees[oldidx].count--;
  178. rb_erase(&eb->rb, eb->root);
  179. }
  180. }
  181. static void __mtdswap_rb_add(struct rb_root *root, struct swap_eb *eb)
  182. {
  183. struct rb_node **p, *parent = NULL;
  184. struct swap_eb *cur;
  185. p = &root->rb_node;
  186. while (*p) {
  187. parent = *p;
  188. cur = rb_entry(parent, struct swap_eb, rb);
  189. if (eb->erase_count > cur->erase_count)
  190. p = &(*p)->rb_right;
  191. else
  192. p = &(*p)->rb_left;
  193. }
  194. rb_link_node(&eb->rb, parent, p);
  195. rb_insert_color(&eb->rb, root);
  196. }
  197. static void mtdswap_rb_add(struct mtdswap_dev *d, struct swap_eb *eb, int idx)
  198. {
  199. struct rb_root *root;
  200. if (eb->root == &d->trees[idx].root)
  201. return;
  202. mtdswap_eb_detach(d, eb);
  203. root = &d->trees[idx].root;
  204. __mtdswap_rb_add(root, eb);
  205. eb->root = root;
  206. d->trees[idx].count++;
  207. }
  208. static struct rb_node *mtdswap_rb_index(struct rb_root *root, unsigned int idx)
  209. {
  210. struct rb_node *p;
  211. unsigned int i;
  212. p = rb_first(root);
  213. i = 0;
  214. while (i < idx && p) {
  215. p = rb_next(p);
  216. i++;
  217. }
  218. return p;
  219. }
  220. static int mtdswap_handle_badblock(struct mtdswap_dev *d, struct swap_eb *eb)
  221. {
  222. int ret;
  223. loff_t offset;
  224. d->spare_eblks--;
  225. eb->flags |= EBLOCK_BAD;
  226. mtdswap_eb_detach(d, eb);
  227. eb->root = NULL;
  228. /* badblocks not supported */
  229. if (!mtd_can_have_bb(d->mtd))
  230. return 1;
  231. offset = mtdswap_eb_offset(d, eb);
  232. dev_warn(d->dev, "Marking bad block at %08llx\n", offset);
  233. ret = mtd_block_markbad(d->mtd, offset);
  234. if (ret) {
  235. dev_warn(d->dev, "Mark block bad failed for block at %08llx "
  236. "error %d\n", offset, ret);
  237. return ret;
  238. }
  239. return 1;
  240. }
  241. static int mtdswap_handle_write_error(struct mtdswap_dev *d, struct swap_eb *eb)
  242. {
  243. unsigned int marked = eb->flags & EBLOCK_FAILED;
  244. struct swap_eb *curr_write = d->curr_write;
  245. eb->flags |= EBLOCK_FAILED;
  246. if (curr_write == eb) {
  247. d->curr_write = NULL;
  248. if (!marked && d->curr_write_pos != 0) {
  249. mtdswap_rb_add(d, eb, MTDSWAP_FAILING);
  250. return 0;
  251. }
  252. }
  253. return mtdswap_handle_badblock(d, eb);
  254. }
  255. static int mtdswap_read_oob(struct mtdswap_dev *d, loff_t from,
  256. struct mtd_oob_ops *ops)
  257. {
  258. int ret = mtd_read_oob(d->mtd, from, ops);
  259. if (mtd_is_bitflip(ret))
  260. return ret;
  261. if (ret) {
  262. dev_warn(d->dev, "Read OOB failed %d for block at %08llx\n",
  263. ret, from);
  264. return ret;
  265. }
  266. if (ops->oobretlen < ops->ooblen) {
  267. dev_warn(d->dev, "Read OOB return short read (%zd bytes not "
  268. "%zd) for block at %08llx\n",
  269. ops->oobretlen, ops->ooblen, from);
  270. return -EIO;
  271. }
  272. return 0;
  273. }
  274. static int mtdswap_read_markers(struct mtdswap_dev *d, struct swap_eb *eb)
  275. {
  276. struct mtdswap_oobdata *data, *data2;
  277. int ret;
  278. loff_t offset;
  279. struct mtd_oob_ops ops;
  280. offset = mtdswap_eb_offset(d, eb);
  281. /* Check first if the block is bad. */
  282. if (mtd_can_have_bb(d->mtd) && mtd_block_isbad(d->mtd, offset))
  283. return MTDSWAP_SCANNED_BAD;
  284. ops.ooblen = 2 * d->mtd->oobavail;
  285. ops.oobbuf = d->oob_buf;
  286. ops.ooboffs = 0;
  287. ops.datbuf = NULL;
  288. ops.mode = MTD_OPS_AUTO_OOB;
  289. ret = mtdswap_read_oob(d, offset, &ops);
  290. if (ret && !mtd_is_bitflip(ret))
  291. return ret;
  292. data = (struct mtdswap_oobdata *)d->oob_buf;
  293. data2 = (struct mtdswap_oobdata *)
  294. (d->oob_buf + d->mtd->oobavail);
  295. if (le16_to_cpu(data->magic) == MTDSWAP_MAGIC_CLEAN) {
  296. eb->erase_count = le32_to_cpu(data->count);
  297. if (mtd_is_bitflip(ret))
  298. ret = MTDSWAP_SCANNED_BITFLIP;
  299. else {
  300. if (le16_to_cpu(data2->magic) == MTDSWAP_MAGIC_DIRTY)
  301. ret = MTDSWAP_SCANNED_DIRTY;
  302. else
  303. ret = MTDSWAP_SCANNED_CLEAN;
  304. }
  305. } else {
  306. eb->flags |= EBLOCK_NOMAGIC;
  307. ret = MTDSWAP_SCANNED_DIRTY;
  308. }
  309. return ret;
  310. }
  311. static int mtdswap_write_marker(struct mtdswap_dev *d, struct swap_eb *eb,
  312. u16 marker)
  313. {
  314. struct mtdswap_oobdata n;
  315. int ret;
  316. loff_t offset;
  317. struct mtd_oob_ops ops;
  318. ops.ooboffs = 0;
  319. ops.oobbuf = (uint8_t *)&n;
  320. ops.mode = MTD_OPS_AUTO_OOB;
  321. ops.datbuf = NULL;
  322. if (marker == MTDSWAP_TYPE_CLEAN) {
  323. n.magic = cpu_to_le16(MTDSWAP_MAGIC_CLEAN);
  324. n.count = cpu_to_le32(eb->erase_count);
  325. ops.ooblen = MTDSWAP_OOBSIZE;
  326. offset = mtdswap_eb_offset(d, eb);
  327. } else {
  328. n.magic = cpu_to_le16(MTDSWAP_MAGIC_DIRTY);
  329. ops.ooblen = sizeof(n.magic);
  330. offset = mtdswap_eb_offset(d, eb) + d->mtd->writesize;
  331. }
  332. ret = mtd_write_oob(d->mtd, offset, &ops);
  333. if (ret) {
  334. dev_warn(d->dev, "Write OOB failed for block at %08llx "
  335. "error %d\n", offset, ret);
  336. if (ret == -EIO || mtd_is_eccerr(ret))
  337. mtdswap_handle_write_error(d, eb);
  338. return ret;
  339. }
  340. if (ops.oobretlen != ops.ooblen) {
  341. dev_warn(d->dev, "Short OOB write for block at %08llx: "
  342. "%zd not %zd\n",
  343. offset, ops.oobretlen, ops.ooblen);
  344. return ret;
  345. }
  346. return 0;
  347. }
  348. /*
  349. * Are there any erase blocks without MAGIC_CLEAN header, presumably
  350. * because power was cut off after erase but before header write? We
  351. * need to guestimate the erase count.
  352. */
  353. static void mtdswap_check_counts(struct mtdswap_dev *d)
  354. {
  355. struct rb_root hist_root = RB_ROOT;
  356. struct rb_node *medrb;
  357. struct swap_eb *eb;
  358. unsigned int i, cnt, median;
  359. cnt = 0;
  360. for (i = 0; i < d->eblks; i++) {
  361. eb = d->eb_data + i;
  362. if (eb->flags & (EBLOCK_NOMAGIC | EBLOCK_BAD | EBLOCK_READERR))
  363. continue;
  364. __mtdswap_rb_add(&hist_root, eb);
  365. cnt++;
  366. }
  367. if (cnt == 0)
  368. return;
  369. medrb = mtdswap_rb_index(&hist_root, cnt / 2);
  370. median = rb_entry(medrb, struct swap_eb, rb)->erase_count;
  371. d->max_erase_count = MTDSWAP_ECNT_MAX(&hist_root);
  372. for (i = 0; i < d->eblks; i++) {
  373. eb = d->eb_data + i;
  374. if (eb->flags & (EBLOCK_NOMAGIC | EBLOCK_READERR))
  375. eb->erase_count = median;
  376. if (eb->flags & (EBLOCK_NOMAGIC | EBLOCK_BAD | EBLOCK_READERR))
  377. continue;
  378. rb_erase(&eb->rb, &hist_root);
  379. }
  380. }
  381. static void mtdswap_scan_eblks(struct mtdswap_dev *d)
  382. {
  383. int status;
  384. unsigned int i, idx;
  385. struct swap_eb *eb;
  386. for (i = 0; i < d->eblks; i++) {
  387. eb = d->eb_data + i;
  388. status = mtdswap_read_markers(d, eb);
  389. if (status < 0)
  390. eb->flags |= EBLOCK_READERR;
  391. else if (status == MTDSWAP_SCANNED_BAD) {
  392. eb->flags |= EBLOCK_BAD;
  393. continue;
  394. }
  395. switch (status) {
  396. case MTDSWAP_SCANNED_CLEAN:
  397. idx = MTDSWAP_CLEAN;
  398. break;
  399. case MTDSWAP_SCANNED_DIRTY:
  400. case MTDSWAP_SCANNED_BITFLIP:
  401. idx = MTDSWAP_DIRTY;
  402. break;
  403. default:
  404. idx = MTDSWAP_FAILING;
  405. }
  406. eb->flags |= (idx << EBLOCK_IDX_SHIFT);
  407. }
  408. mtdswap_check_counts(d);
  409. for (i = 0; i < d->eblks; i++) {
  410. eb = d->eb_data + i;
  411. if (eb->flags & EBLOCK_BAD)
  412. continue;
  413. idx = eb->flags >> EBLOCK_IDX_SHIFT;
  414. mtdswap_rb_add(d, eb, idx);
  415. }
  416. }
  417. /*
  418. * Place eblk into a tree corresponding to its number of active blocks
  419. * it contains.
  420. */
  421. static void mtdswap_store_eb(struct mtdswap_dev *d, struct swap_eb *eb)
  422. {
  423. unsigned int weight = eb->active_count;
  424. unsigned int maxweight = d->pages_per_eblk;
  425. if (eb == d->curr_write)
  426. return;
  427. if (eb->flags & EBLOCK_BITFLIP)
  428. mtdswap_rb_add(d, eb, MTDSWAP_BITFLIP);
  429. else if (eb->flags & (EBLOCK_READERR | EBLOCK_FAILED))
  430. mtdswap_rb_add(d, eb, MTDSWAP_FAILING);
  431. if (weight == maxweight)
  432. mtdswap_rb_add(d, eb, MTDSWAP_USED);
  433. else if (weight == 0)
  434. mtdswap_rb_add(d, eb, MTDSWAP_DIRTY);
  435. else if (weight > (maxweight/2))
  436. mtdswap_rb_add(d, eb, MTDSWAP_LOWFRAG);
  437. else
  438. mtdswap_rb_add(d, eb, MTDSWAP_HIFRAG);
  439. }
  440. static int mtdswap_erase_block(struct mtdswap_dev *d, struct swap_eb *eb)
  441. {
  442. struct mtd_info *mtd = d->mtd;
  443. struct erase_info erase;
  444. unsigned int retries = 0;
  445. int ret;
  446. eb->erase_count++;
  447. if (eb->erase_count > d->max_erase_count)
  448. d->max_erase_count = eb->erase_count;
  449. retry:
  450. memset(&erase, 0, sizeof(struct erase_info));
  451. erase.addr = mtdswap_eb_offset(d, eb);
  452. erase.len = mtd->erasesize;
  453. ret = mtd_erase(mtd, &erase);
  454. if (ret) {
  455. if (retries++ < MTDSWAP_ERASE_RETRIES) {
  456. dev_warn(d->dev,
  457. "erase of erase block %#llx on %s failed",
  458. erase.addr, mtd->name);
  459. yield();
  460. goto retry;
  461. }
  462. dev_err(d->dev, "Cannot erase erase block %#llx on %s\n",
  463. erase.addr, mtd->name);
  464. mtdswap_handle_badblock(d, eb);
  465. return -EIO;
  466. }
  467. return 0;
  468. }
  469. static int mtdswap_map_free_block(struct mtdswap_dev *d, unsigned int page,
  470. unsigned int *block)
  471. {
  472. int ret;
  473. struct swap_eb *old_eb = d->curr_write;
  474. struct rb_root *clean_root;
  475. struct swap_eb *eb;
  476. if (old_eb == NULL || d->curr_write_pos >= d->pages_per_eblk) {
  477. do {
  478. if (TREE_EMPTY(d, CLEAN))
  479. return -ENOSPC;
  480. clean_root = TREE_ROOT(d, CLEAN);
  481. eb = rb_entry(rb_first(clean_root), struct swap_eb, rb);
  482. rb_erase(&eb->rb, clean_root);
  483. eb->root = NULL;
  484. TREE_COUNT(d, CLEAN)--;
  485. ret = mtdswap_write_marker(d, eb, MTDSWAP_TYPE_DIRTY);
  486. } while (ret == -EIO || mtd_is_eccerr(ret));
  487. if (ret)
  488. return ret;
  489. d->curr_write_pos = 0;
  490. d->curr_write = eb;
  491. if (old_eb)
  492. mtdswap_store_eb(d, old_eb);
  493. }
  494. *block = (d->curr_write - d->eb_data) * d->pages_per_eblk +
  495. d->curr_write_pos;
  496. d->curr_write->active_count++;
  497. d->revmap[*block] = page;
  498. d->curr_write_pos++;
  499. return 0;
  500. }
  501. static unsigned int mtdswap_free_page_cnt(struct mtdswap_dev *d)
  502. {
  503. return TREE_COUNT(d, CLEAN) * d->pages_per_eblk +
  504. d->pages_per_eblk - d->curr_write_pos;
  505. }
  506. static unsigned int mtdswap_enough_free_pages(struct mtdswap_dev *d)
  507. {
  508. return mtdswap_free_page_cnt(d) > d->pages_per_eblk;
  509. }
  510. static int mtdswap_write_block(struct mtdswap_dev *d, char *buf,
  511. unsigned int page, unsigned int *bp, int gc_context)
  512. {
  513. struct mtd_info *mtd = d->mtd;
  514. struct swap_eb *eb;
  515. size_t retlen;
  516. loff_t writepos;
  517. int ret;
  518. retry:
  519. if (!gc_context)
  520. while (!mtdswap_enough_free_pages(d))
  521. if (mtdswap_gc(d, 0) > 0)
  522. return -ENOSPC;
  523. ret = mtdswap_map_free_block(d, page, bp);
  524. eb = d->eb_data + (*bp / d->pages_per_eblk);
  525. if (ret == -EIO || mtd_is_eccerr(ret)) {
  526. d->curr_write = NULL;
  527. eb->active_count--;
  528. d->revmap[*bp] = PAGE_UNDEF;
  529. goto retry;
  530. }
  531. if (ret < 0)
  532. return ret;
  533. writepos = (loff_t)*bp << PAGE_SHIFT;
  534. ret = mtd_write(mtd, writepos, PAGE_SIZE, &retlen, buf);
  535. if (ret == -EIO || mtd_is_eccerr(ret)) {
  536. d->curr_write_pos--;
  537. eb->active_count--;
  538. d->revmap[*bp] = PAGE_UNDEF;
  539. mtdswap_handle_write_error(d, eb);
  540. goto retry;
  541. }
  542. if (ret < 0) {
  543. dev_err(d->dev, "Write to MTD device failed: %d (%zd written)",
  544. ret, retlen);
  545. goto err;
  546. }
  547. if (retlen != PAGE_SIZE) {
  548. dev_err(d->dev, "Short write to MTD device: %zd written",
  549. retlen);
  550. ret = -EIO;
  551. goto err;
  552. }
  553. return ret;
  554. err:
  555. d->curr_write_pos--;
  556. eb->active_count--;
  557. d->revmap[*bp] = PAGE_UNDEF;
  558. return ret;
  559. }
  560. static int mtdswap_move_block(struct mtdswap_dev *d, unsigned int oldblock,
  561. unsigned int *newblock)
  562. {
  563. struct mtd_info *mtd = d->mtd;
  564. struct swap_eb *eb, *oldeb;
  565. int ret;
  566. size_t retlen;
  567. unsigned int page, retries;
  568. loff_t readpos;
  569. page = d->revmap[oldblock];
  570. readpos = (loff_t) oldblock << PAGE_SHIFT;
  571. retries = 0;
  572. retry:
  573. ret = mtd_read(mtd, readpos, PAGE_SIZE, &retlen, d->page_buf);
  574. if (ret < 0 && !mtd_is_bitflip(ret)) {
  575. oldeb = d->eb_data + oldblock / d->pages_per_eblk;
  576. oldeb->flags |= EBLOCK_READERR;
  577. dev_err(d->dev, "Read Error: %d (block %u)\n", ret,
  578. oldblock);
  579. retries++;
  580. if (retries < MTDSWAP_IO_RETRIES)
  581. goto retry;
  582. goto read_error;
  583. }
  584. if (retlen != PAGE_SIZE) {
  585. dev_err(d->dev, "Short read: %zd (block %u)\n", retlen,
  586. oldblock);
  587. ret = -EIO;
  588. goto read_error;
  589. }
  590. ret = mtdswap_write_block(d, d->page_buf, page, newblock, 1);
  591. if (ret < 0) {
  592. d->page_data[page] = BLOCK_ERROR;
  593. dev_err(d->dev, "Write error: %d\n", ret);
  594. return ret;
  595. }
  596. eb = d->eb_data + *newblock / d->pages_per_eblk;
  597. d->page_data[page] = *newblock;
  598. d->revmap[oldblock] = PAGE_UNDEF;
  599. eb = d->eb_data + oldblock / d->pages_per_eblk;
  600. eb->active_count--;
  601. return 0;
  602. read_error:
  603. d->page_data[page] = BLOCK_ERROR;
  604. d->revmap[oldblock] = PAGE_UNDEF;
  605. return ret;
  606. }
  607. static int mtdswap_gc_eblock(struct mtdswap_dev *d, struct swap_eb *eb)
  608. {
  609. unsigned int i, block, eblk_base, newblock;
  610. int ret, errcode;
  611. errcode = 0;
  612. eblk_base = (eb - d->eb_data) * d->pages_per_eblk;
  613. for (i = 0; i < d->pages_per_eblk; i++) {
  614. if (d->spare_eblks < MIN_SPARE_EBLOCKS)
  615. return -ENOSPC;
  616. block = eblk_base + i;
  617. if (d->revmap[block] == PAGE_UNDEF)
  618. continue;
  619. ret = mtdswap_move_block(d, block, &newblock);
  620. if (ret < 0 && !errcode)
  621. errcode = ret;
  622. }
  623. return errcode;
  624. }
  625. static int __mtdswap_choose_gc_tree(struct mtdswap_dev *d)
  626. {
  627. int idx, stopat;
  628. if (TREE_COUNT(d, CLEAN) < LOW_FRAG_GC_THRESHOLD)
  629. stopat = MTDSWAP_LOWFRAG;
  630. else
  631. stopat = MTDSWAP_HIFRAG;
  632. for (idx = MTDSWAP_BITFLIP; idx >= stopat; idx--)
  633. if (d->trees[idx].root.rb_node != NULL)
  634. return idx;
  635. return -1;
  636. }
  637. static int mtdswap_wlfreq(unsigned int maxdiff)
  638. {
  639. unsigned int h, x, y, dist, base;
  640. /*
  641. * Calculate linear ramp down from f1 to f2 when maxdiff goes from
  642. * MAX_ERASE_DIFF to MAX_ERASE_DIFF + COLLECT_NONDIRTY_BASE. Similar
  643. * to triangle with height f1 - f1 and width COLLECT_NONDIRTY_BASE.
  644. */
  645. dist = maxdiff - MAX_ERASE_DIFF;
  646. if (dist > COLLECT_NONDIRTY_BASE)
  647. dist = COLLECT_NONDIRTY_BASE;
  648. /*
  649. * Modelling the slop as right angular triangle with base
  650. * COLLECT_NONDIRTY_BASE and height freq1 - freq2. The ratio y/x is
  651. * equal to the ratio h/base.
  652. */
  653. h = COLLECT_NONDIRTY_FREQ1 - COLLECT_NONDIRTY_FREQ2;
  654. base = COLLECT_NONDIRTY_BASE;
  655. x = dist - base;
  656. y = (x * h + base / 2) / base;
  657. return COLLECT_NONDIRTY_FREQ2 + y;
  658. }
  659. static int mtdswap_choose_wl_tree(struct mtdswap_dev *d)
  660. {
  661. static unsigned int pick_cnt;
  662. unsigned int i, idx = -1, wear, max;
  663. struct rb_root *root;
  664. max = 0;
  665. for (i = 0; i <= MTDSWAP_DIRTY; i++) {
  666. root = &d->trees[i].root;
  667. if (root->rb_node == NULL)
  668. continue;
  669. wear = d->max_erase_count - MTDSWAP_ECNT_MIN(root);
  670. if (wear > max) {
  671. max = wear;
  672. idx = i;
  673. }
  674. }
  675. if (max > MAX_ERASE_DIFF && pick_cnt >= mtdswap_wlfreq(max) - 1) {
  676. pick_cnt = 0;
  677. return idx;
  678. }
  679. pick_cnt++;
  680. return -1;
  681. }
  682. static int mtdswap_choose_gc_tree(struct mtdswap_dev *d,
  683. unsigned int background)
  684. {
  685. int idx;
  686. if (TREE_NONEMPTY(d, FAILING) &&
  687. (background || (TREE_EMPTY(d, CLEAN) && TREE_EMPTY(d, DIRTY))))
  688. return MTDSWAP_FAILING;
  689. idx = mtdswap_choose_wl_tree(d);
  690. if (idx >= MTDSWAP_CLEAN)
  691. return idx;
  692. return __mtdswap_choose_gc_tree(d);
  693. }
  694. static struct swap_eb *mtdswap_pick_gc_eblk(struct mtdswap_dev *d,
  695. unsigned int background)
  696. {
  697. struct rb_root *rp = NULL;
  698. struct swap_eb *eb = NULL;
  699. int idx;
  700. if (background && TREE_COUNT(d, CLEAN) > CLEAN_BLOCK_THRESHOLD &&
  701. TREE_EMPTY(d, DIRTY) && TREE_EMPTY(d, FAILING))
  702. return NULL;
  703. idx = mtdswap_choose_gc_tree(d, background);
  704. if (idx < 0)
  705. return NULL;
  706. rp = &d->trees[idx].root;
  707. eb = rb_entry(rb_first(rp), struct swap_eb, rb);
  708. rb_erase(&eb->rb, rp);
  709. eb->root = NULL;
  710. d->trees[idx].count--;
  711. return eb;
  712. }
  713. static unsigned int mtdswap_test_patt(unsigned int i)
  714. {
  715. return i % 2 ? 0x55555555 : 0xAAAAAAAA;
  716. }
  717. static unsigned int mtdswap_eblk_passes(struct mtdswap_dev *d,
  718. struct swap_eb *eb)
  719. {
  720. struct mtd_info *mtd = d->mtd;
  721. unsigned int test, i, j, patt, mtd_pages;
  722. loff_t base, pos;
  723. unsigned int *p1 = (unsigned int *)d->page_buf;
  724. unsigned char *p2 = (unsigned char *)d->oob_buf;
  725. struct mtd_oob_ops ops;
  726. int ret;
  727. ops.mode = MTD_OPS_AUTO_OOB;
  728. ops.len = mtd->writesize;
  729. ops.ooblen = mtd->oobavail;
  730. ops.ooboffs = 0;
  731. ops.datbuf = d->page_buf;
  732. ops.oobbuf = d->oob_buf;
  733. base = mtdswap_eb_offset(d, eb);
  734. mtd_pages = d->pages_per_eblk * PAGE_SIZE / mtd->writesize;
  735. for (test = 0; test < 2; test++) {
  736. pos = base;
  737. for (i = 0; i < mtd_pages; i++) {
  738. patt = mtdswap_test_patt(test + i);
  739. memset(d->page_buf, patt, mtd->writesize);
  740. memset(d->oob_buf, patt, mtd->oobavail);
  741. ret = mtd_write_oob(mtd, pos, &ops);
  742. if (ret)
  743. goto error;
  744. pos += mtd->writesize;
  745. }
  746. pos = base;
  747. for (i = 0; i < mtd_pages; i++) {
  748. ret = mtd_read_oob(mtd, pos, &ops);
  749. if (ret)
  750. goto error;
  751. patt = mtdswap_test_patt(test + i);
  752. for (j = 0; j < mtd->writesize/sizeof(int); j++)
  753. if (p1[j] != patt)
  754. goto error;
  755. for (j = 0; j < mtd->oobavail; j++)
  756. if (p2[j] != (unsigned char)patt)
  757. goto error;
  758. pos += mtd->writesize;
  759. }
  760. ret = mtdswap_erase_block(d, eb);
  761. if (ret)
  762. goto error;
  763. }
  764. eb->flags &= ~EBLOCK_READERR;
  765. return 1;
  766. error:
  767. mtdswap_handle_badblock(d, eb);
  768. return 0;
  769. }
  770. static int mtdswap_gc(struct mtdswap_dev *d, unsigned int background)
  771. {
  772. struct swap_eb *eb;
  773. int ret;
  774. if (d->spare_eblks < MIN_SPARE_EBLOCKS)
  775. return 1;
  776. eb = mtdswap_pick_gc_eblk(d, background);
  777. if (!eb)
  778. return 1;
  779. ret = mtdswap_gc_eblock(d, eb);
  780. if (ret == -ENOSPC)
  781. return 1;
  782. if (eb->flags & EBLOCK_FAILED) {
  783. mtdswap_handle_badblock(d, eb);
  784. return 0;
  785. }
  786. eb->flags &= ~EBLOCK_BITFLIP;
  787. ret = mtdswap_erase_block(d, eb);
  788. if ((eb->flags & EBLOCK_READERR) &&
  789. (ret || !mtdswap_eblk_passes(d, eb)))
  790. return 0;
  791. if (ret == 0)
  792. ret = mtdswap_write_marker(d, eb, MTDSWAP_TYPE_CLEAN);
  793. if (ret == 0)
  794. mtdswap_rb_add(d, eb, MTDSWAP_CLEAN);
  795. else if (ret != -EIO && !mtd_is_eccerr(ret))
  796. mtdswap_rb_add(d, eb, MTDSWAP_DIRTY);
  797. return 0;
  798. }
  799. static void mtdswap_background(struct mtd_blktrans_dev *dev)
  800. {
  801. struct mtdswap_dev *d = MTDSWAP_MBD_TO_MTDSWAP(dev);
  802. int ret;
  803. while (1) {
  804. ret = mtdswap_gc(d, 1);
  805. if (ret || mtd_blktrans_cease_background(dev))
  806. return;
  807. }
  808. }
  809. static void mtdswap_cleanup(struct mtdswap_dev *d)
  810. {
  811. vfree(d->eb_data);
  812. vfree(d->revmap);
  813. vfree(d->page_data);
  814. kfree(d->oob_buf);
  815. kfree(d->page_buf);
  816. }
  817. static int mtdswap_flush(struct mtd_blktrans_dev *dev)
  818. {
  819. struct mtdswap_dev *d = MTDSWAP_MBD_TO_MTDSWAP(dev);
  820. mtd_sync(d->mtd);
  821. return 0;
  822. }
  823. static unsigned int mtdswap_badblocks(struct mtd_info *mtd, uint64_t size)
  824. {
  825. loff_t offset;
  826. unsigned int badcnt;
  827. badcnt = 0;
  828. if (mtd_can_have_bb(mtd))
  829. for (offset = 0; offset < size; offset += mtd->erasesize)
  830. if (mtd_block_isbad(mtd, offset))
  831. badcnt++;
  832. return badcnt;
  833. }
  834. static int mtdswap_writesect(struct mtd_blktrans_dev *dev,
  835. unsigned long page, char *buf)
  836. {
  837. struct mtdswap_dev *d = MTDSWAP_MBD_TO_MTDSWAP(dev);
  838. unsigned int newblock, mapped;
  839. struct swap_eb *eb;
  840. int ret;
  841. d->sect_write_count++;
  842. if (d->spare_eblks < MIN_SPARE_EBLOCKS)
  843. return -ENOSPC;
  844. if (header) {
  845. /* Ignore writes to the header page */
  846. if (unlikely(page == 0))
  847. return 0;
  848. page--;
  849. }
  850. mapped = d->page_data[page];
  851. if (mapped <= BLOCK_MAX) {
  852. eb = d->eb_data + (mapped / d->pages_per_eblk);
  853. eb->active_count--;
  854. mtdswap_store_eb(d, eb);
  855. d->page_data[page] = BLOCK_UNDEF;
  856. d->revmap[mapped] = PAGE_UNDEF;
  857. }
  858. ret = mtdswap_write_block(d, buf, page, &newblock, 0);
  859. d->mtd_write_count++;
  860. if (ret < 0)
  861. return ret;
  862. eb = d->eb_data + (newblock / d->pages_per_eblk);
  863. d->page_data[page] = newblock;
  864. return 0;
  865. }
  866. /* Provide a dummy swap header for the kernel */
  867. static int mtdswap_auto_header(struct mtdswap_dev *d, char *buf)
  868. {
  869. union swap_header *hd = (union swap_header *)(buf);
  870. memset(buf, 0, PAGE_SIZE - 10);
  871. hd->info.version = 1;
  872. hd->info.last_page = d->mbd_dev->size - 1;
  873. hd->info.nr_badpages = 0;
  874. memcpy(buf + PAGE_SIZE - 10, "SWAPSPACE2", 10);
  875. return 0;
  876. }
  877. static int mtdswap_readsect(struct mtd_blktrans_dev *dev,
  878. unsigned long page, char *buf)
  879. {
  880. struct mtdswap_dev *d = MTDSWAP_MBD_TO_MTDSWAP(dev);
  881. struct mtd_info *mtd = d->mtd;
  882. unsigned int realblock, retries;
  883. loff_t readpos;
  884. struct swap_eb *eb;
  885. size_t retlen;
  886. int ret;
  887. d->sect_read_count++;
  888. if (header) {
  889. if (unlikely(page == 0))
  890. return mtdswap_auto_header(d, buf);
  891. page--;
  892. }
  893. realblock = d->page_data[page];
  894. if (realblock > BLOCK_MAX) {
  895. memset(buf, 0x0, PAGE_SIZE);
  896. if (realblock == BLOCK_UNDEF)
  897. return 0;
  898. else
  899. return -EIO;
  900. }
  901. eb = d->eb_data + (realblock / d->pages_per_eblk);
  902. BUG_ON(d->revmap[realblock] == PAGE_UNDEF);
  903. readpos = (loff_t)realblock << PAGE_SHIFT;
  904. retries = 0;
  905. retry:
  906. ret = mtd_read(mtd, readpos, PAGE_SIZE, &retlen, buf);
  907. d->mtd_read_count++;
  908. if (mtd_is_bitflip(ret)) {
  909. eb->flags |= EBLOCK_BITFLIP;
  910. mtdswap_rb_add(d, eb, MTDSWAP_BITFLIP);
  911. ret = 0;
  912. }
  913. if (ret < 0) {
  914. dev_err(d->dev, "Read error %d\n", ret);
  915. eb->flags |= EBLOCK_READERR;
  916. mtdswap_rb_add(d, eb, MTDSWAP_FAILING);
  917. retries++;
  918. if (retries < MTDSWAP_IO_RETRIES)
  919. goto retry;
  920. return ret;
  921. }
  922. if (retlen != PAGE_SIZE) {
  923. dev_err(d->dev, "Short read %zd\n", retlen);
  924. return -EIO;
  925. }
  926. return 0;
  927. }
  928. static int mtdswap_discard(struct mtd_blktrans_dev *dev, unsigned long first,
  929. unsigned nr_pages)
  930. {
  931. struct mtdswap_dev *d = MTDSWAP_MBD_TO_MTDSWAP(dev);
  932. unsigned long page;
  933. struct swap_eb *eb;
  934. unsigned int mapped;
  935. d->discard_count++;
  936. for (page = first; page < first + nr_pages; page++) {
  937. mapped = d->page_data[page];
  938. if (mapped <= BLOCK_MAX) {
  939. eb = d->eb_data + (mapped / d->pages_per_eblk);
  940. eb->active_count--;
  941. mtdswap_store_eb(d, eb);
  942. d->page_data[page] = BLOCK_UNDEF;
  943. d->revmap[mapped] = PAGE_UNDEF;
  944. d->discard_page_count++;
  945. } else if (mapped == BLOCK_ERROR) {
  946. d->page_data[page] = BLOCK_UNDEF;
  947. d->discard_page_count++;
  948. }
  949. }
  950. return 0;
  951. }
  952. static int mtdswap_show(struct seq_file *s, void *data)
  953. {
  954. struct mtdswap_dev *d = (struct mtdswap_dev *) s->private;
  955. unsigned long sum;
  956. unsigned int count[MTDSWAP_TREE_CNT];
  957. unsigned int min[MTDSWAP_TREE_CNT];
  958. unsigned int max[MTDSWAP_TREE_CNT];
  959. unsigned int i, cw = 0, cwp = 0, cwecount = 0, bb_cnt, mapped, pages;
  960. uint64_t use_size;
  961. static const char * const name[] = {
  962. "clean", "used", "low", "high", "dirty", "bitflip", "failing"
  963. };
  964. mutex_lock(&d->mbd_dev->lock);
  965. for (i = 0; i < MTDSWAP_TREE_CNT; i++) {
  966. struct rb_root *root = &d->trees[i].root;
  967. if (root->rb_node) {
  968. count[i] = d->trees[i].count;
  969. min[i] = MTDSWAP_ECNT_MIN(root);
  970. max[i] = MTDSWAP_ECNT_MAX(root);
  971. } else
  972. count[i] = 0;
  973. }
  974. if (d->curr_write) {
  975. cw = 1;
  976. cwp = d->curr_write_pos;
  977. cwecount = d->curr_write->erase_count;
  978. }
  979. sum = 0;
  980. for (i = 0; i < d->eblks; i++)
  981. sum += d->eb_data[i].erase_count;
  982. use_size = (uint64_t)d->eblks * d->mtd->erasesize;
  983. bb_cnt = mtdswap_badblocks(d->mtd, use_size);
  984. mapped = 0;
  985. pages = d->mbd_dev->size;
  986. for (i = 0; i < pages; i++)
  987. if (d->page_data[i] != BLOCK_UNDEF)
  988. mapped++;
  989. mutex_unlock(&d->mbd_dev->lock);
  990. for (i = 0; i < MTDSWAP_TREE_CNT; i++) {
  991. if (!count[i])
  992. continue;
  993. if (min[i] != max[i])
  994. seq_printf(s, "%s:\t%5d erase blocks, erased min %d, "
  995. "max %d times\n",
  996. name[i], count[i], min[i], max[i]);
  997. else
  998. seq_printf(s, "%s:\t%5d erase blocks, all erased %d "
  999. "times\n", name[i], count[i], min[i]);
  1000. }
  1001. if (bb_cnt)
  1002. seq_printf(s, "bad:\t%5u erase blocks\n", bb_cnt);
  1003. if (cw)
  1004. seq_printf(s, "current erase block: %u pages used, %u free, "
  1005. "erased %u times\n",
  1006. cwp, d->pages_per_eblk - cwp, cwecount);
  1007. seq_printf(s, "total erasures: %lu\n", sum);
  1008. seq_puts(s, "\n");
  1009. seq_printf(s, "mtdswap_readsect count: %llu\n", d->sect_read_count);
  1010. seq_printf(s, "mtdswap_writesect count: %llu\n", d->sect_write_count);
  1011. seq_printf(s, "mtdswap_discard count: %llu\n", d->discard_count);
  1012. seq_printf(s, "mtd read count: %llu\n", d->mtd_read_count);
  1013. seq_printf(s, "mtd write count: %llu\n", d->mtd_write_count);
  1014. seq_printf(s, "discarded pages count: %llu\n", d->discard_page_count);
  1015. seq_puts(s, "\n");
  1016. seq_printf(s, "total pages: %u\n", pages);
  1017. seq_printf(s, "pages mapped: %u\n", mapped);
  1018. return 0;
  1019. }
  1020. static int mtdswap_open(struct inode *inode, struct file *file)
  1021. {
  1022. return single_open(file, mtdswap_show, inode->i_private);
  1023. }
  1024. static const struct file_operations mtdswap_fops = {
  1025. .open = mtdswap_open,
  1026. .read = seq_read,
  1027. .llseek = seq_lseek,
  1028. .release = single_release,
  1029. };
  1030. static int mtdswap_add_debugfs(struct mtdswap_dev *d)
  1031. {
  1032. struct dentry *root = d->mtd->dbg.dfs_dir;
  1033. struct dentry *dent;
  1034. if (!IS_ENABLED(CONFIG_DEBUG_FS))
  1035. return 0;
  1036. if (IS_ERR_OR_NULL(root))
  1037. return -1;
  1038. dent = debugfs_create_file("mtdswap_stats", S_IRUSR, root, d,
  1039. &mtdswap_fops);
  1040. if (!dent) {
  1041. dev_err(d->dev, "debugfs_create_file failed\n");
  1042. return -1;
  1043. }
  1044. return 0;
  1045. }
  1046. static int mtdswap_init(struct mtdswap_dev *d, unsigned int eblocks,
  1047. unsigned int spare_cnt)
  1048. {
  1049. struct mtd_info *mtd = d->mbd_dev->mtd;
  1050. unsigned int i, eblk_bytes, pages, blocks;
  1051. int ret = -ENOMEM;
  1052. d->mtd = mtd;
  1053. d->eblks = eblocks;
  1054. d->spare_eblks = spare_cnt;
  1055. d->pages_per_eblk = mtd->erasesize >> PAGE_SHIFT;
  1056. pages = d->mbd_dev->size;
  1057. blocks = eblocks * d->pages_per_eblk;
  1058. for (i = 0; i < MTDSWAP_TREE_CNT; i++)
  1059. d->trees[i].root = RB_ROOT;
  1060. d->page_data = vmalloc(array_size(pages, sizeof(int)));
  1061. if (!d->page_data)
  1062. goto page_data_fail;
  1063. d->revmap = vmalloc(array_size(blocks, sizeof(int)));
  1064. if (!d->revmap)
  1065. goto revmap_fail;
  1066. eblk_bytes = sizeof(struct swap_eb)*d->eblks;
  1067. d->eb_data = vzalloc(eblk_bytes);
  1068. if (!d->eb_data)
  1069. goto eb_data_fail;
  1070. for (i = 0; i < pages; i++)
  1071. d->page_data[i] = BLOCK_UNDEF;
  1072. for (i = 0; i < blocks; i++)
  1073. d->revmap[i] = PAGE_UNDEF;
  1074. d->page_buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  1075. if (!d->page_buf)
  1076. goto page_buf_fail;
  1077. d->oob_buf = kmalloc_array(2, mtd->oobavail, GFP_KERNEL);
  1078. if (!d->oob_buf)
  1079. goto oob_buf_fail;
  1080. mtdswap_scan_eblks(d);
  1081. return 0;
  1082. oob_buf_fail:
  1083. kfree(d->page_buf);
  1084. page_buf_fail:
  1085. vfree(d->eb_data);
  1086. eb_data_fail:
  1087. vfree(d->revmap);
  1088. revmap_fail:
  1089. vfree(d->page_data);
  1090. page_data_fail:
  1091. printk(KERN_ERR "%s: init failed (%d)\n", MTDSWAP_PREFIX, ret);
  1092. return ret;
  1093. }
  1094. static void mtdswap_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
  1095. {
  1096. struct mtdswap_dev *d;
  1097. struct mtd_blktrans_dev *mbd_dev;
  1098. char *parts;
  1099. char *this_opt;
  1100. unsigned long part;
  1101. unsigned int eblocks, eavailable, bad_blocks, spare_cnt;
  1102. uint64_t swap_size, use_size, size_limit;
  1103. int ret;
  1104. parts = &partitions[0];
  1105. if (!*parts)
  1106. return;
  1107. while ((this_opt = strsep(&parts, ",")) != NULL) {
  1108. if (kstrtoul(this_opt, 0, &part) < 0)
  1109. return;
  1110. if (mtd->index == part)
  1111. break;
  1112. }
  1113. if (mtd->index != part)
  1114. return;
  1115. if (mtd->erasesize < PAGE_SIZE || mtd->erasesize % PAGE_SIZE) {
  1116. printk(KERN_ERR "%s: Erase size %u not multiple of PAGE_SIZE "
  1117. "%lu\n", MTDSWAP_PREFIX, mtd->erasesize, PAGE_SIZE);
  1118. return;
  1119. }
  1120. if (PAGE_SIZE % mtd->writesize || mtd->writesize > PAGE_SIZE) {
  1121. printk(KERN_ERR "%s: PAGE_SIZE %lu not multiple of write size"
  1122. " %u\n", MTDSWAP_PREFIX, PAGE_SIZE, mtd->writesize);
  1123. return;
  1124. }
  1125. if (!mtd->oobsize || mtd->oobavail < MTDSWAP_OOBSIZE) {
  1126. printk(KERN_ERR "%s: Not enough free bytes in OOB, "
  1127. "%d available, %zu needed.\n",
  1128. MTDSWAP_PREFIX, mtd->oobavail, MTDSWAP_OOBSIZE);
  1129. return;
  1130. }
  1131. if (spare_eblocks > 100)
  1132. spare_eblocks = 100;
  1133. use_size = mtd->size;
  1134. size_limit = (uint64_t) BLOCK_MAX * PAGE_SIZE;
  1135. if (mtd->size > size_limit) {
  1136. printk(KERN_WARNING "%s: Device too large. Limiting size to "
  1137. "%llu bytes\n", MTDSWAP_PREFIX, size_limit);
  1138. use_size = size_limit;
  1139. }
  1140. eblocks = mtd_div_by_eb(use_size, mtd);
  1141. use_size = (uint64_t)eblocks * mtd->erasesize;
  1142. bad_blocks = mtdswap_badblocks(mtd, use_size);
  1143. eavailable = eblocks - bad_blocks;
  1144. if (eavailable < MIN_ERASE_BLOCKS) {
  1145. printk(KERN_ERR "%s: Not enough erase blocks. %u available, "
  1146. "%d needed\n", MTDSWAP_PREFIX, eavailable,
  1147. MIN_ERASE_BLOCKS);
  1148. return;
  1149. }
  1150. spare_cnt = div_u64((uint64_t)eavailable * spare_eblocks, 100);
  1151. if (spare_cnt < MIN_SPARE_EBLOCKS)
  1152. spare_cnt = MIN_SPARE_EBLOCKS;
  1153. if (spare_cnt > eavailable - 1)
  1154. spare_cnt = eavailable - 1;
  1155. swap_size = (uint64_t)(eavailable - spare_cnt) * mtd->erasesize +
  1156. (header ? PAGE_SIZE : 0);
  1157. printk(KERN_INFO "%s: Enabling MTD swap on device %lu, size %llu KB, "
  1158. "%u spare, %u bad blocks\n",
  1159. MTDSWAP_PREFIX, part, swap_size / 1024, spare_cnt, bad_blocks);
  1160. d = kzalloc(sizeof(struct mtdswap_dev), GFP_KERNEL);
  1161. if (!d)
  1162. return;
  1163. mbd_dev = kzalloc(sizeof(struct mtd_blktrans_dev), GFP_KERNEL);
  1164. if (!mbd_dev) {
  1165. kfree(d);
  1166. return;
  1167. }
  1168. d->mbd_dev = mbd_dev;
  1169. mbd_dev->priv = d;
  1170. mbd_dev->mtd = mtd;
  1171. mbd_dev->devnum = mtd->index;
  1172. mbd_dev->size = swap_size >> PAGE_SHIFT;
  1173. mbd_dev->tr = tr;
  1174. if (!(mtd->flags & MTD_WRITEABLE))
  1175. mbd_dev->readonly = 1;
  1176. if (mtdswap_init(d, eblocks, spare_cnt) < 0)
  1177. goto init_failed;
  1178. if (add_mtd_blktrans_dev(mbd_dev) < 0)
  1179. goto cleanup;
  1180. d->dev = disk_to_dev(mbd_dev->disk);
  1181. ret = mtdswap_add_debugfs(d);
  1182. if (ret < 0)
  1183. goto debugfs_failed;
  1184. return;
  1185. debugfs_failed:
  1186. del_mtd_blktrans_dev(mbd_dev);
  1187. cleanup:
  1188. mtdswap_cleanup(d);
  1189. init_failed:
  1190. kfree(mbd_dev);
  1191. kfree(d);
  1192. }
  1193. static void mtdswap_remove_dev(struct mtd_blktrans_dev *dev)
  1194. {
  1195. struct mtdswap_dev *d = MTDSWAP_MBD_TO_MTDSWAP(dev);
  1196. del_mtd_blktrans_dev(dev);
  1197. mtdswap_cleanup(d);
  1198. kfree(d);
  1199. }
  1200. static struct mtd_blktrans_ops mtdswap_ops = {
  1201. .name = "mtdswap",
  1202. .major = 0,
  1203. .part_bits = 0,
  1204. .blksize = PAGE_SIZE,
  1205. .flush = mtdswap_flush,
  1206. .readsect = mtdswap_readsect,
  1207. .writesect = mtdswap_writesect,
  1208. .discard = mtdswap_discard,
  1209. .background = mtdswap_background,
  1210. .add_mtd = mtdswap_add_mtd,
  1211. .remove_dev = mtdswap_remove_dev,
  1212. .owner = THIS_MODULE,
  1213. };
  1214. static int __init mtdswap_modinit(void)
  1215. {
  1216. return register_mtd_blktrans(&mtdswap_ops);
  1217. }
  1218. static void __exit mtdswap_modexit(void)
  1219. {
  1220. deregister_mtd_blktrans(&mtdswap_ops);
  1221. }
  1222. module_init(mtdswap_modinit);
  1223. module_exit(mtdswap_modexit);
  1224. MODULE_LICENSE("GPL");
  1225. MODULE_AUTHOR("Jarkko Lavinen <jarkko.lavinen@nokia.com>");
  1226. MODULE_DESCRIPTION("Block device access to an MTD suitable for using as "
  1227. "swap space");