debug.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546
  1. /*
  2. * This file is part of UBIFS.
  3. *
  4. * Copyright (C) 2006-2008 Nokia Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc., 51
  17. * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. * Authors: Artem Bityutskiy (Битюцкий Артём)
  20. * Adrian Hunter
  21. */
  22. /*
  23. * This file implements most of the debugging stuff which is compiled in only
  24. * when it is enabled. But some debugging check functions are implemented in
  25. * corresponding subsystem, just because they are closely related and utilize
  26. * various local functions of those subsystems.
  27. */
  28. #define UBIFS_DBG_PRESERVE_UBI
  29. #include "ubifs.h"
  30. #include <linux/module.h>
  31. #include <linux/moduleparam.h>
  32. #include <linux/debugfs.h>
  33. #ifdef CONFIG_UBIFS_FS_DEBUG
  34. DEFINE_SPINLOCK(dbg_lock);
  35. static char dbg_key_buf0[128];
  36. static char dbg_key_buf1[128];
  37. unsigned int ubifs_msg_flags = UBIFS_MSG_FLAGS_DEFAULT;
  38. unsigned int ubifs_chk_flags = UBIFS_CHK_FLAGS_DEFAULT;
  39. unsigned int ubifs_tst_flags;
  40. module_param_named(debug_msgs, ubifs_msg_flags, uint, S_IRUGO | S_IWUSR);
  41. module_param_named(debug_chks, ubifs_chk_flags, uint, S_IRUGO | S_IWUSR);
  42. module_param_named(debug_tsts, ubifs_tst_flags, uint, S_IRUGO | S_IWUSR);
  43. MODULE_PARM_DESC(debug_msgs, "Debug message type flags");
  44. MODULE_PARM_DESC(debug_chks, "Debug check flags");
  45. MODULE_PARM_DESC(debug_tsts, "Debug special test flags");
  46. static const char *get_key_fmt(int fmt)
  47. {
  48. switch (fmt) {
  49. case UBIFS_SIMPLE_KEY_FMT:
  50. return "simple";
  51. default:
  52. return "unknown/invalid format";
  53. }
  54. }
  55. static const char *get_key_hash(int hash)
  56. {
  57. switch (hash) {
  58. case UBIFS_KEY_HASH_R5:
  59. return "R5";
  60. case UBIFS_KEY_HASH_TEST:
  61. return "test";
  62. default:
  63. return "unknown/invalid name hash";
  64. }
  65. }
  66. static const char *get_key_type(int type)
  67. {
  68. switch (type) {
  69. case UBIFS_INO_KEY:
  70. return "inode";
  71. case UBIFS_DENT_KEY:
  72. return "direntry";
  73. case UBIFS_XENT_KEY:
  74. return "xentry";
  75. case UBIFS_DATA_KEY:
  76. return "data";
  77. case UBIFS_TRUN_KEY:
  78. return "truncate";
  79. default:
  80. return "unknown/invalid key";
  81. }
  82. }
  83. static void sprintf_key(const struct ubifs_info *c, const union ubifs_key *key,
  84. char *buffer)
  85. {
  86. char *p = buffer;
  87. int type = key_type(c, key);
  88. if (c->key_fmt == UBIFS_SIMPLE_KEY_FMT) {
  89. switch (type) {
  90. case UBIFS_INO_KEY:
  91. sprintf(p, "(%lu, %s)", (unsigned long)key_inum(c, key),
  92. get_key_type(type));
  93. break;
  94. case UBIFS_DENT_KEY:
  95. case UBIFS_XENT_KEY:
  96. sprintf(p, "(%lu, %s, %#08x)",
  97. (unsigned long)key_inum(c, key),
  98. get_key_type(type), key_hash(c, key));
  99. break;
  100. case UBIFS_DATA_KEY:
  101. sprintf(p, "(%lu, %s, %u)",
  102. (unsigned long)key_inum(c, key),
  103. get_key_type(type), key_block(c, key));
  104. break;
  105. case UBIFS_TRUN_KEY:
  106. sprintf(p, "(%lu, %s)",
  107. (unsigned long)key_inum(c, key),
  108. get_key_type(type));
  109. break;
  110. default:
  111. sprintf(p, "(bad key type: %#08x, %#08x)",
  112. key->u32[0], key->u32[1]);
  113. }
  114. } else
  115. sprintf(p, "bad key format %d", c->key_fmt);
  116. }
  117. const char *dbg_key_str0(const struct ubifs_info *c, const union ubifs_key *key)
  118. {
  119. /* dbg_lock must be held */
  120. sprintf_key(c, key, dbg_key_buf0);
  121. return dbg_key_buf0;
  122. }
  123. const char *dbg_key_str1(const struct ubifs_info *c, const union ubifs_key *key)
  124. {
  125. /* dbg_lock must be held */
  126. sprintf_key(c, key, dbg_key_buf1);
  127. return dbg_key_buf1;
  128. }
  129. const char *dbg_ntype(int type)
  130. {
  131. switch (type) {
  132. case UBIFS_PAD_NODE:
  133. return "padding node";
  134. case UBIFS_SB_NODE:
  135. return "superblock node";
  136. case UBIFS_MST_NODE:
  137. return "master node";
  138. case UBIFS_REF_NODE:
  139. return "reference node";
  140. case UBIFS_INO_NODE:
  141. return "inode node";
  142. case UBIFS_DENT_NODE:
  143. return "direntry node";
  144. case UBIFS_XENT_NODE:
  145. return "xentry node";
  146. case UBIFS_DATA_NODE:
  147. return "data node";
  148. case UBIFS_TRUN_NODE:
  149. return "truncate node";
  150. case UBIFS_IDX_NODE:
  151. return "indexing node";
  152. case UBIFS_CS_NODE:
  153. return "commit start node";
  154. case UBIFS_ORPH_NODE:
  155. return "orphan node";
  156. default:
  157. return "unknown node";
  158. }
  159. }
  160. static const char *dbg_gtype(int type)
  161. {
  162. switch (type) {
  163. case UBIFS_NO_NODE_GROUP:
  164. return "no node group";
  165. case UBIFS_IN_NODE_GROUP:
  166. return "in node group";
  167. case UBIFS_LAST_OF_NODE_GROUP:
  168. return "last of node group";
  169. default:
  170. return "unknown";
  171. }
  172. }
  173. const char *dbg_cstate(int cmt_state)
  174. {
  175. switch (cmt_state) {
  176. case COMMIT_RESTING:
  177. return "commit resting";
  178. case COMMIT_BACKGROUND:
  179. return "background commit requested";
  180. case COMMIT_REQUIRED:
  181. return "commit required";
  182. case COMMIT_RUNNING_BACKGROUND:
  183. return "BACKGROUND commit running";
  184. case COMMIT_RUNNING_REQUIRED:
  185. return "commit running and required";
  186. case COMMIT_BROKEN:
  187. return "broken commit";
  188. default:
  189. return "unknown commit state";
  190. }
  191. }
  192. static void dump_ch(const struct ubifs_ch *ch)
  193. {
  194. printk(KERN_DEBUG "\tmagic %#x\n", le32_to_cpu(ch->magic));
  195. printk(KERN_DEBUG "\tcrc %#x\n", le32_to_cpu(ch->crc));
  196. printk(KERN_DEBUG "\tnode_type %d (%s)\n", ch->node_type,
  197. dbg_ntype(ch->node_type));
  198. printk(KERN_DEBUG "\tgroup_type %d (%s)\n", ch->group_type,
  199. dbg_gtype(ch->group_type));
  200. printk(KERN_DEBUG "\tsqnum %llu\n",
  201. (unsigned long long)le64_to_cpu(ch->sqnum));
  202. printk(KERN_DEBUG "\tlen %u\n", le32_to_cpu(ch->len));
  203. }
  204. void dbg_dump_inode(const struct ubifs_info *c, const struct inode *inode)
  205. {
  206. const struct ubifs_inode *ui = ubifs_inode(inode);
  207. printk(KERN_DEBUG "Dump in-memory inode:");
  208. printk(KERN_DEBUG "\tinode %lu\n", inode->i_ino);
  209. printk(KERN_DEBUG "\tsize %llu\n",
  210. (unsigned long long)i_size_read(inode));
  211. printk(KERN_DEBUG "\tnlink %u\n", inode->i_nlink);
  212. printk(KERN_DEBUG "\tuid %u\n", (unsigned int)inode->i_uid);
  213. printk(KERN_DEBUG "\tgid %u\n", (unsigned int)inode->i_gid);
  214. printk(KERN_DEBUG "\tatime %u.%u\n",
  215. (unsigned int)inode->i_atime.tv_sec,
  216. (unsigned int)inode->i_atime.tv_nsec);
  217. printk(KERN_DEBUG "\tmtime %u.%u\n",
  218. (unsigned int)inode->i_mtime.tv_sec,
  219. (unsigned int)inode->i_mtime.tv_nsec);
  220. printk(KERN_DEBUG "\tctime %u.%u\n",
  221. (unsigned int)inode->i_ctime.tv_sec,
  222. (unsigned int)inode->i_ctime.tv_nsec);
  223. printk(KERN_DEBUG "\tcreat_sqnum %llu\n", ui->creat_sqnum);
  224. printk(KERN_DEBUG "\txattr_size %u\n", ui->xattr_size);
  225. printk(KERN_DEBUG "\txattr_cnt %u\n", ui->xattr_cnt);
  226. printk(KERN_DEBUG "\txattr_names %u\n", ui->xattr_names);
  227. printk(KERN_DEBUG "\tdirty %u\n", ui->dirty);
  228. printk(KERN_DEBUG "\txattr %u\n", ui->xattr);
  229. printk(KERN_DEBUG "\tbulk_read %u\n", ui->xattr);
  230. printk(KERN_DEBUG "\tsynced_i_size %llu\n",
  231. (unsigned long long)ui->synced_i_size);
  232. printk(KERN_DEBUG "\tui_size %llu\n",
  233. (unsigned long long)ui->ui_size);
  234. printk(KERN_DEBUG "\tflags %d\n", ui->flags);
  235. printk(KERN_DEBUG "\tcompr_type %d\n", ui->compr_type);
  236. printk(KERN_DEBUG "\tlast_page_read %lu\n", ui->last_page_read);
  237. printk(KERN_DEBUG "\tread_in_a_row %lu\n", ui->read_in_a_row);
  238. printk(KERN_DEBUG "\tdata_len %d\n", ui->data_len);
  239. }
  240. void dbg_dump_node(const struct ubifs_info *c, const void *node)
  241. {
  242. int i, n;
  243. union ubifs_key key;
  244. const struct ubifs_ch *ch = node;
  245. if (dbg_failure_mode)
  246. return;
  247. /* If the magic is incorrect, just hexdump the first bytes */
  248. if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC) {
  249. printk(KERN_DEBUG "Not a node, first %zu bytes:", UBIFS_CH_SZ);
  250. print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
  251. (void *)node, UBIFS_CH_SZ, 1);
  252. return;
  253. }
  254. spin_lock(&dbg_lock);
  255. dump_ch(node);
  256. switch (ch->node_type) {
  257. case UBIFS_PAD_NODE:
  258. {
  259. const struct ubifs_pad_node *pad = node;
  260. printk(KERN_DEBUG "\tpad_len %u\n",
  261. le32_to_cpu(pad->pad_len));
  262. break;
  263. }
  264. case UBIFS_SB_NODE:
  265. {
  266. const struct ubifs_sb_node *sup = node;
  267. unsigned int sup_flags = le32_to_cpu(sup->flags);
  268. printk(KERN_DEBUG "\tkey_hash %d (%s)\n",
  269. (int)sup->key_hash, get_key_hash(sup->key_hash));
  270. printk(KERN_DEBUG "\tkey_fmt %d (%s)\n",
  271. (int)sup->key_fmt, get_key_fmt(sup->key_fmt));
  272. printk(KERN_DEBUG "\tflags %#x\n", sup_flags);
  273. printk(KERN_DEBUG "\t big_lpt %u\n",
  274. !!(sup_flags & UBIFS_FLG_BIGLPT));
  275. printk(KERN_DEBUG "\tmin_io_size %u\n",
  276. le32_to_cpu(sup->min_io_size));
  277. printk(KERN_DEBUG "\tleb_size %u\n",
  278. le32_to_cpu(sup->leb_size));
  279. printk(KERN_DEBUG "\tleb_cnt %u\n",
  280. le32_to_cpu(sup->leb_cnt));
  281. printk(KERN_DEBUG "\tmax_leb_cnt %u\n",
  282. le32_to_cpu(sup->max_leb_cnt));
  283. printk(KERN_DEBUG "\tmax_bud_bytes %llu\n",
  284. (unsigned long long)le64_to_cpu(sup->max_bud_bytes));
  285. printk(KERN_DEBUG "\tlog_lebs %u\n",
  286. le32_to_cpu(sup->log_lebs));
  287. printk(KERN_DEBUG "\tlpt_lebs %u\n",
  288. le32_to_cpu(sup->lpt_lebs));
  289. printk(KERN_DEBUG "\torph_lebs %u\n",
  290. le32_to_cpu(sup->orph_lebs));
  291. printk(KERN_DEBUG "\tjhead_cnt %u\n",
  292. le32_to_cpu(sup->jhead_cnt));
  293. printk(KERN_DEBUG "\tfanout %u\n",
  294. le32_to_cpu(sup->fanout));
  295. printk(KERN_DEBUG "\tlsave_cnt %u\n",
  296. le32_to_cpu(sup->lsave_cnt));
  297. printk(KERN_DEBUG "\tdefault_compr %u\n",
  298. (int)le16_to_cpu(sup->default_compr));
  299. printk(KERN_DEBUG "\trp_size %llu\n",
  300. (unsigned long long)le64_to_cpu(sup->rp_size));
  301. printk(KERN_DEBUG "\trp_uid %u\n",
  302. le32_to_cpu(sup->rp_uid));
  303. printk(KERN_DEBUG "\trp_gid %u\n",
  304. le32_to_cpu(sup->rp_gid));
  305. printk(KERN_DEBUG "\tfmt_version %u\n",
  306. le32_to_cpu(sup->fmt_version));
  307. printk(KERN_DEBUG "\ttime_gran %u\n",
  308. le32_to_cpu(sup->time_gran));
  309. printk(KERN_DEBUG "\tUUID %02X%02X%02X%02X-%02X%02X"
  310. "-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X\n",
  311. sup->uuid[0], sup->uuid[1], sup->uuid[2], sup->uuid[3],
  312. sup->uuid[4], sup->uuid[5], sup->uuid[6], sup->uuid[7],
  313. sup->uuid[8], sup->uuid[9], sup->uuid[10], sup->uuid[11],
  314. sup->uuid[12], sup->uuid[13], sup->uuid[14],
  315. sup->uuid[15]);
  316. break;
  317. }
  318. case UBIFS_MST_NODE:
  319. {
  320. const struct ubifs_mst_node *mst = node;
  321. printk(KERN_DEBUG "\thighest_inum %llu\n",
  322. (unsigned long long)le64_to_cpu(mst->highest_inum));
  323. printk(KERN_DEBUG "\tcommit number %llu\n",
  324. (unsigned long long)le64_to_cpu(mst->cmt_no));
  325. printk(KERN_DEBUG "\tflags %#x\n",
  326. le32_to_cpu(mst->flags));
  327. printk(KERN_DEBUG "\tlog_lnum %u\n",
  328. le32_to_cpu(mst->log_lnum));
  329. printk(KERN_DEBUG "\troot_lnum %u\n",
  330. le32_to_cpu(mst->root_lnum));
  331. printk(KERN_DEBUG "\troot_offs %u\n",
  332. le32_to_cpu(mst->root_offs));
  333. printk(KERN_DEBUG "\troot_len %u\n",
  334. le32_to_cpu(mst->root_len));
  335. printk(KERN_DEBUG "\tgc_lnum %u\n",
  336. le32_to_cpu(mst->gc_lnum));
  337. printk(KERN_DEBUG "\tihead_lnum %u\n",
  338. le32_to_cpu(mst->ihead_lnum));
  339. printk(KERN_DEBUG "\tihead_offs %u\n",
  340. le32_to_cpu(mst->ihead_offs));
  341. printk(KERN_DEBUG "\tindex_size %llu\n",
  342. (unsigned long long)le64_to_cpu(mst->index_size));
  343. printk(KERN_DEBUG "\tlpt_lnum %u\n",
  344. le32_to_cpu(mst->lpt_lnum));
  345. printk(KERN_DEBUG "\tlpt_offs %u\n",
  346. le32_to_cpu(mst->lpt_offs));
  347. printk(KERN_DEBUG "\tnhead_lnum %u\n",
  348. le32_to_cpu(mst->nhead_lnum));
  349. printk(KERN_DEBUG "\tnhead_offs %u\n",
  350. le32_to_cpu(mst->nhead_offs));
  351. printk(KERN_DEBUG "\tltab_lnum %u\n",
  352. le32_to_cpu(mst->ltab_lnum));
  353. printk(KERN_DEBUG "\tltab_offs %u\n",
  354. le32_to_cpu(mst->ltab_offs));
  355. printk(KERN_DEBUG "\tlsave_lnum %u\n",
  356. le32_to_cpu(mst->lsave_lnum));
  357. printk(KERN_DEBUG "\tlsave_offs %u\n",
  358. le32_to_cpu(mst->lsave_offs));
  359. printk(KERN_DEBUG "\tlscan_lnum %u\n",
  360. le32_to_cpu(mst->lscan_lnum));
  361. printk(KERN_DEBUG "\tleb_cnt %u\n",
  362. le32_to_cpu(mst->leb_cnt));
  363. printk(KERN_DEBUG "\tempty_lebs %u\n",
  364. le32_to_cpu(mst->empty_lebs));
  365. printk(KERN_DEBUG "\tidx_lebs %u\n",
  366. le32_to_cpu(mst->idx_lebs));
  367. printk(KERN_DEBUG "\ttotal_free %llu\n",
  368. (unsigned long long)le64_to_cpu(mst->total_free));
  369. printk(KERN_DEBUG "\ttotal_dirty %llu\n",
  370. (unsigned long long)le64_to_cpu(mst->total_dirty));
  371. printk(KERN_DEBUG "\ttotal_used %llu\n",
  372. (unsigned long long)le64_to_cpu(mst->total_used));
  373. printk(KERN_DEBUG "\ttotal_dead %llu\n",
  374. (unsigned long long)le64_to_cpu(mst->total_dead));
  375. printk(KERN_DEBUG "\ttotal_dark %llu\n",
  376. (unsigned long long)le64_to_cpu(mst->total_dark));
  377. break;
  378. }
  379. case UBIFS_REF_NODE:
  380. {
  381. const struct ubifs_ref_node *ref = node;
  382. printk(KERN_DEBUG "\tlnum %u\n",
  383. le32_to_cpu(ref->lnum));
  384. printk(KERN_DEBUG "\toffs %u\n",
  385. le32_to_cpu(ref->offs));
  386. printk(KERN_DEBUG "\tjhead %u\n",
  387. le32_to_cpu(ref->jhead));
  388. break;
  389. }
  390. case UBIFS_INO_NODE:
  391. {
  392. const struct ubifs_ino_node *ino = node;
  393. key_read(c, &ino->key, &key);
  394. printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
  395. printk(KERN_DEBUG "\tcreat_sqnum %llu\n",
  396. (unsigned long long)le64_to_cpu(ino->creat_sqnum));
  397. printk(KERN_DEBUG "\tsize %llu\n",
  398. (unsigned long long)le64_to_cpu(ino->size));
  399. printk(KERN_DEBUG "\tnlink %u\n",
  400. le32_to_cpu(ino->nlink));
  401. printk(KERN_DEBUG "\tatime %lld.%u\n",
  402. (long long)le64_to_cpu(ino->atime_sec),
  403. le32_to_cpu(ino->atime_nsec));
  404. printk(KERN_DEBUG "\tmtime %lld.%u\n",
  405. (long long)le64_to_cpu(ino->mtime_sec),
  406. le32_to_cpu(ino->mtime_nsec));
  407. printk(KERN_DEBUG "\tctime %lld.%u\n",
  408. (long long)le64_to_cpu(ino->ctime_sec),
  409. le32_to_cpu(ino->ctime_nsec));
  410. printk(KERN_DEBUG "\tuid %u\n",
  411. le32_to_cpu(ino->uid));
  412. printk(KERN_DEBUG "\tgid %u\n",
  413. le32_to_cpu(ino->gid));
  414. printk(KERN_DEBUG "\tmode %u\n",
  415. le32_to_cpu(ino->mode));
  416. printk(KERN_DEBUG "\tflags %#x\n",
  417. le32_to_cpu(ino->flags));
  418. printk(KERN_DEBUG "\txattr_cnt %u\n",
  419. le32_to_cpu(ino->xattr_cnt));
  420. printk(KERN_DEBUG "\txattr_size %u\n",
  421. le32_to_cpu(ino->xattr_size));
  422. printk(KERN_DEBUG "\txattr_names %u\n",
  423. le32_to_cpu(ino->xattr_names));
  424. printk(KERN_DEBUG "\tcompr_type %#x\n",
  425. (int)le16_to_cpu(ino->compr_type));
  426. printk(KERN_DEBUG "\tdata len %u\n",
  427. le32_to_cpu(ino->data_len));
  428. break;
  429. }
  430. case UBIFS_DENT_NODE:
  431. case UBIFS_XENT_NODE:
  432. {
  433. const struct ubifs_dent_node *dent = node;
  434. int nlen = le16_to_cpu(dent->nlen);
  435. key_read(c, &dent->key, &key);
  436. printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
  437. printk(KERN_DEBUG "\tinum %llu\n",
  438. (unsigned long long)le64_to_cpu(dent->inum));
  439. printk(KERN_DEBUG "\ttype %d\n", (int)dent->type);
  440. printk(KERN_DEBUG "\tnlen %d\n", nlen);
  441. printk(KERN_DEBUG "\tname ");
  442. if (nlen > UBIFS_MAX_NLEN)
  443. printk(KERN_DEBUG "(bad name length, not printing, "
  444. "bad or corrupted node)");
  445. else {
  446. for (i = 0; i < nlen && dent->name[i]; i++)
  447. printk("%c", dent->name[i]);
  448. }
  449. printk("\n");
  450. break;
  451. }
  452. case UBIFS_DATA_NODE:
  453. {
  454. const struct ubifs_data_node *dn = node;
  455. int dlen = le32_to_cpu(ch->len) - UBIFS_DATA_NODE_SZ;
  456. key_read(c, &dn->key, &key);
  457. printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
  458. printk(KERN_DEBUG "\tsize %u\n",
  459. le32_to_cpu(dn->size));
  460. printk(KERN_DEBUG "\tcompr_typ %d\n",
  461. (int)le16_to_cpu(dn->compr_type));
  462. printk(KERN_DEBUG "\tdata size %d\n",
  463. dlen);
  464. printk(KERN_DEBUG "\tdata:\n");
  465. print_hex_dump(KERN_DEBUG, "\t", DUMP_PREFIX_OFFSET, 32, 1,
  466. (void *)&dn->data, dlen, 0);
  467. break;
  468. }
  469. case UBIFS_TRUN_NODE:
  470. {
  471. const struct ubifs_trun_node *trun = node;
  472. printk(KERN_DEBUG "\tinum %u\n",
  473. le32_to_cpu(trun->inum));
  474. printk(KERN_DEBUG "\told_size %llu\n",
  475. (unsigned long long)le64_to_cpu(trun->old_size));
  476. printk(KERN_DEBUG "\tnew_size %llu\n",
  477. (unsigned long long)le64_to_cpu(trun->new_size));
  478. break;
  479. }
  480. case UBIFS_IDX_NODE:
  481. {
  482. const struct ubifs_idx_node *idx = node;
  483. n = le16_to_cpu(idx->child_cnt);
  484. printk(KERN_DEBUG "\tchild_cnt %d\n", n);
  485. printk(KERN_DEBUG "\tlevel %d\n",
  486. (int)le16_to_cpu(idx->level));
  487. printk(KERN_DEBUG "\tBranches:\n");
  488. for (i = 0; i < n && i < c->fanout - 1; i++) {
  489. const struct ubifs_branch *br;
  490. br = ubifs_idx_branch(c, idx, i);
  491. key_read(c, &br->key, &key);
  492. printk(KERN_DEBUG "\t%d: LEB %d:%d len %d key %s\n",
  493. i, le32_to_cpu(br->lnum), le32_to_cpu(br->offs),
  494. le32_to_cpu(br->len), DBGKEY(&key));
  495. }
  496. break;
  497. }
  498. case UBIFS_CS_NODE:
  499. break;
  500. case UBIFS_ORPH_NODE:
  501. {
  502. const struct ubifs_orph_node *orph = node;
  503. printk(KERN_DEBUG "\tcommit number %llu\n",
  504. (unsigned long long)
  505. le64_to_cpu(orph->cmt_no) & LLONG_MAX);
  506. printk(KERN_DEBUG "\tlast node flag %llu\n",
  507. (unsigned long long)(le64_to_cpu(orph->cmt_no)) >> 63);
  508. n = (le32_to_cpu(ch->len) - UBIFS_ORPH_NODE_SZ) >> 3;
  509. printk(KERN_DEBUG "\t%d orphan inode numbers:\n", n);
  510. for (i = 0; i < n; i++)
  511. printk(KERN_DEBUG "\t ino %llu\n",
  512. (unsigned long long)le64_to_cpu(orph->inos[i]));
  513. break;
  514. }
  515. default:
  516. printk(KERN_DEBUG "node type %d was not recognized\n",
  517. (int)ch->node_type);
  518. }
  519. spin_unlock(&dbg_lock);
  520. }
  521. void dbg_dump_budget_req(const struct ubifs_budget_req *req)
  522. {
  523. spin_lock(&dbg_lock);
  524. printk(KERN_DEBUG "Budgeting request: new_ino %d, dirtied_ino %d\n",
  525. req->new_ino, req->dirtied_ino);
  526. printk(KERN_DEBUG "\tnew_ino_d %d, dirtied_ino_d %d\n",
  527. req->new_ino_d, req->dirtied_ino_d);
  528. printk(KERN_DEBUG "\tnew_page %d, dirtied_page %d\n",
  529. req->new_page, req->dirtied_page);
  530. printk(KERN_DEBUG "\tnew_dent %d, mod_dent %d\n",
  531. req->new_dent, req->mod_dent);
  532. printk(KERN_DEBUG "\tidx_growth %d\n", req->idx_growth);
  533. printk(KERN_DEBUG "\tdata_growth %d dd_growth %d\n",
  534. req->data_growth, req->dd_growth);
  535. spin_unlock(&dbg_lock);
  536. }
  537. void dbg_dump_lstats(const struct ubifs_lp_stats *lst)
  538. {
  539. spin_lock(&dbg_lock);
  540. printk(KERN_DEBUG "(pid %d) Lprops statistics: empty_lebs %d, "
  541. "idx_lebs %d\n", current->pid, lst->empty_lebs, lst->idx_lebs);
  542. printk(KERN_DEBUG "\ttaken_empty_lebs %d, total_free %lld, "
  543. "total_dirty %lld\n", lst->taken_empty_lebs, lst->total_free,
  544. lst->total_dirty);
  545. printk(KERN_DEBUG "\ttotal_used %lld, total_dark %lld, "
  546. "total_dead %lld\n", lst->total_used, lst->total_dark,
  547. lst->total_dead);
  548. spin_unlock(&dbg_lock);
  549. }
  550. void dbg_dump_budg(struct ubifs_info *c)
  551. {
  552. int i;
  553. struct rb_node *rb;
  554. struct ubifs_bud *bud;
  555. struct ubifs_gced_idx_leb *idx_gc;
  556. long long available, outstanding, free;
  557. ubifs_assert(spin_is_locked(&c->space_lock));
  558. spin_lock(&dbg_lock);
  559. printk(KERN_DEBUG "(pid %d) Budgeting info: budg_data_growth %lld, "
  560. "budg_dd_growth %lld, budg_idx_growth %lld\n", current->pid,
  561. c->budg_data_growth, c->budg_dd_growth, c->budg_idx_growth);
  562. printk(KERN_DEBUG "\tdata budget sum %lld, total budget sum %lld, "
  563. "freeable_cnt %d\n", c->budg_data_growth + c->budg_dd_growth,
  564. c->budg_data_growth + c->budg_dd_growth + c->budg_idx_growth,
  565. c->freeable_cnt);
  566. printk(KERN_DEBUG "\tmin_idx_lebs %d, old_idx_sz %lld, "
  567. "calc_idx_sz %lld, idx_gc_cnt %d\n", c->min_idx_lebs,
  568. c->old_idx_sz, c->calc_idx_sz, c->idx_gc_cnt);
  569. printk(KERN_DEBUG "\tdirty_pg_cnt %ld, dirty_zn_cnt %ld, "
  570. "clean_zn_cnt %ld\n", atomic_long_read(&c->dirty_pg_cnt),
  571. atomic_long_read(&c->dirty_zn_cnt),
  572. atomic_long_read(&c->clean_zn_cnt));
  573. printk(KERN_DEBUG "\tdark_wm %d, dead_wm %d, max_idx_node_sz %d\n",
  574. c->dark_wm, c->dead_wm, c->max_idx_node_sz);
  575. printk(KERN_DEBUG "\tgc_lnum %d, ihead_lnum %d\n",
  576. c->gc_lnum, c->ihead_lnum);
  577. for (i = 0; i < c->jhead_cnt; i++)
  578. printk(KERN_DEBUG "\tjhead %d\t LEB %d\n",
  579. c->jheads[i].wbuf.jhead, c->jheads[i].wbuf.lnum);
  580. for (rb = rb_first(&c->buds); rb; rb = rb_next(rb)) {
  581. bud = rb_entry(rb, struct ubifs_bud, rb);
  582. printk(KERN_DEBUG "\tbud LEB %d\n", bud->lnum);
  583. }
  584. list_for_each_entry(bud, &c->old_buds, list)
  585. printk(KERN_DEBUG "\told bud LEB %d\n", bud->lnum);
  586. list_for_each_entry(idx_gc, &c->idx_gc, list)
  587. printk(KERN_DEBUG "\tGC'ed idx LEB %d unmap %d\n",
  588. idx_gc->lnum, idx_gc->unmap);
  589. printk(KERN_DEBUG "\tcommit state %d\n", c->cmt_state);
  590. /* Print budgeting predictions */
  591. available = ubifs_calc_available(c, c->min_idx_lebs);
  592. outstanding = c->budg_data_growth + c->budg_dd_growth;
  593. if (available > outstanding)
  594. free = ubifs_reported_space(c, available - outstanding);
  595. else
  596. free = 0;
  597. printk(KERN_DEBUG "Budgeting predictions:\n");
  598. printk(KERN_DEBUG "\tavailable: %lld, outstanding %lld, free %lld\n",
  599. available, outstanding, free);
  600. spin_unlock(&dbg_lock);
  601. }
  602. void dbg_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp)
  603. {
  604. printk(KERN_DEBUG "LEB %d lprops: free %d, dirty %d (used %d), "
  605. "flags %#x\n", lp->lnum, lp->free, lp->dirty,
  606. c->leb_size - lp->free - lp->dirty, lp->flags);
  607. }
  608. void dbg_dump_lprops(struct ubifs_info *c)
  609. {
  610. int lnum, err;
  611. struct ubifs_lprops lp;
  612. struct ubifs_lp_stats lst;
  613. printk(KERN_DEBUG "(pid %d) start dumping LEB properties\n",
  614. current->pid);
  615. ubifs_get_lp_stats(c, &lst);
  616. dbg_dump_lstats(&lst);
  617. for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) {
  618. err = ubifs_read_one_lp(c, lnum, &lp);
  619. if (err)
  620. ubifs_err("cannot read lprops for LEB %d", lnum);
  621. dbg_dump_lprop(c, &lp);
  622. }
  623. printk(KERN_DEBUG "(pid %d) finish dumping LEB properties\n",
  624. current->pid);
  625. }
  626. void dbg_dump_lpt_info(struct ubifs_info *c)
  627. {
  628. int i;
  629. spin_lock(&dbg_lock);
  630. printk(KERN_DEBUG "(pid %d) dumping LPT information\n", current->pid);
  631. printk(KERN_DEBUG "\tlpt_sz: %lld\n", c->lpt_sz);
  632. printk(KERN_DEBUG "\tpnode_sz: %d\n", c->pnode_sz);
  633. printk(KERN_DEBUG "\tnnode_sz: %d\n", c->nnode_sz);
  634. printk(KERN_DEBUG "\tltab_sz: %d\n", c->ltab_sz);
  635. printk(KERN_DEBUG "\tlsave_sz: %d\n", c->lsave_sz);
  636. printk(KERN_DEBUG "\tbig_lpt: %d\n", c->big_lpt);
  637. printk(KERN_DEBUG "\tlpt_hght: %d\n", c->lpt_hght);
  638. printk(KERN_DEBUG "\tpnode_cnt: %d\n", c->pnode_cnt);
  639. printk(KERN_DEBUG "\tnnode_cnt: %d\n", c->nnode_cnt);
  640. printk(KERN_DEBUG "\tdirty_pn_cnt: %d\n", c->dirty_pn_cnt);
  641. printk(KERN_DEBUG "\tdirty_nn_cnt: %d\n", c->dirty_nn_cnt);
  642. printk(KERN_DEBUG "\tlsave_cnt: %d\n", c->lsave_cnt);
  643. printk(KERN_DEBUG "\tspace_bits: %d\n", c->space_bits);
  644. printk(KERN_DEBUG "\tlpt_lnum_bits: %d\n", c->lpt_lnum_bits);
  645. printk(KERN_DEBUG "\tlpt_offs_bits: %d\n", c->lpt_offs_bits);
  646. printk(KERN_DEBUG "\tlpt_spc_bits: %d\n", c->lpt_spc_bits);
  647. printk(KERN_DEBUG "\tpcnt_bits: %d\n", c->pcnt_bits);
  648. printk(KERN_DEBUG "\tlnum_bits: %d\n", c->lnum_bits);
  649. printk(KERN_DEBUG "\tLPT root is at %d:%d\n", c->lpt_lnum, c->lpt_offs);
  650. printk(KERN_DEBUG "\tLPT head is at %d:%d\n",
  651. c->nhead_lnum, c->nhead_offs);
  652. printk(KERN_DEBUG "\tLPT ltab is at %d:%d\n", c->ltab_lnum, c->ltab_offs);
  653. if (c->big_lpt)
  654. printk(KERN_DEBUG "\tLPT lsave is at %d:%d\n",
  655. c->lsave_lnum, c->lsave_offs);
  656. for (i = 0; i < c->lpt_lebs; i++)
  657. printk(KERN_DEBUG "\tLPT LEB %d free %d dirty %d tgc %d "
  658. "cmt %d\n", i + c->lpt_first, c->ltab[i].free,
  659. c->ltab[i].dirty, c->ltab[i].tgc, c->ltab[i].cmt);
  660. spin_unlock(&dbg_lock);
  661. }
  662. void dbg_dump_leb(const struct ubifs_info *c, int lnum)
  663. {
  664. struct ubifs_scan_leb *sleb;
  665. struct ubifs_scan_node *snod;
  666. if (dbg_failure_mode)
  667. return;
  668. printk(KERN_DEBUG "(pid %d) start dumping LEB %d\n",
  669. current->pid, lnum);
  670. sleb = ubifs_scan(c, lnum, 0, c->dbg->buf);
  671. if (IS_ERR(sleb)) {
  672. ubifs_err("scan error %d", (int)PTR_ERR(sleb));
  673. return;
  674. }
  675. printk(KERN_DEBUG "LEB %d has %d nodes ending at %d\n", lnum,
  676. sleb->nodes_cnt, sleb->endpt);
  677. list_for_each_entry(snod, &sleb->nodes, list) {
  678. cond_resched();
  679. printk(KERN_DEBUG "Dumping node at LEB %d:%d len %d\n", lnum,
  680. snod->offs, snod->len);
  681. dbg_dump_node(c, snod->node);
  682. }
  683. printk(KERN_DEBUG "(pid %d) finish dumping LEB %d\n",
  684. current->pid, lnum);
  685. ubifs_scan_destroy(sleb);
  686. return;
  687. }
  688. void dbg_dump_znode(const struct ubifs_info *c,
  689. const struct ubifs_znode *znode)
  690. {
  691. int n;
  692. const struct ubifs_zbranch *zbr;
  693. spin_lock(&dbg_lock);
  694. if (znode->parent)
  695. zbr = &znode->parent->zbranch[znode->iip];
  696. else
  697. zbr = &c->zroot;
  698. printk(KERN_DEBUG "znode %p, LEB %d:%d len %d parent %p iip %d level %d"
  699. " child_cnt %d flags %lx\n", znode, zbr->lnum, zbr->offs,
  700. zbr->len, znode->parent, znode->iip, znode->level,
  701. znode->child_cnt, znode->flags);
  702. if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
  703. spin_unlock(&dbg_lock);
  704. return;
  705. }
  706. printk(KERN_DEBUG "zbranches:\n");
  707. for (n = 0; n < znode->child_cnt; n++) {
  708. zbr = &znode->zbranch[n];
  709. if (znode->level > 0)
  710. printk(KERN_DEBUG "\t%d: znode %p LEB %d:%d len %d key "
  711. "%s\n", n, zbr->znode, zbr->lnum,
  712. zbr->offs, zbr->len,
  713. DBGKEY(&zbr->key));
  714. else
  715. printk(KERN_DEBUG "\t%d: LNC %p LEB %d:%d len %d key "
  716. "%s\n", n, zbr->znode, zbr->lnum,
  717. zbr->offs, zbr->len,
  718. DBGKEY(&zbr->key));
  719. }
  720. spin_unlock(&dbg_lock);
  721. }
  722. void dbg_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat)
  723. {
  724. int i;
  725. printk(KERN_DEBUG "(pid %d) start dumping heap cat %d (%d elements)\n",
  726. current->pid, cat, heap->cnt);
  727. for (i = 0; i < heap->cnt; i++) {
  728. struct ubifs_lprops *lprops = heap->arr[i];
  729. printk(KERN_DEBUG "\t%d. LEB %d hpos %d free %d dirty %d "
  730. "flags %d\n", i, lprops->lnum, lprops->hpos,
  731. lprops->free, lprops->dirty, lprops->flags);
  732. }
  733. printk(KERN_DEBUG "(pid %d) finish dumping heap\n", current->pid);
  734. }
  735. void dbg_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
  736. struct ubifs_nnode *parent, int iip)
  737. {
  738. int i;
  739. printk(KERN_DEBUG "(pid %d) dumping pnode:\n", current->pid);
  740. printk(KERN_DEBUG "\taddress %zx parent %zx cnext %zx\n",
  741. (size_t)pnode, (size_t)parent, (size_t)pnode->cnext);
  742. printk(KERN_DEBUG "\tflags %lu iip %d level %d num %d\n",
  743. pnode->flags, iip, pnode->level, pnode->num);
  744. for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
  745. struct ubifs_lprops *lp = &pnode->lprops[i];
  746. printk(KERN_DEBUG "\t%d: free %d dirty %d flags %d lnum %d\n",
  747. i, lp->free, lp->dirty, lp->flags, lp->lnum);
  748. }
  749. }
  750. void dbg_dump_tnc(struct ubifs_info *c)
  751. {
  752. struct ubifs_znode *znode;
  753. int level;
  754. printk(KERN_DEBUG "\n");
  755. printk(KERN_DEBUG "(pid %d) start dumping TNC tree\n", current->pid);
  756. znode = ubifs_tnc_levelorder_next(c->zroot.znode, NULL);
  757. level = znode->level;
  758. printk(KERN_DEBUG "== Level %d ==\n", level);
  759. while (znode) {
  760. if (level != znode->level) {
  761. level = znode->level;
  762. printk(KERN_DEBUG "== Level %d ==\n", level);
  763. }
  764. dbg_dump_znode(c, znode);
  765. znode = ubifs_tnc_levelorder_next(c->zroot.znode, znode);
  766. }
  767. printk(KERN_DEBUG "(pid %d) finish dumping TNC tree\n", current->pid);
  768. }
  769. static int dump_znode(struct ubifs_info *c, struct ubifs_znode *znode,
  770. void *priv)
  771. {
  772. dbg_dump_znode(c, znode);
  773. return 0;
  774. }
  775. /**
  776. * dbg_dump_index - dump the on-flash index.
  777. * @c: UBIFS file-system description object
  778. *
  779. * This function dumps whole UBIFS indexing B-tree, unlike 'dbg_dump_tnc()'
  780. * which dumps only in-memory znodes and does not read znodes which from flash.
  781. */
  782. void dbg_dump_index(struct ubifs_info *c)
  783. {
  784. dbg_walk_index(c, NULL, dump_znode, NULL);
  785. }
  786. /**
  787. * dbg_check_synced_i_size - check synchronized inode size.
  788. * @inode: inode to check
  789. *
  790. * If inode is clean, synchronized inode size has to be equivalent to current
  791. * inode size. This function has to be called only for locked inodes (@i_mutex
  792. * has to be locked). Returns %0 if synchronized inode size if correct, and
  793. * %-EINVAL if not.
  794. */
  795. int dbg_check_synced_i_size(struct inode *inode)
  796. {
  797. int err = 0;
  798. struct ubifs_inode *ui = ubifs_inode(inode);
  799. if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
  800. return 0;
  801. if (!S_ISREG(inode->i_mode))
  802. return 0;
  803. mutex_lock(&ui->ui_mutex);
  804. spin_lock(&ui->ui_lock);
  805. if (ui->ui_size != ui->synced_i_size && !ui->dirty) {
  806. ubifs_err("ui_size is %lld, synced_i_size is %lld, but inode "
  807. "is clean", ui->ui_size, ui->synced_i_size);
  808. ubifs_err("i_ino %lu, i_mode %#x, i_size %lld", inode->i_ino,
  809. inode->i_mode, i_size_read(inode));
  810. dbg_dump_stack();
  811. err = -EINVAL;
  812. }
  813. spin_unlock(&ui->ui_lock);
  814. mutex_unlock(&ui->ui_mutex);
  815. return err;
  816. }
  817. /*
  818. * dbg_check_dir - check directory inode size and link count.
  819. * @c: UBIFS file-system description object
  820. * @dir: the directory to calculate size for
  821. * @size: the result is returned here
  822. *
  823. * This function makes sure that directory size and link count are correct.
  824. * Returns zero in case of success and a negative error code in case of
  825. * failure.
  826. *
  827. * Note, it is good idea to make sure the @dir->i_mutex is locked before
  828. * calling this function.
  829. */
  830. int dbg_check_dir_size(struct ubifs_info *c, const struct inode *dir)
  831. {
  832. unsigned int nlink = 2;
  833. union ubifs_key key;
  834. struct ubifs_dent_node *dent, *pdent = NULL;
  835. struct qstr nm = { .name = NULL };
  836. loff_t size = UBIFS_INO_NODE_SZ;
  837. if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
  838. return 0;
  839. if (!S_ISDIR(dir->i_mode))
  840. return 0;
  841. lowest_dent_key(c, &key, dir->i_ino);
  842. while (1) {
  843. int err;
  844. dent = ubifs_tnc_next_ent(c, &key, &nm);
  845. if (IS_ERR(dent)) {
  846. err = PTR_ERR(dent);
  847. if (err == -ENOENT)
  848. break;
  849. return err;
  850. }
  851. nm.name = dent->name;
  852. nm.len = le16_to_cpu(dent->nlen);
  853. size += CALC_DENT_SIZE(nm.len);
  854. if (dent->type == UBIFS_ITYPE_DIR)
  855. nlink += 1;
  856. kfree(pdent);
  857. pdent = dent;
  858. key_read(c, &dent->key, &key);
  859. }
  860. kfree(pdent);
  861. if (i_size_read(dir) != size) {
  862. ubifs_err("directory inode %lu has size %llu, "
  863. "but calculated size is %llu", dir->i_ino,
  864. (unsigned long long)i_size_read(dir),
  865. (unsigned long long)size);
  866. dump_stack();
  867. return -EINVAL;
  868. }
  869. if (dir->i_nlink != nlink) {
  870. ubifs_err("directory inode %lu has nlink %u, but calculated "
  871. "nlink is %u", dir->i_ino, dir->i_nlink, nlink);
  872. dump_stack();
  873. return -EINVAL;
  874. }
  875. return 0;
  876. }
  877. /**
  878. * dbg_check_key_order - make sure that colliding keys are properly ordered.
  879. * @c: UBIFS file-system description object
  880. * @zbr1: first zbranch
  881. * @zbr2: following zbranch
  882. *
  883. * In UBIFS indexing B-tree colliding keys has to be sorted in binary order of
  884. * names of the direntries/xentries which are referred by the keys. This
  885. * function reads direntries/xentries referred by @zbr1 and @zbr2 and makes
  886. * sure the name of direntry/xentry referred by @zbr1 is less than
  887. * direntry/xentry referred by @zbr2. Returns zero if this is true, %1 if not,
  888. * and a negative error code in case of failure.
  889. */
  890. static int dbg_check_key_order(struct ubifs_info *c, struct ubifs_zbranch *zbr1,
  891. struct ubifs_zbranch *zbr2)
  892. {
  893. int err, nlen1, nlen2, cmp;
  894. struct ubifs_dent_node *dent1, *dent2;
  895. union ubifs_key key;
  896. ubifs_assert(!keys_cmp(c, &zbr1->key, &zbr2->key));
  897. dent1 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
  898. if (!dent1)
  899. return -ENOMEM;
  900. dent2 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
  901. if (!dent2) {
  902. err = -ENOMEM;
  903. goto out_free;
  904. }
  905. err = ubifs_tnc_read_node(c, zbr1, dent1);
  906. if (err)
  907. goto out_free;
  908. err = ubifs_validate_entry(c, dent1);
  909. if (err)
  910. goto out_free;
  911. err = ubifs_tnc_read_node(c, zbr2, dent2);
  912. if (err)
  913. goto out_free;
  914. err = ubifs_validate_entry(c, dent2);
  915. if (err)
  916. goto out_free;
  917. /* Make sure node keys are the same as in zbranch */
  918. err = 1;
  919. key_read(c, &dent1->key, &key);
  920. if (keys_cmp(c, &zbr1->key, &key)) {
  921. ubifs_err("1st entry at %d:%d has key %s", zbr1->lnum,
  922. zbr1->offs, DBGKEY(&key));
  923. ubifs_err("but it should have key %s according to tnc",
  924. DBGKEY(&zbr1->key));
  925. dbg_dump_node(c, dent1);
  926. goto out_free;
  927. }
  928. key_read(c, &dent2->key, &key);
  929. if (keys_cmp(c, &zbr2->key, &key)) {
  930. ubifs_err("2nd entry at %d:%d has key %s", zbr1->lnum,
  931. zbr1->offs, DBGKEY(&key));
  932. ubifs_err("but it should have key %s according to tnc",
  933. DBGKEY(&zbr2->key));
  934. dbg_dump_node(c, dent2);
  935. goto out_free;
  936. }
  937. nlen1 = le16_to_cpu(dent1->nlen);
  938. nlen2 = le16_to_cpu(dent2->nlen);
  939. cmp = memcmp(dent1->name, dent2->name, min_t(int, nlen1, nlen2));
  940. if (cmp < 0 || (cmp == 0 && nlen1 < nlen2)) {
  941. err = 0;
  942. goto out_free;
  943. }
  944. if (cmp == 0 && nlen1 == nlen2)
  945. ubifs_err("2 xent/dent nodes with the same name");
  946. else
  947. ubifs_err("bad order of colliding key %s",
  948. DBGKEY(&key));
  949. ubifs_msg("first node at %d:%d\n", zbr1->lnum, zbr1->offs);
  950. dbg_dump_node(c, dent1);
  951. ubifs_msg("second node at %d:%d\n", zbr2->lnum, zbr2->offs);
  952. dbg_dump_node(c, dent2);
  953. out_free:
  954. kfree(dent2);
  955. kfree(dent1);
  956. return err;
  957. }
  958. /**
  959. * dbg_check_znode - check if znode is all right.
  960. * @c: UBIFS file-system description object
  961. * @zbr: zbranch which points to this znode
  962. *
  963. * This function makes sure that znode referred to by @zbr is all right.
  964. * Returns zero if it is, and %-EINVAL if it is not.
  965. */
  966. static int dbg_check_znode(struct ubifs_info *c, struct ubifs_zbranch *zbr)
  967. {
  968. struct ubifs_znode *znode = zbr->znode;
  969. struct ubifs_znode *zp = znode->parent;
  970. int n, err, cmp;
  971. if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
  972. err = 1;
  973. goto out;
  974. }
  975. if (znode->level < 0) {
  976. err = 2;
  977. goto out;
  978. }
  979. if (znode->iip < 0 || znode->iip >= c->fanout) {
  980. err = 3;
  981. goto out;
  982. }
  983. if (zbr->len == 0)
  984. /* Only dirty zbranch may have no on-flash nodes */
  985. if (!ubifs_zn_dirty(znode)) {
  986. err = 4;
  987. goto out;
  988. }
  989. if (ubifs_zn_dirty(znode)) {
  990. /*
  991. * If znode is dirty, its parent has to be dirty as well. The
  992. * order of the operation is important, so we have to have
  993. * memory barriers.
  994. */
  995. smp_mb();
  996. if (zp && !ubifs_zn_dirty(zp)) {
  997. /*
  998. * The dirty flag is atomic and is cleared outside the
  999. * TNC mutex, so znode's dirty flag may now have
  1000. * been cleared. The child is always cleared before the
  1001. * parent, so we just need to check again.
  1002. */
  1003. smp_mb();
  1004. if (ubifs_zn_dirty(znode)) {
  1005. err = 5;
  1006. goto out;
  1007. }
  1008. }
  1009. }
  1010. if (zp) {
  1011. const union ubifs_key *min, *max;
  1012. if (znode->level != zp->level - 1) {
  1013. err = 6;
  1014. goto out;
  1015. }
  1016. /* Make sure the 'parent' pointer in our znode is correct */
  1017. err = ubifs_search_zbranch(c, zp, &zbr->key, &n);
  1018. if (!err) {
  1019. /* This zbranch does not exist in the parent */
  1020. err = 7;
  1021. goto out;
  1022. }
  1023. if (znode->iip >= zp->child_cnt) {
  1024. err = 8;
  1025. goto out;
  1026. }
  1027. if (znode->iip != n) {
  1028. /* This may happen only in case of collisions */
  1029. if (keys_cmp(c, &zp->zbranch[n].key,
  1030. &zp->zbranch[znode->iip].key)) {
  1031. err = 9;
  1032. goto out;
  1033. }
  1034. n = znode->iip;
  1035. }
  1036. /*
  1037. * Make sure that the first key in our znode is greater than or
  1038. * equal to the key in the pointing zbranch.
  1039. */
  1040. min = &zbr->key;
  1041. cmp = keys_cmp(c, min, &znode->zbranch[0].key);
  1042. if (cmp == 1) {
  1043. err = 10;
  1044. goto out;
  1045. }
  1046. if (n + 1 < zp->child_cnt) {
  1047. max = &zp->zbranch[n + 1].key;
  1048. /*
  1049. * Make sure the last key in our znode is less or
  1050. * equivalent than the the key in zbranch which goes
  1051. * after our pointing zbranch.
  1052. */
  1053. cmp = keys_cmp(c, max,
  1054. &znode->zbranch[znode->child_cnt - 1].key);
  1055. if (cmp == -1) {
  1056. err = 11;
  1057. goto out;
  1058. }
  1059. }
  1060. } else {
  1061. /* This may only be root znode */
  1062. if (zbr != &c->zroot) {
  1063. err = 12;
  1064. goto out;
  1065. }
  1066. }
  1067. /*
  1068. * Make sure that next key is greater or equivalent then the previous
  1069. * one.
  1070. */
  1071. for (n = 1; n < znode->child_cnt; n++) {
  1072. cmp = keys_cmp(c, &znode->zbranch[n - 1].key,
  1073. &znode->zbranch[n].key);
  1074. if (cmp > 0) {
  1075. err = 13;
  1076. goto out;
  1077. }
  1078. if (cmp == 0) {
  1079. /* This can only be keys with colliding hash */
  1080. if (!is_hash_key(c, &znode->zbranch[n].key)) {
  1081. err = 14;
  1082. goto out;
  1083. }
  1084. if (znode->level != 0 || c->replaying)
  1085. continue;
  1086. /*
  1087. * Colliding keys should follow binary order of
  1088. * corresponding xentry/dentry names.
  1089. */
  1090. err = dbg_check_key_order(c, &znode->zbranch[n - 1],
  1091. &znode->zbranch[n]);
  1092. if (err < 0)
  1093. return err;
  1094. if (err) {
  1095. err = 15;
  1096. goto out;
  1097. }
  1098. }
  1099. }
  1100. for (n = 0; n < znode->child_cnt; n++) {
  1101. if (!znode->zbranch[n].znode &&
  1102. (znode->zbranch[n].lnum == 0 ||
  1103. znode->zbranch[n].len == 0)) {
  1104. err = 16;
  1105. goto out;
  1106. }
  1107. if (znode->zbranch[n].lnum != 0 &&
  1108. znode->zbranch[n].len == 0) {
  1109. err = 17;
  1110. goto out;
  1111. }
  1112. if (znode->zbranch[n].lnum == 0 &&
  1113. znode->zbranch[n].len != 0) {
  1114. err = 18;
  1115. goto out;
  1116. }
  1117. if (znode->zbranch[n].lnum == 0 &&
  1118. znode->zbranch[n].offs != 0) {
  1119. err = 19;
  1120. goto out;
  1121. }
  1122. if (znode->level != 0 && znode->zbranch[n].znode)
  1123. if (znode->zbranch[n].znode->parent != znode) {
  1124. err = 20;
  1125. goto out;
  1126. }
  1127. }
  1128. return 0;
  1129. out:
  1130. ubifs_err("failed, error %d", err);
  1131. ubifs_msg("dump of the znode");
  1132. dbg_dump_znode(c, znode);
  1133. if (zp) {
  1134. ubifs_msg("dump of the parent znode");
  1135. dbg_dump_znode(c, zp);
  1136. }
  1137. dump_stack();
  1138. return -EINVAL;
  1139. }
  1140. /**
  1141. * dbg_check_tnc - check TNC tree.
  1142. * @c: UBIFS file-system description object
  1143. * @extra: do extra checks that are possible at start commit
  1144. *
  1145. * This function traverses whole TNC tree and checks every znode. Returns zero
  1146. * if everything is all right and %-EINVAL if something is wrong with TNC.
  1147. */
  1148. int dbg_check_tnc(struct ubifs_info *c, int extra)
  1149. {
  1150. struct ubifs_znode *znode;
  1151. long clean_cnt = 0, dirty_cnt = 0;
  1152. int err, last;
  1153. if (!(ubifs_chk_flags & UBIFS_CHK_TNC))
  1154. return 0;
  1155. ubifs_assert(mutex_is_locked(&c->tnc_mutex));
  1156. if (!c->zroot.znode)
  1157. return 0;
  1158. znode = ubifs_tnc_postorder_first(c->zroot.znode);
  1159. while (1) {
  1160. struct ubifs_znode *prev;
  1161. struct ubifs_zbranch *zbr;
  1162. if (!znode->parent)
  1163. zbr = &c->zroot;
  1164. else
  1165. zbr = &znode->parent->zbranch[znode->iip];
  1166. err = dbg_check_znode(c, zbr);
  1167. if (err)
  1168. return err;
  1169. if (extra) {
  1170. if (ubifs_zn_dirty(znode))
  1171. dirty_cnt += 1;
  1172. else
  1173. clean_cnt += 1;
  1174. }
  1175. prev = znode;
  1176. znode = ubifs_tnc_postorder_next(znode);
  1177. if (!znode)
  1178. break;
  1179. /*
  1180. * If the last key of this znode is equivalent to the first key
  1181. * of the next znode (collision), then check order of the keys.
  1182. */
  1183. last = prev->child_cnt - 1;
  1184. if (prev->level == 0 && znode->level == 0 && !c->replaying &&
  1185. !keys_cmp(c, &prev->zbranch[last].key,
  1186. &znode->zbranch[0].key)) {
  1187. err = dbg_check_key_order(c, &prev->zbranch[last],
  1188. &znode->zbranch[0]);
  1189. if (err < 0)
  1190. return err;
  1191. if (err) {
  1192. ubifs_msg("first znode");
  1193. dbg_dump_znode(c, prev);
  1194. ubifs_msg("second znode");
  1195. dbg_dump_znode(c, znode);
  1196. return -EINVAL;
  1197. }
  1198. }
  1199. }
  1200. if (extra) {
  1201. if (clean_cnt != atomic_long_read(&c->clean_zn_cnt)) {
  1202. ubifs_err("incorrect clean_zn_cnt %ld, calculated %ld",
  1203. atomic_long_read(&c->clean_zn_cnt),
  1204. clean_cnt);
  1205. return -EINVAL;
  1206. }
  1207. if (dirty_cnt != atomic_long_read(&c->dirty_zn_cnt)) {
  1208. ubifs_err("incorrect dirty_zn_cnt %ld, calculated %ld",
  1209. atomic_long_read(&c->dirty_zn_cnt),
  1210. dirty_cnt);
  1211. return -EINVAL;
  1212. }
  1213. }
  1214. return 0;
  1215. }
  1216. /**
  1217. * dbg_walk_index - walk the on-flash index.
  1218. * @c: UBIFS file-system description object
  1219. * @leaf_cb: called for each leaf node
  1220. * @znode_cb: called for each indexing node
  1221. * @priv: private date which is passed to callbacks
  1222. *
  1223. * This function walks the UBIFS index and calls the @leaf_cb for each leaf
  1224. * node and @znode_cb for each indexing node. Returns zero in case of success
  1225. * and a negative error code in case of failure.
  1226. *
  1227. * It would be better if this function removed every znode it pulled to into
  1228. * the TNC, so that the behavior more closely matched the non-debugging
  1229. * behavior.
  1230. */
  1231. int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
  1232. dbg_znode_callback znode_cb, void *priv)
  1233. {
  1234. int err;
  1235. struct ubifs_zbranch *zbr;
  1236. struct ubifs_znode *znode, *child;
  1237. mutex_lock(&c->tnc_mutex);
  1238. /* If the root indexing node is not in TNC - pull it */
  1239. if (!c->zroot.znode) {
  1240. c->zroot.znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
  1241. if (IS_ERR(c->zroot.znode)) {
  1242. err = PTR_ERR(c->zroot.znode);
  1243. c->zroot.znode = NULL;
  1244. goto out_unlock;
  1245. }
  1246. }
  1247. /*
  1248. * We are going to traverse the indexing tree in the postorder manner.
  1249. * Go down and find the leftmost indexing node where we are going to
  1250. * start from.
  1251. */
  1252. znode = c->zroot.znode;
  1253. while (znode->level > 0) {
  1254. zbr = &znode->zbranch[0];
  1255. child = zbr->znode;
  1256. if (!child) {
  1257. child = ubifs_load_znode(c, zbr, znode, 0);
  1258. if (IS_ERR(child)) {
  1259. err = PTR_ERR(child);
  1260. goto out_unlock;
  1261. }
  1262. zbr->znode = child;
  1263. }
  1264. znode = child;
  1265. }
  1266. /* Iterate over all indexing nodes */
  1267. while (1) {
  1268. int idx;
  1269. cond_resched();
  1270. if (znode_cb) {
  1271. err = znode_cb(c, znode, priv);
  1272. if (err) {
  1273. ubifs_err("znode checking function returned "
  1274. "error %d", err);
  1275. dbg_dump_znode(c, znode);
  1276. goto out_dump;
  1277. }
  1278. }
  1279. if (leaf_cb && znode->level == 0) {
  1280. for (idx = 0; idx < znode->child_cnt; idx++) {
  1281. zbr = &znode->zbranch[idx];
  1282. err = leaf_cb(c, zbr, priv);
  1283. if (err) {
  1284. ubifs_err("leaf checking function "
  1285. "returned error %d, for leaf "
  1286. "at LEB %d:%d",
  1287. err, zbr->lnum, zbr->offs);
  1288. goto out_dump;
  1289. }
  1290. }
  1291. }
  1292. if (!znode->parent)
  1293. break;
  1294. idx = znode->iip + 1;
  1295. znode = znode->parent;
  1296. if (idx < znode->child_cnt) {
  1297. /* Switch to the next index in the parent */
  1298. zbr = &znode->zbranch[idx];
  1299. child = zbr->znode;
  1300. if (!child) {
  1301. child = ubifs_load_znode(c, zbr, znode, idx);
  1302. if (IS_ERR(child)) {
  1303. err = PTR_ERR(child);
  1304. goto out_unlock;
  1305. }
  1306. zbr->znode = child;
  1307. }
  1308. znode = child;
  1309. } else
  1310. /*
  1311. * This is the last child, switch to the parent and
  1312. * continue.
  1313. */
  1314. continue;
  1315. /* Go to the lowest leftmost znode in the new sub-tree */
  1316. while (znode->level > 0) {
  1317. zbr = &znode->zbranch[0];
  1318. child = zbr->znode;
  1319. if (!child) {
  1320. child = ubifs_load_znode(c, zbr, znode, 0);
  1321. if (IS_ERR(child)) {
  1322. err = PTR_ERR(child);
  1323. goto out_unlock;
  1324. }
  1325. zbr->znode = child;
  1326. }
  1327. znode = child;
  1328. }
  1329. }
  1330. mutex_unlock(&c->tnc_mutex);
  1331. return 0;
  1332. out_dump:
  1333. if (znode->parent)
  1334. zbr = &znode->parent->zbranch[znode->iip];
  1335. else
  1336. zbr = &c->zroot;
  1337. ubifs_msg("dump of znode at LEB %d:%d", zbr->lnum, zbr->offs);
  1338. dbg_dump_znode(c, znode);
  1339. out_unlock:
  1340. mutex_unlock(&c->tnc_mutex);
  1341. return err;
  1342. }
  1343. /**
  1344. * add_size - add znode size to partially calculated index size.
  1345. * @c: UBIFS file-system description object
  1346. * @znode: znode to add size for
  1347. * @priv: partially calculated index size
  1348. *
  1349. * This is a helper function for 'dbg_check_idx_size()' which is called for
  1350. * every indexing node and adds its size to the 'long long' variable pointed to
  1351. * by @priv.
  1352. */
  1353. static int add_size(struct ubifs_info *c, struct ubifs_znode *znode, void *priv)
  1354. {
  1355. long long *idx_size = priv;
  1356. int add;
  1357. add = ubifs_idx_node_sz(c, znode->child_cnt);
  1358. add = ALIGN(add, 8);
  1359. *idx_size += add;
  1360. return 0;
  1361. }
  1362. /**
  1363. * dbg_check_idx_size - check index size.
  1364. * @c: UBIFS file-system description object
  1365. * @idx_size: size to check
  1366. *
  1367. * This function walks the UBIFS index, calculates its size and checks that the
  1368. * size is equivalent to @idx_size. Returns zero in case of success and a
  1369. * negative error code in case of failure.
  1370. */
  1371. int dbg_check_idx_size(struct ubifs_info *c, long long idx_size)
  1372. {
  1373. int err;
  1374. long long calc = 0;
  1375. if (!(ubifs_chk_flags & UBIFS_CHK_IDX_SZ))
  1376. return 0;
  1377. err = dbg_walk_index(c, NULL, add_size, &calc);
  1378. if (err) {
  1379. ubifs_err("error %d while walking the index", err);
  1380. return err;
  1381. }
  1382. if (calc != idx_size) {
  1383. ubifs_err("index size check failed: calculated size is %lld, "
  1384. "should be %lld", calc, idx_size);
  1385. dump_stack();
  1386. return -EINVAL;
  1387. }
  1388. return 0;
  1389. }
  1390. /**
  1391. * struct fsck_inode - information about an inode used when checking the file-system.
  1392. * @rb: link in the RB-tree of inodes
  1393. * @inum: inode number
  1394. * @mode: inode type, permissions, etc
  1395. * @nlink: inode link count
  1396. * @xattr_cnt: count of extended attributes
  1397. * @references: how many directory/xattr entries refer this inode (calculated
  1398. * while walking the index)
  1399. * @calc_cnt: for directory inode count of child directories
  1400. * @size: inode size (read from on-flash inode)
  1401. * @xattr_sz: summary size of all extended attributes (read from on-flash
  1402. * inode)
  1403. * @calc_sz: for directories calculated directory size
  1404. * @calc_xcnt: count of extended attributes
  1405. * @calc_xsz: calculated summary size of all extended attributes
  1406. * @xattr_nms: sum of lengths of all extended attribute names belonging to this
  1407. * inode (read from on-flash inode)
  1408. * @calc_xnms: calculated sum of lengths of all extended attribute names
  1409. */
  1410. struct fsck_inode {
  1411. struct rb_node rb;
  1412. ino_t inum;
  1413. umode_t mode;
  1414. unsigned int nlink;
  1415. unsigned int xattr_cnt;
  1416. int references;
  1417. int calc_cnt;
  1418. long long size;
  1419. unsigned int xattr_sz;
  1420. long long calc_sz;
  1421. long long calc_xcnt;
  1422. long long calc_xsz;
  1423. unsigned int xattr_nms;
  1424. long long calc_xnms;
  1425. };
  1426. /**
  1427. * struct fsck_data - private FS checking information.
  1428. * @inodes: RB-tree of all inodes (contains @struct fsck_inode objects)
  1429. */
  1430. struct fsck_data {
  1431. struct rb_root inodes;
  1432. };
  1433. /**
  1434. * add_inode - add inode information to RB-tree of inodes.
  1435. * @c: UBIFS file-system description object
  1436. * @fsckd: FS checking information
  1437. * @ino: raw UBIFS inode to add
  1438. *
  1439. * This is a helper function for 'check_leaf()' which adds information about
  1440. * inode @ino to the RB-tree of inodes. Returns inode information pointer in
  1441. * case of success and a negative error code in case of failure.
  1442. */
  1443. static struct fsck_inode *add_inode(struct ubifs_info *c,
  1444. struct fsck_data *fsckd,
  1445. struct ubifs_ino_node *ino)
  1446. {
  1447. struct rb_node **p, *parent = NULL;
  1448. struct fsck_inode *fscki;
  1449. ino_t inum = key_inum_flash(c, &ino->key);
  1450. p = &fsckd->inodes.rb_node;
  1451. while (*p) {
  1452. parent = *p;
  1453. fscki = rb_entry(parent, struct fsck_inode, rb);
  1454. if (inum < fscki->inum)
  1455. p = &(*p)->rb_left;
  1456. else if (inum > fscki->inum)
  1457. p = &(*p)->rb_right;
  1458. else
  1459. return fscki;
  1460. }
  1461. if (inum > c->highest_inum) {
  1462. ubifs_err("too high inode number, max. is %lu",
  1463. (unsigned long)c->highest_inum);
  1464. return ERR_PTR(-EINVAL);
  1465. }
  1466. fscki = kzalloc(sizeof(struct fsck_inode), GFP_NOFS);
  1467. if (!fscki)
  1468. return ERR_PTR(-ENOMEM);
  1469. fscki->inum = inum;
  1470. fscki->nlink = le32_to_cpu(ino->nlink);
  1471. fscki->size = le64_to_cpu(ino->size);
  1472. fscki->xattr_cnt = le32_to_cpu(ino->xattr_cnt);
  1473. fscki->xattr_sz = le32_to_cpu(ino->xattr_size);
  1474. fscki->xattr_nms = le32_to_cpu(ino->xattr_names);
  1475. fscki->mode = le32_to_cpu(ino->mode);
  1476. if (S_ISDIR(fscki->mode)) {
  1477. fscki->calc_sz = UBIFS_INO_NODE_SZ;
  1478. fscki->calc_cnt = 2;
  1479. }
  1480. rb_link_node(&fscki->rb, parent, p);
  1481. rb_insert_color(&fscki->rb, &fsckd->inodes);
  1482. return fscki;
  1483. }
  1484. /**
  1485. * search_inode - search inode in the RB-tree of inodes.
  1486. * @fsckd: FS checking information
  1487. * @inum: inode number to search
  1488. *
  1489. * This is a helper function for 'check_leaf()' which searches inode @inum in
  1490. * the RB-tree of inodes and returns an inode information pointer or %NULL if
  1491. * the inode was not found.
  1492. */
  1493. static struct fsck_inode *search_inode(struct fsck_data *fsckd, ino_t inum)
  1494. {
  1495. struct rb_node *p;
  1496. struct fsck_inode *fscki;
  1497. p = fsckd->inodes.rb_node;
  1498. while (p) {
  1499. fscki = rb_entry(p, struct fsck_inode, rb);
  1500. if (inum < fscki->inum)
  1501. p = p->rb_left;
  1502. else if (inum > fscki->inum)
  1503. p = p->rb_right;
  1504. else
  1505. return fscki;
  1506. }
  1507. return NULL;
  1508. }
  1509. /**
  1510. * read_add_inode - read inode node and add it to RB-tree of inodes.
  1511. * @c: UBIFS file-system description object
  1512. * @fsckd: FS checking information
  1513. * @inum: inode number to read
  1514. *
  1515. * This is a helper function for 'check_leaf()' which finds inode node @inum in
  1516. * the index, reads it, and adds it to the RB-tree of inodes. Returns inode
  1517. * information pointer in case of success and a negative error code in case of
  1518. * failure.
  1519. */
  1520. static struct fsck_inode *read_add_inode(struct ubifs_info *c,
  1521. struct fsck_data *fsckd, ino_t inum)
  1522. {
  1523. int n, err;
  1524. union ubifs_key key;
  1525. struct ubifs_znode *znode;
  1526. struct ubifs_zbranch *zbr;
  1527. struct ubifs_ino_node *ino;
  1528. struct fsck_inode *fscki;
  1529. fscki = search_inode(fsckd, inum);
  1530. if (fscki)
  1531. return fscki;
  1532. ino_key_init(c, &key, inum);
  1533. err = ubifs_lookup_level0(c, &key, &znode, &n);
  1534. if (!err) {
  1535. ubifs_err("inode %lu not found in index", (unsigned long)inum);
  1536. return ERR_PTR(-ENOENT);
  1537. } else if (err < 0) {
  1538. ubifs_err("error %d while looking up inode %lu",
  1539. err, (unsigned long)inum);
  1540. return ERR_PTR(err);
  1541. }
  1542. zbr = &znode->zbranch[n];
  1543. if (zbr->len < UBIFS_INO_NODE_SZ) {
  1544. ubifs_err("bad node %lu node length %d",
  1545. (unsigned long)inum, zbr->len);
  1546. return ERR_PTR(-EINVAL);
  1547. }
  1548. ino = kmalloc(zbr->len, GFP_NOFS);
  1549. if (!ino)
  1550. return ERR_PTR(-ENOMEM);
  1551. err = ubifs_tnc_read_node(c, zbr, ino);
  1552. if (err) {
  1553. ubifs_err("cannot read inode node at LEB %d:%d, error %d",
  1554. zbr->lnum, zbr->offs, err);
  1555. kfree(ino);
  1556. return ERR_PTR(err);
  1557. }
  1558. fscki = add_inode(c, fsckd, ino);
  1559. kfree(ino);
  1560. if (IS_ERR(fscki)) {
  1561. ubifs_err("error %ld while adding inode %lu node",
  1562. PTR_ERR(fscki), (unsigned long)inum);
  1563. return fscki;
  1564. }
  1565. return fscki;
  1566. }
  1567. /**
  1568. * check_leaf - check leaf node.
  1569. * @c: UBIFS file-system description object
  1570. * @zbr: zbranch of the leaf node to check
  1571. * @priv: FS checking information
  1572. *
  1573. * This is a helper function for 'dbg_check_filesystem()' which is called for
  1574. * every single leaf node while walking the indexing tree. It checks that the
  1575. * leaf node referred from the indexing tree exists, has correct CRC, and does
  1576. * some other basic validation. This function is also responsible for building
  1577. * an RB-tree of inodes - it adds all inodes into the RB-tree. It also
  1578. * calculates reference count, size, etc for each inode in order to later
  1579. * compare them to the information stored inside the inodes and detect possible
  1580. * inconsistencies. Returns zero in case of success and a negative error code
  1581. * in case of failure.
  1582. */
  1583. static int check_leaf(struct ubifs_info *c, struct ubifs_zbranch *zbr,
  1584. void *priv)
  1585. {
  1586. ino_t inum;
  1587. void *node;
  1588. struct ubifs_ch *ch;
  1589. int err, type = key_type(c, &zbr->key);
  1590. struct fsck_inode *fscki;
  1591. if (zbr->len < UBIFS_CH_SZ) {
  1592. ubifs_err("bad leaf length %d (LEB %d:%d)",
  1593. zbr->len, zbr->lnum, zbr->offs);
  1594. return -EINVAL;
  1595. }
  1596. node = kmalloc(zbr->len, GFP_NOFS);
  1597. if (!node)
  1598. return -ENOMEM;
  1599. err = ubifs_tnc_read_node(c, zbr, node);
  1600. if (err) {
  1601. ubifs_err("cannot read leaf node at LEB %d:%d, error %d",
  1602. zbr->lnum, zbr->offs, err);
  1603. goto out_free;
  1604. }
  1605. /* If this is an inode node, add it to RB-tree of inodes */
  1606. if (type == UBIFS_INO_KEY) {
  1607. fscki = add_inode(c, priv, node);
  1608. if (IS_ERR(fscki)) {
  1609. err = PTR_ERR(fscki);
  1610. ubifs_err("error %d while adding inode node", err);
  1611. goto out_dump;
  1612. }
  1613. goto out;
  1614. }
  1615. if (type != UBIFS_DENT_KEY && type != UBIFS_XENT_KEY &&
  1616. type != UBIFS_DATA_KEY) {
  1617. ubifs_err("unexpected node type %d at LEB %d:%d",
  1618. type, zbr->lnum, zbr->offs);
  1619. err = -EINVAL;
  1620. goto out_free;
  1621. }
  1622. ch = node;
  1623. if (le64_to_cpu(ch->sqnum) > c->max_sqnum) {
  1624. ubifs_err("too high sequence number, max. is %llu",
  1625. c->max_sqnum);
  1626. err = -EINVAL;
  1627. goto out_dump;
  1628. }
  1629. if (type == UBIFS_DATA_KEY) {
  1630. long long blk_offs;
  1631. struct ubifs_data_node *dn = node;
  1632. /*
  1633. * Search the inode node this data node belongs to and insert
  1634. * it to the RB-tree of inodes.
  1635. */
  1636. inum = key_inum_flash(c, &dn->key);
  1637. fscki = read_add_inode(c, priv, inum);
  1638. if (IS_ERR(fscki)) {
  1639. err = PTR_ERR(fscki);
  1640. ubifs_err("error %d while processing data node and "
  1641. "trying to find inode node %lu",
  1642. err, (unsigned long)inum);
  1643. goto out_dump;
  1644. }
  1645. /* Make sure the data node is within inode size */
  1646. blk_offs = key_block_flash(c, &dn->key);
  1647. blk_offs <<= UBIFS_BLOCK_SHIFT;
  1648. blk_offs += le32_to_cpu(dn->size);
  1649. if (blk_offs > fscki->size) {
  1650. ubifs_err("data node at LEB %d:%d is not within inode "
  1651. "size %lld", zbr->lnum, zbr->offs,
  1652. fscki->size);
  1653. err = -EINVAL;
  1654. goto out_dump;
  1655. }
  1656. } else {
  1657. int nlen;
  1658. struct ubifs_dent_node *dent = node;
  1659. struct fsck_inode *fscki1;
  1660. err = ubifs_validate_entry(c, dent);
  1661. if (err)
  1662. goto out_dump;
  1663. /*
  1664. * Search the inode node this entry refers to and the parent
  1665. * inode node and insert them to the RB-tree of inodes.
  1666. */
  1667. inum = le64_to_cpu(dent->inum);
  1668. fscki = read_add_inode(c, priv, inum);
  1669. if (IS_ERR(fscki)) {
  1670. err = PTR_ERR(fscki);
  1671. ubifs_err("error %d while processing entry node and "
  1672. "trying to find inode node %lu",
  1673. err, (unsigned long)inum);
  1674. goto out_dump;
  1675. }
  1676. /* Count how many direntries or xentries refers this inode */
  1677. fscki->references += 1;
  1678. inum = key_inum_flash(c, &dent->key);
  1679. fscki1 = read_add_inode(c, priv, inum);
  1680. if (IS_ERR(fscki1)) {
  1681. err = PTR_ERR(fscki);
  1682. ubifs_err("error %d while processing entry node and "
  1683. "trying to find parent inode node %lu",
  1684. err, (unsigned long)inum);
  1685. goto out_dump;
  1686. }
  1687. nlen = le16_to_cpu(dent->nlen);
  1688. if (type == UBIFS_XENT_KEY) {
  1689. fscki1->calc_xcnt += 1;
  1690. fscki1->calc_xsz += CALC_DENT_SIZE(nlen);
  1691. fscki1->calc_xsz += CALC_XATTR_BYTES(fscki->size);
  1692. fscki1->calc_xnms += nlen;
  1693. } else {
  1694. fscki1->calc_sz += CALC_DENT_SIZE(nlen);
  1695. if (dent->type == UBIFS_ITYPE_DIR)
  1696. fscki1->calc_cnt += 1;
  1697. }
  1698. }
  1699. out:
  1700. kfree(node);
  1701. return 0;
  1702. out_dump:
  1703. ubifs_msg("dump of node at LEB %d:%d", zbr->lnum, zbr->offs);
  1704. dbg_dump_node(c, node);
  1705. out_free:
  1706. kfree(node);
  1707. return err;
  1708. }
  1709. /**
  1710. * free_inodes - free RB-tree of inodes.
  1711. * @fsckd: FS checking information
  1712. */
  1713. static void free_inodes(struct fsck_data *fsckd)
  1714. {
  1715. struct rb_node *this = fsckd->inodes.rb_node;
  1716. struct fsck_inode *fscki;
  1717. while (this) {
  1718. if (this->rb_left)
  1719. this = this->rb_left;
  1720. else if (this->rb_right)
  1721. this = this->rb_right;
  1722. else {
  1723. fscki = rb_entry(this, struct fsck_inode, rb);
  1724. this = rb_parent(this);
  1725. if (this) {
  1726. if (this->rb_left == &fscki->rb)
  1727. this->rb_left = NULL;
  1728. else
  1729. this->rb_right = NULL;
  1730. }
  1731. kfree(fscki);
  1732. }
  1733. }
  1734. }
  1735. /**
  1736. * check_inodes - checks all inodes.
  1737. * @c: UBIFS file-system description object
  1738. * @fsckd: FS checking information
  1739. *
  1740. * This is a helper function for 'dbg_check_filesystem()' which walks the
  1741. * RB-tree of inodes after the index scan has been finished, and checks that
  1742. * inode nlink, size, etc are correct. Returns zero if inodes are fine,
  1743. * %-EINVAL if not, and a negative error code in case of failure.
  1744. */
  1745. static int check_inodes(struct ubifs_info *c, struct fsck_data *fsckd)
  1746. {
  1747. int n, err;
  1748. union ubifs_key key;
  1749. struct ubifs_znode *znode;
  1750. struct ubifs_zbranch *zbr;
  1751. struct ubifs_ino_node *ino;
  1752. struct fsck_inode *fscki;
  1753. struct rb_node *this = rb_first(&fsckd->inodes);
  1754. while (this) {
  1755. fscki = rb_entry(this, struct fsck_inode, rb);
  1756. this = rb_next(this);
  1757. if (S_ISDIR(fscki->mode)) {
  1758. /*
  1759. * Directories have to have exactly one reference (they
  1760. * cannot have hardlinks), although root inode is an
  1761. * exception.
  1762. */
  1763. if (fscki->inum != UBIFS_ROOT_INO &&
  1764. fscki->references != 1) {
  1765. ubifs_err("directory inode %lu has %d "
  1766. "direntries which refer it, but "
  1767. "should be 1",
  1768. (unsigned long)fscki->inum,
  1769. fscki->references);
  1770. goto out_dump;
  1771. }
  1772. if (fscki->inum == UBIFS_ROOT_INO &&
  1773. fscki->references != 0) {
  1774. ubifs_err("root inode %lu has non-zero (%d) "
  1775. "direntries which refer it",
  1776. (unsigned long)fscki->inum,
  1777. fscki->references);
  1778. goto out_dump;
  1779. }
  1780. if (fscki->calc_sz != fscki->size) {
  1781. ubifs_err("directory inode %lu size is %lld, "
  1782. "but calculated size is %lld",
  1783. (unsigned long)fscki->inum,
  1784. fscki->size, fscki->calc_sz);
  1785. goto out_dump;
  1786. }
  1787. if (fscki->calc_cnt != fscki->nlink) {
  1788. ubifs_err("directory inode %lu nlink is %d, "
  1789. "but calculated nlink is %d",
  1790. (unsigned long)fscki->inum,
  1791. fscki->nlink, fscki->calc_cnt);
  1792. goto out_dump;
  1793. }
  1794. } else {
  1795. if (fscki->references != fscki->nlink) {
  1796. ubifs_err("inode %lu nlink is %d, but "
  1797. "calculated nlink is %d",
  1798. (unsigned long)fscki->inum,
  1799. fscki->nlink, fscki->references);
  1800. goto out_dump;
  1801. }
  1802. }
  1803. if (fscki->xattr_sz != fscki->calc_xsz) {
  1804. ubifs_err("inode %lu has xattr size %u, but "
  1805. "calculated size is %lld",
  1806. (unsigned long)fscki->inum, fscki->xattr_sz,
  1807. fscki->calc_xsz);
  1808. goto out_dump;
  1809. }
  1810. if (fscki->xattr_cnt != fscki->calc_xcnt) {
  1811. ubifs_err("inode %lu has %u xattrs, but "
  1812. "calculated count is %lld",
  1813. (unsigned long)fscki->inum,
  1814. fscki->xattr_cnt, fscki->calc_xcnt);
  1815. goto out_dump;
  1816. }
  1817. if (fscki->xattr_nms != fscki->calc_xnms) {
  1818. ubifs_err("inode %lu has xattr names' size %u, but "
  1819. "calculated names' size is %lld",
  1820. (unsigned long)fscki->inum, fscki->xattr_nms,
  1821. fscki->calc_xnms);
  1822. goto out_dump;
  1823. }
  1824. }
  1825. return 0;
  1826. out_dump:
  1827. /* Read the bad inode and dump it */
  1828. ino_key_init(c, &key, fscki->inum);
  1829. err = ubifs_lookup_level0(c, &key, &znode, &n);
  1830. if (!err) {
  1831. ubifs_err("inode %lu not found in index",
  1832. (unsigned long)fscki->inum);
  1833. return -ENOENT;
  1834. } else if (err < 0) {
  1835. ubifs_err("error %d while looking up inode %lu",
  1836. err, (unsigned long)fscki->inum);
  1837. return err;
  1838. }
  1839. zbr = &znode->zbranch[n];
  1840. ino = kmalloc(zbr->len, GFP_NOFS);
  1841. if (!ino)
  1842. return -ENOMEM;
  1843. err = ubifs_tnc_read_node(c, zbr, ino);
  1844. if (err) {
  1845. ubifs_err("cannot read inode node at LEB %d:%d, error %d",
  1846. zbr->lnum, zbr->offs, err);
  1847. kfree(ino);
  1848. return err;
  1849. }
  1850. ubifs_msg("dump of the inode %lu sitting in LEB %d:%d",
  1851. (unsigned long)fscki->inum, zbr->lnum, zbr->offs);
  1852. dbg_dump_node(c, ino);
  1853. kfree(ino);
  1854. return -EINVAL;
  1855. }
  1856. /**
  1857. * dbg_check_filesystem - check the file-system.
  1858. * @c: UBIFS file-system description object
  1859. *
  1860. * This function checks the file system, namely:
  1861. * o makes sure that all leaf nodes exist and their CRCs are correct;
  1862. * o makes sure inode nlink, size, xattr size/count are correct (for all
  1863. * inodes).
  1864. *
  1865. * The function reads whole indexing tree and all nodes, so it is pretty
  1866. * heavy-weight. Returns zero if the file-system is consistent, %-EINVAL if
  1867. * not, and a negative error code in case of failure.
  1868. */
  1869. int dbg_check_filesystem(struct ubifs_info *c)
  1870. {
  1871. int err;
  1872. struct fsck_data fsckd;
  1873. if (!(ubifs_chk_flags & UBIFS_CHK_FS))
  1874. return 0;
  1875. fsckd.inodes = RB_ROOT;
  1876. err = dbg_walk_index(c, check_leaf, NULL, &fsckd);
  1877. if (err)
  1878. goto out_free;
  1879. err = check_inodes(c, &fsckd);
  1880. if (err)
  1881. goto out_free;
  1882. free_inodes(&fsckd);
  1883. return 0;
  1884. out_free:
  1885. ubifs_err("file-system check failed with error %d", err);
  1886. dump_stack();
  1887. free_inodes(&fsckd);
  1888. return err;
  1889. }
  1890. static int invocation_cnt;
  1891. int dbg_force_in_the_gaps(void)
  1892. {
  1893. if (!dbg_force_in_the_gaps_enabled)
  1894. return 0;
  1895. /* Force in-the-gaps every 8th commit */
  1896. return !((invocation_cnt++) & 0x7);
  1897. }
  1898. /* Failure mode for recovery testing */
  1899. #define chance(n, d) (simple_rand() <= (n) * 32768LL / (d))
  1900. struct failure_mode_info {
  1901. struct list_head list;
  1902. struct ubifs_info *c;
  1903. };
  1904. static LIST_HEAD(fmi_list);
  1905. static DEFINE_SPINLOCK(fmi_lock);
  1906. static unsigned int next;
  1907. static int simple_rand(void)
  1908. {
  1909. if (next == 0)
  1910. next = current->pid;
  1911. next = next * 1103515245 + 12345;
  1912. return (next >> 16) & 32767;
  1913. }
  1914. static void failure_mode_init(struct ubifs_info *c)
  1915. {
  1916. struct failure_mode_info *fmi;
  1917. fmi = kmalloc(sizeof(struct failure_mode_info), GFP_NOFS);
  1918. if (!fmi) {
  1919. ubifs_err("Failed to register failure mode - no memory");
  1920. return;
  1921. }
  1922. fmi->c = c;
  1923. spin_lock(&fmi_lock);
  1924. list_add_tail(&fmi->list, &fmi_list);
  1925. spin_unlock(&fmi_lock);
  1926. }
  1927. static void failure_mode_exit(struct ubifs_info *c)
  1928. {
  1929. struct failure_mode_info *fmi, *tmp;
  1930. spin_lock(&fmi_lock);
  1931. list_for_each_entry_safe(fmi, tmp, &fmi_list, list)
  1932. if (fmi->c == c) {
  1933. list_del(&fmi->list);
  1934. kfree(fmi);
  1935. }
  1936. spin_unlock(&fmi_lock);
  1937. }
  1938. static struct ubifs_info *dbg_find_info(struct ubi_volume_desc *desc)
  1939. {
  1940. struct failure_mode_info *fmi;
  1941. spin_lock(&fmi_lock);
  1942. list_for_each_entry(fmi, &fmi_list, list)
  1943. if (fmi->c->ubi == desc) {
  1944. struct ubifs_info *c = fmi->c;
  1945. spin_unlock(&fmi_lock);
  1946. return c;
  1947. }
  1948. spin_unlock(&fmi_lock);
  1949. return NULL;
  1950. }
  1951. static int in_failure_mode(struct ubi_volume_desc *desc)
  1952. {
  1953. struct ubifs_info *c = dbg_find_info(desc);
  1954. if (c && dbg_failure_mode)
  1955. return c->dbg->failure_mode;
  1956. return 0;
  1957. }
  1958. static int do_fail(struct ubi_volume_desc *desc, int lnum, int write)
  1959. {
  1960. struct ubifs_info *c = dbg_find_info(desc);
  1961. struct ubifs_debug_info *d;
  1962. if (!c || !dbg_failure_mode)
  1963. return 0;
  1964. d = c->dbg;
  1965. if (d->failure_mode)
  1966. return 1;
  1967. if (!d->fail_cnt) {
  1968. /* First call - decide delay to failure */
  1969. if (chance(1, 2)) {
  1970. unsigned int delay = 1 << (simple_rand() >> 11);
  1971. if (chance(1, 2)) {
  1972. d->fail_delay = 1;
  1973. d->fail_timeout = jiffies +
  1974. msecs_to_jiffies(delay);
  1975. dbg_rcvry("failing after %ums", delay);
  1976. } else {
  1977. d->fail_delay = 2;
  1978. d->fail_cnt_max = delay;
  1979. dbg_rcvry("failing after %u calls", delay);
  1980. }
  1981. }
  1982. d->fail_cnt += 1;
  1983. }
  1984. /* Determine if failure delay has expired */
  1985. if (d->fail_delay == 1) {
  1986. if (time_before(jiffies, d->fail_timeout))
  1987. return 0;
  1988. } else if (d->fail_delay == 2)
  1989. if (d->fail_cnt++ < d->fail_cnt_max)
  1990. return 0;
  1991. if (lnum == UBIFS_SB_LNUM) {
  1992. if (write) {
  1993. if (chance(1, 2))
  1994. return 0;
  1995. } else if (chance(19, 20))
  1996. return 0;
  1997. dbg_rcvry("failing in super block LEB %d", lnum);
  1998. } else if (lnum == UBIFS_MST_LNUM || lnum == UBIFS_MST_LNUM + 1) {
  1999. if (chance(19, 20))
  2000. return 0;
  2001. dbg_rcvry("failing in master LEB %d", lnum);
  2002. } else if (lnum >= UBIFS_LOG_LNUM && lnum <= c->log_last) {
  2003. if (write) {
  2004. if (chance(99, 100))
  2005. return 0;
  2006. } else if (chance(399, 400))
  2007. return 0;
  2008. dbg_rcvry("failing in log LEB %d", lnum);
  2009. } else if (lnum >= c->lpt_first && lnum <= c->lpt_last) {
  2010. if (write) {
  2011. if (chance(7, 8))
  2012. return 0;
  2013. } else if (chance(19, 20))
  2014. return 0;
  2015. dbg_rcvry("failing in LPT LEB %d", lnum);
  2016. } else if (lnum >= c->orph_first && lnum <= c->orph_last) {
  2017. if (write) {
  2018. if (chance(1, 2))
  2019. return 0;
  2020. } else if (chance(9, 10))
  2021. return 0;
  2022. dbg_rcvry("failing in orphan LEB %d", lnum);
  2023. } else if (lnum == c->ihead_lnum) {
  2024. if (chance(99, 100))
  2025. return 0;
  2026. dbg_rcvry("failing in index head LEB %d", lnum);
  2027. } else if (c->jheads && lnum == c->jheads[GCHD].wbuf.lnum) {
  2028. if (chance(9, 10))
  2029. return 0;
  2030. dbg_rcvry("failing in GC head LEB %d", lnum);
  2031. } else if (write && !RB_EMPTY_ROOT(&c->buds) &&
  2032. !ubifs_search_bud(c, lnum)) {
  2033. if (chance(19, 20))
  2034. return 0;
  2035. dbg_rcvry("failing in non-bud LEB %d", lnum);
  2036. } else if (c->cmt_state == COMMIT_RUNNING_BACKGROUND ||
  2037. c->cmt_state == COMMIT_RUNNING_REQUIRED) {
  2038. if (chance(999, 1000))
  2039. return 0;
  2040. dbg_rcvry("failing in bud LEB %d commit running", lnum);
  2041. } else {
  2042. if (chance(9999, 10000))
  2043. return 0;
  2044. dbg_rcvry("failing in bud LEB %d commit not running", lnum);
  2045. }
  2046. ubifs_err("*** SETTING FAILURE MODE ON (LEB %d) ***", lnum);
  2047. d->failure_mode = 1;
  2048. dump_stack();
  2049. return 1;
  2050. }
  2051. static void cut_data(const void *buf, int len)
  2052. {
  2053. int flen, i;
  2054. unsigned char *p = (void *)buf;
  2055. flen = (len * (long long)simple_rand()) >> 15;
  2056. for (i = flen; i < len; i++)
  2057. p[i] = 0xff;
  2058. }
  2059. int dbg_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
  2060. int len, int check)
  2061. {
  2062. if (in_failure_mode(desc))
  2063. return -EIO;
  2064. return ubi_leb_read(desc, lnum, buf, offset, len, check);
  2065. }
  2066. int dbg_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
  2067. int offset, int len, int dtype)
  2068. {
  2069. int err, failing;
  2070. if (in_failure_mode(desc))
  2071. return -EIO;
  2072. failing = do_fail(desc, lnum, 1);
  2073. if (failing)
  2074. cut_data(buf, len);
  2075. err = ubi_leb_write(desc, lnum, buf, offset, len, dtype);
  2076. if (err)
  2077. return err;
  2078. if (failing)
  2079. return -EIO;
  2080. return 0;
  2081. }
  2082. int dbg_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
  2083. int len, int dtype)
  2084. {
  2085. int err;
  2086. if (do_fail(desc, lnum, 1))
  2087. return -EIO;
  2088. err = ubi_leb_change(desc, lnum, buf, len, dtype);
  2089. if (err)
  2090. return err;
  2091. if (do_fail(desc, lnum, 1))
  2092. return -EIO;
  2093. return 0;
  2094. }
  2095. int dbg_leb_erase(struct ubi_volume_desc *desc, int lnum)
  2096. {
  2097. int err;
  2098. if (do_fail(desc, lnum, 0))
  2099. return -EIO;
  2100. err = ubi_leb_erase(desc, lnum);
  2101. if (err)
  2102. return err;
  2103. if (do_fail(desc, lnum, 0))
  2104. return -EIO;
  2105. return 0;
  2106. }
  2107. int dbg_leb_unmap(struct ubi_volume_desc *desc, int lnum)
  2108. {
  2109. int err;
  2110. if (do_fail(desc, lnum, 0))
  2111. return -EIO;
  2112. err = ubi_leb_unmap(desc, lnum);
  2113. if (err)
  2114. return err;
  2115. if (do_fail(desc, lnum, 0))
  2116. return -EIO;
  2117. return 0;
  2118. }
  2119. int dbg_is_mapped(struct ubi_volume_desc *desc, int lnum)
  2120. {
  2121. if (in_failure_mode(desc))
  2122. return -EIO;
  2123. return ubi_is_mapped(desc, lnum);
  2124. }
  2125. int dbg_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype)
  2126. {
  2127. int err;
  2128. if (do_fail(desc, lnum, 0))
  2129. return -EIO;
  2130. err = ubi_leb_map(desc, lnum, dtype);
  2131. if (err)
  2132. return err;
  2133. if (do_fail(desc, lnum, 0))
  2134. return -EIO;
  2135. return 0;
  2136. }
  2137. /**
  2138. * ubifs_debugging_init - initialize UBIFS debugging.
  2139. * @c: UBIFS file-system description object
  2140. *
  2141. * This function initializes debugging-related data for the file system.
  2142. * Returns zero in case of success and a negative error code in case of
  2143. * failure.
  2144. */
  2145. int ubifs_debugging_init(struct ubifs_info *c)
  2146. {
  2147. c->dbg = kzalloc(sizeof(struct ubifs_debug_info), GFP_KERNEL);
  2148. if (!c->dbg)
  2149. return -ENOMEM;
  2150. c->dbg->buf = vmalloc(c->leb_size);
  2151. if (!c->dbg->buf)
  2152. goto out;
  2153. failure_mode_init(c);
  2154. return 0;
  2155. out:
  2156. kfree(c->dbg);
  2157. return -ENOMEM;
  2158. }
  2159. /**
  2160. * ubifs_debugging_exit - free debugging data.
  2161. * @c: UBIFS file-system description object
  2162. */
  2163. void ubifs_debugging_exit(struct ubifs_info *c)
  2164. {
  2165. failure_mode_exit(c);
  2166. vfree(c->dbg->buf);
  2167. kfree(c->dbg);
  2168. }
  2169. /*
  2170. * Root directory for UBIFS stuff in debugfs. Contains sub-directories which
  2171. * contain the stuff specific to particular file-system mounts.
  2172. */
  2173. static struct dentry *debugfs_rootdir;
  2174. /**
  2175. * dbg_debugfs_init - initialize debugfs file-system.
  2176. *
  2177. * UBIFS uses debugfs file-system to expose various debugging knobs to
  2178. * user-space. This function creates "ubifs" directory in the debugfs
  2179. * file-system. Returns zero in case of success and a negative error code in
  2180. * case of failure.
  2181. */
  2182. int dbg_debugfs_init(void)
  2183. {
  2184. debugfs_rootdir = debugfs_create_dir("ubifs", NULL);
  2185. if (IS_ERR(debugfs_rootdir)) {
  2186. int err = PTR_ERR(debugfs_rootdir);
  2187. ubifs_err("cannot create \"ubifs\" debugfs directory, "
  2188. "error %d\n", err);
  2189. return err;
  2190. }
  2191. return 0;
  2192. }
  2193. /**
  2194. * dbg_debugfs_exit - remove the "ubifs" directory from debugfs file-system.
  2195. */
  2196. void dbg_debugfs_exit(void)
  2197. {
  2198. debugfs_remove(debugfs_rootdir);
  2199. }
  2200. static int open_debugfs_file(struct inode *inode, struct file *file)
  2201. {
  2202. file->private_data = inode->i_private;
  2203. return 0;
  2204. }
  2205. static ssize_t write_debugfs_file(struct file *file, const char __user *buf,
  2206. size_t count, loff_t *ppos)
  2207. {
  2208. struct ubifs_info *c = file->private_data;
  2209. struct ubifs_debug_info *d = c->dbg;
  2210. if (file->f_path.dentry == d->dump_lprops)
  2211. dbg_dump_lprops(c);
  2212. else if (file->f_path.dentry == d->dump_budg) {
  2213. spin_lock(&c->space_lock);
  2214. dbg_dump_budg(c);
  2215. spin_unlock(&c->space_lock);
  2216. } else if (file->f_path.dentry == d->dump_tnc) {
  2217. mutex_lock(&c->tnc_mutex);
  2218. dbg_dump_tnc(c);
  2219. mutex_unlock(&c->tnc_mutex);
  2220. } else
  2221. return -EINVAL;
  2222. *ppos += count;
  2223. return count;
  2224. }
  2225. static const struct file_operations debugfs_fops = {
  2226. .open = open_debugfs_file,
  2227. .write = write_debugfs_file,
  2228. .owner = THIS_MODULE,
  2229. };
  2230. /**
  2231. * dbg_debugfs_init_fs - initialize debugfs for UBIFS instance.
  2232. * @c: UBIFS file-system description object
  2233. *
  2234. * This function creates all debugfs files for this instance of UBIFS. Returns
  2235. * zero in case of success and a negative error code in case of failure.
  2236. *
  2237. * Note, the only reason we have not merged this function with the
  2238. * 'ubifs_debugging_init()' function is because it is better to initialize
  2239. * debugfs interfaces at the very end of the mount process, and remove them at
  2240. * the very beginning of the mount process.
  2241. */
  2242. int dbg_debugfs_init_fs(struct ubifs_info *c)
  2243. {
  2244. int err;
  2245. const char *fname;
  2246. struct dentry *dent;
  2247. struct ubifs_debug_info *d = c->dbg;
  2248. sprintf(d->debugfs_dir_name, "ubi%d_%d", c->vi.ubi_num, c->vi.vol_id);
  2249. d->debugfs_dir = debugfs_create_dir(d->debugfs_dir_name,
  2250. debugfs_rootdir);
  2251. if (IS_ERR(d->debugfs_dir)) {
  2252. err = PTR_ERR(d->debugfs_dir);
  2253. ubifs_err("cannot create \"%s\" debugfs directory, error %d\n",
  2254. d->debugfs_dir_name, err);
  2255. goto out;
  2256. }
  2257. fname = "dump_lprops";
  2258. dent = debugfs_create_file(fname, S_IWUGO, d->debugfs_dir, c,
  2259. &debugfs_fops);
  2260. if (IS_ERR(dent))
  2261. goto out_remove;
  2262. d->dump_lprops = dent;
  2263. fname = "dump_budg";
  2264. dent = debugfs_create_file(fname, S_IWUGO, d->debugfs_dir, c,
  2265. &debugfs_fops);
  2266. if (IS_ERR(dent))
  2267. goto out_remove;
  2268. d->dump_budg = dent;
  2269. fname = "dump_tnc";
  2270. dent = debugfs_create_file(fname, S_IWUGO, d->debugfs_dir, c,
  2271. &debugfs_fops);
  2272. if (IS_ERR(dent))
  2273. goto out_remove;
  2274. d->dump_tnc = dent;
  2275. return 0;
  2276. out_remove:
  2277. err = PTR_ERR(dent);
  2278. ubifs_err("cannot create \"%s\" debugfs directory, error %d\n",
  2279. fname, err);
  2280. debugfs_remove_recursive(d->debugfs_dir);
  2281. out:
  2282. return err;
  2283. }
  2284. /**
  2285. * dbg_debugfs_exit_fs - remove all debugfs files.
  2286. * @c: UBIFS file-system description object
  2287. */
  2288. void dbg_debugfs_exit_fs(struct ubifs_info *c)
  2289. {
  2290. debugfs_remove_recursive(c->dbg->debugfs_dir);
  2291. }
  2292. #endif /* CONFIG_UBIFS_FS_DEBUG */