attach.c 47 KB

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