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