debug.c 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696
  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 = UBIFS_MSG_FLAGS_DEFAULT;
  40. unsigned int ubifs_chk_flags = UBIFS_CHK_FLAGS_DEFAULT;
  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. if (dbg_failure_mode)
  754. return;
  755. printk(KERN_DEBUG "(pid %d) start dumping LEB %d\n",
  756. current->pid, lnum);
  757. sleb = ubifs_scan(c, lnum, 0, c->dbg->buf, 0);
  758. if (IS_ERR(sleb)) {
  759. ubifs_err("scan error %d", (int)PTR_ERR(sleb));
  760. return;
  761. }
  762. printk(KERN_DEBUG "LEB %d has %d nodes ending at %d\n", lnum,
  763. sleb->nodes_cnt, sleb->endpt);
  764. list_for_each_entry(snod, &sleb->nodes, list) {
  765. cond_resched();
  766. printk(KERN_DEBUG "Dumping node at LEB %d:%d len %d\n", lnum,
  767. snod->offs, snod->len);
  768. dbg_dump_node(c, snod->node);
  769. }
  770. printk(KERN_DEBUG "(pid %d) finish dumping LEB %d\n",
  771. current->pid, lnum);
  772. ubifs_scan_destroy(sleb);
  773. return;
  774. }
  775. void dbg_dump_znode(const struct ubifs_info *c,
  776. const struct ubifs_znode *znode)
  777. {
  778. int n;
  779. const struct ubifs_zbranch *zbr;
  780. spin_lock(&dbg_lock);
  781. if (znode->parent)
  782. zbr = &znode->parent->zbranch[znode->iip];
  783. else
  784. zbr = &c->zroot;
  785. printk(KERN_DEBUG "znode %p, LEB %d:%d len %d parent %p iip %d level %d"
  786. " child_cnt %d flags %lx\n", znode, zbr->lnum, zbr->offs,
  787. zbr->len, znode->parent, znode->iip, znode->level,
  788. znode->child_cnt, znode->flags);
  789. if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
  790. spin_unlock(&dbg_lock);
  791. return;
  792. }
  793. printk(KERN_DEBUG "zbranches:\n");
  794. for (n = 0; n < znode->child_cnt; n++) {
  795. zbr = &znode->zbranch[n];
  796. if (znode->level > 0)
  797. printk(KERN_DEBUG "\t%d: znode %p LEB %d:%d len %d key "
  798. "%s\n", n, zbr->znode, zbr->lnum,
  799. zbr->offs, zbr->len,
  800. DBGKEY(&zbr->key));
  801. else
  802. printk(KERN_DEBUG "\t%d: LNC %p LEB %d:%d len %d key "
  803. "%s\n", n, zbr->znode, zbr->lnum,
  804. zbr->offs, zbr->len,
  805. DBGKEY(&zbr->key));
  806. }
  807. spin_unlock(&dbg_lock);
  808. }
  809. void dbg_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat)
  810. {
  811. int i;
  812. printk(KERN_DEBUG "(pid %d) start dumping heap cat %d (%d elements)\n",
  813. current->pid, cat, heap->cnt);
  814. for (i = 0; i < heap->cnt; i++) {
  815. struct ubifs_lprops *lprops = heap->arr[i];
  816. printk(KERN_DEBUG "\t%d. LEB %d hpos %d free %d dirty %d "
  817. "flags %d\n", i, lprops->lnum, lprops->hpos,
  818. lprops->free, lprops->dirty, lprops->flags);
  819. }
  820. printk(KERN_DEBUG "(pid %d) finish dumping heap\n", current->pid);
  821. }
  822. void dbg_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
  823. struct ubifs_nnode *parent, int iip)
  824. {
  825. int i;
  826. printk(KERN_DEBUG "(pid %d) dumping pnode:\n", current->pid);
  827. printk(KERN_DEBUG "\taddress %zx parent %zx cnext %zx\n",
  828. (size_t)pnode, (size_t)parent, (size_t)pnode->cnext);
  829. printk(KERN_DEBUG "\tflags %lu iip %d level %d num %d\n",
  830. pnode->flags, iip, pnode->level, pnode->num);
  831. for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
  832. struct ubifs_lprops *lp = &pnode->lprops[i];
  833. printk(KERN_DEBUG "\t%d: free %d dirty %d flags %d lnum %d\n",
  834. i, lp->free, lp->dirty, lp->flags, lp->lnum);
  835. }
  836. }
  837. void dbg_dump_tnc(struct ubifs_info *c)
  838. {
  839. struct ubifs_znode *znode;
  840. int level;
  841. printk(KERN_DEBUG "\n");
  842. printk(KERN_DEBUG "(pid %d) start dumping TNC tree\n", current->pid);
  843. znode = ubifs_tnc_levelorder_next(c->zroot.znode, NULL);
  844. level = znode->level;
  845. printk(KERN_DEBUG "== Level %d ==\n", level);
  846. while (znode) {
  847. if (level != znode->level) {
  848. level = znode->level;
  849. printk(KERN_DEBUG "== Level %d ==\n", level);
  850. }
  851. dbg_dump_znode(c, znode);
  852. znode = ubifs_tnc_levelorder_next(c->zroot.znode, znode);
  853. }
  854. printk(KERN_DEBUG "(pid %d) finish dumping TNC tree\n", current->pid);
  855. }
  856. static int dump_znode(struct ubifs_info *c, struct ubifs_znode *znode,
  857. void *priv)
  858. {
  859. dbg_dump_znode(c, znode);
  860. return 0;
  861. }
  862. /**
  863. * dbg_dump_index - dump the on-flash index.
  864. * @c: UBIFS file-system description object
  865. *
  866. * This function dumps whole UBIFS indexing B-tree, unlike 'dbg_dump_tnc()'
  867. * which dumps only in-memory znodes and does not read znodes which from flash.
  868. */
  869. void dbg_dump_index(struct ubifs_info *c)
  870. {
  871. dbg_walk_index(c, NULL, dump_znode, NULL);
  872. }
  873. /**
  874. * dbg_save_space_info - save information about flash space.
  875. * @c: UBIFS file-system description object
  876. *
  877. * This function saves information about UBIFS free space, dirty space, etc, in
  878. * order to check it later.
  879. */
  880. void dbg_save_space_info(struct ubifs_info *c)
  881. {
  882. struct ubifs_debug_info *d = c->dbg;
  883. ubifs_get_lp_stats(c, &d->saved_lst);
  884. spin_lock(&c->space_lock);
  885. d->saved_free = ubifs_get_free_space_nolock(c);
  886. spin_unlock(&c->space_lock);
  887. }
  888. /**
  889. * dbg_check_space_info - check flash space information.
  890. * @c: UBIFS file-system description object
  891. *
  892. * This function compares current flash space information with the information
  893. * which was saved when the 'dbg_save_space_info()' function was called.
  894. * Returns zero if the information has not changed, and %-EINVAL it it has
  895. * changed.
  896. */
  897. int dbg_check_space_info(struct ubifs_info *c)
  898. {
  899. struct ubifs_debug_info *d = c->dbg;
  900. struct ubifs_lp_stats lst;
  901. long long avail, free;
  902. spin_lock(&c->space_lock);
  903. avail = ubifs_calc_available(c, c->min_idx_lebs);
  904. spin_unlock(&c->space_lock);
  905. free = ubifs_get_free_space(c);
  906. if (free != d->saved_free) {
  907. ubifs_err("free space changed from %lld to %lld",
  908. d->saved_free, free);
  909. goto out;
  910. }
  911. return 0;
  912. out:
  913. ubifs_msg("saved lprops statistics dump");
  914. dbg_dump_lstats(&d->saved_lst);
  915. ubifs_get_lp_stats(c, &lst);
  916. ubifs_msg("current lprops statistics dump");
  917. dbg_dump_lstats(&lst);
  918. spin_lock(&c->space_lock);
  919. dbg_dump_budg(c);
  920. spin_unlock(&c->space_lock);
  921. dump_stack();
  922. return -EINVAL;
  923. }
  924. /**
  925. * dbg_check_synced_i_size - check synchronized inode size.
  926. * @inode: inode to check
  927. *
  928. * If inode is clean, synchronized inode size has to be equivalent to current
  929. * inode size. This function has to be called only for locked inodes (@i_mutex
  930. * has to be locked). Returns %0 if synchronized inode size if correct, and
  931. * %-EINVAL if not.
  932. */
  933. int dbg_check_synced_i_size(struct inode *inode)
  934. {
  935. int err = 0;
  936. struct ubifs_inode *ui = ubifs_inode(inode);
  937. if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
  938. return 0;
  939. if (!S_ISREG(inode->i_mode))
  940. return 0;
  941. mutex_lock(&ui->ui_mutex);
  942. spin_lock(&ui->ui_lock);
  943. if (ui->ui_size != ui->synced_i_size && !ui->dirty) {
  944. ubifs_err("ui_size is %lld, synced_i_size is %lld, but inode "
  945. "is clean", ui->ui_size, ui->synced_i_size);
  946. ubifs_err("i_ino %lu, i_mode %#x, i_size %lld", inode->i_ino,
  947. inode->i_mode, i_size_read(inode));
  948. dbg_dump_stack();
  949. err = -EINVAL;
  950. }
  951. spin_unlock(&ui->ui_lock);
  952. mutex_unlock(&ui->ui_mutex);
  953. return err;
  954. }
  955. /*
  956. * dbg_check_dir - check directory inode size and link count.
  957. * @c: UBIFS file-system description object
  958. * @dir: the directory to calculate size for
  959. * @size: the result is returned here
  960. *
  961. * This function makes sure that directory size and link count are correct.
  962. * Returns zero in case of success and a negative error code in case of
  963. * failure.
  964. *
  965. * Note, it is good idea to make sure the @dir->i_mutex is locked before
  966. * calling this function.
  967. */
  968. int dbg_check_dir_size(struct ubifs_info *c, const struct inode *dir)
  969. {
  970. unsigned int nlink = 2;
  971. union ubifs_key key;
  972. struct ubifs_dent_node *dent, *pdent = NULL;
  973. struct qstr nm = { .name = NULL };
  974. loff_t size = UBIFS_INO_NODE_SZ;
  975. if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
  976. return 0;
  977. if (!S_ISDIR(dir->i_mode))
  978. return 0;
  979. lowest_dent_key(c, &key, dir->i_ino);
  980. while (1) {
  981. int err;
  982. dent = ubifs_tnc_next_ent(c, &key, &nm);
  983. if (IS_ERR(dent)) {
  984. err = PTR_ERR(dent);
  985. if (err == -ENOENT)
  986. break;
  987. return err;
  988. }
  989. nm.name = dent->name;
  990. nm.len = le16_to_cpu(dent->nlen);
  991. size += CALC_DENT_SIZE(nm.len);
  992. if (dent->type == UBIFS_ITYPE_DIR)
  993. nlink += 1;
  994. kfree(pdent);
  995. pdent = dent;
  996. key_read(c, &dent->key, &key);
  997. }
  998. kfree(pdent);
  999. if (i_size_read(dir) != size) {
  1000. ubifs_err("directory inode %lu has size %llu, "
  1001. "but calculated size is %llu", dir->i_ino,
  1002. (unsigned long long)i_size_read(dir),
  1003. (unsigned long long)size);
  1004. dump_stack();
  1005. return -EINVAL;
  1006. }
  1007. if (dir->i_nlink != nlink) {
  1008. ubifs_err("directory inode %lu has nlink %u, but calculated "
  1009. "nlink is %u", dir->i_ino, dir->i_nlink, nlink);
  1010. dump_stack();
  1011. return -EINVAL;
  1012. }
  1013. return 0;
  1014. }
  1015. /**
  1016. * dbg_check_key_order - make sure that colliding keys are properly ordered.
  1017. * @c: UBIFS file-system description object
  1018. * @zbr1: first zbranch
  1019. * @zbr2: following zbranch
  1020. *
  1021. * In UBIFS indexing B-tree colliding keys has to be sorted in binary order of
  1022. * names of the direntries/xentries which are referred by the keys. This
  1023. * function reads direntries/xentries referred by @zbr1 and @zbr2 and makes
  1024. * sure the name of direntry/xentry referred by @zbr1 is less than
  1025. * direntry/xentry referred by @zbr2. Returns zero if this is true, %1 if not,
  1026. * and a negative error code in case of failure.
  1027. */
  1028. static int dbg_check_key_order(struct ubifs_info *c, struct ubifs_zbranch *zbr1,
  1029. struct ubifs_zbranch *zbr2)
  1030. {
  1031. int err, nlen1, nlen2, cmp;
  1032. struct ubifs_dent_node *dent1, *dent2;
  1033. union ubifs_key key;
  1034. ubifs_assert(!keys_cmp(c, &zbr1->key, &zbr2->key));
  1035. dent1 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
  1036. if (!dent1)
  1037. return -ENOMEM;
  1038. dent2 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
  1039. if (!dent2) {
  1040. err = -ENOMEM;
  1041. goto out_free;
  1042. }
  1043. err = ubifs_tnc_read_node(c, zbr1, dent1);
  1044. if (err)
  1045. goto out_free;
  1046. err = ubifs_validate_entry(c, dent1);
  1047. if (err)
  1048. goto out_free;
  1049. err = ubifs_tnc_read_node(c, zbr2, dent2);
  1050. if (err)
  1051. goto out_free;
  1052. err = ubifs_validate_entry(c, dent2);
  1053. if (err)
  1054. goto out_free;
  1055. /* Make sure node keys are the same as in zbranch */
  1056. err = 1;
  1057. key_read(c, &dent1->key, &key);
  1058. if (keys_cmp(c, &zbr1->key, &key)) {
  1059. dbg_err("1st entry at %d:%d has key %s", zbr1->lnum,
  1060. zbr1->offs, DBGKEY(&key));
  1061. dbg_err("but it should have key %s according to tnc",
  1062. DBGKEY(&zbr1->key));
  1063. dbg_dump_node(c, dent1);
  1064. goto out_free;
  1065. }
  1066. key_read(c, &dent2->key, &key);
  1067. if (keys_cmp(c, &zbr2->key, &key)) {
  1068. dbg_err("2nd entry at %d:%d has key %s", zbr1->lnum,
  1069. zbr1->offs, DBGKEY(&key));
  1070. dbg_err("but it should have key %s according to tnc",
  1071. DBGKEY(&zbr2->key));
  1072. dbg_dump_node(c, dent2);
  1073. goto out_free;
  1074. }
  1075. nlen1 = le16_to_cpu(dent1->nlen);
  1076. nlen2 = le16_to_cpu(dent2->nlen);
  1077. cmp = memcmp(dent1->name, dent2->name, min_t(int, nlen1, nlen2));
  1078. if (cmp < 0 || (cmp == 0 && nlen1 < nlen2)) {
  1079. err = 0;
  1080. goto out_free;
  1081. }
  1082. if (cmp == 0 && nlen1 == nlen2)
  1083. dbg_err("2 xent/dent nodes with the same name");
  1084. else
  1085. dbg_err("bad order of colliding key %s",
  1086. DBGKEY(&key));
  1087. ubifs_msg("first node at %d:%d\n", zbr1->lnum, zbr1->offs);
  1088. dbg_dump_node(c, dent1);
  1089. ubifs_msg("second node at %d:%d\n", zbr2->lnum, zbr2->offs);
  1090. dbg_dump_node(c, dent2);
  1091. out_free:
  1092. kfree(dent2);
  1093. kfree(dent1);
  1094. return err;
  1095. }
  1096. /**
  1097. * dbg_check_znode - check if znode is all right.
  1098. * @c: UBIFS file-system description object
  1099. * @zbr: zbranch which points to this znode
  1100. *
  1101. * This function makes sure that znode referred to by @zbr is all right.
  1102. * Returns zero if it is, and %-EINVAL if it is not.
  1103. */
  1104. static int dbg_check_znode(struct ubifs_info *c, struct ubifs_zbranch *zbr)
  1105. {
  1106. struct ubifs_znode *znode = zbr->znode;
  1107. struct ubifs_znode *zp = znode->parent;
  1108. int n, err, cmp;
  1109. if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
  1110. err = 1;
  1111. goto out;
  1112. }
  1113. if (znode->level < 0) {
  1114. err = 2;
  1115. goto out;
  1116. }
  1117. if (znode->iip < 0 || znode->iip >= c->fanout) {
  1118. err = 3;
  1119. goto out;
  1120. }
  1121. if (zbr->len == 0)
  1122. /* Only dirty zbranch may have no on-flash nodes */
  1123. if (!ubifs_zn_dirty(znode)) {
  1124. err = 4;
  1125. goto out;
  1126. }
  1127. if (ubifs_zn_dirty(znode)) {
  1128. /*
  1129. * If znode is dirty, its parent has to be dirty as well. The
  1130. * order of the operation is important, so we have to have
  1131. * memory barriers.
  1132. */
  1133. smp_mb();
  1134. if (zp && !ubifs_zn_dirty(zp)) {
  1135. /*
  1136. * The dirty flag is atomic and is cleared outside the
  1137. * TNC mutex, so znode's dirty flag may now have
  1138. * been cleared. The child is always cleared before the
  1139. * parent, so we just need to check again.
  1140. */
  1141. smp_mb();
  1142. if (ubifs_zn_dirty(znode)) {
  1143. err = 5;
  1144. goto out;
  1145. }
  1146. }
  1147. }
  1148. if (zp) {
  1149. const union ubifs_key *min, *max;
  1150. if (znode->level != zp->level - 1) {
  1151. err = 6;
  1152. goto out;
  1153. }
  1154. /* Make sure the 'parent' pointer in our znode is correct */
  1155. err = ubifs_search_zbranch(c, zp, &zbr->key, &n);
  1156. if (!err) {
  1157. /* This zbranch does not exist in the parent */
  1158. err = 7;
  1159. goto out;
  1160. }
  1161. if (znode->iip >= zp->child_cnt) {
  1162. err = 8;
  1163. goto out;
  1164. }
  1165. if (znode->iip != n) {
  1166. /* This may happen only in case of collisions */
  1167. if (keys_cmp(c, &zp->zbranch[n].key,
  1168. &zp->zbranch[znode->iip].key)) {
  1169. err = 9;
  1170. goto out;
  1171. }
  1172. n = znode->iip;
  1173. }
  1174. /*
  1175. * Make sure that the first key in our znode is greater than or
  1176. * equal to the key in the pointing zbranch.
  1177. */
  1178. min = &zbr->key;
  1179. cmp = keys_cmp(c, min, &znode->zbranch[0].key);
  1180. if (cmp == 1) {
  1181. err = 10;
  1182. goto out;
  1183. }
  1184. if (n + 1 < zp->child_cnt) {
  1185. max = &zp->zbranch[n + 1].key;
  1186. /*
  1187. * Make sure the last key in our znode is less or
  1188. * equivalent than the key in the zbranch which goes
  1189. * after our pointing zbranch.
  1190. */
  1191. cmp = keys_cmp(c, max,
  1192. &znode->zbranch[znode->child_cnt - 1].key);
  1193. if (cmp == -1) {
  1194. err = 11;
  1195. goto out;
  1196. }
  1197. }
  1198. } else {
  1199. /* This may only be root znode */
  1200. if (zbr != &c->zroot) {
  1201. err = 12;
  1202. goto out;
  1203. }
  1204. }
  1205. /*
  1206. * Make sure that next key is greater or equivalent then the previous
  1207. * one.
  1208. */
  1209. for (n = 1; n < znode->child_cnt; n++) {
  1210. cmp = keys_cmp(c, &znode->zbranch[n - 1].key,
  1211. &znode->zbranch[n].key);
  1212. if (cmp > 0) {
  1213. err = 13;
  1214. goto out;
  1215. }
  1216. if (cmp == 0) {
  1217. /* This can only be keys with colliding hash */
  1218. if (!is_hash_key(c, &znode->zbranch[n].key)) {
  1219. err = 14;
  1220. goto out;
  1221. }
  1222. if (znode->level != 0 || c->replaying)
  1223. continue;
  1224. /*
  1225. * Colliding keys should follow binary order of
  1226. * corresponding xentry/dentry names.
  1227. */
  1228. err = dbg_check_key_order(c, &znode->zbranch[n - 1],
  1229. &znode->zbranch[n]);
  1230. if (err < 0)
  1231. return err;
  1232. if (err) {
  1233. err = 15;
  1234. goto out;
  1235. }
  1236. }
  1237. }
  1238. for (n = 0; n < znode->child_cnt; n++) {
  1239. if (!znode->zbranch[n].znode &&
  1240. (znode->zbranch[n].lnum == 0 ||
  1241. znode->zbranch[n].len == 0)) {
  1242. err = 16;
  1243. goto out;
  1244. }
  1245. if (znode->zbranch[n].lnum != 0 &&
  1246. znode->zbranch[n].len == 0) {
  1247. err = 17;
  1248. goto out;
  1249. }
  1250. if (znode->zbranch[n].lnum == 0 &&
  1251. znode->zbranch[n].len != 0) {
  1252. err = 18;
  1253. goto out;
  1254. }
  1255. if (znode->zbranch[n].lnum == 0 &&
  1256. znode->zbranch[n].offs != 0) {
  1257. err = 19;
  1258. goto out;
  1259. }
  1260. if (znode->level != 0 && znode->zbranch[n].znode)
  1261. if (znode->zbranch[n].znode->parent != znode) {
  1262. err = 20;
  1263. goto out;
  1264. }
  1265. }
  1266. return 0;
  1267. out:
  1268. ubifs_err("failed, error %d", err);
  1269. ubifs_msg("dump of the znode");
  1270. dbg_dump_znode(c, znode);
  1271. if (zp) {
  1272. ubifs_msg("dump of the parent znode");
  1273. dbg_dump_znode(c, zp);
  1274. }
  1275. dump_stack();
  1276. return -EINVAL;
  1277. }
  1278. /**
  1279. * dbg_check_tnc - check TNC tree.
  1280. * @c: UBIFS file-system description object
  1281. * @extra: do extra checks that are possible at start commit
  1282. *
  1283. * This function traverses whole TNC tree and checks every znode. Returns zero
  1284. * if everything is all right and %-EINVAL if something is wrong with TNC.
  1285. */
  1286. int dbg_check_tnc(struct ubifs_info *c, int extra)
  1287. {
  1288. struct ubifs_znode *znode;
  1289. long clean_cnt = 0, dirty_cnt = 0;
  1290. int err, last;
  1291. if (!(ubifs_chk_flags & UBIFS_CHK_TNC))
  1292. return 0;
  1293. ubifs_assert(mutex_is_locked(&c->tnc_mutex));
  1294. if (!c->zroot.znode)
  1295. return 0;
  1296. znode = ubifs_tnc_postorder_first(c->zroot.znode);
  1297. while (1) {
  1298. struct ubifs_znode *prev;
  1299. struct ubifs_zbranch *zbr;
  1300. if (!znode->parent)
  1301. zbr = &c->zroot;
  1302. else
  1303. zbr = &znode->parent->zbranch[znode->iip];
  1304. err = dbg_check_znode(c, zbr);
  1305. if (err)
  1306. return err;
  1307. if (extra) {
  1308. if (ubifs_zn_dirty(znode))
  1309. dirty_cnt += 1;
  1310. else
  1311. clean_cnt += 1;
  1312. }
  1313. prev = znode;
  1314. znode = ubifs_tnc_postorder_next(znode);
  1315. if (!znode)
  1316. break;
  1317. /*
  1318. * If the last key of this znode is equivalent to the first key
  1319. * of the next znode (collision), then check order of the keys.
  1320. */
  1321. last = prev->child_cnt - 1;
  1322. if (prev->level == 0 && znode->level == 0 && !c->replaying &&
  1323. !keys_cmp(c, &prev->zbranch[last].key,
  1324. &znode->zbranch[0].key)) {
  1325. err = dbg_check_key_order(c, &prev->zbranch[last],
  1326. &znode->zbranch[0]);
  1327. if (err < 0)
  1328. return err;
  1329. if (err) {
  1330. ubifs_msg("first znode");
  1331. dbg_dump_znode(c, prev);
  1332. ubifs_msg("second znode");
  1333. dbg_dump_znode(c, znode);
  1334. return -EINVAL;
  1335. }
  1336. }
  1337. }
  1338. if (extra) {
  1339. if (clean_cnt != atomic_long_read(&c->clean_zn_cnt)) {
  1340. ubifs_err("incorrect clean_zn_cnt %ld, calculated %ld",
  1341. atomic_long_read(&c->clean_zn_cnt),
  1342. clean_cnt);
  1343. return -EINVAL;
  1344. }
  1345. if (dirty_cnt != atomic_long_read(&c->dirty_zn_cnt)) {
  1346. ubifs_err("incorrect dirty_zn_cnt %ld, calculated %ld",
  1347. atomic_long_read(&c->dirty_zn_cnt),
  1348. dirty_cnt);
  1349. return -EINVAL;
  1350. }
  1351. }
  1352. return 0;
  1353. }
  1354. /**
  1355. * dbg_walk_index - walk the on-flash index.
  1356. * @c: UBIFS file-system description object
  1357. * @leaf_cb: called for each leaf node
  1358. * @znode_cb: called for each indexing node
  1359. * @priv: private data which is passed to callbacks
  1360. *
  1361. * This function walks the UBIFS index and calls the @leaf_cb for each leaf
  1362. * node and @znode_cb for each indexing node. Returns zero in case of success
  1363. * and a negative error code in case of failure.
  1364. *
  1365. * It would be better if this function removed every znode it pulled to into
  1366. * the TNC, so that the behavior more closely matched the non-debugging
  1367. * behavior.
  1368. */
  1369. int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
  1370. dbg_znode_callback znode_cb, void *priv)
  1371. {
  1372. int err;
  1373. struct ubifs_zbranch *zbr;
  1374. struct ubifs_znode *znode, *child;
  1375. mutex_lock(&c->tnc_mutex);
  1376. /* If the root indexing node is not in TNC - pull it */
  1377. if (!c->zroot.znode) {
  1378. c->zroot.znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
  1379. if (IS_ERR(c->zroot.znode)) {
  1380. err = PTR_ERR(c->zroot.znode);
  1381. c->zroot.znode = NULL;
  1382. goto out_unlock;
  1383. }
  1384. }
  1385. /*
  1386. * We are going to traverse the indexing tree in the postorder manner.
  1387. * Go down and find the leftmost indexing node where we are going to
  1388. * start from.
  1389. */
  1390. znode = c->zroot.znode;
  1391. while (znode->level > 0) {
  1392. zbr = &znode->zbranch[0];
  1393. child = zbr->znode;
  1394. if (!child) {
  1395. child = ubifs_load_znode(c, zbr, znode, 0);
  1396. if (IS_ERR(child)) {
  1397. err = PTR_ERR(child);
  1398. goto out_unlock;
  1399. }
  1400. zbr->znode = child;
  1401. }
  1402. znode = child;
  1403. }
  1404. /* Iterate over all indexing nodes */
  1405. while (1) {
  1406. int idx;
  1407. cond_resched();
  1408. if (znode_cb) {
  1409. err = znode_cb(c, znode, priv);
  1410. if (err) {
  1411. ubifs_err("znode checking function returned "
  1412. "error %d", err);
  1413. dbg_dump_znode(c, znode);
  1414. goto out_dump;
  1415. }
  1416. }
  1417. if (leaf_cb && znode->level == 0) {
  1418. for (idx = 0; idx < znode->child_cnt; idx++) {
  1419. zbr = &znode->zbranch[idx];
  1420. err = leaf_cb(c, zbr, priv);
  1421. if (err) {
  1422. ubifs_err("leaf checking function "
  1423. "returned error %d, for leaf "
  1424. "at LEB %d:%d",
  1425. err, zbr->lnum, zbr->offs);
  1426. goto out_dump;
  1427. }
  1428. }
  1429. }
  1430. if (!znode->parent)
  1431. break;
  1432. idx = znode->iip + 1;
  1433. znode = znode->parent;
  1434. if (idx < znode->child_cnt) {
  1435. /* Switch to the next index in the parent */
  1436. zbr = &znode->zbranch[idx];
  1437. child = zbr->znode;
  1438. if (!child) {
  1439. child = ubifs_load_znode(c, zbr, znode, idx);
  1440. if (IS_ERR(child)) {
  1441. err = PTR_ERR(child);
  1442. goto out_unlock;
  1443. }
  1444. zbr->znode = child;
  1445. }
  1446. znode = child;
  1447. } else
  1448. /*
  1449. * This is the last child, switch to the parent and
  1450. * continue.
  1451. */
  1452. continue;
  1453. /* Go to the lowest leftmost znode in the new sub-tree */
  1454. while (znode->level > 0) {
  1455. zbr = &znode->zbranch[0];
  1456. child = zbr->znode;
  1457. if (!child) {
  1458. child = ubifs_load_znode(c, zbr, znode, 0);
  1459. if (IS_ERR(child)) {
  1460. err = PTR_ERR(child);
  1461. goto out_unlock;
  1462. }
  1463. zbr->znode = child;
  1464. }
  1465. znode = child;
  1466. }
  1467. }
  1468. mutex_unlock(&c->tnc_mutex);
  1469. return 0;
  1470. out_dump:
  1471. if (znode->parent)
  1472. zbr = &znode->parent->zbranch[znode->iip];
  1473. else
  1474. zbr = &c->zroot;
  1475. ubifs_msg("dump of znode at LEB %d:%d", zbr->lnum, zbr->offs);
  1476. dbg_dump_znode(c, znode);
  1477. out_unlock:
  1478. mutex_unlock(&c->tnc_mutex);
  1479. return err;
  1480. }
  1481. /**
  1482. * add_size - add znode size to partially calculated index size.
  1483. * @c: UBIFS file-system description object
  1484. * @znode: znode to add size for
  1485. * @priv: partially calculated index size
  1486. *
  1487. * This is a helper function for 'dbg_check_idx_size()' which is called for
  1488. * every indexing node and adds its size to the 'long long' variable pointed to
  1489. * by @priv.
  1490. */
  1491. static int add_size(struct ubifs_info *c, struct ubifs_znode *znode, void *priv)
  1492. {
  1493. long long *idx_size = priv;
  1494. int add;
  1495. add = ubifs_idx_node_sz(c, znode->child_cnt);
  1496. add = ALIGN(add, 8);
  1497. *idx_size += add;
  1498. return 0;
  1499. }
  1500. /**
  1501. * dbg_check_idx_size - check index size.
  1502. * @c: UBIFS file-system description object
  1503. * @idx_size: size to check
  1504. *
  1505. * This function walks the UBIFS index, calculates its size and checks that the
  1506. * size is equivalent to @idx_size. Returns zero in case of success and a
  1507. * negative error code in case of failure.
  1508. */
  1509. int dbg_check_idx_size(struct ubifs_info *c, long long idx_size)
  1510. {
  1511. int err;
  1512. long long calc = 0;
  1513. if (!(ubifs_chk_flags & UBIFS_CHK_IDX_SZ))
  1514. return 0;
  1515. err = dbg_walk_index(c, NULL, add_size, &calc);
  1516. if (err) {
  1517. ubifs_err("error %d while walking the index", err);
  1518. return err;
  1519. }
  1520. if (calc != idx_size) {
  1521. ubifs_err("index size check failed: calculated size is %lld, "
  1522. "should be %lld", calc, idx_size);
  1523. dump_stack();
  1524. return -EINVAL;
  1525. }
  1526. return 0;
  1527. }
  1528. /**
  1529. * struct fsck_inode - information about an inode used when checking the file-system.
  1530. * @rb: link in the RB-tree of inodes
  1531. * @inum: inode number
  1532. * @mode: inode type, permissions, etc
  1533. * @nlink: inode link count
  1534. * @xattr_cnt: count of extended attributes
  1535. * @references: how many directory/xattr entries refer this inode (calculated
  1536. * while walking the index)
  1537. * @calc_cnt: for directory inode count of child directories
  1538. * @size: inode size (read from on-flash inode)
  1539. * @xattr_sz: summary size of all extended attributes (read from on-flash
  1540. * inode)
  1541. * @calc_sz: for directories calculated directory size
  1542. * @calc_xcnt: count of extended attributes
  1543. * @calc_xsz: calculated summary size of all extended attributes
  1544. * @xattr_nms: sum of lengths of all extended attribute names belonging to this
  1545. * inode (read from on-flash inode)
  1546. * @calc_xnms: calculated sum of lengths of all extended attribute names
  1547. */
  1548. struct fsck_inode {
  1549. struct rb_node rb;
  1550. ino_t inum;
  1551. umode_t mode;
  1552. unsigned int nlink;
  1553. unsigned int xattr_cnt;
  1554. int references;
  1555. int calc_cnt;
  1556. long long size;
  1557. unsigned int xattr_sz;
  1558. long long calc_sz;
  1559. long long calc_xcnt;
  1560. long long calc_xsz;
  1561. unsigned int xattr_nms;
  1562. long long calc_xnms;
  1563. };
  1564. /**
  1565. * struct fsck_data - private FS checking information.
  1566. * @inodes: RB-tree of all inodes (contains @struct fsck_inode objects)
  1567. */
  1568. struct fsck_data {
  1569. struct rb_root inodes;
  1570. };
  1571. /**
  1572. * add_inode - add inode information to RB-tree of inodes.
  1573. * @c: UBIFS file-system description object
  1574. * @fsckd: FS checking information
  1575. * @ino: raw UBIFS inode to add
  1576. *
  1577. * This is a helper function for 'check_leaf()' which adds information about
  1578. * inode @ino to the RB-tree of inodes. Returns inode information pointer in
  1579. * case of success and a negative error code in case of failure.
  1580. */
  1581. static struct fsck_inode *add_inode(struct ubifs_info *c,
  1582. struct fsck_data *fsckd,
  1583. struct ubifs_ino_node *ino)
  1584. {
  1585. struct rb_node **p, *parent = NULL;
  1586. struct fsck_inode *fscki;
  1587. ino_t inum = key_inum_flash(c, &ino->key);
  1588. p = &fsckd->inodes.rb_node;
  1589. while (*p) {
  1590. parent = *p;
  1591. fscki = rb_entry(parent, struct fsck_inode, rb);
  1592. if (inum < fscki->inum)
  1593. p = &(*p)->rb_left;
  1594. else if (inum > fscki->inum)
  1595. p = &(*p)->rb_right;
  1596. else
  1597. return fscki;
  1598. }
  1599. if (inum > c->highest_inum) {
  1600. ubifs_err("too high inode number, max. is %lu",
  1601. (unsigned long)c->highest_inum);
  1602. return ERR_PTR(-EINVAL);
  1603. }
  1604. fscki = kzalloc(sizeof(struct fsck_inode), GFP_NOFS);
  1605. if (!fscki)
  1606. return ERR_PTR(-ENOMEM);
  1607. fscki->inum = inum;
  1608. fscki->nlink = le32_to_cpu(ino->nlink);
  1609. fscki->size = le64_to_cpu(ino->size);
  1610. fscki->xattr_cnt = le32_to_cpu(ino->xattr_cnt);
  1611. fscki->xattr_sz = le32_to_cpu(ino->xattr_size);
  1612. fscki->xattr_nms = le32_to_cpu(ino->xattr_names);
  1613. fscki->mode = le32_to_cpu(ino->mode);
  1614. if (S_ISDIR(fscki->mode)) {
  1615. fscki->calc_sz = UBIFS_INO_NODE_SZ;
  1616. fscki->calc_cnt = 2;
  1617. }
  1618. rb_link_node(&fscki->rb, parent, p);
  1619. rb_insert_color(&fscki->rb, &fsckd->inodes);
  1620. return fscki;
  1621. }
  1622. /**
  1623. * search_inode - search inode in the RB-tree of inodes.
  1624. * @fsckd: FS checking information
  1625. * @inum: inode number to search
  1626. *
  1627. * This is a helper function for 'check_leaf()' which searches inode @inum in
  1628. * the RB-tree of inodes and returns an inode information pointer or %NULL if
  1629. * the inode was not found.
  1630. */
  1631. static struct fsck_inode *search_inode(struct fsck_data *fsckd, ino_t inum)
  1632. {
  1633. struct rb_node *p;
  1634. struct fsck_inode *fscki;
  1635. p = fsckd->inodes.rb_node;
  1636. while (p) {
  1637. fscki = rb_entry(p, struct fsck_inode, rb);
  1638. if (inum < fscki->inum)
  1639. p = p->rb_left;
  1640. else if (inum > fscki->inum)
  1641. p = p->rb_right;
  1642. else
  1643. return fscki;
  1644. }
  1645. return NULL;
  1646. }
  1647. /**
  1648. * read_add_inode - read inode node and add it to RB-tree of inodes.
  1649. * @c: UBIFS file-system description object
  1650. * @fsckd: FS checking information
  1651. * @inum: inode number to read
  1652. *
  1653. * This is a helper function for 'check_leaf()' which finds inode node @inum in
  1654. * the index, reads it, and adds it to the RB-tree of inodes. Returns inode
  1655. * information pointer in case of success and a negative error code in case of
  1656. * failure.
  1657. */
  1658. static struct fsck_inode *read_add_inode(struct ubifs_info *c,
  1659. struct fsck_data *fsckd, ino_t inum)
  1660. {
  1661. int n, err;
  1662. union ubifs_key key;
  1663. struct ubifs_znode *znode;
  1664. struct ubifs_zbranch *zbr;
  1665. struct ubifs_ino_node *ino;
  1666. struct fsck_inode *fscki;
  1667. fscki = search_inode(fsckd, inum);
  1668. if (fscki)
  1669. return fscki;
  1670. ino_key_init(c, &key, inum);
  1671. err = ubifs_lookup_level0(c, &key, &znode, &n);
  1672. if (!err) {
  1673. ubifs_err("inode %lu not found in index", (unsigned long)inum);
  1674. return ERR_PTR(-ENOENT);
  1675. } else if (err < 0) {
  1676. ubifs_err("error %d while looking up inode %lu",
  1677. err, (unsigned long)inum);
  1678. return ERR_PTR(err);
  1679. }
  1680. zbr = &znode->zbranch[n];
  1681. if (zbr->len < UBIFS_INO_NODE_SZ) {
  1682. ubifs_err("bad node %lu node length %d",
  1683. (unsigned long)inum, zbr->len);
  1684. return ERR_PTR(-EINVAL);
  1685. }
  1686. ino = kmalloc(zbr->len, GFP_NOFS);
  1687. if (!ino)
  1688. return ERR_PTR(-ENOMEM);
  1689. err = ubifs_tnc_read_node(c, zbr, ino);
  1690. if (err) {
  1691. ubifs_err("cannot read inode node at LEB %d:%d, error %d",
  1692. zbr->lnum, zbr->offs, err);
  1693. kfree(ino);
  1694. return ERR_PTR(err);
  1695. }
  1696. fscki = add_inode(c, fsckd, ino);
  1697. kfree(ino);
  1698. if (IS_ERR(fscki)) {
  1699. ubifs_err("error %ld while adding inode %lu node",
  1700. PTR_ERR(fscki), (unsigned long)inum);
  1701. return fscki;
  1702. }
  1703. return fscki;
  1704. }
  1705. /**
  1706. * check_leaf - check leaf node.
  1707. * @c: UBIFS file-system description object
  1708. * @zbr: zbranch of the leaf node to check
  1709. * @priv: FS checking information
  1710. *
  1711. * This is a helper function for 'dbg_check_filesystem()' which is called for
  1712. * every single leaf node while walking the indexing tree. It checks that the
  1713. * leaf node referred from the indexing tree exists, has correct CRC, and does
  1714. * some other basic validation. This function is also responsible for building
  1715. * an RB-tree of inodes - it adds all inodes into the RB-tree. It also
  1716. * calculates reference count, size, etc for each inode in order to later
  1717. * compare them to the information stored inside the inodes and detect possible
  1718. * inconsistencies. Returns zero in case of success and a negative error code
  1719. * in case of failure.
  1720. */
  1721. static int check_leaf(struct ubifs_info *c, struct ubifs_zbranch *zbr,
  1722. void *priv)
  1723. {
  1724. ino_t inum;
  1725. void *node;
  1726. struct ubifs_ch *ch;
  1727. int err, type = key_type(c, &zbr->key);
  1728. struct fsck_inode *fscki;
  1729. if (zbr->len < UBIFS_CH_SZ) {
  1730. ubifs_err("bad leaf length %d (LEB %d:%d)",
  1731. zbr->len, zbr->lnum, zbr->offs);
  1732. return -EINVAL;
  1733. }
  1734. node = kmalloc(zbr->len, GFP_NOFS);
  1735. if (!node)
  1736. return -ENOMEM;
  1737. err = ubifs_tnc_read_node(c, zbr, node);
  1738. if (err) {
  1739. ubifs_err("cannot read leaf node at LEB %d:%d, error %d",
  1740. zbr->lnum, zbr->offs, err);
  1741. goto out_free;
  1742. }
  1743. /* If this is an inode node, add it to RB-tree of inodes */
  1744. if (type == UBIFS_INO_KEY) {
  1745. fscki = add_inode(c, priv, node);
  1746. if (IS_ERR(fscki)) {
  1747. err = PTR_ERR(fscki);
  1748. ubifs_err("error %d while adding inode node", err);
  1749. goto out_dump;
  1750. }
  1751. goto out;
  1752. }
  1753. if (type != UBIFS_DENT_KEY && type != UBIFS_XENT_KEY &&
  1754. type != UBIFS_DATA_KEY) {
  1755. ubifs_err("unexpected node type %d at LEB %d:%d",
  1756. type, zbr->lnum, zbr->offs);
  1757. err = -EINVAL;
  1758. goto out_free;
  1759. }
  1760. ch = node;
  1761. if (le64_to_cpu(ch->sqnum) > c->max_sqnum) {
  1762. ubifs_err("too high sequence number, max. is %llu",
  1763. c->max_sqnum);
  1764. err = -EINVAL;
  1765. goto out_dump;
  1766. }
  1767. if (type == UBIFS_DATA_KEY) {
  1768. long long blk_offs;
  1769. struct ubifs_data_node *dn = node;
  1770. /*
  1771. * Search the inode node this data node belongs to and insert
  1772. * it to the RB-tree of inodes.
  1773. */
  1774. inum = key_inum_flash(c, &dn->key);
  1775. fscki = read_add_inode(c, priv, inum);
  1776. if (IS_ERR(fscki)) {
  1777. err = PTR_ERR(fscki);
  1778. ubifs_err("error %d while processing data node and "
  1779. "trying to find inode node %lu",
  1780. err, (unsigned long)inum);
  1781. goto out_dump;
  1782. }
  1783. /* Make sure the data node is within inode size */
  1784. blk_offs = key_block_flash(c, &dn->key);
  1785. blk_offs <<= UBIFS_BLOCK_SHIFT;
  1786. blk_offs += le32_to_cpu(dn->size);
  1787. if (blk_offs > fscki->size) {
  1788. ubifs_err("data node at LEB %d:%d is not within inode "
  1789. "size %lld", zbr->lnum, zbr->offs,
  1790. fscki->size);
  1791. err = -EINVAL;
  1792. goto out_dump;
  1793. }
  1794. } else {
  1795. int nlen;
  1796. struct ubifs_dent_node *dent = node;
  1797. struct fsck_inode *fscki1;
  1798. err = ubifs_validate_entry(c, dent);
  1799. if (err)
  1800. goto out_dump;
  1801. /*
  1802. * Search the inode node this entry refers to and the parent
  1803. * inode node and insert them to the RB-tree of inodes.
  1804. */
  1805. inum = le64_to_cpu(dent->inum);
  1806. fscki = read_add_inode(c, priv, inum);
  1807. if (IS_ERR(fscki)) {
  1808. err = PTR_ERR(fscki);
  1809. ubifs_err("error %d while processing entry node and "
  1810. "trying to find inode node %lu",
  1811. err, (unsigned long)inum);
  1812. goto out_dump;
  1813. }
  1814. /* Count how many direntries or xentries refers this inode */
  1815. fscki->references += 1;
  1816. inum = key_inum_flash(c, &dent->key);
  1817. fscki1 = read_add_inode(c, priv, inum);
  1818. if (IS_ERR(fscki1)) {
  1819. err = PTR_ERR(fscki1);
  1820. ubifs_err("error %d while processing entry node and "
  1821. "trying to find parent inode node %lu",
  1822. err, (unsigned long)inum);
  1823. goto out_dump;
  1824. }
  1825. nlen = le16_to_cpu(dent->nlen);
  1826. if (type == UBIFS_XENT_KEY) {
  1827. fscki1->calc_xcnt += 1;
  1828. fscki1->calc_xsz += CALC_DENT_SIZE(nlen);
  1829. fscki1->calc_xsz += CALC_XATTR_BYTES(fscki->size);
  1830. fscki1->calc_xnms += nlen;
  1831. } else {
  1832. fscki1->calc_sz += CALC_DENT_SIZE(nlen);
  1833. if (dent->type == UBIFS_ITYPE_DIR)
  1834. fscki1->calc_cnt += 1;
  1835. }
  1836. }
  1837. out:
  1838. kfree(node);
  1839. return 0;
  1840. out_dump:
  1841. ubifs_msg("dump of node at LEB %d:%d", zbr->lnum, zbr->offs);
  1842. dbg_dump_node(c, node);
  1843. out_free:
  1844. kfree(node);
  1845. return err;
  1846. }
  1847. /**
  1848. * free_inodes - free RB-tree of inodes.
  1849. * @fsckd: FS checking information
  1850. */
  1851. static void free_inodes(struct fsck_data *fsckd)
  1852. {
  1853. struct rb_node *this = fsckd->inodes.rb_node;
  1854. struct fsck_inode *fscki;
  1855. while (this) {
  1856. if (this->rb_left)
  1857. this = this->rb_left;
  1858. else if (this->rb_right)
  1859. this = this->rb_right;
  1860. else {
  1861. fscki = rb_entry(this, struct fsck_inode, rb);
  1862. this = rb_parent(this);
  1863. if (this) {
  1864. if (this->rb_left == &fscki->rb)
  1865. this->rb_left = NULL;
  1866. else
  1867. this->rb_right = NULL;
  1868. }
  1869. kfree(fscki);
  1870. }
  1871. }
  1872. }
  1873. /**
  1874. * check_inodes - checks all inodes.
  1875. * @c: UBIFS file-system description object
  1876. * @fsckd: FS checking information
  1877. *
  1878. * This is a helper function for 'dbg_check_filesystem()' which walks the
  1879. * RB-tree of inodes after the index scan has been finished, and checks that
  1880. * inode nlink, size, etc are correct. Returns zero if inodes are fine,
  1881. * %-EINVAL if not, and a negative error code in case of failure.
  1882. */
  1883. static int check_inodes(struct ubifs_info *c, struct fsck_data *fsckd)
  1884. {
  1885. int n, err;
  1886. union ubifs_key key;
  1887. struct ubifs_znode *znode;
  1888. struct ubifs_zbranch *zbr;
  1889. struct ubifs_ino_node *ino;
  1890. struct fsck_inode *fscki;
  1891. struct rb_node *this = rb_first(&fsckd->inodes);
  1892. while (this) {
  1893. fscki = rb_entry(this, struct fsck_inode, rb);
  1894. this = rb_next(this);
  1895. if (S_ISDIR(fscki->mode)) {
  1896. /*
  1897. * Directories have to have exactly one reference (they
  1898. * cannot have hardlinks), although root inode is an
  1899. * exception.
  1900. */
  1901. if (fscki->inum != UBIFS_ROOT_INO &&
  1902. fscki->references != 1) {
  1903. ubifs_err("directory inode %lu has %d "
  1904. "direntries which refer it, but "
  1905. "should be 1",
  1906. (unsigned long)fscki->inum,
  1907. fscki->references);
  1908. goto out_dump;
  1909. }
  1910. if (fscki->inum == UBIFS_ROOT_INO &&
  1911. fscki->references != 0) {
  1912. ubifs_err("root inode %lu has non-zero (%d) "
  1913. "direntries which refer it",
  1914. (unsigned long)fscki->inum,
  1915. fscki->references);
  1916. goto out_dump;
  1917. }
  1918. if (fscki->calc_sz != fscki->size) {
  1919. ubifs_err("directory inode %lu size is %lld, "
  1920. "but calculated size is %lld",
  1921. (unsigned long)fscki->inum,
  1922. fscki->size, fscki->calc_sz);
  1923. goto out_dump;
  1924. }
  1925. if (fscki->calc_cnt != fscki->nlink) {
  1926. ubifs_err("directory inode %lu nlink is %d, "
  1927. "but calculated nlink is %d",
  1928. (unsigned long)fscki->inum,
  1929. fscki->nlink, fscki->calc_cnt);
  1930. goto out_dump;
  1931. }
  1932. } else {
  1933. if (fscki->references != fscki->nlink) {
  1934. ubifs_err("inode %lu nlink is %d, but "
  1935. "calculated nlink is %d",
  1936. (unsigned long)fscki->inum,
  1937. fscki->nlink, fscki->references);
  1938. goto out_dump;
  1939. }
  1940. }
  1941. if (fscki->xattr_sz != fscki->calc_xsz) {
  1942. ubifs_err("inode %lu has xattr size %u, but "
  1943. "calculated size is %lld",
  1944. (unsigned long)fscki->inum, fscki->xattr_sz,
  1945. fscki->calc_xsz);
  1946. goto out_dump;
  1947. }
  1948. if (fscki->xattr_cnt != fscki->calc_xcnt) {
  1949. ubifs_err("inode %lu has %u xattrs, but "
  1950. "calculated count is %lld",
  1951. (unsigned long)fscki->inum,
  1952. fscki->xattr_cnt, fscki->calc_xcnt);
  1953. goto out_dump;
  1954. }
  1955. if (fscki->xattr_nms != fscki->calc_xnms) {
  1956. ubifs_err("inode %lu has xattr names' size %u, but "
  1957. "calculated names' size is %lld",
  1958. (unsigned long)fscki->inum, fscki->xattr_nms,
  1959. fscki->calc_xnms);
  1960. goto out_dump;
  1961. }
  1962. }
  1963. return 0;
  1964. out_dump:
  1965. /* Read the bad inode and dump it */
  1966. ino_key_init(c, &key, fscki->inum);
  1967. err = ubifs_lookup_level0(c, &key, &znode, &n);
  1968. if (!err) {
  1969. ubifs_err("inode %lu not found in index",
  1970. (unsigned long)fscki->inum);
  1971. return -ENOENT;
  1972. } else if (err < 0) {
  1973. ubifs_err("error %d while looking up inode %lu",
  1974. err, (unsigned long)fscki->inum);
  1975. return err;
  1976. }
  1977. zbr = &znode->zbranch[n];
  1978. ino = kmalloc(zbr->len, GFP_NOFS);
  1979. if (!ino)
  1980. return -ENOMEM;
  1981. err = ubifs_tnc_read_node(c, zbr, ino);
  1982. if (err) {
  1983. ubifs_err("cannot read inode node at LEB %d:%d, error %d",
  1984. zbr->lnum, zbr->offs, err);
  1985. kfree(ino);
  1986. return err;
  1987. }
  1988. ubifs_msg("dump of the inode %lu sitting in LEB %d:%d",
  1989. (unsigned long)fscki->inum, zbr->lnum, zbr->offs);
  1990. dbg_dump_node(c, ino);
  1991. kfree(ino);
  1992. return -EINVAL;
  1993. }
  1994. /**
  1995. * dbg_check_filesystem - check the file-system.
  1996. * @c: UBIFS file-system description object
  1997. *
  1998. * This function checks the file system, namely:
  1999. * o makes sure that all leaf nodes exist and their CRCs are correct;
  2000. * o makes sure inode nlink, size, xattr size/count are correct (for all
  2001. * inodes).
  2002. *
  2003. * The function reads whole indexing tree and all nodes, so it is pretty
  2004. * heavy-weight. Returns zero if the file-system is consistent, %-EINVAL if
  2005. * not, and a negative error code in case of failure.
  2006. */
  2007. int dbg_check_filesystem(struct ubifs_info *c)
  2008. {
  2009. int err;
  2010. struct fsck_data fsckd;
  2011. if (!(ubifs_chk_flags & UBIFS_CHK_FS))
  2012. return 0;
  2013. fsckd.inodes = RB_ROOT;
  2014. err = dbg_walk_index(c, check_leaf, NULL, &fsckd);
  2015. if (err)
  2016. goto out_free;
  2017. err = check_inodes(c, &fsckd);
  2018. if (err)
  2019. goto out_free;
  2020. free_inodes(&fsckd);
  2021. return 0;
  2022. out_free:
  2023. ubifs_err("file-system check failed with error %d", err);
  2024. dump_stack();
  2025. free_inodes(&fsckd);
  2026. return err;
  2027. }
  2028. static int invocation_cnt;
  2029. int dbg_force_in_the_gaps(void)
  2030. {
  2031. if (!dbg_force_in_the_gaps_enabled)
  2032. return 0;
  2033. /* Force in-the-gaps every 8th commit */
  2034. return !((invocation_cnt++) & 0x7);
  2035. }
  2036. /* Failure mode for recovery testing */
  2037. #define chance(n, d) (simple_rand() <= (n) * 32768LL / (d))
  2038. struct failure_mode_info {
  2039. struct list_head list;
  2040. struct ubifs_info *c;
  2041. };
  2042. static LIST_HEAD(fmi_list);
  2043. static DEFINE_SPINLOCK(fmi_lock);
  2044. static unsigned int next;
  2045. static int simple_rand(void)
  2046. {
  2047. if (next == 0)
  2048. next = current->pid;
  2049. next = next * 1103515245 + 12345;
  2050. return (next >> 16) & 32767;
  2051. }
  2052. static void failure_mode_init(struct ubifs_info *c)
  2053. {
  2054. struct failure_mode_info *fmi;
  2055. fmi = kmalloc(sizeof(struct failure_mode_info), GFP_NOFS);
  2056. if (!fmi) {
  2057. ubifs_err("Failed to register failure mode - no memory");
  2058. return;
  2059. }
  2060. fmi->c = c;
  2061. spin_lock(&fmi_lock);
  2062. list_add_tail(&fmi->list, &fmi_list);
  2063. spin_unlock(&fmi_lock);
  2064. }
  2065. static void failure_mode_exit(struct ubifs_info *c)
  2066. {
  2067. struct failure_mode_info *fmi, *tmp;
  2068. spin_lock(&fmi_lock);
  2069. list_for_each_entry_safe(fmi, tmp, &fmi_list, list)
  2070. if (fmi->c == c) {
  2071. list_del(&fmi->list);
  2072. kfree(fmi);
  2073. }
  2074. spin_unlock(&fmi_lock);
  2075. }
  2076. static struct ubifs_info *dbg_find_info(struct ubi_volume_desc *desc)
  2077. {
  2078. struct failure_mode_info *fmi;
  2079. spin_lock(&fmi_lock);
  2080. list_for_each_entry(fmi, &fmi_list, list)
  2081. if (fmi->c->ubi == desc) {
  2082. struct ubifs_info *c = fmi->c;
  2083. spin_unlock(&fmi_lock);
  2084. return c;
  2085. }
  2086. spin_unlock(&fmi_lock);
  2087. return NULL;
  2088. }
  2089. static int in_failure_mode(struct ubi_volume_desc *desc)
  2090. {
  2091. struct ubifs_info *c = dbg_find_info(desc);
  2092. if (c && dbg_failure_mode)
  2093. return c->dbg->failure_mode;
  2094. return 0;
  2095. }
  2096. static int do_fail(struct ubi_volume_desc *desc, int lnum, int write)
  2097. {
  2098. struct ubifs_info *c = dbg_find_info(desc);
  2099. struct ubifs_debug_info *d;
  2100. if (!c || !dbg_failure_mode)
  2101. return 0;
  2102. d = c->dbg;
  2103. if (d->failure_mode)
  2104. return 1;
  2105. if (!d->fail_cnt) {
  2106. /* First call - decide delay to failure */
  2107. if (chance(1, 2)) {
  2108. unsigned int delay = 1 << (simple_rand() >> 11);
  2109. if (chance(1, 2)) {
  2110. d->fail_delay = 1;
  2111. d->fail_timeout = jiffies +
  2112. msecs_to_jiffies(delay);
  2113. dbg_rcvry("failing after %ums", delay);
  2114. } else {
  2115. d->fail_delay = 2;
  2116. d->fail_cnt_max = delay;
  2117. dbg_rcvry("failing after %u calls", delay);
  2118. }
  2119. }
  2120. d->fail_cnt += 1;
  2121. }
  2122. /* Determine if failure delay has expired */
  2123. if (d->fail_delay == 1) {
  2124. if (time_before(jiffies, d->fail_timeout))
  2125. return 0;
  2126. } else if (d->fail_delay == 2)
  2127. if (d->fail_cnt++ < d->fail_cnt_max)
  2128. return 0;
  2129. if (lnum == UBIFS_SB_LNUM) {
  2130. if (write) {
  2131. if (chance(1, 2))
  2132. return 0;
  2133. } else if (chance(19, 20))
  2134. return 0;
  2135. dbg_rcvry("failing in super block LEB %d", lnum);
  2136. } else if (lnum == UBIFS_MST_LNUM || lnum == UBIFS_MST_LNUM + 1) {
  2137. if (chance(19, 20))
  2138. return 0;
  2139. dbg_rcvry("failing in master LEB %d", lnum);
  2140. } else if (lnum >= UBIFS_LOG_LNUM && lnum <= c->log_last) {
  2141. if (write) {
  2142. if (chance(99, 100))
  2143. return 0;
  2144. } else if (chance(399, 400))
  2145. return 0;
  2146. dbg_rcvry("failing in log LEB %d", lnum);
  2147. } else if (lnum >= c->lpt_first && lnum <= c->lpt_last) {
  2148. if (write) {
  2149. if (chance(7, 8))
  2150. return 0;
  2151. } else if (chance(19, 20))
  2152. return 0;
  2153. dbg_rcvry("failing in LPT LEB %d", lnum);
  2154. } else if (lnum >= c->orph_first && lnum <= c->orph_last) {
  2155. if (write) {
  2156. if (chance(1, 2))
  2157. return 0;
  2158. } else if (chance(9, 10))
  2159. return 0;
  2160. dbg_rcvry("failing in orphan LEB %d", lnum);
  2161. } else if (lnum == c->ihead_lnum) {
  2162. if (chance(99, 100))
  2163. return 0;
  2164. dbg_rcvry("failing in index head LEB %d", lnum);
  2165. } else if (c->jheads && lnum == c->jheads[GCHD].wbuf.lnum) {
  2166. if (chance(9, 10))
  2167. return 0;
  2168. dbg_rcvry("failing in GC head LEB %d", lnum);
  2169. } else if (write && !RB_EMPTY_ROOT(&c->buds) &&
  2170. !ubifs_search_bud(c, lnum)) {
  2171. if (chance(19, 20))
  2172. return 0;
  2173. dbg_rcvry("failing in non-bud LEB %d", lnum);
  2174. } else if (c->cmt_state == COMMIT_RUNNING_BACKGROUND ||
  2175. c->cmt_state == COMMIT_RUNNING_REQUIRED) {
  2176. if (chance(999, 1000))
  2177. return 0;
  2178. dbg_rcvry("failing in bud LEB %d commit running", lnum);
  2179. } else {
  2180. if (chance(9999, 10000))
  2181. return 0;
  2182. dbg_rcvry("failing in bud LEB %d commit not running", lnum);
  2183. }
  2184. ubifs_err("*** SETTING FAILURE MODE ON (LEB %d) ***", lnum);
  2185. d->failure_mode = 1;
  2186. dump_stack();
  2187. return 1;
  2188. }
  2189. static void cut_data(const void *buf, int len)
  2190. {
  2191. int flen, i;
  2192. unsigned char *p = (void *)buf;
  2193. flen = (len * (long long)simple_rand()) >> 15;
  2194. for (i = flen; i < len; i++)
  2195. p[i] = 0xff;
  2196. }
  2197. int dbg_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
  2198. int len, int check)
  2199. {
  2200. if (in_failure_mode(desc))
  2201. return -EIO;
  2202. return ubi_leb_read(desc, lnum, buf, offset, len, check);
  2203. }
  2204. int dbg_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
  2205. int offset, int len, int dtype)
  2206. {
  2207. int err, failing;
  2208. if (in_failure_mode(desc))
  2209. return -EIO;
  2210. failing = do_fail(desc, lnum, 1);
  2211. if (failing)
  2212. cut_data(buf, len);
  2213. err = ubi_leb_write(desc, lnum, buf, offset, len, dtype);
  2214. if (err)
  2215. return err;
  2216. if (failing)
  2217. return -EIO;
  2218. return 0;
  2219. }
  2220. int dbg_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
  2221. int len, int dtype)
  2222. {
  2223. int err;
  2224. if (do_fail(desc, lnum, 1))
  2225. return -EIO;
  2226. err = ubi_leb_change(desc, lnum, buf, len, dtype);
  2227. if (err)
  2228. return err;
  2229. if (do_fail(desc, lnum, 1))
  2230. return -EIO;
  2231. return 0;
  2232. }
  2233. int dbg_leb_erase(struct ubi_volume_desc *desc, int lnum)
  2234. {
  2235. int err;
  2236. if (do_fail(desc, lnum, 0))
  2237. return -EIO;
  2238. err = ubi_leb_erase(desc, lnum);
  2239. if (err)
  2240. return err;
  2241. if (do_fail(desc, lnum, 0))
  2242. return -EIO;
  2243. return 0;
  2244. }
  2245. int dbg_leb_unmap(struct ubi_volume_desc *desc, int lnum)
  2246. {
  2247. int err;
  2248. if (do_fail(desc, lnum, 0))
  2249. return -EIO;
  2250. err = ubi_leb_unmap(desc, lnum);
  2251. if (err)
  2252. return err;
  2253. if (do_fail(desc, lnum, 0))
  2254. return -EIO;
  2255. return 0;
  2256. }
  2257. int dbg_is_mapped(struct ubi_volume_desc *desc, int lnum)
  2258. {
  2259. if (in_failure_mode(desc))
  2260. return -EIO;
  2261. return ubi_is_mapped(desc, lnum);
  2262. }
  2263. int dbg_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype)
  2264. {
  2265. int err;
  2266. if (do_fail(desc, lnum, 0))
  2267. return -EIO;
  2268. err = ubi_leb_map(desc, lnum, dtype);
  2269. if (err)
  2270. return err;
  2271. if (do_fail(desc, lnum, 0))
  2272. return -EIO;
  2273. return 0;
  2274. }
  2275. /**
  2276. * ubifs_debugging_init - initialize UBIFS debugging.
  2277. * @c: UBIFS file-system description object
  2278. *
  2279. * This function initializes debugging-related data for the file system.
  2280. * Returns zero in case of success and a negative error code in case of
  2281. * failure.
  2282. */
  2283. int ubifs_debugging_init(struct ubifs_info *c)
  2284. {
  2285. c->dbg = kzalloc(sizeof(struct ubifs_debug_info), GFP_KERNEL);
  2286. if (!c->dbg)
  2287. return -ENOMEM;
  2288. c->dbg->buf = vmalloc(c->leb_size);
  2289. if (!c->dbg->buf)
  2290. goto out;
  2291. failure_mode_init(c);
  2292. return 0;
  2293. out:
  2294. kfree(c->dbg);
  2295. return -ENOMEM;
  2296. }
  2297. /**
  2298. * ubifs_debugging_exit - free debugging data.
  2299. * @c: UBIFS file-system description object
  2300. */
  2301. void ubifs_debugging_exit(struct ubifs_info *c)
  2302. {
  2303. failure_mode_exit(c);
  2304. vfree(c->dbg->buf);
  2305. kfree(c->dbg);
  2306. }
  2307. /*
  2308. * Root directory for UBIFS stuff in debugfs. Contains sub-directories which
  2309. * contain the stuff specific to particular file-system mounts.
  2310. */
  2311. static struct dentry *dfs_rootdir;
  2312. /**
  2313. * dbg_debugfs_init - initialize debugfs file-system.
  2314. *
  2315. * UBIFS uses debugfs file-system to expose various debugging knobs to
  2316. * user-space. This function creates "ubifs" directory in the debugfs
  2317. * file-system. Returns zero in case of success and a negative error code in
  2318. * case of failure.
  2319. */
  2320. int dbg_debugfs_init(void)
  2321. {
  2322. dfs_rootdir = debugfs_create_dir("ubifs", NULL);
  2323. if (IS_ERR(dfs_rootdir)) {
  2324. int err = PTR_ERR(dfs_rootdir);
  2325. ubifs_err("cannot create \"ubifs\" debugfs directory, "
  2326. "error %d\n", err);
  2327. return err;
  2328. }
  2329. return 0;
  2330. }
  2331. /**
  2332. * dbg_debugfs_exit - remove the "ubifs" directory from debugfs file-system.
  2333. */
  2334. void dbg_debugfs_exit(void)
  2335. {
  2336. debugfs_remove(dfs_rootdir);
  2337. }
  2338. static int open_debugfs_file(struct inode *inode, struct file *file)
  2339. {
  2340. file->private_data = inode->i_private;
  2341. return 0;
  2342. }
  2343. static ssize_t write_debugfs_file(struct file *file, const char __user *buf,
  2344. size_t count, loff_t *ppos)
  2345. {
  2346. struct ubifs_info *c = file->private_data;
  2347. struct ubifs_debug_info *d = c->dbg;
  2348. if (file->f_path.dentry == d->dfs_dump_lprops)
  2349. dbg_dump_lprops(c);
  2350. else if (file->f_path.dentry == d->dfs_dump_budg) {
  2351. spin_lock(&c->space_lock);
  2352. dbg_dump_budg(c);
  2353. spin_unlock(&c->space_lock);
  2354. } else if (file->f_path.dentry == d->dfs_dump_tnc) {
  2355. mutex_lock(&c->tnc_mutex);
  2356. dbg_dump_tnc(c);
  2357. mutex_unlock(&c->tnc_mutex);
  2358. } else
  2359. return -EINVAL;
  2360. *ppos += count;
  2361. return count;
  2362. }
  2363. static const struct file_operations dfs_fops = {
  2364. .open = open_debugfs_file,
  2365. .write = write_debugfs_file,
  2366. .owner = THIS_MODULE,
  2367. };
  2368. /**
  2369. * dbg_debugfs_init_fs - initialize debugfs for UBIFS instance.
  2370. * @c: UBIFS file-system description object
  2371. *
  2372. * This function creates all debugfs files for this instance of UBIFS. Returns
  2373. * zero in case of success and a negative error code in case of failure.
  2374. *
  2375. * Note, the only reason we have not merged this function with the
  2376. * 'ubifs_debugging_init()' function is because it is better to initialize
  2377. * debugfs interfaces at the very end of the mount process, and remove them at
  2378. * the very beginning of the mount process.
  2379. */
  2380. int dbg_debugfs_init_fs(struct ubifs_info *c)
  2381. {
  2382. int err;
  2383. const char *fname;
  2384. struct dentry *dent;
  2385. struct ubifs_debug_info *d = c->dbg;
  2386. sprintf(d->dfs_dir_name, "ubi%d_%d", c->vi.ubi_num, c->vi.vol_id);
  2387. d->dfs_dir = debugfs_create_dir(d->dfs_dir_name, dfs_rootdir);
  2388. if (IS_ERR(d->dfs_dir)) {
  2389. err = PTR_ERR(d->dfs_dir);
  2390. ubifs_err("cannot create \"%s\" debugfs directory, error %d\n",
  2391. d->dfs_dir_name, err);
  2392. goto out;
  2393. }
  2394. fname = "dump_lprops";
  2395. dent = debugfs_create_file(fname, S_IWUGO, d->dfs_dir, c, &dfs_fops);
  2396. if (IS_ERR(dent))
  2397. goto out_remove;
  2398. d->dfs_dump_lprops = dent;
  2399. fname = "dump_budg";
  2400. dent = debugfs_create_file(fname, S_IWUGO, d->dfs_dir, c, &dfs_fops);
  2401. if (IS_ERR(dent))
  2402. goto out_remove;
  2403. d->dfs_dump_budg = dent;
  2404. fname = "dump_tnc";
  2405. dent = debugfs_create_file(fname, S_IWUGO, d->dfs_dir, c, &dfs_fops);
  2406. if (IS_ERR(dent))
  2407. goto out_remove;
  2408. d->dfs_dump_tnc = dent;
  2409. return 0;
  2410. out_remove:
  2411. err = PTR_ERR(dent);
  2412. ubifs_err("cannot create \"%s\" debugfs directory, error %d\n",
  2413. fname, err);
  2414. debugfs_remove_recursive(d->dfs_dir);
  2415. out:
  2416. return err;
  2417. }
  2418. /**
  2419. * dbg_debugfs_exit_fs - remove all debugfs files.
  2420. * @c: UBIFS file-system description object
  2421. */
  2422. void dbg_debugfs_exit_fs(struct ubifs_info *c)
  2423. {
  2424. debugfs_remove_recursive(c->dbg->dfs_dir);
  2425. }
  2426. #endif /* CONFIG_UBIFS_FS_DEBUG */