blob: d6fab1a9986c38642b6815bb4c29bc5baff10c43 [file] [log] [blame]
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001/*
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
23/* This file implements TNC functions for committing */
24
25#include "ubifs.h"
26
27/**
28 * make_idx_node - make an index node for fill-the-gaps method of TNC commit.
29 * @c: UBIFS file-system description object
30 * @idx: buffer in which to place new index node
31 * @znode: znode from which to make new index node
32 * @lnum: LEB number where new index node will be written
33 * @offs: offset where new index node will be written
34 * @len: length of new index node
35 */
36static int make_idx_node(struct ubifs_info *c, struct ubifs_idx_node *idx,
37 struct ubifs_znode *znode, int lnum, int offs, int len)
38{
39 struct ubifs_znode *zp;
40 int i, err;
41
42 /* Make index node */
43 idx->ch.node_type = UBIFS_IDX_NODE;
44 idx->child_cnt = cpu_to_le16(znode->child_cnt);
45 idx->level = cpu_to_le16(znode->level);
46 for (i = 0; i < znode->child_cnt; i++) {
47 struct ubifs_branch *br = ubifs_idx_branch(c, idx, i);
48 struct ubifs_zbranch *zbr = &znode->zbranch[i];
49
50 key_write_idx(c, &zbr->key, &br->key);
51 br->lnum = cpu_to_le32(zbr->lnum);
52 br->offs = cpu_to_le32(zbr->offs);
53 br->len = cpu_to_le32(zbr->len);
54 if (!zbr->lnum || !zbr->len) {
55 ubifs_err("bad ref in znode");
56 dbg_dump_znode(c, znode);
57 if (zbr->znode)
58 dbg_dump_znode(c, zbr->znode);
59 }
60 }
61 ubifs_prepare_node(c, idx, len, 0);
62
63#ifdef CONFIG_UBIFS_FS_DEBUG
64 znode->lnum = lnum;
65 znode->offs = offs;
66 znode->len = len;
67#endif
68
69 err = insert_old_idx_znode(c, znode);
70
71 /* Update the parent */
72 zp = znode->parent;
73 if (zp) {
74 struct ubifs_zbranch *zbr;
75
76 zbr = &zp->zbranch[znode->iip];
77 zbr->lnum = lnum;
78 zbr->offs = offs;
79 zbr->len = len;
80 } else {
81 c->zroot.lnum = lnum;
82 c->zroot.offs = offs;
83 c->zroot.len = len;
84 }
85 c->calc_idx_sz += ALIGN(len, 8);
86
87 atomic_long_dec(&c->dirty_zn_cnt);
88
89 ubifs_assert(ubifs_zn_dirty(znode));
Artem Bityutskiyf42eed72011-05-30 14:45:30 +030090 ubifs_assert(ubifs_zn_cow(znode));
Artem Bityutskiy1e517642008-07-14 19:08:37 +030091
Artem Bityutskiy06b282a2011-05-30 18:19:34 +030092 /*
93 * Note, unlike 'write_index()' we do not add memory barriers here
94 * because this function is called with @c->tnc_mutex locked.
95 */
Artem Bityutskiy1e517642008-07-14 19:08:37 +030096 __clear_bit(DIRTY_ZNODE, &znode->flags);
97 __clear_bit(COW_ZNODE, &znode->flags);
98
99 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 */
112static 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 ubifs_assert((gap_start & 7) == 0);
118 ubifs_assert((gap_end & 7) == 0);
119 ubifs_assert(gap_end >= gap_start);
120
121 gap_remains = gap_end - gap_start;
122 if (!gap_remains)
123 return 0;
124 gap_pos = gap_start;
125 written = 0;
126 while (c->enext) {
127 len = ubifs_idx_node_sz(c, c->enext->child_cnt);
128 if (len < gap_remains) {
129 struct ubifs_znode *znode = c->enext;
130 const int alen = ALIGN(len, 8);
131 int err;
132
133 ubifs_assert(alen <= gap_remains);
134 err = make_idx_node(c, c->ileb_buf + gap_pos, znode,
135 lnum, gap_pos, len);
136 if (err)
137 return err;
138 gap_remains -= alen;
139 gap_pos += alen;
140 c->enext = znode->cnext;
141 if (c->enext == c->cnext)
142 c->enext = NULL;
143 written += 1;
144 } else
145 break;
146 }
147 if (gap_end == c->leb_size) {
148 c->ileb_len = ALIGN(gap_pos, c->min_io_size);
149 /* Pad to end of min_io_size */
150 pad_len = c->ileb_len - gap_pos;
151 } else
152 /* Pad to end of gap */
153 pad_len = gap_remains;
154 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 ubifs_pad(c, c->ileb_buf + gap_pos, pad_len);
157 *dirt += pad_len;
158 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 */
169static 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 p = c->old_idx.rb_node;
175 while (p) {
176 o = rb_entry(p, struct ubifs_old_idx, rb);
177 if (lnum < o->lnum)
178 p = p->rb_left;
179 else if (lnum > o->lnum)
180 p = p->rb_right;
181 else if (offs < o->offs)
182 p = p->rb_left;
183 else if (offs > o->offs)
184 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 */
204static 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 ret = is_idx_node_in_tnc(c, key, level, lnum, offs);
210 if (ret < 0)
211 return ret; /* Error code */
212 if (ret == 0)
213 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 here
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 */
230static 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 int lnum, dirt = 0, gap_start, gap_end, err, written, tot_written;
235
236 tot_written = 0;
237 /* Get an index LEB with lots of obsolete index nodes */
238 lnum = ubifs_find_dirty_idx_leb(c);
239 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 *p = lnum;
246 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 */
Artem Bityutskiy348709b2009-08-25 15:00:55 +0300252 sleb = ubifs_scan(c, lnum, 0, c->ileb_buf, 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300253 c->ileb_len = 0;
254 if (IS_ERR(sleb))
255 return PTR_ERR(sleb);
256 gap_start = 0;
257 list_for_each_entry(snod, &sleb->nodes, list) {
258 struct ubifs_idx_node *idx;
259 int in_use, level;
260
261 ubifs_assert(snod->type == UBIFS_IDX_NODE);
262 idx = snod->node;
263 key_read(c, ubifs_idx_key(c, idx), &snod->key);
264 level = le16_to_cpu(idx->level);
265 /* Determine if the index node is in use (not obsolete) */
266 in_use = is_idx_node_in_use(c, &snod->key, level, lnum,
267 snod->offs);
268 if (in_use < 0) {
269 ubifs_scan_destroy(sleb);
270 return in_use; /* Error code */
271 }
272 if (in_use) {
273 if (in_use == 1)
274 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 gap_end = snod->offs;
282 /* Try to fill gap */
283 written = fill_gap(c, lnum, gap_start, gap_end, &dirt);
284 if (written < 0) {
285 ubifs_scan_destroy(sleb);
286 return written; /* Error code */
287 }
288 tot_written += written;
289 gap_start = ALIGN(snod->offs + snod->len, 8);
290 }
291 }
292 ubifs_scan_destroy(sleb);
293 c->ileb_len = c->leb_size;
294 gap_end = c->leb_size;
295 /* Try to fill gap */
296 written = fill_gap(c, lnum, gap_start, gap_end, &dirt);
297 if (written < 0)
298 return written; /* Error code */
299 tot_written += written;
300 if (tot_written == 0) {
301 struct ubifs_lprops lp;
302
303 dbg_gc("LEB %d wrote %d index nodes", lnum, tot_written);
304 err = ubifs_read_one_lp(c, lnum, &lp);
305 if (err)
306 return err;
307 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 err = ubifs_change_one_lp(c, lnum,
313 c->leb_size - c->ileb_len,
314 dirt, 0, 0, 0);
315 if (err)
316 return err;
317 }
318 return 0;
319 }
320 err = ubifs_change_one_lp(c, lnum, c->leb_size - c->ileb_len, dirt,
321 0, 0, 0);
322 if (err)
323 return err;
324 err = ubifs_leb_change(c, lnum, c->ileb_buf, c->ileb_len,
325 UBI_SHORTTERM);
326 if (err)
327 return err;
328 dbg_gc("LEB %d wrote %d index nodes", lnum, tot_written);
329 return tot_written;
330}
331
332/**
333 * get_leb_cnt - calculate the number of empty LEBs needed to commit.
334 * @c: UBIFS file-system description object
335 * @cnt: number of znodes to commit
336 *
337 * This function returns the number of empty LEBs needed to commit @cnt znodes
338 * to the current index head. The number is not exact and may be more than
339 * needed.
340 */
341static int get_leb_cnt(struct ubifs_info *c, int cnt)
342{
343 int d;
344
345 /* Assume maximum index node size (i.e. overestimate space needed) */
346 cnt -= (c->leb_size - c->ihead_offs) / c->max_idx_node_sz;
347 if (cnt < 0)
348 cnt = 0;
349 d = c->leb_size / c->max_idx_node_sz;
350 return DIV_ROUND_UP(cnt, d);
351}
352
353/**
354 * layout_in_gaps - in-the-gaps method of committing TNC.
355 * @c: UBIFS file-system description object
356 * @cnt: number of dirty znodes to commit.
357 *
358 * This function lays out new index nodes for dirty znodes using in-the-gaps
359 * method of TNC commit.
360 *
361 * This function returns %0 on success and a negative error code on failure.
362 */
363static int layout_in_gaps(struct ubifs_info *c, int cnt)
364{
365 int err, leb_needed_cnt, written, *p;
366
367 dbg_gc("%d znodes to write", cnt);
368
369 c->gap_lebs = kmalloc(sizeof(int) * (c->lst.idx_lebs + 1), GFP_NOFS);
370 if (!c->gap_lebs)
371 return -ENOMEM;
372
373 p = c->gap_lebs;
374 do {
375 ubifs_assert(p < c->gap_lebs + sizeof(int) * c->lst.idx_lebs);
376 written = layout_leb_in_gaps(c, p);
377 if (written < 0) {
378 err = written;
Artem Bityutskiy0010f182008-07-25 16:39:44 +0300379 if (err != -ENOSPC) {
380 kfree(c->gap_lebs);
381 c->gap_lebs = NULL;
382 return err;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300383 }
Artem Bityutskiybc3f07f2011-04-05 13:52:20 +0300384 if (dbg_force_in_the_gaps_enabled()) {
Artem Bityutskiy0010f182008-07-25 16:39:44 +0300385 /*
386 * Do not print scary warnings if the debugging
387 * option which forces in-the-gaps is enabled.
388 */
Artem Bityutskiybc3f07f2011-04-05 13:52:20 +0300389 ubifs_warn("out of space");
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +0300390 dbg_dump_budg(c, &c->bi);
Artem Bityutskiy0010f182008-07-25 16:39:44 +0300391 dbg_dump_lprops(c);
392 }
393 /* Try to commit anyway */
394 err = 0;
395 break;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300396 }
397 p++;
398 cnt -= written;
399 leb_needed_cnt = get_leb_cnt(c, cnt);
400 dbg_gc("%d znodes remaining, need %d LEBs, have %d", cnt,
401 leb_needed_cnt, c->ileb_cnt);
402 } while (leb_needed_cnt > c->ileb_cnt);
403
404 *p = -1;
405 return 0;
406}
407
408/**
409 * layout_in_empty_space - layout index nodes in empty space.
410 * @c: UBIFS file-system description object
411 *
412 * This function lays out new index nodes for dirty znodes using empty LEBs.
413 *
414 * This function returns %0 on success and a negative error code on failure.
415 */
416static int layout_in_empty_space(struct ubifs_info *c)
417{
418 struct ubifs_znode *znode, *cnext, *zp;
419 int lnum, offs, len, next_len, buf_len, buf_offs, used, avail;
420 int wlen, blen, err;
421
422 cnext = c->enext;
423 if (!cnext)
424 return 0;
425
426 lnum = c->ihead_lnum;
427 buf_offs = c->ihead_offs;
428
429 buf_len = ubifs_idx_node_sz(c, c->fanout);
430 buf_len = ALIGN(buf_len, c->min_io_size);
431 used = 0;
432 avail = buf_len;
433
434 /* Ensure there is enough room for first write */
435 next_len = ubifs_idx_node_sz(c, cnext->child_cnt);
436 if (buf_offs + next_len > c->leb_size)
437 lnum = -1;
438
439 while (1) {
440 znode = cnext;
441
442 len = ubifs_idx_node_sz(c, znode->child_cnt);
443
444 /* Determine the index node position */
445 if (lnum == -1) {
446 if (c->ileb_nxt >= c->ileb_cnt) {
447 ubifs_err("out of space");
448 return -ENOSPC;
449 }
450 lnum = c->ilebs[c->ileb_nxt++];
451 buf_offs = 0;
452 used = 0;
453 avail = buf_len;
454 }
455
456 offs = buf_offs + used;
457
458#ifdef CONFIG_UBIFS_FS_DEBUG
459 znode->lnum = lnum;
460 znode->offs = offs;
461 znode->len = len;
462#endif
463
464 /* Update the parent */
465 zp = znode->parent;
466 if (zp) {
467 struct ubifs_zbranch *zbr;
468 int i;
469
470 i = znode->iip;
471 zbr = &zp->zbranch[i];
472 zbr->lnum = lnum;
473 zbr->offs = offs;
474 zbr->len = len;
475 } else {
476 c->zroot.lnum = lnum;
477 c->zroot.offs = offs;
478 c->zroot.len = len;
479 }
480 c->calc_idx_sz += ALIGN(len, 8);
481
482 /*
483 * Once lprops is updated, we can decrease the dirty znode count
484 * but it is easier to just do it here.
485 */
486 atomic_long_dec(&c->dirty_zn_cnt);
487
488 /*
489 * Calculate the next index node length to see if there is
490 * enough room for it
491 */
492 cnext = znode->cnext;
493 if (cnext == c->cnext)
494 next_len = 0;
495 else
496 next_len = ubifs_idx_node_sz(c, cnext->child_cnt);
497
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300498 /* Update buffer positions */
499 wlen = used + len;
500 used += ALIGN(len, 8);
501 avail -= ALIGN(len, 8);
502
503 if (next_len != 0 &&
504 buf_offs + used + next_len <= c->leb_size &&
505 avail > 0)
506 continue;
507
508 if (avail <= 0 && next_len &&
509 buf_offs + used + next_len <= c->leb_size)
510 blen = buf_len;
511 else
512 blen = ALIGN(wlen, c->min_io_size);
513
514 /* The buffer is full or there are no more znodes to do */
515 buf_offs += blen;
516 if (next_len) {
517 if (buf_offs + next_len > c->leb_size) {
518 err = ubifs_update_one_lp(c, lnum,
519 c->leb_size - buf_offs, blen - used,
520 0, 0);
521 if (err)
522 return err;
523 lnum = -1;
524 }
525 used -= blen;
526 if (used < 0)
527 used = 0;
528 avail = buf_len - used;
529 continue;
530 }
531 err = ubifs_update_one_lp(c, lnum, c->leb_size - buf_offs,
532 blen - used, 0, 0);
533 if (err)
534 return err;
535 break;
536 }
537
538#ifdef CONFIG_UBIFS_FS_DEBUG
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +0300539 c->dbg->new_ihead_lnum = lnum;
540 c->dbg->new_ihead_offs = buf_offs;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300541#endif
542
543 return 0;
544}
545
546/**
547 * layout_commit - determine positions of index nodes to commit.
548 * @c: UBIFS file-system description object
549 * @no_space: indicates that insufficient empty LEBs were allocated
550 * @cnt: number of znodes to commit
551 *
552 * Calculate and update the positions of index nodes to commit. If there were
553 * an insufficient number of empty LEBs allocated, then index nodes are placed
554 * into the gaps created by obsolete index nodes in non-empty index LEBs. For
555 * this purpose, an obsolete index node is one that was not in the index as at
556 * the end of the last commit. To write "in-the-gaps" requires that those index
557 * LEBs are updated atomically in-place.
558 */
559static int layout_commit(struct ubifs_info *c, int no_space, int cnt)
560{
561 int err;
562
563 if (no_space) {
564 err = layout_in_gaps(c, cnt);
565 if (err)
566 return err;
567 }
568 err = layout_in_empty_space(c);
569 return err;
570}
571
572/**
573 * find_first_dirty - find first dirty znode.
574 * @znode: znode to begin searching from
575 */
576static struct ubifs_znode *find_first_dirty(struct ubifs_znode *znode)
577{
578 int i, cont;
579
580 if (!znode)
581 return NULL;
582
583 while (1) {
584 if (znode->level == 0) {
585 if (ubifs_zn_dirty(znode))
586 return znode;
587 return NULL;
588 }
589 cont = 0;
590 for (i = 0; i < znode->child_cnt; i++) {
591 struct ubifs_zbranch *zbr = &znode->zbranch[i];
592
593 if (zbr->znode && ubifs_zn_dirty(zbr->znode)) {
594 znode = zbr->znode;
595 cont = 1;
596 break;
597 }
598 }
599 if (!cont) {
600 if (ubifs_zn_dirty(znode))
601 return znode;
602 return NULL;
603 }
604 }
605}
606
607/**
608 * find_next_dirty - find next dirty znode.
609 * @znode: znode to begin searching from
610 */
611static struct ubifs_znode *find_next_dirty(struct ubifs_znode *znode)
612{
613 int n = znode->iip + 1;
614
615 znode = znode->parent;
616 if (!znode)
617 return NULL;
618 for (; n < znode->child_cnt; n++) {
619 struct ubifs_zbranch *zbr = &znode->zbranch[n];
620
621 if (zbr->znode && ubifs_zn_dirty(zbr->znode))
622 return find_first_dirty(zbr->znode);
623 }
624 return znode;
625}
626
627/**
628 * get_znodes_to_commit - create list of dirty znodes to commit.
629 * @c: UBIFS file-system description object
630 *
631 * This function returns the number of znodes to commit.
632 */
633static int get_znodes_to_commit(struct ubifs_info *c)
634{
635 struct ubifs_znode *znode, *cnext;
636 int cnt = 0;
637
638 c->cnext = find_first_dirty(c->zroot.znode);
639 znode = c->enext = c->cnext;
640 if (!znode) {
641 dbg_cmt("no znodes to commit");
642 return 0;
643 }
644 cnt += 1;
645 while (1) {
Artem Bityutskiyf42eed72011-05-30 14:45:30 +0300646 ubifs_assert(!ubifs_zn_cow(znode));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300647 __set_bit(COW_ZNODE, &znode->flags);
648 znode->alt = 0;
649 cnext = find_next_dirty(znode);
650 if (!cnext) {
651 znode->cnext = c->cnext;
652 break;
653 }
654 znode->cnext = cnext;
655 znode = cnext;
656 cnt += 1;
657 }
658 dbg_cmt("committing %d znodes", cnt);
659 ubifs_assert(cnt == atomic_long_read(&c->dirty_zn_cnt));
660 return cnt;
661}
662
663/**
664 * alloc_idx_lebs - allocate empty LEBs to be used to commit.
665 * @c: UBIFS file-system description object
666 * @cnt: number of znodes to commit
667 *
668 * This function returns %-ENOSPC if it cannot allocate a sufficient number of
669 * empty LEBs. %0 is returned on success, otherwise a negative error code
670 * is returned.
671 */
672static int alloc_idx_lebs(struct ubifs_info *c, int cnt)
673{
674 int i, leb_cnt, lnum;
675
676 c->ileb_cnt = 0;
677 c->ileb_nxt = 0;
678 leb_cnt = get_leb_cnt(c, cnt);
679 dbg_cmt("need about %d empty LEBS for TNC commit", leb_cnt);
680 if (!leb_cnt)
681 return 0;
682 c->ilebs = kmalloc(leb_cnt * sizeof(int), GFP_NOFS);
683 if (!c->ilebs)
684 return -ENOMEM;
685 for (i = 0; i < leb_cnt; i++) {
686 lnum = ubifs_find_free_leb_for_idx(c);
687 if (lnum < 0)
688 return lnum;
689 c->ilebs[c->ileb_cnt++] = lnum;
690 dbg_cmt("LEB %d", lnum);
691 }
692 if (dbg_force_in_the_gaps())
693 return -ENOSPC;
694 return 0;
695}
696
697/**
698 * free_unused_idx_lebs - free unused LEBs that were allocated for the commit.
699 * @c: UBIFS file-system description object
700 *
701 * It is possible that we allocate more empty LEBs for the commit than we need.
702 * This functions frees the surplus.
703 *
704 * This function returns %0 on success and a negative error code on failure.
705 */
706static int free_unused_idx_lebs(struct ubifs_info *c)
707{
708 int i, err = 0, lnum, er;
709
710 for (i = c->ileb_nxt; i < c->ileb_cnt; i++) {
711 lnum = c->ilebs[i];
712 dbg_cmt("LEB %d", lnum);
713 er = ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0,
714 LPROPS_INDEX | LPROPS_TAKEN, 0);
715 if (!err)
716 err = er;
717 }
718 return err;
719}
720
721/**
722 * free_idx_lebs - free unused LEBs after commit end.
723 * @c: UBIFS file-system description object
724 *
725 * This function returns %0 on success and a negative error code on failure.
726 */
727static int free_idx_lebs(struct ubifs_info *c)
728{
729 int err;
730
731 err = free_unused_idx_lebs(c);
732 kfree(c->ilebs);
733 c->ilebs = NULL;
734 return err;
735}
736
737/**
738 * ubifs_tnc_start_commit - start TNC commit.
739 * @c: UBIFS file-system description object
740 * @zroot: new index root position is returned here
741 *
742 * This function prepares the list of indexing nodes to commit and lays out
743 * their positions on flash. If there is not enough free space it uses the
744 * in-gap commit method. Returns zero in case of success and a negative error
745 * code in case of failure.
746 */
747int ubifs_tnc_start_commit(struct ubifs_info *c, struct ubifs_zbranch *zroot)
748{
749 int err = 0, cnt;
750
751 mutex_lock(&c->tnc_mutex);
752 err = dbg_check_tnc(c, 1);
753 if (err)
754 goto out;
755 cnt = get_znodes_to_commit(c);
756 if (cnt != 0) {
757 int no_space = 0;
758
759 err = alloc_idx_lebs(c, cnt);
760 if (err == -ENOSPC)
761 no_space = 1;
762 else if (err)
763 goto out_free;
764 err = layout_commit(c, no_space, cnt);
765 if (err)
766 goto out_free;
767 ubifs_assert(atomic_long_read(&c->dirty_zn_cnt) == 0);
768 err = free_unused_idx_lebs(c);
769 if (err)
770 goto out;
771 }
772 destroy_old_idx(c);
773 memcpy(zroot, &c->zroot, sizeof(struct ubifs_zbranch));
774
775 err = ubifs_save_dirty_idx_lnums(c);
776 if (err)
777 goto out;
778
779 spin_lock(&c->space_lock);
780 /*
781 * Although we have not finished committing yet, update size of the
Artem Bityutskiyb1375452011-03-29 18:04:05 +0300782 * committed index ('c->bi.old_idx_sz') and zero out the index growth
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300783 * budget. It is OK to do this now, because we've reserved all the
784 * space which is needed to commit the index, and it is save for the
785 * budgeting subsystem to assume the index is already committed,
786 * even though it is not.
787 */
Artem Bityutskiyb1375452011-03-29 18:04:05 +0300788 ubifs_assert(c->bi.min_idx_lebs == ubifs_calc_min_idx_lebs(c));
789 c->bi.old_idx_sz = c->calc_idx_sz;
790 c->bi.uncommitted_idx = 0;
791 c->bi.min_idx_lebs = ubifs_calc_min_idx_lebs(c);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300792 spin_unlock(&c->space_lock);
793 mutex_unlock(&c->tnc_mutex);
794
795 dbg_cmt("number of index LEBs %d", c->lst.idx_lebs);
796 dbg_cmt("size of index %llu", c->calc_idx_sz);
797 return err;
798
799out_free:
800 free_idx_lebs(c);
801out:
802 mutex_unlock(&c->tnc_mutex);
803 return err;
804}
805
806/**
807 * write_index - write index nodes.
808 * @c: UBIFS file-system description object
809 *
810 * This function writes the index nodes whose positions were laid out in the
811 * layout_in_empty_space function.
812 */
813static int write_index(struct ubifs_info *c)
814{
815 struct ubifs_idx_node *idx;
816 struct ubifs_znode *znode, *cnext;
817 int i, lnum, offs, len, next_len, buf_len, buf_offs, used;
Artem Bityutskiy1f425962011-05-30 14:30:51 +0300818 int avail, wlen, err, lnum_pos = 0, blen, nxt_offs;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300819
820 cnext = c->enext;
821 if (!cnext)
822 return 0;
823
824 /*
825 * Always write index nodes to the index head so that index nodes and
826 * other types of nodes are never mixed in the same erase block.
827 */
828 lnum = c->ihead_lnum;
829 buf_offs = c->ihead_offs;
830
831 /* Allocate commit buffer */
832 buf_len = ALIGN(c->max_idx_node_sz, c->min_io_size);
833 used = 0;
834 avail = buf_len;
835
836 /* Ensure there is enough room for first write */
837 next_len = ubifs_idx_node_sz(c, cnext->child_cnt);
838 if (buf_offs + next_len > c->leb_size) {
839 err = ubifs_update_one_lp(c, lnum, LPROPS_NC, 0, 0,
840 LPROPS_TAKEN);
841 if (err)
842 return err;
843 lnum = -1;
844 }
845
846 while (1) {
847 cond_resched();
848
849 znode = cnext;
850 idx = c->cbuf + used;
851
852 /* Make index node */
853 idx->ch.node_type = UBIFS_IDX_NODE;
854 idx->child_cnt = cpu_to_le16(znode->child_cnt);
855 idx->level = cpu_to_le16(znode->level);
856 for (i = 0; i < znode->child_cnt; i++) {
857 struct ubifs_branch *br = ubifs_idx_branch(c, idx, i);
858 struct ubifs_zbranch *zbr = &znode->zbranch[i];
859
860 key_write_idx(c, &zbr->key, &br->key);
861 br->lnum = cpu_to_le32(zbr->lnum);
862 br->offs = cpu_to_le32(zbr->offs);
863 br->len = cpu_to_le32(zbr->len);
864 if (!zbr->lnum || !zbr->len) {
865 ubifs_err("bad ref in znode");
866 dbg_dump_znode(c, znode);
867 if (zbr->znode)
868 dbg_dump_znode(c, zbr->znode);
869 }
870 }
871 len = ubifs_idx_node_sz(c, znode->child_cnt);
872 ubifs_prepare_node(c, idx, len, 0);
873
874 /* Determine the index node position */
875 if (lnum == -1) {
876 lnum = c->ilebs[lnum_pos++];
877 buf_offs = 0;
878 used = 0;
879 avail = buf_len;
880 }
881 offs = buf_offs + used;
882
883#ifdef CONFIG_UBIFS_FS_DEBUG
884 if (lnum != znode->lnum || offs != znode->offs ||
885 len != znode->len) {
886 ubifs_err("inconsistent znode posn");
887 return -EINVAL;
888 }
889#endif
890
891 /* Grab some stuff from znode while we still can */
892 cnext = znode->cnext;
893
894 ubifs_assert(ubifs_zn_dirty(znode));
Artem Bityutskiyf42eed72011-05-30 14:45:30 +0300895 ubifs_assert(ubifs_zn_cow(znode));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300896
897 /*
898 * It is important that other threads should see %DIRTY_ZNODE
899 * flag cleared before %COW_ZNODE. Specifically, it matters in
900 * the 'dirty_cow_znode()' function. This is the reason for the
901 * first barrier. Also, we want the bit changes to be seen to
902 * other threads ASAP, to avoid unnecesarry copying, which is
903 * the reason for the second barrier.
904 */
905 clear_bit(DIRTY_ZNODE, &znode->flags);
906 smp_mb__before_clear_bit();
907 clear_bit(COW_ZNODE, &znode->flags);
908 smp_mb__after_clear_bit();
909
Artem Bityutskiy06b282a2011-05-30 18:19:34 +0300910 /*
911 * We have marked the znode as clean but have not updated the
912 * @c->clean_zn_cnt counter. If this znode becomes dirty again
913 * before 'free_obsolete_znodes()' is called, then
914 * @c->clean_zn_cnt will be decremented before it gets
915 * incremented (resulting in 2 decrements for the same znode).
916 * This means that @c->clean_zn_cnt may become negative for a
917 * while.
918 *
919 * Q: why we cannot increment @c->clean_zn_cnt?
920 * A: because we do not have the @c->tnc_mutex locked, and the
921 * following code would be racy and buggy:
922 *
923 * if (!ubifs_zn_obsolete(znode)) {
924 * atomic_long_inc(&c->clean_zn_cnt);
925 * atomic_long_inc(&ubifs_clean_zn_cnt);
926 * }
927 *
928 * Thus, we just delay the @c->clean_zn_cnt update until we
929 * have the mutex locked.
930 */
931
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300932 /* Do not access znode from this point on */
933
934 /* Update buffer positions */
935 wlen = used + len;
936 used += ALIGN(len, 8);
937 avail -= ALIGN(len, 8);
938
939 /*
940 * Calculate the next index node length to see if there is
941 * enough room for it
942 */
943 if (cnext == c->cnext)
944 next_len = 0;
945 else
946 next_len = ubifs_idx_node_sz(c, cnext->child_cnt);
947
Artem Bityutskiy1f425962011-05-30 14:30:51 +0300948 nxt_offs = buf_offs + used + next_len;
949 if (next_len && nxt_offs <= c->leb_size) {
950 if (avail > 0)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300951 continue;
Artem Bityutskiy1f425962011-05-30 14:30:51 +0300952 else
953 blen = buf_len;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300954 } else {
Artem Bityutskiy1f425962011-05-30 14:30:51 +0300955 wlen = ALIGN(wlen, 8);
956 blen = ALIGN(wlen, c->min_io_size);
957 ubifs_pad(c, c->cbuf + wlen, blen - wlen);
958 }
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300959
Artem Bityutskiy1f425962011-05-30 14:30:51 +0300960 /* The buffer is full or there are no more znodes to do */
961 err = ubifs_leb_write(c, lnum, c->cbuf, buf_offs, blen,
962 UBI_SHORTTERM);
963 if (err)
964 return err;
965 buf_offs += blen;
966 if (next_len) {
967 if (nxt_offs > c->leb_size) {
968 err = ubifs_update_one_lp(c, lnum, LPROPS_NC, 0,
969 0, LPROPS_TAKEN);
970 if (err)
971 return err;
972 lnum = -1;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300973 }
Artem Bityutskiy1f425962011-05-30 14:30:51 +0300974 used -= blen;
975 if (used < 0)
976 used = 0;
977 avail = buf_len - used;
978 memmove(c->cbuf, c->cbuf + blen, used);
979 continue;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300980 }
981 break;
982 }
983
984#ifdef CONFIG_UBIFS_FS_DEBUG
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +0300985 if (lnum != c->dbg->new_ihead_lnum ||
986 buf_offs != c->dbg->new_ihead_offs) {
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300987 ubifs_err("inconsistent ihead");
988 return -EINVAL;
989 }
990#endif
991
992 c->ihead_lnum = lnum;
993 c->ihead_offs = buf_offs;
994
995 return 0;
996}
997
998/**
999 * free_obsolete_znodes - free obsolete znodes.
1000 * @c: UBIFS file-system description object
1001 *
1002 * At the end of commit end, obsolete znodes are freed.
1003 */
1004static void free_obsolete_znodes(struct ubifs_info *c)
1005{
1006 struct ubifs_znode *znode, *cnext;
1007
1008 cnext = c->cnext;
1009 do {
1010 znode = cnext;
1011 cnext = znode->cnext;
Artem Bityutskiyf42eed72011-05-30 14:45:30 +03001012 if (ubifs_zn_obsolete(znode))
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001013 kfree(znode);
1014 else {
1015 znode->cnext = NULL;
1016 atomic_long_inc(&c->clean_zn_cnt);
1017 atomic_long_inc(&ubifs_clean_zn_cnt);
1018 }
1019 } while (cnext != c->cnext);
1020}
1021
1022/**
1023 * return_gap_lebs - return LEBs used by the in-gap commit method.
1024 * @c: UBIFS file-system description object
1025 *
1026 * This function clears the "taken" flag for the LEBs which were used by the
1027 * "commit in-the-gaps" method.
1028 */
1029static int return_gap_lebs(struct ubifs_info *c)
1030{
1031 int *p, err;
1032
1033 if (!c->gap_lebs)
1034 return 0;
1035
1036 dbg_cmt("");
1037 for (p = c->gap_lebs; *p != -1; p++) {
1038 err = ubifs_change_one_lp(c, *p, LPROPS_NC, LPROPS_NC, 0,
1039 LPROPS_TAKEN, 0);
1040 if (err)
1041 return err;
1042 }
1043
1044 kfree(c->gap_lebs);
1045 c->gap_lebs = NULL;
1046 return 0;
1047}
1048
1049/**
1050 * ubifs_tnc_end_commit - update the TNC for commit end.
1051 * @c: UBIFS file-system description object
1052 *
1053 * Write the dirty znodes.
1054 */
1055int ubifs_tnc_end_commit(struct ubifs_info *c)
1056{
1057 int err;
1058
1059 if (!c->cnext)
1060 return 0;
1061
1062 err = return_gap_lebs(c);
1063 if (err)
1064 return err;
1065
1066 err = write_index(c);
1067 if (err)
1068 return err;
1069
1070 mutex_lock(&c->tnc_mutex);
1071
1072 dbg_cmt("TNC height is %d", c->zroot.znode->level + 1);
1073
1074 free_obsolete_znodes(c);
1075
1076 c->cnext = NULL;
1077 kfree(c->ilebs);
1078 c->ilebs = NULL;
1079
1080 mutex_unlock(&c->tnc_mutex);
1081
1082 return 0;
1083}