debug.c 84 KB

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