debug.c 82 KB

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