debug.c 80 KB

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