debug.c 72 KB

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