debug.c 86 KB

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