tnc_commit.c 28 KB

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