mtdswap.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591
  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->ecclayout->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->ecclayout->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->ecclayout->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->ecclayout->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->ecclayout->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->ecclayout->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. struct nand_ecclayout *oinfo;
  1142. int ret;
  1143. parts = &partitions[0];
  1144. if (!*parts)
  1145. return;
  1146. while ((this_opt = strsep(&parts, ",")) != NULL) {
  1147. if (kstrtoul(this_opt, 0, &part) < 0)
  1148. return;
  1149. if (mtd->index == part)
  1150. break;
  1151. }
  1152. if (mtd->index != part)
  1153. return;
  1154. if (mtd->erasesize < PAGE_SIZE || mtd->erasesize % PAGE_SIZE) {
  1155. printk(KERN_ERR "%s: Erase size %u not multiple of PAGE_SIZE "
  1156. "%lu\n", MTDSWAP_PREFIX, mtd->erasesize, PAGE_SIZE);
  1157. return;
  1158. }
  1159. if (PAGE_SIZE % mtd->writesize || mtd->writesize > PAGE_SIZE) {
  1160. printk(KERN_ERR "%s: PAGE_SIZE %lu not multiple of write size"
  1161. " %u\n", MTDSWAP_PREFIX, PAGE_SIZE, mtd->writesize);
  1162. return;
  1163. }
  1164. oinfo = mtd->ecclayout;
  1165. if (!oinfo) {
  1166. printk(KERN_ERR "%s: mtd%d does not have OOB\n",
  1167. MTDSWAP_PREFIX, mtd->index);
  1168. return;
  1169. }
  1170. if (!mtd->oobsize || oinfo->oobavail < MTDSWAP_OOBSIZE) {
  1171. printk(KERN_ERR "%s: Not enough free bytes in OOB, "
  1172. "%d available, %zu needed.\n",
  1173. MTDSWAP_PREFIX, oinfo->oobavail, MTDSWAP_OOBSIZE);
  1174. return;
  1175. }
  1176. if (spare_eblocks > 100)
  1177. spare_eblocks = 100;
  1178. use_size = mtd->size;
  1179. size_limit = (uint64_t) BLOCK_MAX * PAGE_SIZE;
  1180. if (mtd->size > size_limit) {
  1181. printk(KERN_WARNING "%s: Device too large. Limiting size to "
  1182. "%llu bytes\n", MTDSWAP_PREFIX, size_limit);
  1183. use_size = size_limit;
  1184. }
  1185. eblocks = mtd_div_by_eb(use_size, mtd);
  1186. use_size = (uint64_t)eblocks * mtd->erasesize;
  1187. bad_blocks = mtdswap_badblocks(mtd, use_size);
  1188. eavailable = eblocks - bad_blocks;
  1189. if (eavailable < MIN_ERASE_BLOCKS) {
  1190. printk(KERN_ERR "%s: Not enough erase blocks. %u available, "
  1191. "%d needed\n", MTDSWAP_PREFIX, eavailable,
  1192. MIN_ERASE_BLOCKS);
  1193. return;
  1194. }
  1195. spare_cnt = div_u64((uint64_t)eavailable * spare_eblocks, 100);
  1196. if (spare_cnt < MIN_SPARE_EBLOCKS)
  1197. spare_cnt = MIN_SPARE_EBLOCKS;
  1198. if (spare_cnt > eavailable - 1)
  1199. spare_cnt = eavailable - 1;
  1200. swap_size = (uint64_t)(eavailable - spare_cnt) * mtd->erasesize +
  1201. (header ? PAGE_SIZE : 0);
  1202. printk(KERN_INFO "%s: Enabling MTD swap on device %lu, size %llu KB, "
  1203. "%u spare, %u bad blocks\n",
  1204. MTDSWAP_PREFIX, part, swap_size / 1024, spare_cnt, bad_blocks);
  1205. d = kzalloc(sizeof(struct mtdswap_dev), GFP_KERNEL);
  1206. if (!d)
  1207. return;
  1208. mbd_dev = kzalloc(sizeof(struct mtd_blktrans_dev), GFP_KERNEL);
  1209. if (!mbd_dev) {
  1210. kfree(d);
  1211. return;
  1212. }
  1213. d->mbd_dev = mbd_dev;
  1214. mbd_dev->priv = d;
  1215. mbd_dev->mtd = mtd;
  1216. mbd_dev->devnum = mtd->index;
  1217. mbd_dev->size = swap_size >> PAGE_SHIFT;
  1218. mbd_dev->tr = tr;
  1219. if (!(mtd->flags & MTD_WRITEABLE))
  1220. mbd_dev->readonly = 1;
  1221. if (mtdswap_init(d, eblocks, spare_cnt) < 0)
  1222. goto init_failed;
  1223. if (add_mtd_blktrans_dev(mbd_dev) < 0)
  1224. goto cleanup;
  1225. d->dev = disk_to_dev(mbd_dev->disk);
  1226. ret = mtdswap_add_debugfs(d);
  1227. if (ret < 0)
  1228. goto debugfs_failed;
  1229. return;
  1230. debugfs_failed:
  1231. del_mtd_blktrans_dev(mbd_dev);
  1232. cleanup:
  1233. mtdswap_cleanup(d);
  1234. init_failed:
  1235. kfree(mbd_dev);
  1236. kfree(d);
  1237. }
  1238. static void mtdswap_remove_dev(struct mtd_blktrans_dev *dev)
  1239. {
  1240. struct mtdswap_dev *d = MTDSWAP_MBD_TO_MTDSWAP(dev);
  1241. debugfs_remove_recursive(d->debugfs_root);
  1242. del_mtd_blktrans_dev(dev);
  1243. mtdswap_cleanup(d);
  1244. kfree(d);
  1245. }
  1246. static struct mtd_blktrans_ops mtdswap_ops = {
  1247. .name = "mtdswap",
  1248. .major = 0,
  1249. .part_bits = 0,
  1250. .blksize = PAGE_SIZE,
  1251. .flush = mtdswap_flush,
  1252. .readsect = mtdswap_readsect,
  1253. .writesect = mtdswap_writesect,
  1254. .discard = mtdswap_discard,
  1255. .background = mtdswap_background,
  1256. .add_mtd = mtdswap_add_mtd,
  1257. .remove_dev = mtdswap_remove_dev,
  1258. .owner = THIS_MODULE,
  1259. };
  1260. static int __init mtdswap_modinit(void)
  1261. {
  1262. return register_mtd_blktrans(&mtdswap_ops);
  1263. }
  1264. static void __exit mtdswap_modexit(void)
  1265. {
  1266. deregister_mtd_blktrans(&mtdswap_ops);
  1267. }
  1268. module_init(mtdswap_modinit);
  1269. module_exit(mtdswap_modexit);
  1270. MODULE_LICENSE("GPL");
  1271. MODULE_AUTHOR("Jarkko Lavinen <jarkko.lavinen@nokia.com>");
  1272. MODULE_DESCRIPTION("Block device access to an MTD suitable for using as "
  1273. "swap space");