aboutsummaryrefslogtreecommitdiffstats
path: root/fs/ubifs
diff options
context:
space:
mode:
Diffstat (limited to 'fs/ubifs')
-rw-r--r--fs/ubifs/Kconfig2
-rw-r--r--fs/ubifs/budget.c243
-rw-r--r--fs/ubifs/commit.c25
-rw-r--r--fs/ubifs/compress.c18
-rw-r--r--fs/ubifs/debug.c327
-rw-r--r--fs/ubifs/debug.h127
-rw-r--r--fs/ubifs/dir.c96
-rw-r--r--fs/ubifs/file.c26
-rw-r--r--fs/ubifs/gc.c30
-rw-r--r--fs/ubifs/io.c22
-rw-r--r--fs/ubifs/ioctl.c2
-rw-r--r--fs/ubifs/journal.c8
-rw-r--r--fs/ubifs/key.h32
-rw-r--r--fs/ubifs/lprops.c26
-rw-r--r--fs/ubifs/lpt.c45
-rw-r--r--fs/ubifs/lpt_commit.c254
-rw-r--r--fs/ubifs/master.c2
-rw-r--r--fs/ubifs/orphan.c40
-rw-r--r--fs/ubifs/replay.c15
-rw-r--r--fs/ubifs/sb.c20
-rw-r--r--fs/ubifs/shrinker.c2
-rw-r--r--fs/ubifs/super.c383
-rw-r--r--fs/ubifs/tnc.c43
-rw-r--r--fs/ubifs/tnc_commit.c9
-rw-r--r--fs/ubifs/ubifs-media.h7
-rw-r--r--fs/ubifs/ubifs.h137
26 files changed, 1253 insertions, 688 deletions
diff --git a/fs/ubifs/Kconfig b/fs/ubifs/Kconfig
index 91ceeda7e5bf..e35b54d5059d 100644
--- a/fs/ubifs/Kconfig
+++ b/fs/ubifs/Kconfig
@@ -40,7 +40,7 @@ config UBIFS_FS_ZLIB
40 depends on UBIFS_FS 40 depends on UBIFS_FS
41 default y 41 default y
42 help 42 help
43 Zlib copresses better then LZO but it is slower. Say 'Y' if unsure. 43 Zlib compresses better than LZO but it is slower. Say 'Y' if unsure.
44 44
45# Debugging-related stuff 45# Debugging-related stuff
46config UBIFS_FS_DEBUG 46config UBIFS_FS_DEBUG
diff --git a/fs/ubifs/budget.c b/fs/ubifs/budget.c
index 4a18f084cc42..f393620890ee 100644
--- a/fs/ubifs/budget.c
+++ b/fs/ubifs/budget.c
@@ -32,18 +32,15 @@
32 32
33#include "ubifs.h" 33#include "ubifs.h"
34#include <linux/writeback.h> 34#include <linux/writeback.h>
35#include <asm/div64.h> 35#include <linux/math64.h>
36 36
37/* 37/*
38 * When pessimistic budget calculations say that there is no enough space, 38 * When pessimistic budget calculations say that there is no enough space,
39 * UBIFS starts writing back dirty inodes and pages, doing garbage collection, 39 * UBIFS starts writing back dirty inodes and pages, doing garbage collection,
40 * or committing. The below constants define maximum number of times UBIFS 40 * or committing. The below constant defines maximum number of times UBIFS
41 * repeats the operations. 41 * repeats the operations.
42 */ 42 */
43#define MAX_SHRINK_RETRIES 8 43#define MAX_MKSPC_RETRIES 3
44#define MAX_GC_RETRIES 4
45#define MAX_CMT_RETRIES 2
46#define MAX_NOSPC_RETRIES 1
47 44
48/* 45/*
49 * The below constant defines amount of dirty pages which should be written 46 * The below constant defines amount of dirty pages which should be written
@@ -52,30 +49,6 @@
52#define NR_TO_WRITE 16 49#define NR_TO_WRITE 16
53 50
54/** 51/**
55 * struct retries_info - information about re-tries while making free space.
56 * @prev_liability: previous liability
57 * @shrink_cnt: how many times the liability was shrinked
58 * @shrink_retries: count of liability shrink re-tries (increased when
59 * liability does not shrink)
60 * @try_gc: GC should be tried first
61 * @gc_retries: how many times GC was run
62 * @cmt_retries: how many times commit has been done
63 * @nospc_retries: how many times GC returned %-ENOSPC
64 *
65 * Since we consider budgeting to be the fast-path, and this structure has to
66 * be allocated on stack and zeroed out, we make it smaller using bit-fields.
67 */
68struct retries_info {
69 long long prev_liability;
70 unsigned int shrink_cnt;
71 unsigned int shrink_retries:5;
72 unsigned int try_gc:1;
73 unsigned int gc_retries:4;
74 unsigned int cmt_retries:3;
75 unsigned int nospc_retries:1;
76};
77
78/**
79 * shrink_liability - write-back some dirty pages/inodes. 52 * shrink_liability - write-back some dirty pages/inodes.
80 * @c: UBIFS file-system description object 53 * @c: UBIFS file-system description object
81 * @nr_to_write: how many dirty pages to write-back 54 * @nr_to_write: how many dirty pages to write-back
@@ -147,13 +120,29 @@ static int run_gc(struct ubifs_info *c)
147} 120}
148 121
149/** 122/**
123 * get_liability - calculate current liability.
124 * @c: UBIFS file-system description object
125 *
126 * This function calculates and returns current UBIFS liability, i.e. the
127 * amount of bytes UBIFS has "promised" to write to the media.
128 */
129static long long get_liability(struct ubifs_info *c)
130{
131 long long liab;
132
133 spin_lock(&c->space_lock);
134 liab = c->budg_idx_growth + c->budg_data_growth + c->budg_dd_growth;
135 spin_unlock(&c->space_lock);
136 return liab;
137}
138
139/**
150 * make_free_space - make more free space on the file-system. 140 * make_free_space - make more free space on the file-system.
151 * @c: UBIFS file-system description object 141 * @c: UBIFS file-system description object
152 * @ri: information about previous invocations of this function
153 * 142 *
154 * This function is called when an operation cannot be budgeted because there 143 * This function is called when an operation cannot be budgeted because there
155 * is supposedly no free space. But in most cases there is some free space: 144 * is supposedly no free space. But in most cases there is some free space:
156 * o budgeting is pessimistic, so it always budgets more then it is actually 145 * o budgeting is pessimistic, so it always budgets more than it is actually
157 * needed, so shrinking the liability is one way to make free space - the 146 * needed, so shrinking the liability is one way to make free space - the
158 * cached data will take less space then it was budgeted for; 147 * cached data will take less space then it was budgeted for;
159 * o GC may turn some dark space into free space (budgeting treats dark space 148 * o GC may turn some dark space into free space (budgeting treats dark space
@@ -165,87 +154,42 @@ static int run_gc(struct ubifs_info *c)
165 * Returns %-ENOSPC if it couldn't do more free space, and other negative error 154 * Returns %-ENOSPC if it couldn't do more free space, and other negative error
166 * codes on failures. 155 * codes on failures.
167 */ 156 */
168static int make_free_space(struct ubifs_info *c, struct retries_info *ri) 157static int make_free_space(struct ubifs_info *c)
169{ 158{
170 int err; 159 int err, retries = 0;
160 long long liab1, liab2;
171 161
172 /* 162 do {
173 * If we have some dirty pages and inodes (liability), try to write 163 liab1 = get_liability(c);
174 * them back unless this was tried too many times without effect 164 /*
175 * already. 165 * We probably have some dirty pages or inodes (liability), try
176 */ 166 * to write them back.
177 if (ri->shrink_retries < MAX_SHRINK_RETRIES && !ri->try_gc) { 167 */
178 long long liability; 168 dbg_budg("liability %lld, run write-back", liab1);
179 169 shrink_liability(c, NR_TO_WRITE);
180 spin_lock(&c->space_lock);
181 liability = c->budg_idx_growth + c->budg_data_growth +
182 c->budg_dd_growth;
183 spin_unlock(&c->space_lock);
184
185 if (ri->prev_liability >= liability) {
186 /* Liability does not shrink, next time try GC then */
187 ri->shrink_retries += 1;
188 if (ri->gc_retries < MAX_GC_RETRIES)
189 ri->try_gc = 1;
190 dbg_budg("liability did not shrink: retries %d of %d",
191 ri->shrink_retries, MAX_SHRINK_RETRIES);
192 }
193
194 dbg_budg("force write-back (count %d)", ri->shrink_cnt);
195 shrink_liability(c, NR_TO_WRITE + ri->shrink_cnt);
196 170
197 ri->prev_liability = liability; 171 liab2 = get_liability(c);
198 ri->shrink_cnt += 1; 172 if (liab2 < liab1)
199 return -EAGAIN; 173 return -EAGAIN;
200 }
201 174
202 /* 175 dbg_budg("new liability %lld (not shrinked)", liab2);
203 * Try to run garbage collector unless it was already tried too many
204 * times.
205 */
206 if (ri->gc_retries < MAX_GC_RETRIES) {
207 ri->gc_retries += 1;
208 dbg_budg("run GC, retries %d of %d",
209 ri->gc_retries, MAX_GC_RETRIES);
210 176
211 ri->try_gc = 0; 177 /* Liability did not shrink again, try GC */
178 dbg_budg("Run GC");
212 err = run_gc(c); 179 err = run_gc(c);
213 if (!err) 180 if (!err)
214 return -EAGAIN; 181 return -EAGAIN;
215 182
216 if (err == -EAGAIN) { 183 if (err != -EAGAIN && err != -ENOSPC)
217 dbg_budg("GC asked to commit"); 184 /* Some real error happened */
218 err = ubifs_run_commit(c);
219 if (err)
220 return err;
221 return -EAGAIN;
222 }
223
224 if (err != -ENOSPC)
225 return err;
226
227 /*
228 * GC could not make any progress. If this is the first time,
229 * then it makes sense to try to commit, because it might make
230 * some dirty space.
231 */
232 dbg_budg("GC returned -ENOSPC, retries %d",
233 ri->nospc_retries);
234 if (ri->nospc_retries >= MAX_NOSPC_RETRIES)
235 return err; 185 return err;
236 ri->nospc_retries += 1;
237 }
238 186
239 /* Neither GC nor write-back helped, try to commit */ 187 dbg_budg("Run commit (retries %d)", retries);
240 if (ri->cmt_retries < MAX_CMT_RETRIES) {
241 ri->cmt_retries += 1;
242 dbg_budg("run commit, retries %d of %d",
243 ri->cmt_retries, MAX_CMT_RETRIES);
244 err = ubifs_run_commit(c); 188 err = ubifs_run_commit(c);
245 if (err) 189 if (err)
246 return err; 190 return err;
247 return -EAGAIN; 191 } while (retries++ < MAX_MKSPC_RETRIES);
248 } 192
249 return -ENOSPC; 193 return -ENOSPC;
250} 194}
251 195
@@ -258,8 +202,8 @@ static int make_free_space(struct ubifs_info *c, struct retries_info *ri)
258 */ 202 */
259int ubifs_calc_min_idx_lebs(struct ubifs_info *c) 203int ubifs_calc_min_idx_lebs(struct ubifs_info *c)
260{ 204{
261 int ret; 205 int idx_lebs, eff_leb_size = c->leb_size - c->max_idx_node_sz;
262 uint64_t idx_size; 206 long long idx_size;
263 207
264 idx_size = c->old_idx_sz + c->budg_idx_growth + c->budg_uncommitted_idx; 208 idx_size = c->old_idx_sz + c->budg_idx_growth + c->budg_uncommitted_idx;
265 209
@@ -271,23 +215,16 @@ int ubifs_calc_min_idx_lebs(struct ubifs_info *c)
271 * pair, nor similarly the two variables for the new index size, so we 215 * pair, nor similarly the two variables for the new index size, so we
272 * have to do this costly 64-bit division on fast-path. 216 * have to do this costly 64-bit division on fast-path.
273 */ 217 */
274 if (do_div(idx_size, c->leb_size - c->max_idx_node_sz)) 218 idx_size += eff_leb_size - 1;
275 ret = idx_size + 1; 219 idx_lebs = div_u64(idx_size, eff_leb_size);
276 else
277 ret = idx_size;
278 /* 220 /*
279 * The index head is not available for the in-the-gaps method, so add an 221 * The index head is not available for the in-the-gaps method, so add an
280 * extra LEB to compensate. 222 * extra LEB to compensate.
281 */ 223 */
282 ret += 1; 224 idx_lebs += 1;
283 /* 225 if (idx_lebs < MIN_INDEX_LEBS)
284 * At present the index needs at least 2 LEBs: one for the index head 226 idx_lebs = MIN_INDEX_LEBS;
285 * and one for in-the-gaps method (which currently does not cater for 227 return idx_lebs;
286 * the index head and so excludes it from consideration).
287 */
288 if (ret < 2)
289 ret = 2;
290 return ret;
291} 228}
292 229
293/** 230/**
@@ -530,8 +467,7 @@ static int calc_dd_growth(const struct ubifs_info *c,
530int ubifs_budget_space(struct ubifs_info *c, struct ubifs_budget_req *req) 467int ubifs_budget_space(struct ubifs_info *c, struct ubifs_budget_req *req)
531{ 468{
532 int uninitialized_var(cmt_retries), uninitialized_var(wb_retries); 469 int uninitialized_var(cmt_retries), uninitialized_var(wb_retries);
533 int err, idx_growth, data_growth, dd_growth; 470 int err, idx_growth, data_growth, dd_growth, retried = 0;
534 struct retries_info ri;
535 471
536 ubifs_assert(req->new_page <= 1); 472 ubifs_assert(req->new_page <= 1);
537 ubifs_assert(req->dirtied_page <= 1); 473 ubifs_assert(req->dirtied_page <= 1);
@@ -549,7 +485,6 @@ int ubifs_budget_space(struct ubifs_info *c, struct ubifs_budget_req *req)
549 if (!data_growth && !dd_growth) 485 if (!data_growth && !dd_growth)
550 return 0; 486 return 0;
551 idx_growth = calc_idx_growth(c, req); 487 idx_growth = calc_idx_growth(c, req);
552 memset(&ri, 0, sizeof(struct retries_info));
553 488
554again: 489again:
555 spin_lock(&c->space_lock); 490 spin_lock(&c->space_lock);
@@ -587,12 +522,17 @@ again:
587 return err; 522 return err;
588 } 523 }
589 524
590 err = make_free_space(c, &ri); 525 err = make_free_space(c);
526 cond_resched();
591 if (err == -EAGAIN) { 527 if (err == -EAGAIN) {
592 dbg_budg("try again"); 528 dbg_budg("try again");
593 cond_resched();
594 goto again; 529 goto again;
595 } else if (err == -ENOSPC) { 530 } else if (err == -ENOSPC) {
531 if (!retried) {
532 retried = 1;
533 dbg_budg("-ENOSPC, but anyway try once again");
534 goto again;
535 }
596 dbg_budg("FS is full, -ENOSPC"); 536 dbg_budg("FS is full, -ENOSPC");
597 c->nospace = 1; 537 c->nospace = 1;
598 if (can_use_rp(c) || c->rp_size == 0) 538 if (can_use_rp(c) || c->rp_size == 0)
@@ -666,7 +606,7 @@ void ubifs_release_budget(struct ubifs_info *c, struct ubifs_budget_req *req)
666 * @c: UBIFS file-system description object 606 * @c: UBIFS file-system description object
667 * 607 *
668 * This function converts budget which was allocated for a new page of data to 608 * This function converts budget which was allocated for a new page of data to
669 * the budget of changing an existing page of data. The latter is smaller then 609 * the budget of changing an existing page of data. The latter is smaller than
670 * the former, so this function only does simple re-calculation and does not 610 * the former, so this function only does simple re-calculation and does not
671 * involve any write-back. 611 * involve any write-back.
672 */ 612 */
@@ -712,9 +652,9 @@ void ubifs_release_dirty_inode_budget(struct ubifs_info *c,
712 * user-space. User-space application tend to expect that if the file-system 652 * user-space. User-space application tend to expect that if the file-system
713 * (e.g., via the 'statfs()' call) reports that it has N bytes available, they 653 * (e.g., via the 'statfs()' call) reports that it has N bytes available, they
714 * are able to write a file of size N. UBIFS attaches node headers to each data 654 * are able to write a file of size N. UBIFS attaches node headers to each data
715 * node and it has to write indexind nodes as well. This introduces additional 655 * node and it has to write indexing nodes as well. This introduces additional
716 * overhead, and UBIFS it has to report sligtly less free space to meet the 656 * overhead, and UBIFS has to report slightly less free space to meet the above
717 * above expectetion. 657 * expectations.
718 * 658 *
719 * This function assumes free space is made up of uncompressed data nodes and 659 * This function assumes free space is made up of uncompressed data nodes and
720 * full index nodes (one per data node, tripled because we always allow enough 660 * full index nodes (one per data node, tripled because we always allow enough
@@ -723,7 +663,7 @@ void ubifs_release_dirty_inode_budget(struct ubifs_info *c,
723 * Note, the calculation is pessimistic, which means that most of the time 663 * Note, the calculation is pessimistic, which means that most of the time
724 * UBIFS reports less space than it actually has. 664 * UBIFS reports less space than it actually has.
725 */ 665 */
726long long ubifs_reported_space(const struct ubifs_info *c, uint64_t free) 666long long ubifs_reported_space(const struct ubifs_info *c, long long free)
727{ 667{
728 int divisor, factor, f; 668 int divisor, factor, f;
729 669
@@ -737,7 +677,7 @@ long long ubifs_reported_space(const struct ubifs_info *c, uint64_t free)
737 * of data nodes, f - fanout. Because effective UBIFS fanout is twice 677 * of data nodes, f - fanout. Because effective UBIFS fanout is twice
738 * as less than maximum fanout, we assume that each data node 678 * as less than maximum fanout, we assume that each data node
739 * introduces 3 * @c->max_idx_node_sz / (@c->fanout/2 - 1) bytes. 679 * introduces 3 * @c->max_idx_node_sz / (@c->fanout/2 - 1) bytes.
740 * Note, the multiplier 3 is because UBIFS reseves thrice as more space 680 * Note, the multiplier 3 is because UBIFS reserves thrice as more space
741 * for the index. 681 * for the index.
742 */ 682 */
743 f = c->fanout > 3 ? c->fanout >> 1 : 2; 683 f = c->fanout > 3 ? c->fanout >> 1 : 2;
@@ -745,45 +685,33 @@ long long ubifs_reported_space(const struct ubifs_info *c, uint64_t free)
745 divisor = UBIFS_MAX_DATA_NODE_SZ; 685 divisor = UBIFS_MAX_DATA_NODE_SZ;
746 divisor += (c->max_idx_node_sz * 3) / (f - 1); 686 divisor += (c->max_idx_node_sz * 3) / (f - 1);
747 free *= factor; 687 free *= factor;
748 do_div(free, divisor); 688 return div_u64(free, divisor);
749 return free;
750} 689}
751 690
752/** 691/**
753 * ubifs_get_free_space - return amount of free space. 692 * ubifs_get_free_space_nolock - return amount of free space.
754 * @c: UBIFS file-system description object 693 * @c: UBIFS file-system description object
755 * 694 *
756 * This function calculates amount of free space to report to user-space. 695 * This function calculates amount of free space to report to user-space.
757 * 696 *
758 * Because UBIFS may introduce substantial overhead (the index, node headers, 697 * Because UBIFS may introduce substantial overhead (the index, node headers,
759 * alighment, wastage at the end of eraseblocks, etc), it cannot report real 698 * alignment, wastage at the end of eraseblocks, etc), it cannot report real
760 * amount of free flash space it has (well, because not all dirty space is 699 * amount of free flash space it has (well, because not all dirty space is
761 * reclamable, UBIFS does not actually know the real amount). If UBIFS did so, 700 * reclaimable, UBIFS does not actually know the real amount). If UBIFS did so,
762 * it would bread user expectetion about what free space is. Users seem to 701 * it would bread user expectations about what free space is. Users seem to
763 * accustomed to assume that if the file-system reports N bytes of free space, 702 * accustomed to assume that if the file-system reports N bytes of free space,
764 * they would be able to fit a file of N bytes to the FS. This almost works for 703 * they would be able to fit a file of N bytes to the FS. This almost works for
765 * traditional file-systems, because they have way less overhead than UBIFS. 704 * traditional file-systems, because they have way less overhead than UBIFS.
766 * So, to keep users happy, UBIFS tries to take the overhead into account. 705 * So, to keep users happy, UBIFS tries to take the overhead into account.
767 */ 706 */
768long long ubifs_get_free_space(struct ubifs_info *c) 707long long ubifs_get_free_space_nolock(struct ubifs_info *c)
769{ 708{
770 int min_idx_lebs, rsvd_idx_lebs, lebs; 709 int rsvd_idx_lebs, lebs;
771 long long available, outstanding, free; 710 long long available, outstanding, free;
772 711
773 spin_lock(&c->space_lock); 712 ubifs_assert(c->min_idx_lebs == ubifs_calc_min_idx_lebs(c));
774 min_idx_lebs = ubifs_calc_min_idx_lebs(c);
775 outstanding = c->budg_data_growth + c->budg_dd_growth; 713 outstanding = c->budg_data_growth + c->budg_dd_growth;
776 714 available = ubifs_calc_available(c, c->min_idx_lebs);
777 /*
778 * Force the amount available to the total size reported if the used
779 * space is zero.
780 */
781 if (c->lst.total_used <= UBIFS_INO_NODE_SZ && !outstanding) {
782 spin_unlock(&c->space_lock);
783 return (long long)c->block_cnt << UBIFS_BLOCK_SHIFT;
784 }
785
786 available = ubifs_calc_available(c, min_idx_lebs);
787 715
788 /* 716 /*
789 * When reporting free space to user-space, UBIFS guarantees that it is 717 * When reporting free space to user-space, UBIFS guarantees that it is
@@ -796,15 +724,14 @@ long long ubifs_get_free_space(struct ubifs_info *c)
796 * Note, the calculations below are similar to what we have in 724 * Note, the calculations below are similar to what we have in
797 * 'do_budget_space()', so refer there for comments. 725 * 'do_budget_space()', so refer there for comments.
798 */ 726 */
799 if (min_idx_lebs > c->lst.idx_lebs) 727 if (c->min_idx_lebs > c->lst.idx_lebs)
800 rsvd_idx_lebs = min_idx_lebs - c->lst.idx_lebs; 728 rsvd_idx_lebs = c->min_idx_lebs - c->lst.idx_lebs;
801 else 729 else
802 rsvd_idx_lebs = 0; 730 rsvd_idx_lebs = 0;
803 lebs = c->lst.empty_lebs + c->freeable_cnt + c->idx_gc_cnt - 731 lebs = c->lst.empty_lebs + c->freeable_cnt + c->idx_gc_cnt -
804 c->lst.taken_empty_lebs; 732 c->lst.taken_empty_lebs;
805 lebs -= rsvd_idx_lebs; 733 lebs -= rsvd_idx_lebs;
806 available += lebs * (c->dark_wm - c->leb_overhead); 734 available += lebs * (c->dark_wm - c->leb_overhead);
807 spin_unlock(&c->space_lock);
808 735
809 if (available > outstanding) 736 if (available > outstanding)
810 free = ubifs_reported_space(c, available - outstanding); 737 free = ubifs_reported_space(c, available - outstanding);
@@ -812,3 +739,21 @@ long long ubifs_get_free_space(struct ubifs_info *c)
812 free = 0; 739 free = 0;
813 return free; 740 return free;
814} 741}
742
743/**
744 * ubifs_get_free_space - return amount of free space.
745 * @c: UBIFS file-system description object
746 *
747 * This function calculates and retuns amount of free space to report to
748 * user-space.
749 */
750long long ubifs_get_free_space(struct ubifs_info *c)
751{
752 long long free;
753
754 spin_lock(&c->space_lock);
755 free = ubifs_get_free_space_nolock(c);
756 spin_unlock(&c->space_lock);
757
758 return free;
759}
diff --git a/fs/ubifs/commit.c b/fs/ubifs/commit.c
index b49884c8c10e..f3a7945527fb 100644
--- a/fs/ubifs/commit.c
+++ b/fs/ubifs/commit.c
@@ -470,12 +470,12 @@ int dbg_old_index_check_init(struct ubifs_info *c, struct ubifs_zbranch *zroot)
470{ 470{
471 struct ubifs_idx_node *idx; 471 struct ubifs_idx_node *idx;
472 int lnum, offs, len, err = 0; 472 int lnum, offs, len, err = 0;
473 struct ubifs_debug_info *d = c->dbg;
473 474
474 c->old_zroot = *zroot; 475 d->old_zroot = *zroot;
475 476 lnum = d->old_zroot.lnum;
476 lnum = c->old_zroot.lnum; 477 offs = d->old_zroot.offs;
477 offs = c->old_zroot.offs; 478 len = d->old_zroot.len;
478 len = c->old_zroot.len;
479 479
480 idx = kmalloc(c->max_idx_node_sz, GFP_NOFS); 480 idx = kmalloc(c->max_idx_node_sz, GFP_NOFS);
481 if (!idx) 481 if (!idx)
@@ -485,8 +485,8 @@ int dbg_old_index_check_init(struct ubifs_info *c, struct ubifs_zbranch *zroot)
485 if (err) 485 if (err)
486 goto out; 486 goto out;
487 487
488 c->old_zroot_level = le16_to_cpu(idx->level); 488 d->old_zroot_level = le16_to_cpu(idx->level);
489 c->old_zroot_sqnum = le64_to_cpu(idx->ch.sqnum); 489 d->old_zroot_sqnum = le64_to_cpu(idx->ch.sqnum);
490out: 490out:
491 kfree(idx); 491 kfree(idx);
492 return err; 492 return err;
@@ -509,6 +509,7 @@ int dbg_check_old_index(struct ubifs_info *c, struct ubifs_zbranch *zroot)
509{ 509{
510 int lnum, offs, len, err = 0, uninitialized_var(last_level), child_cnt; 510 int lnum, offs, len, err = 0, uninitialized_var(last_level), child_cnt;
511 int first = 1, iip; 511 int first = 1, iip;
512 struct ubifs_debug_info *d = c->dbg;
512 union ubifs_key lower_key, upper_key, l_key, u_key; 513 union ubifs_key lower_key, upper_key, l_key, u_key;
513 unsigned long long uninitialized_var(last_sqnum); 514 unsigned long long uninitialized_var(last_sqnum);
514 struct ubifs_idx_node *idx; 515 struct ubifs_idx_node *idx;
@@ -525,9 +526,9 @@ int dbg_check_old_index(struct ubifs_info *c, struct ubifs_zbranch *zroot)
525 UBIFS_IDX_NODE_SZ; 526 UBIFS_IDX_NODE_SZ;
526 527
527 /* Start at the old zroot */ 528 /* Start at the old zroot */
528 lnum = c->old_zroot.lnum; 529 lnum = d->old_zroot.lnum;
529 offs = c->old_zroot.offs; 530 offs = d->old_zroot.offs;
530 len = c->old_zroot.len; 531 len = d->old_zroot.len;
531 iip = 0; 532 iip = 0;
532 533
533 /* 534 /*
@@ -560,11 +561,11 @@ int dbg_check_old_index(struct ubifs_info *c, struct ubifs_zbranch *zroot)
560 if (first) { 561 if (first) {
561 first = 0; 562 first = 0;
562 /* Check root level and sqnum */ 563 /* Check root level and sqnum */
563 if (le16_to_cpu(idx->level) != c->old_zroot_level) { 564 if (le16_to_cpu(idx->level) != d->old_zroot_level) {
564 err = 2; 565 err = 2;
565 goto out_dump; 566 goto out_dump;
566 } 567 }
567 if (le64_to_cpu(idx->ch.sqnum) != c->old_zroot_sqnum) { 568 if (le64_to_cpu(idx->ch.sqnum) != d->old_zroot_sqnum) {
568 err = 3; 569 err = 3;
569 goto out_dump; 570 goto out_dump;
570 } 571 }
diff --git a/fs/ubifs/compress.c b/fs/ubifs/compress.c
index a0ada596b17c..11e4132f314a 100644
--- a/fs/ubifs/compress.c
+++ b/fs/ubifs/compress.c
@@ -33,7 +33,7 @@
33/* Fake description object for the "none" compressor */ 33/* Fake description object for the "none" compressor */
34static struct ubifs_compressor none_compr = { 34static struct ubifs_compressor none_compr = {
35 .compr_type = UBIFS_COMPR_NONE, 35 .compr_type = UBIFS_COMPR_NONE,
36 .name = "no compression", 36 .name = "none",
37 .capi_name = "", 37 .capi_name = "",
38}; 38};
39 39
@@ -43,13 +43,13 @@ static DEFINE_MUTEX(lzo_mutex);
43static struct ubifs_compressor lzo_compr = { 43static struct ubifs_compressor lzo_compr = {
44 .compr_type = UBIFS_COMPR_LZO, 44 .compr_type = UBIFS_COMPR_LZO,
45 .comp_mutex = &lzo_mutex, 45 .comp_mutex = &lzo_mutex,
46 .name = "LZO", 46 .name = "lzo",
47 .capi_name = "lzo", 47 .capi_name = "lzo",
48}; 48};
49#else 49#else
50static struct ubifs_compressor lzo_compr = { 50static struct ubifs_compressor lzo_compr = {
51 .compr_type = UBIFS_COMPR_LZO, 51 .compr_type = UBIFS_COMPR_LZO,
52 .name = "LZO", 52 .name = "lzo",
53}; 53};
54#endif 54#endif
55 55
@@ -108,7 +108,7 @@ void ubifs_compress(const void *in_buf, int in_len, void *out_buf, int *out_len,
108 if (compr->comp_mutex) 108 if (compr->comp_mutex)
109 mutex_lock(compr->comp_mutex); 109 mutex_lock(compr->comp_mutex);
110 err = crypto_comp_compress(compr->cc, in_buf, in_len, out_buf, 110 err = crypto_comp_compress(compr->cc, in_buf, in_len, out_buf,
111 out_len); 111 (unsigned int *)out_len);
112 if (compr->comp_mutex) 112 if (compr->comp_mutex)
113 mutex_unlock(compr->comp_mutex); 113 mutex_unlock(compr->comp_mutex);
114 if (unlikely(err)) { 114 if (unlikely(err)) {
@@ -119,10 +119,10 @@ void ubifs_compress(const void *in_buf, int in_len, void *out_buf, int *out_len,
119 } 119 }
120 120
121 /* 121 /*
122 * Presently, we just require that compression results in less data, 122 * If the data compressed only slightly, it is better to leave it
123 * rather than any defined minimum compression ratio or amount. 123 * uncompressed to improve read speed.
124 */ 124 */
125 if (ALIGN(*out_len, 8) >= ALIGN(in_len, 8)) 125 if (in_len - *out_len < UBIFS_MIN_COMPRESS_DIFF)
126 goto no_compr; 126 goto no_compr;
127 127
128 return; 128 return;
@@ -172,7 +172,7 @@ int ubifs_decompress(const void *in_buf, int in_len, void *out_buf,
172 if (compr->decomp_mutex) 172 if (compr->decomp_mutex)
173 mutex_lock(compr->decomp_mutex); 173 mutex_lock(compr->decomp_mutex);
174 err = crypto_comp_decompress(compr->cc, in_buf, in_len, out_buf, 174 err = crypto_comp_decompress(compr->cc, in_buf, in_len, out_buf,
175 out_len); 175 (unsigned int *)out_len);
176 if (compr->decomp_mutex) 176 if (compr->decomp_mutex)
177 mutex_unlock(compr->decomp_mutex); 177 mutex_unlock(compr->decomp_mutex);
178 if (err) 178 if (err)
@@ -244,7 +244,7 @@ out_lzo:
244/** 244/**
245 * ubifs_compressors_exit - de-initialize UBIFS compressors. 245 * ubifs_compressors_exit - de-initialize UBIFS compressors.
246 */ 246 */
247void __exit ubifs_compressors_exit(void) 247void ubifs_compressors_exit(void)
248{ 248{
249 compr_exit(&lzo_compr); 249 compr_exit(&lzo_compr);
250 compr_exit(&zlib_compr); 250 compr_exit(&zlib_compr);
diff --git a/fs/ubifs/debug.c b/fs/ubifs/debug.c
index 510ffa0bbda4..e975bd82f38b 100644
--- a/fs/ubifs/debug.c
+++ b/fs/ubifs/debug.c
@@ -32,6 +32,8 @@
32#include "ubifs.h" 32#include "ubifs.h"
33#include <linux/module.h> 33#include <linux/module.h>
34#include <linux/moduleparam.h> 34#include <linux/moduleparam.h>
35#include <linux/debugfs.h>
36#include <linux/math64.h>
35 37
36#ifdef CONFIG_UBIFS_FS_DEBUG 38#ifdef CONFIG_UBIFS_FS_DEBUG
37 39
@@ -596,7 +598,9 @@ void dbg_dump_budg(struct ubifs_info *c)
596 struct rb_node *rb; 598 struct rb_node *rb;
597 struct ubifs_bud *bud; 599 struct ubifs_bud *bud;
598 struct ubifs_gced_idx_leb *idx_gc; 600 struct ubifs_gced_idx_leb *idx_gc;
601 long long available, outstanding, free;
599 602
603 ubifs_assert(spin_is_locked(&c->space_lock));
600 spin_lock(&dbg_lock); 604 spin_lock(&dbg_lock);
601 printk(KERN_DEBUG "(pid %d) Budgeting info: budg_data_growth %lld, " 605 printk(KERN_DEBUG "(pid %d) Budgeting info: budg_data_growth %lld, "
602 "budg_dd_growth %lld, budg_idx_growth %lld\n", current->pid, 606 "budg_dd_growth %lld, budg_idx_growth %lld\n", current->pid,
@@ -616,9 +620,11 @@ void dbg_dump_budg(struct ubifs_info *c)
616 c->dark_wm, c->dead_wm, c->max_idx_node_sz); 620 c->dark_wm, c->dead_wm, c->max_idx_node_sz);
617 printk(KERN_DEBUG "\tgc_lnum %d, ihead_lnum %d\n", 621 printk(KERN_DEBUG "\tgc_lnum %d, ihead_lnum %d\n",
618 c->gc_lnum, c->ihead_lnum); 622 c->gc_lnum, c->ihead_lnum);
619 for (i = 0; i < c->jhead_cnt; i++) 623 /* If we are in R/O mode, journal heads do not exist */
620 printk(KERN_DEBUG "\tjhead %d\t LEB %d\n", 624 if (c->jheads)
621 c->jheads[i].wbuf.jhead, c->jheads[i].wbuf.lnum); 625 for (i = 0; i < c->jhead_cnt; i++)
626 printk(KERN_DEBUG "\tjhead %d\t LEB %d\n",
627 c->jheads[i].wbuf.jhead, c->jheads[i].wbuf.lnum);
622 for (rb = rb_first(&c->buds); rb; rb = rb_next(rb)) { 628 for (rb = rb_first(&c->buds); rb; rb = rb_next(rb)) {
623 bud = rb_entry(rb, struct ubifs_bud, rb); 629 bud = rb_entry(rb, struct ubifs_bud, rb);
624 printk(KERN_DEBUG "\tbud LEB %d\n", bud->lnum); 630 printk(KERN_DEBUG "\tbud LEB %d\n", bud->lnum);
@@ -629,6 +635,14 @@ void dbg_dump_budg(struct ubifs_info *c)
629 printk(KERN_DEBUG "\tGC'ed idx LEB %d unmap %d\n", 635 printk(KERN_DEBUG "\tGC'ed idx LEB %d unmap %d\n",
630 idx_gc->lnum, idx_gc->unmap); 636 idx_gc->lnum, idx_gc->unmap);
631 printk(KERN_DEBUG "\tcommit state %d\n", c->cmt_state); 637 printk(KERN_DEBUG "\tcommit state %d\n", c->cmt_state);
638
639 /* Print budgeting predictions */
640 available = ubifs_calc_available(c, c->min_idx_lebs);
641 outstanding = c->budg_data_growth + c->budg_dd_growth;
642 free = ubifs_get_free_space_nolock(c);
643 printk(KERN_DEBUG "Budgeting predictions:\n");
644 printk(KERN_DEBUG "\tavailable: %lld, outstanding %lld, free %lld\n",
645 available, outstanding, free);
632 spin_unlock(&dbg_lock); 646 spin_unlock(&dbg_lock);
633} 647}
634 648
@@ -645,7 +659,8 @@ void dbg_dump_lprops(struct ubifs_info *c)
645 struct ubifs_lprops lp; 659 struct ubifs_lprops lp;
646 struct ubifs_lp_stats lst; 660 struct ubifs_lp_stats lst;
647 661
648 printk(KERN_DEBUG "(pid %d) Dumping LEB properties\n", current->pid); 662 printk(KERN_DEBUG "(pid %d) start dumping LEB properties\n",
663 current->pid);
649 ubifs_get_lp_stats(c, &lst); 664 ubifs_get_lp_stats(c, &lst);
650 dbg_dump_lstats(&lst); 665 dbg_dump_lstats(&lst);
651 666
@@ -656,6 +671,8 @@ void dbg_dump_lprops(struct ubifs_info *c)
656 671
657 dbg_dump_lprop(c, &lp); 672 dbg_dump_lprop(c, &lp);
658 } 673 }
674 printk(KERN_DEBUG "(pid %d) finish dumping LEB properties\n",
675 current->pid);
659} 676}
660 677
661void dbg_dump_lpt_info(struct ubifs_info *c) 678void dbg_dump_lpt_info(struct ubifs_info *c)
@@ -663,6 +680,7 @@ void dbg_dump_lpt_info(struct ubifs_info *c)
663 int i; 680 int i;
664 681
665 spin_lock(&dbg_lock); 682 spin_lock(&dbg_lock);
683 printk(KERN_DEBUG "(pid %d) dumping LPT information\n", current->pid);
666 printk(KERN_DEBUG "\tlpt_sz: %lld\n", c->lpt_sz); 684 printk(KERN_DEBUG "\tlpt_sz: %lld\n", c->lpt_sz);
667 printk(KERN_DEBUG "\tpnode_sz: %d\n", c->pnode_sz); 685 printk(KERN_DEBUG "\tpnode_sz: %d\n", c->pnode_sz);
668 printk(KERN_DEBUG "\tnnode_sz: %d\n", c->nnode_sz); 686 printk(KERN_DEBUG "\tnnode_sz: %d\n", c->nnode_sz);
@@ -684,7 +702,8 @@ void dbg_dump_lpt_info(struct ubifs_info *c)
684 printk(KERN_DEBUG "\tLPT root is at %d:%d\n", c->lpt_lnum, c->lpt_offs); 702 printk(KERN_DEBUG "\tLPT root is at %d:%d\n", c->lpt_lnum, c->lpt_offs);
685 printk(KERN_DEBUG "\tLPT head is at %d:%d\n", 703 printk(KERN_DEBUG "\tLPT head is at %d:%d\n",
686 c->nhead_lnum, c->nhead_offs); 704 c->nhead_lnum, c->nhead_offs);
687 printk(KERN_DEBUG "\tLPT ltab is at %d:%d\n", c->ltab_lnum, c->ltab_offs); 705 printk(KERN_DEBUG "\tLPT ltab is at %d:%d\n",
706 c->ltab_lnum, c->ltab_offs);
688 if (c->big_lpt) 707 if (c->big_lpt)
689 printk(KERN_DEBUG "\tLPT lsave is at %d:%d\n", 708 printk(KERN_DEBUG "\tLPT lsave is at %d:%d\n",
690 c->lsave_lnum, c->lsave_offs); 709 c->lsave_lnum, c->lsave_offs);
@@ -703,9 +722,9 @@ void dbg_dump_leb(const struct ubifs_info *c, int lnum)
703 if (dbg_failure_mode) 722 if (dbg_failure_mode)
704 return; 723 return;
705 724
706 printk(KERN_DEBUG "(pid %d) Dumping LEB %d\n", current->pid, lnum); 725 printk(KERN_DEBUG "(pid %d) start dumping LEB %d\n",
707 726 current->pid, lnum);
708 sleb = ubifs_scan(c, lnum, 0, c->dbg_buf); 727 sleb = ubifs_scan(c, lnum, 0, c->dbg->buf);
709 if (IS_ERR(sleb)) { 728 if (IS_ERR(sleb)) {
710 ubifs_err("scan error %d", (int)PTR_ERR(sleb)); 729 ubifs_err("scan error %d", (int)PTR_ERR(sleb));
711 return; 730 return;
@@ -721,6 +740,8 @@ void dbg_dump_leb(const struct ubifs_info *c, int lnum)
721 dbg_dump_node(c, snod->node); 740 dbg_dump_node(c, snod->node);
722 } 741 }
723 742
743 printk(KERN_DEBUG "(pid %d) finish dumping LEB %d\n",
744 current->pid, lnum);
724 ubifs_scan_destroy(sleb); 745 ubifs_scan_destroy(sleb);
725 return; 746 return;
726} 747}
@@ -768,7 +789,7 @@ void dbg_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat)
768{ 789{
769 int i; 790 int i;
770 791
771 printk(KERN_DEBUG "(pid %d) Dumping heap cat %d (%d elements)\n", 792 printk(KERN_DEBUG "(pid %d) start dumping heap cat %d (%d elements)\n",
772 current->pid, cat, heap->cnt); 793 current->pid, cat, heap->cnt);
773 for (i = 0; i < heap->cnt; i++) { 794 for (i = 0; i < heap->cnt; i++) {
774 struct ubifs_lprops *lprops = heap->arr[i]; 795 struct ubifs_lprops *lprops = heap->arr[i];
@@ -777,6 +798,7 @@ void dbg_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat)
777 "flags %d\n", i, lprops->lnum, lprops->hpos, 798 "flags %d\n", i, lprops->lnum, lprops->hpos,
778 lprops->free, lprops->dirty, lprops->flags); 799 lprops->free, lprops->dirty, lprops->flags);
779 } 800 }
801 printk(KERN_DEBUG "(pid %d) finish dumping heap\n", current->pid);
780} 802}
781 803
782void dbg_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode, 804void dbg_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
@@ -784,7 +806,7 @@ void dbg_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
784{ 806{
785 int i; 807 int i;
786 808
787 printk(KERN_DEBUG "(pid %d) Dumping pnode:\n", current->pid); 809 printk(KERN_DEBUG "(pid %d) dumping pnode:\n", current->pid);
788 printk(KERN_DEBUG "\taddress %zx parent %zx cnext %zx\n", 810 printk(KERN_DEBUG "\taddress %zx parent %zx cnext %zx\n",
789 (size_t)pnode, (size_t)parent, (size_t)pnode->cnext); 811 (size_t)pnode, (size_t)parent, (size_t)pnode->cnext);
790 printk(KERN_DEBUG "\tflags %lu iip %d level %d num %d\n", 812 printk(KERN_DEBUG "\tflags %lu iip %d level %d num %d\n",
@@ -803,7 +825,7 @@ void dbg_dump_tnc(struct ubifs_info *c)
803 int level; 825 int level;
804 826
805 printk(KERN_DEBUG "\n"); 827 printk(KERN_DEBUG "\n");
806 printk(KERN_DEBUG "(pid %d) Dumping the TNC tree\n", current->pid); 828 printk(KERN_DEBUG "(pid %d) start dumping TNC tree\n", current->pid);
807 znode = ubifs_tnc_levelorder_next(c->zroot.znode, NULL); 829 znode = ubifs_tnc_levelorder_next(c->zroot.znode, NULL);
808 level = znode->level; 830 level = znode->level;
809 printk(KERN_DEBUG "== Level %d ==\n", level); 831 printk(KERN_DEBUG "== Level %d ==\n", level);
@@ -815,8 +837,7 @@ void dbg_dump_tnc(struct ubifs_info *c)
815 dbg_dump_znode(c, znode); 837 dbg_dump_znode(c, znode);
816 znode = ubifs_tnc_levelorder_next(c->zroot.znode, znode); 838 znode = ubifs_tnc_levelorder_next(c->zroot.znode, znode);
817 } 839 }
818 840 printk(KERN_DEBUG "(pid %d) finish dumping TNC tree\n", current->pid);
819 printk(KERN_DEBUG "\n");
820} 841}
821 842
822static int dump_znode(struct ubifs_info *c, struct ubifs_znode *znode, 843static int dump_znode(struct ubifs_info *c, struct ubifs_znode *znode,
@@ -839,6 +860,65 @@ void dbg_dump_index(struct ubifs_info *c)
839} 860}
840 861
841/** 862/**
863 * dbg_save_space_info - save information about flash space.
864 * @c: UBIFS file-system description object
865 *
866 * This function saves information about UBIFS free space, dirty space, etc, in
867 * order to check it later.
868 */
869void dbg_save_space_info(struct ubifs_info *c)
870{
871 struct ubifs_debug_info *d = c->dbg;
872
873 ubifs_get_lp_stats(c, &d->saved_lst);
874
875 spin_lock(&c->space_lock);
876 d->saved_free = ubifs_get_free_space_nolock(c);
877 spin_unlock(&c->space_lock);
878}
879
880/**
881 * dbg_check_space_info - check flash space information.
882 * @c: UBIFS file-system description object
883 *
884 * This function compares current flash space information with the information
885 * which was saved when the 'dbg_save_space_info()' function was called.
886 * Returns zero if the information has not changed, and %-EINVAL it it has
887 * changed.
888 */
889int dbg_check_space_info(struct ubifs_info *c)
890{
891 struct ubifs_debug_info *d = c->dbg;
892 struct ubifs_lp_stats lst;
893 long long avail, free;
894
895 spin_lock(&c->space_lock);
896 avail = ubifs_calc_available(c, c->min_idx_lebs);
897 spin_unlock(&c->space_lock);
898 free = ubifs_get_free_space(c);
899
900 if (free != d->saved_free) {
901 ubifs_err("free space changed from %lld to %lld",
902 d->saved_free, free);
903 goto out;
904 }
905
906 return 0;
907
908out:
909 ubifs_msg("saved lprops statistics dump");
910 dbg_dump_lstats(&d->saved_lst);
911 ubifs_get_lp_stats(c, &lst);
912 ubifs_msg("current lprops statistics dump");
913 dbg_dump_lstats(&d->saved_lst);
914 spin_lock(&c->space_lock);
915 dbg_dump_budg(c);
916 spin_unlock(&c->space_lock);
917 dump_stack();
918 return -EINVAL;
919}
920
921/**
842 * dbg_check_synced_i_size - check synchronized inode size. 922 * dbg_check_synced_i_size - check synchronized inode size.
843 * @inode: inode to check 923 * @inode: inode to check
844 * 924 *
@@ -992,8 +1072,8 @@ static int dbg_check_key_order(struct ubifs_info *c, struct ubifs_zbranch *zbr1,
992 zbr1->offs, DBGKEY(&key)); 1072 zbr1->offs, DBGKEY(&key));
993 dbg_err("but it should have key %s according to tnc", 1073 dbg_err("but it should have key %s according to tnc",
994 DBGKEY(&zbr1->key)); 1074 DBGKEY(&zbr1->key));
995 dbg_dump_node(c, dent1); 1075 dbg_dump_node(c, dent1);
996 goto out_free; 1076 goto out_free;
997 } 1077 }
998 1078
999 key_read(c, &dent2->key, &key); 1079 key_read(c, &dent2->key, &key);
@@ -1002,8 +1082,8 @@ static int dbg_check_key_order(struct ubifs_info *c, struct ubifs_zbranch *zbr1,
1002 zbr1->offs, DBGKEY(&key)); 1082 zbr1->offs, DBGKEY(&key));
1003 dbg_err("but it should have key %s according to tnc", 1083 dbg_err("but it should have key %s according to tnc",
1004 DBGKEY(&zbr2->key)); 1084 DBGKEY(&zbr2->key));
1005 dbg_dump_node(c, dent2); 1085 dbg_dump_node(c, dent2);
1006 goto out_free; 1086 goto out_free;
1007 } 1087 }
1008 1088
1009 nlen1 = le16_to_cpu(dent1->nlen); 1089 nlen1 = le16_to_cpu(dent1->nlen);
@@ -1020,9 +1100,9 @@ static int dbg_check_key_order(struct ubifs_info *c, struct ubifs_zbranch *zbr1,
1020 dbg_err("bad order of colliding key %s", 1100 dbg_err("bad order of colliding key %s",
1021 DBGKEY(&key)); 1101 DBGKEY(&key));
1022 1102
1023 dbg_msg("first node at %d:%d\n", zbr1->lnum, zbr1->offs); 1103 ubifs_msg("first node at %d:%d\n", zbr1->lnum, zbr1->offs);
1024 dbg_dump_node(c, dent1); 1104 dbg_dump_node(c, dent1);
1025 dbg_msg("second node at %d:%d\n", zbr2->lnum, zbr2->offs); 1105 ubifs_msg("second node at %d:%d\n", zbr2->lnum, zbr2->offs);
1026 dbg_dump_node(c, dent2); 1106 dbg_dump_node(c, dent2);
1027 1107
1028out_free: 1108out_free:
@@ -1327,7 +1407,7 @@ int dbg_check_tnc(struct ubifs_info *c, int extra)
1327 * @c: UBIFS file-system description object 1407 * @c: UBIFS file-system description object
1328 * @leaf_cb: called for each leaf node 1408 * @leaf_cb: called for each leaf node
1329 * @znode_cb: called for each indexing node 1409 * @znode_cb: called for each indexing node
1330 * @priv: private date which is passed to callbacks 1410 * @priv: private data which is passed to callbacks
1331 * 1411 *
1332 * This function walks the UBIFS index and calls the @leaf_cb for each leaf 1412 * This function walks the UBIFS index and calls the @leaf_cb for each leaf
1333 * node and @znode_cb for each indexing node. Returns zero in case of success 1413 * node and @znode_cb for each indexing node. Returns zero in case of success
@@ -2097,13 +2177,13 @@ static int simple_rand(void)
2097 return (next >> 16) & 32767; 2177 return (next >> 16) & 32767;
2098} 2178}
2099 2179
2100void dbg_failure_mode_registration(struct ubifs_info *c) 2180static void failure_mode_init(struct ubifs_info *c)
2101{ 2181{
2102 struct failure_mode_info *fmi; 2182 struct failure_mode_info *fmi;
2103 2183
2104 fmi = kmalloc(sizeof(struct failure_mode_info), GFP_NOFS); 2184 fmi = kmalloc(sizeof(struct failure_mode_info), GFP_NOFS);
2105 if (!fmi) { 2185 if (!fmi) {
2106 dbg_err("Failed to register failure mode - no memory"); 2186 ubifs_err("Failed to register failure mode - no memory");
2107 return; 2187 return;
2108 } 2188 }
2109 fmi->c = c; 2189 fmi->c = c;
@@ -2112,7 +2192,7 @@ void dbg_failure_mode_registration(struct ubifs_info *c)
2112 spin_unlock(&fmi_lock); 2192 spin_unlock(&fmi_lock);
2113} 2193}
2114 2194
2115void dbg_failure_mode_deregistration(struct ubifs_info *c) 2195static void failure_mode_exit(struct ubifs_info *c)
2116{ 2196{
2117 struct failure_mode_info *fmi, *tmp; 2197 struct failure_mode_info *fmi, *tmp;
2118 2198
@@ -2146,42 +2226,44 @@ static int in_failure_mode(struct ubi_volume_desc *desc)
2146 struct ubifs_info *c = dbg_find_info(desc); 2226 struct ubifs_info *c = dbg_find_info(desc);
2147 2227
2148 if (c && dbg_failure_mode) 2228 if (c && dbg_failure_mode)
2149 return c->failure_mode; 2229 return c->dbg->failure_mode;
2150 return 0; 2230 return 0;
2151} 2231}
2152 2232
2153static int do_fail(struct ubi_volume_desc *desc, int lnum, int write) 2233static int do_fail(struct ubi_volume_desc *desc, int lnum, int write)
2154{ 2234{
2155 struct ubifs_info *c = dbg_find_info(desc); 2235 struct ubifs_info *c = dbg_find_info(desc);
2236 struct ubifs_debug_info *d;
2156 2237
2157 if (!c || !dbg_failure_mode) 2238 if (!c || !dbg_failure_mode)
2158 return 0; 2239 return 0;
2159 if (c->failure_mode) 2240 d = c->dbg;
2241 if (d->failure_mode)
2160 return 1; 2242 return 1;
2161 if (!c->fail_cnt) { 2243 if (!d->fail_cnt) {
2162 /* First call - decide delay to failure */ 2244 /* First call - decide delay to failure */
2163 if (chance(1, 2)) { 2245 if (chance(1, 2)) {
2164 unsigned int delay = 1 << (simple_rand() >> 11); 2246 unsigned int delay = 1 << (simple_rand() >> 11);
2165 2247
2166 if (chance(1, 2)) { 2248 if (chance(1, 2)) {
2167 c->fail_delay = 1; 2249 d->fail_delay = 1;
2168 c->fail_timeout = jiffies + 2250 d->fail_timeout = jiffies +
2169 msecs_to_jiffies(delay); 2251 msecs_to_jiffies(delay);
2170 dbg_rcvry("failing after %ums", delay); 2252 dbg_rcvry("failing after %ums", delay);
2171 } else { 2253 } else {
2172 c->fail_delay = 2; 2254 d->fail_delay = 2;
2173 c->fail_cnt_max = delay; 2255 d->fail_cnt_max = delay;
2174 dbg_rcvry("failing after %u calls", delay); 2256 dbg_rcvry("failing after %u calls", delay);
2175 } 2257 }
2176 } 2258 }
2177 c->fail_cnt += 1; 2259 d->fail_cnt += 1;
2178 } 2260 }
2179 /* Determine if failure delay has expired */ 2261 /* Determine if failure delay has expired */
2180 if (c->fail_delay == 1) { 2262 if (d->fail_delay == 1) {
2181 if (time_before(jiffies, c->fail_timeout)) 2263 if (time_before(jiffies, d->fail_timeout))
2182 return 0; 2264 return 0;
2183 } else if (c->fail_delay == 2) 2265 } else if (d->fail_delay == 2)
2184 if (c->fail_cnt++ < c->fail_cnt_max) 2266 if (d->fail_cnt++ < d->fail_cnt_max)
2185 return 0; 2267 return 0;
2186 if (lnum == UBIFS_SB_LNUM) { 2268 if (lnum == UBIFS_SB_LNUM) {
2187 if (write) { 2269 if (write) {
@@ -2239,7 +2321,7 @@ static int do_fail(struct ubi_volume_desc *desc, int lnum, int write)
2239 dbg_rcvry("failing in bud LEB %d commit not running", lnum); 2321 dbg_rcvry("failing in bud LEB %d commit not running", lnum);
2240 } 2322 }
2241 ubifs_err("*** SETTING FAILURE MODE ON (LEB %d) ***", lnum); 2323 ubifs_err("*** SETTING FAILURE MODE ON (LEB %d) ***", lnum);
2242 c->failure_mode = 1; 2324 d->failure_mode = 1;
2243 dump_stack(); 2325 dump_stack();
2244 return 1; 2326 return 1;
2245} 2327}
@@ -2344,4 +2426,177 @@ int dbg_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype)
2344 return 0; 2426 return 0;
2345} 2427}
2346 2428
2429/**
2430 * ubifs_debugging_init - initialize UBIFS debugging.
2431 * @c: UBIFS file-system description object
2432 *
2433 * This function initializes debugging-related data for the file system.
2434 * Returns zero in case of success and a negative error code in case of
2435 * failure.
2436 */
2437int ubifs_debugging_init(struct ubifs_info *c)
2438{
2439 c->dbg = kzalloc(sizeof(struct ubifs_debug_info), GFP_KERNEL);
2440 if (!c->dbg)
2441 return -ENOMEM;
2442
2443 c->dbg->buf = vmalloc(c->leb_size);
2444 if (!c->dbg->buf)
2445 goto out;
2446
2447 failure_mode_init(c);
2448 return 0;
2449
2450out:
2451 kfree(c->dbg);
2452 return -ENOMEM;
2453}
2454
2455/**
2456 * ubifs_debugging_exit - free debugging data.
2457 * @c: UBIFS file-system description object
2458 */
2459void ubifs_debugging_exit(struct ubifs_info *c)
2460{
2461 failure_mode_exit(c);
2462 vfree(c->dbg->buf);
2463 kfree(c->dbg);
2464}
2465
2466/*
2467 * Root directory for UBIFS stuff in debugfs. Contains sub-directories which
2468 * contain the stuff specific to particular file-system mounts.
2469 */
2470static struct dentry *dfs_rootdir;
2471
2472/**
2473 * dbg_debugfs_init - initialize debugfs file-system.
2474 *
2475 * UBIFS uses debugfs file-system to expose various debugging knobs to
2476 * user-space. This function creates "ubifs" directory in the debugfs
2477 * file-system. Returns zero in case of success and a negative error code in
2478 * case of failure.
2479 */
2480int dbg_debugfs_init(void)
2481{
2482 dfs_rootdir = debugfs_create_dir("ubifs", NULL);
2483 if (IS_ERR(dfs_rootdir)) {
2484 int err = PTR_ERR(dfs_rootdir);
2485 ubifs_err("cannot create \"ubifs\" debugfs directory, "
2486 "error %d\n", err);
2487 return err;
2488 }
2489
2490 return 0;
2491}
2492
2493/**
2494 * dbg_debugfs_exit - remove the "ubifs" directory from debugfs file-system.
2495 */
2496void dbg_debugfs_exit(void)
2497{
2498 debugfs_remove(dfs_rootdir);
2499}
2500
2501static int open_debugfs_file(struct inode *inode, struct file *file)
2502{
2503 file->private_data = inode->i_private;
2504 return 0;
2505}
2506
2507static ssize_t write_debugfs_file(struct file *file, const char __user *buf,
2508 size_t count, loff_t *ppos)
2509{
2510 struct ubifs_info *c = file->private_data;
2511 struct ubifs_debug_info *d = c->dbg;
2512
2513 if (file->f_path.dentry == d->dfs_dump_lprops)
2514 dbg_dump_lprops(c);
2515 else if (file->f_path.dentry == d->dfs_dump_budg) {
2516 spin_lock(&c->space_lock);
2517 dbg_dump_budg(c);
2518 spin_unlock(&c->space_lock);
2519 } else if (file->f_path.dentry == d->dfs_dump_tnc) {
2520 mutex_lock(&c->tnc_mutex);
2521 dbg_dump_tnc(c);
2522 mutex_unlock(&c->tnc_mutex);
2523 } else
2524 return -EINVAL;
2525
2526 *ppos += count;
2527 return count;
2528}
2529
2530static const struct file_operations dfs_fops = {
2531 .open = open_debugfs_file,
2532 .write = write_debugfs_file,
2533 .owner = THIS_MODULE,
2534};
2535
2536/**
2537 * dbg_debugfs_init_fs - initialize debugfs for UBIFS instance.
2538 * @c: UBIFS file-system description object
2539 *
2540 * This function creates all debugfs files for this instance of UBIFS. Returns
2541 * zero in case of success and a negative error code in case of failure.
2542 *
2543 * Note, the only reason we have not merged this function with the
2544 * 'ubifs_debugging_init()' function is because it is better to initialize
2545 * debugfs interfaces at the very end of the mount process, and remove them at
2546 * the very beginning of the mount process.
2547 */
2548int dbg_debugfs_init_fs(struct ubifs_info *c)
2549{
2550 int err;
2551 const char *fname;
2552 struct dentry *dent;
2553 struct ubifs_debug_info *d = c->dbg;
2554
2555 sprintf(d->dfs_dir_name, "ubi%d_%d", c->vi.ubi_num, c->vi.vol_id);
2556 d->dfs_dir = debugfs_create_dir(d->dfs_dir_name, dfs_rootdir);
2557 if (IS_ERR(d->dfs_dir)) {
2558 err = PTR_ERR(d->dfs_dir);
2559 ubifs_err("cannot create \"%s\" debugfs directory, error %d\n",
2560 d->dfs_dir_name, err);
2561 goto out;
2562 }
2563
2564 fname = "dump_lprops";
2565 dent = debugfs_create_file(fname, S_IWUGO, d->dfs_dir, c, &dfs_fops);
2566 if (IS_ERR(dent))
2567 goto out_remove;
2568 d->dfs_dump_lprops = dent;
2569
2570 fname = "dump_budg";
2571 dent = debugfs_create_file(fname, S_IWUGO, d->dfs_dir, c, &dfs_fops);
2572 if (IS_ERR(dent))
2573 goto out_remove;
2574 d->dfs_dump_budg = dent;
2575
2576 fname = "dump_tnc";
2577 dent = debugfs_create_file(fname, S_IWUGO, d->dfs_dir, c, &dfs_fops);
2578 if (IS_ERR(dent))
2579 goto out_remove;
2580 d->dfs_dump_tnc = dent;
2581
2582 return 0;
2583
2584out_remove:
2585 err = PTR_ERR(dent);
2586 ubifs_err("cannot create \"%s\" debugfs directory, error %d\n",
2587 fname, err);
2588 debugfs_remove_recursive(d->dfs_dir);
2589out:
2590 return err;
2591}
2592
2593/**
2594 * dbg_debugfs_exit_fs - remove all debugfs files.
2595 * @c: UBIFS file-system description object
2596 */
2597void dbg_debugfs_exit_fs(struct ubifs_info *c)
2598{
2599 debugfs_remove_recursive(c->dbg->dfs_dir);
2600}
2601
2347#endif /* CONFIG_UBIFS_FS_DEBUG */ 2602#endif /* CONFIG_UBIFS_FS_DEBUG */
diff --git a/fs/ubifs/debug.h b/fs/ubifs/debug.h
index 33d6b95071e4..c1cd73b2e06e 100644
--- a/fs/ubifs/debug.h
+++ b/fs/ubifs/debug.h
@@ -25,7 +25,61 @@
25 25
26#ifdef CONFIG_UBIFS_FS_DEBUG 26#ifdef CONFIG_UBIFS_FS_DEBUG
27 27
28#define UBIFS_DBG(op) op 28/**
29 * ubifs_debug_info - per-FS debugging information.
30 * @buf: a buffer of LEB size, used for various purposes
31 * @old_zroot: old index root - used by 'dbg_check_old_index()'
32 * @old_zroot_level: old index root level - used by 'dbg_check_old_index()'
33 * @old_zroot_sqnum: old index root sqnum - used by 'dbg_check_old_index()'
34 * @failure_mode: failure mode for recovery testing
35 * @fail_delay: 0=>don't delay, 1=>delay a time, 2=>delay a number of calls
36 * @fail_timeout: time in jiffies when delay of failure mode expires
37 * @fail_cnt: current number of calls to failure mode I/O functions
38 * @fail_cnt_max: number of calls by which to delay failure mode
39 * @chk_lpt_sz: used by LPT tree size checker
40 * @chk_lpt_sz2: used by LPT tree size checker
41 * @chk_lpt_wastage: used by LPT tree size checker
42 * @chk_lpt_lebs: used by LPT tree size checker
43 * @new_nhead_offs: used by LPT tree size checker
44 * @new_ihead_lnum: used by debugging to check @c->ihead_lnum
45 * @new_ihead_offs: used by debugging to check @c->ihead_offs
46 *
47 * @saved_lst: saved lprops statistics (used by 'dbg_save_space_info()')
48 * @saved_free: saved free space (used by 'dbg_save_space_info()')
49 *
50 * dfs_dir_name: name of debugfs directory containing this file-system's files
51 * dfs_dir: direntry object of the file-system debugfs directory
52 * dfs_dump_lprops: "dump lprops" debugfs knob
53 * dfs_dump_budg: "dump budgeting information" debugfs knob
54 * dfs_dump_tnc: "dump TNC" debugfs knob
55 */
56struct ubifs_debug_info {
57 void *buf;
58 struct ubifs_zbranch old_zroot;
59 int old_zroot_level;
60 unsigned long long old_zroot_sqnum;
61 int failure_mode;
62 int fail_delay;
63 unsigned long fail_timeout;
64 unsigned int fail_cnt;
65 unsigned int fail_cnt_max;
66 long long chk_lpt_sz;
67 long long chk_lpt_sz2;
68 long long chk_lpt_wastage;
69 int chk_lpt_lebs;
70 int new_nhead_offs;
71 int new_ihead_lnum;
72 int new_ihead_offs;
73
74 struct ubifs_lp_stats saved_lst;
75 long long saved_free;
76
77 char dfs_dir_name[100];
78 struct dentry *dfs_dir;
79 struct dentry *dfs_dump_lprops;
80 struct dentry *dfs_dump_budg;
81 struct dentry *dfs_dump_tnc;
82};
29 83
30#define ubifs_assert(expr) do { \ 84#define ubifs_assert(expr) do { \
31 if (unlikely(!(expr))) { \ 85 if (unlikely(!(expr))) { \
@@ -211,14 +265,18 @@ extern unsigned int ubifs_msg_flags;
211extern unsigned int ubifs_chk_flags; 265extern unsigned int ubifs_chk_flags;
212extern unsigned int ubifs_tst_flags; 266extern unsigned int ubifs_tst_flags;
213 267
214/* Dump functions */ 268int ubifs_debugging_init(struct ubifs_info *c);
269void ubifs_debugging_exit(struct ubifs_info *c);
215 270
271/* Dump functions */
216const char *dbg_ntype(int type); 272const char *dbg_ntype(int type);
217const char *dbg_cstate(int cmt_state); 273const char *dbg_cstate(int cmt_state);
218const char *dbg_get_key_dump(const struct ubifs_info *c, 274const char *dbg_get_key_dump(const struct ubifs_info *c,
219 const union ubifs_key *key); 275 const union ubifs_key *key);
220void dbg_dump_inode(const struct ubifs_info *c, const struct inode *inode); 276void dbg_dump_inode(const struct ubifs_info *c, const struct inode *inode);
221void dbg_dump_node(const struct ubifs_info *c, const void *node); 277void dbg_dump_node(const struct ubifs_info *c, const void *node);
278void dbg_dump_lpt_node(const struct ubifs_info *c, void *node, int lnum,
279 int offs);
222void dbg_dump_budget_req(const struct ubifs_budget_req *req); 280void dbg_dump_budget_req(const struct ubifs_budget_req *req);
223void dbg_dump_lstats(const struct ubifs_lp_stats *lst); 281void dbg_dump_lstats(const struct ubifs_lp_stats *lst);
224void dbg_dump_budg(struct ubifs_info *c); 282void dbg_dump_budg(struct ubifs_info *c);
@@ -233,9 +291,9 @@ void dbg_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
233 struct ubifs_nnode *parent, int iip); 291 struct ubifs_nnode *parent, int iip);
234void dbg_dump_tnc(struct ubifs_info *c); 292void dbg_dump_tnc(struct ubifs_info *c);
235void dbg_dump_index(struct ubifs_info *c); 293void dbg_dump_index(struct ubifs_info *c);
294void dbg_dump_lpt_lebs(const struct ubifs_info *c);
236 295
237/* Checking helper functions */ 296/* Checking helper functions */
238
239typedef int (*dbg_leaf_callback)(struct ubifs_info *c, 297typedef int (*dbg_leaf_callback)(struct ubifs_info *c,
240 struct ubifs_zbranch *zbr, void *priv); 298 struct ubifs_zbranch *zbr, void *priv);
241typedef int (*dbg_znode_callback)(struct ubifs_info *c, 299typedef int (*dbg_znode_callback)(struct ubifs_info *c,
@@ -244,7 +302,8 @@ int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
244 dbg_znode_callback znode_cb, void *priv); 302 dbg_znode_callback znode_cb, void *priv);
245 303
246/* Checking functions */ 304/* Checking functions */
247 305void dbg_save_space_info(struct ubifs_info *c);
306int dbg_check_space_info(struct ubifs_info *c);
248int dbg_check_lprops(struct ubifs_info *c); 307int dbg_check_lprops(struct ubifs_info *c);
249int dbg_old_index_check_init(struct ubifs_info *c, struct ubifs_zbranch *zroot); 308int dbg_old_index_check_init(struct ubifs_info *c, struct ubifs_zbranch *zroot);
250int dbg_check_old_index(struct ubifs_info *c, struct ubifs_zbranch *zroot); 309int dbg_check_old_index(struct ubifs_info *c, struct ubifs_zbranch *zroot);
@@ -274,9 +333,6 @@ int dbg_force_in_the_gaps(void);
274 333
275#define dbg_failure_mode (ubifs_tst_flags & UBIFS_TST_RCVRY) 334#define dbg_failure_mode (ubifs_tst_flags & UBIFS_TST_RCVRY)
276 335
277void dbg_failure_mode_registration(struct ubifs_info *c);
278void dbg_failure_mode_deregistration(struct ubifs_info *c);
279
280#ifndef UBIFS_DBG_PRESERVE_UBI 336#ifndef UBIFS_DBG_PRESERVE_UBI
281 337
282#define ubi_leb_read dbg_leb_read 338#define ubi_leb_read dbg_leb_read
@@ -318,9 +374,13 @@ static inline int dbg_change(struct ubi_volume_desc *desc, int lnum,
318 return dbg_leb_change(desc, lnum, buf, len, UBI_UNKNOWN); 374 return dbg_leb_change(desc, lnum, buf, len, UBI_UNKNOWN);
319} 375}
320 376
321#else /* !CONFIG_UBIFS_FS_DEBUG */ 377/* Debugfs-related stuff */
378int dbg_debugfs_init(void);
379void dbg_debugfs_exit(void);
380int dbg_debugfs_init_fs(struct ubifs_info *c);
381void dbg_debugfs_exit_fs(struct ubifs_info *c);
322 382
323#define UBIFS_DBG(op) 383#else /* !CONFIG_UBIFS_FS_DEBUG */
324 384
325/* Use "if (0)" to make compiler check arguments even if debugging is off */ 385/* Use "if (0)" to make compiler check arguments even if debugging is off */
326#define ubifs_assert(expr) do { \ 386#define ubifs_assert(expr) do { \
@@ -360,26 +420,33 @@ static inline int dbg_change(struct ubi_volume_desc *desc, int lnum,
360#define DBGKEY(key) ((char *)(key)) 420#define DBGKEY(key) ((char *)(key))
361#define DBGKEY1(key) ((char *)(key)) 421#define DBGKEY1(key) ((char *)(key))
362 422
363#define dbg_ntype(type) "" 423#define ubifs_debugging_init(c) 0
364#define dbg_cstate(cmt_state) "" 424#define ubifs_debugging_exit(c) ({})
365#define dbg_get_key_dump(c, key) ({}) 425
366#define dbg_dump_inode(c, inode) ({}) 426#define dbg_ntype(type) ""
367#define dbg_dump_node(c, node) ({}) 427#define dbg_cstate(cmt_state) ""
368#define dbg_dump_budget_req(req) ({}) 428#define dbg_get_key_dump(c, key) ({})
369#define dbg_dump_lstats(lst) ({}) 429#define dbg_dump_inode(c, inode) ({})
370#define dbg_dump_budg(c) ({}) 430#define dbg_dump_node(c, node) ({})
371#define dbg_dump_lprop(c, lp) ({}) 431#define dbg_dump_lpt_node(c, node, lnum, offs) ({})
372#define dbg_dump_lprops(c) ({}) 432#define dbg_dump_budget_req(req) ({})
373#define dbg_dump_lpt_info(c) ({}) 433#define dbg_dump_lstats(lst) ({})
374#define dbg_dump_leb(c, lnum) ({}) 434#define dbg_dump_budg(c) ({})
375#define dbg_dump_znode(c, znode) ({}) 435#define dbg_dump_lprop(c, lp) ({})
376#define dbg_dump_heap(c, heap, cat) ({}) 436#define dbg_dump_lprops(c) ({})
377#define dbg_dump_pnode(c, pnode, parent, iip) ({}) 437#define dbg_dump_lpt_info(c) ({})
378#define dbg_dump_tnc(c) ({}) 438#define dbg_dump_leb(c, lnum) ({})
379#define dbg_dump_index(c) ({}) 439#define dbg_dump_znode(c, znode) ({})
440#define dbg_dump_heap(c, heap, cat) ({})
441#define dbg_dump_pnode(c, pnode, parent, iip) ({})
442#define dbg_dump_tnc(c) ({})
443#define dbg_dump_index(c) ({})
444#define dbg_dump_lpt_lebs(c) ({})
380 445
381#define dbg_walk_index(c, leaf_cb, znode_cb, priv) 0 446#define dbg_walk_index(c, leaf_cb, znode_cb, priv) 0
382#define dbg_old_index_check_init(c, zroot) 0 447#define dbg_old_index_check_init(c, zroot) 0
448#define dbg_save_space_info(c) ({})
449#define dbg_check_space_info(c) 0
383#define dbg_check_old_index(c, zroot) 0 450#define dbg_check_old_index(c, zroot) 0
384#define dbg_check_cats(c) 0 451#define dbg_check_cats(c) 0
385#define dbg_check_ltab(c) 0 452#define dbg_check_ltab(c) 0
@@ -396,9 +463,11 @@ static inline int dbg_change(struct ubi_volume_desc *desc, int lnum,
396#define dbg_force_in_the_gaps_enabled 0 463#define dbg_force_in_the_gaps_enabled 0
397#define dbg_force_in_the_gaps() 0 464#define dbg_force_in_the_gaps() 0
398#define dbg_failure_mode 0 465#define dbg_failure_mode 0
399#define dbg_failure_mode_registration(c) ({})
400#define dbg_failure_mode_deregistration(c) ({})
401 466
402#endif /* !CONFIG_UBIFS_FS_DEBUG */ 467#define dbg_debugfs_init() 0
468#define dbg_debugfs_exit()
469#define dbg_debugfs_init_fs(c) 0
470#define dbg_debugfs_exit_fs(c) 0
403 471
472#endif /* !CONFIG_UBIFS_FS_DEBUG */
404#endif /* !__UBIFS_DEBUG_H__ */ 473#endif /* !__UBIFS_DEBUG_H__ */
diff --git a/fs/ubifs/dir.c b/fs/ubifs/dir.c
index f448ab1f9c38..f55d523c52bb 100644
--- a/fs/ubifs/dir.c
+++ b/fs/ubifs/dir.c
@@ -482,30 +482,29 @@ static int ubifs_dir_release(struct inode *dir, struct file *file)
482} 482}
483 483
484/** 484/**
485 * lock_2_inodes - lock two UBIFS inodes. 485 * lock_2_inodes - a wrapper for locking two UBIFS inodes.
486 * @inode1: first inode 486 * @inode1: first inode
487 * @inode2: second inode 487 * @inode2: second inode
488 *
489 * We do not implement any tricks to guarantee strict lock ordering, because
490 * VFS has already done it for us on the @i_mutex. So this is just a simple
491 * wrapper function.
488 */ 492 */
489static void lock_2_inodes(struct inode *inode1, struct inode *inode2) 493static void lock_2_inodes(struct inode *inode1, struct inode *inode2)
490{ 494{
491 if (inode1->i_ino < inode2->i_ino) { 495 mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1);
492 mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_2); 496 mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2);
493 mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_3);
494 } else {
495 mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2);
496 mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_3);
497 }
498} 497}
499 498
500/** 499/**
501 * unlock_2_inodes - unlock two UBIFS inodes inodes. 500 * unlock_2_inodes - a wrapper for unlocking two UBIFS inodes.
502 * @inode1: first inode 501 * @inode1: first inode
503 * @inode2: second inode 502 * @inode2: second inode
504 */ 503 */
505static void unlock_2_inodes(struct inode *inode1, struct inode *inode2) 504static void unlock_2_inodes(struct inode *inode1, struct inode *inode2)
506{ 505{
507 mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
508 mutex_unlock(&ubifs_inode(inode2)->ui_mutex); 506 mutex_unlock(&ubifs_inode(inode2)->ui_mutex);
507 mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
509} 508}
510 509
511static int ubifs_link(struct dentry *old_dentry, struct inode *dir, 510static int ubifs_link(struct dentry *old_dentry, struct inode *dir,
@@ -527,6 +526,8 @@ static int ubifs_link(struct dentry *old_dentry, struct inode *dir,
527 dbg_gen("dent '%.*s' to ino %lu (nlink %d) in dir ino %lu", 526 dbg_gen("dent '%.*s' to ino %lu (nlink %d) in dir ino %lu",
528 dentry->d_name.len, dentry->d_name.name, inode->i_ino, 527 dentry->d_name.len, dentry->d_name.name, inode->i_ino,
529 inode->i_nlink, dir->i_ino); 528 inode->i_nlink, dir->i_ino);
529 ubifs_assert(mutex_is_locked(&dir->i_mutex));
530 ubifs_assert(mutex_is_locked(&inode->i_mutex));
530 err = dbg_check_synced_i_size(inode); 531 err = dbg_check_synced_i_size(inode);
531 if (err) 532 if (err)
532 return err; 533 return err;
@@ -580,6 +581,8 @@ static int ubifs_unlink(struct inode *dir, struct dentry *dentry)
580 dbg_gen("dent '%.*s' from ino %lu (nlink %d) in dir ino %lu", 581 dbg_gen("dent '%.*s' from ino %lu (nlink %d) in dir ino %lu",
581 dentry->d_name.len, dentry->d_name.name, inode->i_ino, 582 dentry->d_name.len, dentry->d_name.name, inode->i_ino,
582 inode->i_nlink, dir->i_ino); 583 inode->i_nlink, dir->i_ino);
584 ubifs_assert(mutex_is_locked(&dir->i_mutex));
585 ubifs_assert(mutex_is_locked(&inode->i_mutex));
583 err = dbg_check_synced_i_size(inode); 586 err = dbg_check_synced_i_size(inode);
584 if (err) 587 if (err)
585 return err; 588 return err;
@@ -667,7 +670,8 @@ static int ubifs_rmdir(struct inode *dir, struct dentry *dentry)
667 670
668 dbg_gen("directory '%.*s', ino %lu in dir ino %lu", dentry->d_name.len, 671 dbg_gen("directory '%.*s', ino %lu in dir ino %lu", dentry->d_name.len,
669 dentry->d_name.name, inode->i_ino, dir->i_ino); 672 dentry->d_name.name, inode->i_ino, dir->i_ino);
670 673 ubifs_assert(mutex_is_locked(&dir->i_mutex));
674 ubifs_assert(mutex_is_locked(&inode->i_mutex));
671 err = check_dir_empty(c, dentry->d_inode); 675 err = check_dir_empty(c, dentry->d_inode);
672 if (err) 676 if (err)
673 return err; 677 return err;
@@ -922,59 +926,30 @@ out_budg:
922} 926}
923 927
924/** 928/**
925 * lock_3_inodes - lock three UBIFS inodes for rename. 929 * lock_3_inodes - a wrapper for locking three UBIFS inodes.
926 * @inode1: first inode 930 * @inode1: first inode
927 * @inode2: second inode 931 * @inode2: second inode
928 * @inode3: third inode 932 * @inode3: third inode
929 * 933 *
930 * For 'ubifs_rename()', @inode1 may be the same as @inode2 whereas @inode3 may 934 * This function is used for 'ubifs_rename()' and @inode1 may be the same as
931 * be null. 935 * @inode2 whereas @inode3 may be %NULL.
936 *
937 * We do not implement any tricks to guarantee strict lock ordering, because
938 * VFS has already done it for us on the @i_mutex. So this is just a simple
939 * wrapper function.
932 */ 940 */
933static void lock_3_inodes(struct inode *inode1, struct inode *inode2, 941static void lock_3_inodes(struct inode *inode1, struct inode *inode2,
934 struct inode *inode3) 942 struct inode *inode3)
935{ 943{
936 struct inode *i1, *i2, *i3; 944 mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1);
937 945 if (inode2 != inode1)
938 if (!inode3) { 946 mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2);
939 if (inode1 != inode2) { 947 if (inode3)
940 lock_2_inodes(inode1, inode2); 948 mutex_lock_nested(&ubifs_inode(inode3)->ui_mutex, WB_MUTEX_3);
941 return;
942 }
943 mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1);
944 return;
945 }
946
947 if (inode1 == inode2) {
948 lock_2_inodes(inode1, inode3);
949 return;
950 }
951
952 /* 3 different inodes */
953 if (inode1 < inode2) {
954 i3 = inode2;
955 if (inode1 < inode3) {
956 i1 = inode1;
957 i2 = inode3;
958 } else {
959 i1 = inode3;
960 i2 = inode1;
961 }
962 } else {
963 i3 = inode1;
964 if (inode2 < inode3) {
965 i1 = inode2;
966 i2 = inode3;
967 } else {
968 i1 = inode3;
969 i2 = inode2;
970 }
971 }
972 mutex_lock_nested(&ubifs_inode(i1)->ui_mutex, WB_MUTEX_1);
973 lock_2_inodes(i2, i3);
974} 949}
975 950
976/** 951/**
977 * unlock_3_inodes - unlock three UBIFS inodes for rename. 952 * unlock_3_inodes - a wrapper for unlocking three UBIFS inodes for rename.
978 * @inode1: first inode 953 * @inode1: first inode
979 * @inode2: second inode 954 * @inode2: second inode
980 * @inode3: third inode 955 * @inode3: third inode
@@ -982,11 +957,11 @@ static void lock_3_inodes(struct inode *inode1, struct inode *inode2,
982static void unlock_3_inodes(struct inode *inode1, struct inode *inode2, 957static void unlock_3_inodes(struct inode *inode1, struct inode *inode2,
983 struct inode *inode3) 958 struct inode *inode3)
984{ 959{
985 mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
986 if (inode1 != inode2)
987 mutex_unlock(&ubifs_inode(inode2)->ui_mutex);
988 if (inode3) 960 if (inode3)
989 mutex_unlock(&ubifs_inode(inode3)->ui_mutex); 961 mutex_unlock(&ubifs_inode(inode3)->ui_mutex);
962 if (inode1 != inode2)
963 mutex_unlock(&ubifs_inode(inode2)->ui_mutex);
964 mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
990} 965}
991 966
992static int ubifs_rename(struct inode *old_dir, struct dentry *old_dentry, 967static int ubifs_rename(struct inode *old_dir, struct dentry *old_dentry,
@@ -1020,6 +995,11 @@ static int ubifs_rename(struct inode *old_dir, struct dentry *old_dentry,
1020 "dir ino %lu", old_dentry->d_name.len, old_dentry->d_name.name, 995 "dir ino %lu", old_dentry->d_name.len, old_dentry->d_name.name,
1021 old_inode->i_ino, old_dir->i_ino, new_dentry->d_name.len, 996 old_inode->i_ino, old_dir->i_ino, new_dentry->d_name.len,
1022 new_dentry->d_name.name, new_dir->i_ino); 997 new_dentry->d_name.name, new_dir->i_ino);
998 ubifs_assert(mutex_is_locked(&old_dir->i_mutex));
999 ubifs_assert(mutex_is_locked(&new_dir->i_mutex));
1000 if (unlink)
1001 ubifs_assert(mutex_is_locked(&new_inode->i_mutex));
1002
1023 1003
1024 if (unlink && is_dir) { 1004 if (unlink && is_dir) {
1025 err = check_dir_empty(c, new_inode); 1005 err = check_dir_empty(c, new_inode);
@@ -1199,7 +1179,7 @@ int ubifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
1199 return 0; 1179 return 0;
1200} 1180}
1201 1181
1202struct inode_operations ubifs_dir_inode_operations = { 1182const struct inode_operations ubifs_dir_inode_operations = {
1203 .lookup = ubifs_lookup, 1183 .lookup = ubifs_lookup,
1204 .create = ubifs_create, 1184 .create = ubifs_create,
1205 .link = ubifs_link, 1185 .link = ubifs_link,
@@ -1219,7 +1199,7 @@ struct inode_operations ubifs_dir_inode_operations = {
1219#endif 1199#endif
1220}; 1200};
1221 1201
1222struct file_operations ubifs_dir_operations = { 1202const struct file_operations ubifs_dir_operations = {
1223 .llseek = ubifs_dir_llseek, 1203 .llseek = ubifs_dir_llseek,
1224 .release = ubifs_dir_release, 1204 .release = ubifs_dir_release,
1225 .read = generic_read_dir, 1205 .read = generic_read_dir,
diff --git a/fs/ubifs/file.c b/fs/ubifs/file.c
index 2624411d9758..93b6de51f261 100644
--- a/fs/ubifs/file.c
+++ b/fs/ubifs/file.c
@@ -72,8 +72,8 @@ static int read_block(struct inode *inode, void *addr, unsigned int block,
72 return err; 72 return err;
73 } 73 }
74 74
75 ubifs_assert(le64_to_cpu(dn->ch.sqnum) > ubifs_inode(inode)->creat_sqnum); 75 ubifs_assert(le64_to_cpu(dn->ch.sqnum) >
76 76 ubifs_inode(inode)->creat_sqnum);
77 len = le32_to_cpu(dn->size); 77 len = le32_to_cpu(dn->size);
78 if (len <= 0 || len > UBIFS_BLOCK_SIZE) 78 if (len <= 0 || len > UBIFS_BLOCK_SIZE)
79 goto dump; 79 goto dump;
@@ -219,7 +219,8 @@ static void release_existing_page_budget(struct ubifs_info *c)
219} 219}
220 220
221static int write_begin_slow(struct address_space *mapping, 221static int write_begin_slow(struct address_space *mapping,
222 loff_t pos, unsigned len, struct page **pagep) 222 loff_t pos, unsigned len, struct page **pagep,
223 unsigned flags)
223{ 224{
224 struct inode *inode = mapping->host; 225 struct inode *inode = mapping->host;
225 struct ubifs_info *c = inode->i_sb->s_fs_info; 226 struct ubifs_info *c = inode->i_sb->s_fs_info;
@@ -247,14 +248,14 @@ static int write_begin_slow(struct address_space *mapping,
247 if (unlikely(err)) 248 if (unlikely(err))
248 return err; 249 return err;
249 250
250 page = __grab_cache_page(mapping, index); 251 page = grab_cache_page_write_begin(mapping, index, flags);
251 if (unlikely(!page)) { 252 if (unlikely(!page)) {
252 ubifs_release_budget(c, &req); 253 ubifs_release_budget(c, &req);
253 return -ENOMEM; 254 return -ENOMEM;
254 } 255 }
255 256
256 if (!PageUptodate(page)) { 257 if (!PageUptodate(page)) {
257 if (!(pos & PAGE_CACHE_MASK) && len == PAGE_CACHE_SIZE) 258 if (!(pos & ~PAGE_CACHE_MASK) && len == PAGE_CACHE_SIZE)
258 SetPageChecked(page); 259 SetPageChecked(page);
259 else { 260 else {
260 err = do_readpage(page); 261 err = do_readpage(page);
@@ -431,20 +432,19 @@ static int ubifs_write_begin(struct file *file, struct address_space *mapping,
431 int uninitialized_var(err), appending = !!(pos + len > inode->i_size); 432 int uninitialized_var(err), appending = !!(pos + len > inode->i_size);
432 struct page *page; 433 struct page *page;
433 434
434
435 ubifs_assert(ubifs_inode(inode)->ui_size == inode->i_size); 435 ubifs_assert(ubifs_inode(inode)->ui_size == inode->i_size);
436 436
437 if (unlikely(c->ro_media)) 437 if (unlikely(c->ro_media))
438 return -EROFS; 438 return -EROFS;
439 439
440 /* Try out the fast-path part first */ 440 /* Try out the fast-path part first */
441 page = __grab_cache_page(mapping, index); 441 page = grab_cache_page_write_begin(mapping, index, flags);
442 if (unlikely(!page)) 442 if (unlikely(!page))
443 return -ENOMEM; 443 return -ENOMEM;
444 444
445 if (!PageUptodate(page)) { 445 if (!PageUptodate(page)) {
446 /* The page is not loaded from the flash */ 446 /* The page is not loaded from the flash */
447 if (!(pos & PAGE_CACHE_MASK) && len == PAGE_CACHE_SIZE) 447 if (!(pos & ~PAGE_CACHE_MASK) && len == PAGE_CACHE_SIZE)
448 /* 448 /*
449 * We change whole page so no need to load it. But we 449 * We change whole page so no need to load it. But we
450 * have to set the @PG_checked flag to make the further 450 * have to set the @PG_checked flag to make the further
@@ -483,7 +483,7 @@ static int ubifs_write_begin(struct file *file, struct address_space *mapping,
483 unlock_page(page); 483 unlock_page(page);
484 page_cache_release(page); 484 page_cache_release(page);
485 485
486 return write_begin_slow(mapping, pos, len, pagep); 486 return write_begin_slow(mapping, pos, len, pagep, flags);
487 } 487 }
488 488
489 /* 489 /*
@@ -1540,7 +1540,7 @@ static int ubifs_file_mmap(struct file *file, struct vm_area_struct *vma)
1540 return 0; 1540 return 0;
1541} 1541}
1542 1542
1543struct address_space_operations ubifs_file_address_operations = { 1543const struct address_space_operations ubifs_file_address_operations = {
1544 .readpage = ubifs_readpage, 1544 .readpage = ubifs_readpage,
1545 .writepage = ubifs_writepage, 1545 .writepage = ubifs_writepage,
1546 .write_begin = ubifs_write_begin, 1546 .write_begin = ubifs_write_begin,
@@ -1550,7 +1550,7 @@ struct address_space_operations ubifs_file_address_operations = {
1550 .releasepage = ubifs_releasepage, 1550 .releasepage = ubifs_releasepage,
1551}; 1551};
1552 1552
1553struct inode_operations ubifs_file_inode_operations = { 1553const struct inode_operations ubifs_file_inode_operations = {
1554 .setattr = ubifs_setattr, 1554 .setattr = ubifs_setattr,
1555 .getattr = ubifs_getattr, 1555 .getattr = ubifs_getattr,
1556#ifdef CONFIG_UBIFS_FS_XATTR 1556#ifdef CONFIG_UBIFS_FS_XATTR
@@ -1561,14 +1561,14 @@ struct inode_operations ubifs_file_inode_operations = {
1561#endif 1561#endif
1562}; 1562};
1563 1563
1564struct inode_operations ubifs_symlink_inode_operations = { 1564const struct inode_operations ubifs_symlink_inode_operations = {
1565 .readlink = generic_readlink, 1565 .readlink = generic_readlink,
1566 .follow_link = ubifs_follow_link, 1566 .follow_link = ubifs_follow_link,
1567 .setattr = ubifs_setattr, 1567 .setattr = ubifs_setattr,
1568 .getattr = ubifs_getattr, 1568 .getattr = ubifs_getattr,
1569}; 1569};
1570 1570
1571struct file_operations ubifs_file_operations = { 1571const struct file_operations ubifs_file_operations = {
1572 .llseek = generic_file_llseek, 1572 .llseek = generic_file_llseek,
1573 .read = do_sync_read, 1573 .read = do_sync_read,
1574 .write = do_sync_write, 1574 .write = do_sync_write,
diff --git a/fs/ubifs/gc.c b/fs/ubifs/gc.c
index 0bef6501d58a..a711d33b3d3e 100644
--- a/fs/ubifs/gc.c
+++ b/fs/ubifs/gc.c
@@ -31,6 +31,26 @@
31 * to be reused. Garbage collection will cause the number of dirty index nodes 31 * to be reused. Garbage collection will cause the number of dirty index nodes
32 * to grow, however sufficient space is reserved for the index to ensure the 32 * to grow, however sufficient space is reserved for the index to ensure the
33 * commit will never run out of space. 33 * commit will never run out of space.
34 *
35 * Notes about dead watermark. At current UBIFS implementation we assume that
36 * LEBs which have less than @c->dead_wm bytes of free + dirty space are full
37 * and not worth garbage-collecting. The dead watermark is one min. I/O unit
38 * size, or min. UBIFS node size, depending on what is greater. Indeed, UBIFS
39 * Garbage Collector has to synchronize the GC head's write buffer before
40 * returning, so this is about wasting one min. I/O unit. However, UBIFS GC can
41 * actually reclaim even very small pieces of dirty space by garbage collecting
42 * enough dirty LEBs, but we do not bother doing this at this implementation.
43 *
44 * Notes about dark watermark. The results of GC work depends on how big are
45 * the UBIFS nodes GC deals with. Large nodes make GC waste more space. Indeed,
46 * if GC move data from LEB A to LEB B and nodes in LEB A are large, GC would
47 * have to waste large pieces of free space at the end of LEB B, because nodes
48 * from LEB A would not fit. And the worst situation is when all nodes are of
49 * maximum size. So dark watermark is the amount of free + dirty space in LEB
50 * which are guaranteed to be reclaimable. If LEB has less space, the GC migh
51 * be unable to reclaim it. So, LEBs with free + dirty greater than dark
52 * watermark are "good" LEBs from GC's point of few. The other LEBs are not so
53 * good, and GC takes extra care when moving them.
34 */ 54 */
35 55
36#include <linux/pagemap.h> 56#include <linux/pagemap.h>
@@ -45,7 +65,7 @@
45#define SMALL_NODE_WM UBIFS_MAX_DENT_NODE_SZ 65#define SMALL_NODE_WM UBIFS_MAX_DENT_NODE_SZ
46 66
47/* 67/*
48 * GC may need to move more then one LEB to make progress. The below constants 68 * GC may need to move more than one LEB to make progress. The below constants
49 * define "soft" and "hard" limits on the number of LEBs the garbage collector 69 * define "soft" and "hard" limits on the number of LEBs the garbage collector
50 * may move. 70 * may move.
51 */ 71 */
@@ -381,7 +401,7 @@ int ubifs_garbage_collect_leb(struct ubifs_info *c, struct ubifs_lprops *lp)
381 401
382 /* 402 /*
383 * Don't release the LEB until after the next commit, because 403 * Don't release the LEB until after the next commit, because
384 * it may contain date which is needed for recovery. So 404 * it may contain data which is needed for recovery. So
385 * although we freed this LEB, it will become usable only after 405 * although we freed this LEB, it will become usable only after
386 * the commit. 406 * the commit.
387 */ 407 */
@@ -810,8 +830,9 @@ out:
810 * ubifs_destroy_idx_gc - destroy idx_gc list. 830 * ubifs_destroy_idx_gc - destroy idx_gc list.
811 * @c: UBIFS file-system description object 831 * @c: UBIFS file-system description object
812 * 832 *
813 * This function destroys the idx_gc list. It is called when unmounting or 833 * This function destroys the @c->idx_gc list. It is called when unmounting
814 * remounting read-only so locks are not needed. 834 * so locks are not needed. Returns zero in case of success and a negative
835 * error code in case of failure.
815 */ 836 */
816void ubifs_destroy_idx_gc(struct ubifs_info *c) 837void ubifs_destroy_idx_gc(struct ubifs_info *c)
817{ 838{
@@ -824,7 +845,6 @@ void ubifs_destroy_idx_gc(struct ubifs_info *c)
824 list_del(&idx_gc->list); 845 list_del(&idx_gc->list);
825 kfree(idx_gc); 846 kfree(idx_gc);
826 } 847 }
827
828} 848}
829 849
830/** 850/**
diff --git a/fs/ubifs/io.c b/fs/ubifs/io.c
index 01682713af69..e8e632a1dcdf 100644
--- a/fs/ubifs/io.c
+++ b/fs/ubifs/io.c
@@ -29,7 +29,7 @@
29 * would have been wasted for padding to the nearest minimal I/O unit boundary. 29 * would have been wasted for padding to the nearest minimal I/O unit boundary.
30 * Instead, data first goes to the write-buffer and is flushed when the 30 * Instead, data first goes to the write-buffer and is flushed when the
31 * buffer is full or when it is not used for some time (by timer). This is 31 * buffer is full or when it is not used for some time (by timer). This is
32 * similarto the mechanism is used by JFFS2. 32 * similar to the mechanism is used by JFFS2.
33 * 33 *
34 * Write-buffers are defined by 'struct ubifs_wbuf' objects and protected by 34 * Write-buffers are defined by 'struct ubifs_wbuf' objects and protected by
35 * mutexes defined inside these objects. Since sometimes upper-level code 35 * mutexes defined inside these objects. Since sometimes upper-level code
@@ -75,7 +75,7 @@ void ubifs_ro_mode(struct ubifs_info *c, int err)
75 * @lnum: logical eraseblock number 75 * @lnum: logical eraseblock number
76 * @offs: offset within the logical eraseblock 76 * @offs: offset within the logical eraseblock
77 * @quiet: print no messages 77 * @quiet: print no messages
78 * @chk_crc: indicates whether to always check the CRC 78 * @must_chk_crc: indicates whether to always check the CRC
79 * 79 *
80 * This function checks node magic number and CRC checksum. This function also 80 * This function checks node magic number and CRC checksum. This function also
81 * validates node length to prevent UBIFS from becoming crazy when an attacker 81 * validates node length to prevent UBIFS from becoming crazy when an attacker
@@ -83,11 +83,17 @@ void ubifs_ro_mode(struct ubifs_info *c, int err)
83 * node length in the common header could cause UBIFS to read memory outside of 83 * node length in the common header could cause UBIFS to read memory outside of
84 * allocated buffer when checking the CRC checksum. 84 * allocated buffer when checking the CRC checksum.
85 * 85 *
86 * This function returns zero in case of success %-EUCLEAN in case of bad CRC 86 * This function may skip data nodes CRC checking if @c->no_chk_data_crc is
87 * or magic. 87 * true, which is controlled by corresponding UBIFS mount option. However, if
88 * @must_chk_crc is true, then @c->no_chk_data_crc is ignored and CRC is
89 * checked. Similarly, if @c->always_chk_crc is true, @c->no_chk_data_crc is
90 * ignored and CRC is checked.
91 *
92 * This function returns zero in case of success and %-EUCLEAN in case of bad
93 * CRC or magic.
88 */ 94 */
89int ubifs_check_node(const struct ubifs_info *c, const void *buf, int lnum, 95int ubifs_check_node(const struct ubifs_info *c, const void *buf, int lnum,
90 int offs, int quiet, int chk_crc) 96 int offs, int quiet, int must_chk_crc)
91{ 97{
92 int err = -EINVAL, type, node_len; 98 int err = -EINVAL, type, node_len;
93 uint32_t crc, node_crc, magic; 99 uint32_t crc, node_crc, magic;
@@ -123,9 +129,9 @@ int ubifs_check_node(const struct ubifs_info *c, const void *buf, int lnum,
123 node_len > c->ranges[type].max_len) 129 node_len > c->ranges[type].max_len)
124 goto out_len; 130 goto out_len;
125 131
126 if (!chk_crc && type == UBIFS_DATA_NODE && !c->always_chk_crc) 132 if (!must_chk_crc && type == UBIFS_DATA_NODE && !c->always_chk_crc &&
127 if (c->no_chk_data_crc) 133 c->no_chk_data_crc)
128 return 0; 134 return 0;
129 135
130 crc = crc32(UBIFS_CRC32_INIT, buf + 8, node_len - 8); 136 crc = crc32(UBIFS_CRC32_INIT, buf + 8, node_len - 8);
131 node_crc = le32_to_cpu(ch->crc); 137 node_crc = le32_to_cpu(ch->crc);
diff --git a/fs/ubifs/ioctl.c b/fs/ubifs/ioctl.c
index 5e82cffe9695..6db7a6be6c97 100644
--- a/fs/ubifs/ioctl.c
+++ b/fs/ubifs/ioctl.c
@@ -154,6 +154,7 @@ long ubifs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
154 case FS_IOC_GETFLAGS: 154 case FS_IOC_GETFLAGS:
155 flags = ubifs2ioctl(ubifs_inode(inode)->flags); 155 flags = ubifs2ioctl(ubifs_inode(inode)->flags);
156 156
157 dbg_gen("get flags: %#x, i_flags %#x", flags, inode->i_flags);
157 return put_user(flags, (int __user *) arg); 158 return put_user(flags, (int __user *) arg);
158 159
159 case FS_IOC_SETFLAGS: { 160 case FS_IOC_SETFLAGS: {
@@ -176,6 +177,7 @@ long ubifs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
176 err = mnt_want_write(file->f_path.mnt); 177 err = mnt_want_write(file->f_path.mnt);
177 if (err) 178 if (err)
178 return err; 179 return err;
180 dbg_gen("set flags: %#x, i_flags %#x", flags, inode->i_flags);
179 err = setflags(inode, flags); 181 err = setflags(inode, flags);
180 mnt_drop_write(file->f_path.mnt); 182 mnt_drop_write(file->f_path.mnt);
181 return err; 183 return err;
diff --git a/fs/ubifs/journal.c b/fs/ubifs/journal.c
index f91b745908ea..a11ca0958a23 100644
--- a/fs/ubifs/journal.c
+++ b/fs/ubifs/journal.c
@@ -191,7 +191,7 @@ again:
191 if (wbuf->lnum != -1 && avail >= len) { 191 if (wbuf->lnum != -1 && avail >= len) {
192 /* 192 /*
193 * Someone else has switched the journal head and we have 193 * Someone else has switched the journal head and we have
194 * enough space now. This happens when more then one process is 194 * enough space now. This happens when more than one process is
195 * trying to write to the same journal head at the same time. 195 * trying to write to the same journal head at the same time.
196 */ 196 */
197 dbg_jnl("return LEB %d back, already have LEB %d:%d", 197 dbg_jnl("return LEB %d back, already have LEB %d:%d",
@@ -208,7 +208,7 @@ again:
208 offs = 0; 208 offs = 0;
209 209
210out: 210out:
211 err = ubifs_wbuf_seek_nolock(wbuf, lnum, offs, UBI_SHORTTERM); 211 err = ubifs_wbuf_seek_nolock(wbuf, lnum, offs, wbuf->dtype);
212 if (err) 212 if (err)
213 goto out_unlock; 213 goto out_unlock;
214 214
@@ -704,7 +704,7 @@ int ubifs_jnl_write_data(struct ubifs_info *c, const struct inode *inode,
704 data->size = cpu_to_le32(len); 704 data->size = cpu_to_le32(len);
705 zero_data_node_unused(data); 705 zero_data_node_unused(data);
706 706
707 if (!(ui->flags && UBIFS_COMPR_FL)) 707 if (!(ui->flags & UBIFS_COMPR_FL))
708 /* Compression is disabled for this inode */ 708 /* Compression is disabled for this inode */
709 compr_type = UBIFS_COMPR_NONE; 709 compr_type = UBIFS_COMPR_NONE;
710 else 710 else
@@ -1220,7 +1220,7 @@ int ubifs_jnl_truncate(struct ubifs_info *c, const struct inode *inode,
1220 data_key_init(c, &key, inum, blk); 1220 data_key_init(c, &key, inum, blk);
1221 1221
1222 bit = old_size & (UBIFS_BLOCK_SIZE - 1); 1222 bit = old_size & (UBIFS_BLOCK_SIZE - 1);
1223 blk = (old_size >> UBIFS_BLOCK_SHIFT) - (bit ? 0: 1); 1223 blk = (old_size >> UBIFS_BLOCK_SHIFT) - (bit ? 0 : 1);
1224 data_key_init(c, &to_key, inum, blk); 1224 data_key_init(c, &to_key, inum, blk);
1225 1225
1226 err = ubifs_tnc_remove_range(c, &key, &to_key); 1226 err = ubifs_tnc_remove_range(c, &key, &to_key);
diff --git a/fs/ubifs/key.h b/fs/ubifs/key.h
index 3f1f16bc25c9..efb3430a2581 100644
--- a/fs/ubifs/key.h
+++ b/fs/ubifs/key.h
@@ -38,6 +38,22 @@
38#define __UBIFS_KEY_H__ 38#define __UBIFS_KEY_H__
39 39
40/** 40/**
41 * key_mask_hash - mask a valid hash value.
42 * @val: value to be masked
43 *
44 * We use hash values as offset in directories, so values %0 and %1 are
45 * reserved for "." and "..". %2 is reserved for "end of readdir" marker. This
46 * function makes sure the reserved values are not used.
47 */
48static inline uint32_t key_mask_hash(uint32_t hash)
49{
50 hash &= UBIFS_S_KEY_HASH_MASK;
51 if (unlikely(hash <= 2))
52 hash += 3;
53 return hash;
54}
55
56/**
41 * key_r5_hash - R5 hash function (borrowed from reiserfs). 57 * key_r5_hash - R5 hash function (borrowed from reiserfs).
42 * @s: direntry name 58 * @s: direntry name
43 * @len: name length 59 * @len: name length
@@ -54,16 +70,7 @@ static inline uint32_t key_r5_hash(const char *s, int len)
54 str++; 70 str++;
55 } 71 }
56 72
57 a &= UBIFS_S_KEY_HASH_MASK; 73 return key_mask_hash(a);
58
59 /*
60 * We use hash values as offset in directories, so values %0 and %1 are
61 * reserved for "." and "..". %2 is reserved for "end of readdir"
62 * marker.
63 */
64 if (unlikely(a >= 0 && a <= 2))
65 a += 3;
66 return a;
67} 74}
68 75
69/** 76/**
@@ -77,10 +84,7 @@ static inline uint32_t key_test_hash(const char *str, int len)
77 84
78 len = min_t(uint32_t, len, 4); 85 len = min_t(uint32_t, len, 4);
79 memcpy(&a, str, len); 86 memcpy(&a, str, len);
80 a &= UBIFS_S_KEY_HASH_MASK; 87 return key_mask_hash(a);
81 if (unlikely(a >= 0 && a <= 2))
82 a += 3;
83 return a;
84} 88}
85 89
86/** 90/**
diff --git a/fs/ubifs/lprops.c b/fs/ubifs/lprops.c
index f27176e9b70d..4cdd284dea56 100644
--- a/fs/ubifs/lprops.c
+++ b/fs/ubifs/lprops.c
@@ -520,13 +520,13 @@ static int is_lprops_dirty(struct ubifs_info *c, struct ubifs_lprops *lprops)
520 * @flags: new flags 520 * @flags: new flags
521 * @idx_gc_cnt: change to the count of idx_gc list 521 * @idx_gc_cnt: change to the count of idx_gc list
522 * 522 *
523 * This function changes LEB properties. This function does not change a LEB 523 * This function changes LEB properties (@free, @dirty or @flag). However, the
524 * property (@free, @dirty or @flag) if the value passed is %LPROPS_NC. 524 * property which has the %LPROPS_NC value is not changed. Returns a pointer to
525 * the updated LEB properties on success and a negative error code on failure.
525 * 526 *
526 * This function returns a pointer to the updated LEB properties on success 527 * Note, the LEB properties may have had to be copied (due to COW) and
527 * and a negative error code on failure. N.B. the LEB properties may have had to 528 * consequently the pointer returned may not be the same as the pointer
528 * be copied (due to COW) and consequently the pointer returned may not be the 529 * passed.
529 * same as the pointer passed.
530 */ 530 */
531const struct ubifs_lprops *ubifs_change_lp(struct ubifs_info *c, 531const struct ubifs_lprops *ubifs_change_lp(struct ubifs_info *c,
532 const struct ubifs_lprops *lp, 532 const struct ubifs_lprops *lp,
@@ -635,10 +635,10 @@ const struct ubifs_lprops *ubifs_change_lp(struct ubifs_info *c,
635 * @c: UBIFS file-system description object 635 * @c: UBIFS file-system description object
636 * @st: return statistics 636 * @st: return statistics
637 */ 637 */
638void ubifs_get_lp_stats(struct ubifs_info *c, struct ubifs_lp_stats *st) 638void ubifs_get_lp_stats(struct ubifs_info *c, struct ubifs_lp_stats *lst)
639{ 639{
640 spin_lock(&c->space_lock); 640 spin_lock(&c->space_lock);
641 memcpy(st, &c->lst, sizeof(struct ubifs_lp_stats)); 641 memcpy(lst, &c->lst, sizeof(struct ubifs_lp_stats));
642 spin_unlock(&c->space_lock); 642 spin_unlock(&c->space_lock);
643} 643}
644 644
@@ -678,6 +678,9 @@ int ubifs_change_one_lp(struct ubifs_info *c, int lnum, int free, int dirty,
678 678
679out: 679out:
680 ubifs_release_lprops(c); 680 ubifs_release_lprops(c);
681 if (err)
682 ubifs_err("cannot change properties of LEB %d, error %d",
683 lnum, err);
681 return err; 684 return err;
682} 685}
683 686
@@ -714,6 +717,9 @@ int ubifs_update_one_lp(struct ubifs_info *c, int lnum, int free, int dirty,
714 717
715out: 718out:
716 ubifs_release_lprops(c); 719 ubifs_release_lprops(c);
720 if (err)
721 ubifs_err("cannot update properties of LEB %d, error %d",
722 lnum, err);
717 return err; 723 return err;
718} 724}
719 725
@@ -737,6 +743,8 @@ int ubifs_read_one_lp(struct ubifs_info *c, int lnum, struct ubifs_lprops *lp)
737 lpp = ubifs_lpt_lookup(c, lnum); 743 lpp = ubifs_lpt_lookup(c, lnum);
738 if (IS_ERR(lpp)) { 744 if (IS_ERR(lpp)) {
739 err = PTR_ERR(lpp); 745 err = PTR_ERR(lpp);
746 ubifs_err("cannot read properties of LEB %d, error %d",
747 lnum, err);
740 goto out; 748 goto out;
741 } 749 }
742 750
@@ -1088,7 +1096,7 @@ static int scan_check_cb(struct ubifs_info *c,
1088 } 1096 }
1089 } 1097 }
1090 1098
1091 sleb = ubifs_scan(c, lnum, 0, c->dbg_buf); 1099 sleb = ubifs_scan(c, lnum, 0, c->dbg->buf);
1092 if (IS_ERR(sleb)) { 1100 if (IS_ERR(sleb)) {
1093 /* 1101 /*
1094 * After an unclean unmount, empty and freeable LEBs 1102 * After an unclean unmount, empty and freeable LEBs
diff --git a/fs/ubifs/lpt.c b/fs/ubifs/lpt.c
index db8bd0e518b2..b2792e84d245 100644
--- a/fs/ubifs/lpt.c
+++ b/fs/ubifs/lpt.c
@@ -36,15 +36,16 @@
36 * can be written into a single eraseblock. In that case, garbage collection 36 * can be written into a single eraseblock. In that case, garbage collection
37 * consists of just writing the whole table, which therefore makes all other 37 * consists of just writing the whole table, which therefore makes all other
38 * eraseblocks reusable. In the case of the big model, dirty eraseblocks are 38 * eraseblocks reusable. In the case of the big model, dirty eraseblocks are
39 * selected for garbage collection, which consists are marking the nodes in 39 * selected for garbage collection, which consists of marking the clean nodes in
40 * that LEB as dirty, and then only the dirty nodes are written out. Also, in 40 * that LEB as dirty, and then only the dirty nodes are written out. Also, in
41 * the case of the big model, a table of LEB numbers is saved so that the entire 41 * the case of the big model, a table of LEB numbers is saved so that the entire
42 * LPT does not to be scanned looking for empty eraseblocks when UBIFS is first 42 * LPT does not to be scanned looking for empty eraseblocks when UBIFS is first
43 * mounted. 43 * mounted.
44 */ 44 */
45 45
46#include <linux/crc16.h>
47#include "ubifs.h" 46#include "ubifs.h"
47#include <linux/crc16.h>
48#include <linux/math64.h>
48 49
49/** 50/**
50 * do_calc_lpt_geom - calculate sizes for the LPT area. 51 * do_calc_lpt_geom - calculate sizes for the LPT area.
@@ -135,15 +136,13 @@ static void do_calc_lpt_geom(struct ubifs_info *c)
135int ubifs_calc_lpt_geom(struct ubifs_info *c) 136int ubifs_calc_lpt_geom(struct ubifs_info *c)
136{ 137{
137 int lebs_needed; 138 int lebs_needed;
138 uint64_t sz; 139 long long sz;
139 140
140 do_calc_lpt_geom(c); 141 do_calc_lpt_geom(c);
141 142
142 /* Verify that lpt_lebs is big enough */ 143 /* Verify that lpt_lebs is big enough */
143 sz = c->lpt_sz * 2; /* Must have at least 2 times the size */ 144 sz = c->lpt_sz * 2; /* Must have at least 2 times the size */
144 sz += c->leb_size - 1; 145 lebs_needed = div_u64(sz + c->leb_size - 1, c->leb_size);
145 do_div(sz, c->leb_size);
146 lebs_needed = sz;
147 if (lebs_needed > c->lpt_lebs) { 146 if (lebs_needed > c->lpt_lebs) {
148 ubifs_err("too few LPT LEBs"); 147 ubifs_err("too few LPT LEBs");
149 return -EINVAL; 148 return -EINVAL;
@@ -156,7 +155,6 @@ int ubifs_calc_lpt_geom(struct ubifs_info *c)
156 } 155 }
157 156
158 c->check_lpt_free = c->big_lpt; 157 c->check_lpt_free = c->big_lpt;
159
160 return 0; 158 return 0;
161} 159}
162 160
@@ -176,7 +174,7 @@ static int calc_dflt_lpt_geom(struct ubifs_info *c, int *main_lebs,
176 int *big_lpt) 174 int *big_lpt)
177{ 175{
178 int i, lebs_needed; 176 int i, lebs_needed;
179 uint64_t sz; 177 long long sz;
180 178
181 /* Start by assuming the minimum number of LPT LEBs */ 179 /* Start by assuming the minimum number of LPT LEBs */
182 c->lpt_lebs = UBIFS_MIN_LPT_LEBS; 180 c->lpt_lebs = UBIFS_MIN_LPT_LEBS;
@@ -203,9 +201,7 @@ static int calc_dflt_lpt_geom(struct ubifs_info *c, int *main_lebs,
203 /* Now check there are enough LPT LEBs */ 201 /* Now check there are enough LPT LEBs */
204 for (i = 0; i < 64 ; i++) { 202 for (i = 0; i < 64 ; i++) {
205 sz = c->lpt_sz * 4; /* Allow 4 times the size */ 203 sz = c->lpt_sz * 4; /* Allow 4 times the size */
206 sz += c->leb_size - 1; 204 lebs_needed = div_u64(sz + c->leb_size - 1, c->leb_size);
207 do_div(sz, c->leb_size);
208 lebs_needed = sz;
209 if (lebs_needed > c->lpt_lebs) { 205 if (lebs_needed > c->lpt_lebs) {
210 /* Not enough LPT LEBs so try again with more */ 206 /* Not enough LPT LEBs so try again with more */
211 c->lpt_lebs = lebs_needed; 207 c->lpt_lebs = lebs_needed;
@@ -558,7 +554,7 @@ static int calc_nnode_num(int row, int col)
558 * This function calculates and returns the nnode number based on the parent's 554 * This function calculates and returns the nnode number based on the parent's
559 * nnode number and the index in parent. 555 * nnode number and the index in parent.
560 */ 556 */
561static int calc_nnode_num_from_parent(struct ubifs_info *c, 557static int calc_nnode_num_from_parent(const struct ubifs_info *c,
562 struct ubifs_nnode *parent, int iip) 558 struct ubifs_nnode *parent, int iip)
563{ 559{
564 int num, shft; 560 int num, shft;
@@ -583,7 +579,7 @@ static int calc_nnode_num_from_parent(struct ubifs_info *c,
583 * This function calculates and returns the pnode number based on the parent's 579 * This function calculates and returns the pnode number based on the parent's
584 * nnode number and the index in parent. 580 * nnode number and the index in parent.
585 */ 581 */
586static int calc_pnode_num_from_parent(struct ubifs_info *c, 582static int calc_pnode_num_from_parent(const struct ubifs_info *c,
587 struct ubifs_nnode *parent, int iip) 583 struct ubifs_nnode *parent, int iip)
588{ 584{
589 int i, n = c->lpt_hght - 1, pnum = parent->num, num = 0; 585 int i, n = c->lpt_hght - 1, pnum = parent->num, num = 0;
@@ -966,7 +962,7 @@ static int check_lpt_type(uint8_t **addr, int *pos, int type)
966 * 962 *
967 * This function returns %0 on success and a negative error code on failure. 963 * This function returns %0 on success and a negative error code on failure.
968 */ 964 */
969static int unpack_pnode(struct ubifs_info *c, void *buf, 965static int unpack_pnode(const struct ubifs_info *c, void *buf,
970 struct ubifs_pnode *pnode) 966 struct ubifs_pnode *pnode)
971{ 967{
972 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES; 968 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
@@ -996,15 +992,15 @@ static int unpack_pnode(struct ubifs_info *c, void *buf,
996} 992}
997 993
998/** 994/**
999 * unpack_nnode - unpack a nnode. 995 * ubifs_unpack_nnode - unpack a nnode.
1000 * @c: UBIFS file-system description object 996 * @c: UBIFS file-system description object
1001 * @buf: buffer containing packed nnode to unpack 997 * @buf: buffer containing packed nnode to unpack
1002 * @nnode: nnode structure to fill 998 * @nnode: nnode structure to fill
1003 * 999 *
1004 * This function returns %0 on success and a negative error code on failure. 1000 * This function returns %0 on success and a negative error code on failure.
1005 */ 1001 */
1006static int unpack_nnode(struct ubifs_info *c, void *buf, 1002int ubifs_unpack_nnode(const struct ubifs_info *c, void *buf,
1007 struct ubifs_nnode *nnode) 1003 struct ubifs_nnode *nnode)
1008{ 1004{
1009 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES; 1005 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1010 int i, pos = 0, err; 1006 int i, pos = 0, err;
@@ -1036,7 +1032,7 @@ static int unpack_nnode(struct ubifs_info *c, void *buf,
1036 * 1032 *
1037 * This function returns %0 on success and a negative error code on failure. 1033 * This function returns %0 on success and a negative error code on failure.
1038 */ 1034 */
1039static int unpack_ltab(struct ubifs_info *c, void *buf) 1035static int unpack_ltab(const struct ubifs_info *c, void *buf)
1040{ 1036{
1041 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES; 1037 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1042 int i, pos = 0, err; 1038 int i, pos = 0, err;
@@ -1068,7 +1064,7 @@ static int unpack_ltab(struct ubifs_info *c, void *buf)
1068 * 1064 *
1069 * This function returns %0 on success and a negative error code on failure. 1065 * This function returns %0 on success and a negative error code on failure.
1070 */ 1066 */
1071static int unpack_lsave(struct ubifs_info *c, void *buf) 1067static int unpack_lsave(const struct ubifs_info *c, void *buf)
1072{ 1068{
1073 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES; 1069 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1074 int i, pos = 0, err; 1070 int i, pos = 0, err;
@@ -1096,7 +1092,7 @@ static int unpack_lsave(struct ubifs_info *c, void *buf)
1096 * 1092 *
1097 * This function returns %0 on success and a negative error code on failure. 1093 * This function returns %0 on success and a negative error code on failure.
1098 */ 1094 */
1099static int validate_nnode(struct ubifs_info *c, struct ubifs_nnode *nnode, 1095static int validate_nnode(const struct ubifs_info *c, struct ubifs_nnode *nnode,
1100 struct ubifs_nnode *parent, int iip) 1096 struct ubifs_nnode *parent, int iip)
1101{ 1097{
1102 int i, lvl, max_offs; 1098 int i, lvl, max_offs;
@@ -1140,7 +1136,7 @@ static int validate_nnode(struct ubifs_info *c, struct ubifs_nnode *nnode,
1140 * 1136 *
1141 * This function returns %0 on success and a negative error code on failure. 1137 * This function returns %0 on success and a negative error code on failure.
1142 */ 1138 */
1143static int validate_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode, 1139static int validate_pnode(const struct ubifs_info *c, struct ubifs_pnode *pnode,
1144 struct ubifs_nnode *parent, int iip) 1140 struct ubifs_nnode *parent, int iip)
1145{ 1141{
1146 int i; 1142 int i;
@@ -1174,7 +1170,8 @@ static int validate_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
1174 * This function calculates the LEB numbers for the LEB properties it contains 1170 * This function calculates the LEB numbers for the LEB properties it contains
1175 * based on the pnode number. 1171 * based on the pnode number.
1176 */ 1172 */
1177static void set_pnode_lnum(struct ubifs_info *c, struct ubifs_pnode *pnode) 1173static void set_pnode_lnum(const struct ubifs_info *c,
1174 struct ubifs_pnode *pnode)
1178{ 1175{
1179 int i, lnum; 1176 int i, lnum;
1180 1177
@@ -1227,7 +1224,7 @@ int ubifs_read_nnode(struct ubifs_info *c, struct ubifs_nnode *parent, int iip)
1227 err = ubi_read(c->ubi, lnum, buf, offs, c->nnode_sz); 1224 err = ubi_read(c->ubi, lnum, buf, offs, c->nnode_sz);
1228 if (err) 1225 if (err)
1229 goto out; 1226 goto out;
1230 err = unpack_nnode(c, buf, nnode); 1227 err = ubifs_unpack_nnode(c, buf, nnode);
1231 if (err) 1228 if (err)
1232 goto out; 1229 goto out;
1233 } 1230 }
@@ -1816,7 +1813,7 @@ static struct ubifs_nnode *scan_get_nnode(struct ubifs_info *c,
1816 c->nnode_sz); 1813 c->nnode_sz);
1817 if (err) 1814 if (err)
1818 return ERR_PTR(err); 1815 return ERR_PTR(err);
1819 err = unpack_nnode(c, buf, nnode); 1816 err = ubifs_unpack_nnode(c, buf, nnode);
1820 if (err) 1817 if (err)
1821 return ERR_PTR(err); 1818 return ERR_PTR(err);
1822 } 1819 }
diff --git a/fs/ubifs/lpt_commit.c b/fs/ubifs/lpt_commit.c
index a41434b42785..3216a1f277f8 100644
--- a/fs/ubifs/lpt_commit.c
+++ b/fs/ubifs/lpt_commit.c
@@ -320,6 +320,8 @@ no_space:
320 dbg_err("LPT out of space at LEB %d:%d needing %d, done_ltab %d, " 320 dbg_err("LPT out of space at LEB %d:%d needing %d, done_ltab %d, "
321 "done_lsave %d", lnum, offs, len, done_ltab, done_lsave); 321 "done_lsave %d", lnum, offs, len, done_ltab, done_lsave);
322 dbg_dump_lpt_info(c); 322 dbg_dump_lpt_info(c);
323 dbg_dump_lpt_lebs(c);
324 dump_stack();
323 return err; 325 return err;
324} 326}
325 327
@@ -546,29 +548,31 @@ static int write_cnodes(struct ubifs_info *c)
546no_space: 548no_space:
547 ubifs_err("LPT out of space mismatch"); 549 ubifs_err("LPT out of space mismatch");
548 dbg_err("LPT out of space mismatch at LEB %d:%d needing %d, done_ltab " 550 dbg_err("LPT out of space mismatch at LEB %d:%d needing %d, done_ltab "
549 "%d, done_lsave %d", lnum, offs, len, done_ltab, done_lsave); 551 "%d, done_lsave %d", lnum, offs, len, done_ltab, done_lsave);
550 dbg_dump_lpt_info(c); 552 dbg_dump_lpt_info(c);
553 dbg_dump_lpt_lebs(c);
554 dump_stack();
551 return err; 555 return err;
552} 556}
553 557
554/** 558/**
555 * next_pnode - find next pnode. 559 * next_pnode_to_dirty - find next pnode to dirty.
556 * @c: UBIFS file-system description object 560 * @c: UBIFS file-system description object
557 * @pnode: pnode 561 * @pnode: pnode
558 * 562 *
559 * This function returns the next pnode or %NULL if there are no more pnodes. 563 * This function returns the next pnode to dirty or %NULL if there are no more
564 * pnodes. Note that pnodes that have never been written (lnum == 0) are
565 * skipped.
560 */ 566 */
561static struct ubifs_pnode *next_pnode(struct ubifs_info *c, 567static struct ubifs_pnode *next_pnode_to_dirty(struct ubifs_info *c,
562 struct ubifs_pnode *pnode) 568 struct ubifs_pnode *pnode)
563{ 569{
564 struct ubifs_nnode *nnode; 570 struct ubifs_nnode *nnode;
565 int iip; 571 int iip;
566 572
567 /* Try to go right */ 573 /* Try to go right */
568 nnode = pnode->parent; 574 nnode = pnode->parent;
569 iip = pnode->iip + 1; 575 for (iip = pnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) {
570 if (iip < UBIFS_LPT_FANOUT) {
571 /* We assume here that LEB zero is never an LPT LEB */
572 if (nnode->nbranch[iip].lnum) 576 if (nnode->nbranch[iip].lnum)
573 return ubifs_get_pnode(c, nnode, iip); 577 return ubifs_get_pnode(c, nnode, iip);
574 } 578 }
@@ -579,8 +583,11 @@ static struct ubifs_pnode *next_pnode(struct ubifs_info *c,
579 nnode = nnode->parent; 583 nnode = nnode->parent;
580 if (!nnode) 584 if (!nnode)
581 return NULL; 585 return NULL;
582 /* We assume here that LEB zero is never an LPT LEB */ 586 for (; iip < UBIFS_LPT_FANOUT; iip++) {
583 } while (iip >= UBIFS_LPT_FANOUT || !nnode->nbranch[iip].lnum); 587 if (nnode->nbranch[iip].lnum)
588 break;
589 }
590 } while (iip >= UBIFS_LPT_FANOUT);
584 591
585 /* Go right */ 592 /* Go right */
586 nnode = ubifs_get_nnode(c, nnode, iip); 593 nnode = ubifs_get_nnode(c, nnode, iip);
@@ -589,12 +596,29 @@ static struct ubifs_pnode *next_pnode(struct ubifs_info *c,
589 596
590 /* Go down to level 1 */ 597 /* Go down to level 1 */
591 while (nnode->level > 1) { 598 while (nnode->level > 1) {
592 nnode = ubifs_get_nnode(c, nnode, 0); 599 for (iip = 0; iip < UBIFS_LPT_FANOUT; iip++) {
600 if (nnode->nbranch[iip].lnum)
601 break;
602 }
603 if (iip >= UBIFS_LPT_FANOUT) {
604 /*
605 * Should not happen, but we need to keep going
606 * if it does.
607 */
608 iip = 0;
609 }
610 nnode = ubifs_get_nnode(c, nnode, iip);
593 if (IS_ERR(nnode)) 611 if (IS_ERR(nnode))
594 return (void *)nnode; 612 return (void *)nnode;
595 } 613 }
596 614
597 return ubifs_get_pnode(c, nnode, 0); 615 for (iip = 0; iip < UBIFS_LPT_FANOUT; iip++)
616 if (nnode->nbranch[iip].lnum)
617 break;
618 if (iip >= UBIFS_LPT_FANOUT)
619 /* Should not happen, but we need to keep going if it does */
620 iip = 0;
621 return ubifs_get_pnode(c, nnode, iip);
598} 622}
599 623
600/** 624/**
@@ -684,7 +708,7 @@ static int make_tree_dirty(struct ubifs_info *c)
684 pnode = pnode_lookup(c, 0); 708 pnode = pnode_lookup(c, 0);
685 while (pnode) { 709 while (pnode) {
686 do_make_pnode_dirty(c, pnode); 710 do_make_pnode_dirty(c, pnode);
687 pnode = next_pnode(c, pnode); 711 pnode = next_pnode_to_dirty(c, pnode);
688 if (IS_ERR(pnode)) 712 if (IS_ERR(pnode))
689 return PTR_ERR(pnode); 713 return PTR_ERR(pnode);
690 } 714 }
@@ -749,7 +773,7 @@ static void lpt_tgc_start(struct ubifs_info *c)
749 * LPT trivial garbage collection is where a LPT LEB contains only dirty and 773 * LPT trivial garbage collection is where a LPT LEB contains only dirty and
750 * free space and so may be reused as soon as the next commit is completed. 774 * free space and so may be reused as soon as the next commit is completed.
751 * This function is called after the commit is completed (master node has been 775 * This function is called after the commit is completed (master node has been
752 * written) and unmaps LPT LEBs that were marked for trivial GC. 776 * written) and un-maps LPT LEBs that were marked for trivial GC.
753 */ 777 */
754static int lpt_tgc_end(struct ubifs_info *c) 778static int lpt_tgc_end(struct ubifs_info *c)
755{ 779{
@@ -1025,7 +1049,7 @@ static int make_node_dirty(struct ubifs_info *c, int node_type, int node_num,
1025 * @c: UBIFS file-system description object 1049 * @c: UBIFS file-system description object
1026 * @node_type: LPT node type 1050 * @node_type: LPT node type
1027 */ 1051 */
1028static int get_lpt_node_len(struct ubifs_info *c, int node_type) 1052static int get_lpt_node_len(const struct ubifs_info *c, int node_type)
1029{ 1053{
1030 switch (node_type) { 1054 switch (node_type) {
1031 case UBIFS_LPT_NNODE: 1055 case UBIFS_LPT_NNODE:
@@ -1046,7 +1070,7 @@ static int get_lpt_node_len(struct ubifs_info *c, int node_type)
1046 * @buf: buffer 1070 * @buf: buffer
1047 * @len: length of buffer 1071 * @len: length of buffer
1048 */ 1072 */
1049static int get_pad_len(struct ubifs_info *c, uint8_t *buf, int len) 1073static int get_pad_len(const struct ubifs_info *c, uint8_t *buf, int len)
1050{ 1074{
1051 int offs, pad_len; 1075 int offs, pad_len;
1052 1076
@@ -1063,7 +1087,8 @@ static int get_pad_len(struct ubifs_info *c, uint8_t *buf, int len)
1063 * @buf: buffer 1087 * @buf: buffer
1064 * @node_num: node number is returned here 1088 * @node_num: node number is returned here
1065 */ 1089 */
1066static int get_lpt_node_type(struct ubifs_info *c, uint8_t *buf, int *node_num) 1090static int get_lpt_node_type(const struct ubifs_info *c, uint8_t *buf,
1091 int *node_num)
1067{ 1092{
1068 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES; 1093 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1069 int pos = 0, node_type; 1094 int pos = 0, node_type;
@@ -1081,7 +1106,7 @@ static int get_lpt_node_type(struct ubifs_info *c, uint8_t *buf, int *node_num)
1081 * 1106 *
1082 * This function returns %1 if the buffer contains a node or %0 if it does not. 1107 * This function returns %1 if the buffer contains a node or %0 if it does not.
1083 */ 1108 */
1084static int is_a_node(struct ubifs_info *c, uint8_t *buf, int len) 1109static int is_a_node(const struct ubifs_info *c, uint8_t *buf, int len)
1085{ 1110{
1086 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES; 1111 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1087 int pos = 0, node_type, node_len; 1112 int pos = 0, node_type, node_len;
@@ -1105,7 +1130,6 @@ static int is_a_node(struct ubifs_info *c, uint8_t *buf, int len)
1105 return 1; 1130 return 1;
1106} 1131}
1107 1132
1108
1109/** 1133/**
1110 * lpt_gc_lnum - garbage collect a LPT LEB. 1134 * lpt_gc_lnum - garbage collect a LPT LEB.
1111 * @c: UBIFS file-system description object 1135 * @c: UBIFS file-system description object
@@ -1463,7 +1487,7 @@ void ubifs_lpt_free(struct ubifs_info *c, int wr_only)
1463#ifdef CONFIG_UBIFS_FS_DEBUG 1487#ifdef CONFIG_UBIFS_FS_DEBUG
1464 1488
1465/** 1489/**
1466 * dbg_is_all_ff - determine if a buffer contains only 0xff bytes. 1490 * dbg_is_all_ff - determine if a buffer contains only 0xFF bytes.
1467 * @buf: buffer 1491 * @buf: buffer
1468 * @len: buffer length 1492 * @len: buffer length
1469 */ 1493 */
@@ -1488,7 +1512,7 @@ static int dbg_is_nnode_dirty(struct ubifs_info *c, int lnum, int offs)
1488 struct ubifs_nnode *nnode; 1512 struct ubifs_nnode *nnode;
1489 int hght; 1513 int hght;
1490 1514
1491 /* Entire tree is in memory so first_nnode / next_nnode are ok */ 1515 /* Entire tree is in memory so first_nnode / next_nnode are OK */
1492 nnode = first_nnode(c, &hght); 1516 nnode = first_nnode(c, &hght);
1493 for (; nnode; nnode = next_nnode(c, nnode, &hght)) { 1517 for (; nnode; nnode = next_nnode(c, nnode, &hght)) {
1494 struct ubifs_nbranch *branch; 1518 struct ubifs_nbranch *branch;
@@ -1602,7 +1626,10 @@ static int dbg_check_ltab_lnum(struct ubifs_info *c, int lnum)
1602{ 1626{
1603 int err, len = c->leb_size, dirty = 0, node_type, node_num, node_len; 1627 int err, len = c->leb_size, dirty = 0, node_type, node_num, node_len;
1604 int ret; 1628 int ret;
1605 void *buf = c->dbg_buf; 1629 void *buf = c->dbg->buf;
1630
1631 if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
1632 return 0;
1606 1633
1607 dbg_lp("LEB %d", lnum); 1634 dbg_lp("LEB %d", lnum);
1608 err = ubi_read(c->ubi, lnum, buf, 0, c->leb_size); 1635 err = ubi_read(c->ubi, lnum, buf, 0, c->leb_size);
@@ -1704,6 +1731,9 @@ int dbg_chk_lpt_free_spc(struct ubifs_info *c)
1704 long long free = 0; 1731 long long free = 0;
1705 int i; 1732 int i;
1706 1733
1734 if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
1735 return 0;
1736
1707 for (i = 0; i < c->lpt_lebs; i++) { 1737 for (i = 0; i < c->lpt_lebs; i++) {
1708 if (c->ltab[i].tgc || c->ltab[i].cmt) 1738 if (c->ltab[i].tgc || c->ltab[i].cmt)
1709 continue; 1739 continue;
@@ -1716,6 +1746,8 @@ int dbg_chk_lpt_free_spc(struct ubifs_info *c)
1716 dbg_err("LPT space error: free %lld lpt_sz %lld", 1746 dbg_err("LPT space error: free %lld lpt_sz %lld",
1717 free, c->lpt_sz); 1747 free, c->lpt_sz);
1718 dbg_dump_lpt_info(c); 1748 dbg_dump_lpt_info(c);
1749 dbg_dump_lpt_lebs(c);
1750 dump_stack();
1719 return -EINVAL; 1751 return -EINVAL;
1720 } 1752 }
1721 return 0; 1753 return 0;
@@ -1731,15 +1763,19 @@ int dbg_chk_lpt_free_spc(struct ubifs_info *c)
1731 */ 1763 */
1732int dbg_chk_lpt_sz(struct ubifs_info *c, int action, int len) 1764int dbg_chk_lpt_sz(struct ubifs_info *c, int action, int len)
1733{ 1765{
1766 struct ubifs_debug_info *d = c->dbg;
1734 long long chk_lpt_sz, lpt_sz; 1767 long long chk_lpt_sz, lpt_sz;
1735 int err = 0; 1768 int err = 0;
1736 1769
1770 if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
1771 return 0;
1772
1737 switch (action) { 1773 switch (action) {
1738 case 0: 1774 case 0:
1739 c->chk_lpt_sz = 0; 1775 d->chk_lpt_sz = 0;
1740 c->chk_lpt_sz2 = 0; 1776 d->chk_lpt_sz2 = 0;
1741 c->chk_lpt_lebs = 0; 1777 d->chk_lpt_lebs = 0;
1742 c->chk_lpt_wastage = 0; 1778 d->chk_lpt_wastage = 0;
1743 if (c->dirty_pn_cnt > c->pnode_cnt) { 1779 if (c->dirty_pn_cnt > c->pnode_cnt) {
1744 dbg_err("dirty pnodes %d exceed max %d", 1780 dbg_err("dirty pnodes %d exceed max %d",
1745 c->dirty_pn_cnt, c->pnode_cnt); 1781 c->dirty_pn_cnt, c->pnode_cnt);
@@ -1752,35 +1788,35 @@ int dbg_chk_lpt_sz(struct ubifs_info *c, int action, int len)
1752 } 1788 }
1753 return err; 1789 return err;
1754 case 1: 1790 case 1:
1755 c->chk_lpt_sz += len; 1791 d->chk_lpt_sz += len;
1756 return 0; 1792 return 0;
1757 case 2: 1793 case 2:
1758 c->chk_lpt_sz += len; 1794 d->chk_lpt_sz += len;
1759 c->chk_lpt_wastage += len; 1795 d->chk_lpt_wastage += len;
1760 c->chk_lpt_lebs += 1; 1796 d->chk_lpt_lebs += 1;
1761 return 0; 1797 return 0;
1762 case 3: 1798 case 3:
1763 chk_lpt_sz = c->leb_size; 1799 chk_lpt_sz = c->leb_size;
1764 chk_lpt_sz *= c->chk_lpt_lebs; 1800 chk_lpt_sz *= d->chk_lpt_lebs;
1765 chk_lpt_sz += len - c->nhead_offs; 1801 chk_lpt_sz += len - c->nhead_offs;
1766 if (c->chk_lpt_sz != chk_lpt_sz) { 1802 if (d->chk_lpt_sz != chk_lpt_sz) {
1767 dbg_err("LPT wrote %lld but space used was %lld", 1803 dbg_err("LPT wrote %lld but space used was %lld",
1768 c->chk_lpt_sz, chk_lpt_sz); 1804 d->chk_lpt_sz, chk_lpt_sz);
1769 err = -EINVAL; 1805 err = -EINVAL;
1770 } 1806 }
1771 if (c->chk_lpt_sz > c->lpt_sz) { 1807 if (d->chk_lpt_sz > c->lpt_sz) {
1772 dbg_err("LPT wrote %lld but lpt_sz is %lld", 1808 dbg_err("LPT wrote %lld but lpt_sz is %lld",
1773 c->chk_lpt_sz, c->lpt_sz); 1809 d->chk_lpt_sz, c->lpt_sz);
1774 err = -EINVAL; 1810 err = -EINVAL;
1775 } 1811 }
1776 if (c->chk_lpt_sz2 && c->chk_lpt_sz != c->chk_lpt_sz2) { 1812 if (d->chk_lpt_sz2 && d->chk_lpt_sz != d->chk_lpt_sz2) {
1777 dbg_err("LPT layout size %lld but wrote %lld", 1813 dbg_err("LPT layout size %lld but wrote %lld",
1778 c->chk_lpt_sz, c->chk_lpt_sz2); 1814 d->chk_lpt_sz, d->chk_lpt_sz2);
1779 err = -EINVAL; 1815 err = -EINVAL;
1780 } 1816 }
1781 if (c->chk_lpt_sz2 && c->new_nhead_offs != len) { 1817 if (d->chk_lpt_sz2 && d->new_nhead_offs != len) {
1782 dbg_err("LPT new nhead offs: expected %d was %d", 1818 dbg_err("LPT new nhead offs: expected %d was %d",
1783 c->new_nhead_offs, len); 1819 d->new_nhead_offs, len);
1784 err = -EINVAL; 1820 err = -EINVAL;
1785 } 1821 }
1786 lpt_sz = (long long)c->pnode_cnt * c->pnode_sz; 1822 lpt_sz = (long long)c->pnode_cnt * c->pnode_sz;
@@ -1788,26 +1824,146 @@ int dbg_chk_lpt_sz(struct ubifs_info *c, int action, int len)
1788 lpt_sz += c->ltab_sz; 1824 lpt_sz += c->ltab_sz;
1789 if (c->big_lpt) 1825 if (c->big_lpt)
1790 lpt_sz += c->lsave_sz; 1826 lpt_sz += c->lsave_sz;
1791 if (c->chk_lpt_sz - c->chk_lpt_wastage > lpt_sz) { 1827 if (d->chk_lpt_sz - d->chk_lpt_wastage > lpt_sz) {
1792 dbg_err("LPT chk_lpt_sz %lld + waste %lld exceeds %lld", 1828 dbg_err("LPT chk_lpt_sz %lld + waste %lld exceeds %lld",
1793 c->chk_lpt_sz, c->chk_lpt_wastage, lpt_sz); 1829 d->chk_lpt_sz, d->chk_lpt_wastage, lpt_sz);
1794 err = -EINVAL; 1830 err = -EINVAL;
1795 } 1831 }
1796 if (err) 1832 if (err) {
1797 dbg_dump_lpt_info(c); 1833 dbg_dump_lpt_info(c);
1798 c->chk_lpt_sz2 = c->chk_lpt_sz; 1834 dbg_dump_lpt_lebs(c);
1799 c->chk_lpt_sz = 0; 1835 dump_stack();
1800 c->chk_lpt_wastage = 0; 1836 }
1801 c->chk_lpt_lebs = 0; 1837 d->chk_lpt_sz2 = d->chk_lpt_sz;
1802 c->new_nhead_offs = len; 1838 d->chk_lpt_sz = 0;
1839 d->chk_lpt_wastage = 0;
1840 d->chk_lpt_lebs = 0;
1841 d->new_nhead_offs = len;
1803 return err; 1842 return err;
1804 case 4: 1843 case 4:
1805 c->chk_lpt_sz += len; 1844 d->chk_lpt_sz += len;
1806 c->chk_lpt_wastage += len; 1845 d->chk_lpt_wastage += len;
1807 return 0; 1846 return 0;
1808 default: 1847 default:
1809 return -EINVAL; 1848 return -EINVAL;
1810 } 1849 }
1811} 1850}
1812 1851
1852/**
1853 * dbg_dump_lpt_leb - dump an LPT LEB.
1854 * @c: UBIFS file-system description object
1855 * @lnum: LEB number to dump
1856 *
1857 * This function dumps an LEB from LPT area. Nodes in this area are very
1858 * different to nodes in the main area (e.g., they do not have common headers,
1859 * they do not have 8-byte alignments, etc), so we have a separate function to
1860 * dump LPT area LEBs. Note, LPT has to be locked by the caller.
1861 */
1862static void dump_lpt_leb(const struct ubifs_info *c, int lnum)
1863{
1864 int err, len = c->leb_size, node_type, node_num, node_len, offs;
1865 void *buf = c->dbg->buf;
1866
1867 printk(KERN_DEBUG "(pid %d) start dumping LEB %d\n",
1868 current->pid, lnum);
1869 err = ubi_read(c->ubi, lnum, buf, 0, c->leb_size);
1870 if (err) {
1871 ubifs_err("cannot read LEB %d, error %d", lnum, err);
1872 return;
1873 }
1874 while (1) {
1875 offs = c->leb_size - len;
1876 if (!is_a_node(c, buf, len)) {
1877 int pad_len;
1878
1879 pad_len = get_pad_len(c, buf, len);
1880 if (pad_len) {
1881 printk(KERN_DEBUG "LEB %d:%d, pad %d bytes\n",
1882 lnum, offs, pad_len);
1883 buf += pad_len;
1884 len -= pad_len;
1885 continue;
1886 }
1887 if (len)
1888 printk(KERN_DEBUG "LEB %d:%d, free %d bytes\n",
1889 lnum, offs, len);
1890 break;
1891 }
1892
1893 node_type = get_lpt_node_type(c, buf, &node_num);
1894 switch (node_type) {
1895 case UBIFS_LPT_PNODE:
1896 {
1897 node_len = c->pnode_sz;
1898 if (c->big_lpt)
1899 printk(KERN_DEBUG "LEB %d:%d, pnode num %d\n",
1900 lnum, offs, node_num);
1901 else
1902 printk(KERN_DEBUG "LEB %d:%d, pnode\n",
1903 lnum, offs);
1904 break;
1905 }
1906 case UBIFS_LPT_NNODE:
1907 {
1908 int i;
1909 struct ubifs_nnode nnode;
1910
1911 node_len = c->nnode_sz;
1912 if (c->big_lpt)
1913 printk(KERN_DEBUG "LEB %d:%d, nnode num %d, ",
1914 lnum, offs, node_num);
1915 else
1916 printk(KERN_DEBUG "LEB %d:%d, nnode, ",
1917 lnum, offs);
1918 err = ubifs_unpack_nnode(c, buf, &nnode);
1919 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1920 printk("%d:%d", nnode.nbranch[i].lnum,
1921 nnode.nbranch[i].offs);
1922 if (i != UBIFS_LPT_FANOUT - 1)
1923 printk(", ");
1924 }
1925 printk("\n");
1926 break;
1927 }
1928 case UBIFS_LPT_LTAB:
1929 node_len = c->ltab_sz;
1930 printk(KERN_DEBUG "LEB %d:%d, ltab\n",
1931 lnum, offs);
1932 break;
1933 case UBIFS_LPT_LSAVE:
1934 node_len = c->lsave_sz;
1935 printk(KERN_DEBUG "LEB %d:%d, lsave len\n", lnum, offs);
1936 break;
1937 default:
1938 ubifs_err("LPT node type %d not recognized", node_type);
1939 return;
1940 }
1941
1942 buf += node_len;
1943 len -= node_len;
1944 }
1945
1946 printk(KERN_DEBUG "(pid %d) finish dumping LEB %d\n",
1947 current->pid, lnum);
1948}
1949
1950/**
1951 * dbg_dump_lpt_lebs - dump LPT lebs.
1952 * @c: UBIFS file-system description object
1953 *
1954 * This function dumps all LPT LEBs. The caller has to make sure the LPT is
1955 * locked.
1956 */
1957void dbg_dump_lpt_lebs(const struct ubifs_info *c)
1958{
1959 int i;
1960
1961 printk(KERN_DEBUG "(pid %d) start dumping all LPT LEBs\n",
1962 current->pid);
1963 for (i = 0; i < c->lpt_lebs; i++)
1964 dump_lpt_leb(c, i + c->lpt_first);
1965 printk(KERN_DEBUG "(pid %d) finish dumping all LPT LEBs\n",
1966 current->pid);
1967}
1968
1813#endif /* CONFIG_UBIFS_FS_DEBUG */ 1969#endif /* CONFIG_UBIFS_FS_DEBUG */
diff --git a/fs/ubifs/master.c b/fs/ubifs/master.c
index 71d5493bf565..a88f33801b98 100644
--- a/fs/ubifs/master.c
+++ b/fs/ubifs/master.c
@@ -354,7 +354,7 @@ int ubifs_write_master(struct ubifs_info *c)
354 int err, lnum, offs, len; 354 int err, lnum, offs, len;
355 355
356 if (c->ro_media) 356 if (c->ro_media)
357 return -EINVAL; 357 return -EROFS;
358 358
359 lnum = UBIFS_MST_LNUM; 359 lnum = UBIFS_MST_LNUM;
360 offs = c->mst_offs + c->mst_node_alsz; 360 offs = c->mst_offs + c->mst_node_alsz;
diff --git a/fs/ubifs/orphan.c b/fs/ubifs/orphan.c
index 9bd5a43d4526..152a7b34a141 100644
--- a/fs/ubifs/orphan.c
+++ b/fs/ubifs/orphan.c
@@ -46,7 +46,7 @@
46 * Orphans are accumulated in a rb-tree. When an inode's link count drops to 46 * Orphans are accumulated in a rb-tree. When an inode's link count drops to
47 * zero, the inode number is added to the rb-tree. It is removed from the tree 47 * zero, the inode number is added to the rb-tree. It is removed from the tree
48 * when the inode is deleted. Any new orphans that are in the orphan tree when 48 * when the inode is deleted. Any new orphans that are in the orphan tree when
49 * the commit is run, are written to the orphan area in 1 or more orph nodes. 49 * the commit is run, are written to the orphan area in 1 or more orphan nodes.
50 * If the orphan area is full, it is consolidated to make space. There is 50 * If the orphan area is full, it is consolidated to make space. There is
51 * always enough space because validation prevents the user from creating more 51 * always enough space because validation prevents the user from creating more
52 * than the maximum number of orphans allowed. 52 * than the maximum number of orphans allowed.
@@ -231,7 +231,7 @@ static int tot_avail_orphs(struct ubifs_info *c)
231} 231}
232 232
233/** 233/**
234 * do_write_orph_node - write a node 234 * do_write_orph_node - write a node to the orphan head.
235 * @c: UBIFS file-system description object 235 * @c: UBIFS file-system description object
236 * @len: length of node 236 * @len: length of node
237 * @atomic: write atomically 237 * @atomic: write atomically
@@ -264,11 +264,11 @@ static int do_write_orph_node(struct ubifs_info *c, int len, int atomic)
264} 264}
265 265
266/** 266/**
267 * write_orph_node - write an orph node 267 * write_orph_node - write an orphan node.
268 * @c: UBIFS file-system description object 268 * @c: UBIFS file-system description object
269 * @atomic: write atomically 269 * @atomic: write atomically
270 * 270 *
271 * This function builds an orph node from the cnext list and writes it to the 271 * This function builds an orphan node from the cnext list and writes it to the
272 * orphan head. On success, %0 is returned, otherwise a negative error code 272 * orphan head. On success, %0 is returned, otherwise a negative error code
273 * is returned. 273 * is returned.
274 */ 274 */
@@ -326,11 +326,11 @@ static int write_orph_node(struct ubifs_info *c, int atomic)
326} 326}
327 327
328/** 328/**
329 * write_orph_nodes - write orph nodes until there are no more to commit 329 * write_orph_nodes - write orphan nodes until there are no more to commit.
330 * @c: UBIFS file-system description object 330 * @c: UBIFS file-system description object
331 * @atomic: write atomically 331 * @atomic: write atomically
332 * 332 *
333 * This function writes orph nodes for all the orphans to commit. On success, 333 * This function writes orphan nodes for all the orphans to commit. On success,
334 * %0 is returned, otherwise a negative error code is returned. 334 * %0 is returned, otherwise a negative error code is returned.
335 */ 335 */
336static int write_orph_nodes(struct ubifs_info *c, int atomic) 336static int write_orph_nodes(struct ubifs_info *c, int atomic)
@@ -478,14 +478,14 @@ int ubifs_orphan_end_commit(struct ubifs_info *c)
478} 478}
479 479
480/** 480/**
481 * clear_orphans - erase all LEBs used for orphans. 481 * ubifs_clear_orphans - erase all LEBs used for orphans.
482 * @c: UBIFS file-system description object 482 * @c: UBIFS file-system description object
483 * 483 *
484 * If recovery is not required, then the orphans from the previous session 484 * If recovery is not required, then the orphans from the previous session
485 * are not needed. This function locates the LEBs used to record 485 * are not needed. This function locates the LEBs used to record
486 * orphans, and un-maps them. 486 * orphans, and un-maps them.
487 */ 487 */
488static int clear_orphans(struct ubifs_info *c) 488int ubifs_clear_orphans(struct ubifs_info *c)
489{ 489{
490 int lnum, err; 490 int lnum, err;
491 491
@@ -547,9 +547,9 @@ static int insert_dead_orphan(struct ubifs_info *c, ino_t inum)
547 * do_kill_orphans - remove orphan inodes from the index. 547 * do_kill_orphans - remove orphan inodes from the index.
548 * @c: UBIFS file-system description object 548 * @c: UBIFS file-system description object
549 * @sleb: scanned LEB 549 * @sleb: scanned LEB
550 * @last_cmt_no: cmt_no of last orph node read is passed and returned here 550 * @last_cmt_no: cmt_no of last orphan node read is passed and returned here
551 * @outofdate: whether the LEB is out of date is returned here 551 * @outofdate: whether the LEB is out of date is returned here
552 * @last_flagged: whether the end orph node is encountered 552 * @last_flagged: whether the end orphan node is encountered
553 * 553 *
554 * This function is a helper to the 'kill_orphans()' function. It goes through 554 * This function is a helper to the 'kill_orphans()' function. It goes through
555 * every orphan node in a LEB and for every inode number recorded, removes 555 * every orphan node in a LEB and for every inode number recorded, removes
@@ -580,8 +580,8 @@ static int do_kill_orphans(struct ubifs_info *c, struct ubifs_scan_leb *sleb,
580 /* 580 /*
581 * The commit number on the master node may be less, because 581 * The commit number on the master node may be less, because
582 * of a failed commit. If there are several failed commits in a 582 * of a failed commit. If there are several failed commits in a
583 * row, the commit number written on orph nodes will continue to 583 * row, the commit number written on orphan nodes will continue
584 * increase (because the commit number is adjusted here) even 584 * to increase (because the commit number is adjusted here) even
585 * though the commit number on the master node stays the same 585 * though the commit number on the master node stays the same
586 * because the master node has not been re-written. 586 * because the master node has not been re-written.
587 */ 587 */
@@ -589,9 +589,9 @@ static int do_kill_orphans(struct ubifs_info *c, struct ubifs_scan_leb *sleb,
589 c->cmt_no = cmt_no; 589 c->cmt_no = cmt_no;
590 if (cmt_no < *last_cmt_no && *last_flagged) { 590 if (cmt_no < *last_cmt_no && *last_flagged) {
591 /* 591 /*
592 * The last orph node had a higher commit number and was 592 * The last orphan node had a higher commit number and
593 * flagged as the last written for that commit number. 593 * was flagged as the last written for that commit
594 * That makes this orph node, out of date. 594 * number. That makes this orphan node, out of date.
595 */ 595 */
596 if (!first) { 596 if (!first) {
597 ubifs_err("out of order commit number %llu in " 597 ubifs_err("out of order commit number %llu in "
@@ -658,10 +658,10 @@ static int kill_orphans(struct ubifs_info *c)
658 /* 658 /*
659 * Orph nodes always start at c->orph_first and are written to each 659 * Orph nodes always start at c->orph_first and are written to each
660 * successive LEB in turn. Generally unused LEBs will have been unmapped 660 * successive LEB in turn. Generally unused LEBs will have been unmapped
661 * but may contain out of date orph nodes if the unmap didn't go 661 * but may contain out of date orphan nodes if the unmap didn't go
662 * through. In addition, the last orph node written for each commit is 662 * through. In addition, the last orphan node written for each commit is
663 * marked (top bit of orph->cmt_no is set to 1). It is possible that 663 * marked (top bit of orph->cmt_no is set to 1). It is possible that
664 * there are orph nodes from the next commit (i.e. the commit did not 664 * there are orphan nodes from the next commit (i.e. the commit did not
665 * complete successfully). In that case, no orphans will have been lost 665 * complete successfully). In that case, no orphans will have been lost
666 * due to the way that orphans are written, and any orphans added will 666 * due to the way that orphans are written, and any orphans added will
667 * be valid orphans anyway and so can be deleted. 667 * be valid orphans anyway and so can be deleted.
@@ -718,7 +718,7 @@ int ubifs_mount_orphans(struct ubifs_info *c, int unclean, int read_only)
718 if (unclean) 718 if (unclean)
719 err = kill_orphans(c); 719 err = kill_orphans(c);
720 else if (!read_only) 720 else if (!read_only)
721 err = clear_orphans(c); 721 err = ubifs_clear_orphans(c);
722 722
723 return err; 723 return err;
724} 724}
@@ -899,7 +899,7 @@ static int dbg_scan_orphans(struct ubifs_info *c, struct check_info *ci)
899 for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) { 899 for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
900 struct ubifs_scan_leb *sleb; 900 struct ubifs_scan_leb *sleb;
901 901
902 sleb = ubifs_scan(c, lnum, 0, c->dbg_buf); 902 sleb = ubifs_scan(c, lnum, 0, c->dbg->buf);
903 if (IS_ERR(sleb)) { 903 if (IS_ERR(sleb)) {
904 err = PTR_ERR(sleb); 904 err = PTR_ERR(sleb);
905 break; 905 break;
diff --git a/fs/ubifs/replay.c b/fs/ubifs/replay.c
index 21f7d047c306..ce42a7b0ca5a 100644
--- a/fs/ubifs/replay.c
+++ b/fs/ubifs/replay.c
@@ -144,7 +144,7 @@ static int set_bud_lprops(struct ubifs_info *c, struct replay_entry *r)
144 /* 144 /*
145 * If the replay order was perfect the dirty space would now be 145 * If the replay order was perfect the dirty space would now be
146 * zero. The order is not perfect because the the journal heads 146 * zero. The order is not perfect because the the journal heads
147 * race with eachother. This is not a problem but is does mean 147 * race with each other. This is not a problem but is does mean
148 * that the dirty space may temporarily exceed c->leb_size 148 * that the dirty space may temporarily exceed c->leb_size
149 * during the replay. 149 * during the replay.
150 */ 150 */
@@ -656,7 +656,7 @@ out_dump:
656 * @dirty: amount of dirty space from padding and deletion nodes 656 * @dirty: amount of dirty space from padding and deletion nodes
657 * 657 *
658 * This function inserts a reference node to the replay tree and returns zero 658 * This function inserts a reference node to the replay tree and returns zero
659 * in case of success ort a negative error code in case of failure. 659 * in case of success or a negative error code in case of failure.
660 */ 660 */
661static int insert_ref_node(struct ubifs_info *c, int lnum, int offs, 661static int insert_ref_node(struct ubifs_info *c, int lnum, int offs,
662 unsigned long long sqnum, int free, int dirty) 662 unsigned long long sqnum, int free, int dirty)
@@ -883,7 +883,7 @@ static int replay_log_leb(struct ubifs_info *c, int lnum, int offs, void *sbuf)
883 * This means that we reached end of log and now 883 * This means that we reached end of log and now
884 * look to the older log data, which was already 884 * look to the older log data, which was already
885 * committed but the eraseblock was not erased (UBIFS 885 * committed but the eraseblock was not erased (UBIFS
886 * only unmaps it). So this basically means we have to 886 * only un-maps it). So this basically means we have to
887 * exit with "end of log" code. 887 * exit with "end of log" code.
888 */ 888 */
889 err = 1; 889 err = 1;
@@ -1062,6 +1062,15 @@ int ubifs_replay_journal(struct ubifs_info *c)
1062 if (err) 1062 if (err)
1063 goto out; 1063 goto out;
1064 1064
1065 /*
1066 * UBIFS budgeting calculations use @c->budg_uncommitted_idx variable
1067 * to roughly estimate index growth. Things like @c->min_idx_lebs
1068 * depend on it. This means we have to initialize it to make sure
1069 * budgeting works properly.
1070 */
1071 c->budg_uncommitted_idx = atomic_long_read(&c->dirty_zn_cnt);
1072 c->budg_uncommitted_idx *= c->max_idx_node_sz;
1073
1065 ubifs_assert(c->bud_bytes <= c->max_bud_bytes || c->need_recovery); 1074 ubifs_assert(c->bud_bytes <= c->max_bud_bytes || c->need_recovery);
1066 dbg_mnt("finished, log head LEB %d:%d, max_sqnum %llu, " 1075 dbg_mnt("finished, log head LEB %d:%d, max_sqnum %llu, "
1067 "highest_inum %lu", c->lhead_lnum, c->lhead_offs, c->max_sqnum, 1076 "highest_inum %lu", c->lhead_lnum, c->lhead_offs, c->max_sqnum,
diff --git a/fs/ubifs/sb.c b/fs/ubifs/sb.c
index 0f392351dc5a..e070c643d1bb 100644
--- a/fs/ubifs/sb.c
+++ b/fs/ubifs/sb.c
@@ -28,6 +28,7 @@
28 28
29#include "ubifs.h" 29#include "ubifs.h"
30#include <linux/random.h> 30#include <linux/random.h>
31#include <linux/math64.h>
31 32
32/* 33/*
33 * Default journal size in logical eraseblocks as a percent of total 34 * Default journal size in logical eraseblocks as a percent of total
@@ -80,7 +81,7 @@ static int create_default_filesystem(struct ubifs_info *c)
80 int err, tmp, jnl_lebs, log_lebs, max_buds, main_lebs, main_first; 81 int err, tmp, jnl_lebs, log_lebs, max_buds, main_lebs, main_first;
81 int lpt_lebs, lpt_first, orph_lebs, big_lpt, ino_waste, sup_flags = 0; 82 int lpt_lebs, lpt_first, orph_lebs, big_lpt, ino_waste, sup_flags = 0;
82 int min_leb_cnt = UBIFS_MIN_LEB_CNT; 83 int min_leb_cnt = UBIFS_MIN_LEB_CNT;
83 uint64_t tmp64, main_bytes; 84 long long tmp64, main_bytes;
84 __le64 tmp_le64; 85 __le64 tmp_le64;
85 86
86 /* Some functions called from here depend on the @c->key_len filed */ 87 /* Some functions called from here depend on the @c->key_len filed */
@@ -160,7 +161,7 @@ static int create_default_filesystem(struct ubifs_info *c)
160 if (!sup) 161 if (!sup)
161 return -ENOMEM; 162 return -ENOMEM;
162 163
163 tmp64 = (uint64_t)max_buds * c->leb_size; 164 tmp64 = (long long)max_buds * c->leb_size;
164 if (big_lpt) 165 if (big_lpt)
165 sup_flags |= UBIFS_FLG_BIGLPT; 166 sup_flags |= UBIFS_FLG_BIGLPT;
166 167
@@ -179,14 +180,16 @@ static int create_default_filesystem(struct ubifs_info *c)
179 sup->fanout = cpu_to_le32(DEFAULT_FANOUT); 180 sup->fanout = cpu_to_le32(DEFAULT_FANOUT);
180 sup->lsave_cnt = cpu_to_le32(c->lsave_cnt); 181 sup->lsave_cnt = cpu_to_le32(c->lsave_cnt);
181 sup->fmt_version = cpu_to_le32(UBIFS_FORMAT_VERSION); 182 sup->fmt_version = cpu_to_le32(UBIFS_FORMAT_VERSION);
182 sup->default_compr = cpu_to_le16(UBIFS_COMPR_LZO);
183 sup->time_gran = cpu_to_le32(DEFAULT_TIME_GRAN); 183 sup->time_gran = cpu_to_le32(DEFAULT_TIME_GRAN);
184 if (c->mount_opts.override_compr)
185 sup->default_compr = cpu_to_le16(c->mount_opts.compr_type);
186 else
187 sup->default_compr = cpu_to_le16(UBIFS_COMPR_LZO);
184 188
185 generate_random_uuid(sup->uuid); 189 generate_random_uuid(sup->uuid);
186 190
187 main_bytes = (uint64_t)main_lebs * c->leb_size; 191 main_bytes = (long long)main_lebs * c->leb_size;
188 tmp64 = main_bytes * DEFAULT_RP_PERCENT; 192 tmp64 = div_u64(main_bytes * DEFAULT_RP_PERCENT, 100);
189 do_div(tmp64, 100);
190 if (tmp64 > DEFAULT_MAX_RP_SIZE) 193 if (tmp64 > DEFAULT_MAX_RP_SIZE)
191 tmp64 = DEFAULT_MAX_RP_SIZE; 194 tmp64 = DEFAULT_MAX_RP_SIZE;
192 sup->rp_size = cpu_to_le64(tmp64); 195 sup->rp_size = cpu_to_le64(tmp64);
@@ -582,16 +585,15 @@ int ubifs_read_superblock(struct ubifs_info *c)
582 c->jhead_cnt = le32_to_cpu(sup->jhead_cnt) + NONDATA_JHEADS_CNT; 585 c->jhead_cnt = le32_to_cpu(sup->jhead_cnt) + NONDATA_JHEADS_CNT;
583 c->fanout = le32_to_cpu(sup->fanout); 586 c->fanout = le32_to_cpu(sup->fanout);
584 c->lsave_cnt = le32_to_cpu(sup->lsave_cnt); 587 c->lsave_cnt = le32_to_cpu(sup->lsave_cnt);
585 c->default_compr = le16_to_cpu(sup->default_compr);
586 c->rp_size = le64_to_cpu(sup->rp_size); 588 c->rp_size = le64_to_cpu(sup->rp_size);
587 c->rp_uid = le32_to_cpu(sup->rp_uid); 589 c->rp_uid = le32_to_cpu(sup->rp_uid);
588 c->rp_gid = le32_to_cpu(sup->rp_gid); 590 c->rp_gid = le32_to_cpu(sup->rp_gid);
589 sup_flags = le32_to_cpu(sup->flags); 591 sup_flags = le32_to_cpu(sup->flags);
592 if (!c->mount_opts.override_compr)
593 c->default_compr = le16_to_cpu(sup->default_compr);
590 594
591 c->vfs_sb->s_time_gran = le32_to_cpu(sup->time_gran); 595 c->vfs_sb->s_time_gran = le32_to_cpu(sup->time_gran);
592
593 memcpy(&c->uuid, &sup->uuid, 16); 596 memcpy(&c->uuid, &sup->uuid, 16);
594
595 c->big_lpt = !!(sup_flags & UBIFS_FLG_BIGLPT); 597 c->big_lpt = !!(sup_flags & UBIFS_FLG_BIGLPT);
596 598
597 /* Automatically increase file system size to the maximum size */ 599 /* Automatically increase file system size to the maximum size */
diff --git a/fs/ubifs/shrinker.c b/fs/ubifs/shrinker.c
index f248533841a2..e7bab52a1410 100644
--- a/fs/ubifs/shrinker.c
+++ b/fs/ubifs/shrinker.c
@@ -151,7 +151,7 @@ static int shrink_tnc(struct ubifs_info *c, int nr, int age, int *contention)
151 * @contention: if any contention, this is set to %1 151 * @contention: if any contention, this is set to %1
152 * 152 *
153 * This function walks the list of mounted UBIFS file-systems and frees clean 153 * This function walks the list of mounted UBIFS file-systems and frees clean
154 * znodes which are older then @age, until at least @nr znodes are freed. 154 * znodes which are older than @age, until at least @nr znodes are freed.
155 * Returns the number of freed znodes. 155 * Returns the number of freed znodes.
156 */ 156 */
157static int shrink_tnc_trees(int nr, int age, int *contention) 157static int shrink_tnc_trees(int nr, int age, int *contention)
diff --git a/fs/ubifs/super.c b/fs/ubifs/super.c
index d80b2aef42b6..1182b66a5491 100644
--- a/fs/ubifs/super.c
+++ b/fs/ubifs/super.c
@@ -34,6 +34,8 @@
34#include <linux/parser.h> 34#include <linux/parser.h>
35#include <linux/seq_file.h> 35#include <linux/seq_file.h>
36#include <linux/mount.h> 36#include <linux/mount.h>
37#include <linux/math64.h>
38#include <linux/writeback.h>
37#include "ubifs.h" 39#include "ubifs.h"
38 40
39/* 41/*
@@ -395,6 +397,7 @@ static int ubifs_statfs(struct dentry *dentry, struct kstatfs *buf)
395 buf->f_namelen = UBIFS_MAX_NLEN; 397 buf->f_namelen = UBIFS_MAX_NLEN;
396 buf->f_fsid.val[0] = le32_to_cpu(uuid[0]) ^ le32_to_cpu(uuid[2]); 398 buf->f_fsid.val[0] = le32_to_cpu(uuid[0]) ^ le32_to_cpu(uuid[2]);
397 buf->f_fsid.val[1] = le32_to_cpu(uuid[1]) ^ le32_to_cpu(uuid[3]); 399 buf->f_fsid.val[1] = le32_to_cpu(uuid[1]) ^ le32_to_cpu(uuid[3]);
400 ubifs_assert(buf->f_bfree <= c->block_cnt);
398 return 0; 401 return 0;
399} 402}
400 403
@@ -417,39 +420,62 @@ static int ubifs_show_options(struct seq_file *s, struct vfsmount *mnt)
417 else if (c->mount_opts.chk_data_crc == 1) 420 else if (c->mount_opts.chk_data_crc == 1)
418 seq_printf(s, ",no_chk_data_crc"); 421 seq_printf(s, ",no_chk_data_crc");
419 422
423 if (c->mount_opts.override_compr) {
424 seq_printf(s, ",compr=");
425 seq_printf(s, ubifs_compr_name(c->mount_opts.compr_type));
426 }
427
420 return 0; 428 return 0;
421} 429}
422 430
423static int ubifs_sync_fs(struct super_block *sb, int wait) 431static int ubifs_sync_fs(struct super_block *sb, int wait)
424{ 432{
433 int i, err;
425 struct ubifs_info *c = sb->s_fs_info; 434 struct ubifs_info *c = sb->s_fs_info;
426 int i, ret = 0, err; 435 struct writeback_control wbc = {
427 long long bud_bytes; 436 .sync_mode = WB_SYNC_ALL,
437 .range_start = 0,
438 .range_end = LLONG_MAX,
439 .nr_to_write = LONG_MAX,
440 };
428 441
429 if (c->jheads) { 442 /*
430 for (i = 0; i < c->jhead_cnt; i++) { 443 * Zero @wait is just an advisory thing to help the file system shove
431 err = ubifs_wbuf_sync(&c->jheads[i].wbuf); 444 * lots of data into the queues, and there will be the second
432 if (err && !ret) 445 * '->sync_fs()' call, with non-zero @wait.
433 ret = err; 446 */
434 } 447 if (!wait)
448 return 0;
435 449
436 /* Commit the journal unless it has too little data */ 450 if (sb->s_flags & MS_RDONLY)
437 spin_lock(&c->buds_lock); 451 return 0;
438 bud_bytes = c->bud_bytes; 452
439 spin_unlock(&c->buds_lock); 453 /*
440 if (bud_bytes > c->leb_size) { 454 * VFS calls '->sync_fs()' before synchronizing all dirty inodes and
441 err = ubifs_run_commit(c); 455 * pages, so synchronize them first, then commit the journal. Strictly
442 if (err) 456 * speaking, it is not necessary to commit the journal here,
443 return err; 457 * synchronizing write-buffers would be enough. But committing makes
444 } 458 * UBIFS free space predictions much more accurate, so we want to let
445 } 459 * the user be able to get more accurate results of 'statfs()' after
460 * they synchronize the file system.
461 */
462 generic_sync_sb_inodes(sb, &wbc);
446 463
447 /* 464 /*
448 * We ought to call sync for c->ubi but it does not have one. If it had 465 * Synchronize write buffers, because 'ubifs_run_commit()' does not
449 * it would in turn call mtd->sync, however mtd operations are 466 * do this if it waits for an already running commit.
450 * synchronous anyway, so we don't lose any sleep here.
451 */ 467 */
452 return ret; 468 for (i = 0; i < c->jhead_cnt; i++) {
469 err = ubifs_wbuf_sync(&c->jheads[i].wbuf);
470 if (err)
471 return err;
472 }
473
474 err = ubifs_run_commit(c);
475 if (err)
476 return err;
477
478 return ubi_sync(c->vi.ubi_num);
453} 479}
454 480
455/** 481/**
@@ -548,15 +574,8 @@ static int init_constants_early(struct ubifs_info *c)
548 c->ranges[UBIFS_IDX_NODE].max_len = INT_MAX; 574 c->ranges[UBIFS_IDX_NODE].max_len = INT_MAX;
549 575
550 /* 576 /*
551 * Initialize dead and dark LEB space watermarks. 577 * Initialize dead and dark LEB space watermarks. See gc.c for comments
552 * 578 * about these values.
553 * Dead space is the space which cannot be used. Its watermark is
554 * equivalent to min. I/O unit or minimum node size if it is greater
555 * then min. I/O unit.
556 *
557 * Dark space is the space which might be used, or might not, depending
558 * on which node should be written to the LEB. Its watermark is
559 * equivalent to maximum UBIFS node size.
560 */ 579 */
561 c->dead_wm = ALIGN(MIN_WRITE_SZ, c->min_io_size); 580 c->dead_wm = ALIGN(MIN_WRITE_SZ, c->min_io_size);
562 c->dark_wm = ALIGN(UBIFS_MAX_NODE_SZ, c->min_io_size); 581 c->dark_wm = ALIGN(UBIFS_MAX_NODE_SZ, c->min_io_size);
@@ -596,7 +615,7 @@ static int bud_wbuf_callback(struct ubifs_info *c, int lnum, int free, int pad)
596} 615}
597 616
598/* 617/*
599 * init_constants_late - initialize UBIFS constants. 618 * init_constants_sb - initialize UBIFS constants.
600 * @c: UBIFS file-system description object 619 * @c: UBIFS file-system description object
601 * 620 *
602 * This is a helper function which initializes various UBIFS constants after 621 * This is a helper function which initializes various UBIFS constants after
@@ -604,10 +623,10 @@ static int bud_wbuf_callback(struct ubifs_info *c, int lnum, int free, int pad)
604 * makes sure they are all right. Returns zero in case of success and a 623 * makes sure they are all right. Returns zero in case of success and a
605 * negative error code in case of failure. 624 * negative error code in case of failure.
606 */ 625 */
607static int init_constants_late(struct ubifs_info *c) 626static int init_constants_sb(struct ubifs_info *c)
608{ 627{
609 int tmp, err; 628 int tmp, err;
610 uint64_t tmp64; 629 long long tmp64;
611 630
612 c->main_bytes = (long long)c->main_lebs * c->leb_size; 631 c->main_bytes = (long long)c->main_lebs * c->leb_size;
613 c->max_znode_sz = sizeof(struct ubifs_znode) + 632 c->max_znode_sz = sizeof(struct ubifs_znode) +
@@ -634,9 +653,8 @@ static int init_constants_late(struct ubifs_info *c)
634 * Make sure that the log is large enough to fit reference nodes for 653 * Make sure that the log is large enough to fit reference nodes for
635 * all buds plus one reserved LEB. 654 * all buds plus one reserved LEB.
636 */ 655 */
637 tmp64 = c->max_bud_bytes; 656 tmp64 = c->max_bud_bytes + c->leb_size - 1;
638 tmp = do_div(tmp64, c->leb_size); 657 c->max_bud_cnt = div_u64(tmp64, c->leb_size);
639 c->max_bud_cnt = tmp64 + !!tmp;
640 tmp = (c->ref_node_alsz * c->max_bud_cnt + c->leb_size - 1); 658 tmp = (c->ref_node_alsz * c->max_bud_cnt + c->leb_size - 1);
641 tmp /= c->leb_size; 659 tmp /= c->leb_size;
642 tmp += 1; 660 tmp += 1;
@@ -672,7 +690,7 @@ static int init_constants_late(struct ubifs_info *c)
672 * Consequently, if the journal is too small, UBIFS will treat it as 690 * Consequently, if the journal is too small, UBIFS will treat it as
673 * always full. 691 * always full.
674 */ 692 */
675 tmp64 = (uint64_t)(c->jhead_cnt + 1) * c->leb_size + 1; 693 tmp64 = (long long)(c->jhead_cnt + 1) * c->leb_size + 1;
676 if (c->bg_bud_bytes < tmp64) 694 if (c->bg_bud_bytes < tmp64)
677 c->bg_bud_bytes = tmp64; 695 c->bg_bud_bytes = tmp64;
678 if (c->max_bud_bytes < tmp64 + c->leb_size) 696 if (c->max_bud_bytes < tmp64 + c->leb_size)
@@ -682,6 +700,21 @@ static int init_constants_late(struct ubifs_info *c)
682 if (err) 700 if (err)
683 return err; 701 return err;
684 702
703 return 0;
704}
705
706/*
707 * init_constants_master - initialize UBIFS constants.
708 * @c: UBIFS file-system description object
709 *
710 * This is a helper function which initializes various UBIFS constants after
711 * the master node has been read. It also checks various UBIFS parameters and
712 * makes sure they are all right.
713 */
714static void init_constants_master(struct ubifs_info *c)
715{
716 long long tmp64;
717
685 c->min_idx_lebs = ubifs_calc_min_idx_lebs(c); 718 c->min_idx_lebs = ubifs_calc_min_idx_lebs(c);
686 719
687 /* 720 /*
@@ -690,26 +723,25 @@ static int init_constants_late(struct ubifs_info *c)
690 * necessary to report something for the 'statfs()' call. 723 * necessary to report something for the 'statfs()' call.
691 * 724 *
692 * Subtract the LEB reserved for GC, the LEB which is reserved for 725 * Subtract the LEB reserved for GC, the LEB which is reserved for
693 * deletions, and assume only one journal head is available. 726 * deletions, minimum LEBs for the index, and assume only one journal
727 * head is available.
694 */ 728 */
695 tmp64 = c->main_lebs - 2 - c->jhead_cnt + 1; 729 tmp64 = c->main_lebs - 1 - 1 - MIN_INDEX_LEBS - c->jhead_cnt + 1;
696 tmp64 *= (uint64_t)c->leb_size - c->leb_overhead; 730 tmp64 *= (long long)c->leb_size - c->leb_overhead;
697 tmp64 = ubifs_reported_space(c, tmp64); 731 tmp64 = ubifs_reported_space(c, tmp64);
698 c->block_cnt = tmp64 >> UBIFS_BLOCK_SHIFT; 732 c->block_cnt = tmp64 >> UBIFS_BLOCK_SHIFT;
699
700 return 0;
701} 733}
702 734
703/** 735/**
704 * take_gc_lnum - reserve GC LEB. 736 * take_gc_lnum - reserve GC LEB.
705 * @c: UBIFS file-system description object 737 * @c: UBIFS file-system description object
706 * 738 *
707 * This function ensures that the LEB reserved for garbage collection is 739 * This function ensures that the LEB reserved for garbage collection is marked
708 * unmapped and is marked as "taken" in lprops. We also have to set free space 740 * as "taken" in lprops. We also have to set free space to LEB size and dirty
709 * to LEB size and dirty space to zero, because lprops may contain out-of-date 741 * space to zero, because lprops may contain out-of-date information if the
710 * information if the file-system was un-mounted before it has been committed. 742 * file-system was un-mounted before it has been committed. This function
711 * This function returns zero in case of success and a negative error code in 743 * returns zero in case of success and a negative error code in case of
712 * case of failure. 744 * failure.
713 */ 745 */
714static int take_gc_lnum(struct ubifs_info *c) 746static int take_gc_lnum(struct ubifs_info *c)
715{ 747{
@@ -720,10 +752,6 @@ static int take_gc_lnum(struct ubifs_info *c)
720 return -EINVAL; 752 return -EINVAL;
721 } 753 }
722 754
723 err = ubifs_leb_unmap(c, c->gc_lnum);
724 if (err)
725 return err;
726
727 /* And we have to tell lprops that this LEB is taken */ 755 /* And we have to tell lprops that this LEB is taken */
728 err = ubifs_change_one_lp(c, c->gc_lnum, c->leb_size, 0, 756 err = ubifs_change_one_lp(c, c->gc_lnum, c->leb_size, 0,
729 LPROPS_TAKEN, 0, 0); 757 LPROPS_TAKEN, 0, 0);
@@ -878,6 +906,7 @@ static int check_volume_empty(struct ubifs_info *c)
878 * Opt_no_bulk_read: disable bulk-reads 906 * Opt_no_bulk_read: disable bulk-reads
879 * Opt_chk_data_crc: check CRCs when reading data nodes 907 * Opt_chk_data_crc: check CRCs when reading data nodes
880 * Opt_no_chk_data_crc: do not check CRCs when reading data nodes 908 * Opt_no_chk_data_crc: do not check CRCs when reading data nodes
909 * Opt_override_compr: override default compressor
881 * Opt_err: just end of array marker 910 * Opt_err: just end of array marker
882 */ 911 */
883enum { 912enum {
@@ -887,6 +916,7 @@ enum {
887 Opt_no_bulk_read, 916 Opt_no_bulk_read,
888 Opt_chk_data_crc, 917 Opt_chk_data_crc,
889 Opt_no_chk_data_crc, 918 Opt_no_chk_data_crc,
919 Opt_override_compr,
890 Opt_err, 920 Opt_err,
891}; 921};
892 922
@@ -897,6 +927,7 @@ static const match_table_t tokens = {
897 {Opt_no_bulk_read, "no_bulk_read"}, 927 {Opt_no_bulk_read, "no_bulk_read"},
898 {Opt_chk_data_crc, "chk_data_crc"}, 928 {Opt_chk_data_crc, "chk_data_crc"},
899 {Opt_no_chk_data_crc, "no_chk_data_crc"}, 929 {Opt_no_chk_data_crc, "no_chk_data_crc"},
930 {Opt_override_compr, "compr=%s"},
900 {Opt_err, NULL}, 931 {Opt_err, NULL},
901}; 932};
902 933
@@ -926,13 +957,16 @@ static int ubifs_parse_options(struct ubifs_info *c, char *options,
926 957
927 token = match_token(p, tokens, args); 958 token = match_token(p, tokens, args);
928 switch (token) { 959 switch (token) {
960 /*
961 * %Opt_fast_unmount and %Opt_norm_unmount options are ignored.
962 * We accepte them in order to be backware-compatible. But this
963 * should be removed at some point.
964 */
929 case Opt_fast_unmount: 965 case Opt_fast_unmount:
930 c->mount_opts.unmount_mode = 2; 966 c->mount_opts.unmount_mode = 2;
931 c->fast_unmount = 1;
932 break; 967 break;
933 case Opt_norm_unmount: 968 case Opt_norm_unmount:
934 c->mount_opts.unmount_mode = 1; 969 c->mount_opts.unmount_mode = 1;
935 c->fast_unmount = 0;
936 break; 970 break;
937 case Opt_bulk_read: 971 case Opt_bulk_read:
938 c->mount_opts.bulk_read = 2; 972 c->mount_opts.bulk_read = 2;
@@ -950,6 +984,28 @@ static int ubifs_parse_options(struct ubifs_info *c, char *options,
950 c->mount_opts.chk_data_crc = 1; 984 c->mount_opts.chk_data_crc = 1;
951 c->no_chk_data_crc = 1; 985 c->no_chk_data_crc = 1;
952 break; 986 break;
987 case Opt_override_compr:
988 {
989 char *name = match_strdup(&args[0]);
990
991 if (!name)
992 return -ENOMEM;
993 if (!strcmp(name, "none"))
994 c->mount_opts.compr_type = UBIFS_COMPR_NONE;
995 else if (!strcmp(name, "lzo"))
996 c->mount_opts.compr_type = UBIFS_COMPR_LZO;
997 else if (!strcmp(name, "zlib"))
998 c->mount_opts.compr_type = UBIFS_COMPR_ZLIB;
999 else {
1000 ubifs_err("unknown compressor \"%s\"", name);
1001 kfree(name);
1002 return -EINVAL;
1003 }
1004 kfree(name);
1005 c->mount_opts.override_compr = 1;
1006 c->default_compr = c->mount_opts.compr_type;
1007 break;
1008 }
953 default: 1009 default:
954 ubifs_err("unrecognized mount option \"%s\" " 1010 ubifs_err("unrecognized mount option \"%s\" "
955 "or missing value", p); 1011 "or missing value", p);
@@ -1019,6 +1075,25 @@ again:
1019} 1075}
1020 1076
1021/** 1077/**
1078 * check_free_space - check if there is enough free space to mount.
1079 * @c: UBIFS file-system description object
1080 *
1081 * This function makes sure UBIFS has enough free space to be mounted in
1082 * read/write mode. UBIFS must always have some free space to allow deletions.
1083 */
1084static int check_free_space(struct ubifs_info *c)
1085{
1086 ubifs_assert(c->dark_wm > 0);
1087 if (c->lst.total_free + c->lst.total_dirty < c->dark_wm) {
1088 ubifs_err("insufficient free space to mount in read/write mode");
1089 dbg_dump_budg(c);
1090 dbg_dump_lprops(c);
1091 return -ENOSPC;
1092 }
1093 return 0;
1094}
1095
1096/**
1022 * mount_ubifs - mount UBIFS file-system. 1097 * mount_ubifs - mount UBIFS file-system.
1023 * @c: UBIFS file-system description object 1098 * @c: UBIFS file-system description object
1024 * 1099 *
@@ -1039,11 +1114,9 @@ static int mount_ubifs(struct ubifs_info *c)
1039 if (err) 1114 if (err)
1040 return err; 1115 return err;
1041 1116
1042#ifdef CONFIG_UBIFS_FS_DEBUG 1117 err = ubifs_debugging_init(c);
1043 c->dbg_buf = vmalloc(c->leb_size); 1118 if (err)
1044 if (!c->dbg_buf) 1119 return err;
1045 return -ENOMEM;
1046#endif
1047 1120
1048 err = check_volume_empty(c); 1121 err = check_volume_empty(c);
1049 if (err) 1122 if (err)
@@ -1100,27 +1173,25 @@ static int mount_ubifs(struct ubifs_info *c)
1100 goto out_free; 1173 goto out_free;
1101 1174
1102 /* 1175 /*
1103 * Make sure the compressor which is set as the default on in the 1176 * Make sure the compressor which is set as default in the superblock
1104 * superblock was actually compiled in. 1177 * or overridden by mount options is actually compiled in.
1105 */ 1178 */
1106 if (!ubifs_compr_present(c->default_compr)) { 1179 if (!ubifs_compr_present(c->default_compr)) {
1107 ubifs_warn("'%s' compressor is set by superblock, but not " 1180 ubifs_err("'compressor \"%s\" is not compiled in",
1108 "compiled in", ubifs_compr_name(c->default_compr)); 1181 ubifs_compr_name(c->default_compr));
1109 c->default_compr = UBIFS_COMPR_NONE; 1182 goto out_free;
1110 } 1183 }
1111 1184
1112 dbg_failure_mode_registration(c); 1185 err = init_constants_sb(c);
1113
1114 err = init_constants_late(c);
1115 if (err) 1186 if (err)
1116 goto out_dereg; 1187 goto out_free;
1117 1188
1118 sz = ALIGN(c->max_idx_node_sz, c->min_io_size); 1189 sz = ALIGN(c->max_idx_node_sz, c->min_io_size);
1119 sz = ALIGN(sz + c->max_idx_node_sz, c->min_io_size); 1190 sz = ALIGN(sz + c->max_idx_node_sz, c->min_io_size);
1120 c->cbuf = kmalloc(sz, GFP_NOFS); 1191 c->cbuf = kmalloc(sz, GFP_NOFS);
1121 if (!c->cbuf) { 1192 if (!c->cbuf) {
1122 err = -ENOMEM; 1193 err = -ENOMEM;
1123 goto out_dereg; 1194 goto out_free;
1124 } 1195 }
1125 1196
1126 sprintf(c->bgt_name, BGT_NAME_PATTERN, c->vi.ubi_num, c->vi.vol_id); 1197 sprintf(c->bgt_name, BGT_NAME_PATTERN, c->vi.ubi_num, c->vi.vol_id);
@@ -1145,6 +1216,8 @@ static int mount_ubifs(struct ubifs_info *c)
1145 if (err) 1216 if (err)
1146 goto out_master; 1217 goto out_master;
1147 1218
1219 init_constants_master(c);
1220
1148 if ((c->mst_node->flags & cpu_to_le32(UBIFS_MST_DIRTY)) != 0) { 1221 if ((c->mst_node->flags & cpu_to_le32(UBIFS_MST_DIRTY)) != 0) {
1149 ubifs_msg("recovery needed"); 1222 ubifs_msg("recovery needed");
1150 c->need_recovery = 1; 1223 c->need_recovery = 1;
@@ -1183,12 +1256,9 @@ static int mount_ubifs(struct ubifs_info *c)
1183 if (!mounted_read_only) { 1256 if (!mounted_read_only) {
1184 int lnum; 1257 int lnum;
1185 1258
1186 /* Check for enough free space */ 1259 err = check_free_space(c);
1187 if (ubifs_calc_available(c, c->min_idx_lebs) <= 0) { 1260 if (err)
1188 ubifs_err("insufficient available space");
1189 err = -EINVAL;
1190 goto out_orphans; 1261 goto out_orphans;
1191 }
1192 1262
1193 /* Check for enough log space */ 1263 /* Check for enough log space */
1194 lnum = c->lhead_lnum + 1; 1264 lnum = c->lhead_lnum + 1;
@@ -1205,10 +1275,19 @@ static int mount_ubifs(struct ubifs_info *c)
1205 if (err) 1275 if (err)
1206 goto out_orphans; 1276 goto out_orphans;
1207 err = ubifs_rcvry_gc_commit(c); 1277 err = ubifs_rcvry_gc_commit(c);
1208 } else 1278 } else {
1209 err = take_gc_lnum(c); 1279 err = take_gc_lnum(c);
1210 if (err) 1280 if (err)
1211 goto out_orphans; 1281 goto out_orphans;
1282
1283 /*
1284 * GC LEB may contain garbage if there was an unclean
1285 * reboot, and it should be un-mapped.
1286 */
1287 err = ubifs_leb_unmap(c, c->gc_lnum);
1288 if (err)
1289 return err;
1290 }
1212 1291
1213 err = dbg_check_lprops(c); 1292 err = dbg_check_lprops(c);
1214 if (err) 1293 if (err)
@@ -1217,6 +1296,16 @@ static int mount_ubifs(struct ubifs_info *c)
1217 err = ubifs_recover_size(c); 1296 err = ubifs_recover_size(c);
1218 if (err) 1297 if (err)
1219 goto out_orphans; 1298 goto out_orphans;
1299 } else {
1300 /*
1301 * Even if we mount read-only, we have to set space in GC LEB
1302 * to proper value because this affects UBIFS free space
1303 * reporting. We do not want to have a situation when
1304 * re-mounting from R/O to R/W changes amount of free space.
1305 */
1306 err = take_gc_lnum(c);
1307 if (err)
1308 goto out_orphans;
1220 } 1309 }
1221 1310
1222 spin_lock(&ubifs_infos_lock); 1311 spin_lock(&ubifs_infos_lock);
@@ -1229,13 +1318,20 @@ static int mount_ubifs(struct ubifs_info *c)
1229 else { 1318 else {
1230 c->need_recovery = 0; 1319 c->need_recovery = 0;
1231 ubifs_msg("recovery completed"); 1320 ubifs_msg("recovery completed");
1321 /* GC LEB has to be empty and taken at this point */
1322 ubifs_assert(c->lst.taken_empty_lebs == 1);
1232 } 1323 }
1233 } 1324 } else
1325 ubifs_assert(c->lst.taken_empty_lebs == 1);
1234 1326
1235 err = dbg_check_filesystem(c); 1327 err = dbg_check_filesystem(c);
1236 if (err) 1328 if (err)
1237 goto out_infos; 1329 goto out_infos;
1238 1330
1331 err = dbg_debugfs_init_fs(c);
1332 if (err)
1333 goto out_infos;
1334
1239 c->always_chk_crc = 0; 1335 c->always_chk_crc = 0;
1240 1336
1241 ubifs_msg("mounted UBI device %d, volume %d, name \"%s\"", 1337 ubifs_msg("mounted UBI device %d, volume %d, name \"%s\"",
@@ -1266,7 +1362,6 @@ static int mount_ubifs(struct ubifs_info *c)
1266 c->uuid[4], c->uuid[5], c->uuid[6], c->uuid[7], 1362 c->uuid[4], c->uuid[5], c->uuid[6], c->uuid[7],
1267 c->uuid[8], c->uuid[9], c->uuid[10], c->uuid[11], 1363 c->uuid[8], c->uuid[9], c->uuid[10], c->uuid[11],
1268 c->uuid[12], c->uuid[13], c->uuid[14], c->uuid[15]); 1364 c->uuid[12], c->uuid[13], c->uuid[14], c->uuid[15]);
1269 dbg_msg("fast unmount: %d", c->fast_unmount);
1270 dbg_msg("big_lpt %d", c->big_lpt); 1365 dbg_msg("big_lpt %d", c->big_lpt);
1271 dbg_msg("log LEBs: %d (%d - %d)", 1366 dbg_msg("log LEBs: %d (%d - %d)",
1272 c->log_lebs, UBIFS_LOG_LNUM, c->log_last); 1367 c->log_lebs, UBIFS_LOG_LNUM, c->log_last);
@@ -1283,8 +1378,20 @@ static int mount_ubifs(struct ubifs_info *c)
1283 dbg_msg("tree fanout: %d", c->fanout); 1378 dbg_msg("tree fanout: %d", c->fanout);
1284 dbg_msg("reserved GC LEB: %d", c->gc_lnum); 1379 dbg_msg("reserved GC LEB: %d", c->gc_lnum);
1285 dbg_msg("first main LEB: %d", c->main_first); 1380 dbg_msg("first main LEB: %d", c->main_first);
1381 dbg_msg("max. znode size %d", c->max_znode_sz);
1382 dbg_msg("max. index node size %d", c->max_idx_node_sz);
1383 dbg_msg("node sizes: data %zu, inode %zu, dentry %zu",
1384 UBIFS_DATA_NODE_SZ, UBIFS_INO_NODE_SZ, UBIFS_DENT_NODE_SZ);
1385 dbg_msg("node sizes: trun %zu, sb %zu, master %zu",
1386 UBIFS_TRUN_NODE_SZ, UBIFS_SB_NODE_SZ, UBIFS_MST_NODE_SZ);
1387 dbg_msg("node sizes: ref %zu, cmt. start %zu, orph %zu",
1388 UBIFS_REF_NODE_SZ, UBIFS_CS_NODE_SZ, UBIFS_ORPH_NODE_SZ);
1389 dbg_msg("max. node sizes: data %zu, inode %zu dentry %zu",
1390 UBIFS_MAX_DATA_NODE_SZ, UBIFS_MAX_INO_NODE_SZ,
1391 UBIFS_MAX_DENT_NODE_SZ);
1286 dbg_msg("dead watermark: %d", c->dead_wm); 1392 dbg_msg("dead watermark: %d", c->dead_wm);
1287 dbg_msg("dark watermark: %d", c->dark_wm); 1393 dbg_msg("dark watermark: %d", c->dark_wm);
1394 dbg_msg("LEB overhead: %d", c->leb_overhead);
1288 x = (long long)c->main_lebs * c->dark_wm; 1395 x = (long long)c->main_lebs * c->dark_wm;
1289 dbg_msg("max. dark space: %lld (%lld KiB, %lld MiB)", 1396 dbg_msg("max. dark space: %lld (%lld KiB, %lld MiB)",
1290 x, x >> 10, x >> 20); 1397 x, x >> 10, x >> 20);
@@ -1320,14 +1427,12 @@ out_wbufs:
1320 free_wbufs(c); 1427 free_wbufs(c);
1321out_cbuf: 1428out_cbuf:
1322 kfree(c->cbuf); 1429 kfree(c->cbuf);
1323out_dereg:
1324 dbg_failure_mode_deregistration(c);
1325out_free: 1430out_free:
1326 kfree(c->bu.buf); 1431 kfree(c->bu.buf);
1327 vfree(c->ileb_buf); 1432 vfree(c->ileb_buf);
1328 vfree(c->sbuf); 1433 vfree(c->sbuf);
1329 kfree(c->bottom_up_buf); 1434 kfree(c->bottom_up_buf);
1330 UBIFS_DBG(vfree(c->dbg_buf)); 1435 ubifs_debugging_exit(c);
1331 return err; 1436 return err;
1332} 1437}
1333 1438
@@ -1345,6 +1450,7 @@ static void ubifs_umount(struct ubifs_info *c)
1345 dbg_gen("un-mounting UBI device %d, volume %d", c->vi.ubi_num, 1450 dbg_gen("un-mounting UBI device %d, volume %d", c->vi.ubi_num,
1346 c->vi.vol_id); 1451 c->vi.vol_id);
1347 1452
1453 dbg_debugfs_exit_fs(c);
1348 spin_lock(&ubifs_infos_lock); 1454 spin_lock(&ubifs_infos_lock);
1349 list_del(&c->infos_list); 1455 list_del(&c->infos_list);
1350 spin_unlock(&ubifs_infos_lock); 1456 spin_unlock(&ubifs_infos_lock);
@@ -1364,8 +1470,7 @@ static void ubifs_umount(struct ubifs_info *c)
1364 vfree(c->ileb_buf); 1470 vfree(c->ileb_buf);
1365 vfree(c->sbuf); 1471 vfree(c->sbuf);
1366 kfree(c->bottom_up_buf); 1472 kfree(c->bottom_up_buf);
1367 UBIFS_DBG(vfree(c->dbg_buf)); 1473 ubifs_debugging_exit(c);
1368 dbg_failure_mode_deregistration(c);
1369} 1474}
1370 1475
1371/** 1476/**
@@ -1380,19 +1485,14 @@ static int ubifs_remount_rw(struct ubifs_info *c)
1380{ 1485{
1381 int err, lnum; 1486 int err, lnum;
1382 1487
1383 if (c->ro_media)
1384 return -EINVAL;
1385
1386 mutex_lock(&c->umount_mutex); 1488 mutex_lock(&c->umount_mutex);
1489 dbg_save_space_info(c);
1387 c->remounting_rw = 1; 1490 c->remounting_rw = 1;
1388 c->always_chk_crc = 1; 1491 c->always_chk_crc = 1;
1389 1492
1390 /* Check for enough free space */ 1493 err = check_free_space(c);
1391 if (ubifs_calc_available(c, c->min_idx_lebs) <= 0) { 1494 if (err)
1392 ubifs_err("insufficient available space");
1393 err = -EINVAL;
1394 goto out; 1495 goto out;
1395 }
1396 1496
1397 if (c->old_leb_cnt != c->leb_cnt) { 1497 if (c->old_leb_cnt != c->leb_cnt) {
1398 struct ubifs_sb_node *sup; 1498 struct ubifs_sb_node *sup;
@@ -1422,6 +1522,12 @@ static int ubifs_remount_rw(struct ubifs_info *c)
1422 err = ubifs_recover_inl_heads(c, c->sbuf); 1522 err = ubifs_recover_inl_heads(c, c->sbuf);
1423 if (err) 1523 if (err)
1424 goto out; 1524 goto out;
1525 } else {
1526 /* A readonly mount is not allowed to have orphans */
1527 ubifs_assert(c->tot_orphans == 0);
1528 err = ubifs_clear_orphans(c);
1529 if (err)
1530 goto out;
1425 } 1531 }
1426 1532
1427 if (!(c->mst_node->flags & cpu_to_le32(UBIFS_MST_DIRTY))) { 1533 if (!(c->mst_node->flags & cpu_to_le32(UBIFS_MST_DIRTY))) {
@@ -1477,7 +1583,7 @@ static int ubifs_remount_rw(struct ubifs_info *c)
1477 if (c->need_recovery) 1583 if (c->need_recovery)
1478 err = ubifs_rcvry_gc_commit(c); 1584 err = ubifs_rcvry_gc_commit(c);
1479 else 1585 else
1480 err = take_gc_lnum(c); 1586 err = ubifs_leb_unmap(c, c->gc_lnum);
1481 if (err) 1587 if (err)
1482 goto out; 1588 goto out;
1483 1589
@@ -1490,8 +1596,9 @@ static int ubifs_remount_rw(struct ubifs_info *c)
1490 c->vfs_sb->s_flags &= ~MS_RDONLY; 1596 c->vfs_sb->s_flags &= ~MS_RDONLY;
1491 c->remounting_rw = 0; 1597 c->remounting_rw = 0;
1492 c->always_chk_crc = 0; 1598 c->always_chk_crc = 0;
1599 err = dbg_check_space_info(c);
1493 mutex_unlock(&c->umount_mutex); 1600 mutex_unlock(&c->umount_mutex);
1494 return 0; 1601 return err;
1495 1602
1496out: 1603out:
1497 vfree(c->orph_buf); 1604 vfree(c->orph_buf);
@@ -1511,39 +1618,18 @@ out:
1511} 1618}
1512 1619
1513/** 1620/**
1514 * commit_on_unmount - commit the journal when un-mounting.
1515 * @c: UBIFS file-system description object
1516 *
1517 * This function is called during un-mounting and re-mounting, and it commits
1518 * the journal unless the "fast unmount" mode is enabled. It also avoids
1519 * committing the journal if it contains too few data.
1520 */
1521static void commit_on_unmount(struct ubifs_info *c)
1522{
1523 if (!c->fast_unmount) {
1524 long long bud_bytes;
1525
1526 spin_lock(&c->buds_lock);
1527 bud_bytes = c->bud_bytes;
1528 spin_unlock(&c->buds_lock);
1529 if (bud_bytes > c->leb_size)
1530 ubifs_run_commit(c);
1531 }
1532}
1533
1534/**
1535 * ubifs_remount_ro - re-mount in read-only mode. 1621 * ubifs_remount_ro - re-mount in read-only mode.
1536 * @c: UBIFS file-system description object 1622 * @c: UBIFS file-system description object
1537 * 1623 *
1538 * We rely on VFS to have stopped writing. Possibly the background thread could 1624 * We assume VFS has stopped writing. Possibly the background thread could be
1539 * be running a commit, however kthread_stop will wait in that case. 1625 * running a commit, however kthread_stop will wait in that case.
1540 */ 1626 */
1541static void ubifs_remount_ro(struct ubifs_info *c) 1627static void ubifs_remount_ro(struct ubifs_info *c)
1542{ 1628{
1543 int i, err; 1629 int i, err;
1544 1630
1545 ubifs_assert(!c->need_recovery); 1631 ubifs_assert(!c->need_recovery);
1546 commit_on_unmount(c); 1632 ubifs_assert(!(c->vfs_sb->s_flags & MS_RDONLY));
1547 1633
1548 mutex_lock(&c->umount_mutex); 1634 mutex_lock(&c->umount_mutex);
1549 if (c->bgt) { 1635 if (c->bgt) {
@@ -1551,27 +1637,29 @@ static void ubifs_remount_ro(struct ubifs_info *c)
1551 c->bgt = NULL; 1637 c->bgt = NULL;
1552 } 1638 }
1553 1639
1640 dbg_save_space_info(c);
1641
1554 for (i = 0; i < c->jhead_cnt; i++) { 1642 for (i = 0; i < c->jhead_cnt; i++) {
1555 ubifs_wbuf_sync(&c->jheads[i].wbuf); 1643 ubifs_wbuf_sync(&c->jheads[i].wbuf);
1556 del_timer_sync(&c->jheads[i].wbuf.timer); 1644 del_timer_sync(&c->jheads[i].wbuf.timer);
1557 } 1645 }
1558 1646
1559 if (!c->ro_media) { 1647 c->mst_node->flags &= ~cpu_to_le32(UBIFS_MST_DIRTY);
1560 c->mst_node->flags &= ~cpu_to_le32(UBIFS_MST_DIRTY); 1648 c->mst_node->flags |= cpu_to_le32(UBIFS_MST_NO_ORPHS);
1561 c->mst_node->flags |= cpu_to_le32(UBIFS_MST_NO_ORPHS); 1649 c->mst_node->gc_lnum = cpu_to_le32(c->gc_lnum);
1562 c->mst_node->gc_lnum = cpu_to_le32(c->gc_lnum); 1650 err = ubifs_write_master(c);
1563 err = ubifs_write_master(c); 1651 if (err)
1564 if (err) 1652 ubifs_ro_mode(c, err);
1565 ubifs_ro_mode(c, err);
1566 }
1567 1653
1568 ubifs_destroy_idx_gc(c);
1569 free_wbufs(c); 1654 free_wbufs(c);
1570 vfree(c->orph_buf); 1655 vfree(c->orph_buf);
1571 c->orph_buf = NULL; 1656 c->orph_buf = NULL;
1572 vfree(c->ileb_buf); 1657 vfree(c->ileb_buf);
1573 c->ileb_buf = NULL; 1658 c->ileb_buf = NULL;
1574 ubifs_lpt_free(c, 1); 1659 ubifs_lpt_free(c, 1);
1660 err = dbg_check_space_info(c);
1661 if (err)
1662 ubifs_ro_mode(c, err);
1575 mutex_unlock(&c->umount_mutex); 1663 mutex_unlock(&c->umount_mutex);
1576} 1664}
1577 1665
@@ -1664,11 +1752,20 @@ static int ubifs_remount_fs(struct super_block *sb, int *flags, char *data)
1664 } 1752 }
1665 1753
1666 if ((sb->s_flags & MS_RDONLY) && !(*flags & MS_RDONLY)) { 1754 if ((sb->s_flags & MS_RDONLY) && !(*flags & MS_RDONLY)) {
1755 if (c->ro_media) {
1756 ubifs_msg("cannot re-mount due to prior errors");
1757 return -EROFS;
1758 }
1667 err = ubifs_remount_rw(c); 1759 err = ubifs_remount_rw(c);
1668 if (err) 1760 if (err)
1669 return err; 1761 return err;
1670 } else if (!(sb->s_flags & MS_RDONLY) && (*flags & MS_RDONLY)) 1762 } else if (!(sb->s_flags & MS_RDONLY) && (*flags & MS_RDONLY)) {
1763 if (c->ro_media) {
1764 ubifs_msg("cannot re-mount due to prior errors");
1765 return -EROFS;
1766 }
1671 ubifs_remount_ro(c); 1767 ubifs_remount_ro(c);
1768 }
1672 1769
1673 if (c->bulk_read == 1) 1770 if (c->bulk_read == 1)
1674 bu_init(c); 1771 bu_init(c);
@@ -1678,10 +1775,11 @@ static int ubifs_remount_fs(struct super_block *sb, int *flags, char *data)
1678 c->bu.buf = NULL; 1775 c->bu.buf = NULL;
1679 } 1776 }
1680 1777
1778 ubifs_assert(c->lst.taken_empty_lebs == 1);
1681 return 0; 1779 return 0;
1682} 1780}
1683 1781
1684struct super_operations ubifs_super_operations = { 1782const struct super_operations ubifs_super_operations = {
1685 .alloc_inode = ubifs_alloc_inode, 1783 .alloc_inode = ubifs_alloc_inode,
1686 .destroy_inode = ubifs_destroy_inode, 1784 .destroy_inode = ubifs_destroy_inode,
1687 .put_super = ubifs_put_super, 1785 .put_super = ubifs_put_super,
@@ -1849,7 +1947,6 @@ static int ubifs_fill_super(struct super_block *sb, void *data, int silent)
1849 goto out_iput; 1947 goto out_iput;
1850 1948
1851 mutex_unlock(&c->umount_mutex); 1949 mutex_unlock(&c->umount_mutex);
1852
1853 return 0; 1950 return 0;
1854 1951
1855out_iput: 1952out_iput:
@@ -1949,15 +2046,6 @@ out_close:
1949 2046
1950static void ubifs_kill_sb(struct super_block *sb) 2047static void ubifs_kill_sb(struct super_block *sb)
1951{ 2048{
1952 struct ubifs_info *c = sb->s_fs_info;
1953
1954 /*
1955 * We do 'commit_on_unmount()' here instead of 'ubifs_put_super()'
1956 * in order to be outside BKL.
1957 */
1958 if (sb->s_root && !(sb->s_flags & MS_RDONLY))
1959 commit_on_unmount(c);
1960 /* The un-mount routine is actually done in put_super() */
1961 generic_shutdown_super(sb); 2049 generic_shutdown_super(sb);
1962} 2050}
1963 2051
@@ -2021,6 +2109,14 @@ static int __init ubifs_init(void)
2021 BUILD_BUG_ON(UBIFS_REF_NODE_SZ != 64); 2109 BUILD_BUG_ON(UBIFS_REF_NODE_SZ != 64);
2022 2110
2023 /* 2111 /*
2112 * We use 2 bit wide bit-fields to store compression type, which should
2113 * be amended if more compressors are added. The bit-fields are:
2114 * @compr_type in 'struct ubifs_inode', @default_compr in
2115 * 'struct ubifs_info' and @compr_type in 'struct ubifs_mount_opts'.
2116 */
2117 BUILD_BUG_ON(UBIFS_COMPR_TYPES_CNT > 4);
2118
2119 /*
2024 * We require that PAGE_CACHE_SIZE is greater-than-or-equal-to 2120 * We require that PAGE_CACHE_SIZE is greater-than-or-equal-to
2025 * UBIFS_BLOCK_SIZE. It is assumed that both are powers of 2. 2121 * UBIFS_BLOCK_SIZE. It is assumed that both are powers of 2.
2026 */ 2122 */
@@ -2049,11 +2145,17 @@ static int __init ubifs_init(void)
2049 2145
2050 err = ubifs_compressors_init(); 2146 err = ubifs_compressors_init();
2051 if (err) 2147 if (err)
2148 goto out_shrinker;
2149
2150 err = dbg_debugfs_init();
2151 if (err)
2052 goto out_compr; 2152 goto out_compr;
2053 2153
2054 return 0; 2154 return 0;
2055 2155
2056out_compr: 2156out_compr:
2157 ubifs_compressors_exit();
2158out_shrinker:
2057 unregister_shrinker(&ubifs_shrinker_info); 2159 unregister_shrinker(&ubifs_shrinker_info);
2058 kmem_cache_destroy(ubifs_inode_slab); 2160 kmem_cache_destroy(ubifs_inode_slab);
2059out_reg: 2161out_reg:
@@ -2068,6 +2170,7 @@ static void __exit ubifs_exit(void)
2068 ubifs_assert(list_empty(&ubifs_infos)); 2170 ubifs_assert(list_empty(&ubifs_infos));
2069 ubifs_assert(atomic_long_read(&ubifs_clean_zn_cnt) == 0); 2171 ubifs_assert(atomic_long_read(&ubifs_clean_zn_cnt) == 0);
2070 2172
2173 dbg_debugfs_exit();
2071 ubifs_compressors_exit(); 2174 ubifs_compressors_exit();
2072 unregister_shrinker(&ubifs_shrinker_info); 2175 unregister_shrinker(&ubifs_shrinker_info);
2073 kmem_cache_destroy(ubifs_inode_slab); 2176 kmem_cache_destroy(ubifs_inode_slab);
diff --git a/fs/ubifs/tnc.c b/fs/ubifs/tnc.c
index 6eef5344a145..fa28a84c6a1b 100644
--- a/fs/ubifs/tnc.c
+++ b/fs/ubifs/tnc.c
@@ -443,6 +443,11 @@ static int tnc_read_node_nm(struct ubifs_info *c, struct ubifs_zbranch *zbr,
443 * This function performs that same function as ubifs_read_node except that 443 * This function performs that same function as ubifs_read_node except that
444 * it does not require that there is actually a node present and instead 444 * it does not require that there is actually a node present and instead
445 * the return code indicates if a node was read. 445 * the return code indicates if a node was read.
446 *
447 * Note, this function does not check CRC of data nodes if @c->no_chk_data_crc
448 * is true (it is controlled by corresponding mount option). However, if
449 * @c->always_chk_crc is true, @c->no_chk_data_crc is ignored and CRC is always
450 * checked.
446 */ 451 */
447static int try_read_node(const struct ubifs_info *c, void *buf, int type, 452static int try_read_node(const struct ubifs_info *c, void *buf, int type,
448 int len, int lnum, int offs) 453 int len, int lnum, int offs)
@@ -470,9 +475,8 @@ static int try_read_node(const struct ubifs_info *c, void *buf, int type,
470 if (node_len != len) 475 if (node_len != len)
471 return 0; 476 return 0;
472 477
473 if (type == UBIFS_DATA_NODE && !c->always_chk_crc) 478 if (type == UBIFS_DATA_NODE && !c->always_chk_crc && c->no_chk_data_crc)
474 if (c->no_chk_data_crc) 479 return 1;
475 return 0;
476 480
477 crc = crc32(UBIFS_CRC32_INIT, buf + 8, node_len - 8); 481 crc = crc32(UBIFS_CRC32_INIT, buf + 8, node_len - 8);
478 node_crc = le32_to_cpu(ch->crc); 482 node_crc = le32_to_cpu(ch->crc);
@@ -1506,7 +1510,7 @@ out:
1506 * 1510 *
1507 * Note, if the bulk-read buffer length (@bu->buf_len) is known, this function 1511 * Note, if the bulk-read buffer length (@bu->buf_len) is known, this function
1508 * makes sure bulk-read nodes fit the buffer. Otherwise, this function prepares 1512 * makes sure bulk-read nodes fit the buffer. Otherwise, this function prepares
1509 * maxumum possible amount of nodes for bulk-read. 1513 * maximum possible amount of nodes for bulk-read.
1510 */ 1514 */
1511int ubifs_tnc_get_bu_keys(struct ubifs_info *c, struct bu_info *bu) 1515int ubifs_tnc_get_bu_keys(struct ubifs_info *c, struct bu_info *bu)
1512{ 1516{
@@ -2245,12 +2249,11 @@ int ubifs_tnc_replace(struct ubifs_info *c, const union ubifs_key *key,
2245 if (found) { 2249 if (found) {
2246 /* Ensure the znode is dirtied */ 2250 /* Ensure the znode is dirtied */
2247 if (znode->cnext || !ubifs_zn_dirty(znode)) { 2251 if (znode->cnext || !ubifs_zn_dirty(znode)) {
2248 znode = dirty_cow_bottom_up(c, 2252 znode = dirty_cow_bottom_up(c, znode);
2249 znode); 2253 if (IS_ERR(znode)) {
2250 if (IS_ERR(znode)) { 2254 err = PTR_ERR(znode);
2251 err = PTR_ERR(znode); 2255 goto out_unlock;
2252 goto out_unlock; 2256 }
2253 }
2254 } 2257 }
2255 zbr = &znode->zbranch[n]; 2258 zbr = &znode->zbranch[n];
2256 lnc_free(zbr); 2259 lnc_free(zbr);
@@ -2317,11 +2320,11 @@ int ubifs_tnc_add_nm(struct ubifs_info *c, const union ubifs_key *key,
2317 2320
2318 /* Ensure the znode is dirtied */ 2321 /* Ensure the znode is dirtied */
2319 if (znode->cnext || !ubifs_zn_dirty(znode)) { 2322 if (znode->cnext || !ubifs_zn_dirty(znode)) {
2320 znode = dirty_cow_bottom_up(c, znode); 2323 znode = dirty_cow_bottom_up(c, znode);
2321 if (IS_ERR(znode)) { 2324 if (IS_ERR(znode)) {
2322 err = PTR_ERR(znode); 2325 err = PTR_ERR(znode);
2323 goto out_unlock; 2326 goto out_unlock;
2324 } 2327 }
2325 } 2328 }
2326 2329
2327 if (found == 1) { 2330 if (found == 1) {
@@ -2627,11 +2630,11 @@ int ubifs_tnc_remove_range(struct ubifs_info *c, union ubifs_key *from_key,
2627 2630
2628 /* Ensure the znode is dirtied */ 2631 /* Ensure the znode is dirtied */
2629 if (znode->cnext || !ubifs_zn_dirty(znode)) { 2632 if (znode->cnext || !ubifs_zn_dirty(znode)) {
2630 znode = dirty_cow_bottom_up(c, znode); 2633 znode = dirty_cow_bottom_up(c, znode);
2631 if (IS_ERR(znode)) { 2634 if (IS_ERR(znode)) {
2632 err = PTR_ERR(znode); 2635 err = PTR_ERR(znode);
2633 goto out_unlock; 2636 goto out_unlock;
2634 } 2637 }
2635 } 2638 }
2636 2639
2637 /* Remove all keys in range except the first */ 2640 /* Remove all keys in range except the first */
diff --git a/fs/ubifs/tnc_commit.c b/fs/ubifs/tnc_commit.c
index 8ac76b1c2d55..fde8d127c768 100644
--- a/fs/ubifs/tnc_commit.c
+++ b/fs/ubifs/tnc_commit.c
@@ -553,8 +553,8 @@ static int layout_in_empty_space(struct ubifs_info *c)
553 } 553 }
554 554
555#ifdef CONFIG_UBIFS_FS_DEBUG 555#ifdef CONFIG_UBIFS_FS_DEBUG
556 c->new_ihead_lnum = lnum; 556 c->dbg->new_ihead_lnum = lnum;
557 c->new_ihead_offs = buf_offs; 557 c->dbg->new_ihead_offs = buf_offs;
558#endif 558#endif
559 559
560 return 0; 560 return 0;
@@ -802,8 +802,10 @@ int ubifs_tnc_start_commit(struct ubifs_info *c, struct ubifs_zbranch *zroot)
802 * budgeting subsystem to assume the index is already committed, 802 * budgeting subsystem to assume the index is already committed,
803 * even though it is not. 803 * even though it is not.
804 */ 804 */
805 ubifs_assert(c->min_idx_lebs == ubifs_calc_min_idx_lebs(c));
805 c->old_idx_sz = c->calc_idx_sz; 806 c->old_idx_sz = c->calc_idx_sz;
806 c->budg_uncommitted_idx = 0; 807 c->budg_uncommitted_idx = 0;
808 c->min_idx_lebs = ubifs_calc_min_idx_lebs(c);
807 spin_unlock(&c->space_lock); 809 spin_unlock(&c->space_lock);
808 mutex_unlock(&c->tnc_mutex); 810 mutex_unlock(&c->tnc_mutex);
809 811
@@ -1002,7 +1004,8 @@ static int write_index(struct ubifs_info *c)
1002 } 1004 }
1003 1005
1004#ifdef CONFIG_UBIFS_FS_DEBUG 1006#ifdef CONFIG_UBIFS_FS_DEBUG
1005 if (lnum != c->new_ihead_lnum || buf_offs != c->new_ihead_offs) { 1007 if (lnum != c->dbg->new_ihead_lnum ||
1008 buf_offs != c->dbg->new_ihead_offs) {
1006 ubifs_err("inconsistent ihead"); 1009 ubifs_err("inconsistent ihead");
1007 return -EINVAL; 1010 return -EINVAL;
1008 } 1011 }
diff --git a/fs/ubifs/ubifs-media.h b/fs/ubifs/ubifs-media.h
index 0b378042a3a2..b25fc36cf72f 100644
--- a/fs/ubifs/ubifs-media.h
+++ b/fs/ubifs/ubifs-media.h
@@ -51,6 +51,13 @@
51 */ 51 */
52#define UBIFS_MIN_COMPR_LEN 128 52#define UBIFS_MIN_COMPR_LEN 128
53 53
54/*
55 * If compressed data length is less than %UBIFS_MIN_COMPRESS_DIFF bytes
56 * shorter than uncompressed data length, UBIFS preferes to leave this data
57 * node uncompress, because it'll be read faster.
58 */
59#define UBIFS_MIN_COMPRESS_DIFF 64
60
54/* Root inode number */ 61/* Root inode number */
55#define UBIFS_ROOT_INO 1 62#define UBIFS_ROOT_INO 1
56 63
diff --git a/fs/ubifs/ubifs.h b/fs/ubifs/ubifs.h
index 46b172560a06..039a68bee29a 100644
--- a/fs/ubifs/ubifs.h
+++ b/fs/ubifs/ubifs.h
@@ -63,6 +63,14 @@
63#define SQNUM_WARN_WATERMARK 0xFFFFFFFF00000000ULL 63#define SQNUM_WARN_WATERMARK 0xFFFFFFFF00000000ULL
64#define SQNUM_WATERMARK 0xFFFFFFFFFF000000ULL 64#define SQNUM_WATERMARK 0xFFFFFFFFFF000000ULL
65 65
66/*
67 * Minimum amount of LEBs reserved for the index. At present the index needs at
68 * least 2 LEBs: one for the index head and one for in-the-gaps method (which
69 * currently does not cater for the index head and so excludes it from
70 * consideration).
71 */
72#define MIN_INDEX_LEBS 2
73
66/* Minimum amount of data UBIFS writes to the flash */ 74/* Minimum amount of data UBIFS writes to the flash */
67#define MIN_WRITE_SZ (UBIFS_DATA_NODE_SZ + 8) 75#define MIN_WRITE_SZ (UBIFS_DATA_NODE_SZ + 8)
68 76
@@ -386,12 +394,12 @@ struct ubifs_inode {
386 unsigned int dirty:1; 394 unsigned int dirty:1;
387 unsigned int xattr:1; 395 unsigned int xattr:1;
388 unsigned int bulk_read:1; 396 unsigned int bulk_read:1;
397 unsigned int compr_type:2;
389 struct mutex ui_mutex; 398 struct mutex ui_mutex;
390 spinlock_t ui_lock; 399 spinlock_t ui_lock;
391 loff_t synced_i_size; 400 loff_t synced_i_size;
392 loff_t ui_size; 401 loff_t ui_size;
393 int flags; 402 int flags;
394 int compr_type;
395 pgoff_t last_page_read; 403 pgoff_t last_page_read;
396 pgoff_t read_in_a_row; 404 pgoff_t read_in_a_row;
397 int data_len; 405 int data_len;
@@ -418,9 +426,9 @@ struct ubifs_unclean_leb {
418 * LEB properties flags. 426 * LEB properties flags.
419 * 427 *
420 * LPROPS_UNCAT: not categorized 428 * LPROPS_UNCAT: not categorized
421 * LPROPS_DIRTY: dirty > 0, not index 429 * LPROPS_DIRTY: dirty > free, dirty >= @c->dead_wm, not index
422 * LPROPS_DIRTY_IDX: dirty + free > UBIFS_CH_SZ and index 430 * LPROPS_DIRTY_IDX: dirty + free > @c->min_idx_node_sze and index
423 * LPROPS_FREE: free > 0, not empty, not index 431 * LPROPS_FREE: free > 0, dirty < @c->dead_wm, not empty, not index
424 * LPROPS_HEAP_CNT: number of heaps used for storing categorized LEBs 432 * LPROPS_HEAP_CNT: number of heaps used for storing categorized LEBs
425 * LPROPS_EMPTY: LEB is empty, not taken 433 * LPROPS_EMPTY: LEB is empty, not taken
426 * LPROPS_FREEABLE: free + dirty == leb_size, not index, not taken 434 * LPROPS_FREEABLE: free + dirty == leb_size, not index, not taken
@@ -473,8 +481,8 @@ struct ubifs_lprops {
473struct ubifs_lpt_lprops { 481struct ubifs_lpt_lprops {
474 int free; 482 int free;
475 int dirty; 483 int dirty;
476 unsigned tgc : 1; 484 unsigned tgc:1;
477 unsigned cmt : 1; 485 unsigned cmt:1;
478}; 486};
479 487
480/** 488/**
@@ -482,24 +490,26 @@ struct ubifs_lpt_lprops {
482 * @empty_lebs: number of empty LEBs 490 * @empty_lebs: number of empty LEBs
483 * @taken_empty_lebs: number of taken LEBs 491 * @taken_empty_lebs: number of taken LEBs
484 * @idx_lebs: number of indexing LEBs 492 * @idx_lebs: number of indexing LEBs
485 * @total_free: total free space in bytes 493 * @total_free: total free space in bytes (includes all LEBs)
486 * @total_dirty: total dirty space in bytes 494 * @total_dirty: total dirty space in bytes (includes all LEBs)
487 * @total_used: total used space in bytes (includes only data LEBs) 495 * @total_used: total used space in bytes (does not include index LEBs)
488 * @total_dead: total dead space in bytes (includes only data LEBs) 496 * @total_dead: total dead space in bytes (does not include index LEBs)
489 * @total_dark: total dark space in bytes (includes only data LEBs) 497 * @total_dark: total dark space in bytes (does not include index LEBs)
498 *
499 * The @taken_empty_lebs field counts the LEBs that are in the transient state
500 * of having been "taken" for use but not yet written to. @taken_empty_lebs is
501 * needed to account correctly for @gc_lnum, otherwise @empty_lebs could be
502 * used by itself (in which case 'unused_lebs' would be a better name). In the
503 * case of @gc_lnum, it is "taken" at mount time or whenever a LEB is retained
504 * by GC, but unlike other empty LEBs that are "taken", it may not be written
505 * straight away (i.e. before the next commit start or unmount), so either
506 * @gc_lnum must be specially accounted for, or the current approach followed
507 * i.e. count it under @taken_empty_lebs.
490 * 508 *
491 * N.B. total_dirty and total_used are different to other total_* fields, 509 * @empty_lebs includes @taken_empty_lebs.
492 * because they account _all_ LEBs, not just data LEBs.
493 * 510 *
494 * 'taken_empty_lebs' counts the LEBs that are in the transient state of having 511 * @total_used, @total_dead and @total_dark fields do not account indexing
495 * been 'taken' for use but not yet written to. 'taken_empty_lebs' is needed 512 * LEBs.
496 * to account correctly for gc_lnum, otherwise 'empty_lebs' could be used
497 * by itself (in which case 'unused_lebs' would be a better name). In the case
498 * of gc_lnum, it is 'taken' at mount time or whenever a LEB is retained by GC,
499 * but unlike other empty LEBs that are 'taken', it may not be written straight
500 * away (i.e. before the next commit start or unmount), so either gc_lnum must
501 * be specially accounted for, or the current approach followed i.e. count it
502 * under 'taken_empty_lebs'.
503 */ 513 */
504struct ubifs_lp_stats { 514struct ubifs_lp_stats {
505 int empty_lebs; 515 int empty_lebs;
@@ -893,15 +903,25 @@ struct ubifs_orphan {
893/** 903/**
894 * struct ubifs_mount_opts - UBIFS-specific mount options information. 904 * struct ubifs_mount_opts - UBIFS-specific mount options information.
895 * @unmount_mode: selected unmount mode (%0 default, %1 normal, %2 fast) 905 * @unmount_mode: selected unmount mode (%0 default, %1 normal, %2 fast)
896 * @bulk_read: enable bulk-reads 906 * @bulk_read: enable/disable bulk-reads (%0 default, %1 disabe, %2 enable)
897 * @chk_data_crc: check CRCs when reading data nodes 907 * @chk_data_crc: enable/disable CRC data checking when reading data nodes
908 * (%0 default, %1 disabe, %2 enable)
909 * @override_compr: override default compressor (%0 - do not override and use
910 * superblock compressor, %1 - override and use compressor
911 * specified in @compr_type)
912 * @compr_type: compressor type to override the superblock compressor with
913 * (%UBIFS_COMPR_NONE, etc)
898 */ 914 */
899struct ubifs_mount_opts { 915struct ubifs_mount_opts {
900 unsigned int unmount_mode:2; 916 unsigned int unmount_mode:2;
901 unsigned int bulk_read:2; 917 unsigned int bulk_read:2;
902 unsigned int chk_data_crc:2; 918 unsigned int chk_data_crc:2;
919 unsigned int override_compr:1;
920 unsigned int compr_type:2;
903}; 921};
904 922
923struct ubifs_debug_info;
924
905/** 925/**
906 * struct ubifs_info - UBIFS file-system description data structure 926 * struct ubifs_info - UBIFS file-system description data structure
907 * (per-superblock). 927 * (per-superblock).
@@ -941,11 +961,11 @@ struct ubifs_mount_opts {
941 * @cs_lock: commit state lock 961 * @cs_lock: commit state lock
942 * @cmt_wq: wait queue to sleep on if the log is full and a commit is running 962 * @cmt_wq: wait queue to sleep on if the log is full and a commit is running
943 * 963 *
944 * @fast_unmount: do not run journal commit before un-mounting
945 * @big_lpt: flag that LPT is too big to write whole during commit 964 * @big_lpt: flag that LPT is too big to write whole during commit
946 * @no_chk_data_crc: do not check CRCs when reading data nodes (except during 965 * @no_chk_data_crc: do not check CRCs when reading data nodes (except during
947 * recovery) 966 * recovery)
948 * @bulk_read: enable bulk-reads 967 * @bulk_read: enable bulk-reads
968 * @default_compr: default compression algorithm (%UBIFS_COMPR_LZO, etc)
949 * 969 *
950 * @tnc_mutex: protects the Tree Node Cache (TNC), @zroot, @cnext, @enext, and 970 * @tnc_mutex: protects the Tree Node Cache (TNC), @zroot, @cnext, @enext, and
951 * @calc_idx_sz 971 * @calc_idx_sz
@@ -963,8 +983,6 @@ struct ubifs_mount_opts {
963 * @ileb_nxt: next pre-allocated index LEBs 983 * @ileb_nxt: next pre-allocated index LEBs
964 * @old_idx: tree of index nodes obsoleted since the last commit start 984 * @old_idx: tree of index nodes obsoleted since the last commit start
965 * @bottom_up_buf: a buffer which is used by 'dirty_cow_bottom_up()' in tnc.c 985 * @bottom_up_buf: a buffer which is used by 'dirty_cow_bottom_up()' in tnc.c
966 * @new_ihead_lnum: used by debugging to check ihead_lnum
967 * @new_ihead_offs: used by debugging to check ihead_offs
968 * 986 *
969 * @mst_node: master node 987 * @mst_node: master node
970 * @mst_offs: offset of valid master node 988 * @mst_offs: offset of valid master node
@@ -986,7 +1004,6 @@ struct ubifs_mount_opts {
986 * @main_lebs: count of LEBs in the main area 1004 * @main_lebs: count of LEBs in the main area
987 * @main_first: first LEB of the main area 1005 * @main_first: first LEB of the main area
988 * @main_bytes: main area size in bytes 1006 * @main_bytes: main area size in bytes
989 * @default_compr: default compression algorithm (%UBIFS_COMPR_LZO, etc)
990 * 1007 *
991 * @key_hash_type: type of the key hash 1008 * @key_hash_type: type of the key hash
992 * @key_hash: direntry key hash function 1009 * @key_hash: direntry key hash function
@@ -1149,15 +1166,7 @@ struct ubifs_mount_opts {
1149 * @always_chk_crc: always check CRCs (while mounting and remounting rw) 1166 * @always_chk_crc: always check CRCs (while mounting and remounting rw)
1150 * @mount_opts: UBIFS-specific mount options 1167 * @mount_opts: UBIFS-specific mount options
1151 * 1168 *
1152 * @dbg_buf: a buffer of LEB size used for debugging purposes 1169 * @dbg: debugging-related information
1153 * @old_zroot: old index root - used by 'dbg_check_old_index()'
1154 * @old_zroot_level: old index root level - used by 'dbg_check_old_index()'
1155 * @old_zroot_sqnum: old index root sqnum - used by 'dbg_check_old_index()'
1156 * @failure_mode: failure mode for recovery testing
1157 * @fail_delay: 0=>don't delay, 1=>delay a time, 2=>delay a number of calls
1158 * @fail_timeout: time in jiffies when delay of failure mode expires
1159 * @fail_cnt: current number of calls to failure mode I/O functions
1160 * @fail_cnt_max: number of calls by which to delay failure mode
1161 */ 1170 */
1162struct ubifs_info { 1171struct ubifs_info {
1163 struct super_block *vfs_sb; 1172 struct super_block *vfs_sb;
@@ -1192,10 +1201,10 @@ struct ubifs_info {
1192 spinlock_t cs_lock; 1201 spinlock_t cs_lock;
1193 wait_queue_head_t cmt_wq; 1202 wait_queue_head_t cmt_wq;
1194 1203
1195 unsigned int fast_unmount:1;
1196 unsigned int big_lpt:1; 1204 unsigned int big_lpt:1;
1197 unsigned int no_chk_data_crc:1; 1205 unsigned int no_chk_data_crc:1;
1198 unsigned int bulk_read:1; 1206 unsigned int bulk_read:1;
1207 unsigned int default_compr:2;
1199 1208
1200 struct mutex tnc_mutex; 1209 struct mutex tnc_mutex;
1201 struct ubifs_zbranch zroot; 1210 struct ubifs_zbranch zroot;
@@ -1212,10 +1221,6 @@ struct ubifs_info {
1212 int ileb_nxt; 1221 int ileb_nxt;
1213 struct rb_root old_idx; 1222 struct rb_root old_idx;
1214 int *bottom_up_buf; 1223 int *bottom_up_buf;
1215#ifdef CONFIG_UBIFS_FS_DEBUG
1216 int new_ihead_lnum;
1217 int new_ihead_offs;
1218#endif
1219 1224
1220 struct ubifs_mst_node *mst_node; 1225 struct ubifs_mst_node *mst_node;
1221 int mst_offs; 1226 int mst_offs;
@@ -1237,7 +1242,6 @@ struct ubifs_info {
1237 int main_lebs; 1242 int main_lebs;
1238 int main_first; 1243 int main_first;
1239 long long main_bytes; 1244 long long main_bytes;
1240 int default_compr;
1241 1245
1242 uint8_t key_hash_type; 1246 uint8_t key_hash_type;
1243 uint32_t (*key_hash)(const char *str, int len); 1247 uint32_t (*key_hash)(const char *str, int len);
@@ -1315,8 +1319,8 @@ struct ubifs_info {
1315 void *sbuf; 1319 void *sbuf;
1316 struct list_head idx_gc; 1320 struct list_head idx_gc;
1317 int idx_gc_cnt; 1321 int idx_gc_cnt;
1318 volatile int gc_seq; 1322 int gc_seq;
1319 volatile int gced_lnum; 1323 int gced_lnum;
1320 1324
1321 struct list_head infos_list; 1325 struct list_head infos_list;
1322 struct mutex umount_mutex; 1326 struct mutex umount_mutex;
@@ -1391,21 +1395,7 @@ struct ubifs_info {
1391 struct ubifs_mount_opts mount_opts; 1395 struct ubifs_mount_opts mount_opts;
1392 1396
1393#ifdef CONFIG_UBIFS_FS_DEBUG 1397#ifdef CONFIG_UBIFS_FS_DEBUG
1394 void *dbg_buf; 1398 struct ubifs_debug_info *dbg;
1395 struct ubifs_zbranch old_zroot;
1396 int old_zroot_level;
1397 unsigned long long old_zroot_sqnum;
1398 int failure_mode;
1399 int fail_delay;
1400 unsigned long fail_timeout;
1401 unsigned int fail_cnt;
1402 unsigned int fail_cnt_max;
1403 long long chk_lpt_sz;
1404 long long chk_lpt_sz2;
1405 long long chk_lpt_wastage;
1406 int chk_lpt_lebs;
1407 int new_nhead_lnum;
1408 int new_nhead_offs;
1409#endif 1399#endif
1410}; 1400};
1411 1401
@@ -1413,13 +1403,13 @@ extern struct list_head ubifs_infos;
1413extern spinlock_t ubifs_infos_lock; 1403extern spinlock_t ubifs_infos_lock;
1414extern atomic_long_t ubifs_clean_zn_cnt; 1404extern atomic_long_t ubifs_clean_zn_cnt;
1415extern struct kmem_cache *ubifs_inode_slab; 1405extern struct kmem_cache *ubifs_inode_slab;
1416extern struct super_operations ubifs_super_operations; 1406extern const struct super_operations ubifs_super_operations;
1417extern struct address_space_operations ubifs_file_address_operations; 1407extern const struct address_space_operations ubifs_file_address_operations;
1418extern struct file_operations ubifs_file_operations; 1408extern const struct file_operations ubifs_file_operations;
1419extern struct inode_operations ubifs_file_inode_operations; 1409extern const struct inode_operations ubifs_file_inode_operations;
1420extern struct file_operations ubifs_dir_operations; 1410extern const struct file_operations ubifs_dir_operations;
1421extern struct inode_operations ubifs_dir_inode_operations; 1411extern const struct inode_operations ubifs_dir_inode_operations;
1422extern struct inode_operations ubifs_symlink_inode_operations; 1412extern const struct inode_operations ubifs_symlink_inode_operations;
1423extern struct backing_dev_info ubifs_backing_dev_info; 1413extern struct backing_dev_info ubifs_backing_dev_info;
1424extern struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT]; 1414extern struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
1425 1415
@@ -1436,7 +1426,7 @@ int ubifs_read_node_wbuf(struct ubifs_wbuf *wbuf, void *buf, int type, int len,
1436int ubifs_write_node(struct ubifs_info *c, void *node, int len, int lnum, 1426int ubifs_write_node(struct ubifs_info *c, void *node, int len, int lnum,
1437 int offs, int dtype); 1427 int offs, int dtype);
1438int ubifs_check_node(const struct ubifs_info *c, const void *buf, int lnum, 1428int ubifs_check_node(const struct ubifs_info *c, const void *buf, int lnum,
1439 int offs, int quiet, int chk_crc); 1429 int offs, int quiet, int must_chk_crc);
1440void ubifs_prepare_node(struct ubifs_info *c, void *buf, int len, int pad); 1430void ubifs_prepare_node(struct ubifs_info *c, void *buf, int len, int pad);
1441void ubifs_prep_grp_node(struct ubifs_info *c, void *node, int len, int last); 1431void ubifs_prep_grp_node(struct ubifs_info *c, void *node, int len, int last);
1442int ubifs_io_init(struct ubifs_info *c); 1432int ubifs_io_init(struct ubifs_info *c);
@@ -1503,9 +1493,10 @@ void ubifs_release_ino_dirty(struct ubifs_info *c, struct inode *inode,
1503void ubifs_cancel_ino_op(struct ubifs_info *c, struct inode *inode, 1493void ubifs_cancel_ino_op(struct ubifs_info *c, struct inode *inode,
1504 struct ubifs_budget_req *req); 1494 struct ubifs_budget_req *req);
1505long long ubifs_get_free_space(struct ubifs_info *c); 1495long long ubifs_get_free_space(struct ubifs_info *c);
1496long long ubifs_get_free_space_nolock(struct ubifs_info *c);
1506int ubifs_calc_min_idx_lebs(struct ubifs_info *c); 1497int ubifs_calc_min_idx_lebs(struct ubifs_info *c);
1507void ubifs_convert_page_budget(struct ubifs_info *c); 1498void ubifs_convert_page_budget(struct ubifs_info *c);
1508long long ubifs_reported_space(const struct ubifs_info *c, uint64_t free); 1499long long ubifs_reported_space(const struct ubifs_info *c, long long free);
1509long long ubifs_calc_available(const struct ubifs_info *c, int min_idx_lebs); 1500long long ubifs_calc_available(const struct ubifs_info *c, int min_idx_lebs);
1510 1501
1511/* find.c */ 1502/* find.c */
@@ -1611,6 +1602,7 @@ void ubifs_delete_orphan(struct ubifs_info *c, ino_t inum);
1611int ubifs_orphan_start_commit(struct ubifs_info *c); 1602int ubifs_orphan_start_commit(struct ubifs_info *c);
1612int ubifs_orphan_end_commit(struct ubifs_info *c); 1603int ubifs_orphan_end_commit(struct ubifs_info *c);
1613int ubifs_mount_orphans(struct ubifs_info *c, int unclean, int read_only); 1604int ubifs_mount_orphans(struct ubifs_info *c, int unclean, int read_only);
1605int ubifs_clear_orphans(struct ubifs_info *c);
1614 1606
1615/* lpt.c */ 1607/* lpt.c */
1616int ubifs_calc_lpt_geom(struct ubifs_info *c); 1608int ubifs_calc_lpt_geom(struct ubifs_info *c);
@@ -1639,6 +1631,9 @@ void ubifs_add_lpt_dirt(struct ubifs_info *c, int lnum, int dirty);
1639void ubifs_add_nnode_dirt(struct ubifs_info *c, struct ubifs_nnode *nnode); 1631void ubifs_add_nnode_dirt(struct ubifs_info *c, struct ubifs_nnode *nnode);
1640uint32_t ubifs_unpack_bits(uint8_t **addr, int *pos, int nrbits); 1632uint32_t ubifs_unpack_bits(uint8_t **addr, int *pos, int nrbits);
1641struct ubifs_nnode *ubifs_first_nnode(struct ubifs_info *c, int *hght); 1633struct ubifs_nnode *ubifs_first_nnode(struct ubifs_info *c, int *hght);
1634/* Needed only in debugging code in lpt_commit.c */
1635int ubifs_unpack_nnode(const struct ubifs_info *c, void *buf,
1636 struct ubifs_nnode *nnode);
1642 1637
1643/* lpt_commit.c */ 1638/* lpt_commit.c */
1644int ubifs_lpt_start_commit(struct ubifs_info *c); 1639int ubifs_lpt_start_commit(struct ubifs_info *c);
@@ -1651,7 +1646,7 @@ const struct ubifs_lprops *ubifs_change_lp(struct ubifs_info *c,
1651 const struct ubifs_lprops *lp, 1646 const struct ubifs_lprops *lp,
1652 int free, int dirty, int flags, 1647 int free, int dirty, int flags,
1653 int idx_gc_cnt); 1648 int idx_gc_cnt);
1654void ubifs_get_lp_stats(struct ubifs_info *c, struct ubifs_lp_stats *stats); 1649void ubifs_get_lp_stats(struct ubifs_info *c, struct ubifs_lp_stats *lst);
1655void ubifs_add_to_cat(struct ubifs_info *c, struct ubifs_lprops *lprops, 1650void ubifs_add_to_cat(struct ubifs_info *c, struct ubifs_lprops *lprops,
1656 int cat); 1651 int cat);
1657void ubifs_replace_cat(struct ubifs_info *c, struct ubifs_lprops *old_lprops, 1652void ubifs_replace_cat(struct ubifs_info *c, struct ubifs_lprops *old_lprops,
@@ -1714,7 +1709,7 @@ long ubifs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
1714 1709
1715/* compressor.c */ 1710/* compressor.c */
1716int __init ubifs_compressors_init(void); 1711int __init ubifs_compressors_init(void);
1717void __exit ubifs_compressors_exit(void); 1712void ubifs_compressors_exit(void);
1718void ubifs_compress(const void *in_buf, int in_len, void *out_buf, int *out_len, 1713void ubifs_compress(const void *in_buf, int in_len, void *out_buf, int *out_len,
1719 int *compr_type); 1714 int *compr_type);
1720int ubifs_decompress(const void *buf, int len, void *out, int *out_len, 1715int ubifs_decompress(const void *buf, int len, void *out, int *out_len,