debug.c 84 KB

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