mtdswap.c 35 KB

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