tnc_commit.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  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(c, "bad ref in znode");
  52. ubifs_dump_znode(c, znode);
  53. if (zbr->znode)
  54. ubifs_dump_znode(c, zbr->znode);
  55. return -EINVAL;
  56. }
  57. }
  58. ubifs_prepare_node(c, idx, len, 0);
  59. znode->lnum = lnum;
  60. znode->offs = offs;
  61. znode->len = len;
  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. 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 + 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(c, "out of space");
  358. ubifs_dump_budg(c, &c->bi);
  359. ubifs_dump_lprops(c);
  360. }
  361. /* Try to commit anyway */
  362. break;
  363. }
  364. p++;
  365. cnt -= written;
  366. leb_needed_cnt = get_leb_cnt(c, cnt);
  367. dbg_gc("%d znodes remaining, need %d LEBs, have %d", cnt,
  368. leb_needed_cnt, c->ileb_cnt);
  369. } while (leb_needed_cnt > c->ileb_cnt);
  370. *p = -1;
  371. return 0;
  372. }
  373. /**
  374. * layout_in_empty_space - layout index nodes in empty space.
  375. * @c: UBIFS file-system description object
  376. *
  377. * This function lays out new index nodes for dirty znodes using empty LEBs.
  378. *
  379. * This function returns %0 on success and a negative error code on failure.
  380. */
  381. static int layout_in_empty_space(struct ubifs_info *c)
  382. {
  383. struct ubifs_znode *znode, *cnext, *zp;
  384. int lnum, offs, len, next_len, buf_len, buf_offs, used, avail;
  385. int wlen, blen, err;
  386. cnext = c->enext;
  387. if (!cnext)
  388. return 0;
  389. lnum = c->ihead_lnum;
  390. buf_offs = c->ihead_offs;
  391. buf_len = ubifs_idx_node_sz(c, c->fanout);
  392. buf_len = ALIGN(buf_len, c->min_io_size);
  393. used = 0;
  394. avail = buf_len;
  395. /* Ensure there is enough room for first write */
  396. next_len = ubifs_idx_node_sz(c, cnext->child_cnt);
  397. if (buf_offs + next_len > c->leb_size)
  398. lnum = -1;
  399. while (1) {
  400. znode = cnext;
  401. len = ubifs_idx_node_sz(c, znode->child_cnt);
  402. /* Determine the index node position */
  403. if (lnum == -1) {
  404. if (c->ileb_nxt >= c->ileb_cnt) {
  405. ubifs_err(c, "out of space");
  406. return -ENOSPC;
  407. }
  408. lnum = c->ilebs[c->ileb_nxt++];
  409. buf_offs = 0;
  410. used = 0;
  411. avail = buf_len;
  412. }
  413. offs = buf_offs + used;
  414. znode->lnum = lnum;
  415. znode->offs = offs;
  416. znode->len = len;
  417. /* Update the parent */
  418. zp = znode->parent;
  419. if (zp) {
  420. struct ubifs_zbranch *zbr;
  421. int i;
  422. i = znode->iip;
  423. zbr = &zp->zbranch[i];
  424. zbr->lnum = lnum;
  425. zbr->offs = offs;
  426. zbr->len = len;
  427. } else {
  428. c->zroot.lnum = lnum;
  429. c->zroot.offs = offs;
  430. c->zroot.len = len;
  431. }
  432. c->calc_idx_sz += ALIGN(len, 8);
  433. /*
  434. * Once lprops is updated, we can decrease the dirty znode count
  435. * but it is easier to just do it here.
  436. */
  437. atomic_long_dec(&c->dirty_zn_cnt);
  438. /*
  439. * Calculate the next index node length to see if there is
  440. * enough room for it
  441. */
  442. cnext = znode->cnext;
  443. if (cnext == c->cnext)
  444. next_len = 0;
  445. else
  446. next_len = ubifs_idx_node_sz(c, cnext->child_cnt);
  447. /* Update buffer positions */
  448. wlen = used + len;
  449. used += ALIGN(len, 8);
  450. avail -= ALIGN(len, 8);
  451. if (next_len != 0 &&
  452. buf_offs + used + next_len <= c->leb_size &&
  453. avail > 0)
  454. continue;
  455. if (avail <= 0 && next_len &&
  456. buf_offs + used + next_len <= c->leb_size)
  457. blen = buf_len;
  458. else
  459. blen = ALIGN(wlen, c->min_io_size);
  460. /* The buffer is full or there are no more znodes to do */
  461. buf_offs += blen;
  462. if (next_len) {
  463. if (buf_offs + next_len > c->leb_size) {
  464. err = ubifs_update_one_lp(c, lnum,
  465. c->leb_size - buf_offs, blen - used,
  466. 0, 0);
  467. if (err)
  468. return err;
  469. lnum = -1;
  470. }
  471. used -= blen;
  472. if (used < 0)
  473. used = 0;
  474. avail = buf_len - used;
  475. continue;
  476. }
  477. err = ubifs_update_one_lp(c, lnum, c->leb_size - buf_offs,
  478. blen - used, 0, 0);
  479. if (err)
  480. return err;
  481. break;
  482. }
  483. c->dbg->new_ihead_lnum = lnum;
  484. c->dbg->new_ihead_offs = buf_offs;
  485. return 0;
  486. }
  487. /**
  488. * layout_commit - determine positions of index nodes to commit.
  489. * @c: UBIFS file-system description object
  490. * @no_space: indicates that insufficient empty LEBs were allocated
  491. * @cnt: number of znodes to commit
  492. *
  493. * Calculate and update the positions of index nodes to commit. If there were
  494. * an insufficient number of empty LEBs allocated, then index nodes are placed
  495. * into the gaps created by obsolete index nodes in non-empty index LEBs. For
  496. * this purpose, an obsolete index node is one that was not in the index as at
  497. * the end of the last commit. To write "in-the-gaps" requires that those index
  498. * LEBs are updated atomically in-place.
  499. */
  500. static int layout_commit(struct ubifs_info *c, int no_space, int cnt)
  501. {
  502. int err;
  503. if (no_space) {
  504. err = layout_in_gaps(c, cnt);
  505. if (err)
  506. return err;
  507. }
  508. err = layout_in_empty_space(c);
  509. return err;
  510. }
  511. /**
  512. * find_first_dirty - find first dirty znode.
  513. * @znode: znode to begin searching from
  514. */
  515. static struct ubifs_znode *find_first_dirty(struct ubifs_znode *znode)
  516. {
  517. int i, cont;
  518. if (!znode)
  519. return NULL;
  520. while (1) {
  521. if (znode->level == 0) {
  522. if (ubifs_zn_dirty(znode))
  523. return znode;
  524. return NULL;
  525. }
  526. cont = 0;
  527. for (i = 0; i < znode->child_cnt; i++) {
  528. struct ubifs_zbranch *zbr = &znode->zbranch[i];
  529. if (zbr->znode && ubifs_zn_dirty(zbr->znode)) {
  530. znode = zbr->znode;
  531. cont = 1;
  532. break;
  533. }
  534. }
  535. if (!cont) {
  536. if (ubifs_zn_dirty(znode))
  537. return znode;
  538. return NULL;
  539. }
  540. }
  541. }
  542. /**
  543. * find_next_dirty - find next dirty znode.
  544. * @znode: znode to begin searching from
  545. */
  546. static struct ubifs_znode *find_next_dirty(struct ubifs_znode *znode)
  547. {
  548. int n = znode->iip + 1;
  549. znode = znode->parent;
  550. if (!znode)
  551. return NULL;
  552. for (; n < znode->child_cnt; n++) {
  553. struct ubifs_zbranch *zbr = &znode->zbranch[n];
  554. if (zbr->znode && ubifs_zn_dirty(zbr->znode))
  555. return find_first_dirty(zbr->znode);
  556. }
  557. return znode;
  558. }
  559. /**
  560. * get_znodes_to_commit - create list of dirty znodes to commit.
  561. * @c: UBIFS file-system description object
  562. *
  563. * This function returns the number of znodes to commit.
  564. */
  565. static int get_znodes_to_commit(struct ubifs_info *c)
  566. {
  567. struct ubifs_znode *znode, *cnext;
  568. int cnt = 0;
  569. c->cnext = find_first_dirty(c->zroot.znode);
  570. znode = c->enext = c->cnext;
  571. if (!znode) {
  572. dbg_cmt("no znodes to commit");
  573. return 0;
  574. }
  575. cnt += 1;
  576. while (1) {
  577. ubifs_assert(!ubifs_zn_cow(znode));
  578. __set_bit(COW_ZNODE, &znode->flags);
  579. znode->alt = 0;
  580. cnext = find_next_dirty(znode);
  581. if (!cnext) {
  582. znode->cnext = c->cnext;
  583. break;
  584. }
  585. znode->cnext = cnext;
  586. znode = cnext;
  587. cnt += 1;
  588. }
  589. dbg_cmt("committing %d znodes", cnt);
  590. ubifs_assert(cnt == atomic_long_read(&c->dirty_zn_cnt));
  591. return cnt;
  592. }
  593. /**
  594. * alloc_idx_lebs - allocate empty LEBs to be used to commit.
  595. * @c: UBIFS file-system description object
  596. * @cnt: number of znodes to commit
  597. *
  598. * This function returns %-ENOSPC if it cannot allocate a sufficient number of
  599. * empty LEBs. %0 is returned on success, otherwise a negative error code
  600. * is returned.
  601. */
  602. static int alloc_idx_lebs(struct ubifs_info *c, int cnt)
  603. {
  604. int i, leb_cnt, lnum;
  605. c->ileb_cnt = 0;
  606. c->ileb_nxt = 0;
  607. leb_cnt = get_leb_cnt(c, cnt);
  608. dbg_cmt("need about %d empty LEBS for TNC commit", leb_cnt);
  609. if (!leb_cnt)
  610. return 0;
  611. c->ilebs = kmalloc(leb_cnt * sizeof(int), GFP_NOFS);
  612. if (!c->ilebs)
  613. return -ENOMEM;
  614. for (i = 0; i < leb_cnt; i++) {
  615. lnum = ubifs_find_free_leb_for_idx(c);
  616. if (lnum < 0)
  617. return lnum;
  618. c->ilebs[c->ileb_cnt++] = lnum;
  619. dbg_cmt("LEB %d", lnum);
  620. }
  621. if (dbg_is_chk_index(c) && !(prandom_u32() & 7))
  622. return -ENOSPC;
  623. return 0;
  624. }
  625. /**
  626. * free_unused_idx_lebs - free unused LEBs that were allocated for the commit.
  627. * @c: UBIFS file-system description object
  628. *
  629. * It is possible that we allocate more empty LEBs for the commit than we need.
  630. * This functions frees the surplus.
  631. *
  632. * This function returns %0 on success and a negative error code on failure.
  633. */
  634. static int free_unused_idx_lebs(struct ubifs_info *c)
  635. {
  636. int i, err = 0, lnum, er;
  637. for (i = c->ileb_nxt; i < c->ileb_cnt; i++) {
  638. lnum = c->ilebs[i];
  639. dbg_cmt("LEB %d", lnum);
  640. er = ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0,
  641. LPROPS_INDEX | LPROPS_TAKEN, 0);
  642. if (!err)
  643. err = er;
  644. }
  645. return err;
  646. }
  647. /**
  648. * free_idx_lebs - free unused LEBs after commit end.
  649. * @c: UBIFS file-system description object
  650. *
  651. * This function returns %0 on success and a negative error code on failure.
  652. */
  653. static int free_idx_lebs(struct ubifs_info *c)
  654. {
  655. int err;
  656. err = free_unused_idx_lebs(c);
  657. kfree(c->ilebs);
  658. c->ilebs = NULL;
  659. return err;
  660. }
  661. /**
  662. * ubifs_tnc_start_commit - start TNC commit.
  663. * @c: UBIFS file-system description object
  664. * @zroot: new index root position is returned here
  665. *
  666. * This function prepares the list of indexing nodes to commit and lays out
  667. * their positions on flash. If there is not enough free space it uses the
  668. * in-gap commit method. Returns zero in case of success and a negative error
  669. * code in case of failure.
  670. */
  671. int ubifs_tnc_start_commit(struct ubifs_info *c, struct ubifs_zbranch *zroot)
  672. {
  673. int err = 0, cnt;
  674. mutex_lock(&c->tnc_mutex);
  675. err = dbg_check_tnc(c, 1);
  676. if (err)
  677. goto out;
  678. cnt = get_znodes_to_commit(c);
  679. if (cnt != 0) {
  680. int no_space = 0;
  681. err = alloc_idx_lebs(c, cnt);
  682. if (err == -ENOSPC)
  683. no_space = 1;
  684. else if (err)
  685. goto out_free;
  686. err = layout_commit(c, no_space, cnt);
  687. if (err)
  688. goto out_free;
  689. ubifs_assert(atomic_long_read(&c->dirty_zn_cnt) == 0);
  690. err = free_unused_idx_lebs(c);
  691. if (err)
  692. goto out;
  693. }
  694. destroy_old_idx(c);
  695. memcpy(zroot, &c->zroot, sizeof(struct ubifs_zbranch));
  696. err = ubifs_save_dirty_idx_lnums(c);
  697. if (err)
  698. goto out;
  699. spin_lock(&c->space_lock);
  700. /*
  701. * Although we have not finished committing yet, update size of the
  702. * committed index ('c->bi.old_idx_sz') and zero out the index growth
  703. * budget. It is OK to do this now, because we've reserved all the
  704. * space which is needed to commit the index, and it is save for the
  705. * budgeting subsystem to assume the index is already committed,
  706. * even though it is not.
  707. */
  708. ubifs_assert(c->bi.min_idx_lebs == ubifs_calc_min_idx_lebs(c));
  709. c->bi.old_idx_sz = c->calc_idx_sz;
  710. c->bi.uncommitted_idx = 0;
  711. c->bi.min_idx_lebs = ubifs_calc_min_idx_lebs(c);
  712. spin_unlock(&c->space_lock);
  713. mutex_unlock(&c->tnc_mutex);
  714. dbg_cmt("number of index LEBs %d", c->lst.idx_lebs);
  715. dbg_cmt("size of index %llu", c->calc_idx_sz);
  716. return err;
  717. out_free:
  718. free_idx_lebs(c);
  719. out:
  720. mutex_unlock(&c->tnc_mutex);
  721. return err;
  722. }
  723. /**
  724. * write_index - write index nodes.
  725. * @c: UBIFS file-system description object
  726. *
  727. * This function writes the index nodes whose positions were laid out in the
  728. * layout_in_empty_space function.
  729. */
  730. static int write_index(struct ubifs_info *c)
  731. {
  732. struct ubifs_idx_node *idx;
  733. struct ubifs_znode *znode, *cnext;
  734. int i, lnum, offs, len, next_len, buf_len, buf_offs, used;
  735. int avail, wlen, err, lnum_pos = 0, blen, nxt_offs;
  736. cnext = c->enext;
  737. if (!cnext)
  738. return 0;
  739. /*
  740. * Always write index nodes to the index head so that index nodes and
  741. * other types of nodes are never mixed in the same erase block.
  742. */
  743. lnum = c->ihead_lnum;
  744. buf_offs = c->ihead_offs;
  745. /* Allocate commit buffer */
  746. buf_len = ALIGN(c->max_idx_node_sz, c->min_io_size);
  747. used = 0;
  748. avail = buf_len;
  749. /* Ensure there is enough room for first write */
  750. next_len = ubifs_idx_node_sz(c, cnext->child_cnt);
  751. if (buf_offs + next_len > c->leb_size) {
  752. err = ubifs_update_one_lp(c, lnum, LPROPS_NC, 0, 0,
  753. LPROPS_TAKEN);
  754. if (err)
  755. return err;
  756. lnum = -1;
  757. }
  758. while (1) {
  759. cond_resched();
  760. znode = cnext;
  761. idx = c->cbuf + used;
  762. /* Make index node */
  763. idx->ch.node_type = UBIFS_IDX_NODE;
  764. idx->child_cnt = cpu_to_le16(znode->child_cnt);
  765. idx->level = cpu_to_le16(znode->level);
  766. for (i = 0; i < znode->child_cnt; i++) {
  767. struct ubifs_branch *br = ubifs_idx_branch(c, idx, i);
  768. struct ubifs_zbranch *zbr = &znode->zbranch[i];
  769. key_write_idx(c, &zbr->key, &br->key);
  770. br->lnum = cpu_to_le32(zbr->lnum);
  771. br->offs = cpu_to_le32(zbr->offs);
  772. br->len = cpu_to_le32(zbr->len);
  773. if (!zbr->lnum || !zbr->len) {
  774. ubifs_err(c, "bad ref in znode");
  775. ubifs_dump_znode(c, znode);
  776. if (zbr->znode)
  777. ubifs_dump_znode(c, zbr->znode);
  778. return -EINVAL;
  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(c, "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_atomic();
  810. clear_bit(COW_ZNODE, &znode->flags);
  811. smp_mb__after_atomic();
  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. if (err)
  860. return err;
  861. buf_offs += blen;
  862. if (next_len) {
  863. if (nxt_offs > c->leb_size) {
  864. err = ubifs_update_one_lp(c, lnum, LPROPS_NC, 0,
  865. 0, LPROPS_TAKEN);
  866. if (err)
  867. return err;
  868. lnum = -1;
  869. }
  870. used -= blen;
  871. if (used < 0)
  872. used = 0;
  873. avail = buf_len - used;
  874. memmove(c->cbuf, c->cbuf + blen, used);
  875. continue;
  876. }
  877. break;
  878. }
  879. if (lnum != c->dbg->new_ihead_lnum ||
  880. buf_offs != c->dbg->new_ihead_offs) {
  881. ubifs_err(c, "inconsistent ihead");
  882. return -EINVAL;
  883. }
  884. c->ihead_lnum = lnum;
  885. c->ihead_offs = buf_offs;
  886. return 0;
  887. }
  888. /**
  889. * free_obsolete_znodes - free obsolete znodes.
  890. * @c: UBIFS file-system description object
  891. *
  892. * At the end of commit end, obsolete znodes are freed.
  893. */
  894. static void free_obsolete_znodes(struct ubifs_info *c)
  895. {
  896. struct ubifs_znode *znode, *cnext;
  897. cnext = c->cnext;
  898. do {
  899. znode = cnext;
  900. cnext = znode->cnext;
  901. if (ubifs_zn_obsolete(znode))
  902. kfree(znode);
  903. else {
  904. znode->cnext = NULL;
  905. atomic_long_inc(&c->clean_zn_cnt);
  906. atomic_long_inc(&ubifs_clean_zn_cnt);
  907. }
  908. } while (cnext != c->cnext);
  909. }
  910. /**
  911. * return_gap_lebs - return LEBs used by the in-gap commit method.
  912. * @c: UBIFS file-system description object
  913. *
  914. * This function clears the "taken" flag for the LEBs which were used by the
  915. * "commit in-the-gaps" method.
  916. */
  917. static int return_gap_lebs(struct ubifs_info *c)
  918. {
  919. int *p, err;
  920. if (!c->gap_lebs)
  921. return 0;
  922. dbg_cmt("");
  923. for (p = c->gap_lebs; *p != -1; p++) {
  924. err = ubifs_change_one_lp(c, *p, LPROPS_NC, LPROPS_NC, 0,
  925. LPROPS_TAKEN, 0);
  926. if (err)
  927. return err;
  928. }
  929. kfree(c->gap_lebs);
  930. c->gap_lebs = NULL;
  931. return 0;
  932. }
  933. /**
  934. * ubifs_tnc_end_commit - update the TNC for commit end.
  935. * @c: UBIFS file-system description object
  936. *
  937. * Write the dirty znodes.
  938. */
  939. int ubifs_tnc_end_commit(struct ubifs_info *c)
  940. {
  941. int err;
  942. if (!c->cnext)
  943. return 0;
  944. err = return_gap_lebs(c);
  945. if (err)
  946. return err;
  947. err = write_index(c);
  948. if (err)
  949. return err;
  950. mutex_lock(&c->tnc_mutex);
  951. dbg_cmt("TNC height is %d", c->zroot.znode->level + 1);
  952. free_obsolete_znodes(c);
  953. c->cnext = NULL;
  954. kfree(c->ilebs);
  955. c->ilebs = NULL;
  956. mutex_unlock(&c->tnc_mutex);
  957. return 0;
  958. }