tnc_commit.c 27 KB

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