replay.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * This file is part of UBIFS.
  4. *
  5. * Copyright (C) 2006-2008 Nokia Corporation.
  6. *
  7. * Authors: Adrian Hunter
  8. * Artem Bityutskiy (Битюцкий Артём)
  9. */
  10. /*
  11. * This file contains journal replay code. It runs when the file-system is being
  12. * mounted and requires no locking.
  13. *
  14. * The larger is the journal, the longer it takes to scan it, so the longer it
  15. * takes to mount UBIFS. This is why the journal has limited size which may be
  16. * changed depending on the system requirements. But a larger journal gives
  17. * faster I/O speed because it writes the index less frequently. So this is a
  18. * trade-off. Also, the journal is indexed by the in-memory index (TNC), so the
  19. * larger is the journal, the more memory its index may consume.
  20. */
  21. #include "ubifs.h"
  22. #include <linux/list_sort.h>
  23. #include <crypto/hash.h>
  24. /**
  25. * struct replay_entry - replay list entry.
  26. * @lnum: logical eraseblock number of the node
  27. * @offs: node offset
  28. * @len: node length
  29. * @hash: node hash
  30. * @deletion: non-zero if this entry corresponds to a node deletion
  31. * @sqnum: node sequence number
  32. * @list: links the replay list
  33. * @key: node key
  34. * @nm: directory entry name
  35. * @old_size: truncation old size
  36. * @new_size: truncation new size
  37. *
  38. * The replay process first scans all buds and builds the replay list, then
  39. * sorts the replay list in nodes sequence number order, and then inserts all
  40. * the replay entries to the TNC.
  41. */
  42. struct replay_entry {
  43. int lnum;
  44. int offs;
  45. int len;
  46. u8 hash[UBIFS_HASH_ARR_SZ];
  47. unsigned int deletion:1;
  48. unsigned long long sqnum;
  49. struct list_head list;
  50. union ubifs_key key;
  51. union {
  52. struct fscrypt_name nm;
  53. struct {
  54. loff_t old_size;
  55. loff_t new_size;
  56. };
  57. };
  58. };
  59. /**
  60. * struct bud_entry - entry in the list of buds to replay.
  61. * @list: next bud in the list
  62. * @bud: bud description object
  63. * @sqnum: reference node sequence number
  64. * @free: free bytes in the bud
  65. * @dirty: dirty bytes in the bud
  66. */
  67. struct bud_entry {
  68. struct list_head list;
  69. struct ubifs_bud *bud;
  70. unsigned long long sqnum;
  71. int free;
  72. int dirty;
  73. };
  74. /**
  75. * set_bud_lprops - set free and dirty space used by a bud.
  76. * @c: UBIFS file-system description object
  77. * @b: bud entry which describes the bud
  78. *
  79. * This function makes sure the LEB properties of bud @b are set correctly
  80. * after the replay. Returns zero in case of success and a negative error code
  81. * in case of failure.
  82. */
  83. static int set_bud_lprops(struct ubifs_info *c, struct bud_entry *b)
  84. {
  85. const struct ubifs_lprops *lp;
  86. int err = 0, dirty;
  87. ubifs_get_lprops(c);
  88. lp = ubifs_lpt_lookup_dirty(c, b->bud->lnum);
  89. if (IS_ERR(lp)) {
  90. err = PTR_ERR(lp);
  91. goto out;
  92. }
  93. dirty = lp->dirty;
  94. if (b->bud->start == 0 && (lp->free != c->leb_size || lp->dirty != 0)) {
  95. /*
  96. * The LEB was added to the journal with a starting offset of
  97. * zero which means the LEB must have been empty. The LEB
  98. * property values should be @lp->free == @c->leb_size and
  99. * @lp->dirty == 0, but that is not the case. The reason is that
  100. * the LEB had been garbage collected before it became the bud,
  101. * and there was no commit in between. The garbage collector
  102. * resets the free and dirty space without recording it
  103. * anywhere except lprops, so if there was no commit then
  104. * lprops does not have that information.
  105. *
  106. * We do not need to adjust free space because the scan has told
  107. * us the exact value which is recorded in the replay entry as
  108. * @b->free.
  109. *
  110. * However we do need to subtract from the dirty space the
  111. * amount of space that the garbage collector reclaimed, which
  112. * is the whole LEB minus the amount of space that was free.
  113. */
  114. dbg_mnt("bud LEB %d was GC'd (%d free, %d dirty)", b->bud->lnum,
  115. lp->free, lp->dirty);
  116. dbg_gc("bud LEB %d was GC'd (%d free, %d dirty)", b->bud->lnum,
  117. lp->free, lp->dirty);
  118. dirty -= c->leb_size - lp->free;
  119. /*
  120. * If the replay order was perfect the dirty space would now be
  121. * zero. The order is not perfect because the journal heads
  122. * race with each other. This is not a problem but is does mean
  123. * that the dirty space may temporarily exceed c->leb_size
  124. * during the replay.
  125. */
  126. if (dirty != 0)
  127. dbg_mnt("LEB %d lp: %d free %d dirty replay: %d free %d dirty",
  128. b->bud->lnum, lp->free, lp->dirty, b->free,
  129. b->dirty);
  130. }
  131. lp = ubifs_change_lp(c, lp, b->free, dirty + b->dirty,
  132. lp->flags | LPROPS_TAKEN, 0);
  133. if (IS_ERR(lp)) {
  134. err = PTR_ERR(lp);
  135. goto out;
  136. }
  137. /* Make sure the journal head points to the latest bud */
  138. err = ubifs_wbuf_seek_nolock(&c->jheads[b->bud->jhead].wbuf,
  139. b->bud->lnum, c->leb_size - b->free);
  140. out:
  141. ubifs_release_lprops(c);
  142. return err;
  143. }
  144. /**
  145. * set_buds_lprops - set free and dirty space for all replayed buds.
  146. * @c: UBIFS file-system description object
  147. *
  148. * This function sets LEB properties for all replayed buds. Returns zero in
  149. * case of success and a negative error code in case of failure.
  150. */
  151. static int set_buds_lprops(struct ubifs_info *c)
  152. {
  153. struct bud_entry *b;
  154. int err;
  155. list_for_each_entry(b, &c->replay_buds, list) {
  156. err = set_bud_lprops(c, b);
  157. if (err)
  158. return err;
  159. }
  160. return 0;
  161. }
  162. /**
  163. * trun_remove_range - apply a replay entry for a truncation to the TNC.
  164. * @c: UBIFS file-system description object
  165. * @r: replay entry of truncation
  166. */
  167. static int trun_remove_range(struct ubifs_info *c, struct replay_entry *r)
  168. {
  169. unsigned min_blk, max_blk;
  170. union ubifs_key min_key, max_key;
  171. ino_t ino;
  172. min_blk = r->new_size / UBIFS_BLOCK_SIZE;
  173. if (r->new_size & (UBIFS_BLOCK_SIZE - 1))
  174. min_blk += 1;
  175. max_blk = r->old_size / UBIFS_BLOCK_SIZE;
  176. if ((r->old_size & (UBIFS_BLOCK_SIZE - 1)) == 0)
  177. max_blk -= 1;
  178. ino = key_inum(c, &r->key);
  179. data_key_init(c, &min_key, ino, min_blk);
  180. data_key_init(c, &max_key, ino, max_blk);
  181. return ubifs_tnc_remove_range(c, &min_key, &max_key);
  182. }
  183. /**
  184. * inode_still_linked - check whether inode in question will be re-linked.
  185. * @c: UBIFS file-system description object
  186. * @rino: replay entry to test
  187. *
  188. * O_TMPFILE files can be re-linked, this means link count goes from 0 to 1.
  189. * This case needs special care, otherwise all references to the inode will
  190. * be removed upon the first replay entry of an inode with link count 0
  191. * is found.
  192. */
  193. static bool inode_still_linked(struct ubifs_info *c, struct replay_entry *rino)
  194. {
  195. struct replay_entry *r;
  196. ubifs_assert(c, rino->deletion);
  197. ubifs_assert(c, key_type(c, &rino->key) == UBIFS_INO_KEY);
  198. /*
  199. * Find the most recent entry for the inode behind @rino and check
  200. * whether it is a deletion.
  201. */
  202. list_for_each_entry_reverse(r, &c->replay_list, list) {
  203. ubifs_assert(c, r->sqnum >= rino->sqnum);
  204. if (key_inum(c, &r->key) == key_inum(c, &rino->key) &&
  205. key_type(c, &r->key) == UBIFS_INO_KEY)
  206. return r->deletion == 0;
  207. }
  208. ubifs_assert(c, 0);
  209. return false;
  210. }
  211. /**
  212. * apply_replay_entry - apply a replay entry to the TNC.
  213. * @c: UBIFS file-system description object
  214. * @r: replay entry to apply
  215. *
  216. * Apply a replay entry to the TNC.
  217. */
  218. static int apply_replay_entry(struct ubifs_info *c, struct replay_entry *r)
  219. {
  220. int err;
  221. dbg_mntk(&r->key, "LEB %d:%d len %d deletion %d sqnum %llu key ",
  222. r->lnum, r->offs, r->len, r->deletion, r->sqnum);
  223. if (is_hash_key(c, &r->key)) {
  224. if (r->deletion)
  225. err = ubifs_tnc_remove_nm(c, &r->key, &r->nm);
  226. else
  227. err = ubifs_tnc_add_nm(c, &r->key, r->lnum, r->offs,
  228. r->len, r->hash, &r->nm);
  229. } else {
  230. if (r->deletion)
  231. switch (key_type(c, &r->key)) {
  232. case UBIFS_INO_KEY:
  233. {
  234. ino_t inum = key_inum(c, &r->key);
  235. if (inode_still_linked(c, r)) {
  236. err = 0;
  237. break;
  238. }
  239. err = ubifs_tnc_remove_ino(c, inum);
  240. break;
  241. }
  242. case UBIFS_TRUN_KEY:
  243. err = trun_remove_range(c, r);
  244. break;
  245. default:
  246. err = ubifs_tnc_remove(c, &r->key);
  247. break;
  248. }
  249. else
  250. err = ubifs_tnc_add(c, &r->key, r->lnum, r->offs,
  251. r->len, r->hash);
  252. if (err)
  253. return err;
  254. if (c->need_recovery)
  255. err = ubifs_recover_size_accum(c, &r->key, r->deletion,
  256. r->new_size);
  257. }
  258. return err;
  259. }
  260. /**
  261. * replay_entries_cmp - compare 2 replay entries.
  262. * @priv: UBIFS file-system description object
  263. * @a: first replay entry
  264. * @b: second replay entry
  265. *
  266. * This is a comparios function for 'list_sort()' which compares 2 replay
  267. * entries @a and @b by comparing their sequence number. Returns %1 if @a has
  268. * greater sequence number and %-1 otherwise.
  269. */
  270. static int replay_entries_cmp(void *priv, const struct list_head *a,
  271. const struct list_head *b)
  272. {
  273. struct ubifs_info *c = priv;
  274. struct replay_entry *ra, *rb;
  275. cond_resched();
  276. if (a == b)
  277. return 0;
  278. ra = list_entry(a, struct replay_entry, list);
  279. rb = list_entry(b, struct replay_entry, list);
  280. ubifs_assert(c, ra->sqnum != rb->sqnum);
  281. if (ra->sqnum > rb->sqnum)
  282. return 1;
  283. return -1;
  284. }
  285. /**
  286. * apply_replay_list - apply the replay list to the TNC.
  287. * @c: UBIFS file-system description object
  288. *
  289. * Apply all entries in the replay list to the TNC. Returns zero in case of
  290. * success and a negative error code in case of failure.
  291. */
  292. static int apply_replay_list(struct ubifs_info *c)
  293. {
  294. struct replay_entry *r;
  295. int err;
  296. list_sort(c, &c->replay_list, &replay_entries_cmp);
  297. list_for_each_entry(r, &c->replay_list, list) {
  298. cond_resched();
  299. err = apply_replay_entry(c, r);
  300. if (err)
  301. return err;
  302. }
  303. return 0;
  304. }
  305. /**
  306. * destroy_replay_list - destroy the replay.
  307. * @c: UBIFS file-system description object
  308. *
  309. * Destroy the replay list.
  310. */
  311. static void destroy_replay_list(struct ubifs_info *c)
  312. {
  313. struct replay_entry *r, *tmp;
  314. list_for_each_entry_safe(r, tmp, &c->replay_list, list) {
  315. if (is_hash_key(c, &r->key))
  316. kfree(fname_name(&r->nm));
  317. list_del(&r->list);
  318. kfree(r);
  319. }
  320. }
  321. /**
  322. * insert_node - insert a node to the replay list
  323. * @c: UBIFS file-system description object
  324. * @lnum: node logical eraseblock number
  325. * @offs: node offset
  326. * @len: node length
  327. * @hash: node hash
  328. * @key: node key
  329. * @sqnum: sequence number
  330. * @deletion: non-zero if this is a deletion
  331. * @used: number of bytes in use in a LEB
  332. * @old_size: truncation old size
  333. * @new_size: truncation new size
  334. *
  335. * This function inserts a scanned non-direntry node to the replay list. The
  336. * replay list contains @struct replay_entry elements, and we sort this list in
  337. * sequence number order before applying it. The replay list is applied at the
  338. * very end of the replay process. Since the list is sorted in sequence number
  339. * order, the older modifications are applied first. This function returns zero
  340. * in case of success and a negative error code in case of failure.
  341. */
  342. static int insert_node(struct ubifs_info *c, int lnum, int offs, int len,
  343. const u8 *hash, union ubifs_key *key,
  344. unsigned long long sqnum, int deletion, int *used,
  345. loff_t old_size, loff_t new_size)
  346. {
  347. struct replay_entry *r;
  348. dbg_mntk(key, "add LEB %d:%d, key ", lnum, offs);
  349. if (key_inum(c, key) >= c->highest_inum)
  350. c->highest_inum = key_inum(c, key);
  351. r = kzalloc_obj(struct replay_entry);
  352. if (!r)
  353. return -ENOMEM;
  354. if (!deletion)
  355. *used += ALIGN(len, 8);
  356. r->lnum = lnum;
  357. r->offs = offs;
  358. r->len = len;
  359. ubifs_copy_hash(c, hash, r->hash);
  360. r->deletion = !!deletion;
  361. r->sqnum = sqnum;
  362. key_copy(c, key, &r->key);
  363. r->old_size = old_size;
  364. r->new_size = new_size;
  365. list_add_tail(&r->list, &c->replay_list);
  366. return 0;
  367. }
  368. /**
  369. * insert_dent - insert a directory entry node into the replay list.
  370. * @c: UBIFS file-system description object
  371. * @lnum: node logical eraseblock number
  372. * @offs: node offset
  373. * @len: node length
  374. * @hash: node hash
  375. * @key: node key
  376. * @name: directory entry name
  377. * @nlen: directory entry name length
  378. * @sqnum: sequence number
  379. * @deletion: non-zero if this is a deletion
  380. * @used: number of bytes in use in a LEB
  381. *
  382. * This function inserts a scanned directory entry node or an extended
  383. * attribute entry to the replay list. Returns zero in case of success and a
  384. * negative error code in case of failure.
  385. */
  386. static int insert_dent(struct ubifs_info *c, int lnum, int offs, int len,
  387. const u8 *hash, union ubifs_key *key,
  388. const char *name, int nlen, unsigned long long sqnum,
  389. int deletion, int *used)
  390. {
  391. struct replay_entry *r;
  392. char *nbuf;
  393. dbg_mntk(key, "add LEB %d:%d, key ", lnum, offs);
  394. if (key_inum(c, key) >= c->highest_inum)
  395. c->highest_inum = key_inum(c, key);
  396. r = kzalloc_obj(struct replay_entry);
  397. if (!r)
  398. return -ENOMEM;
  399. nbuf = kmalloc(nlen + 1, GFP_KERNEL);
  400. if (!nbuf) {
  401. kfree(r);
  402. return -ENOMEM;
  403. }
  404. if (!deletion)
  405. *used += ALIGN(len, 8);
  406. r->lnum = lnum;
  407. r->offs = offs;
  408. r->len = len;
  409. ubifs_copy_hash(c, hash, r->hash);
  410. r->deletion = !!deletion;
  411. r->sqnum = sqnum;
  412. key_copy(c, key, &r->key);
  413. fname_len(&r->nm) = nlen;
  414. memcpy(nbuf, name, nlen);
  415. nbuf[nlen] = '\0';
  416. fname_name(&r->nm) = nbuf;
  417. list_add_tail(&r->list, &c->replay_list);
  418. return 0;
  419. }
  420. /**
  421. * ubifs_validate_entry - validate directory or extended attribute entry node.
  422. * @c: UBIFS file-system description object
  423. * @dent: the node to validate
  424. *
  425. * This function validates directory or extended attribute entry node @dent.
  426. * Returns zero if the node is all right and a %-EINVAL if not.
  427. */
  428. int ubifs_validate_entry(struct ubifs_info *c,
  429. const struct ubifs_dent_node *dent)
  430. {
  431. int key_type = key_type_flash(c, dent->key);
  432. int nlen = le16_to_cpu(dent->nlen);
  433. if (le32_to_cpu(dent->ch.len) != nlen + UBIFS_DENT_NODE_SZ + 1 ||
  434. dent->type >= UBIFS_ITYPES_CNT ||
  435. nlen > UBIFS_MAX_NLEN || dent->name[nlen] != 0 ||
  436. (key_type == UBIFS_XENT_KEY && strnlen(dent->name, nlen) != nlen) ||
  437. le64_to_cpu(dent->inum) > MAX_INUM) {
  438. ubifs_err(c, "bad %s node", key_type == UBIFS_DENT_KEY ?
  439. "directory entry" : "extended attribute entry");
  440. return -EINVAL;
  441. }
  442. if (key_type != UBIFS_DENT_KEY && key_type != UBIFS_XENT_KEY) {
  443. ubifs_err(c, "bad key type %d", key_type);
  444. return -EINVAL;
  445. }
  446. return 0;
  447. }
  448. /**
  449. * is_last_bud - check if the bud is the last in the journal head.
  450. * @c: UBIFS file-system description object
  451. * @bud: bud description object
  452. *
  453. * This function checks if bud @bud is the last bud in its journal head. This
  454. * information is then used by 'replay_bud()' to decide whether the bud can
  455. * have corruptions or not. Indeed, only last buds can be corrupted by power
  456. * cuts. Returns %1 if this is the last bud, and %0 if not.
  457. */
  458. static int is_last_bud(struct ubifs_info *c, struct ubifs_bud *bud)
  459. {
  460. struct ubifs_jhead *jh = &c->jheads[bud->jhead];
  461. struct ubifs_bud *next;
  462. uint32_t data;
  463. int err;
  464. if (list_is_last(&bud->list, &jh->buds_list))
  465. return 1;
  466. /*
  467. * The following is a quirk to make sure we work correctly with UBIFS
  468. * images used with older UBIFS.
  469. *
  470. * Normally, the last bud will be the last in the journal head's list
  471. * of bud. However, there is one exception if the UBIFS image belongs
  472. * to older UBIFS. This is fairly unlikely: one would need to use old
  473. * UBIFS, then have a power cut exactly at the right point, and then
  474. * try to mount this image with new UBIFS.
  475. *
  476. * The exception is: it is possible to have 2 buds A and B, A goes
  477. * before B, and B is the last, bud B is contains no data, and bud A is
  478. * corrupted at the end. The reason is that in older versions when the
  479. * journal code switched the next bud (from A to B), it first added a
  480. * log reference node for the new bud (B), and only after this it
  481. * synchronized the write-buffer of current bud (A). But later this was
  482. * changed and UBIFS started to always synchronize the write-buffer of
  483. * the bud (A) before writing the log reference for the new bud (B).
  484. *
  485. * But because older UBIFS always synchronized A's write-buffer before
  486. * writing to B, we can recognize this exceptional situation but
  487. * checking the contents of bud B - if it is empty, then A can be
  488. * treated as the last and we can recover it.
  489. *
  490. * TODO: remove this piece of code in a couple of years (today it is
  491. * 16.05.2011).
  492. */
  493. next = list_entry(bud->list.next, struct ubifs_bud, list);
  494. if (!list_is_last(&next->list, &jh->buds_list))
  495. return 0;
  496. err = ubifs_leb_read(c, next->lnum, (char *)&data, next->start, 4, 1);
  497. if (err)
  498. return 0;
  499. return data == 0xFFFFFFFF;
  500. }
  501. /* authenticate_sleb_hash is split out for stack usage */
  502. static int noinline_for_stack
  503. authenticate_sleb_hash(struct ubifs_info *c,
  504. struct shash_desc *log_hash, u8 *hash)
  505. {
  506. SHASH_DESC_ON_STACK(hash_desc, c->hash_tfm);
  507. hash_desc->tfm = c->hash_tfm;
  508. ubifs_shash_copy_state(c, log_hash, hash_desc);
  509. return crypto_shash_final(hash_desc, hash);
  510. }
  511. /**
  512. * authenticate_sleb - authenticate one scan LEB
  513. * @c: UBIFS file-system description object
  514. * @sleb: the scan LEB to authenticate
  515. * @log_hash:
  516. * @is_last: if true, this is the last LEB
  517. *
  518. * This function iterates over the buds of a single LEB authenticating all buds
  519. * with the authentication nodes on this LEB. Authentication nodes are written
  520. * after some buds and contain a HMAC covering the authentication node itself
  521. * and the buds between the last authentication node and the current
  522. * authentication node. It can happen that the last buds cannot be authenticated
  523. * because a powercut happened when some nodes were written but not the
  524. * corresponding authentication node. This function returns the number of nodes
  525. * that could be authenticated or a negative error code.
  526. */
  527. static int authenticate_sleb(struct ubifs_info *c, struct ubifs_scan_leb *sleb,
  528. struct shash_desc *log_hash, int is_last)
  529. {
  530. int n_not_auth = 0;
  531. struct ubifs_scan_node *snod;
  532. int n_nodes = 0;
  533. int err;
  534. u8 hash[UBIFS_HASH_ARR_SZ];
  535. u8 hmac[UBIFS_HMAC_ARR_SZ];
  536. if (!ubifs_authenticated(c))
  537. return sleb->nodes_cnt;
  538. list_for_each_entry(snod, &sleb->nodes, list) {
  539. n_nodes++;
  540. if (snod->type == UBIFS_AUTH_NODE) {
  541. struct ubifs_auth_node *auth = snod->node;
  542. err = authenticate_sleb_hash(c, log_hash, hash);
  543. if (err)
  544. goto out;
  545. err = crypto_shash_tfm_digest(c->hmac_tfm, hash,
  546. c->hash_len, hmac);
  547. if (err)
  548. goto out;
  549. err = ubifs_check_hmac(c, auth->hmac, hmac);
  550. if (err) {
  551. err = -EPERM;
  552. goto out;
  553. }
  554. n_not_auth = 0;
  555. } else {
  556. err = crypto_shash_update(log_hash, snod->node,
  557. snod->len);
  558. if (err)
  559. goto out;
  560. n_not_auth++;
  561. }
  562. }
  563. /*
  564. * A powercut can happen when some nodes were written, but not yet
  565. * the corresponding authentication node. This may only happen on
  566. * the last bud though.
  567. */
  568. if (n_not_auth) {
  569. if (is_last) {
  570. dbg_mnt("%d unauthenticated nodes found on LEB %d, Ignoring them",
  571. n_not_auth, sleb->lnum);
  572. err = 0;
  573. } else {
  574. dbg_mnt("%d unauthenticated nodes found on non-last LEB %d",
  575. n_not_auth, sleb->lnum);
  576. err = -EPERM;
  577. }
  578. } else {
  579. err = 0;
  580. }
  581. out:
  582. return err ? err : n_nodes - n_not_auth;
  583. }
  584. /**
  585. * replay_bud - replay a bud logical eraseblock.
  586. * @c: UBIFS file-system description object
  587. * @b: bud entry which describes the bud
  588. *
  589. * This function replays bud @bud, recovers it if needed, and adds all nodes
  590. * from this bud to the replay list. Returns zero in case of success and a
  591. * negative error code in case of failure.
  592. */
  593. static int replay_bud(struct ubifs_info *c, struct bud_entry *b)
  594. {
  595. int is_last = is_last_bud(c, b->bud);
  596. int err = 0, used = 0, lnum = b->bud->lnum, offs = b->bud->start;
  597. int n_nodes, n = 0;
  598. struct ubifs_scan_leb *sleb;
  599. struct ubifs_scan_node *snod;
  600. dbg_mnt("replay bud LEB %d, head %d, offs %d, is_last %d",
  601. lnum, b->bud->jhead, offs, is_last);
  602. if (c->need_recovery && is_last)
  603. /*
  604. * Recover only last LEBs in the journal heads, because power
  605. * cuts may cause corruptions only in these LEBs, because only
  606. * these LEBs could possibly be written to at the power cut
  607. * time.
  608. */
  609. sleb = ubifs_recover_leb(c, lnum, offs, c->sbuf, b->bud->jhead);
  610. else
  611. sleb = ubifs_scan(c, lnum, offs, c->sbuf, 0);
  612. if (IS_ERR(sleb))
  613. return PTR_ERR(sleb);
  614. n_nodes = authenticate_sleb(c, sleb, b->bud->log_hash, is_last);
  615. if (n_nodes < 0) {
  616. err = n_nodes;
  617. goto out;
  618. }
  619. ubifs_shash_copy_state(c, b->bud->log_hash,
  620. c->jheads[b->bud->jhead].log_hash);
  621. /*
  622. * The bud does not have to start from offset zero - the beginning of
  623. * the 'lnum' LEB may contain previously committed data. One of the
  624. * things we have to do in replay is to correctly update lprops with
  625. * newer information about this LEB.
  626. *
  627. * At this point lprops thinks that this LEB has 'c->leb_size - offs'
  628. * bytes of free space because it only contain information about
  629. * committed data.
  630. *
  631. * But we know that real amount of free space is 'c->leb_size -
  632. * sleb->endpt', and the space in the 'lnum' LEB between 'offs' and
  633. * 'sleb->endpt' is used by bud data. We have to correctly calculate
  634. * how much of these data are dirty and update lprops with this
  635. * information.
  636. *
  637. * The dirt in that LEB region is comprised of padding nodes, deletion
  638. * nodes, truncation nodes and nodes which are obsoleted by subsequent
  639. * nodes in this LEB. So instead of calculating clean space, we
  640. * calculate used space ('used' variable).
  641. */
  642. list_for_each_entry(snod, &sleb->nodes, list) {
  643. u8 hash[UBIFS_HASH_ARR_SZ];
  644. int deletion = 0;
  645. cond_resched();
  646. if (snod->sqnum >= SQNUM_WATERMARK) {
  647. ubifs_err(c, "file system's life ended");
  648. goto out_dump;
  649. }
  650. ubifs_node_calc_hash(c, snod->node, hash);
  651. if (snod->sqnum > c->max_sqnum)
  652. c->max_sqnum = snod->sqnum;
  653. switch (snod->type) {
  654. case UBIFS_INO_NODE:
  655. {
  656. struct ubifs_ino_node *ino = snod->node;
  657. loff_t new_size = le64_to_cpu(ino->size);
  658. if (le32_to_cpu(ino->nlink) == 0)
  659. deletion = 1;
  660. err = insert_node(c, lnum, snod->offs, snod->len, hash,
  661. &snod->key, snod->sqnum, deletion,
  662. &used, 0, new_size);
  663. break;
  664. }
  665. case UBIFS_DATA_NODE:
  666. {
  667. struct ubifs_data_node *dn = snod->node;
  668. loff_t new_size = le32_to_cpu(dn->size) +
  669. key_block(c, &snod->key) *
  670. UBIFS_BLOCK_SIZE;
  671. err = insert_node(c, lnum, snod->offs, snod->len, hash,
  672. &snod->key, snod->sqnum, deletion,
  673. &used, 0, new_size);
  674. break;
  675. }
  676. case UBIFS_DENT_NODE:
  677. case UBIFS_XENT_NODE:
  678. {
  679. struct ubifs_dent_node *dent = snod->node;
  680. err = ubifs_validate_entry(c, dent);
  681. if (err)
  682. goto out_dump;
  683. err = insert_dent(c, lnum, snod->offs, snod->len, hash,
  684. &snod->key, dent->name,
  685. le16_to_cpu(dent->nlen), snod->sqnum,
  686. !le64_to_cpu(dent->inum), &used);
  687. break;
  688. }
  689. case UBIFS_TRUN_NODE:
  690. {
  691. struct ubifs_trun_node *trun = snod->node;
  692. loff_t old_size = le64_to_cpu(trun->old_size);
  693. loff_t new_size = le64_to_cpu(trun->new_size);
  694. union ubifs_key key;
  695. /* Validate truncation node */
  696. if (old_size < 0 || old_size > c->max_inode_sz ||
  697. new_size < 0 || new_size > c->max_inode_sz ||
  698. old_size <= new_size) {
  699. ubifs_err(c, "bad truncation node");
  700. goto out_dump;
  701. }
  702. /*
  703. * Create a fake truncation key just to use the same
  704. * functions which expect nodes to have keys.
  705. */
  706. trun_key_init(c, &key, le32_to_cpu(trun->inum));
  707. err = insert_node(c, lnum, snod->offs, snod->len, hash,
  708. &key, snod->sqnum, 1, &used,
  709. old_size, new_size);
  710. break;
  711. }
  712. case UBIFS_AUTH_NODE:
  713. break;
  714. default:
  715. ubifs_err(c, "unexpected node type %d in bud LEB %d:%d",
  716. snod->type, lnum, snod->offs);
  717. err = -EINVAL;
  718. goto out_dump;
  719. }
  720. if (err)
  721. goto out;
  722. n++;
  723. if (n == n_nodes)
  724. break;
  725. }
  726. ubifs_assert(c, ubifs_search_bud(c, lnum));
  727. ubifs_assert(c, sleb->endpt - offs >= used);
  728. ubifs_assert(c, sleb->endpt % c->min_io_size == 0);
  729. b->dirty = sleb->endpt - offs - used;
  730. b->free = c->leb_size - sleb->endpt;
  731. dbg_mnt("bud LEB %d replied: dirty %d, free %d",
  732. lnum, b->dirty, b->free);
  733. out:
  734. ubifs_scan_destroy(sleb);
  735. return err;
  736. out_dump:
  737. ubifs_err(c, "bad node is at LEB %d:%d", lnum, snod->offs);
  738. ubifs_dump_node(c, snod->node, c->leb_size - snod->offs);
  739. ubifs_scan_destroy(sleb);
  740. return -EINVAL;
  741. }
  742. /**
  743. * replay_buds - replay all buds.
  744. * @c: UBIFS file-system description object
  745. *
  746. * This function returns zero in case of success and a negative error code in
  747. * case of failure.
  748. */
  749. static int replay_buds(struct ubifs_info *c)
  750. {
  751. struct bud_entry *b;
  752. int err;
  753. unsigned long long prev_sqnum = 0;
  754. list_for_each_entry(b, &c->replay_buds, list) {
  755. err = replay_bud(c, b);
  756. if (err)
  757. return err;
  758. ubifs_assert(c, b->sqnum > prev_sqnum);
  759. prev_sqnum = b->sqnum;
  760. }
  761. return 0;
  762. }
  763. /**
  764. * destroy_bud_list - destroy the list of buds to replay.
  765. * @c: UBIFS file-system description object
  766. */
  767. static void destroy_bud_list(struct ubifs_info *c)
  768. {
  769. struct bud_entry *b;
  770. while (!list_empty(&c->replay_buds)) {
  771. b = list_entry(c->replay_buds.next, struct bud_entry, list);
  772. list_del(&b->list);
  773. kfree(b);
  774. }
  775. }
  776. /**
  777. * add_replay_bud - add a bud to the list of buds to replay.
  778. * @c: UBIFS file-system description object
  779. * @lnum: bud logical eraseblock number to replay
  780. * @offs: bud start offset
  781. * @jhead: journal head to which this bud belongs
  782. * @sqnum: reference node sequence number
  783. *
  784. * This function returns zero in case of success and a negative error code in
  785. * case of failure.
  786. */
  787. static int add_replay_bud(struct ubifs_info *c, int lnum, int offs, int jhead,
  788. unsigned long long sqnum)
  789. {
  790. struct ubifs_bud *bud;
  791. struct bud_entry *b;
  792. int err;
  793. dbg_mnt("add replay bud LEB %d:%d, head %d", lnum, offs, jhead);
  794. bud = kmalloc_obj(struct ubifs_bud);
  795. if (!bud)
  796. return -ENOMEM;
  797. b = kmalloc_obj(struct bud_entry);
  798. if (!b) {
  799. err = -ENOMEM;
  800. goto out;
  801. }
  802. bud->lnum = lnum;
  803. bud->start = offs;
  804. bud->jhead = jhead;
  805. bud->log_hash = ubifs_hash_get_desc(c);
  806. if (IS_ERR(bud->log_hash)) {
  807. err = PTR_ERR(bud->log_hash);
  808. goto out;
  809. }
  810. ubifs_shash_copy_state(c, c->log_hash, bud->log_hash);
  811. ubifs_add_bud(c, bud);
  812. b->bud = bud;
  813. b->sqnum = sqnum;
  814. list_add_tail(&b->list, &c->replay_buds);
  815. return 0;
  816. out:
  817. kfree(bud);
  818. kfree(b);
  819. return err;
  820. }
  821. /**
  822. * validate_ref - validate a reference node.
  823. * @c: UBIFS file-system description object
  824. * @ref: the reference node to validate
  825. *
  826. * This function returns %1 if a bud reference already exists for the LEB. %0 is
  827. * returned if the reference node is new, otherwise %-EINVAL is returned if
  828. * validation failed.
  829. */
  830. static int validate_ref(struct ubifs_info *c, const struct ubifs_ref_node *ref)
  831. {
  832. struct ubifs_bud *bud;
  833. int lnum = le32_to_cpu(ref->lnum);
  834. unsigned int offs = le32_to_cpu(ref->offs);
  835. unsigned int jhead = le32_to_cpu(ref->jhead);
  836. /*
  837. * ref->offs may point to the end of LEB when the journal head points
  838. * to the end of LEB and we write reference node for it during commit.
  839. * So this is why we require 'offs > c->leb_size'.
  840. */
  841. if (jhead >= c->jhead_cnt || lnum >= c->leb_cnt ||
  842. lnum < c->main_first || offs > c->leb_size ||
  843. offs & (c->min_io_size - 1))
  844. return -EINVAL;
  845. /* Make sure we have not already looked at this bud */
  846. bud = ubifs_search_bud(c, lnum);
  847. if (bud) {
  848. if (bud->jhead == jhead && bud->start <= offs)
  849. return 1;
  850. ubifs_err(c, "bud at LEB %d:%d was already referred", lnum, offs);
  851. return -EINVAL;
  852. }
  853. return 0;
  854. }
  855. /**
  856. * replay_log_leb - replay a log logical eraseblock.
  857. * @c: UBIFS file-system description object
  858. * @lnum: log logical eraseblock to replay
  859. * @offs: offset to start replaying from
  860. * @sbuf: scan buffer
  861. *
  862. * This function replays a log LEB and returns zero in case of success, %1 if
  863. * this is the last LEB in the log, and a negative error code in case of
  864. * failure.
  865. */
  866. static int replay_log_leb(struct ubifs_info *c, int lnum, int offs, void *sbuf)
  867. {
  868. int err;
  869. struct ubifs_scan_leb *sleb;
  870. struct ubifs_scan_node *snod;
  871. const struct ubifs_cs_node *node;
  872. dbg_mnt("replay log LEB %d:%d", lnum, offs);
  873. sleb = ubifs_scan(c, lnum, offs, sbuf, c->need_recovery);
  874. if (IS_ERR(sleb)) {
  875. if (PTR_ERR(sleb) != -EUCLEAN || !c->need_recovery)
  876. return PTR_ERR(sleb);
  877. /*
  878. * Note, the below function will recover this log LEB only if
  879. * it is the last, because unclean reboots can possibly corrupt
  880. * only the tail of the log.
  881. */
  882. sleb = ubifs_recover_log_leb(c, lnum, offs, sbuf);
  883. if (IS_ERR(sleb))
  884. return PTR_ERR(sleb);
  885. }
  886. if (sleb->nodes_cnt == 0) {
  887. err = 1;
  888. goto out;
  889. }
  890. node = sleb->buf;
  891. snod = list_entry(sleb->nodes.next, struct ubifs_scan_node, list);
  892. if (c->cs_sqnum == 0) {
  893. /*
  894. * This is the first log LEB we are looking at, make sure that
  895. * the first node is a commit start node. Also record its
  896. * sequence number so that UBIFS can determine where the log
  897. * ends, because all nodes which were have higher sequence
  898. * numbers.
  899. */
  900. if (snod->type != UBIFS_CS_NODE) {
  901. ubifs_err(c, "first log node at LEB %d:%d is not CS node",
  902. lnum, offs);
  903. goto out_dump;
  904. }
  905. if (le64_to_cpu(node->cmt_no) != c->cmt_no) {
  906. ubifs_err(c, "first CS node at LEB %d:%d has wrong commit number %llu expected %llu",
  907. lnum, offs,
  908. (unsigned long long)le64_to_cpu(node->cmt_no),
  909. c->cmt_no);
  910. goto out_dump;
  911. }
  912. c->cs_sqnum = le64_to_cpu(node->ch.sqnum);
  913. dbg_mnt("commit start sqnum %llu", c->cs_sqnum);
  914. err = ubifs_shash_init(c, c->log_hash);
  915. if (err)
  916. goto out;
  917. err = ubifs_shash_update(c, c->log_hash, node, UBIFS_CS_NODE_SZ);
  918. if (err < 0)
  919. goto out;
  920. }
  921. if (snod->sqnum < c->cs_sqnum) {
  922. /*
  923. * This means that we reached end of log and now
  924. * look to the older log data, which was already
  925. * committed but the eraseblock was not erased (UBIFS
  926. * only un-maps it). So this basically means we have to
  927. * exit with "end of log" code.
  928. */
  929. err = 1;
  930. goto out;
  931. }
  932. /* Make sure the first node sits at offset zero of the LEB */
  933. if (snod->offs != 0) {
  934. ubifs_err(c, "first node is not at zero offset");
  935. goto out_dump;
  936. }
  937. list_for_each_entry(snod, &sleb->nodes, list) {
  938. cond_resched();
  939. if (snod->sqnum >= SQNUM_WATERMARK) {
  940. ubifs_err(c, "file system's life ended");
  941. goto out_dump;
  942. }
  943. if (snod->sqnum < c->cs_sqnum) {
  944. ubifs_err(c, "bad sqnum %llu, commit sqnum %llu",
  945. snod->sqnum, c->cs_sqnum);
  946. goto out_dump;
  947. }
  948. if (snod->sqnum > c->max_sqnum)
  949. c->max_sqnum = snod->sqnum;
  950. switch (snod->type) {
  951. case UBIFS_REF_NODE: {
  952. const struct ubifs_ref_node *ref = snod->node;
  953. err = validate_ref(c, ref);
  954. if (err == 1)
  955. break; /* Already have this bud */
  956. if (err)
  957. goto out_dump;
  958. err = ubifs_shash_update(c, c->log_hash, ref,
  959. UBIFS_REF_NODE_SZ);
  960. if (err)
  961. goto out;
  962. err = add_replay_bud(c, le32_to_cpu(ref->lnum),
  963. le32_to_cpu(ref->offs),
  964. le32_to_cpu(ref->jhead),
  965. snod->sqnum);
  966. if (err)
  967. goto out;
  968. break;
  969. }
  970. case UBIFS_CS_NODE:
  971. /* Make sure it sits at the beginning of LEB */
  972. if (snod->offs != 0) {
  973. ubifs_err(c, "unexpected node in log");
  974. goto out_dump;
  975. }
  976. break;
  977. default:
  978. ubifs_err(c, "unexpected node in log");
  979. goto out_dump;
  980. }
  981. }
  982. if (sleb->endpt || c->lhead_offs >= c->leb_size) {
  983. c->lhead_lnum = lnum;
  984. c->lhead_offs = sleb->endpt;
  985. }
  986. err = !sleb->endpt;
  987. out:
  988. ubifs_scan_destroy(sleb);
  989. return err;
  990. out_dump:
  991. ubifs_err(c, "log error detected while replaying the log at LEB %d:%d",
  992. lnum, offs + snod->offs);
  993. ubifs_dump_node(c, snod->node, c->leb_size - snod->offs);
  994. ubifs_scan_destroy(sleb);
  995. return -EINVAL;
  996. }
  997. /**
  998. * take_ihead - update the status of the index head in lprops to 'taken'.
  999. * @c: UBIFS file-system description object
  1000. *
  1001. * This function returns the amount of free space in the index head LEB or a
  1002. * negative error code.
  1003. */
  1004. static int take_ihead(struct ubifs_info *c)
  1005. {
  1006. const struct ubifs_lprops *lp;
  1007. int err, free;
  1008. ubifs_get_lprops(c);
  1009. lp = ubifs_lpt_lookup_dirty(c, c->ihead_lnum);
  1010. if (IS_ERR(lp)) {
  1011. err = PTR_ERR(lp);
  1012. goto out;
  1013. }
  1014. free = lp->free;
  1015. lp = ubifs_change_lp(c, lp, LPROPS_NC, LPROPS_NC,
  1016. lp->flags | LPROPS_TAKEN, 0);
  1017. if (IS_ERR(lp)) {
  1018. err = PTR_ERR(lp);
  1019. goto out;
  1020. }
  1021. err = free;
  1022. out:
  1023. ubifs_release_lprops(c);
  1024. return err;
  1025. }
  1026. /**
  1027. * ubifs_replay_journal - replay journal.
  1028. * @c: UBIFS file-system description object
  1029. *
  1030. * This function scans the journal, replays and cleans it up. It makes sure all
  1031. * memory data structures related to uncommitted journal are built (dirty TNC
  1032. * tree, tree of buds, modified lprops, etc).
  1033. */
  1034. int ubifs_replay_journal(struct ubifs_info *c)
  1035. {
  1036. int err, lnum, free;
  1037. BUILD_BUG_ON(UBIFS_TRUN_KEY > 5);
  1038. /* Update the status of the index head in lprops to 'taken' */
  1039. free = take_ihead(c);
  1040. if (free < 0)
  1041. return free; /* Error code */
  1042. if (c->ihead_offs != c->leb_size - free) {
  1043. ubifs_err(c, "bad index head LEB %d:%d", c->ihead_lnum,
  1044. c->ihead_offs);
  1045. return -EINVAL;
  1046. }
  1047. dbg_mnt("start replaying the journal");
  1048. c->replaying = 1;
  1049. lnum = c->ltail_lnum = c->lhead_lnum;
  1050. do {
  1051. err = replay_log_leb(c, lnum, 0, c->sbuf);
  1052. if (err == 1) {
  1053. if (lnum != c->lhead_lnum)
  1054. /* We hit the end of the log */
  1055. break;
  1056. /*
  1057. * The head of the log must always start with the
  1058. * "commit start" node on a properly formatted UBIFS.
  1059. * But we found no nodes at all, which means that
  1060. * something went wrong and we cannot proceed mounting
  1061. * the file-system.
  1062. */
  1063. ubifs_err(c, "no UBIFS nodes found at the log head LEB %d:%d, possibly corrupted",
  1064. lnum, 0);
  1065. err = -EINVAL;
  1066. }
  1067. if (err)
  1068. goto out;
  1069. lnum = ubifs_next_log_lnum(c, lnum);
  1070. } while (lnum != c->ltail_lnum);
  1071. err = replay_buds(c);
  1072. if (err)
  1073. goto out;
  1074. err = apply_replay_list(c);
  1075. if (err)
  1076. goto out;
  1077. err = set_buds_lprops(c);
  1078. if (err)
  1079. goto out;
  1080. /*
  1081. * UBIFS budgeting calculations use @c->bi.uncommitted_idx variable
  1082. * to roughly estimate index growth. Things like @c->bi.min_idx_lebs
  1083. * depend on it. This means we have to initialize it to make sure
  1084. * budgeting works properly.
  1085. */
  1086. c->bi.uncommitted_idx = atomic_long_read(&c->dirty_zn_cnt);
  1087. c->bi.uncommitted_idx *= c->max_idx_node_sz;
  1088. ubifs_assert(c, c->bud_bytes <= c->max_bud_bytes || c->need_recovery);
  1089. dbg_mnt("finished, log head LEB %d:%d, max_sqnum %llu, highest_inum %lu",
  1090. c->lhead_lnum, c->lhead_offs, c->max_sqnum,
  1091. (unsigned long)c->highest_inum);
  1092. out:
  1093. destroy_replay_list(c);
  1094. destroy_bud_list(c);
  1095. c->replaying = 0;
  1096. return err;
  1097. }