debug.c 82 KB

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