tnc_commit.c 27 KB

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