scan.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598
  1. /*
  2. * Copyright (c) International Business Machines Corp., 2006
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  12. * the GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. * Author: Artem Bityutskiy (Битюцкий Артём)
  19. */
  20. /*
  21. * UBI scanning sub-system.
  22. *
  23. * This sub-system is responsible for scanning the flash media, checking UBI
  24. * headers and providing complete information about the UBI flash image.
  25. *
  26. * The scanning information is represented by a &struct ubi_attach_info' object.
  27. * Information about found volumes is represented by &struct ubi_ainf_volume
  28. * objects which are kept in volume RB-tree with root at the @volumes field.
  29. * The RB-tree is indexed by the volume ID.
  30. *
  31. * Scanned logical eraseblocks are represented by &struct ubi_ainf_peb objects.
  32. * These objects are kept in per-volume RB-trees with the root at the
  33. * corresponding &struct ubi_ainf_volume object. To put it differently, we keep
  34. * an RB-tree of per-volume objects and each of these objects is the root of
  35. * RB-tree of per-eraseblock objects.
  36. *
  37. * Corrupted physical eraseblocks are put to the @corr list, free physical
  38. * eraseblocks are put to the @free list and the physical eraseblock to be
  39. * erased are put to the @erase list.
  40. *
  41. * About corruptions
  42. * ~~~~~~~~~~~~~~~~~
  43. *
  44. * UBI protects EC and VID headers with CRC-32 checksums, so it can detect
  45. * whether the headers are corrupted or not. Sometimes UBI also protects the
  46. * data with CRC-32, e.g., when it executes the atomic LEB change operation, or
  47. * when it moves the contents of a PEB for wear-leveling purposes.
  48. *
  49. * UBI tries to distinguish between 2 types of corruptions.
  50. *
  51. * 1. Corruptions caused by power cuts. These are expected corruptions and UBI
  52. * tries to handle them gracefully, without printing too many warnings and
  53. * error messages. The idea is that we do not lose important data in these case
  54. * - we may lose only the data which was being written to the media just before
  55. * the power cut happened, and the upper layers (e.g., UBIFS) are supposed to
  56. * handle such data losses (e.g., by using the FS journal).
  57. *
  58. * When UBI detects a corruption (CRC-32 mismatch) in a PEB, and it looks like
  59. * the reason is a power cut, UBI puts this PEB to the @erase list, and all
  60. * PEBs in the @erase list are scheduled for erasure later.
  61. *
  62. * 2. Unexpected corruptions which are not caused by power cuts. During
  63. * scanning, such PEBs are put to the @corr list and UBI preserves them.
  64. * Obviously, this lessens the amount of available PEBs, and if at some point
  65. * UBI runs out of free PEBs, it switches to R/O mode. UBI also loudly informs
  66. * about such PEBs every time the MTD device is attached.
  67. *
  68. * However, it is difficult to reliably distinguish between these types of
  69. * corruptions and UBI's strategy is as follows. UBI assumes corruption type 2
  70. * if the VID header is corrupted and the data area does not contain all 0xFFs,
  71. * and there were no bit-flips or integrity errors while reading the data area.
  72. * Otherwise UBI assumes corruption type 1. So the decision criteria are as
  73. * follows.
  74. * o If the data area contains only 0xFFs, there is no data, and it is safe
  75. * to just erase this PEB - this is corruption type 1.
  76. * o If the data area has bit-flips or data integrity errors (ECC errors on
  77. * NAND), it is probably a PEB which was being erased when power cut
  78. * happened, so this is corruption type 1. However, this is just a guess,
  79. * which might be wrong.
  80. * o Otherwise this it corruption type 2.
  81. */
  82. #include <linux/err.h>
  83. #include <linux/slab.h>
  84. #include <linux/crc32.h>
  85. #include <linux/math64.h>
  86. #include <linux/random.h>
  87. #include "ubi.h"
  88. static int self_check_si(struct ubi_device *ubi, struct ubi_attach_info *si);
  89. /* Temporary variables used during scanning */
  90. static struct ubi_ec_hdr *ech;
  91. static struct ubi_vid_hdr *vidh;
  92. /**
  93. * add_to_list - add physical eraseblock to a list.
  94. * @si: scanning information
  95. * @pnum: physical eraseblock number to add
  96. * @ec: erase counter of the physical eraseblock
  97. * @to_head: if not zero, add to the head of the list
  98. * @list: the list to add to
  99. *
  100. * This function adds physical eraseblock @pnum to free, erase, or alien lists.
  101. * If @to_head is not zero, PEB will be added to the head of the list, which
  102. * basically means it will be processed first later. E.g., we add corrupted
  103. * PEBs (corrupted due to power cuts) to the head of the erase list to make
  104. * sure we erase them first and get rid of corruptions ASAP. This function
  105. * returns zero in case of success and a negative error code in case of
  106. * failure.
  107. */
  108. static int add_to_list(struct ubi_attach_info *si, int pnum, int ec,
  109. int to_head, struct list_head *list)
  110. {
  111. struct ubi_ainf_peb *aeb;
  112. if (list == &si->free) {
  113. dbg_bld("add to free: PEB %d, EC %d", pnum, ec);
  114. } else if (list == &si->erase) {
  115. dbg_bld("add to erase: PEB %d, EC %d", pnum, ec);
  116. } else if (list == &si->alien) {
  117. dbg_bld("add to alien: PEB %d, EC %d", pnum, ec);
  118. si->alien_peb_count += 1;
  119. } else
  120. BUG();
  121. aeb = kmem_cache_alloc(si->scan_leb_slab, GFP_KERNEL);
  122. if (!aeb)
  123. return -ENOMEM;
  124. aeb->pnum = pnum;
  125. aeb->ec = ec;
  126. if (to_head)
  127. list_add(&aeb->u.list, list);
  128. else
  129. list_add_tail(&aeb->u.list, list);
  130. return 0;
  131. }
  132. /**
  133. * add_corrupted - add a corrupted physical eraseblock.
  134. * @si: scanning information
  135. * @pnum: physical eraseblock number to add
  136. * @ec: erase counter of the physical eraseblock
  137. *
  138. * This function adds corrupted physical eraseblock @pnum to the 'corr' list.
  139. * The corruption was presumably not caused by a power cut. Returns zero in
  140. * case of success and a negative error code in case of failure.
  141. */
  142. static int add_corrupted(struct ubi_attach_info *si, int pnum, int ec)
  143. {
  144. struct ubi_ainf_peb *aeb;
  145. dbg_bld("add to corrupted: PEB %d, EC %d", pnum, ec);
  146. aeb = kmem_cache_alloc(si->scan_leb_slab, GFP_KERNEL);
  147. if (!aeb)
  148. return -ENOMEM;
  149. si->corr_peb_count += 1;
  150. aeb->pnum = pnum;
  151. aeb->ec = ec;
  152. list_add(&aeb->u.list, &si->corr);
  153. return 0;
  154. }
  155. /**
  156. * validate_vid_hdr - check volume identifier header.
  157. * @vid_hdr: the volume identifier header to check
  158. * @sv: information about the volume this logical eraseblock belongs to
  159. * @pnum: physical eraseblock number the VID header came from
  160. *
  161. * This function checks that data stored in @vid_hdr is consistent. Returns
  162. * non-zero if an inconsistency was found and zero if not.
  163. *
  164. * Note, UBI does sanity check of everything it reads from the flash media.
  165. * Most of the checks are done in the I/O sub-system. Here we check that the
  166. * information in the VID header is consistent to the information in other VID
  167. * headers of the same volume.
  168. */
  169. static int validate_vid_hdr(const struct ubi_vid_hdr *vid_hdr,
  170. const struct ubi_ainf_volume *sv, int pnum)
  171. {
  172. int vol_type = vid_hdr->vol_type;
  173. int vol_id = be32_to_cpu(vid_hdr->vol_id);
  174. int used_ebs = be32_to_cpu(vid_hdr->used_ebs);
  175. int data_pad = be32_to_cpu(vid_hdr->data_pad);
  176. if (sv->leb_count != 0) {
  177. int sv_vol_type;
  178. /*
  179. * This is not the first logical eraseblock belonging to this
  180. * volume. Ensure that the data in its VID header is consistent
  181. * to the data in previous logical eraseblock headers.
  182. */
  183. if (vol_id != sv->vol_id) {
  184. ubi_err("inconsistent vol_id");
  185. goto bad;
  186. }
  187. if (sv->vol_type == UBI_STATIC_VOLUME)
  188. sv_vol_type = UBI_VID_STATIC;
  189. else
  190. sv_vol_type = UBI_VID_DYNAMIC;
  191. if (vol_type != sv_vol_type) {
  192. ubi_err("inconsistent vol_type");
  193. goto bad;
  194. }
  195. if (used_ebs != sv->used_ebs) {
  196. ubi_err("inconsistent used_ebs");
  197. goto bad;
  198. }
  199. if (data_pad != sv->data_pad) {
  200. ubi_err("inconsistent data_pad");
  201. goto bad;
  202. }
  203. }
  204. return 0;
  205. bad:
  206. ubi_err("inconsistent VID header at PEB %d", pnum);
  207. ubi_dump_vid_hdr(vid_hdr);
  208. ubi_dump_sv(sv);
  209. return -EINVAL;
  210. }
  211. /**
  212. * add_volume - add volume to the scanning information.
  213. * @si: scanning information
  214. * @vol_id: ID of the volume to add
  215. * @pnum: physical eraseblock number
  216. * @vid_hdr: volume identifier header
  217. *
  218. * If the volume corresponding to the @vid_hdr logical eraseblock is already
  219. * present in the scanning information, this function does nothing. Otherwise
  220. * it adds corresponding volume to the scanning information. Returns a pointer
  221. * to the scanning volume object in case of success and a negative error code
  222. * in case of failure.
  223. */
  224. static struct ubi_ainf_volume *add_volume(struct ubi_attach_info *si,
  225. int vol_id, int pnum,
  226. const struct ubi_vid_hdr *vid_hdr)
  227. {
  228. struct ubi_ainf_volume *sv;
  229. struct rb_node **p = &si->volumes.rb_node, *parent = NULL;
  230. ubi_assert(vol_id == be32_to_cpu(vid_hdr->vol_id));
  231. /* Walk the volume RB-tree to look if this volume is already present */
  232. while (*p) {
  233. parent = *p;
  234. sv = rb_entry(parent, struct ubi_ainf_volume, rb);
  235. if (vol_id == sv->vol_id)
  236. return sv;
  237. if (vol_id > sv->vol_id)
  238. p = &(*p)->rb_left;
  239. else
  240. p = &(*p)->rb_right;
  241. }
  242. /* The volume is absent - add it */
  243. sv = kmalloc(sizeof(struct ubi_ainf_volume), GFP_KERNEL);
  244. if (!sv)
  245. return ERR_PTR(-ENOMEM);
  246. sv->highest_lnum = sv->leb_count = 0;
  247. sv->vol_id = vol_id;
  248. sv->root = RB_ROOT;
  249. sv->used_ebs = be32_to_cpu(vid_hdr->used_ebs);
  250. sv->data_pad = be32_to_cpu(vid_hdr->data_pad);
  251. sv->compat = vid_hdr->compat;
  252. sv->vol_type = vid_hdr->vol_type == UBI_VID_DYNAMIC ? UBI_DYNAMIC_VOLUME
  253. : UBI_STATIC_VOLUME;
  254. if (vol_id > si->highest_vol_id)
  255. si->highest_vol_id = vol_id;
  256. rb_link_node(&sv->rb, parent, p);
  257. rb_insert_color(&sv->rb, &si->volumes);
  258. si->vols_found += 1;
  259. dbg_bld("added volume %d", vol_id);
  260. return sv;
  261. }
  262. /**
  263. * compare_lebs - find out which logical eraseblock is newer.
  264. * @ubi: UBI device description object
  265. * @aeb: first logical eraseblock to compare
  266. * @pnum: physical eraseblock number of the second logical eraseblock to
  267. * compare
  268. * @vid_hdr: volume identifier header of the second logical eraseblock
  269. *
  270. * This function compares 2 copies of a LEB and informs which one is newer. In
  271. * case of success this function returns a positive value, in case of failure, a
  272. * negative error code is returned. The success return codes use the following
  273. * bits:
  274. * o bit 0 is cleared: the first PEB (described by @aeb) is newer than the
  275. * second PEB (described by @pnum and @vid_hdr);
  276. * o bit 0 is set: the second PEB is newer;
  277. * o bit 1 is cleared: no bit-flips were detected in the newer LEB;
  278. * o bit 1 is set: bit-flips were detected in the newer LEB;
  279. * o bit 2 is cleared: the older LEB is not corrupted;
  280. * o bit 2 is set: the older LEB is corrupted.
  281. */
  282. static int compare_lebs(struct ubi_device *ubi, const struct ubi_ainf_peb *aeb,
  283. int pnum, const struct ubi_vid_hdr *vid_hdr)
  284. {
  285. void *buf;
  286. int len, err, second_is_newer, bitflips = 0, corrupted = 0;
  287. uint32_t data_crc, crc;
  288. struct ubi_vid_hdr *vh = NULL;
  289. unsigned long long sqnum2 = be64_to_cpu(vid_hdr->sqnum);
  290. if (sqnum2 == aeb->sqnum) {
  291. /*
  292. * This must be a really ancient UBI image which has been
  293. * created before sequence numbers support has been added. At
  294. * that times we used 32-bit LEB versions stored in logical
  295. * eraseblocks. That was before UBI got into mainline. We do not
  296. * support these images anymore. Well, those images still work,
  297. * but only if no unclean reboots happened.
  298. */
  299. ubi_err("unsupported on-flash UBI format\n");
  300. return -EINVAL;
  301. }
  302. /* Obviously the LEB with lower sequence counter is older */
  303. second_is_newer = (sqnum2 > aeb->sqnum);
  304. /*
  305. * Now we know which copy is newer. If the copy flag of the PEB with
  306. * newer version is not set, then we just return, otherwise we have to
  307. * check data CRC. For the second PEB we already have the VID header,
  308. * for the first one - we'll need to re-read it from flash.
  309. *
  310. * Note: this may be optimized so that we wouldn't read twice.
  311. */
  312. if (second_is_newer) {
  313. if (!vid_hdr->copy_flag) {
  314. /* It is not a copy, so it is newer */
  315. dbg_bld("second PEB %d is newer, copy_flag is unset",
  316. pnum);
  317. return 1;
  318. }
  319. } else {
  320. if (!aeb->copy_flag) {
  321. /* It is not a copy, so it is newer */
  322. dbg_bld("first PEB %d is newer, copy_flag is unset",
  323. pnum);
  324. return bitflips << 1;
  325. }
  326. vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
  327. if (!vh)
  328. return -ENOMEM;
  329. pnum = aeb->pnum;
  330. err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
  331. if (err) {
  332. if (err == UBI_IO_BITFLIPS)
  333. bitflips = 1;
  334. else {
  335. ubi_err("VID of PEB %d header is bad, but it "
  336. "was OK earlier, err %d", pnum, err);
  337. if (err > 0)
  338. err = -EIO;
  339. goto out_free_vidh;
  340. }
  341. }
  342. vid_hdr = vh;
  343. }
  344. /* Read the data of the copy and check the CRC */
  345. len = be32_to_cpu(vid_hdr->data_size);
  346. buf = vmalloc(len);
  347. if (!buf) {
  348. err = -ENOMEM;
  349. goto out_free_vidh;
  350. }
  351. err = ubi_io_read_data(ubi, buf, pnum, 0, len);
  352. if (err && err != UBI_IO_BITFLIPS && !mtd_is_eccerr(err))
  353. goto out_free_buf;
  354. data_crc = be32_to_cpu(vid_hdr->data_crc);
  355. crc = crc32(UBI_CRC32_INIT, buf, len);
  356. if (crc != data_crc) {
  357. dbg_bld("PEB %d CRC error: calculated %#08x, must be %#08x",
  358. pnum, crc, data_crc);
  359. corrupted = 1;
  360. bitflips = 0;
  361. second_is_newer = !second_is_newer;
  362. } else {
  363. dbg_bld("PEB %d CRC is OK", pnum);
  364. bitflips = !!err;
  365. }
  366. vfree(buf);
  367. ubi_free_vid_hdr(ubi, vh);
  368. if (second_is_newer)
  369. dbg_bld("second PEB %d is newer, copy_flag is set", pnum);
  370. else
  371. dbg_bld("first PEB %d is newer, copy_flag is set", pnum);
  372. return second_is_newer | (bitflips << 1) | (corrupted << 2);
  373. out_free_buf:
  374. vfree(buf);
  375. out_free_vidh:
  376. ubi_free_vid_hdr(ubi, vh);
  377. return err;
  378. }
  379. /**
  380. * ubi_scan_add_used - add physical eraseblock to the scanning information.
  381. * @ubi: UBI device description object
  382. * @si: scanning information
  383. * @pnum: the physical eraseblock number
  384. * @ec: erase counter
  385. * @vid_hdr: the volume identifier header
  386. * @bitflips: if bit-flips were detected when this physical eraseblock was read
  387. *
  388. * This function adds information about a used physical eraseblock to the
  389. * 'used' tree of the corresponding volume. The function is rather complex
  390. * because it has to handle cases when this is not the first physical
  391. * eraseblock belonging to the same logical eraseblock, and the newer one has
  392. * to be picked, while the older one has to be dropped. This function returns
  393. * zero in case of success and a negative error code in case of failure.
  394. */
  395. int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_attach_info *si,
  396. int pnum, int ec, const struct ubi_vid_hdr *vid_hdr,
  397. int bitflips)
  398. {
  399. int err, vol_id, lnum;
  400. unsigned long long sqnum;
  401. struct ubi_ainf_volume *sv;
  402. struct ubi_ainf_peb *aeb;
  403. struct rb_node **p, *parent = NULL;
  404. vol_id = be32_to_cpu(vid_hdr->vol_id);
  405. lnum = be32_to_cpu(vid_hdr->lnum);
  406. sqnum = be64_to_cpu(vid_hdr->sqnum);
  407. dbg_bld("PEB %d, LEB %d:%d, EC %d, sqnum %llu, bitflips %d",
  408. pnum, vol_id, lnum, ec, sqnum, bitflips);
  409. sv = add_volume(si, vol_id, pnum, vid_hdr);
  410. if (IS_ERR(sv))
  411. return PTR_ERR(sv);
  412. if (si->max_sqnum < sqnum)
  413. si->max_sqnum = sqnum;
  414. /*
  415. * Walk the RB-tree of logical eraseblocks of volume @vol_id to look
  416. * if this is the first instance of this logical eraseblock or not.
  417. */
  418. p = &sv->root.rb_node;
  419. while (*p) {
  420. int cmp_res;
  421. parent = *p;
  422. aeb = rb_entry(parent, struct ubi_ainf_peb, u.rb);
  423. if (lnum != aeb->lnum) {
  424. if (lnum < aeb->lnum)
  425. p = &(*p)->rb_left;
  426. else
  427. p = &(*p)->rb_right;
  428. continue;
  429. }
  430. /*
  431. * There is already a physical eraseblock describing the same
  432. * logical eraseblock present.
  433. */
  434. dbg_bld("this LEB already exists: PEB %d, sqnum %llu, EC %d",
  435. aeb->pnum, aeb->sqnum, aeb->ec);
  436. /*
  437. * Make sure that the logical eraseblocks have different
  438. * sequence numbers. Otherwise the image is bad.
  439. *
  440. * However, if the sequence number is zero, we assume it must
  441. * be an ancient UBI image from the era when UBI did not have
  442. * sequence numbers. We still can attach these images, unless
  443. * there is a need to distinguish between old and new
  444. * eraseblocks, in which case we'll refuse the image in
  445. * 'compare_lebs()'. In other words, we attach old clean
  446. * images, but refuse attaching old images with duplicated
  447. * logical eraseblocks because there was an unclean reboot.
  448. */
  449. if (aeb->sqnum == sqnum && sqnum != 0) {
  450. ubi_err("two LEBs with same sequence number %llu",
  451. sqnum);
  452. ubi_dump_aeb(aeb, 0);
  453. ubi_dump_vid_hdr(vid_hdr);
  454. return -EINVAL;
  455. }
  456. /*
  457. * Now we have to drop the older one and preserve the newer
  458. * one.
  459. */
  460. cmp_res = compare_lebs(ubi, aeb, pnum, vid_hdr);
  461. if (cmp_res < 0)
  462. return cmp_res;
  463. if (cmp_res & 1) {
  464. /*
  465. * This logical eraseblock is newer than the one
  466. * found earlier.
  467. */
  468. err = validate_vid_hdr(vid_hdr, sv, pnum);
  469. if (err)
  470. return err;
  471. err = add_to_list(si, aeb->pnum, aeb->ec, cmp_res & 4,
  472. &si->erase);
  473. if (err)
  474. return err;
  475. aeb->ec = ec;
  476. aeb->pnum = pnum;
  477. aeb->scrub = ((cmp_res & 2) || bitflips);
  478. aeb->copy_flag = vid_hdr->copy_flag;
  479. aeb->sqnum = sqnum;
  480. if (sv->highest_lnum == lnum)
  481. sv->last_data_size =
  482. be32_to_cpu(vid_hdr->data_size);
  483. return 0;
  484. } else {
  485. /*
  486. * This logical eraseblock is older than the one found
  487. * previously.
  488. */
  489. return add_to_list(si, pnum, ec, cmp_res & 4,
  490. &si->erase);
  491. }
  492. }
  493. /*
  494. * We've met this logical eraseblock for the first time, add it to the
  495. * scanning information.
  496. */
  497. err = validate_vid_hdr(vid_hdr, sv, pnum);
  498. if (err)
  499. return err;
  500. aeb = kmem_cache_alloc(si->scan_leb_slab, GFP_KERNEL);
  501. if (!aeb)
  502. return -ENOMEM;
  503. aeb->ec = ec;
  504. aeb->pnum = pnum;
  505. aeb->lnum = lnum;
  506. aeb->scrub = bitflips;
  507. aeb->copy_flag = vid_hdr->copy_flag;
  508. aeb->sqnum = sqnum;
  509. if (sv->highest_lnum <= lnum) {
  510. sv->highest_lnum = lnum;
  511. sv->last_data_size = be32_to_cpu(vid_hdr->data_size);
  512. }
  513. sv->leb_count += 1;
  514. rb_link_node(&aeb->u.rb, parent, p);
  515. rb_insert_color(&aeb->u.rb, &sv->root);
  516. return 0;
  517. }
  518. /**
  519. * ubi_scan_find_sv - find volume in the scanning information.
  520. * @si: scanning information
  521. * @vol_id: the requested volume ID
  522. *
  523. * This function returns a pointer to the volume description or %NULL if there
  524. * are no data about this volume in the scanning information.
  525. */
  526. struct ubi_ainf_volume *ubi_scan_find_sv(const struct ubi_attach_info *si,
  527. int vol_id)
  528. {
  529. struct ubi_ainf_volume *sv;
  530. struct rb_node *p = si->volumes.rb_node;
  531. while (p) {
  532. sv = rb_entry(p, struct ubi_ainf_volume, rb);
  533. if (vol_id == sv->vol_id)
  534. return sv;
  535. if (vol_id > sv->vol_id)
  536. p = p->rb_left;
  537. else
  538. p = p->rb_right;
  539. }
  540. return NULL;
  541. }
  542. /**
  543. * ubi_scan_find_aeb - find LEB in the volume scanning information.
  544. * @sv: a pointer to the volume scanning information
  545. * @lnum: the requested logical eraseblock
  546. *
  547. * This function returns a pointer to the scanning logical eraseblock or %NULL
  548. * if there are no data about it in the scanning volume information.
  549. */
  550. struct ubi_ainf_peb *ubi_scan_find_aeb(const struct ubi_ainf_volume *sv,
  551. int lnum)
  552. {
  553. struct ubi_ainf_peb *aeb;
  554. struct rb_node *p = sv->root.rb_node;
  555. while (p) {
  556. aeb = rb_entry(p, struct ubi_ainf_peb, u.rb);
  557. if (lnum == aeb->lnum)
  558. return aeb;
  559. if (lnum > aeb->lnum)
  560. p = p->rb_left;
  561. else
  562. p = p->rb_right;
  563. }
  564. return NULL;
  565. }
  566. /**
  567. * ubi_scan_rm_volume - delete scanning information about a volume.
  568. * @si: scanning information
  569. * @sv: the volume scanning information to delete
  570. */
  571. void ubi_scan_rm_volume(struct ubi_attach_info *si, struct ubi_ainf_volume *sv)
  572. {
  573. struct rb_node *rb;
  574. struct ubi_ainf_peb *aeb;
  575. dbg_bld("remove scanning information about volume %d", sv->vol_id);
  576. while ((rb = rb_first(&sv->root))) {
  577. aeb = rb_entry(rb, struct ubi_ainf_peb, u.rb);
  578. rb_erase(&aeb->u.rb, &sv->root);
  579. list_add_tail(&aeb->u.list, &si->erase);
  580. }
  581. rb_erase(&sv->rb, &si->volumes);
  582. kfree(sv);
  583. si->vols_found -= 1;
  584. }
  585. /**
  586. * ubi_scan_erase_peb - erase a physical eraseblock.
  587. * @ubi: UBI device description object
  588. * @si: scanning information
  589. * @pnum: physical eraseblock number to erase;
  590. * @ec: erase counter value to write (%UBI_SCAN_UNKNOWN_EC if it is unknown)
  591. *
  592. * This function erases physical eraseblock 'pnum', and writes the erase
  593. * counter header to it. This function should only be used on UBI device
  594. * initialization stages, when the EBA sub-system had not been yet initialized.
  595. * This function returns zero in case of success and a negative error code in
  596. * case of failure.
  597. */
  598. int ubi_scan_erase_peb(struct ubi_device *ubi, const struct ubi_attach_info *si,
  599. int pnum, int ec)
  600. {
  601. int err;
  602. struct ubi_ec_hdr *ec_hdr;
  603. if ((long long)ec >= UBI_MAX_ERASECOUNTER) {
  604. /*
  605. * Erase counter overflow. Upgrade UBI and use 64-bit
  606. * erase counters internally.
  607. */
  608. ubi_err("erase counter overflow at PEB %d, EC %d", pnum, ec);
  609. return -EINVAL;
  610. }
  611. ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
  612. if (!ec_hdr)
  613. return -ENOMEM;
  614. ec_hdr->ec = cpu_to_be64(ec);
  615. err = ubi_io_sync_erase(ubi, pnum, 0);
  616. if (err < 0)
  617. goto out_free;
  618. err = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr);
  619. out_free:
  620. kfree(ec_hdr);
  621. return err;
  622. }
  623. /**
  624. * ubi_scan_get_free_peb - get a free physical eraseblock.
  625. * @ubi: UBI device description object
  626. * @si: scanning information
  627. *
  628. * This function returns a free physical eraseblock. It is supposed to be
  629. * called on the UBI initialization stages when the wear-leveling sub-system is
  630. * not initialized yet. This function picks a physical eraseblocks from one of
  631. * the lists, writes the EC header if it is needed, and removes it from the
  632. * list.
  633. *
  634. * This function returns scanning physical eraseblock information in case of
  635. * success and an error code in case of failure.
  636. */
  637. struct ubi_ainf_peb *ubi_scan_get_free_peb(struct ubi_device *ubi,
  638. struct ubi_attach_info *si)
  639. {
  640. int err = 0;
  641. struct ubi_ainf_peb *aeb, *tmp_aeb;
  642. if (!list_empty(&si->free)) {
  643. aeb = list_entry(si->free.next, struct ubi_ainf_peb, u.list);
  644. list_del(&aeb->u.list);
  645. dbg_bld("return free PEB %d, EC %d", aeb->pnum, aeb->ec);
  646. return aeb;
  647. }
  648. /*
  649. * We try to erase the first physical eraseblock from the erase list
  650. * and pick it if we succeed, or try to erase the next one if not. And
  651. * so forth. We don't want to take care about bad eraseblocks here -
  652. * they'll be handled later.
  653. */
  654. list_for_each_entry_safe(aeb, tmp_aeb, &si->erase, u.list) {
  655. if (aeb->ec == UBI_SCAN_UNKNOWN_EC)
  656. aeb->ec = si->mean_ec;
  657. err = ubi_scan_erase_peb(ubi, si, aeb->pnum, aeb->ec+1);
  658. if (err)
  659. continue;
  660. aeb->ec += 1;
  661. list_del(&aeb->u.list);
  662. dbg_bld("return PEB %d, EC %d", aeb->pnum, aeb->ec);
  663. return aeb;
  664. }
  665. ubi_err("no free eraseblocks");
  666. return ERR_PTR(-ENOSPC);
  667. }
  668. /**
  669. * check_corruption - check the data area of PEB.
  670. * @ubi: UBI device description object
  671. * @vid_hrd: the (corrupted) VID header of this PEB
  672. * @pnum: the physical eraseblock number to check
  673. *
  674. * This is a helper function which is used to distinguish between VID header
  675. * corruptions caused by power cuts and other reasons. If the PEB contains only
  676. * 0xFF bytes in the data area, the VID header is most probably corrupted
  677. * because of a power cut (%0 is returned in this case). Otherwise, it was
  678. * probably corrupted for some other reasons (%1 is returned in this case). A
  679. * negative error code is returned if a read error occurred.
  680. *
  681. * If the corruption reason was a power cut, UBI can safely erase this PEB.
  682. * Otherwise, it should preserve it to avoid possibly destroying important
  683. * information.
  684. */
  685. static int check_corruption(struct ubi_device *ubi, struct ubi_vid_hdr *vid_hdr,
  686. int pnum)
  687. {
  688. int err;
  689. mutex_lock(&ubi->buf_mutex);
  690. memset(ubi->peb_buf, 0x00, ubi->leb_size);
  691. err = ubi_io_read(ubi, ubi->peb_buf, pnum, ubi->leb_start,
  692. ubi->leb_size);
  693. if (err == UBI_IO_BITFLIPS || mtd_is_eccerr(err)) {
  694. /*
  695. * Bit-flips or integrity errors while reading the data area.
  696. * It is difficult to say for sure what type of corruption is
  697. * this, but presumably a power cut happened while this PEB was
  698. * erased, so it became unstable and corrupted, and should be
  699. * erased.
  700. */
  701. err = 0;
  702. goto out_unlock;
  703. }
  704. if (err)
  705. goto out_unlock;
  706. if (ubi_check_pattern(ubi->peb_buf, 0xFF, ubi->leb_size))
  707. goto out_unlock;
  708. ubi_err("PEB %d contains corrupted VID header, and the data does not "
  709. "contain all 0xFF, this may be a non-UBI PEB or a severe VID "
  710. "header corruption which requires manual inspection", pnum);
  711. ubi_dump_vid_hdr(vid_hdr);
  712. dbg_msg("hexdump of PEB %d offset %d, length %d",
  713. pnum, ubi->leb_start, ubi->leb_size);
  714. ubi_dbg_print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
  715. ubi->peb_buf, ubi->leb_size, 1);
  716. err = 1;
  717. out_unlock:
  718. mutex_unlock(&ubi->buf_mutex);
  719. return err;
  720. }
  721. /**
  722. * process_eb - read, check UBI headers, and add them to scanning information.
  723. * @ubi: UBI device description object
  724. * @si: scanning information
  725. * @pnum: the physical eraseblock number
  726. *
  727. * This function returns a zero if the physical eraseblock was successfully
  728. * handled and a negative error code in case of failure.
  729. */
  730. static int process_eb(struct ubi_device *ubi, struct ubi_attach_info *si,
  731. int pnum)
  732. {
  733. long long uninitialized_var(ec);
  734. int err, bitflips = 0, vol_id, ec_err = 0;
  735. dbg_bld("scan PEB %d", pnum);
  736. /* Skip bad physical eraseblocks */
  737. err = ubi_io_is_bad(ubi, pnum);
  738. if (err < 0)
  739. return err;
  740. else if (err) {
  741. /*
  742. * FIXME: this is actually duty of the I/O sub-system to
  743. * initialize this, but MTD does not provide enough
  744. * information.
  745. */
  746. si->bad_peb_count += 1;
  747. return 0;
  748. }
  749. err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
  750. if (err < 0)
  751. return err;
  752. switch (err) {
  753. case 0:
  754. break;
  755. case UBI_IO_BITFLIPS:
  756. bitflips = 1;
  757. break;
  758. case UBI_IO_FF:
  759. si->empty_peb_count += 1;
  760. return add_to_list(si, pnum, UBI_SCAN_UNKNOWN_EC, 0,
  761. &si->erase);
  762. case UBI_IO_FF_BITFLIPS:
  763. si->empty_peb_count += 1;
  764. return add_to_list(si, pnum, UBI_SCAN_UNKNOWN_EC, 1,
  765. &si->erase);
  766. case UBI_IO_BAD_HDR_EBADMSG:
  767. case UBI_IO_BAD_HDR:
  768. /*
  769. * We have to also look at the VID header, possibly it is not
  770. * corrupted. Set %bitflips flag in order to make this PEB be
  771. * moved and EC be re-created.
  772. */
  773. ec_err = err;
  774. ec = UBI_SCAN_UNKNOWN_EC;
  775. bitflips = 1;
  776. break;
  777. default:
  778. ubi_err("'ubi_io_read_ec_hdr()' returned unknown code %d", err);
  779. return -EINVAL;
  780. }
  781. if (!ec_err) {
  782. int image_seq;
  783. /* Make sure UBI version is OK */
  784. if (ech->version != UBI_VERSION) {
  785. ubi_err("this UBI version is %d, image version is %d",
  786. UBI_VERSION, (int)ech->version);
  787. return -EINVAL;
  788. }
  789. ec = be64_to_cpu(ech->ec);
  790. if (ec > UBI_MAX_ERASECOUNTER) {
  791. /*
  792. * Erase counter overflow. The EC headers have 64 bits
  793. * reserved, but we anyway make use of only 31 bit
  794. * values, as this seems to be enough for any existing
  795. * flash. Upgrade UBI and use 64-bit erase counters
  796. * internally.
  797. */
  798. ubi_err("erase counter overflow, max is %d",
  799. UBI_MAX_ERASECOUNTER);
  800. ubi_dump_ec_hdr(ech);
  801. return -EINVAL;
  802. }
  803. /*
  804. * Make sure that all PEBs have the same image sequence number.
  805. * This allows us to detect situations when users flash UBI
  806. * images incorrectly, so that the flash has the new UBI image
  807. * and leftovers from the old one. This feature was added
  808. * relatively recently, and the sequence number was always
  809. * zero, because old UBI implementations always set it to zero.
  810. * For this reasons, we do not panic if some PEBs have zero
  811. * sequence number, while other PEBs have non-zero sequence
  812. * number.
  813. */
  814. image_seq = be32_to_cpu(ech->image_seq);
  815. if (!ubi->image_seq && image_seq)
  816. ubi->image_seq = image_seq;
  817. if (ubi->image_seq && image_seq &&
  818. ubi->image_seq != image_seq) {
  819. ubi_err("bad image sequence number %d in PEB %d, "
  820. "expected %d", image_seq, pnum, ubi->image_seq);
  821. ubi_dump_ec_hdr(ech);
  822. return -EINVAL;
  823. }
  824. }
  825. /* OK, we've done with the EC header, let's look at the VID header */
  826. err = ubi_io_read_vid_hdr(ubi, pnum, vidh, 0);
  827. if (err < 0)
  828. return err;
  829. switch (err) {
  830. case 0:
  831. break;
  832. case UBI_IO_BITFLIPS:
  833. bitflips = 1;
  834. break;
  835. case UBI_IO_BAD_HDR_EBADMSG:
  836. if (ec_err == UBI_IO_BAD_HDR_EBADMSG)
  837. /*
  838. * Both EC and VID headers are corrupted and were read
  839. * with data integrity error, probably this is a bad
  840. * PEB, bit it is not marked as bad yet. This may also
  841. * be a result of power cut during erasure.
  842. */
  843. si->maybe_bad_peb_count += 1;
  844. case UBI_IO_BAD_HDR:
  845. if (ec_err)
  846. /*
  847. * Both headers are corrupted. There is a possibility
  848. * that this a valid UBI PEB which has corresponding
  849. * LEB, but the headers are corrupted. However, it is
  850. * impossible to distinguish it from a PEB which just
  851. * contains garbage because of a power cut during erase
  852. * operation. So we just schedule this PEB for erasure.
  853. *
  854. * Besides, in case of NOR flash, we deliberately
  855. * corrupt both headers because NOR flash erasure is
  856. * slow and can start from the end.
  857. */
  858. err = 0;
  859. else
  860. /*
  861. * The EC was OK, but the VID header is corrupted. We
  862. * have to check what is in the data area.
  863. */
  864. err = check_corruption(ubi, vidh, pnum);
  865. if (err < 0)
  866. return err;
  867. else if (!err)
  868. /* This corruption is caused by a power cut */
  869. err = add_to_list(si, pnum, ec, 1, &si->erase);
  870. else
  871. /* This is an unexpected corruption */
  872. err = add_corrupted(si, pnum, ec);
  873. if (err)
  874. return err;
  875. goto adjust_mean_ec;
  876. case UBI_IO_FF_BITFLIPS:
  877. err = add_to_list(si, pnum, ec, 1, &si->erase);
  878. if (err)
  879. return err;
  880. goto adjust_mean_ec;
  881. case UBI_IO_FF:
  882. if (ec_err)
  883. err = add_to_list(si, pnum, ec, 1, &si->erase);
  884. else
  885. err = add_to_list(si, pnum, ec, 0, &si->free);
  886. if (err)
  887. return err;
  888. goto adjust_mean_ec;
  889. default:
  890. ubi_err("'ubi_io_read_vid_hdr()' returned unknown code %d",
  891. err);
  892. return -EINVAL;
  893. }
  894. vol_id = be32_to_cpu(vidh->vol_id);
  895. if (vol_id > UBI_MAX_VOLUMES && vol_id != UBI_LAYOUT_VOLUME_ID) {
  896. int lnum = be32_to_cpu(vidh->lnum);
  897. /* Unsupported internal volume */
  898. switch (vidh->compat) {
  899. case UBI_COMPAT_DELETE:
  900. ubi_msg("\"delete\" compatible internal volume %d:%d"
  901. " found, will remove it", vol_id, lnum);
  902. err = add_to_list(si, pnum, ec, 1, &si->erase);
  903. if (err)
  904. return err;
  905. return 0;
  906. case UBI_COMPAT_RO:
  907. ubi_msg("read-only compatible internal volume %d:%d"
  908. " found, switch to read-only mode",
  909. vol_id, lnum);
  910. ubi->ro_mode = 1;
  911. break;
  912. case UBI_COMPAT_PRESERVE:
  913. ubi_msg("\"preserve\" compatible internal volume %d:%d"
  914. " found", vol_id, lnum);
  915. err = add_to_list(si, pnum, ec, 0, &si->alien);
  916. if (err)
  917. return err;
  918. return 0;
  919. case UBI_COMPAT_REJECT:
  920. ubi_err("incompatible internal volume %d:%d found",
  921. vol_id, lnum);
  922. return -EINVAL;
  923. }
  924. }
  925. if (ec_err)
  926. ubi_warn("valid VID header but corrupted EC header at PEB %d",
  927. pnum);
  928. err = ubi_scan_add_used(ubi, si, pnum, ec, vidh, bitflips);
  929. if (err)
  930. return err;
  931. adjust_mean_ec:
  932. if (!ec_err) {
  933. si->ec_sum += ec;
  934. si->ec_count += 1;
  935. if (ec > si->max_ec)
  936. si->max_ec = ec;
  937. if (ec < si->min_ec)
  938. si->min_ec = ec;
  939. }
  940. return 0;
  941. }
  942. /**
  943. * check_what_we_have - check what PEB were found by scanning.
  944. * @ubi: UBI device description object
  945. * @si: scanning information
  946. *
  947. * This is a helper function which takes a look what PEBs were found by
  948. * scanning, and decides whether the flash is empty and should be formatted and
  949. * whether there are too many corrupted PEBs and we should not attach this
  950. * MTD device. Returns zero if we should proceed with attaching the MTD device,
  951. * and %-EINVAL if we should not.
  952. */
  953. static int check_what_we_have(struct ubi_device *ubi,
  954. struct ubi_attach_info *si)
  955. {
  956. struct ubi_ainf_peb *aeb;
  957. int max_corr, peb_count;
  958. peb_count = ubi->peb_count - si->bad_peb_count - si->alien_peb_count;
  959. max_corr = peb_count / 20 ?: 8;
  960. /*
  961. * Few corrupted PEBs is not a problem and may be just a result of
  962. * unclean reboots. However, many of them may indicate some problems
  963. * with the flash HW or driver.
  964. */
  965. if (si->corr_peb_count) {
  966. ubi_err("%d PEBs are corrupted and preserved",
  967. si->corr_peb_count);
  968. printk(KERN_ERR "Corrupted PEBs are:");
  969. list_for_each_entry(aeb, &si->corr, u.list)
  970. printk(KERN_CONT " %d", aeb->pnum);
  971. printk(KERN_CONT "\n");
  972. /*
  973. * If too many PEBs are corrupted, we refuse attaching,
  974. * otherwise, only print a warning.
  975. */
  976. if (si->corr_peb_count >= max_corr) {
  977. ubi_err("too many corrupted PEBs, refusing");
  978. return -EINVAL;
  979. }
  980. }
  981. if (si->empty_peb_count + si->maybe_bad_peb_count == peb_count) {
  982. /*
  983. * All PEBs are empty, or almost all - a couple PEBs look like
  984. * they may be bad PEBs which were not marked as bad yet.
  985. *
  986. * This piece of code basically tries to distinguish between
  987. * the following situations:
  988. *
  989. * 1. Flash is empty, but there are few bad PEBs, which are not
  990. * marked as bad so far, and which were read with error. We
  991. * want to go ahead and format this flash. While formatting,
  992. * the faulty PEBs will probably be marked as bad.
  993. *
  994. * 2. Flash contains non-UBI data and we do not want to format
  995. * it and destroy possibly important information.
  996. */
  997. if (si->maybe_bad_peb_count <= 2) {
  998. si->is_empty = 1;
  999. ubi_msg("empty MTD device detected");
  1000. get_random_bytes(&ubi->image_seq,
  1001. sizeof(ubi->image_seq));
  1002. } else {
  1003. ubi_err("MTD device is not UBI-formatted and possibly "
  1004. "contains non-UBI data - refusing it");
  1005. return -EINVAL;
  1006. }
  1007. }
  1008. return 0;
  1009. }
  1010. /**
  1011. * ubi_scan - scan an MTD device.
  1012. * @ubi: UBI device description object
  1013. *
  1014. * This function does full scanning of an MTD device and returns complete
  1015. * information about it. In case of failure, an error code is returned.
  1016. */
  1017. struct ubi_attach_info *ubi_scan(struct ubi_device *ubi)
  1018. {
  1019. int err, pnum;
  1020. struct rb_node *rb1, *rb2;
  1021. struct ubi_ainf_volume *sv;
  1022. struct ubi_ainf_peb *aeb;
  1023. struct ubi_attach_info *si;
  1024. si = kzalloc(sizeof(struct ubi_attach_info), GFP_KERNEL);
  1025. if (!si)
  1026. return ERR_PTR(-ENOMEM);
  1027. INIT_LIST_HEAD(&si->corr);
  1028. INIT_LIST_HEAD(&si->free);
  1029. INIT_LIST_HEAD(&si->erase);
  1030. INIT_LIST_HEAD(&si->alien);
  1031. si->volumes = RB_ROOT;
  1032. err = -ENOMEM;
  1033. si->scan_leb_slab = kmem_cache_create("ubi_scan_leb_slab",
  1034. sizeof(struct ubi_ainf_peb),
  1035. 0, 0, NULL);
  1036. if (!si->scan_leb_slab)
  1037. goto out_si;
  1038. ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
  1039. if (!ech)
  1040. goto out_si;
  1041. vidh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
  1042. if (!vidh)
  1043. goto out_ech;
  1044. for (pnum = 0; pnum < ubi->peb_count; pnum++) {
  1045. cond_resched();
  1046. dbg_gen("process PEB %d", pnum);
  1047. err = process_eb(ubi, si, pnum);
  1048. if (err < 0)
  1049. goto out_vidh;
  1050. }
  1051. dbg_msg("scanning is finished");
  1052. /* Calculate mean erase counter */
  1053. if (si->ec_count)
  1054. si->mean_ec = div_u64(si->ec_sum, si->ec_count);
  1055. err = check_what_we_have(ubi, si);
  1056. if (err)
  1057. goto out_vidh;
  1058. /*
  1059. * In case of unknown erase counter we use the mean erase counter
  1060. * value.
  1061. */
  1062. ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
  1063. ubi_rb_for_each_entry(rb2, aeb, &sv->root, u.rb)
  1064. if (aeb->ec == UBI_SCAN_UNKNOWN_EC)
  1065. aeb->ec = si->mean_ec;
  1066. }
  1067. list_for_each_entry(aeb, &si->free, u.list) {
  1068. if (aeb->ec == UBI_SCAN_UNKNOWN_EC)
  1069. aeb->ec = si->mean_ec;
  1070. }
  1071. list_for_each_entry(aeb, &si->corr, u.list)
  1072. if (aeb->ec == UBI_SCAN_UNKNOWN_EC)
  1073. aeb->ec = si->mean_ec;
  1074. list_for_each_entry(aeb, &si->erase, u.list)
  1075. if (aeb->ec == UBI_SCAN_UNKNOWN_EC)
  1076. aeb->ec = si->mean_ec;
  1077. err = self_check_si(ubi, si);
  1078. if (err)
  1079. goto out_vidh;
  1080. ubi_free_vid_hdr(ubi, vidh);
  1081. kfree(ech);
  1082. return si;
  1083. out_vidh:
  1084. ubi_free_vid_hdr(ubi, vidh);
  1085. out_ech:
  1086. kfree(ech);
  1087. out_si:
  1088. ubi_scan_destroy_si(si);
  1089. return ERR_PTR(err);
  1090. }
  1091. /**
  1092. * destroy_sv - free the scanning volume information
  1093. * @sv: scanning volume information
  1094. * @si: scanning information
  1095. *
  1096. * This function destroys the volume RB-tree (@sv->root) and the scanning
  1097. * volume information.
  1098. */
  1099. static void destroy_sv(struct ubi_attach_info *si, struct ubi_ainf_volume *sv)
  1100. {
  1101. struct ubi_ainf_peb *aeb;
  1102. struct rb_node *this = sv->root.rb_node;
  1103. while (this) {
  1104. if (this->rb_left)
  1105. this = this->rb_left;
  1106. else if (this->rb_right)
  1107. this = this->rb_right;
  1108. else {
  1109. aeb = rb_entry(this, struct ubi_ainf_peb, u.rb);
  1110. this = rb_parent(this);
  1111. if (this) {
  1112. if (this->rb_left == &aeb->u.rb)
  1113. this->rb_left = NULL;
  1114. else
  1115. this->rb_right = NULL;
  1116. }
  1117. kmem_cache_free(si->scan_leb_slab, aeb);
  1118. }
  1119. }
  1120. kfree(sv);
  1121. }
  1122. /**
  1123. * ubi_scan_destroy_si - destroy scanning information.
  1124. * @si: scanning information
  1125. */
  1126. void ubi_scan_destroy_si(struct ubi_attach_info *si)
  1127. {
  1128. struct ubi_ainf_peb *aeb, *aeb_tmp;
  1129. struct ubi_ainf_volume *sv;
  1130. struct rb_node *rb;
  1131. list_for_each_entry_safe(aeb, aeb_tmp, &si->alien, u.list) {
  1132. list_del(&aeb->u.list);
  1133. kmem_cache_free(si->scan_leb_slab, aeb);
  1134. }
  1135. list_for_each_entry_safe(aeb, aeb_tmp, &si->erase, u.list) {
  1136. list_del(&aeb->u.list);
  1137. kmem_cache_free(si->scan_leb_slab, aeb);
  1138. }
  1139. list_for_each_entry_safe(aeb, aeb_tmp, &si->corr, u.list) {
  1140. list_del(&aeb->u.list);
  1141. kmem_cache_free(si->scan_leb_slab, aeb);
  1142. }
  1143. list_for_each_entry_safe(aeb, aeb_tmp, &si->free, u.list) {
  1144. list_del(&aeb->u.list);
  1145. kmem_cache_free(si->scan_leb_slab, aeb);
  1146. }
  1147. /* Destroy the volume RB-tree */
  1148. rb = si->volumes.rb_node;
  1149. while (rb) {
  1150. if (rb->rb_left)
  1151. rb = rb->rb_left;
  1152. else if (rb->rb_right)
  1153. rb = rb->rb_right;
  1154. else {
  1155. sv = rb_entry(rb, struct ubi_ainf_volume, rb);
  1156. rb = rb_parent(rb);
  1157. if (rb) {
  1158. if (rb->rb_left == &sv->rb)
  1159. rb->rb_left = NULL;
  1160. else
  1161. rb->rb_right = NULL;
  1162. }
  1163. destroy_sv(si, sv);
  1164. }
  1165. }
  1166. if (si->scan_leb_slab)
  1167. kmem_cache_destroy(si->scan_leb_slab);
  1168. kfree(si);
  1169. }
  1170. /**
  1171. * self_check_si - check the scanning information.
  1172. * @ubi: UBI device description object
  1173. * @si: scanning information
  1174. *
  1175. * This function returns zero if the scanning information is all right, and a
  1176. * negative error code if not or if an error occurred.
  1177. */
  1178. static int self_check_si(struct ubi_device *ubi, struct ubi_attach_info *si)
  1179. {
  1180. int pnum, err, vols_found = 0;
  1181. struct rb_node *rb1, *rb2;
  1182. struct ubi_ainf_volume *sv;
  1183. struct ubi_ainf_peb *aeb, *last_aeb;
  1184. uint8_t *buf;
  1185. if (!ubi->dbg->chk_gen)
  1186. return 0;
  1187. /*
  1188. * At first, check that scanning information is OK.
  1189. */
  1190. ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
  1191. int leb_count = 0;
  1192. cond_resched();
  1193. vols_found += 1;
  1194. if (si->is_empty) {
  1195. ubi_err("bad is_empty flag");
  1196. goto bad_sv;
  1197. }
  1198. if (sv->vol_id < 0 || sv->highest_lnum < 0 ||
  1199. sv->leb_count < 0 || sv->vol_type < 0 || sv->used_ebs < 0 ||
  1200. sv->data_pad < 0 || sv->last_data_size < 0) {
  1201. ubi_err("negative values");
  1202. goto bad_sv;
  1203. }
  1204. if (sv->vol_id >= UBI_MAX_VOLUMES &&
  1205. sv->vol_id < UBI_INTERNAL_VOL_START) {
  1206. ubi_err("bad vol_id");
  1207. goto bad_sv;
  1208. }
  1209. if (sv->vol_id > si->highest_vol_id) {
  1210. ubi_err("highest_vol_id is %d, but vol_id %d is there",
  1211. si->highest_vol_id, sv->vol_id);
  1212. goto out;
  1213. }
  1214. if (sv->vol_type != UBI_DYNAMIC_VOLUME &&
  1215. sv->vol_type != UBI_STATIC_VOLUME) {
  1216. ubi_err("bad vol_type");
  1217. goto bad_sv;
  1218. }
  1219. if (sv->data_pad > ubi->leb_size / 2) {
  1220. ubi_err("bad data_pad");
  1221. goto bad_sv;
  1222. }
  1223. last_aeb = NULL;
  1224. ubi_rb_for_each_entry(rb2, aeb, &sv->root, u.rb) {
  1225. cond_resched();
  1226. last_aeb = aeb;
  1227. leb_count += 1;
  1228. if (aeb->pnum < 0 || aeb->ec < 0) {
  1229. ubi_err("negative values");
  1230. goto bad_aeb;
  1231. }
  1232. if (aeb->ec < si->min_ec) {
  1233. ubi_err("bad si->min_ec (%d), %d found",
  1234. si->min_ec, aeb->ec);
  1235. goto bad_aeb;
  1236. }
  1237. if (aeb->ec > si->max_ec) {
  1238. ubi_err("bad si->max_ec (%d), %d found",
  1239. si->max_ec, aeb->ec);
  1240. goto bad_aeb;
  1241. }
  1242. if (aeb->pnum >= ubi->peb_count) {
  1243. ubi_err("too high PEB number %d, total PEBs %d",
  1244. aeb->pnum, ubi->peb_count);
  1245. goto bad_aeb;
  1246. }
  1247. if (sv->vol_type == UBI_STATIC_VOLUME) {
  1248. if (aeb->lnum >= sv->used_ebs) {
  1249. ubi_err("bad lnum or used_ebs");
  1250. goto bad_aeb;
  1251. }
  1252. } else {
  1253. if (sv->used_ebs != 0) {
  1254. ubi_err("non-zero used_ebs");
  1255. goto bad_aeb;
  1256. }
  1257. }
  1258. if (aeb->lnum > sv->highest_lnum) {
  1259. ubi_err("incorrect highest_lnum or lnum");
  1260. goto bad_aeb;
  1261. }
  1262. }
  1263. if (sv->leb_count != leb_count) {
  1264. ubi_err("bad leb_count, %d objects in the tree",
  1265. leb_count);
  1266. goto bad_sv;
  1267. }
  1268. if (!last_aeb)
  1269. continue;
  1270. aeb = last_aeb;
  1271. if (aeb->lnum != sv->highest_lnum) {
  1272. ubi_err("bad highest_lnum");
  1273. goto bad_aeb;
  1274. }
  1275. }
  1276. if (vols_found != si->vols_found) {
  1277. ubi_err("bad si->vols_found %d, should be %d",
  1278. si->vols_found, vols_found);
  1279. goto out;
  1280. }
  1281. /* Check that scanning information is correct */
  1282. ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
  1283. last_aeb = NULL;
  1284. ubi_rb_for_each_entry(rb2, aeb, &sv->root, u.rb) {
  1285. int vol_type;
  1286. cond_resched();
  1287. last_aeb = aeb;
  1288. err = ubi_io_read_vid_hdr(ubi, aeb->pnum, vidh, 1);
  1289. if (err && err != UBI_IO_BITFLIPS) {
  1290. ubi_err("VID header is not OK (%d)", err);
  1291. if (err > 0)
  1292. err = -EIO;
  1293. return err;
  1294. }
  1295. vol_type = vidh->vol_type == UBI_VID_DYNAMIC ?
  1296. UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME;
  1297. if (sv->vol_type != vol_type) {
  1298. ubi_err("bad vol_type");
  1299. goto bad_vid_hdr;
  1300. }
  1301. if (aeb->sqnum != be64_to_cpu(vidh->sqnum)) {
  1302. ubi_err("bad sqnum %llu", aeb->sqnum);
  1303. goto bad_vid_hdr;
  1304. }
  1305. if (sv->vol_id != be32_to_cpu(vidh->vol_id)) {
  1306. ubi_err("bad vol_id %d", sv->vol_id);
  1307. goto bad_vid_hdr;
  1308. }
  1309. if (sv->compat != vidh->compat) {
  1310. ubi_err("bad compat %d", vidh->compat);
  1311. goto bad_vid_hdr;
  1312. }
  1313. if (aeb->lnum != be32_to_cpu(vidh->lnum)) {
  1314. ubi_err("bad lnum %d", aeb->lnum);
  1315. goto bad_vid_hdr;
  1316. }
  1317. if (sv->used_ebs != be32_to_cpu(vidh->used_ebs)) {
  1318. ubi_err("bad used_ebs %d", sv->used_ebs);
  1319. goto bad_vid_hdr;
  1320. }
  1321. if (sv->data_pad != be32_to_cpu(vidh->data_pad)) {
  1322. ubi_err("bad data_pad %d", sv->data_pad);
  1323. goto bad_vid_hdr;
  1324. }
  1325. }
  1326. if (!last_aeb)
  1327. continue;
  1328. if (sv->highest_lnum != be32_to_cpu(vidh->lnum)) {
  1329. ubi_err("bad highest_lnum %d", sv->highest_lnum);
  1330. goto bad_vid_hdr;
  1331. }
  1332. if (sv->last_data_size != be32_to_cpu(vidh->data_size)) {
  1333. ubi_err("bad last_data_size %d", sv->last_data_size);
  1334. goto bad_vid_hdr;
  1335. }
  1336. }
  1337. /*
  1338. * Make sure that all the physical eraseblocks are in one of the lists
  1339. * or trees.
  1340. */
  1341. buf = kzalloc(ubi->peb_count, GFP_KERNEL);
  1342. if (!buf)
  1343. return -ENOMEM;
  1344. for (pnum = 0; pnum < ubi->peb_count; pnum++) {
  1345. err = ubi_io_is_bad(ubi, pnum);
  1346. if (err < 0) {
  1347. kfree(buf);
  1348. return err;
  1349. } else if (err)
  1350. buf[pnum] = 1;
  1351. }
  1352. ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb)
  1353. ubi_rb_for_each_entry(rb2, aeb, &sv->root, u.rb)
  1354. buf[aeb->pnum] = 1;
  1355. list_for_each_entry(aeb, &si->free, u.list)
  1356. buf[aeb->pnum] = 1;
  1357. list_for_each_entry(aeb, &si->corr, u.list)
  1358. buf[aeb->pnum] = 1;
  1359. list_for_each_entry(aeb, &si->erase, u.list)
  1360. buf[aeb->pnum] = 1;
  1361. list_for_each_entry(aeb, &si->alien, u.list)
  1362. buf[aeb->pnum] = 1;
  1363. err = 0;
  1364. for (pnum = 0; pnum < ubi->peb_count; pnum++)
  1365. if (!buf[pnum]) {
  1366. ubi_err("PEB %d is not referred", pnum);
  1367. err = 1;
  1368. }
  1369. kfree(buf);
  1370. if (err)
  1371. goto out;
  1372. return 0;
  1373. bad_aeb:
  1374. ubi_err("bad scanning information about LEB %d", aeb->lnum);
  1375. ubi_dump_aeb(aeb, 0);
  1376. ubi_dump_sv(sv);
  1377. goto out;
  1378. bad_sv:
  1379. ubi_err("bad scanning information about volume %d", sv->vol_id);
  1380. ubi_dump_sv(sv);
  1381. goto out;
  1382. bad_vid_hdr:
  1383. ubi_err("bad scanning information about volume %d", sv->vol_id);
  1384. ubi_dump_sv(sv);
  1385. ubi_dump_vid_hdr(vidh);
  1386. out:
  1387. dump_stack();
  1388. return -EINVAL;
  1389. }