tnc_commit.c 28 KB

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