mtdswap.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592
  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");