debug.c 71 KB

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