debug.c 68 KB

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