tnc_commit.c 27 KB

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