tnc_commit.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  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. /* This file implements TNC functions for committing */
  11. #include <linux/random.h>
  12. #include "ubifs.h"
  13. /**
  14. * make_idx_node - make an index node for fill-the-gaps method of TNC commit.
  15. * @c: UBIFS file-system description object
  16. * @idx: buffer in which to place new index node
  17. * @znode: znode from which to make new index node
  18. * @lnum: LEB number where new index node will be written
  19. * @offs: offset where new index node will be written
  20. * @len: length of new index node
  21. */
  22. static int make_idx_node(struct ubifs_info *c, struct ubifs_idx_node *idx,
  23. struct ubifs_znode *znode, int lnum, int offs, int len)
  24. {
  25. struct ubifs_znode *zp;
  26. u8 hash[UBIFS_HASH_ARR_SZ];
  27. int i, err;
  28. /* Make index node */
  29. idx->ch.node_type = UBIFS_IDX_NODE;
  30. idx->child_cnt = cpu_to_le16(znode->child_cnt);
  31. idx->level = cpu_to_le16(znode->level);
  32. for (i = 0; i < znode->child_cnt; i++) {
  33. struct ubifs_branch *br = ubifs_idx_branch(c, idx, i);
  34. struct ubifs_zbranch *zbr = &znode->zbranch[i];
  35. key_write_idx(c, &zbr->key, &br->key);
  36. br->lnum = cpu_to_le32(zbr->lnum);
  37. br->offs = cpu_to_le32(zbr->offs);
  38. br->len = cpu_to_le32(zbr->len);
  39. ubifs_copy_hash(c, zbr->hash, ubifs_branch_hash(c, br));
  40. if (!zbr->lnum || !zbr->len) {
  41. ubifs_err(c, "bad ref in znode");
  42. ubifs_dump_znode(c, znode);
  43. if (zbr->znode)
  44. ubifs_dump_znode(c, zbr->znode);
  45. return -EINVAL;
  46. }
  47. }
  48. ubifs_prepare_node(c, idx, len, 0);
  49. ubifs_node_calc_hash(c, idx, hash);
  50. znode->lnum = lnum;
  51. znode->offs = offs;
  52. znode->len = len;
  53. err = insert_old_idx_znode(c, znode);
  54. /* Update the parent */
  55. zp = znode->parent;
  56. if (zp) {
  57. struct ubifs_zbranch *zbr;
  58. zbr = &zp->zbranch[znode->iip];
  59. zbr->lnum = lnum;
  60. zbr->offs = offs;
  61. zbr->len = len;
  62. ubifs_copy_hash(c, hash, zbr->hash);
  63. } else {
  64. c->zroot.lnum = lnum;
  65. c->zroot.offs = offs;
  66. c->zroot.len = len;
  67. ubifs_copy_hash(c, hash, c->zroot.hash);
  68. }
  69. c->calc_idx_sz += ALIGN(len, 8);
  70. atomic_long_dec(&c->dirty_zn_cnt);
  71. ubifs_assert(c, ubifs_zn_dirty(znode));
  72. ubifs_assert(c, ubifs_zn_cow(znode));
  73. /*
  74. * Note, unlike 'write_index()' we do not add memory barriers here
  75. * because this function is called with @c->tnc_mutex locked.
  76. */
  77. __clear_bit(DIRTY_ZNODE, &znode->flags);
  78. __clear_bit(COW_ZNODE, &znode->flags);
  79. return err;
  80. }
  81. /**
  82. * fill_gap - make index nodes in gaps in dirty index LEBs.
  83. * @c: UBIFS file-system description object
  84. * @lnum: LEB number that gap appears in
  85. * @gap_start: offset of start of gap
  86. * @gap_end: offset of end of gap
  87. * @dirt: adds dirty space to this
  88. *
  89. * This function returns the number of index nodes written into the gap.
  90. */
  91. static int fill_gap(struct ubifs_info *c, int lnum, int gap_start, int gap_end,
  92. int *dirt)
  93. {
  94. int len, gap_remains, gap_pos, written, pad_len;
  95. ubifs_assert(c, (gap_start & 7) == 0);
  96. ubifs_assert(c, (gap_end & 7) == 0);
  97. ubifs_assert(c, gap_end >= gap_start);
  98. gap_remains = gap_end - gap_start;
  99. if (!gap_remains)
  100. return 0;
  101. gap_pos = gap_start;
  102. written = 0;
  103. while (c->enext) {
  104. len = ubifs_idx_node_sz(c, c->enext->child_cnt);
  105. if (len < gap_remains) {
  106. struct ubifs_znode *znode = c->enext;
  107. const int alen = ALIGN(len, 8);
  108. int err;
  109. ubifs_assert(c, alen <= gap_remains);
  110. err = make_idx_node(c, c->ileb_buf + gap_pos, znode,
  111. lnum, gap_pos, len);
  112. if (err)
  113. return err;
  114. gap_remains -= alen;
  115. gap_pos += alen;
  116. c->enext = znode->cnext;
  117. if (c->enext == c->cnext)
  118. c->enext = NULL;
  119. written += 1;
  120. } else
  121. break;
  122. }
  123. if (gap_end == c->leb_size) {
  124. c->ileb_len = ALIGN(gap_pos, c->min_io_size);
  125. /* Pad to end of min_io_size */
  126. pad_len = c->ileb_len - gap_pos;
  127. } else
  128. /* Pad to end of gap */
  129. pad_len = gap_remains;
  130. dbg_gc("LEB %d:%d to %d len %d nodes written %d wasted bytes %d",
  131. lnum, gap_start, gap_end, gap_end - gap_start, written, pad_len);
  132. ubifs_pad(c, c->ileb_buf + gap_pos, pad_len);
  133. *dirt += pad_len;
  134. return written;
  135. }
  136. /**
  137. * find_old_idx - find an index node obsoleted since the last commit start.
  138. * @c: UBIFS file-system description object
  139. * @lnum: LEB number of obsoleted index node
  140. * @offs: offset of obsoleted index node
  141. *
  142. * Returns %1 if found and %0 otherwise.
  143. */
  144. static int find_old_idx(struct ubifs_info *c, int lnum, int offs)
  145. {
  146. struct ubifs_old_idx *o;
  147. struct rb_node *p;
  148. p = c->old_idx.rb_node;
  149. while (p) {
  150. o = rb_entry(p, struct ubifs_old_idx, rb);
  151. if (lnum < o->lnum)
  152. p = p->rb_left;
  153. else if (lnum > o->lnum)
  154. p = p->rb_right;
  155. else if (offs < o->offs)
  156. p = p->rb_left;
  157. else if (offs > o->offs)
  158. p = p->rb_right;
  159. else
  160. return 1;
  161. }
  162. return 0;
  163. }
  164. /**
  165. * is_idx_node_in_use - determine if an index node can be overwritten.
  166. * @c: UBIFS file-system description object
  167. * @key: key of index node
  168. * @level: index node level
  169. * @lnum: LEB number of index node
  170. * @offs: offset of index node
  171. *
  172. * If @key / @lnum / @offs identify an index node that was not part of the old
  173. * index, then this function returns %0 (obsolete). Else if the index node was
  174. * part of the old index but is now dirty %1 is returned, else if it is clean %2
  175. * is returned. A negative error code is returned on failure.
  176. */
  177. static int is_idx_node_in_use(struct ubifs_info *c, union ubifs_key *key,
  178. int level, int lnum, int offs)
  179. {
  180. int ret;
  181. ret = is_idx_node_in_tnc(c, key, level, lnum, offs);
  182. if (ret < 0)
  183. return ret; /* Error code */
  184. if (ret == 0)
  185. if (find_old_idx(c, lnum, offs))
  186. return 1;
  187. return ret;
  188. }
  189. /**
  190. * layout_leb_in_gaps - layout index nodes using in-the-gaps method.
  191. * @c: UBIFS file-system description object
  192. * @p: return LEB number in @c->gap_lebs[p]
  193. *
  194. * This function lays out new index nodes for dirty znodes using in-the-gaps
  195. * method of TNC commit.
  196. * This function merely puts the next znode into the next gap, making no attempt
  197. * to try to maximise the number of znodes that fit.
  198. * This function returns the number of index nodes written into the gaps, or a
  199. * negative error code on failure.
  200. */
  201. static int layout_leb_in_gaps(struct ubifs_info *c, int p)
  202. {
  203. struct ubifs_scan_leb *sleb;
  204. struct ubifs_scan_node *snod;
  205. int lnum, dirt = 0, gap_start, gap_end, err, written, tot_written;
  206. tot_written = 0;
  207. /* Get an index LEB with lots of obsolete index nodes */
  208. lnum = ubifs_find_dirty_idx_leb(c);
  209. if (lnum < 0)
  210. /*
  211. * There also may be dirt in the index head that could be
  212. * filled, however we do not check there at present.
  213. */
  214. return lnum; /* Error code */
  215. c->gap_lebs[p] = lnum;
  216. dbg_gc("LEB %d", lnum);
  217. /*
  218. * Scan the index LEB. We use the generic scan for this even though
  219. * it is more comprehensive and less efficient than is needed for this
  220. * purpose.
  221. */
  222. sleb = ubifs_scan(c, lnum, 0, c->ileb_buf, 0);
  223. c->ileb_len = 0;
  224. if (IS_ERR(sleb))
  225. return PTR_ERR(sleb);
  226. gap_start = 0;
  227. list_for_each_entry(snod, &sleb->nodes, list) {
  228. struct ubifs_idx_node *idx;
  229. int in_use, level;
  230. ubifs_assert(c, snod->type == UBIFS_IDX_NODE);
  231. idx = snod->node;
  232. key_read(c, ubifs_idx_key(c, idx), &snod->key);
  233. level = le16_to_cpu(idx->level);
  234. /* Determine if the index node is in use (not obsolete) */
  235. in_use = is_idx_node_in_use(c, &snod->key, level, lnum,
  236. snod->offs);
  237. if (in_use < 0) {
  238. ubifs_scan_destroy(sleb);
  239. return in_use; /* Error code */
  240. }
  241. if (in_use) {
  242. if (in_use == 1)
  243. dirt += ALIGN(snod->len, 8);
  244. /*
  245. * The obsolete index nodes form gaps that can be
  246. * overwritten. This gap has ended because we have
  247. * found an index node that is still in use
  248. * i.e. not obsolete
  249. */
  250. gap_end = snod->offs;
  251. /* Try to fill gap */
  252. written = fill_gap(c, lnum, gap_start, gap_end, &dirt);
  253. if (written < 0) {
  254. ubifs_scan_destroy(sleb);
  255. return written; /* Error code */
  256. }
  257. tot_written += written;
  258. gap_start = ALIGN(snod->offs + snod->len, 8);
  259. }
  260. }
  261. ubifs_scan_destroy(sleb);
  262. c->ileb_len = c->leb_size;
  263. gap_end = c->leb_size;
  264. /* Try to fill gap */
  265. written = fill_gap(c, lnum, gap_start, gap_end, &dirt);
  266. if (written < 0)
  267. return written; /* Error code */
  268. tot_written += written;
  269. if (tot_written == 0) {
  270. struct ubifs_lprops lp;
  271. dbg_gc("LEB %d wrote %d index nodes", lnum, tot_written);
  272. err = ubifs_read_one_lp(c, lnum, &lp);
  273. if (err)
  274. return err;
  275. if (lp.free == c->leb_size) {
  276. /*
  277. * We must have snatched this LEB from the idx_gc list
  278. * so we need to correct the free and dirty space.
  279. */
  280. err = ubifs_change_one_lp(c, lnum,
  281. c->leb_size - c->ileb_len,
  282. dirt, 0, 0, 0);
  283. if (err)
  284. return err;
  285. }
  286. return 0;
  287. }
  288. err = ubifs_change_one_lp(c, lnum, c->leb_size - c->ileb_len, dirt,
  289. 0, 0, 0);
  290. if (err)
  291. return err;
  292. err = ubifs_leb_change(c, lnum, c->ileb_buf, c->ileb_len);
  293. if (err)
  294. return err;
  295. dbg_gc("LEB %d wrote %d index nodes", lnum, tot_written);
  296. return tot_written;
  297. }
  298. /**
  299. * get_leb_cnt - calculate the number of empty LEBs needed to commit.
  300. * @c: UBIFS file-system description object
  301. * @cnt: number of znodes to commit
  302. *
  303. * This function returns the number of empty LEBs needed to commit @cnt znodes
  304. * to the current index head. The number is not exact and may be more than
  305. * needed.
  306. */
  307. static int get_leb_cnt(struct ubifs_info *c, int cnt)
  308. {
  309. int d;
  310. /* Assume maximum index node size (i.e. overestimate space needed) */
  311. cnt -= (c->leb_size - c->ihead_offs) / c->max_idx_node_sz;
  312. if (cnt < 0)
  313. cnt = 0;
  314. d = c->leb_size / c->max_idx_node_sz;
  315. return DIV_ROUND_UP(cnt, d);
  316. }
  317. /**
  318. * layout_in_gaps - in-the-gaps method of committing TNC.
  319. * @c: UBIFS file-system description object
  320. * @cnt: number of dirty znodes to commit.
  321. *
  322. * This function lays out new index nodes for dirty znodes using in-the-gaps
  323. * method of TNC commit.
  324. *
  325. * This function returns %0 on success and a negative error code on failure.
  326. */
  327. static int layout_in_gaps(struct ubifs_info *c, int cnt)
  328. {
  329. int err, leb_needed_cnt, written, p = 0, old_idx_lebs, *gap_lebs;
  330. dbg_gc("%d znodes to write", cnt);
  331. c->gap_lebs = kmalloc_array(c->lst.idx_lebs + 1, sizeof(int),
  332. GFP_NOFS);
  333. if (!c->gap_lebs)
  334. return -ENOMEM;
  335. old_idx_lebs = c->lst.idx_lebs;
  336. do {
  337. ubifs_assert(c, p < c->lst.idx_lebs);
  338. written = layout_leb_in_gaps(c, p);
  339. if (written < 0) {
  340. err = written;
  341. if (err != -ENOSPC) {
  342. kfree(c->gap_lebs);
  343. c->gap_lebs = NULL;
  344. return err;
  345. }
  346. if (!dbg_is_chk_index(c)) {
  347. /*
  348. * Do not print scary warnings if the debugging
  349. * option which forces in-the-gaps is enabled.
  350. */
  351. ubifs_warn(c, "out of space");
  352. ubifs_dump_budg(c, &c->bi);
  353. ubifs_dump_lprops(c);
  354. }
  355. /* Try to commit anyway */
  356. break;
  357. }
  358. p++;
  359. cnt -= written;
  360. leb_needed_cnt = get_leb_cnt(c, cnt);
  361. dbg_gc("%d znodes remaining, need %d LEBs, have %d", cnt,
  362. leb_needed_cnt, c->ileb_cnt);
  363. /*
  364. * Dynamically change the size of @c->gap_lebs to prevent
  365. * oob, because @c->lst.idx_lebs could be increased by
  366. * function @get_idx_gc_leb (called by layout_leb_in_gaps->
  367. * ubifs_find_dirty_idx_leb) during loop. Only enlarge
  368. * @c->gap_lebs when needed.
  369. *
  370. */
  371. if (leb_needed_cnt > c->ileb_cnt && p >= old_idx_lebs &&
  372. old_idx_lebs < c->lst.idx_lebs) {
  373. old_idx_lebs = c->lst.idx_lebs;
  374. gap_lebs = krealloc(c->gap_lebs, sizeof(int) *
  375. (old_idx_lebs + 1), GFP_NOFS);
  376. if (!gap_lebs) {
  377. kfree(c->gap_lebs);
  378. c->gap_lebs = NULL;
  379. return -ENOMEM;
  380. }
  381. c->gap_lebs = gap_lebs;
  382. }
  383. } while (leb_needed_cnt > c->ileb_cnt);
  384. c->gap_lebs[p] = -1;
  385. return 0;
  386. }
  387. /**
  388. * layout_in_empty_space - layout index nodes in empty space.
  389. * @c: UBIFS file-system description object
  390. *
  391. * This function lays out new index nodes for dirty znodes using empty LEBs.
  392. *
  393. * This function returns %0 on success and a negative error code on failure.
  394. */
  395. static int layout_in_empty_space(struct ubifs_info *c)
  396. {
  397. struct ubifs_znode *znode, *cnext, *zp;
  398. int lnum, offs, len, next_len, buf_len, buf_offs, used, avail;
  399. int wlen, blen, err;
  400. cnext = c->enext;
  401. if (!cnext)
  402. return 0;
  403. lnum = c->ihead_lnum;
  404. buf_offs = c->ihead_offs;
  405. buf_len = ubifs_idx_node_sz(c, c->fanout);
  406. buf_len = ALIGN(buf_len, c->min_io_size);
  407. used = 0;
  408. avail = buf_len;
  409. /* Ensure there is enough room for first write */
  410. next_len = ubifs_idx_node_sz(c, cnext->child_cnt);
  411. if (buf_offs + next_len > c->leb_size)
  412. lnum = -1;
  413. while (1) {
  414. znode = cnext;
  415. len = ubifs_idx_node_sz(c, znode->child_cnt);
  416. /* Determine the index node position */
  417. if (lnum == -1) {
  418. if (c->ileb_nxt >= c->ileb_cnt) {
  419. ubifs_err(c, "out of space");
  420. return -ENOSPC;
  421. }
  422. lnum = c->ilebs[c->ileb_nxt++];
  423. buf_offs = 0;
  424. used = 0;
  425. avail = buf_len;
  426. }
  427. offs = buf_offs + used;
  428. znode->lnum = lnum;
  429. znode->offs = offs;
  430. znode->len = len;
  431. /* Update the parent */
  432. zp = znode->parent;
  433. if (zp) {
  434. struct ubifs_zbranch *zbr;
  435. int i;
  436. i = znode->iip;
  437. zbr = &zp->zbranch[i];
  438. zbr->lnum = lnum;
  439. zbr->offs = offs;
  440. zbr->len = len;
  441. } else {
  442. c->zroot.lnum = lnum;
  443. c->zroot.offs = offs;
  444. c->zroot.len = len;
  445. }
  446. c->calc_idx_sz += ALIGN(len, 8);
  447. /*
  448. * Once lprops is updated, we can decrease the dirty znode count
  449. * but it is easier to just do it here.
  450. */
  451. atomic_long_dec(&c->dirty_zn_cnt);
  452. /*
  453. * Calculate the next index node length to see if there is
  454. * enough room for it
  455. */
  456. cnext = znode->cnext;
  457. if (cnext == c->cnext)
  458. next_len = 0;
  459. else
  460. next_len = ubifs_idx_node_sz(c, cnext->child_cnt);
  461. /* Update buffer positions */
  462. wlen = used + len;
  463. used += ALIGN(len, 8);
  464. avail -= ALIGN(len, 8);
  465. if (next_len != 0 &&
  466. buf_offs + used + next_len <= c->leb_size &&
  467. avail > 0)
  468. continue;
  469. if (avail <= 0 && next_len &&
  470. buf_offs + used + next_len <= c->leb_size)
  471. blen = buf_len;
  472. else
  473. blen = ALIGN(wlen, c->min_io_size);
  474. /* The buffer is full or there are no more znodes to do */
  475. buf_offs += blen;
  476. if (next_len) {
  477. if (buf_offs + next_len > c->leb_size) {
  478. err = ubifs_update_one_lp(c, lnum,
  479. c->leb_size - buf_offs, blen - used,
  480. 0, 0);
  481. if (err)
  482. return err;
  483. lnum = -1;
  484. }
  485. used -= blen;
  486. if (used < 0)
  487. used = 0;
  488. avail = buf_len - used;
  489. continue;
  490. }
  491. err = ubifs_update_one_lp(c, lnum, c->leb_size - buf_offs,
  492. blen - used, 0, 0);
  493. if (err)
  494. return err;
  495. break;
  496. }
  497. c->dbg->new_ihead_lnum = lnum;
  498. c->dbg->new_ihead_offs = buf_offs;
  499. return 0;
  500. }
  501. /**
  502. * layout_commit - determine positions of index nodes to commit.
  503. * @c: UBIFS file-system description object
  504. * @no_space: indicates that insufficient empty LEBs were allocated
  505. * @cnt: number of znodes to commit
  506. *
  507. * Calculate and update the positions of index nodes to commit. If there were
  508. * an insufficient number of empty LEBs allocated, then index nodes are placed
  509. * into the gaps created by obsolete index nodes in non-empty index LEBs. For
  510. * this purpose, an obsolete index node is one that was not in the index as at
  511. * the end of the last commit. To write "in-the-gaps" requires that those index
  512. * LEBs are updated atomically in-place.
  513. */
  514. static int layout_commit(struct ubifs_info *c, int no_space, int cnt)
  515. {
  516. int err;
  517. if (no_space) {
  518. err = layout_in_gaps(c, cnt);
  519. if (err)
  520. return err;
  521. }
  522. err = layout_in_empty_space(c);
  523. return err;
  524. }
  525. /**
  526. * find_first_dirty - find first dirty znode.
  527. * @znode: znode to begin searching from
  528. */
  529. static struct ubifs_znode *find_first_dirty(struct ubifs_znode *znode)
  530. {
  531. int i, cont;
  532. if (!znode)
  533. return NULL;
  534. while (1) {
  535. if (znode->level == 0) {
  536. if (ubifs_zn_dirty(znode))
  537. return znode;
  538. return NULL;
  539. }
  540. cont = 0;
  541. for (i = 0; i < znode->child_cnt; i++) {
  542. struct ubifs_zbranch *zbr = &znode->zbranch[i];
  543. if (zbr->znode && ubifs_zn_dirty(zbr->znode)) {
  544. znode = zbr->znode;
  545. cont = 1;
  546. break;
  547. }
  548. }
  549. if (!cont) {
  550. if (ubifs_zn_dirty(znode))
  551. return znode;
  552. return NULL;
  553. }
  554. }
  555. }
  556. /**
  557. * find_next_dirty - find next dirty znode.
  558. * @znode: znode to begin searching from
  559. */
  560. static struct ubifs_znode *find_next_dirty(struct ubifs_znode *znode)
  561. {
  562. int n = znode->iip + 1;
  563. znode = znode->parent;
  564. if (!znode)
  565. return NULL;
  566. for (; n < znode->child_cnt; n++) {
  567. struct ubifs_zbranch *zbr = &znode->zbranch[n];
  568. if (zbr->znode && ubifs_zn_dirty(zbr->znode))
  569. return find_first_dirty(zbr->znode);
  570. }
  571. return znode;
  572. }
  573. /**
  574. * get_znodes_to_commit - create list of dirty znodes to commit.
  575. * @c: UBIFS file-system description object
  576. *
  577. * This function returns the number of znodes to commit.
  578. */
  579. static int get_znodes_to_commit(struct ubifs_info *c)
  580. {
  581. struct ubifs_znode *znode, *cnext;
  582. int cnt = 0;
  583. c->cnext = find_first_dirty(c->zroot.znode);
  584. znode = c->enext = c->cnext;
  585. if (!znode) {
  586. dbg_cmt("no znodes to commit");
  587. return 0;
  588. }
  589. cnt += 1;
  590. while (1) {
  591. ubifs_assert(c, !ubifs_zn_cow(znode));
  592. __set_bit(COW_ZNODE, &znode->flags);
  593. znode->alt = 0;
  594. cnext = find_next_dirty(znode);
  595. if (!cnext) {
  596. ubifs_assert(c, !znode->parent);
  597. znode->cparent = NULL;
  598. znode->cnext = c->cnext;
  599. break;
  600. }
  601. znode->cparent = znode->parent;
  602. znode->ciip = znode->iip;
  603. znode->cnext = cnext;
  604. znode = cnext;
  605. cnt += 1;
  606. }
  607. dbg_cmt("committing %d znodes", cnt);
  608. ubifs_assert(c, cnt == atomic_long_read(&c->dirty_zn_cnt));
  609. return cnt;
  610. }
  611. /**
  612. * alloc_idx_lebs - allocate empty LEBs to be used to commit.
  613. * @c: UBIFS file-system description object
  614. * @cnt: number of znodes to commit
  615. *
  616. * This function returns %-ENOSPC if it cannot allocate a sufficient number of
  617. * empty LEBs. %0 is returned on success, otherwise a negative error code
  618. * is returned.
  619. */
  620. static int alloc_idx_lebs(struct ubifs_info *c, int cnt)
  621. {
  622. int i, leb_cnt, lnum;
  623. c->ileb_cnt = 0;
  624. c->ileb_nxt = 0;
  625. leb_cnt = get_leb_cnt(c, cnt);
  626. dbg_cmt("need about %d empty LEBS for TNC commit", leb_cnt);
  627. if (!leb_cnt)
  628. return 0;
  629. c->ilebs = kmalloc_array(leb_cnt, sizeof(int), GFP_NOFS);
  630. if (!c->ilebs)
  631. return -ENOMEM;
  632. for (i = 0; i < leb_cnt; i++) {
  633. lnum = ubifs_find_free_leb_for_idx(c);
  634. if (lnum < 0)
  635. return lnum;
  636. c->ilebs[c->ileb_cnt++] = lnum;
  637. dbg_cmt("LEB %d", lnum);
  638. }
  639. if (dbg_is_chk_index(c) && !get_random_u32_below(8))
  640. return -ENOSPC;
  641. return 0;
  642. }
  643. /**
  644. * free_unused_idx_lebs - free unused LEBs that were allocated for the commit.
  645. * @c: UBIFS file-system description object
  646. *
  647. * It is possible that we allocate more empty LEBs for the commit than we need.
  648. * This functions frees the surplus.
  649. *
  650. * This function returns %0 on success and a negative error code on failure.
  651. */
  652. static int free_unused_idx_lebs(struct ubifs_info *c)
  653. {
  654. int i, err = 0, lnum, er;
  655. for (i = c->ileb_nxt; i < c->ileb_cnt; i++) {
  656. lnum = c->ilebs[i];
  657. dbg_cmt("LEB %d", lnum);
  658. er = ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0,
  659. LPROPS_INDEX | LPROPS_TAKEN, 0);
  660. if (!err)
  661. err = er;
  662. }
  663. return err;
  664. }
  665. /**
  666. * free_idx_lebs - free unused LEBs after commit end.
  667. * @c: UBIFS file-system description object
  668. *
  669. * This function returns %0 on success and a negative error code on failure.
  670. */
  671. static int free_idx_lebs(struct ubifs_info *c)
  672. {
  673. int err;
  674. err = free_unused_idx_lebs(c);
  675. kfree(c->ilebs);
  676. c->ilebs = NULL;
  677. return err;
  678. }
  679. /**
  680. * ubifs_tnc_start_commit - start TNC commit.
  681. * @c: UBIFS file-system description object
  682. * @zroot: new index root position is returned here
  683. *
  684. * This function prepares the list of indexing nodes to commit and lays out
  685. * their positions on flash. If there is not enough free space it uses the
  686. * in-gap commit method. Returns zero in case of success and a negative error
  687. * code in case of failure.
  688. */
  689. int ubifs_tnc_start_commit(struct ubifs_info *c, struct ubifs_zbranch *zroot)
  690. {
  691. int err = 0, cnt;
  692. mutex_lock(&c->tnc_mutex);
  693. err = dbg_check_tnc(c, 1);
  694. if (err)
  695. goto out;
  696. cnt = get_znodes_to_commit(c);
  697. if (cnt != 0) {
  698. int no_space = 0;
  699. err = alloc_idx_lebs(c, cnt);
  700. if (err == -ENOSPC)
  701. no_space = 1;
  702. else if (err)
  703. goto out_free;
  704. err = layout_commit(c, no_space, cnt);
  705. if (err)
  706. goto out_free;
  707. ubifs_assert(c, atomic_long_read(&c->dirty_zn_cnt) == 0);
  708. err = free_unused_idx_lebs(c);
  709. if (err)
  710. goto out;
  711. }
  712. destroy_old_idx(c);
  713. memcpy(zroot, &c->zroot, sizeof(struct ubifs_zbranch));
  714. err = ubifs_save_dirty_idx_lnums(c);
  715. if (err)
  716. goto out;
  717. spin_lock(&c->space_lock);
  718. /*
  719. * Although we have not finished committing yet, update size of the
  720. * committed index ('c->bi.old_idx_sz') and zero out the index growth
  721. * budget. It is OK to do this now, because we've reserved all the
  722. * space which is needed to commit the index, and it is save for the
  723. * budgeting subsystem to assume the index is already committed,
  724. * even though it is not.
  725. */
  726. ubifs_assert(c, c->bi.min_idx_lebs == ubifs_calc_min_idx_lebs(c));
  727. c->bi.old_idx_sz = c->calc_idx_sz;
  728. c->bi.uncommitted_idx = 0;
  729. c->bi.min_idx_lebs = ubifs_calc_min_idx_lebs(c);
  730. spin_unlock(&c->space_lock);
  731. mutex_unlock(&c->tnc_mutex);
  732. dbg_cmt("number of index LEBs %d", c->lst.idx_lebs);
  733. dbg_cmt("size of index %llu", c->calc_idx_sz);
  734. return err;
  735. out_free:
  736. free_idx_lebs(c);
  737. out:
  738. mutex_unlock(&c->tnc_mutex);
  739. return err;
  740. }
  741. /**
  742. * write_index - write index nodes.
  743. * @c: UBIFS file-system description object
  744. *
  745. * This function writes the index nodes whose positions were laid out in the
  746. * layout_in_empty_space function.
  747. */
  748. static int write_index(struct ubifs_info *c)
  749. {
  750. struct ubifs_idx_node *idx;
  751. struct ubifs_znode *znode, *cnext;
  752. int i, lnum, offs, len, next_len, buf_len, buf_offs, used;
  753. int avail, wlen, err, lnum_pos = 0, blen, nxt_offs;
  754. cnext = c->enext;
  755. if (!cnext)
  756. return 0;
  757. /*
  758. * Always write index nodes to the index head so that index nodes and
  759. * other types of nodes are never mixed in the same erase block.
  760. */
  761. lnum = c->ihead_lnum;
  762. buf_offs = c->ihead_offs;
  763. /* Allocate commit buffer */
  764. buf_len = ALIGN(c->max_idx_node_sz, c->min_io_size);
  765. used = 0;
  766. avail = buf_len;
  767. /* Ensure there is enough room for first write */
  768. next_len = ubifs_idx_node_sz(c, cnext->child_cnt);
  769. if (buf_offs + next_len > c->leb_size) {
  770. err = ubifs_update_one_lp(c, lnum, LPROPS_NC, 0, 0,
  771. LPROPS_TAKEN);
  772. if (err)
  773. return err;
  774. lnum = -1;
  775. }
  776. while (1) {
  777. u8 hash[UBIFS_HASH_ARR_SZ];
  778. cond_resched();
  779. znode = cnext;
  780. idx = c->cbuf + used;
  781. /* Make index node */
  782. idx->ch.node_type = UBIFS_IDX_NODE;
  783. idx->child_cnt = cpu_to_le16(znode->child_cnt);
  784. idx->level = cpu_to_le16(znode->level);
  785. for (i = 0; i < znode->child_cnt; i++) {
  786. struct ubifs_branch *br = ubifs_idx_branch(c, idx, i);
  787. struct ubifs_zbranch *zbr = &znode->zbranch[i];
  788. key_write_idx(c, &zbr->key, &br->key);
  789. br->lnum = cpu_to_le32(zbr->lnum);
  790. br->offs = cpu_to_le32(zbr->offs);
  791. br->len = cpu_to_le32(zbr->len);
  792. ubifs_copy_hash(c, zbr->hash, ubifs_branch_hash(c, br));
  793. if (!zbr->lnum || !zbr->len) {
  794. ubifs_err(c, "bad ref in znode");
  795. ubifs_dump_znode(c, znode);
  796. if (zbr->znode)
  797. ubifs_dump_znode(c, zbr->znode);
  798. return -EINVAL;
  799. }
  800. }
  801. len = ubifs_idx_node_sz(c, znode->child_cnt);
  802. ubifs_prepare_node(c, idx, len, 0);
  803. ubifs_node_calc_hash(c, idx, hash);
  804. mutex_lock(&c->tnc_mutex);
  805. if (znode->cparent)
  806. ubifs_copy_hash(c, hash,
  807. znode->cparent->zbranch[znode->ciip].hash);
  808. if (znode->parent) {
  809. if (!ubifs_zn_obsolete(znode))
  810. ubifs_copy_hash(c, hash,
  811. znode->parent->zbranch[znode->iip].hash);
  812. } else {
  813. ubifs_copy_hash(c, hash, c->zroot.hash);
  814. }
  815. mutex_unlock(&c->tnc_mutex);
  816. /* Determine the index node position */
  817. if (lnum == -1) {
  818. lnum = c->ilebs[lnum_pos++];
  819. buf_offs = 0;
  820. used = 0;
  821. avail = buf_len;
  822. }
  823. offs = buf_offs + used;
  824. if (lnum != znode->lnum || offs != znode->offs ||
  825. len != znode->len) {
  826. ubifs_err(c, "inconsistent znode posn");
  827. return -EINVAL;
  828. }
  829. /* Grab some stuff from znode while we still can */
  830. cnext = znode->cnext;
  831. ubifs_assert(c, ubifs_zn_dirty(znode));
  832. ubifs_assert(c, ubifs_zn_cow(znode));
  833. /*
  834. * It is important that other threads should see %DIRTY_ZNODE
  835. * flag cleared before %COW_ZNODE. Specifically, it matters in
  836. * the 'dirty_cow_znode()' function. This is the reason for the
  837. * first barrier. Also, we want the bit changes to be seen to
  838. * other threads ASAP, to avoid unnecessary copying, which is
  839. * the reason for the second barrier.
  840. */
  841. clear_bit(DIRTY_ZNODE, &znode->flags);
  842. smp_mb__before_atomic();
  843. clear_bit(COW_ZNODE, &znode->flags);
  844. smp_mb__after_atomic();
  845. /*
  846. * We have marked the znode as clean but have not updated the
  847. * @c->clean_zn_cnt counter. If this znode becomes dirty again
  848. * before 'free_obsolete_znodes()' is called, then
  849. * @c->clean_zn_cnt will be decremented before it gets
  850. * incremented (resulting in 2 decrements for the same znode).
  851. * This means that @c->clean_zn_cnt may become negative for a
  852. * while.
  853. *
  854. * Q: why we cannot increment @c->clean_zn_cnt?
  855. * A: because we do not have the @c->tnc_mutex locked, and the
  856. * following code would be racy and buggy:
  857. *
  858. * if (!ubifs_zn_obsolete(znode)) {
  859. * atomic_long_inc(&c->clean_zn_cnt);
  860. * atomic_long_inc(&ubifs_clean_zn_cnt);
  861. * }
  862. *
  863. * Thus, we just delay the @c->clean_zn_cnt update until we
  864. * have the mutex locked.
  865. */
  866. /* Do not access znode from this point on */
  867. /* Update buffer positions */
  868. wlen = used + len;
  869. used += ALIGN(len, 8);
  870. avail -= ALIGN(len, 8);
  871. /*
  872. * Calculate the next index node length to see if there is
  873. * enough room for it
  874. */
  875. if (cnext == c->cnext)
  876. next_len = 0;
  877. else
  878. next_len = ubifs_idx_node_sz(c, cnext->child_cnt);
  879. nxt_offs = buf_offs + used + next_len;
  880. if (next_len && nxt_offs <= c->leb_size) {
  881. if (avail > 0)
  882. continue;
  883. else
  884. blen = buf_len;
  885. } else {
  886. wlen = ALIGN(wlen, 8);
  887. blen = ALIGN(wlen, c->min_io_size);
  888. ubifs_pad(c, c->cbuf + wlen, blen - wlen);
  889. }
  890. /* The buffer is full or there are no more znodes to do */
  891. err = ubifs_leb_write(c, lnum, c->cbuf, buf_offs, blen);
  892. if (err)
  893. return err;
  894. buf_offs += blen;
  895. if (next_len) {
  896. if (nxt_offs > c->leb_size) {
  897. err = ubifs_update_one_lp(c, lnum, LPROPS_NC, 0,
  898. 0, LPROPS_TAKEN);
  899. if (err)
  900. return err;
  901. lnum = -1;
  902. }
  903. used -= blen;
  904. if (used < 0)
  905. used = 0;
  906. avail = buf_len - used;
  907. memmove(c->cbuf, c->cbuf + blen, used);
  908. continue;
  909. }
  910. break;
  911. }
  912. if (lnum != c->dbg->new_ihead_lnum ||
  913. buf_offs != c->dbg->new_ihead_offs) {
  914. ubifs_err(c, "inconsistent ihead");
  915. return -EINVAL;
  916. }
  917. c->ihead_lnum = lnum;
  918. c->ihead_offs = buf_offs;
  919. return 0;
  920. }
  921. /**
  922. * free_obsolete_znodes - free obsolete znodes.
  923. * @c: UBIFS file-system description object
  924. *
  925. * At the end of commit end, obsolete znodes are freed.
  926. */
  927. static void free_obsolete_znodes(struct ubifs_info *c)
  928. {
  929. struct ubifs_znode *znode, *cnext;
  930. cnext = c->cnext;
  931. do {
  932. znode = cnext;
  933. cnext = znode->cnext;
  934. if (ubifs_zn_obsolete(znode))
  935. kfree(znode);
  936. else {
  937. znode->cnext = NULL;
  938. atomic_long_inc(&c->clean_zn_cnt);
  939. atomic_long_inc(&ubifs_clean_zn_cnt);
  940. }
  941. } while (cnext != c->cnext);
  942. }
  943. /**
  944. * return_gap_lebs - return LEBs used by the in-gap commit method.
  945. * @c: UBIFS file-system description object
  946. *
  947. * This function clears the "taken" flag for the LEBs which were used by the
  948. * "commit in-the-gaps" method.
  949. */
  950. static int return_gap_lebs(struct ubifs_info *c)
  951. {
  952. int *p, err;
  953. if (!c->gap_lebs)
  954. return 0;
  955. dbg_cmt("");
  956. for (p = c->gap_lebs; *p != -1; p++) {
  957. err = ubifs_change_one_lp(c, *p, LPROPS_NC, LPROPS_NC, 0,
  958. LPROPS_TAKEN, 0);
  959. if (err)
  960. return err;
  961. }
  962. kfree(c->gap_lebs);
  963. c->gap_lebs = NULL;
  964. return 0;
  965. }
  966. /**
  967. * ubifs_tnc_end_commit - update the TNC for commit end.
  968. * @c: UBIFS file-system description object
  969. *
  970. * Write the dirty znodes.
  971. */
  972. int ubifs_tnc_end_commit(struct ubifs_info *c)
  973. {
  974. int err;
  975. if (!c->cnext)
  976. return 0;
  977. err = return_gap_lebs(c);
  978. if (err)
  979. return err;
  980. err = write_index(c);
  981. if (err)
  982. return err;
  983. mutex_lock(&c->tnc_mutex);
  984. dbg_cmt("TNC height is %d", c->zroot.znode->level + 1);
  985. free_obsolete_znodes(c);
  986. c->cnext = NULL;
  987. kfree(c->ilebs);
  988. c->ilebs = NULL;
  989. mutex_unlock(&c->tnc_mutex);
  990. return 0;
  991. }