tnc_commit.c 27 KB

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