debug.c 76 KB

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