debug.c 69 KB

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