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