aboutsummaryrefslogtreecommitdiffstats
path: root/fs/ubifs
diff options
context:
space:
mode:
Diffstat (limited to 'fs/ubifs')
-rw-r--r--fs/ubifs/budget.c173
-rw-r--r--fs/ubifs/commit.c3
-rw-r--r--fs/ubifs/compress.c2
-rw-r--r--fs/ubifs/debug.c108
-rw-r--r--fs/ubifs/debug.h149
-rw-r--r--fs/ubifs/dir.c27
-rw-r--r--fs/ubifs/file.c289
-rw-r--r--fs/ubifs/find.c32
-rw-r--r--fs/ubifs/gc.c110
-rw-r--r--fs/ubifs/io.c26
-rw-r--r--fs/ubifs/journal.c110
-rw-r--r--fs/ubifs/key.h22
-rw-r--r--fs/ubifs/log.c4
-rw-r--r--fs/ubifs/lprops.c34
-rw-r--r--fs/ubifs/lpt.c48
-rw-r--r--fs/ubifs/lpt_commit.c187
-rw-r--r--fs/ubifs/misc.h86
-rw-r--r--fs/ubifs/orphan.c4
-rw-r--r--fs/ubifs/scan.c2
-rw-r--r--fs/ubifs/super.c186
-rw-r--r--fs/ubifs/tnc.c445
-rw-r--r--fs/ubifs/tnc_commit.c37
-rw-r--r--fs/ubifs/tnc_misc.c4
-rw-r--r--fs/ubifs/ubifs-media.h7
-rw-r--r--fs/ubifs/ubifs.h132
-rw-r--r--fs/ubifs/xattr.c56
26 files changed, 1733 insertions, 550 deletions
diff --git a/fs/ubifs/budget.c b/fs/ubifs/budget.c
index d81fb9ed2b8e..1a4973e10664 100644
--- a/fs/ubifs/budget.c
+++ b/fs/ubifs/budget.c
@@ -263,8 +263,8 @@ int ubifs_calc_min_idx_lebs(struct ubifs_info *c)
263 263
264 idx_size = c->old_idx_sz + c->budg_idx_growth + c->budg_uncommitted_idx; 264 idx_size = c->old_idx_sz + c->budg_idx_growth + c->budg_uncommitted_idx;
265 265
266 /* And make sure we have twice the index size of space reserved */ 266 /* And make sure we have thrice the index size of space reserved */
267 idx_size <<= 1; 267 idx_size = idx_size + (idx_size << 1);
268 268
269 /* 269 /*
270 * We do not maintain 'old_idx_size' as 'old_idx_lebs'/'old_idx_bytes' 270 * We do not maintain 'old_idx_size' as 'old_idx_lebs'/'old_idx_bytes'
@@ -302,18 +302,6 @@ long long ubifs_calc_available(const struct ubifs_info *c, int min_idx_lebs)
302 int subtract_lebs; 302 int subtract_lebs;
303 long long available; 303 long long available;
304 304
305 /*
306 * Force the amount available to the total size reported if the used
307 * space is zero.
308 */
309 if (c->lst.total_used <= UBIFS_INO_NODE_SZ &&
310 c->budg_data_growth + c->budg_dd_growth == 0) {
311 /* Do the same calculation as for c->block_cnt */
312 available = c->main_lebs - 2;
313 available *= c->leb_size - c->dark_wm;
314 return available;
315 }
316
317 available = c->main_bytes - c->lst.total_used; 305 available = c->main_bytes - c->lst.total_used;
318 306
319 /* 307 /*
@@ -388,11 +376,11 @@ static int can_use_rp(struct ubifs_info *c)
388 * This function makes sure UBIFS has enough free eraseblocks for index growth 376 * This function makes sure UBIFS has enough free eraseblocks for index growth
389 * and data. 377 * and data.
390 * 378 *
391 * When budgeting index space, UBIFS reserves twice as more LEBs as the index 379 * When budgeting index space, UBIFS reserves thrice as many LEBs as the index
392 * would take if it was consolidated and written to the flash. This guarantees 380 * would take if it was consolidated and written to the flash. This guarantees
393 * that the "in-the-gaps" commit method always succeeds and UBIFS will always 381 * that the "in-the-gaps" commit method always succeeds and UBIFS will always
394 * be able to commit dirty index. So this function basically adds amount of 382 * be able to commit dirty index. So this function basically adds amount of
395 * budgeted index space to the size of the current index, multiplies this by 2, 383 * budgeted index space to the size of the current index, multiplies this by 3,
396 * and makes sure this does not exceed the amount of free eraseblocks. 384 * and makes sure this does not exceed the amount of free eraseblocks.
397 * 385 *
398 * Notes about @c->min_idx_lebs and @c->lst.idx_lebs variables: 386 * Notes about @c->min_idx_lebs and @c->lst.idx_lebs variables:
@@ -426,19 +414,21 @@ static int do_budget_space(struct ubifs_info *c)
426 * @c->lst.empty_lebs + @c->freeable_cnt + @c->idx_gc_cnt - 414 * @c->lst.empty_lebs + @c->freeable_cnt + @c->idx_gc_cnt -
427 * @c->lst.taken_empty_lebs 415 * @c->lst.taken_empty_lebs
428 * 416 *
429 * @empty_lebs are available because they are empty. @freeable_cnt are 417 * @c->lst.empty_lebs are available because they are empty.
430 * available because they contain only free and dirty space and the 418 * @c->freeable_cnt are available because they contain only free and
431 * index allocation always occurs after wbufs are synch'ed. 419 * dirty space, @c->idx_gc_cnt are available because they are index
432 * @idx_gc_cnt are available because they are index LEBs that have been 420 * LEBs that have been garbage collected and are awaiting the commit
433 * garbage collected (including trivial GC) and are awaiting the commit 421 * before they can be used. And the in-the-gaps method will grab these
434 * before they can be unmapped - note that the in-the-gaps method will 422 * if it needs them. @c->lst.taken_empty_lebs are empty LEBs that have
435 * grab these if it needs them. @taken_empty_lebs are empty_lebs that 423 * already been allocated for some purpose.
436 * have already been allocated for some purpose (also includes those
437 * LEBs on the @idx_gc list).
438 * 424 *
439 * Note, @taken_empty_lebs may temporarily be higher by one because of 425 * Note, @c->idx_gc_cnt is included to both @c->lst.empty_lebs (because
440 * the way we serialize LEB allocations and budgeting. See a comment in 426 * these LEBs are empty) and to @c->lst.taken_empty_lebs (because they
441 * 'ubifs_find_free_space()'. 427 * are taken until after the commit).
428 *
429 * Note, @c->lst.taken_empty_lebs may temporarily be higher by one
430 * because of the way we serialize LEB allocations and budgeting. See a
431 * comment in 'ubifs_find_free_space()'.
442 */ 432 */
443 lebs = c->lst.empty_lebs + c->freeable_cnt + c->idx_gc_cnt - 433 lebs = c->lst.empty_lebs + c->freeable_cnt + c->idx_gc_cnt -
444 c->lst.taken_empty_lebs; 434 c->lst.taken_empty_lebs;
@@ -543,8 +533,16 @@ int ubifs_budget_space(struct ubifs_info *c, struct ubifs_budget_req *req)
543 int err, idx_growth, data_growth, dd_growth; 533 int err, idx_growth, data_growth, dd_growth;
544 struct retries_info ri; 534 struct retries_info ri;
545 535
536 ubifs_assert(req->new_page <= 1);
537 ubifs_assert(req->dirtied_page <= 1);
538 ubifs_assert(req->new_dent <= 1);
539 ubifs_assert(req->mod_dent <= 1);
540 ubifs_assert(req->new_ino <= 1);
541 ubifs_assert(req->new_ino_d <= UBIFS_MAX_INO_DATA);
546 ubifs_assert(req->dirtied_ino <= 4); 542 ubifs_assert(req->dirtied_ino <= 4);
547 ubifs_assert(req->dirtied_ino_d <= UBIFS_MAX_INO_DATA * 4); 543 ubifs_assert(req->dirtied_ino_d <= UBIFS_MAX_INO_DATA * 4);
544 ubifs_assert(!(req->new_ino_d & 7));
545 ubifs_assert(!(req->dirtied_ino_d & 7));
548 546
549 data_growth = calc_data_growth(c, req); 547 data_growth = calc_data_growth(c, req);
550 dd_growth = calc_dd_growth(c, req); 548 dd_growth = calc_dd_growth(c, req);
@@ -618,8 +616,16 @@ again:
618 */ 616 */
619void ubifs_release_budget(struct ubifs_info *c, struct ubifs_budget_req *req) 617void ubifs_release_budget(struct ubifs_info *c, struct ubifs_budget_req *req)
620{ 618{
619 ubifs_assert(req->new_page <= 1);
620 ubifs_assert(req->dirtied_page <= 1);
621 ubifs_assert(req->new_dent <= 1);
622 ubifs_assert(req->mod_dent <= 1);
623 ubifs_assert(req->new_ino <= 1);
624 ubifs_assert(req->new_ino_d <= UBIFS_MAX_INO_DATA);
621 ubifs_assert(req->dirtied_ino <= 4); 625 ubifs_assert(req->dirtied_ino <= 4);
622 ubifs_assert(req->dirtied_ino_d <= UBIFS_MAX_INO_DATA * 4); 626 ubifs_assert(req->dirtied_ino_d <= UBIFS_MAX_INO_DATA * 4);
627 ubifs_assert(!(req->new_ino_d & 7));
628 ubifs_assert(!(req->dirtied_ino_d & 7));
623 if (!req->recalculate) { 629 if (!req->recalculate) {
624 ubifs_assert(req->idx_growth >= 0); 630 ubifs_assert(req->idx_growth >= 0);
625 ubifs_assert(req->data_growth >= 0); 631 ubifs_assert(req->data_growth >= 0);
@@ -647,7 +653,11 @@ void ubifs_release_budget(struct ubifs_info *c, struct ubifs_budget_req *req)
647 653
648 ubifs_assert(c->budg_idx_growth >= 0); 654 ubifs_assert(c->budg_idx_growth >= 0);
649 ubifs_assert(c->budg_data_growth >= 0); 655 ubifs_assert(c->budg_data_growth >= 0);
656 ubifs_assert(c->budg_dd_growth >= 0);
650 ubifs_assert(c->min_idx_lebs < c->main_lebs); 657 ubifs_assert(c->min_idx_lebs < c->main_lebs);
658 ubifs_assert(!(c->budg_idx_growth & 7));
659 ubifs_assert(!(c->budg_data_growth & 7));
660 ubifs_assert(!(c->budg_dd_growth & 7));
651 spin_unlock(&c->space_lock); 661 spin_unlock(&c->space_lock);
652} 662}
653 663
@@ -686,41 +696,114 @@ void ubifs_convert_page_budget(struct ubifs_info *c)
686void ubifs_release_dirty_inode_budget(struct ubifs_info *c, 696void ubifs_release_dirty_inode_budget(struct ubifs_info *c,
687 struct ubifs_inode *ui) 697 struct ubifs_inode *ui)
688{ 698{
689 struct ubifs_budget_req req = {.dd_growth = c->inode_budget, 699 struct ubifs_budget_req req;
690 .dirtied_ino_d = ui->data_len};
691 700
701 memset(&req, 0, sizeof(struct ubifs_budget_req));
702 req.dd_growth = c->inode_budget + ALIGN(ui->data_len, 8);
692 ubifs_release_budget(c, &req); 703 ubifs_release_budget(c, &req);
693} 704}
694 705
695/** 706/**
696 * ubifs_budg_get_free_space - return amount of free space. 707 * ubifs_reported_space - calculate reported free space.
708 * @c: the UBIFS file-system description object
709 * @free: amount of free space
710 *
711 * This function calculates amount of free space which will be reported to
712 * 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
714 * 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
716 * overhead, and UBIFS it has to report sligtly less free space to meet the
717 * above expectetion.
718 *
719 * 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
721 * space to write the index thrice).
722 *
723 * Note, the calculation is pessimistic, which means that most of the time
724 * UBIFS reports less space than it actually has.
725 */
726long long ubifs_reported_space(const struct ubifs_info *c, uint64_t free)
727{
728 int divisor, factor, f;
729
730 /*
731 * Reported space size is @free * X, where X is UBIFS block size
732 * divided by UBIFS block size + all overhead one data block
733 * introduces. The overhead is the node header + indexing overhead.
734 *
735 * Indexing overhead calculations are based on the following formula:
736 * I = N/(f - 1) + 1, where I - number of indexing nodes, N - number
737 * of data nodes, f - fanout. Because effective UBIFS fanout is twice
738 * as less than maximum fanout, we assume that each data node
739 * 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
741 * for the index.
742 */
743 f = c->fanout > 3 ? c->fanout >> 1 : 2;
744 factor = UBIFS_BLOCK_SIZE;
745 divisor = UBIFS_MAX_DATA_NODE_SZ;
746 divisor += (c->max_idx_node_sz * 3) / (f - 1);
747 free *= factor;
748 do_div(free, divisor);
749 return free;
750}
751
752/**
753 * ubifs_get_free_space - return amount of free space.
697 * @c: UBIFS file-system description object 754 * @c: UBIFS file-system description object
698 * 755 *
699 * This function returns amount of free space on the file-system. 756 * This function calculates amount of free space to report to user-space.
757 *
758 * Because UBIFS may introduce substantial overhead (the index, node headers,
759 * alighment, 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
761 * reclamable, 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
763 * 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
765 * 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.
700 */ 767 */
701long long ubifs_budg_get_free_space(struct ubifs_info *c) 768long long ubifs_get_free_space(struct ubifs_info *c)
702{ 769{
703 int min_idx_lebs, rsvd_idx_lebs; 770 int min_idx_lebs, rsvd_idx_lebs, lebs;
704 long long available, outstanding, free; 771 long long available, outstanding, free;
705 772
706 /* Do exactly the same calculations as in 'do_budget_space()' */
707 spin_lock(&c->space_lock); 773 spin_lock(&c->space_lock);
708 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;
709 776
710 if (min_idx_lebs > c->lst.idx_lebs) 777 /*
711 rsvd_idx_lebs = min_idx_lebs - c->lst.idx_lebs; 778 * Force the amount available to the total size reported if the used
712 else 779 * space is zero.
713 rsvd_idx_lebs = 0; 780 */
714 781 if (c->lst.total_used <= UBIFS_INO_NODE_SZ && !outstanding) {
715 if (rsvd_idx_lebs > c->lst.empty_lebs + c->freeable_cnt + c->idx_gc_cnt
716 - c->lst.taken_empty_lebs) {
717 spin_unlock(&c->space_lock); 782 spin_unlock(&c->space_lock);
718 return 0; 783 return (long long)c->block_cnt << UBIFS_BLOCK_SHIFT;
719 } 784 }
720 785
721 available = ubifs_calc_available(c, min_idx_lebs); 786 available = ubifs_calc_available(c, min_idx_lebs);
722 outstanding = c->budg_data_growth + c->budg_dd_growth; 787
723 c->min_idx_lebs = min_idx_lebs; 788 /*
789 * When reporting free space to user-space, UBIFS guarantees that it is
790 * possible to write a file of free space size. This means that for
791 * empty LEBs we may use more precise calculations than
792 * 'ubifs_calc_available()' is using. Namely, we know that in empty
793 * LEBs we would waste only @c->leb_overhead bytes, not @c->dark_wm.
794 * Thus, amend the available space.
795 *
796 * Note, the calculations below are similar to what we have in
797 * 'do_budget_space()', so refer there for comments.
798 */
799 if (min_idx_lebs > c->lst.idx_lebs)
800 rsvd_idx_lebs = min_idx_lebs - c->lst.idx_lebs;
801 else
802 rsvd_idx_lebs = 0;
803 lebs = c->lst.empty_lebs + c->freeable_cnt + c->idx_gc_cnt -
804 c->lst.taken_empty_lebs;
805 lebs -= rsvd_idx_lebs;
806 available += lebs * (c->dark_wm - c->leb_overhead);
724 spin_unlock(&c->space_lock); 807 spin_unlock(&c->space_lock);
725 808
726 if (available > outstanding) 809 if (available > outstanding)
diff --git a/fs/ubifs/commit.c b/fs/ubifs/commit.c
index 3b516316c9b3..0a6aa2cc78f0 100644
--- a/fs/ubifs/commit.c
+++ b/fs/ubifs/commit.c
@@ -74,6 +74,7 @@ static int do_commit(struct ubifs_info *c)
74 goto out_up; 74 goto out_up;
75 } 75 }
76 76
77 c->cmt_no += 1;
77 err = ubifs_gc_start_commit(c); 78 err = ubifs_gc_start_commit(c);
78 if (err) 79 if (err)
79 goto out_up; 80 goto out_up;
@@ -115,7 +116,7 @@ static int do_commit(struct ubifs_info *c)
115 goto out; 116 goto out;
116 117
117 mutex_lock(&c->mst_mutex); 118 mutex_lock(&c->mst_mutex);
118 c->mst_node->cmt_no = cpu_to_le64(++c->cmt_no); 119 c->mst_node->cmt_no = cpu_to_le64(c->cmt_no);
119 c->mst_node->log_lnum = cpu_to_le32(new_ltail_lnum); 120 c->mst_node->log_lnum = cpu_to_le32(new_ltail_lnum);
120 c->mst_node->root_lnum = cpu_to_le32(zroot.lnum); 121 c->mst_node->root_lnum = cpu_to_le32(zroot.lnum);
121 c->mst_node->root_offs = cpu_to_le32(zroot.offs); 122 c->mst_node->root_offs = cpu_to_le32(zroot.offs);
diff --git a/fs/ubifs/compress.c b/fs/ubifs/compress.c
index 5bb51dac3c16..a0ada596b17c 100644
--- a/fs/ubifs/compress.c
+++ b/fs/ubifs/compress.c
@@ -91,8 +91,6 @@ struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
91 * 91 *
92 * Note, if the input buffer was not compressed, it is copied to the output 92 * Note, if the input buffer was not compressed, it is copied to the output
93 * buffer and %UBIFS_COMPR_NONE is returned in @compr_type. 93 * buffer and %UBIFS_COMPR_NONE is returned in @compr_type.
94 *
95 * This functions returns %0 on success or a negative error code on failure.
96 */ 94 */
97void ubifs_compress(const void *in_buf, int in_len, void *out_buf, int *out_len, 95void ubifs_compress(const void *in_buf, int in_len, void *out_buf, int *out_len,
98 int *compr_type) 96 int *compr_type)
diff --git a/fs/ubifs/debug.c b/fs/ubifs/debug.c
index 4e3aaeba4eca..7186400750e7 100644
--- a/fs/ubifs/debug.c
+++ b/fs/ubifs/debug.c
@@ -222,30 +222,38 @@ void dbg_dump_inode(const struct ubifs_info *c, const struct inode *inode)
222{ 222{
223 const struct ubifs_inode *ui = ubifs_inode(inode); 223 const struct ubifs_inode *ui = ubifs_inode(inode);
224 224
225 printk(KERN_DEBUG "inode %lu\n", inode->i_ino); 225 printk(KERN_DEBUG "Dump in-memory inode:");
226 printk(KERN_DEBUG "size %llu\n", 226 printk(KERN_DEBUG "\tinode %lu\n", inode->i_ino);
227 printk(KERN_DEBUG "\tsize %llu\n",
227 (unsigned long long)i_size_read(inode)); 228 (unsigned long long)i_size_read(inode));
228 printk(KERN_DEBUG "nlink %u\n", inode->i_nlink); 229 printk(KERN_DEBUG "\tnlink %u\n", inode->i_nlink);
229 printk(KERN_DEBUG "uid %u\n", (unsigned int)inode->i_uid); 230 printk(KERN_DEBUG "\tuid %u\n", (unsigned int)inode->i_uid);
230 printk(KERN_DEBUG "gid %u\n", (unsigned int)inode->i_gid); 231 printk(KERN_DEBUG "\tgid %u\n", (unsigned int)inode->i_gid);
231 printk(KERN_DEBUG "atime %u.%u\n", 232 printk(KERN_DEBUG "\tatime %u.%u\n",
232 (unsigned int)inode->i_atime.tv_sec, 233 (unsigned int)inode->i_atime.tv_sec,
233 (unsigned int)inode->i_atime.tv_nsec); 234 (unsigned int)inode->i_atime.tv_nsec);
234 printk(KERN_DEBUG "mtime %u.%u\n", 235 printk(KERN_DEBUG "\tmtime %u.%u\n",
235 (unsigned int)inode->i_mtime.tv_sec, 236 (unsigned int)inode->i_mtime.tv_sec,
236 (unsigned int)inode->i_mtime.tv_nsec); 237 (unsigned int)inode->i_mtime.tv_nsec);
237 printk(KERN_DEBUG "ctime %u.%u\n", 238 printk(KERN_DEBUG "\tctime %u.%u\n",
238 (unsigned int)inode->i_ctime.tv_sec, 239 (unsigned int)inode->i_ctime.tv_sec,
239 (unsigned int)inode->i_ctime.tv_nsec); 240 (unsigned int)inode->i_ctime.tv_nsec);
240 printk(KERN_DEBUG "creat_sqnum %llu\n", ui->creat_sqnum); 241 printk(KERN_DEBUG "\tcreat_sqnum %llu\n", ui->creat_sqnum);
241 printk(KERN_DEBUG "xattr_size %u\n", ui->xattr_size); 242 printk(KERN_DEBUG "\txattr_size %u\n", ui->xattr_size);
242 printk(KERN_DEBUG "xattr_cnt %u\n", ui->xattr_cnt); 243 printk(KERN_DEBUG "\txattr_cnt %u\n", ui->xattr_cnt);
243 printk(KERN_DEBUG "xattr_names %u\n", ui->xattr_names); 244 printk(KERN_DEBUG "\txattr_names %u\n", ui->xattr_names);
244 printk(KERN_DEBUG "dirty %u\n", ui->dirty); 245 printk(KERN_DEBUG "\tdirty %u\n", ui->dirty);
245 printk(KERN_DEBUG "xattr %u\n", ui->xattr); 246 printk(KERN_DEBUG "\txattr %u\n", ui->xattr);
246 printk(KERN_DEBUG "flags %d\n", ui->flags); 247 printk(KERN_DEBUG "\tbulk_read %u\n", ui->xattr);
247 printk(KERN_DEBUG "compr_type %d\n", ui->compr_type); 248 printk(KERN_DEBUG "\tsynced_i_size %llu\n",
248 printk(KERN_DEBUG "data_len %d\n", ui->data_len); 249 (unsigned long long)ui->synced_i_size);
250 printk(KERN_DEBUG "\tui_size %llu\n",
251 (unsigned long long)ui->ui_size);
252 printk(KERN_DEBUG "\tflags %d\n", ui->flags);
253 printk(KERN_DEBUG "\tcompr_type %d\n", ui->compr_type);
254 printk(KERN_DEBUG "\tlast_page_read %lu\n", ui->last_page_read);
255 printk(KERN_DEBUG "\tread_in_a_row %lu\n", ui->read_in_a_row);
256 printk(KERN_DEBUG "\tdata_len %d\n", ui->data_len);
249} 257}
250 258
251void dbg_dump_node(const struct ubifs_info *c, const void *node) 259void dbg_dump_node(const struct ubifs_info *c, const void *node)
@@ -538,7 +546,7 @@ void dbg_dump_node(const struct ubifs_info *c, const void *node)
538 printk(KERN_DEBUG "\t%d orphan inode numbers:\n", n); 546 printk(KERN_DEBUG "\t%d orphan inode numbers:\n", n);
539 for (i = 0; i < n; i++) 547 for (i = 0; i < n; i++)
540 printk(KERN_DEBUG "\t ino %llu\n", 548 printk(KERN_DEBUG "\t ino %llu\n",
541 le64_to_cpu(orph->inos[i])); 549 (unsigned long long)le64_to_cpu(orph->inos[i]));
542 break; 550 break;
543 } 551 }
544 default: 552 default:
@@ -568,8 +576,8 @@ void dbg_dump_budget_req(const struct ubifs_budget_req *req)
568void dbg_dump_lstats(const struct ubifs_lp_stats *lst) 576void dbg_dump_lstats(const struct ubifs_lp_stats *lst)
569{ 577{
570 spin_lock(&dbg_lock); 578 spin_lock(&dbg_lock);
571 printk(KERN_DEBUG "Lprops statistics: empty_lebs %d, idx_lebs %d\n", 579 printk(KERN_DEBUG "(pid %d) Lprops statistics: empty_lebs %d, "
572 lst->empty_lebs, lst->idx_lebs); 580 "idx_lebs %d\n", current->pid, lst->empty_lebs, lst->idx_lebs);
573 printk(KERN_DEBUG "\ttaken_empty_lebs %d, total_free %lld, " 581 printk(KERN_DEBUG "\ttaken_empty_lebs %d, total_free %lld, "
574 "total_dirty %lld\n", lst->taken_empty_lebs, lst->total_free, 582 "total_dirty %lld\n", lst->taken_empty_lebs, lst->total_free,
575 lst->total_dirty); 583 lst->total_dirty);
@@ -587,8 +595,8 @@ void dbg_dump_budg(struct ubifs_info *c)
587 struct ubifs_gced_idx_leb *idx_gc; 595 struct ubifs_gced_idx_leb *idx_gc;
588 596
589 spin_lock(&dbg_lock); 597 spin_lock(&dbg_lock);
590 printk(KERN_DEBUG "Budgeting info: budg_data_growth %lld, " 598 printk(KERN_DEBUG "(pid %d) Budgeting info: budg_data_growth %lld, "
591 "budg_dd_growth %lld, budg_idx_growth %lld\n", 599 "budg_dd_growth %lld, budg_idx_growth %lld\n", current->pid,
592 c->budg_data_growth, c->budg_dd_growth, c->budg_idx_growth); 600 c->budg_data_growth, c->budg_dd_growth, c->budg_idx_growth);
593 printk(KERN_DEBUG "\tdata budget sum %lld, total budget sum %lld, " 601 printk(KERN_DEBUG "\tdata budget sum %lld, total budget sum %lld, "
594 "freeable_cnt %d\n", c->budg_data_growth + c->budg_dd_growth, 602 "freeable_cnt %d\n", c->budg_data_growth + c->budg_dd_growth,
@@ -634,7 +642,7 @@ void dbg_dump_lprops(struct ubifs_info *c)
634 struct ubifs_lprops lp; 642 struct ubifs_lprops lp;
635 struct ubifs_lp_stats lst; 643 struct ubifs_lp_stats lst;
636 644
637 printk(KERN_DEBUG "Dumping LEB properties\n"); 645 printk(KERN_DEBUG "(pid %d) Dumping LEB properties\n", current->pid);
638 ubifs_get_lp_stats(c, &lst); 646 ubifs_get_lp_stats(c, &lst);
639 dbg_dump_lstats(&lst); 647 dbg_dump_lstats(&lst);
640 648
@@ -647,6 +655,43 @@ void dbg_dump_lprops(struct ubifs_info *c)
647 } 655 }
648} 656}
649 657
658void dbg_dump_lpt_info(struct ubifs_info *c)
659{
660 int i;
661
662 spin_lock(&dbg_lock);
663 printk(KERN_DEBUG "\tlpt_sz: %lld\n", c->lpt_sz);
664 printk(KERN_DEBUG "\tpnode_sz: %d\n", c->pnode_sz);
665 printk(KERN_DEBUG "\tnnode_sz: %d\n", c->nnode_sz);
666 printk(KERN_DEBUG "\tltab_sz: %d\n", c->ltab_sz);
667 printk(KERN_DEBUG "\tlsave_sz: %d\n", c->lsave_sz);
668 printk(KERN_DEBUG "\tbig_lpt: %d\n", c->big_lpt);
669 printk(KERN_DEBUG "\tlpt_hght: %d\n", c->lpt_hght);
670 printk(KERN_DEBUG "\tpnode_cnt: %d\n", c->pnode_cnt);
671 printk(KERN_DEBUG "\tnnode_cnt: %d\n", c->nnode_cnt);
672 printk(KERN_DEBUG "\tdirty_pn_cnt: %d\n", c->dirty_pn_cnt);
673 printk(KERN_DEBUG "\tdirty_nn_cnt: %d\n", c->dirty_nn_cnt);
674 printk(KERN_DEBUG "\tlsave_cnt: %d\n", c->lsave_cnt);
675 printk(KERN_DEBUG "\tspace_bits: %d\n", c->space_bits);
676 printk(KERN_DEBUG "\tlpt_lnum_bits: %d\n", c->lpt_lnum_bits);
677 printk(KERN_DEBUG "\tlpt_offs_bits: %d\n", c->lpt_offs_bits);
678 printk(KERN_DEBUG "\tlpt_spc_bits: %d\n", c->lpt_spc_bits);
679 printk(KERN_DEBUG "\tpcnt_bits: %d\n", c->pcnt_bits);
680 printk(KERN_DEBUG "\tlnum_bits: %d\n", c->lnum_bits);
681 printk(KERN_DEBUG "\tLPT root is at %d:%d\n", c->lpt_lnum, c->lpt_offs);
682 printk(KERN_DEBUG "\tLPT head is at %d:%d\n",
683 c->nhead_lnum, c->nhead_offs);
684 printk(KERN_DEBUG "\tLPT ltab is at %d:%d\n", c->ltab_lnum, c->ltab_offs);
685 if (c->big_lpt)
686 printk(KERN_DEBUG "\tLPT lsave is at %d:%d\n",
687 c->lsave_lnum, c->lsave_offs);
688 for (i = 0; i < c->lpt_lebs; i++)
689 printk(KERN_DEBUG "\tLPT LEB %d free %d dirty %d tgc %d "
690 "cmt %d\n", i + c->lpt_first, c->ltab[i].free,
691 c->ltab[i].dirty, c->ltab[i].tgc, c->ltab[i].cmt);
692 spin_unlock(&dbg_lock);
693}
694
650void dbg_dump_leb(const struct ubifs_info *c, int lnum) 695void dbg_dump_leb(const struct ubifs_info *c, int lnum)
651{ 696{
652 struct ubifs_scan_leb *sleb; 697 struct ubifs_scan_leb *sleb;
@@ -655,7 +700,7 @@ void dbg_dump_leb(const struct ubifs_info *c, int lnum)
655 if (dbg_failure_mode) 700 if (dbg_failure_mode)
656 return; 701 return;
657 702
658 printk(KERN_DEBUG "Dumping LEB %d\n", lnum); 703 printk(KERN_DEBUG "(pid %d) Dumping LEB %d\n", current->pid, lnum);
659 704
660 sleb = ubifs_scan(c, lnum, 0, c->dbg_buf); 705 sleb = ubifs_scan(c, lnum, 0, c->dbg_buf);
661 if (IS_ERR(sleb)) { 706 if (IS_ERR(sleb)) {
@@ -720,8 +765,8 @@ void dbg_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat)
720{ 765{
721 int i; 766 int i;
722 767
723 printk(KERN_DEBUG "Dumping heap cat %d (%d elements)\n", 768 printk(KERN_DEBUG "(pid %d) Dumping heap cat %d (%d elements)\n",
724 cat, heap->cnt); 769 current->pid, cat, heap->cnt);
725 for (i = 0; i < heap->cnt; i++) { 770 for (i = 0; i < heap->cnt; i++) {
726 struct ubifs_lprops *lprops = heap->arr[i]; 771 struct ubifs_lprops *lprops = heap->arr[i];
727 772
@@ -736,7 +781,7 @@ void dbg_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
736{ 781{
737 int i; 782 int i;
738 783
739 printk(KERN_DEBUG "Dumping pnode:\n"); 784 printk(KERN_DEBUG "(pid %d) Dumping pnode:\n", current->pid);
740 printk(KERN_DEBUG "\taddress %zx parent %zx cnext %zx\n", 785 printk(KERN_DEBUG "\taddress %zx parent %zx cnext %zx\n",
741 (size_t)pnode, (size_t)parent, (size_t)pnode->cnext); 786 (size_t)pnode, (size_t)parent, (size_t)pnode->cnext);
742 printk(KERN_DEBUG "\tflags %lu iip %d level %d num %d\n", 787 printk(KERN_DEBUG "\tflags %lu iip %d level %d num %d\n",
@@ -755,7 +800,7 @@ void dbg_dump_tnc(struct ubifs_info *c)
755 int level; 800 int level;
756 801
757 printk(KERN_DEBUG "\n"); 802 printk(KERN_DEBUG "\n");
758 printk(KERN_DEBUG "Dumping the TNC tree\n"); 803 printk(KERN_DEBUG "(pid %d) Dumping the TNC tree\n", current->pid);
759 znode = ubifs_tnc_levelorder_next(c->zroot.znode, NULL); 804 znode = ubifs_tnc_levelorder_next(c->zroot.znode, NULL);
760 level = znode->level; 805 level = znode->level;
761 printk(KERN_DEBUG "== Level %d ==\n", level); 806 printk(KERN_DEBUG "== Level %d ==\n", level);
@@ -2208,16 +2253,17 @@ int dbg_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
2208int dbg_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf, 2253int dbg_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
2209 int offset, int len, int dtype) 2254 int offset, int len, int dtype)
2210{ 2255{
2211 int err; 2256 int err, failing;
2212 2257
2213 if (in_failure_mode(desc)) 2258 if (in_failure_mode(desc))
2214 return -EIO; 2259 return -EIO;
2215 if (do_fail(desc, lnum, 1)) 2260 failing = do_fail(desc, lnum, 1);
2261 if (failing)
2216 cut_data(buf, len); 2262 cut_data(buf, len);
2217 err = ubi_leb_write(desc, lnum, buf, offset, len, dtype); 2263 err = ubi_leb_write(desc, lnum, buf, offset, len, dtype);
2218 if (err) 2264 if (err)
2219 return err; 2265 return err;
2220 if (in_failure_mode(desc)) 2266 if (failing)
2221 return -EIO; 2267 return -EIO;
2222 return 0; 2268 return 0;
2223} 2269}
diff --git a/fs/ubifs/debug.h b/fs/ubifs/debug.h
index 3c4f1e93c9e0..33d6b95071e4 100644
--- a/fs/ubifs/debug.h
+++ b/fs/ubifs/debug.h
@@ -27,7 +27,7 @@
27 27
28#define UBIFS_DBG(op) op 28#define UBIFS_DBG(op) op
29 29
30#define ubifs_assert(expr) do { \ 30#define ubifs_assert(expr) do { \
31 if (unlikely(!(expr))) { \ 31 if (unlikely(!(expr))) { \
32 printk(KERN_CRIT "UBIFS assert failed in %s at %u (pid %d)\n", \ 32 printk(KERN_CRIT "UBIFS assert failed in %s at %u (pid %d)\n", \
33 __func__, __LINE__, current->pid); \ 33 __func__, __LINE__, current->pid); \
@@ -73,50 +73,50 @@ const char *dbg_key_str1(const struct ubifs_info *c,
73 const union ubifs_key *key); 73 const union ubifs_key *key);
74 74
75/* 75/*
76 * DBGKEY macros require dbg_lock to be held, which it is in the dbg message 76 * DBGKEY macros require @dbg_lock to be held, which it is in the dbg message
77 * macros. 77 * macros.
78 */ 78 */
79#define DBGKEY(key) dbg_key_str0(c, (key)) 79#define DBGKEY(key) dbg_key_str0(c, (key))
80#define DBGKEY1(key) dbg_key_str1(c, (key)) 80#define DBGKEY1(key) dbg_key_str1(c, (key))
81 81
82/* General messages */ 82/* General messages */
83#define dbg_gen(fmt, ...) dbg_do_msg(UBIFS_MSG_GEN, fmt, ##__VA_ARGS__) 83#define dbg_gen(fmt, ...) dbg_do_msg(UBIFS_MSG_GEN, fmt, ##__VA_ARGS__)
84 84
85/* Additional journal messages */ 85/* Additional journal messages */
86#define dbg_jnl(fmt, ...) dbg_do_msg(UBIFS_MSG_JNL, fmt, ##__VA_ARGS__) 86#define dbg_jnl(fmt, ...) dbg_do_msg(UBIFS_MSG_JNL, fmt, ##__VA_ARGS__)
87 87
88/* Additional TNC messages */ 88/* Additional TNC messages */
89#define dbg_tnc(fmt, ...) dbg_do_msg(UBIFS_MSG_TNC, fmt, ##__VA_ARGS__) 89#define dbg_tnc(fmt, ...) dbg_do_msg(UBIFS_MSG_TNC, fmt, ##__VA_ARGS__)
90 90
91/* Additional lprops messages */ 91/* Additional lprops messages */
92#define dbg_lp(fmt, ...) dbg_do_msg(UBIFS_MSG_LP, fmt, ##__VA_ARGS__) 92#define dbg_lp(fmt, ...) dbg_do_msg(UBIFS_MSG_LP, fmt, ##__VA_ARGS__)
93 93
94/* Additional LEB find messages */ 94/* Additional LEB find messages */
95#define dbg_find(fmt, ...) dbg_do_msg(UBIFS_MSG_FIND, fmt, ##__VA_ARGS__) 95#define dbg_find(fmt, ...) dbg_do_msg(UBIFS_MSG_FIND, fmt, ##__VA_ARGS__)
96 96
97/* Additional mount messages */ 97/* Additional mount messages */
98#define dbg_mnt(fmt, ...) dbg_do_msg(UBIFS_MSG_MNT, fmt, ##__VA_ARGS__) 98#define dbg_mnt(fmt, ...) dbg_do_msg(UBIFS_MSG_MNT, fmt, ##__VA_ARGS__)
99 99
100/* Additional I/O messages */ 100/* Additional I/O messages */
101#define dbg_io(fmt, ...) dbg_do_msg(UBIFS_MSG_IO, fmt, ##__VA_ARGS__) 101#define dbg_io(fmt, ...) dbg_do_msg(UBIFS_MSG_IO, fmt, ##__VA_ARGS__)
102 102
103/* Additional commit messages */ 103/* Additional commit messages */
104#define dbg_cmt(fmt, ...) dbg_do_msg(UBIFS_MSG_CMT, fmt, ##__VA_ARGS__) 104#define dbg_cmt(fmt, ...) dbg_do_msg(UBIFS_MSG_CMT, fmt, ##__VA_ARGS__)
105 105
106/* Additional budgeting messages */ 106/* Additional budgeting messages */
107#define dbg_budg(fmt, ...) dbg_do_msg(UBIFS_MSG_BUDG, fmt, ##__VA_ARGS__) 107#define dbg_budg(fmt, ...) dbg_do_msg(UBIFS_MSG_BUDG, fmt, ##__VA_ARGS__)
108 108
109/* Additional log messages */ 109/* Additional log messages */
110#define dbg_log(fmt, ...) dbg_do_msg(UBIFS_MSG_LOG, fmt, ##__VA_ARGS__) 110#define dbg_log(fmt, ...) dbg_do_msg(UBIFS_MSG_LOG, fmt, ##__VA_ARGS__)
111 111
112/* Additional gc messages */ 112/* Additional gc messages */
113#define dbg_gc(fmt, ...) dbg_do_msg(UBIFS_MSG_GC, fmt, ##__VA_ARGS__) 113#define dbg_gc(fmt, ...) dbg_do_msg(UBIFS_MSG_GC, fmt, ##__VA_ARGS__)
114 114
115/* Additional scan messages */ 115/* Additional scan messages */
116#define dbg_scan(fmt, ...) dbg_do_msg(UBIFS_MSG_SCAN, fmt, ##__VA_ARGS__) 116#define dbg_scan(fmt, ...) dbg_do_msg(UBIFS_MSG_SCAN, fmt, ##__VA_ARGS__)
117 117
118/* Additional recovery messages */ 118/* Additional recovery messages */
119#define dbg_rcvry(fmt, ...) dbg_do_msg(UBIFS_MSG_RCVRY, fmt, ##__VA_ARGS__) 119#define dbg_rcvry(fmt, ...) dbg_do_msg(UBIFS_MSG_RCVRY, fmt, ##__VA_ARGS__)
120 120
121/* 121/*
122 * Debugging message type flags (must match msg_type_names in debug.c). 122 * Debugging message type flags (must match msg_type_names in debug.c).
@@ -224,6 +224,7 @@ void dbg_dump_lstats(const struct ubifs_lp_stats *lst);
224void dbg_dump_budg(struct ubifs_info *c); 224void dbg_dump_budg(struct ubifs_info *c);
225void dbg_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp); 225void dbg_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp);
226void dbg_dump_lprops(struct ubifs_info *c); 226void dbg_dump_lprops(struct ubifs_info *c);
227void dbg_dump_lpt_info(struct ubifs_info *c);
227void dbg_dump_leb(const struct ubifs_info *c, int lnum); 228void dbg_dump_leb(const struct ubifs_info *c, int lnum);
228void dbg_dump_znode(const struct ubifs_info *c, 229void dbg_dump_znode(const struct ubifs_info *c,
229 const struct ubifs_znode *znode); 230 const struct ubifs_znode *znode);
@@ -239,34 +240,25 @@ typedef int (*dbg_leaf_callback)(struct ubifs_info *c,
239 struct ubifs_zbranch *zbr, void *priv); 240 struct ubifs_zbranch *zbr, void *priv);
240typedef int (*dbg_znode_callback)(struct ubifs_info *c, 241typedef int (*dbg_znode_callback)(struct ubifs_info *c,
241 struct ubifs_znode *znode, void *priv); 242 struct ubifs_znode *znode, void *priv);
242
243int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb, 243int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
244 dbg_znode_callback znode_cb, void *priv); 244 dbg_znode_callback znode_cb, void *priv);
245 245
246/* Checking functions */ 246/* Checking functions */
247 247
248int dbg_check_lprops(struct ubifs_info *c); 248int dbg_check_lprops(struct ubifs_info *c);
249
250int dbg_old_index_check_init(struct ubifs_info *c, struct ubifs_zbranch *zroot); 249int dbg_old_index_check_init(struct ubifs_info *c, struct ubifs_zbranch *zroot);
251int dbg_check_old_index(struct ubifs_info *c, struct ubifs_zbranch *zroot); 250int dbg_check_old_index(struct ubifs_info *c, struct ubifs_zbranch *zroot);
252
253int dbg_check_cats(struct ubifs_info *c); 251int dbg_check_cats(struct ubifs_info *c);
254
255int dbg_check_ltab(struct ubifs_info *c); 252int dbg_check_ltab(struct ubifs_info *c);
256 253int dbg_chk_lpt_free_spc(struct ubifs_info *c);
254int dbg_chk_lpt_sz(struct ubifs_info *c, int action, int len);
257int dbg_check_synced_i_size(struct inode *inode); 255int dbg_check_synced_i_size(struct inode *inode);
258
259int dbg_check_dir_size(struct ubifs_info *c, const struct inode *dir); 256int dbg_check_dir_size(struct ubifs_info *c, const struct inode *dir);
260
261int dbg_check_tnc(struct ubifs_info *c, int extra); 257int dbg_check_tnc(struct ubifs_info *c, int extra);
262
263int dbg_check_idx_size(struct ubifs_info *c, long long idx_size); 258int dbg_check_idx_size(struct ubifs_info *c, long long idx_size);
264
265int dbg_check_filesystem(struct ubifs_info *c); 259int dbg_check_filesystem(struct ubifs_info *c);
266
267void dbg_check_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat, 260void dbg_check_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat,
268 int add_pos); 261 int add_pos);
269
270int dbg_check_lprops(struct ubifs_info *c); 262int dbg_check_lprops(struct ubifs_info *c);
271int dbg_check_lpt_nodes(struct ubifs_info *c, struct ubifs_cnode *cnode, 263int dbg_check_lpt_nodes(struct ubifs_info *c, struct ubifs_cnode *cnode,
272 int row, int col); 264 int row, int col);
@@ -329,71 +321,80 @@ static inline int dbg_change(struct ubi_volume_desc *desc, int lnum,
329#else /* !CONFIG_UBIFS_FS_DEBUG */ 321#else /* !CONFIG_UBIFS_FS_DEBUG */
330 322
331#define UBIFS_DBG(op) 323#define UBIFS_DBG(op)
332#define ubifs_assert(expr) ({}) 324
333#define ubifs_assert_cmt_locked(c) 325/* Use "if (0)" to make compiler check arguments even if debugging is off */
326#define ubifs_assert(expr) do { \
327 if (0 && (expr)) \
328 printk(KERN_CRIT "UBIFS assert failed in %s at %u (pid %d)\n", \
329 __func__, __LINE__, current->pid); \
330} while (0)
331
332#define dbg_err(fmt, ...) do { \
333 if (0) \
334 ubifs_err(fmt, ##__VA_ARGS__); \
335} while (0)
336
337#define dbg_msg(fmt, ...) do { \
338 if (0) \
339 printk(KERN_DEBUG "UBIFS DBG (pid %d): %s: " fmt "\n", \
340 current->pid, __func__, ##__VA_ARGS__); \
341} while (0)
342
334#define dbg_dump_stack() 343#define dbg_dump_stack()
335#define dbg_err(fmt, ...) ({}) 344#define ubifs_assert_cmt_locked(c)
336#define dbg_msg(fmt, ...) ({})
337#define dbg_key(c, key, fmt, ...) ({})
338
339#define dbg_gen(fmt, ...) ({})
340#define dbg_jnl(fmt, ...) ({})
341#define dbg_tnc(fmt, ...) ({})
342#define dbg_lp(fmt, ...) ({})
343#define dbg_find(fmt, ...) ({})
344#define dbg_mnt(fmt, ...) ({})
345#define dbg_io(fmt, ...) ({})
346#define dbg_cmt(fmt, ...) ({})
347#define dbg_budg(fmt, ...) ({})
348#define dbg_log(fmt, ...) ({})
349#define dbg_gc(fmt, ...) ({})
350#define dbg_scan(fmt, ...) ({})
351#define dbg_rcvry(fmt, ...) ({})
352
353#define dbg_ntype(type) ""
354#define dbg_cstate(cmt_state) ""
355#define dbg_get_key_dump(c, key) ({})
356#define dbg_dump_inode(c, inode) ({})
357#define dbg_dump_node(c, node) ({})
358#define dbg_dump_budget_req(req) ({})
359#define dbg_dump_lstats(lst) ({})
360#define dbg_dump_budg(c) ({})
361#define dbg_dump_lprop(c, lp) ({})
362#define dbg_dump_lprops(c) ({})
363#define dbg_dump_leb(c, lnum) ({})
364#define dbg_dump_znode(c, znode) ({})
365#define dbg_dump_heap(c, heap, cat) ({})
366#define dbg_dump_pnode(c, pnode, parent, iip) ({})
367#define dbg_dump_tnc(c) ({})
368#define dbg_dump_index(c) ({})
369 345
370#define dbg_walk_index(c, leaf_cb, znode_cb, priv) 0 346#define dbg_gen(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
347#define dbg_jnl(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
348#define dbg_tnc(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
349#define dbg_lp(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
350#define dbg_find(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
351#define dbg_mnt(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
352#define dbg_io(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
353#define dbg_cmt(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
354#define dbg_budg(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
355#define dbg_log(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
356#define dbg_gc(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
357#define dbg_scan(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
358#define dbg_rcvry(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
359
360#define DBGKEY(key) ((char *)(key))
361#define DBGKEY1(key) ((char *)(key))
362
363#define dbg_ntype(type) ""
364#define dbg_cstate(cmt_state) ""
365#define dbg_get_key_dump(c, key) ({})
366#define dbg_dump_inode(c, inode) ({})
367#define dbg_dump_node(c, node) ({})
368#define dbg_dump_budget_req(req) ({})
369#define dbg_dump_lstats(lst) ({})
370#define dbg_dump_budg(c) ({})
371#define dbg_dump_lprop(c, lp) ({})
372#define dbg_dump_lprops(c) ({})
373#define dbg_dump_lpt_info(c) ({})
374#define dbg_dump_leb(c, lnum) ({})
375#define dbg_dump_znode(c, znode) ({})
376#define dbg_dump_heap(c, heap, cat) ({})
377#define dbg_dump_pnode(c, pnode, parent, iip) ({})
378#define dbg_dump_tnc(c) ({})
379#define dbg_dump_index(c) ({})
371 380
381#define dbg_walk_index(c, leaf_cb, znode_cb, priv) 0
372#define dbg_old_index_check_init(c, zroot) 0 382#define dbg_old_index_check_init(c, zroot) 0
373#define dbg_check_old_index(c, zroot) 0 383#define dbg_check_old_index(c, zroot) 0
374
375#define dbg_check_cats(c) 0 384#define dbg_check_cats(c) 0
376
377#define dbg_check_ltab(c) 0 385#define dbg_check_ltab(c) 0
378 386#define dbg_chk_lpt_free_spc(c) 0
387#define dbg_chk_lpt_sz(c, action, len) 0
379#define dbg_check_synced_i_size(inode) 0 388#define dbg_check_synced_i_size(inode) 0
380
381#define dbg_check_dir_size(c, dir) 0 389#define dbg_check_dir_size(c, dir) 0
382
383#define dbg_check_tnc(c, x) 0 390#define dbg_check_tnc(c, x) 0
384
385#define dbg_check_idx_size(c, idx_size) 0 391#define dbg_check_idx_size(c, idx_size) 0
386
387#define dbg_check_filesystem(c) 0 392#define dbg_check_filesystem(c) 0
388
389#define dbg_check_heap(c, heap, cat, add_pos) ({}) 393#define dbg_check_heap(c, heap, cat, add_pos) ({})
390
391#define dbg_check_lprops(c) 0 394#define dbg_check_lprops(c) 0
392#define dbg_check_lpt_nodes(c, cnode, row, col) 0 395#define dbg_check_lpt_nodes(c, cnode, row, col) 0
393
394#define dbg_force_in_the_gaps_enabled 0 396#define dbg_force_in_the_gaps_enabled 0
395#define dbg_force_in_the_gaps() 0 397#define dbg_force_in_the_gaps() 0
396
397#define dbg_failure_mode 0 398#define dbg_failure_mode 0
398#define dbg_failure_mode_registration(c) ({}) 399#define dbg_failure_mode_registration(c) ({})
399#define dbg_failure_mode_deregistration(c) ({}) 400#define dbg_failure_mode_deregistration(c) ({})
diff --git a/fs/ubifs/dir.c b/fs/ubifs/dir.c
index e90374be7d3b..526c01ec8003 100644
--- a/fs/ubifs/dir.c
+++ b/fs/ubifs/dir.c
@@ -165,7 +165,6 @@ struct inode *ubifs_new_inode(struct ubifs_info *c, const struct inode *dir,
165 } 165 }
166 166
167 inode->i_ino = ++c->highest_inum; 167 inode->i_ino = ++c->highest_inum;
168 inode->i_generation = ++c->vfs_gen;
169 /* 168 /*
170 * The creation sequence number remains with this inode for its 169 * The creation sequence number remains with this inode for its
171 * lifetime. All nodes for this inode have a greater sequence number, 170 * lifetime. All nodes for this inode have a greater sequence number,
@@ -220,15 +219,7 @@ static struct dentry *ubifs_lookup(struct inode *dir, struct dentry *dentry,
220 219
221 err = ubifs_tnc_lookup_nm(c, &key, dent, &dentry->d_name); 220 err = ubifs_tnc_lookup_nm(c, &key, dent, &dentry->d_name);
222 if (err) { 221 if (err) {
223 /* 222 if (err == -ENOENT) {
224 * Do not hash the direntry if parent 'i_nlink' is zero, because
225 * this has side-effects - '->delete_inode()' call will not be
226 * called for the parent orphan inode, because 'd_count' of its
227 * direntry will stay 1 (it'll be negative direntry I guess)
228 * and prevent 'iput_final()' until the dentry is destroyed due
229 * to unmount or memory pressure.
230 */
231 if (err == -ENOENT && dir->i_nlink != 0) {
232 dbg_gen("not found"); 223 dbg_gen("not found");
233 goto done; 224 goto done;
234 } 225 }
@@ -435,7 +426,7 @@ static int ubifs_readdir(struct file *file, void *dirent, filldir_t filldir)
435 426
436 while (1) { 427 while (1) {
437 dbg_gen("feed '%s', ino %llu, new f_pos %#x", 428 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
438 dent->name, le64_to_cpu(dent->inum), 429 dent->name, (unsigned long long)le64_to_cpu(dent->inum),
439 key_hash_flash(c, &dent->key)); 430 key_hash_flash(c, &dent->key));
440 ubifs_assert(dent->ch.sqnum > ubifs_inode(dir)->creat_sqnum); 431 ubifs_assert(dent->ch.sqnum > ubifs_inode(dir)->creat_sqnum);
441 432
@@ -525,7 +516,7 @@ static int ubifs_link(struct dentry *old_dentry, struct inode *dir,
525 struct ubifs_inode *dir_ui = ubifs_inode(dir); 516 struct ubifs_inode *dir_ui = ubifs_inode(dir);
526 int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len); 517 int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len);
527 struct ubifs_budget_req req = { .new_dent = 1, .dirtied_ino = 2, 518 struct ubifs_budget_req req = { .new_dent = 1, .dirtied_ino = 2,
528 .dirtied_ino_d = ui->data_len }; 519 .dirtied_ino_d = ALIGN(ui->data_len, 8) };
529 520
530 /* 521 /*
531 * Budget request settings: new direntry, changing the target inode, 522 * Budget request settings: new direntry, changing the target inode,
@@ -596,7 +587,6 @@ static int ubifs_unlink(struct inode *dir, struct dentry *dentry)
596 if (err) { 587 if (err) {
597 if (err != -ENOSPC) 588 if (err != -ENOSPC)
598 return err; 589 return err;
599 err = 0;
600 budgeted = 0; 590 budgeted = 0;
601 } 591 }
602 592
@@ -727,8 +717,7 @@ static int ubifs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
727 struct ubifs_inode *dir_ui = ubifs_inode(dir); 717 struct ubifs_inode *dir_ui = ubifs_inode(dir);
728 struct ubifs_info *c = dir->i_sb->s_fs_info; 718 struct ubifs_info *c = dir->i_sb->s_fs_info;
729 int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len); 719 int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len);
730 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1, 720 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1 };
731 .dirtied_ino_d = 1 };
732 721
733 /* 722 /*
734 * Budget request settings: new inode, new direntry and changing parent 723 * Budget request settings: new inode, new direntry and changing parent
@@ -789,7 +778,8 @@ static int ubifs_mknod(struct inode *dir, struct dentry *dentry,
789 int sz_change = CALC_DENT_SIZE(dentry->d_name.len); 778 int sz_change = CALC_DENT_SIZE(dentry->d_name.len);
790 int err, devlen = 0; 779 int err, devlen = 0;
791 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1, 780 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
792 .new_ino_d = devlen, .dirtied_ino = 1 }; 781 .new_ino_d = ALIGN(devlen, 8),
782 .dirtied_ino = 1 };
793 783
794 /* 784 /*
795 * Budget request settings: new inode, new direntry and changing parent 785 * Budget request settings: new inode, new direntry and changing parent
@@ -863,7 +853,8 @@ static int ubifs_symlink(struct inode *dir, struct dentry *dentry,
863 int err, len = strlen(symname); 853 int err, len = strlen(symname);
864 int sz_change = CALC_DENT_SIZE(dentry->d_name.len); 854 int sz_change = CALC_DENT_SIZE(dentry->d_name.len);
865 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1, 855 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
866 .new_ino_d = len, .dirtied_ino = 1 }; 856 .new_ino_d = ALIGN(len, 8),
857 .dirtied_ino = 1 };
867 858
868 /* 859 /*
869 * Budget request settings: new inode, new direntry and changing parent 860 * Budget request settings: new inode, new direntry and changing parent
@@ -1012,7 +1003,7 @@ static int ubifs_rename(struct inode *old_dir, struct dentry *old_dentry,
1012 struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1, 1003 struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1,
1013 .dirtied_ino = 3 }; 1004 .dirtied_ino = 3 };
1014 struct ubifs_budget_req ino_req = { .dirtied_ino = 1, 1005 struct ubifs_budget_req ino_req = { .dirtied_ino = 1,
1015 .dirtied_ino_d = old_inode_ui->data_len }; 1006 .dirtied_ino_d = ALIGN(old_inode_ui->data_len, 8) };
1016 struct timespec time; 1007 struct timespec time;
1017 1008
1018 /* 1009 /*
diff --git a/fs/ubifs/file.c b/fs/ubifs/file.c
index 005a3b854d96..51cf511d44d9 100644
--- a/fs/ubifs/file.c
+++ b/fs/ubifs/file.c
@@ -53,6 +53,7 @@
53 53
54#include "ubifs.h" 54#include "ubifs.h"
55#include <linux/mount.h> 55#include <linux/mount.h>
56#include <linux/namei.h>
56 57
57static int read_block(struct inode *inode, void *addr, unsigned int block, 58static int read_block(struct inode *inode, void *addr, unsigned int block,
58 struct ubifs_data_node *dn) 59 struct ubifs_data_node *dn)
@@ -146,6 +147,12 @@ static int do_readpage(struct page *page)
146 err = ret; 147 err = ret;
147 if (err != -ENOENT) 148 if (err != -ENOENT)
148 break; 149 break;
150 } else if (block + 1 == beyond) {
151 int dlen = le32_to_cpu(dn->size);
152 int ilen = i_size & (UBIFS_BLOCK_SIZE - 1);
153
154 if (ilen && ilen < dlen)
155 memset(addr + ilen, 0, dlen - ilen);
149 } 156 }
150 } 157 }
151 if (++i >= UBIFS_BLOCKS_PER_PAGE) 158 if (++i >= UBIFS_BLOCKS_PER_PAGE)
@@ -576,8 +583,262 @@ out:
576 return copied; 583 return copied;
577} 584}
578 585
586/**
587 * populate_page - copy data nodes into a page for bulk-read.
588 * @c: UBIFS file-system description object
589 * @page: page
590 * @bu: bulk-read information
591 * @n: next zbranch slot
592 *
593 * This function returns %0 on success and a negative error code on failure.
594 */
595static int populate_page(struct ubifs_info *c, struct page *page,
596 struct bu_info *bu, int *n)
597{
598 int i = 0, nn = *n, offs = bu->zbranch[0].offs, hole = 0, read = 0;
599 struct inode *inode = page->mapping->host;
600 loff_t i_size = i_size_read(inode);
601 unsigned int page_block;
602 void *addr, *zaddr;
603 pgoff_t end_index;
604
605 dbg_gen("ino %lu, pg %lu, i_size %lld, flags %#lx",
606 inode->i_ino, page->index, i_size, page->flags);
607
608 addr = zaddr = kmap(page);
609
610 end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
611 if (!i_size || page->index > end_index) {
612 hole = 1;
613 memset(addr, 0, PAGE_CACHE_SIZE);
614 goto out_hole;
615 }
616
617 page_block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT;
618 while (1) {
619 int err, len, out_len, dlen;
620
621 if (nn >= bu->cnt) {
622 hole = 1;
623 memset(addr, 0, UBIFS_BLOCK_SIZE);
624 } else if (key_block(c, &bu->zbranch[nn].key) == page_block) {
625 struct ubifs_data_node *dn;
626
627 dn = bu->buf + (bu->zbranch[nn].offs - offs);
628
629 ubifs_assert(dn->ch.sqnum >
630 ubifs_inode(inode)->creat_sqnum);
631
632 len = le32_to_cpu(dn->size);
633 if (len <= 0 || len > UBIFS_BLOCK_SIZE)
634 goto out_err;
635
636 dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
637 out_len = UBIFS_BLOCK_SIZE;
638 err = ubifs_decompress(&dn->data, dlen, addr, &out_len,
639 le16_to_cpu(dn->compr_type));
640 if (err || len != out_len)
641 goto out_err;
642
643 if (len < UBIFS_BLOCK_SIZE)
644 memset(addr + len, 0, UBIFS_BLOCK_SIZE - len);
645
646 nn += 1;
647 read = (i << UBIFS_BLOCK_SHIFT) + len;
648 } else if (key_block(c, &bu->zbranch[nn].key) < page_block) {
649 nn += 1;
650 continue;
651 } else {
652 hole = 1;
653 memset(addr, 0, UBIFS_BLOCK_SIZE);
654 }
655 if (++i >= UBIFS_BLOCKS_PER_PAGE)
656 break;
657 addr += UBIFS_BLOCK_SIZE;
658 page_block += 1;
659 }
660
661 if (end_index == page->index) {
662 int len = i_size & (PAGE_CACHE_SIZE - 1);
663
664 if (len && len < read)
665 memset(zaddr + len, 0, read - len);
666 }
667
668out_hole:
669 if (hole) {
670 SetPageChecked(page);
671 dbg_gen("hole");
672 }
673
674 SetPageUptodate(page);
675 ClearPageError(page);
676 flush_dcache_page(page);
677 kunmap(page);
678 *n = nn;
679 return 0;
680
681out_err:
682 ClearPageUptodate(page);
683 SetPageError(page);
684 flush_dcache_page(page);
685 kunmap(page);
686 ubifs_err("bad data node (block %u, inode %lu)",
687 page_block, inode->i_ino);
688 return -EINVAL;
689}
690
691/**
692 * ubifs_do_bulk_read - do bulk-read.
693 * @c: UBIFS file-system description object
694 * @page1: first page
695 *
696 * This function returns %1 if the bulk-read is done, otherwise %0 is returned.
697 */
698static int ubifs_do_bulk_read(struct ubifs_info *c, struct page *page1)
699{
700 pgoff_t offset = page1->index, end_index;
701 struct address_space *mapping = page1->mapping;
702 struct inode *inode = mapping->host;
703 struct ubifs_inode *ui = ubifs_inode(inode);
704 struct bu_info *bu;
705 int err, page_idx, page_cnt, ret = 0, n = 0;
706 loff_t isize;
707
708 bu = kmalloc(sizeof(struct bu_info), GFP_NOFS);
709 if (!bu)
710 return 0;
711
712 bu->buf_len = c->bulk_read_buf_size;
713 bu->buf = kmalloc(bu->buf_len, GFP_NOFS);
714 if (!bu->buf)
715 goto out_free;
716
717 data_key_init(c, &bu->key, inode->i_ino,
718 offset << UBIFS_BLOCKS_PER_PAGE_SHIFT);
719
720 err = ubifs_tnc_get_bu_keys(c, bu);
721 if (err)
722 goto out_warn;
723
724 if (bu->eof) {
725 /* Turn off bulk-read at the end of the file */
726 ui->read_in_a_row = 1;
727 ui->bulk_read = 0;
728 }
729
730 page_cnt = bu->blk_cnt >> UBIFS_BLOCKS_PER_PAGE_SHIFT;
731 if (!page_cnt) {
732 /*
733 * This happens when there are multiple blocks per page and the
734 * blocks for the first page we are looking for, are not
735 * together. If all the pages were like this, bulk-read would
736 * reduce performance, so we turn it off for a while.
737 */
738 ui->read_in_a_row = 0;
739 ui->bulk_read = 0;
740 goto out_free;
741 }
742
743 if (bu->cnt) {
744 err = ubifs_tnc_bulk_read(c, bu);
745 if (err)
746 goto out_warn;
747 }
748
749 err = populate_page(c, page1, bu, &n);
750 if (err)
751 goto out_warn;
752
753 unlock_page(page1);
754 ret = 1;
755
756 isize = i_size_read(inode);
757 if (isize == 0)
758 goto out_free;
759 end_index = ((isize - 1) >> PAGE_CACHE_SHIFT);
760
761 for (page_idx = 1; page_idx < page_cnt; page_idx++) {
762 pgoff_t page_offset = offset + page_idx;
763 struct page *page;
764
765 if (page_offset > end_index)
766 break;
767 page = find_or_create_page(mapping, page_offset,
768 GFP_NOFS | __GFP_COLD);
769 if (!page)
770 break;
771 if (!PageUptodate(page))
772 err = populate_page(c, page, bu, &n);
773 unlock_page(page);
774 page_cache_release(page);
775 if (err)
776 break;
777 }
778
779 ui->last_page_read = offset + page_idx - 1;
780
781out_free:
782 kfree(bu->buf);
783 kfree(bu);
784 return ret;
785
786out_warn:
787 ubifs_warn("ignoring error %d and skipping bulk-read", err);
788 goto out_free;
789}
790
791/**
792 * ubifs_bulk_read - determine whether to bulk-read and, if so, do it.
793 * @page: page from which to start bulk-read.
794 *
795 * Some flash media are capable of reading sequentially at faster rates. UBIFS
796 * bulk-read facility is designed to take advantage of that, by reading in one
797 * go consecutive data nodes that are also located consecutively in the same
798 * LEB. This function returns %1 if a bulk-read is done and %0 otherwise.
799 */
800static int ubifs_bulk_read(struct page *page)
801{
802 struct inode *inode = page->mapping->host;
803 struct ubifs_info *c = inode->i_sb->s_fs_info;
804 struct ubifs_inode *ui = ubifs_inode(inode);
805 pgoff_t index = page->index, last_page_read = ui->last_page_read;
806 int ret = 0;
807
808 ui->last_page_read = index;
809
810 if (!c->bulk_read)
811 return 0;
812 /*
813 * Bulk-read is protected by ui_mutex, but it is an optimization, so
814 * don't bother if we cannot lock the mutex.
815 */
816 if (!mutex_trylock(&ui->ui_mutex))
817 return 0;
818 if (index != last_page_read + 1) {
819 /* Turn off bulk-read if we stop reading sequentially */
820 ui->read_in_a_row = 1;
821 if (ui->bulk_read)
822 ui->bulk_read = 0;
823 goto out_unlock;
824 }
825 if (!ui->bulk_read) {
826 ui->read_in_a_row += 1;
827 if (ui->read_in_a_row < 3)
828 goto out_unlock;
829 /* Three reads in a row, so switch on bulk-read */
830 ui->bulk_read = 1;
831 }
832 ret = ubifs_do_bulk_read(c, page);
833out_unlock:
834 mutex_unlock(&ui->ui_mutex);
835 return ret;
836}
837
579static int ubifs_readpage(struct file *file, struct page *page) 838static int ubifs_readpage(struct file *file, struct page *page)
580{ 839{
840 if (ubifs_bulk_read(page))
841 return 0;
581 do_readpage(page); 842 do_readpage(page);
582 unlock_page(page); 843 unlock_page(page);
583 return 0; 844 return 0;
@@ -792,7 +1053,7 @@ static int do_truncation(struct ubifs_info *c, struct inode *inode,
792 int err; 1053 int err;
793 struct ubifs_budget_req req; 1054 struct ubifs_budget_req req;
794 loff_t old_size = inode->i_size, new_size = attr->ia_size; 1055 loff_t old_size = inode->i_size, new_size = attr->ia_size;
795 int offset = new_size & (UBIFS_BLOCK_SIZE - 1); 1056 int offset = new_size & (UBIFS_BLOCK_SIZE - 1), budgeted = 1;
796 struct ubifs_inode *ui = ubifs_inode(inode); 1057 struct ubifs_inode *ui = ubifs_inode(inode);
797 1058
798 dbg_gen("ino %lu, size %lld -> %lld", inode->i_ino, old_size, new_size); 1059 dbg_gen("ino %lu, size %lld -> %lld", inode->i_ino, old_size, new_size);
@@ -810,8 +1071,15 @@ static int do_truncation(struct ubifs_info *c, struct inode *inode,
810 /* A funny way to budget for truncation node */ 1071 /* A funny way to budget for truncation node */
811 req.dirtied_ino_d = UBIFS_TRUN_NODE_SZ; 1072 req.dirtied_ino_d = UBIFS_TRUN_NODE_SZ;
812 err = ubifs_budget_space(c, &req); 1073 err = ubifs_budget_space(c, &req);
813 if (err) 1074 if (err) {
814 return err; 1075 /*
1076 * Treat truncations to zero as deletion and always allow them,
1077 * just like we do for '->unlink()'.
1078 */
1079 if (new_size || err != -ENOSPC)
1080 return err;
1081 budgeted = 0;
1082 }
815 1083
816 err = vmtruncate(inode, new_size); 1084 err = vmtruncate(inode, new_size);
817 if (err) 1085 if (err)
@@ -868,7 +1136,12 @@ static int do_truncation(struct ubifs_info *c, struct inode *inode,
868 err = ubifs_jnl_truncate(c, inode, old_size, new_size); 1136 err = ubifs_jnl_truncate(c, inode, old_size, new_size);
869 mutex_unlock(&ui->ui_mutex); 1137 mutex_unlock(&ui->ui_mutex);
870out_budg: 1138out_budg:
871 ubifs_release_budget(c, &req); 1139 if (budgeted)
1140 ubifs_release_budget(c, &req);
1141 else {
1142 c->nospace = c->nospace_rp = 0;
1143 smp_wmb();
1144 }
872 return err; 1145 return err;
873} 1146}
874 1147
@@ -889,7 +1162,7 @@ static int do_setattr(struct ubifs_info *c, struct inode *inode,
889 loff_t new_size = attr->ia_size; 1162 loff_t new_size = attr->ia_size;
890 struct ubifs_inode *ui = ubifs_inode(inode); 1163 struct ubifs_inode *ui = ubifs_inode(inode);
891 struct ubifs_budget_req req = { .dirtied_ino = 1, 1164 struct ubifs_budget_req req = { .dirtied_ino = 1,
892 .dirtied_ino_d = ui->data_len }; 1165 .dirtied_ino_d = ALIGN(ui->data_len, 8) };
893 1166
894 err = ubifs_budget_space(c, &req); 1167 err = ubifs_budget_space(c, &req);
895 if (err) 1168 if (err)
@@ -940,7 +1213,8 @@ int ubifs_setattr(struct dentry *dentry, struct iattr *attr)
940 struct inode *inode = dentry->d_inode; 1213 struct inode *inode = dentry->d_inode;
941 struct ubifs_info *c = inode->i_sb->s_fs_info; 1214 struct ubifs_info *c = inode->i_sb->s_fs_info;
942 1215
943 dbg_gen("ino %lu, ia_valid %#x", inode->i_ino, attr->ia_valid); 1216 dbg_gen("ino %lu, mode %#x, ia_valid %#x",
1217 inode->i_ino, inode->i_mode, attr->ia_valid);
944 err = inode_change_ok(inode, attr); 1218 err = inode_change_ok(inode, attr);
945 if (err) 1219 if (err)
946 return err; 1220 return err;
@@ -1050,7 +1324,7 @@ static int update_mctime(struct ubifs_info *c, struct inode *inode)
1050 if (mctime_update_needed(inode, &now)) { 1324 if (mctime_update_needed(inode, &now)) {
1051 int err, release; 1325 int err, release;
1052 struct ubifs_budget_req req = { .dirtied_ino = 1, 1326 struct ubifs_budget_req req = { .dirtied_ino = 1,
1053 .dirtied_ino_d = ui->data_len }; 1327 .dirtied_ino_d = ALIGN(ui->data_len, 8) };
1054 1328
1055 err = ubifs_budget_space(c, &req); 1329 err = ubifs_budget_space(c, &req);
1056 if (err) 1330 if (err)
@@ -1269,6 +1543,7 @@ struct file_operations ubifs_file_operations = {
1269 .fsync = ubifs_fsync, 1543 .fsync = ubifs_fsync,
1270 .unlocked_ioctl = ubifs_ioctl, 1544 .unlocked_ioctl = ubifs_ioctl,
1271 .splice_read = generic_file_splice_read, 1545 .splice_read = generic_file_splice_read,
1546 .splice_write = generic_file_splice_write,
1272#ifdef CONFIG_COMPAT 1547#ifdef CONFIG_COMPAT
1273 .compat_ioctl = ubifs_compat_ioctl, 1548 .compat_ioctl = ubifs_compat_ioctl,
1274#endif 1549#endif
diff --git a/fs/ubifs/find.c b/fs/ubifs/find.c
index 10394c548367..717d79c97c5e 100644
--- a/fs/ubifs/find.c
+++ b/fs/ubifs/find.c
@@ -211,14 +211,8 @@ static const struct ubifs_lprops *scan_for_dirty(struct ubifs_info *c,
211 * dirty index heap, and it falls-back to LPT scanning if the heaps are empty 211 * dirty index heap, and it falls-back to LPT scanning if the heaps are empty
212 * or do not have an LEB which satisfies the @min_space criteria. 212 * or do not have an LEB which satisfies the @min_space criteria.
213 * 213 *
214 * Note: 214 * Note, LEBs which have less than dead watermark of free + dirty space are
215 * o LEBs which have less than dead watermark of dirty space are never picked 215 * never picked by this function.
216 * by this function;
217 *
218 * Returns zero and the LEB properties of
219 * found dirty LEB in case of success, %-ENOSPC if no dirty LEB was found and a
220 * negative error code in case of other failures. The returned LEB is marked as
221 * "taken".
222 * 216 *
223 * The additional @pick_free argument controls if this function has to return a 217 * The additional @pick_free argument controls if this function has to return a
224 * free or freeable LEB if one is present. For example, GC must to set it to %1, 218 * free or freeable LEB if one is present. For example, GC must to set it to %1,
@@ -231,6 +225,10 @@ static const struct ubifs_lprops *scan_for_dirty(struct ubifs_info *c,
231 * 225 *
232 * In addition @pick_free is set to %2 by the recovery process in order to 226 * In addition @pick_free is set to %2 by the recovery process in order to
233 * recover gc_lnum in which case an index LEB must not be returned. 227 * recover gc_lnum in which case an index LEB must not be returned.
228 *
229 * This function returns zero and the LEB properties of found dirty LEB in case
230 * of success, %-ENOSPC if no dirty LEB was found and a negative error code in
231 * case of other failures. The returned LEB is marked as "taken".
234 */ 232 */
235int ubifs_find_dirty_leb(struct ubifs_info *c, struct ubifs_lprops *ret_lp, 233int ubifs_find_dirty_leb(struct ubifs_info *c, struct ubifs_lprops *ret_lp,
236 int min_space, int pick_free) 234 int min_space, int pick_free)
@@ -245,7 +243,7 @@ int ubifs_find_dirty_leb(struct ubifs_info *c, struct ubifs_lprops *ret_lp,
245 int lebs, rsvd_idx_lebs = 0; 243 int lebs, rsvd_idx_lebs = 0;
246 244
247 spin_lock(&c->space_lock); 245 spin_lock(&c->space_lock);
248 lebs = c->lst.empty_lebs; 246 lebs = c->lst.empty_lebs + c->idx_gc_cnt;
249 lebs += c->freeable_cnt - c->lst.taken_empty_lebs; 247 lebs += c->freeable_cnt - c->lst.taken_empty_lebs;
250 248
251 /* 249 /*
@@ -290,9 +288,14 @@ int ubifs_find_dirty_leb(struct ubifs_info *c, struct ubifs_lprops *ret_lp,
290 idx_lp = idx_heap->arr[0]; 288 idx_lp = idx_heap->arr[0];
291 sum = idx_lp->free + idx_lp->dirty; 289 sum = idx_lp->free + idx_lp->dirty;
292 /* 290 /*
293 * Since we reserve twice as more space for the index than it 291 * Since we reserve thrice as much space for the index than it
294 * actually takes, it does not make sense to pick indexing LEBs 292 * actually takes, it does not make sense to pick indexing LEBs
295 * with less than half LEB of dirty space. 293 * with less than, say, half LEB of dirty space. May be half is
294 * not the optimal boundary - this should be tested and
295 * checked. This boundary should determine how much we use
296 * in-the-gaps to consolidate the index comparing to how much
297 * we use garbage collector to consolidate it. The "half"
298 * criteria just feels to be fine.
296 */ 299 */
297 if (sum < min_space || sum < c->half_leb_size) 300 if (sum < min_space || sum < c->half_leb_size)
298 idx_lp = NULL; 301 idx_lp = NULL;
@@ -312,7 +315,7 @@ int ubifs_find_dirty_leb(struct ubifs_info *c, struct ubifs_lprops *ret_lp,
312 lp = idx_lp; 315 lp = idx_lp;
313 316
314 if (lp) { 317 if (lp) {
315 ubifs_assert(lp->dirty >= c->dead_wm); 318 ubifs_assert(lp->free + lp->dirty >= c->dead_wm);
316 goto found; 319 goto found;
317 } 320 }
318 321
@@ -504,7 +507,6 @@ int ubifs_find_free_space(struct ubifs_info *c, int min_space, int *free,
504 rsvd_idx_lebs = 0; 507 rsvd_idx_lebs = 0;
505 lebs = c->lst.empty_lebs + c->freeable_cnt + c->idx_gc_cnt - 508 lebs = c->lst.empty_lebs + c->freeable_cnt + c->idx_gc_cnt -
506 c->lst.taken_empty_lebs; 509 c->lst.taken_empty_lebs;
507 ubifs_assert(lebs + c->lst.idx_lebs >= c->min_idx_lebs);
508 if (rsvd_idx_lebs < lebs) 510 if (rsvd_idx_lebs < lebs)
509 /* 511 /*
510 * OK to allocate an empty LEB, but we still don't want to go 512 * OK to allocate an empty LEB, but we still don't want to go
@@ -899,11 +901,11 @@ static int get_idx_gc_leb(struct ubifs_info *c)
899 * it is needed now for this commit. 901 * it is needed now for this commit.
900 */ 902 */
901 lp = ubifs_lpt_lookup_dirty(c, lnum); 903 lp = ubifs_lpt_lookup_dirty(c, lnum);
902 if (unlikely(IS_ERR(lp))) 904 if (IS_ERR(lp))
903 return PTR_ERR(lp); 905 return PTR_ERR(lp);
904 lp = ubifs_change_lp(c, lp, LPROPS_NC, LPROPS_NC, 906 lp = ubifs_change_lp(c, lp, LPROPS_NC, LPROPS_NC,
905 lp->flags | LPROPS_INDEX, -1); 907 lp->flags | LPROPS_INDEX, -1);
906 if (unlikely(IS_ERR(lp))) 908 if (IS_ERR(lp))
907 return PTR_ERR(lp); 909 return PTR_ERR(lp);
908 dbg_find("LEB %d, dirty %d and free %d flags %#x", 910 dbg_find("LEB %d, dirty %d and free %d flags %#x",
909 lp->lnum, lp->dirty, lp->free, lp->flags); 911 lp->lnum, lp->dirty, lp->free, lp->flags);
diff --git a/fs/ubifs/gc.c b/fs/ubifs/gc.c
index d0f3dac29081..0bef6501d58a 100644
--- a/fs/ubifs/gc.c
+++ b/fs/ubifs/gc.c
@@ -96,6 +96,48 @@ static int switch_gc_head(struct ubifs_info *c)
96} 96}
97 97
98/** 98/**
99 * joinup - bring data nodes for an inode together.
100 * @c: UBIFS file-system description object
101 * @sleb: describes scanned LEB
102 * @inum: inode number
103 * @blk: block number
104 * @data: list to which to add data nodes
105 *
106 * This function looks at the first few nodes in the scanned LEB @sleb and adds
107 * them to @data if they are data nodes from @inum and have a larger block
108 * number than @blk. This function returns %0 on success and a negative error
109 * code on failure.
110 */
111static int joinup(struct ubifs_info *c, struct ubifs_scan_leb *sleb, ino_t inum,
112 unsigned int blk, struct list_head *data)
113{
114 int err, cnt = 6, lnum = sleb->lnum, offs;
115 struct ubifs_scan_node *snod, *tmp;
116 union ubifs_key *key;
117
118 list_for_each_entry_safe(snod, tmp, &sleb->nodes, list) {
119 key = &snod->key;
120 if (key_inum(c, key) == inum &&
121 key_type(c, key) == UBIFS_DATA_KEY &&
122 key_block(c, key) > blk) {
123 offs = snod->offs;
124 err = ubifs_tnc_has_node(c, key, 0, lnum, offs, 0);
125 if (err < 0)
126 return err;
127 list_del(&snod->list);
128 if (err) {
129 list_add_tail(&snod->list, data);
130 blk = key_block(c, key);
131 } else
132 kfree(snod);
133 cnt = 6;
134 } else if (--cnt == 0)
135 break;
136 }
137 return 0;
138}
139
140/**
99 * move_nodes - move nodes. 141 * move_nodes - move nodes.
100 * @c: UBIFS file-system description object 142 * @c: UBIFS file-system description object
101 * @sleb: describes nodes to move 143 * @sleb: describes nodes to move
@@ -116,16 +158,21 @@ static int switch_gc_head(struct ubifs_info *c)
116static int move_nodes(struct ubifs_info *c, struct ubifs_scan_leb *sleb) 158static int move_nodes(struct ubifs_info *c, struct ubifs_scan_leb *sleb)
117{ 159{
118 struct ubifs_scan_node *snod, *tmp; 160 struct ubifs_scan_node *snod, *tmp;
119 struct list_head large, medium, small; 161 struct list_head data, large, medium, small;
120 struct ubifs_wbuf *wbuf = &c->jheads[GCHD].wbuf; 162 struct ubifs_wbuf *wbuf = &c->jheads[GCHD].wbuf;
121 int avail, err, min = INT_MAX; 163 int avail, err, min = INT_MAX;
164 unsigned int blk = 0;
165 ino_t inum = 0;
122 166
167 INIT_LIST_HEAD(&data);
123 INIT_LIST_HEAD(&large); 168 INIT_LIST_HEAD(&large);
124 INIT_LIST_HEAD(&medium); 169 INIT_LIST_HEAD(&medium);
125 INIT_LIST_HEAD(&small); 170 INIT_LIST_HEAD(&small);
126 171
127 list_for_each_entry_safe(snod, tmp, &sleb->nodes, list) { 172 while (!list_empty(&sleb->nodes)) {
128 struct list_head *lst; 173 struct list_head *lst = sleb->nodes.next;
174
175 snod = list_entry(lst, struct ubifs_scan_node, list);
129 176
130 ubifs_assert(snod->type != UBIFS_IDX_NODE); 177 ubifs_assert(snod->type != UBIFS_IDX_NODE);
131 ubifs_assert(snod->type != UBIFS_REF_NODE); 178 ubifs_assert(snod->type != UBIFS_REF_NODE);
@@ -136,7 +183,6 @@ static int move_nodes(struct ubifs_info *c, struct ubifs_scan_leb *sleb)
136 if (err < 0) 183 if (err < 0)
137 goto out; 184 goto out;
138 185
139 lst = &snod->list;
140 list_del(lst); 186 list_del(lst);
141 if (!err) { 187 if (!err) {
142 /* The node is obsolete, remove it from the list */ 188 /* The node is obsolete, remove it from the list */
@@ -145,15 +191,30 @@ static int move_nodes(struct ubifs_info *c, struct ubifs_scan_leb *sleb)
145 } 191 }
146 192
147 /* 193 /*
148 * Sort the list of nodes so that large nodes go first, and 194 * Sort the list of nodes so that data nodes go first, large
149 * small nodes go last. 195 * nodes go second, and small nodes go last.
150 */ 196 */
151 if (snod->len > MEDIUM_NODE_WM) 197 if (key_type(c, &snod->key) == UBIFS_DATA_KEY) {
152 list_add(lst, &large); 198 if (inum != key_inum(c, &snod->key)) {
199 if (inum) {
200 /*
201 * Try to move data nodes from the same
202 * inode together.
203 */
204 err = joinup(c, sleb, inum, blk, &data);
205 if (err)
206 goto out;
207 }
208 inum = key_inum(c, &snod->key);
209 blk = key_block(c, &snod->key);
210 }
211 list_add_tail(lst, &data);
212 } else if (snod->len > MEDIUM_NODE_WM)
213 list_add_tail(lst, &large);
153 else if (snod->len > SMALL_NODE_WM) 214 else if (snod->len > SMALL_NODE_WM)
154 list_add(lst, &medium); 215 list_add_tail(lst, &medium);
155 else 216 else
156 list_add(lst, &small); 217 list_add_tail(lst, &small);
157 218
158 /* And find the smallest node */ 219 /* And find the smallest node */
159 if (snod->len < min) 220 if (snod->len < min)
@@ -164,6 +225,7 @@ static int move_nodes(struct ubifs_info *c, struct ubifs_scan_leb *sleb)
164 * Join the tree lists so that we'd have one roughly sorted list 225 * Join the tree lists so that we'd have one roughly sorted list
165 * ('large' will be the head of the joined list). 226 * ('large' will be the head of the joined list).
166 */ 227 */
228 list_splice(&data, &large);
167 list_splice(&medium, large.prev); 229 list_splice(&medium, large.prev);
168 list_splice(&small, large.prev); 230 list_splice(&small, large.prev);
169 231
@@ -334,15 +396,21 @@ int ubifs_garbage_collect_leb(struct ubifs_info *c, struct ubifs_lprops *lp)
334 396
335 err = move_nodes(c, sleb); 397 err = move_nodes(c, sleb);
336 if (err) 398 if (err)
337 goto out; 399 goto out_inc_seq;
338 400
339 err = gc_sync_wbufs(c); 401 err = gc_sync_wbufs(c);
340 if (err) 402 if (err)
341 goto out; 403 goto out_inc_seq;
342 404
343 err = ubifs_change_one_lp(c, lnum, c->leb_size, 0, 0, 0, 0); 405 err = ubifs_change_one_lp(c, lnum, c->leb_size, 0, 0, 0, 0);
344 if (err) 406 if (err)
345 goto out; 407 goto out_inc_seq;
408
409 /* Allow for races with TNC */
410 c->gced_lnum = lnum;
411 smp_wmb();
412 c->gc_seq += 1;
413 smp_wmb();
346 414
347 if (c->gc_lnum == -1) { 415 if (c->gc_lnum == -1) {
348 c->gc_lnum = lnum; 416 c->gc_lnum = lnum;
@@ -363,6 +431,14 @@ int ubifs_garbage_collect_leb(struct ubifs_info *c, struct ubifs_lprops *lp)
363out: 431out:
364 ubifs_scan_destroy(sleb); 432 ubifs_scan_destroy(sleb);
365 return err; 433 return err;
434
435out_inc_seq:
436 /* We may have moved at least some nodes so allow for races with TNC */
437 c->gced_lnum = lnum;
438 smp_wmb();
439 c->gc_seq += 1;
440 smp_wmb();
441 goto out;
366} 442}
367 443
368/** 444/**
@@ -639,7 +715,7 @@ int ubifs_gc_start_commit(struct ubifs_info *c)
639 */ 715 */
640 while (1) { 716 while (1) {
641 lp = ubifs_fast_find_freeable(c); 717 lp = ubifs_fast_find_freeable(c);
642 if (unlikely(IS_ERR(lp))) { 718 if (IS_ERR(lp)) {
643 err = PTR_ERR(lp); 719 err = PTR_ERR(lp);
644 goto out; 720 goto out;
645 } 721 }
@@ -651,7 +727,7 @@ int ubifs_gc_start_commit(struct ubifs_info *c)
651 if (err) 727 if (err)
652 goto out; 728 goto out;
653 lp = ubifs_change_lp(c, lp, c->leb_size, 0, lp->flags, 0); 729 lp = ubifs_change_lp(c, lp, c->leb_size, 0, lp->flags, 0);
654 if (unlikely(IS_ERR(lp))) { 730 if (IS_ERR(lp)) {
655 err = PTR_ERR(lp); 731 err = PTR_ERR(lp);
656 goto out; 732 goto out;
657 } 733 }
@@ -666,7 +742,7 @@ int ubifs_gc_start_commit(struct ubifs_info *c)
666 /* Record index freeable LEBs for unmapping after commit */ 742 /* Record index freeable LEBs for unmapping after commit */
667 while (1) { 743 while (1) {
668 lp = ubifs_fast_find_frdi_idx(c); 744 lp = ubifs_fast_find_frdi_idx(c);
669 if (unlikely(IS_ERR(lp))) { 745 if (IS_ERR(lp)) {
670 err = PTR_ERR(lp); 746 err = PTR_ERR(lp);
671 goto out; 747 goto out;
672 } 748 }
@@ -682,7 +758,7 @@ int ubifs_gc_start_commit(struct ubifs_info *c)
682 /* Don't release the LEB until after the next commit */ 758 /* Don't release the LEB until after the next commit */
683 flags = (lp->flags | LPROPS_TAKEN) ^ LPROPS_INDEX; 759 flags = (lp->flags | LPROPS_TAKEN) ^ LPROPS_INDEX;
684 lp = ubifs_change_lp(c, lp, c->leb_size, 0, flags, 1); 760 lp = ubifs_change_lp(c, lp, c->leb_size, 0, flags, 1);
685 if (unlikely(IS_ERR(lp))) { 761 if (IS_ERR(lp)) {
686 err = PTR_ERR(lp); 762 err = PTR_ERR(lp);
687 kfree(idx_gc); 763 kfree(idx_gc);
688 goto out; 764 goto out;
diff --git a/fs/ubifs/io.c b/fs/ubifs/io.c
index 3374f91b6709..01682713af69 100644
--- a/fs/ubifs/io.c
+++ b/fs/ubifs/io.c
@@ -54,12 +54,28 @@
54#include "ubifs.h" 54#include "ubifs.h"
55 55
56/** 56/**
57 * ubifs_ro_mode - switch UBIFS to read read-only mode.
58 * @c: UBIFS file-system description object
59 * @err: error code which is the reason of switching to R/O mode
60 */
61void ubifs_ro_mode(struct ubifs_info *c, int err)
62{
63 if (!c->ro_media) {
64 c->ro_media = 1;
65 c->no_chk_data_crc = 0;
66 ubifs_warn("switched to read-only mode, error %d", err);
67 dbg_dump_stack();
68 }
69}
70
71/**
57 * ubifs_check_node - check node. 72 * ubifs_check_node - check node.
58 * @c: UBIFS file-system description object 73 * @c: UBIFS file-system description object
59 * @buf: node to check 74 * @buf: node to check
60 * @lnum: logical eraseblock number 75 * @lnum: logical eraseblock number
61 * @offs: offset within the logical eraseblock 76 * @offs: offset within the logical eraseblock
62 * @quiet: print no messages 77 * @quiet: print no messages
78 * @chk_crc: indicates whether to always check the CRC
63 * 79 *
64 * 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
65 * 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
@@ -71,7 +87,7 @@
71 * or magic. 87 * or magic.
72 */ 88 */
73int ubifs_check_node(const struct ubifs_info *c, const void *buf, int lnum, 89int ubifs_check_node(const struct ubifs_info *c, const void *buf, int lnum,
74 int offs, int quiet) 90 int offs, int quiet, int chk_crc)
75{ 91{
76 int err = -EINVAL, type, node_len; 92 int err = -EINVAL, type, node_len;
77 uint32_t crc, node_crc, magic; 93 uint32_t crc, node_crc, magic;
@@ -107,6 +123,10 @@ int ubifs_check_node(const struct ubifs_info *c, const void *buf, int lnum,
107 node_len > c->ranges[type].max_len) 123 node_len > c->ranges[type].max_len)
108 goto out_len; 124 goto out_len;
109 125
126 if (!chk_crc && type == UBIFS_DATA_NODE && !c->always_chk_crc)
127 if (c->no_chk_data_crc)
128 return 0;
129
110 crc = crc32(UBIFS_CRC32_INIT, buf + 8, node_len - 8); 130 crc = crc32(UBIFS_CRC32_INIT, buf + 8, node_len - 8);
111 node_crc = le32_to_cpu(ch->crc); 131 node_crc = le32_to_cpu(ch->crc);
112 if (crc != node_crc) { 132 if (crc != node_crc) {
@@ -708,7 +728,7 @@ int ubifs_read_node_wbuf(struct ubifs_wbuf *wbuf, void *buf, int type, int len,
708 goto out; 728 goto out;
709 } 729 }
710 730
711 err = ubifs_check_node(c, buf, lnum, offs, 0); 731 err = ubifs_check_node(c, buf, lnum, offs, 0, 0);
712 if (err) { 732 if (err) {
713 ubifs_err("expected node type %d", type); 733 ubifs_err("expected node type %d", type);
714 return err; 734 return err;
@@ -767,7 +787,7 @@ int ubifs_read_node(const struct ubifs_info *c, void *buf, int type, int len,
767 goto out; 787 goto out;
768 } 788 }
769 789
770 err = ubifs_check_node(c, buf, lnum, offs, 0); 790 err = ubifs_check_node(c, buf, lnum, offs, 0, 0);
771 if (err) { 791 if (err) {
772 ubifs_err("expected node type %d", type); 792 ubifs_err("expected node type %d", type);
773 return err; 793 return err;
diff --git a/fs/ubifs/journal.c b/fs/ubifs/journal.c
index 283155abe5f5..22993f867d19 100644
--- a/fs/ubifs/journal.c
+++ b/fs/ubifs/journal.c
@@ -447,13 +447,11 @@ static int get_dent_type(int mode)
447 * @ino: buffer in which to pack inode node 447 * @ino: buffer in which to pack inode node
448 * @inode: inode to pack 448 * @inode: inode to pack
449 * @last: indicates the last node of the group 449 * @last: indicates the last node of the group
450 * @last_reference: non-zero if this is a deletion inode
451 */ 450 */
452static void pack_inode(struct ubifs_info *c, struct ubifs_ino_node *ino, 451static void pack_inode(struct ubifs_info *c, struct ubifs_ino_node *ino,
453 const struct inode *inode, int last, 452 const struct inode *inode, int last)
454 int last_reference)
455{ 453{
456 int data_len = 0; 454 int data_len = 0, last_reference = !inode->i_nlink;
457 struct ubifs_inode *ui = ubifs_inode(inode); 455 struct ubifs_inode *ui = ubifs_inode(inode);
458 456
459 ino->ch.node_type = UBIFS_INO_NODE; 457 ino->ch.node_type = UBIFS_INO_NODE;
@@ -596,9 +594,9 @@ int ubifs_jnl_update(struct ubifs_info *c, const struct inode *dir,
596 ubifs_prep_grp_node(c, dent, dlen, 0); 594 ubifs_prep_grp_node(c, dent, dlen, 0);
597 595
598 ino = (void *)dent + aligned_dlen; 596 ino = (void *)dent + aligned_dlen;
599 pack_inode(c, ino, inode, 0, last_reference); 597 pack_inode(c, ino, inode, 0);
600 ino = (void *)ino + aligned_ilen; 598 ino = (void *)ino + aligned_ilen;
601 pack_inode(c, ino, dir, 1, 0); 599 pack_inode(c, ino, dir, 1);
602 600
603 if (last_reference) { 601 if (last_reference) {
604 err = ubifs_add_orphan(c, inode->i_ino); 602 err = ubifs_add_orphan(c, inode->i_ino);
@@ -606,6 +604,7 @@ int ubifs_jnl_update(struct ubifs_info *c, const struct inode *dir,
606 release_head(c, BASEHD); 604 release_head(c, BASEHD);
607 goto out_finish; 605 goto out_finish;
608 } 606 }
607 ui->del_cmtno = c->cmt_no;
609 } 608 }
610 609
611 err = write_head(c, BASEHD, dent, len, &lnum, &dent_offs, sync); 610 err = write_head(c, BASEHD, dent, len, &lnum, &dent_offs, sync);
@@ -750,30 +749,25 @@ out_free:
750 * ubifs_jnl_write_inode - flush inode to the journal. 749 * ubifs_jnl_write_inode - flush inode to the journal.
751 * @c: UBIFS file-system description object 750 * @c: UBIFS file-system description object
752 * @inode: inode to flush 751 * @inode: inode to flush
753 * @deletion: inode has been deleted
754 * 752 *
755 * This function writes inode @inode to the journal. If the inode is 753 * This function writes inode @inode to the journal. If the inode is
756 * synchronous, it also synchronizes the write-buffer. Returns zero in case of 754 * synchronous, it also synchronizes the write-buffer. Returns zero in case of
757 * success and a negative error code in case of failure. 755 * success and a negative error code in case of failure.
758 */ 756 */
759int ubifs_jnl_write_inode(struct ubifs_info *c, const struct inode *inode, 757int ubifs_jnl_write_inode(struct ubifs_info *c, const struct inode *inode)
760 int deletion)
761{ 758{
762 int err, len, lnum, offs, sync = 0; 759 int err, lnum, offs;
763 struct ubifs_ino_node *ino; 760 struct ubifs_ino_node *ino;
764 struct ubifs_inode *ui = ubifs_inode(inode); 761 struct ubifs_inode *ui = ubifs_inode(inode);
762 int sync = 0, len = UBIFS_INO_NODE_SZ, last_reference = !inode->i_nlink;
765 763
766 dbg_jnl("ino %lu%s", inode->i_ino, 764 dbg_jnl("ino %lu, nlink %u", inode->i_ino, inode->i_nlink);
767 deletion ? " (last reference)" : "");
768 if (deletion)
769 ubifs_assert(inode->i_nlink == 0);
770 765
771 len = UBIFS_INO_NODE_SZ;
772 /* 766 /*
773 * If the inode is being deleted, do not write the attached data. No 767 * If the inode is being deleted, do not write the attached data. No
774 * need to synchronize the write-buffer either. 768 * need to synchronize the write-buffer either.
775 */ 769 */
776 if (!deletion) { 770 if (!last_reference) {
777 len += ui->data_len; 771 len += ui->data_len;
778 sync = IS_SYNC(inode); 772 sync = IS_SYNC(inode);
779 } 773 }
@@ -786,7 +780,7 @@ int ubifs_jnl_write_inode(struct ubifs_info *c, const struct inode *inode,
786 if (err) 780 if (err)
787 goto out_free; 781 goto out_free;
788 782
789 pack_inode(c, ino, inode, 1, deletion); 783 pack_inode(c, ino, inode, 1);
790 err = write_head(c, BASEHD, ino, len, &lnum, &offs, sync); 784 err = write_head(c, BASEHD, ino, len, &lnum, &offs, sync);
791 if (err) 785 if (err)
792 goto out_release; 786 goto out_release;
@@ -795,7 +789,7 @@ int ubifs_jnl_write_inode(struct ubifs_info *c, const struct inode *inode,
795 inode->i_ino); 789 inode->i_ino);
796 release_head(c, BASEHD); 790 release_head(c, BASEHD);
797 791
798 if (deletion) { 792 if (last_reference) {
799 err = ubifs_tnc_remove_ino(c, inode->i_ino); 793 err = ubifs_tnc_remove_ino(c, inode->i_ino);
800 if (err) 794 if (err)
801 goto out_ro; 795 goto out_ro;
@@ -828,6 +822,65 @@ out_free:
828} 822}
829 823
830/** 824/**
825 * ubifs_jnl_delete_inode - delete an inode.
826 * @c: UBIFS file-system description object
827 * @inode: inode to delete
828 *
829 * This function deletes inode @inode which includes removing it from orphans,
830 * deleting it from TNC and, in some cases, writing a deletion inode to the
831 * journal.
832 *
833 * When regular file inodes are unlinked or a directory inode is removed, the
834 * 'ubifs_jnl_update()' function writes a corresponding deletion inode and
835 * direntry to the media, and adds the inode to orphans. After this, when the
836 * last reference to this inode has been dropped, this function is called. In
837 * general, it has to write one more deletion inode to the media, because if
838 * a commit happened between 'ubifs_jnl_update()' and
839 * 'ubifs_jnl_delete_inode()', the deletion inode is not in the journal
840 * anymore, and in fact it might not be on the flash anymore, because it might
841 * have been garbage-collected already. And for optimization reasons UBIFS does
842 * not read the orphan area if it has been unmounted cleanly, so it would have
843 * no indication in the journal that there is a deleted inode which has to be
844 * removed from TNC.
845 *
846 * However, if there was no commit between 'ubifs_jnl_update()' and
847 * 'ubifs_jnl_delete_inode()', then there is no need to write the deletion
848 * inode to the media for the second time. And this is quite a typical case.
849 *
850 * This function returns zero in case of success and a negative error code in
851 * case of failure.
852 */
853int ubifs_jnl_delete_inode(struct ubifs_info *c, const struct inode *inode)
854{
855 int err;
856 struct ubifs_inode *ui = ubifs_inode(inode);
857
858 ubifs_assert(inode->i_nlink == 0);
859
860 if (ui->del_cmtno != c->cmt_no)
861 /* A commit happened for sure */
862 return ubifs_jnl_write_inode(c, inode);
863
864 down_read(&c->commit_sem);
865 /*
866 * Check commit number again, because the first test has been done
867 * without @c->commit_sem, so a commit might have happened.
868 */
869 if (ui->del_cmtno != c->cmt_no) {
870 up_read(&c->commit_sem);
871 return ubifs_jnl_write_inode(c, inode);
872 }
873
874 err = ubifs_tnc_remove_ino(c, inode->i_ino);
875 if (err)
876 ubifs_ro_mode(c, err);
877 else
878 ubifs_delete_orphan(c, inode->i_ino);
879 up_read(&c->commit_sem);
880 return err;
881}
882
883/**
831 * ubifs_jnl_rename - rename a directory entry. 884 * ubifs_jnl_rename - rename a directory entry.
832 * @c: UBIFS file-system description object 885 * @c: UBIFS file-system description object
833 * @old_dir: parent inode of directory entry to rename 886 * @old_dir: parent inode of directory entry to rename
@@ -917,16 +970,16 @@ int ubifs_jnl_rename(struct ubifs_info *c, const struct inode *old_dir,
917 970
918 p = (void *)dent2 + aligned_dlen2; 971 p = (void *)dent2 + aligned_dlen2;
919 if (new_inode) { 972 if (new_inode) {
920 pack_inode(c, p, new_inode, 0, last_reference); 973 pack_inode(c, p, new_inode, 0);
921 p += ALIGN(ilen, 8); 974 p += ALIGN(ilen, 8);
922 } 975 }
923 976
924 if (!move) 977 if (!move)
925 pack_inode(c, p, old_dir, 1, 0); 978 pack_inode(c, p, old_dir, 1);
926 else { 979 else {
927 pack_inode(c, p, old_dir, 0, 0); 980 pack_inode(c, p, old_dir, 0);
928 p += ALIGN(plen, 8); 981 p += ALIGN(plen, 8);
929 pack_inode(c, p, new_dir, 1, 0); 982 pack_inode(c, p, new_dir, 1);
930 } 983 }
931 984
932 if (last_reference) { 985 if (last_reference) {
@@ -935,6 +988,7 @@ int ubifs_jnl_rename(struct ubifs_info *c, const struct inode *old_dir,
935 release_head(c, BASEHD); 988 release_head(c, BASEHD);
936 goto out_finish; 989 goto out_finish;
937 } 990 }
991 new_ui->del_cmtno = c->cmt_no;
938 } 992 }
939 993
940 err = write_head(c, BASEHD, dent, len, &lnum, &offs, sync); 994 err = write_head(c, BASEHD, dent, len, &lnum, &offs, sync);
@@ -1131,7 +1185,7 @@ int ubifs_jnl_truncate(struct ubifs_info *c, const struct inode *inode,
1131 if (err) 1185 if (err)
1132 goto out_free; 1186 goto out_free;
1133 1187
1134 pack_inode(c, ino, inode, 0, 0); 1188 pack_inode(c, ino, inode, 0);
1135 ubifs_prep_grp_node(c, trun, UBIFS_TRUN_NODE_SZ, dlen ? 0 : 1); 1189 ubifs_prep_grp_node(c, trun, UBIFS_TRUN_NODE_SZ, dlen ? 0 : 1);
1136 if (dlen) 1190 if (dlen)
1137 ubifs_prep_grp_node(c, dn, dlen, 1); 1191 ubifs_prep_grp_node(c, dn, dlen, 1);
@@ -1251,9 +1305,9 @@ int ubifs_jnl_delete_xattr(struct ubifs_info *c, const struct inode *host,
1251 ubifs_prep_grp_node(c, xent, xlen, 0); 1305 ubifs_prep_grp_node(c, xent, xlen, 0);
1252 1306
1253 ino = (void *)xent + aligned_xlen; 1307 ino = (void *)xent + aligned_xlen;
1254 pack_inode(c, ino, inode, 0, 1); 1308 pack_inode(c, ino, inode, 0);
1255 ino = (void *)ino + UBIFS_INO_NODE_SZ; 1309 ino = (void *)ino + UBIFS_INO_NODE_SZ;
1256 pack_inode(c, ino, host, 1, 0); 1310 pack_inode(c, ino, host, 1);
1257 1311
1258 err = write_head(c, BASEHD, xent, len, &lnum, &xent_offs, sync); 1312 err = write_head(c, BASEHD, xent, len, &lnum, &xent_offs, sync);
1259 if (!sync && !err) 1313 if (!sync && !err)
@@ -1320,7 +1374,7 @@ int ubifs_jnl_change_xattr(struct ubifs_info *c, const struct inode *inode,
1320 const struct inode *host) 1374 const struct inode *host)
1321{ 1375{
1322 int err, len1, len2, aligned_len, aligned_len1, lnum, offs; 1376 int err, len1, len2, aligned_len, aligned_len1, lnum, offs;
1323 struct ubifs_inode *host_ui = ubifs_inode(inode); 1377 struct ubifs_inode *host_ui = ubifs_inode(host);
1324 struct ubifs_ino_node *ino; 1378 struct ubifs_ino_node *ino;
1325 union ubifs_key key; 1379 union ubifs_key key;
1326 int sync = IS_DIRSYNC(host); 1380 int sync = IS_DIRSYNC(host);
@@ -1344,8 +1398,8 @@ int ubifs_jnl_change_xattr(struct ubifs_info *c, const struct inode *inode,
1344 if (err) 1398 if (err)
1345 goto out_free; 1399 goto out_free;
1346 1400
1347 pack_inode(c, ino, host, 0, 0); 1401 pack_inode(c, ino, host, 0);
1348 pack_inode(c, (void *)ino + aligned_len1, inode, 1, 0); 1402 pack_inode(c, (void *)ino + aligned_len1, inode, 1);
1349 1403
1350 err = write_head(c, BASEHD, ino, aligned_len, &lnum, &offs, 0); 1404 err = write_head(c, BASEHD, ino, aligned_len, &lnum, &offs, 0);
1351 if (!sync && !err) { 1405 if (!sync && !err) {
diff --git a/fs/ubifs/key.h b/fs/ubifs/key.h
index 8f7476007549..9ee65086f627 100644
--- a/fs/ubifs/key.h
+++ b/fs/ubifs/key.h
@@ -484,7 +484,7 @@ static inline void key_copy(const struct ubifs_info *c,
484 * @key2: the second key to compare 484 * @key2: the second key to compare
485 * 485 *
486 * This function compares 2 keys and returns %-1 if @key1 is less than 486 * This function compares 2 keys and returns %-1 if @key1 is less than
487 * @key2, 0 if the keys are equivalent and %1 if @key1 is greater than @key2. 487 * @key2, %0 if the keys are equivalent and %1 if @key1 is greater than @key2.
488 */ 488 */
489static inline int keys_cmp(const struct ubifs_info *c, 489static inline int keys_cmp(const struct ubifs_info *c,
490 const union ubifs_key *key1, 490 const union ubifs_key *key1,
@@ -503,6 +503,26 @@ static inline int keys_cmp(const struct ubifs_info *c,
503} 503}
504 504
505/** 505/**
506 * keys_eq - determine if keys are equivalent.
507 * @c: UBIFS file-system description object
508 * @key1: the first key to compare
509 * @key2: the second key to compare
510 *
511 * This function compares 2 keys and returns %1 if @key1 is equal to @key2 and
512 * %0 if not.
513 */
514static inline int keys_eq(const struct ubifs_info *c,
515 const union ubifs_key *key1,
516 const union ubifs_key *key2)
517{
518 if (key1->u32[0] != key2->u32[0])
519 return 0;
520 if (key1->u32[1] != key2->u32[1])
521 return 0;
522 return 1;
523}
524
525/**
506 * is_hash_key - is a key vulnerable to hash collisions. 526 * is_hash_key - is a key vulnerable to hash collisions.
507 * @c: UBIFS file-system description object 527 * @c: UBIFS file-system description object
508 * @key: key 528 * @key: key
diff --git a/fs/ubifs/log.c b/fs/ubifs/log.c
index 36857b9ed59e..3e0aa7367556 100644
--- a/fs/ubifs/log.c
+++ b/fs/ubifs/log.c
@@ -317,6 +317,8 @@ int ubifs_add_bud_to_log(struct ubifs_info *c, int jhead, int lnum, int offs)
317 return 0; 317 return 0;
318 318
319out_unlock: 319out_unlock:
320 if (err != -EAGAIN)
321 ubifs_ro_mode(c, err);
320 mutex_unlock(&c->log_mutex); 322 mutex_unlock(&c->log_mutex);
321 kfree(ref); 323 kfree(ref);
322 kfree(bud); 324 kfree(bud);
@@ -410,7 +412,7 @@ int ubifs_log_start_commit(struct ubifs_info *c, int *ltail_lnum)
410 return -ENOMEM; 412 return -ENOMEM;
411 413
412 cs->ch.node_type = UBIFS_CS_NODE; 414 cs->ch.node_type = UBIFS_CS_NODE;
413 cs->cmt_no = cpu_to_le64(c->cmt_no + 1); 415 cs->cmt_no = cpu_to_le64(c->cmt_no);
414 ubifs_prepare_node(c, cs, UBIFS_CS_NODE_SZ, 0); 416 ubifs_prepare_node(c, cs, UBIFS_CS_NODE_SZ, 0);
415 417
416 /* 418 /*
diff --git a/fs/ubifs/lprops.c b/fs/ubifs/lprops.c
index 2ba93da71b65..f27176e9b70d 100644
--- a/fs/ubifs/lprops.c
+++ b/fs/ubifs/lprops.c
@@ -125,6 +125,7 @@ static void adjust_lpt_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap,
125 } 125 }
126 } 126 }
127 } 127 }
128
128 /* Not greater than parent, so compare to children */ 129 /* Not greater than parent, so compare to children */
129 while (1) { 130 while (1) {
130 /* Compare to left child */ 131 /* Compare to left child */
@@ -460,18 +461,6 @@ static void change_category(struct ubifs_info *c, struct ubifs_lprops *lprops)
460} 461}
461 462
462/** 463/**
463 * ubifs_get_lprops - get reference to LEB properties.
464 * @c: the UBIFS file-system description object
465 *
466 * This function locks lprops. Lprops have to be unlocked by
467 * 'ubifs_release_lprops()'.
468 */
469void ubifs_get_lprops(struct ubifs_info *c)
470{
471 mutex_lock(&c->lp_mutex);
472}
473
474/**
475 * calc_dark - calculate LEB dark space size. 464 * calc_dark - calculate LEB dark space size.
476 * @c: the UBIFS file-system description object 465 * @c: the UBIFS file-system description object
477 * @spc: amount of free and dirty space in the LEB 466 * @spc: amount of free and dirty space in the LEB
@@ -576,7 +565,6 @@ const struct ubifs_lprops *ubifs_change_lp(struct ubifs_info *c,
576 ubifs_assert(!(lprops->free & 7) && !(lprops->dirty & 7)); 565 ubifs_assert(!(lprops->free & 7) && !(lprops->dirty & 7));
577 566
578 spin_lock(&c->space_lock); 567 spin_lock(&c->space_lock);
579
580 if ((lprops->flags & LPROPS_TAKEN) && lprops->free == c->leb_size) 568 if ((lprops->flags & LPROPS_TAKEN) && lprops->free == c->leb_size)
581 c->lst.taken_empty_lebs -= 1; 569 c->lst.taken_empty_lebs -= 1;
582 570
@@ -637,31 +625,12 @@ const struct ubifs_lprops *ubifs_change_lp(struct ubifs_info *c,
637 c->lst.taken_empty_lebs += 1; 625 c->lst.taken_empty_lebs += 1;
638 626
639 change_category(c, lprops); 627 change_category(c, lprops);
640
641 c->idx_gc_cnt += idx_gc_cnt; 628 c->idx_gc_cnt += idx_gc_cnt;
642
643 spin_unlock(&c->space_lock); 629 spin_unlock(&c->space_lock);
644
645 return lprops; 630 return lprops;
646} 631}
647 632
648/** 633/**
649 * ubifs_release_lprops - release lprops lock.
650 * @c: the UBIFS file-system description object
651 *
652 * This function has to be called after each 'ubifs_get_lprops()' call to
653 * unlock lprops.
654 */
655void ubifs_release_lprops(struct ubifs_info *c)
656{
657 ubifs_assert(mutex_is_locked(&c->lp_mutex));
658 ubifs_assert(c->lst.empty_lebs >= 0 &&
659 c->lst.empty_lebs <= c->main_lebs);
660
661 mutex_unlock(&c->lp_mutex);
662}
663
664/**
665 * ubifs_get_lp_stats - get lprops statistics. 634 * ubifs_get_lp_stats - get lprops statistics.
666 * @c: UBIFS file-system description object 635 * @c: UBIFS file-system description object
667 * @st: return statistics 636 * @st: return statistics
@@ -1262,7 +1231,6 @@ static int scan_check_cb(struct ubifs_info *c,
1262 } 1231 }
1263 1232
1264 ubifs_scan_destroy(sleb); 1233 ubifs_scan_destroy(sleb);
1265
1266 return LPT_SCAN_CONTINUE; 1234 return LPT_SCAN_CONTINUE;
1267 1235
1268out_print: 1236out_print:
diff --git a/fs/ubifs/lpt.c b/fs/ubifs/lpt.c
index 9ff2463177e5..db8bd0e518b2 100644
--- a/fs/ubifs/lpt.c
+++ b/fs/ubifs/lpt.c
@@ -109,7 +109,8 @@ static void do_calc_lpt_geom(struct ubifs_info *c)
109 c->lpt_sz = (long long)c->pnode_cnt * c->pnode_sz; 109 c->lpt_sz = (long long)c->pnode_cnt * c->pnode_sz;
110 c->lpt_sz += (long long)c->nnode_cnt * c->nnode_sz; 110 c->lpt_sz += (long long)c->nnode_cnt * c->nnode_sz;
111 c->lpt_sz += c->ltab_sz; 111 c->lpt_sz += c->ltab_sz;
112 c->lpt_sz += c->lsave_sz; 112 if (c->big_lpt)
113 c->lpt_sz += c->lsave_sz;
113 114
114 /* Add wastage */ 115 /* Add wastage */
115 sz = c->lpt_sz; 116 sz = c->lpt_sz;
@@ -287,25 +288,56 @@ uint32_t ubifs_unpack_bits(uint8_t **addr, int *pos, int nrbits)
287 const int k = 32 - nrbits; 288 const int k = 32 - nrbits;
288 uint8_t *p = *addr; 289 uint8_t *p = *addr;
289 int b = *pos; 290 int b = *pos;
290 uint32_t val; 291 uint32_t uninitialized_var(val);
292 const int bytes = (nrbits + b + 7) >> 3;
291 293
292 ubifs_assert(nrbits > 0); 294 ubifs_assert(nrbits > 0);
293 ubifs_assert(nrbits <= 32); 295 ubifs_assert(nrbits <= 32);
294 ubifs_assert(*pos >= 0); 296 ubifs_assert(*pos >= 0);
295 ubifs_assert(*pos < 8); 297 ubifs_assert(*pos < 8);
296 if (b) { 298 if (b) {
297 val = p[1] | ((uint32_t)p[2] << 8) | ((uint32_t)p[3] << 16) | 299 switch (bytes) {
298 ((uint32_t)p[4] << 24); 300 case 2:
301 val = p[1];
302 break;
303 case 3:
304 val = p[1] | ((uint32_t)p[2] << 8);
305 break;
306 case 4:
307 val = p[1] | ((uint32_t)p[2] << 8) |
308 ((uint32_t)p[3] << 16);
309 break;
310 case 5:
311 val = p[1] | ((uint32_t)p[2] << 8) |
312 ((uint32_t)p[3] << 16) |
313 ((uint32_t)p[4] << 24);
314 }
299 val <<= (8 - b); 315 val <<= (8 - b);
300 val |= *p >> b; 316 val |= *p >> b;
301 nrbits += b; 317 nrbits += b;
302 } else 318 } else {
303 val = p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | 319 switch (bytes) {
304 ((uint32_t)p[3] << 24); 320 case 1:
321 val = p[0];
322 break;
323 case 2:
324 val = p[0] | ((uint32_t)p[1] << 8);
325 break;
326 case 3:
327 val = p[0] | ((uint32_t)p[1] << 8) |
328 ((uint32_t)p[2] << 16);
329 break;
330 case 4:
331 val = p[0] | ((uint32_t)p[1] << 8) |
332 ((uint32_t)p[2] << 16) |
333 ((uint32_t)p[3] << 24);
334 break;
335 }
336 }
305 val <<= k; 337 val <<= k;
306 val >>= k; 338 val >>= k;
307 b = nrbits & 7; 339 b = nrbits & 7;
308 p += nrbits / 8; 340 p += nrbits >> 3;
309 *addr = p; 341 *addr = p;
310 *pos = b; 342 *pos = b;
311 ubifs_assert((val >> nrbits) == 0 || nrbits - b == 32); 343 ubifs_assert((val >> nrbits) == 0 || nrbits - b == 32);
diff --git a/fs/ubifs/lpt_commit.c b/fs/ubifs/lpt_commit.c
index 5f0b83e20af6..eed5a0025d63 100644
--- a/fs/ubifs/lpt_commit.c
+++ b/fs/ubifs/lpt_commit.c
@@ -177,8 +177,6 @@ static int alloc_lpt_leb(struct ubifs_info *c, int *lnum)
177 return 0; 177 return 0;
178 } 178 }
179 } 179 }
180 dbg_err("last LEB %d", *lnum);
181 dump_stack();
182 return -ENOSPC; 180 return -ENOSPC;
183} 181}
184 182
@@ -193,6 +191,9 @@ static int layout_cnodes(struct ubifs_info *c)
193 int lnum, offs, len, alen, done_lsave, done_ltab, err; 191 int lnum, offs, len, alen, done_lsave, done_ltab, err;
194 struct ubifs_cnode *cnode; 192 struct ubifs_cnode *cnode;
195 193
194 err = dbg_chk_lpt_sz(c, 0, 0);
195 if (err)
196 return err;
196 cnode = c->lpt_cnext; 197 cnode = c->lpt_cnext;
197 if (!cnode) 198 if (!cnode)
198 return 0; 199 return 0;
@@ -206,6 +207,7 @@ static int layout_cnodes(struct ubifs_info *c)
206 c->lsave_lnum = lnum; 207 c->lsave_lnum = lnum;
207 c->lsave_offs = offs; 208 c->lsave_offs = offs;
208 offs += c->lsave_sz; 209 offs += c->lsave_sz;
210 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
209 } 211 }
210 212
211 if (offs + c->ltab_sz <= c->leb_size) { 213 if (offs + c->ltab_sz <= c->leb_size) {
@@ -213,6 +215,7 @@ static int layout_cnodes(struct ubifs_info *c)
213 c->ltab_lnum = lnum; 215 c->ltab_lnum = lnum;
214 c->ltab_offs = offs; 216 c->ltab_offs = offs;
215 offs += c->ltab_sz; 217 offs += c->ltab_sz;
218 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
216 } 219 }
217 220
218 do { 221 do {
@@ -226,9 +229,10 @@ static int layout_cnodes(struct ubifs_info *c)
226 while (offs + len > c->leb_size) { 229 while (offs + len > c->leb_size) {
227 alen = ALIGN(offs, c->min_io_size); 230 alen = ALIGN(offs, c->min_io_size);
228 upd_ltab(c, lnum, c->leb_size - alen, alen - offs); 231 upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
232 dbg_chk_lpt_sz(c, 2, alen - offs);
229 err = alloc_lpt_leb(c, &lnum); 233 err = alloc_lpt_leb(c, &lnum);
230 if (err) 234 if (err)
231 return err; 235 goto no_space;
232 offs = 0; 236 offs = 0;
233 ubifs_assert(lnum >= c->lpt_first && 237 ubifs_assert(lnum >= c->lpt_first &&
234 lnum <= c->lpt_last); 238 lnum <= c->lpt_last);
@@ -238,6 +242,7 @@ static int layout_cnodes(struct ubifs_info *c)
238 c->lsave_lnum = lnum; 242 c->lsave_lnum = lnum;
239 c->lsave_offs = offs; 243 c->lsave_offs = offs;
240 offs += c->lsave_sz; 244 offs += c->lsave_sz;
245 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
241 continue; 246 continue;
242 } 247 }
243 if (!done_ltab) { 248 if (!done_ltab) {
@@ -245,6 +250,7 @@ static int layout_cnodes(struct ubifs_info *c)
245 c->ltab_lnum = lnum; 250 c->ltab_lnum = lnum;
246 c->ltab_offs = offs; 251 c->ltab_offs = offs;
247 offs += c->ltab_sz; 252 offs += c->ltab_sz;
253 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
248 continue; 254 continue;
249 } 255 }
250 break; 256 break;
@@ -257,6 +263,7 @@ static int layout_cnodes(struct ubifs_info *c)
257 c->lpt_offs = offs; 263 c->lpt_offs = offs;
258 } 264 }
259 offs += len; 265 offs += len;
266 dbg_chk_lpt_sz(c, 1, len);
260 cnode = cnode->cnext; 267 cnode = cnode->cnext;
261 } while (cnode && cnode != c->lpt_cnext); 268 } while (cnode && cnode != c->lpt_cnext);
262 269
@@ -265,9 +272,10 @@ static int layout_cnodes(struct ubifs_info *c)
265 if (offs + c->lsave_sz > c->leb_size) { 272 if (offs + c->lsave_sz > c->leb_size) {
266 alen = ALIGN(offs, c->min_io_size); 273 alen = ALIGN(offs, c->min_io_size);
267 upd_ltab(c, lnum, c->leb_size - alen, alen - offs); 274 upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
275 dbg_chk_lpt_sz(c, 2, alen - offs);
268 err = alloc_lpt_leb(c, &lnum); 276 err = alloc_lpt_leb(c, &lnum);
269 if (err) 277 if (err)
270 return err; 278 goto no_space;
271 offs = 0; 279 offs = 0;
272 ubifs_assert(lnum >= c->lpt_first && 280 ubifs_assert(lnum >= c->lpt_first &&
273 lnum <= c->lpt_last); 281 lnum <= c->lpt_last);
@@ -276,6 +284,7 @@ static int layout_cnodes(struct ubifs_info *c)
276 c->lsave_lnum = lnum; 284 c->lsave_lnum = lnum;
277 c->lsave_offs = offs; 285 c->lsave_offs = offs;
278 offs += c->lsave_sz; 286 offs += c->lsave_sz;
287 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
279 } 288 }
280 289
281 /* Make sure to place LPT's own lprops table */ 290 /* Make sure to place LPT's own lprops table */
@@ -283,9 +292,10 @@ static int layout_cnodes(struct ubifs_info *c)
283 if (offs + c->ltab_sz > c->leb_size) { 292 if (offs + c->ltab_sz > c->leb_size) {
284 alen = ALIGN(offs, c->min_io_size); 293 alen = ALIGN(offs, c->min_io_size);
285 upd_ltab(c, lnum, c->leb_size - alen, alen - offs); 294 upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
295 dbg_chk_lpt_sz(c, 2, alen - offs);
286 err = alloc_lpt_leb(c, &lnum); 296 err = alloc_lpt_leb(c, &lnum);
287 if (err) 297 if (err)
288 return err; 298 goto no_space;
289 offs = 0; 299 offs = 0;
290 ubifs_assert(lnum >= c->lpt_first && 300 ubifs_assert(lnum >= c->lpt_first &&
291 lnum <= c->lpt_last); 301 lnum <= c->lpt_last);
@@ -294,11 +304,23 @@ static int layout_cnodes(struct ubifs_info *c)
294 c->ltab_lnum = lnum; 304 c->ltab_lnum = lnum;
295 c->ltab_offs = offs; 305 c->ltab_offs = offs;
296 offs += c->ltab_sz; 306 offs += c->ltab_sz;
307 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
297 } 308 }
298 309
299 alen = ALIGN(offs, c->min_io_size); 310 alen = ALIGN(offs, c->min_io_size);
300 upd_ltab(c, lnum, c->leb_size - alen, alen - offs); 311 upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
312 dbg_chk_lpt_sz(c, 4, alen - offs);
313 err = dbg_chk_lpt_sz(c, 3, alen);
314 if (err)
315 return err;
301 return 0; 316 return 0;
317
318no_space:
319 ubifs_err("LPT out of space");
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);
322 dbg_dump_lpt_info(c);
323 return err;
302} 324}
303 325
304/** 326/**
@@ -333,8 +355,6 @@ static int realloc_lpt_leb(struct ubifs_info *c, int *lnum)
333 *lnum = i + c->lpt_first; 355 *lnum = i + c->lpt_first;
334 return 0; 356 return 0;
335 } 357 }
336 dbg_err("last LEB %d", *lnum);
337 dump_stack();
338 return -ENOSPC; 358 return -ENOSPC;
339} 359}
340 360
@@ -369,12 +389,14 @@ static int write_cnodes(struct ubifs_info *c)
369 done_lsave = 1; 389 done_lsave = 1;
370 ubifs_pack_lsave(c, buf + offs, c->lsave); 390 ubifs_pack_lsave(c, buf + offs, c->lsave);
371 offs += c->lsave_sz; 391 offs += c->lsave_sz;
392 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
372 } 393 }
373 394
374 if (offs + c->ltab_sz <= c->leb_size) { 395 if (offs + c->ltab_sz <= c->leb_size) {
375 done_ltab = 1; 396 done_ltab = 1;
376 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt); 397 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
377 offs += c->ltab_sz; 398 offs += c->ltab_sz;
399 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
378 } 400 }
379 401
380 /* Loop for each cnode */ 402 /* Loop for each cnode */
@@ -392,10 +414,12 @@ static int write_cnodes(struct ubifs_info *c)
392 alen, UBI_SHORTTERM); 414 alen, UBI_SHORTTERM);
393 if (err) 415 if (err)
394 return err; 416 return err;
417 dbg_chk_lpt_sz(c, 4, alen - wlen);
395 } 418 }
419 dbg_chk_lpt_sz(c, 2, 0);
396 err = realloc_lpt_leb(c, &lnum); 420 err = realloc_lpt_leb(c, &lnum);
397 if (err) 421 if (err)
398 return err; 422 goto no_space;
399 offs = 0; 423 offs = 0;
400 from = 0; 424 from = 0;
401 ubifs_assert(lnum >= c->lpt_first && 425 ubifs_assert(lnum >= c->lpt_first &&
@@ -408,12 +432,14 @@ static int write_cnodes(struct ubifs_info *c)
408 done_lsave = 1; 432 done_lsave = 1;
409 ubifs_pack_lsave(c, buf + offs, c->lsave); 433 ubifs_pack_lsave(c, buf + offs, c->lsave);
410 offs += c->lsave_sz; 434 offs += c->lsave_sz;
435 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
411 continue; 436 continue;
412 } 437 }
413 if (!done_ltab) { 438 if (!done_ltab) {
414 done_ltab = 1; 439 done_ltab = 1;
415 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt); 440 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
416 offs += c->ltab_sz; 441 offs += c->ltab_sz;
442 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
417 continue; 443 continue;
418 } 444 }
419 break; 445 break;
@@ -435,6 +461,7 @@ static int write_cnodes(struct ubifs_info *c)
435 clear_bit(COW_ZNODE, &cnode->flags); 461 clear_bit(COW_ZNODE, &cnode->flags);
436 smp_mb__after_clear_bit(); 462 smp_mb__after_clear_bit();
437 offs += len; 463 offs += len;
464 dbg_chk_lpt_sz(c, 1, len);
438 cnode = cnode->cnext; 465 cnode = cnode->cnext;
439 } while (cnode && cnode != c->lpt_cnext); 466 } while (cnode && cnode != c->lpt_cnext);
440 467
@@ -448,9 +475,10 @@ static int write_cnodes(struct ubifs_info *c)
448 UBI_SHORTTERM); 475 UBI_SHORTTERM);
449 if (err) 476 if (err)
450 return err; 477 return err;
478 dbg_chk_lpt_sz(c, 2, alen - wlen);
451 err = realloc_lpt_leb(c, &lnum); 479 err = realloc_lpt_leb(c, &lnum);
452 if (err) 480 if (err)
453 return err; 481 goto no_space;
454 offs = 0; 482 offs = 0;
455 ubifs_assert(lnum >= c->lpt_first && 483 ubifs_assert(lnum >= c->lpt_first &&
456 lnum <= c->lpt_last); 484 lnum <= c->lpt_last);
@@ -461,6 +489,7 @@ static int write_cnodes(struct ubifs_info *c)
461 done_lsave = 1; 489 done_lsave = 1;
462 ubifs_pack_lsave(c, buf + offs, c->lsave); 490 ubifs_pack_lsave(c, buf + offs, c->lsave);
463 offs += c->lsave_sz; 491 offs += c->lsave_sz;
492 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
464 } 493 }
465 494
466 /* Make sure to place LPT's own lprops table */ 495 /* Make sure to place LPT's own lprops table */
@@ -473,9 +502,10 @@ static int write_cnodes(struct ubifs_info *c)
473 UBI_SHORTTERM); 502 UBI_SHORTTERM);
474 if (err) 503 if (err)
475 return err; 504 return err;
505 dbg_chk_lpt_sz(c, 2, alen - wlen);
476 err = realloc_lpt_leb(c, &lnum); 506 err = realloc_lpt_leb(c, &lnum);
477 if (err) 507 if (err)
478 return err; 508 goto no_space;
479 offs = 0; 509 offs = 0;
480 ubifs_assert(lnum >= c->lpt_first && 510 ubifs_assert(lnum >= c->lpt_first &&
481 lnum <= c->lpt_last); 511 lnum <= c->lpt_last);
@@ -486,6 +516,7 @@ static int write_cnodes(struct ubifs_info *c)
486 done_ltab = 1; 516 done_ltab = 1;
487 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt); 517 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
488 offs += c->ltab_sz; 518 offs += c->ltab_sz;
519 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
489 } 520 }
490 521
491 /* Write remaining data in buffer */ 522 /* Write remaining data in buffer */
@@ -495,6 +526,12 @@ static int write_cnodes(struct ubifs_info *c)
495 err = ubifs_leb_write(c, lnum, buf + from, from, alen, UBI_SHORTTERM); 526 err = ubifs_leb_write(c, lnum, buf + from, from, alen, UBI_SHORTTERM);
496 if (err) 527 if (err)
497 return err; 528 return err;
529
530 dbg_chk_lpt_sz(c, 4, alen - wlen);
531 err = dbg_chk_lpt_sz(c, 3, ALIGN(offs, c->min_io_size));
532 if (err)
533 return err;
534
498 c->nhead_lnum = lnum; 535 c->nhead_lnum = lnum;
499 c->nhead_offs = ALIGN(offs, c->min_io_size); 536 c->nhead_offs = ALIGN(offs, c->min_io_size);
500 537
@@ -503,7 +540,15 @@ static int write_cnodes(struct ubifs_info *c)
503 dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs); 540 dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs);
504 if (c->big_lpt) 541 if (c->big_lpt)
505 dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs); 542 dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs);
543
506 return 0; 544 return 0;
545
546no_space:
547 ubifs_err("LPT out of space mismatch");
548 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);
550 dbg_dump_lpt_info(c);
551 return err;
507} 552}
508 553
509/** 554/**
@@ -1044,6 +1089,8 @@ static int is_a_node(struct ubifs_info *c, uint8_t *buf, int len)
1044 int pos = 0, node_type, node_len; 1089 int pos = 0, node_type, node_len;
1045 uint16_t crc, calc_crc; 1090 uint16_t crc, calc_crc;
1046 1091
1092 if (len < UBIFS_LPT_CRC_BYTES + (UBIFS_LPT_TYPE_BITS + 7) / 8)
1093 return 0;
1047 node_type = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_TYPE_BITS); 1094 node_type = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_TYPE_BITS);
1048 if (node_type == UBIFS_LPT_NOT_A_NODE) 1095 if (node_type == UBIFS_LPT_NOT_A_NODE)
1049 return 0; 1096 return 0;
@@ -1156,6 +1203,9 @@ int ubifs_lpt_start_commit(struct ubifs_info *c)
1156 dbg_lp(""); 1203 dbg_lp("");
1157 1204
1158 mutex_lock(&c->lp_mutex); 1205 mutex_lock(&c->lp_mutex);
1206 err = dbg_chk_lpt_free_spc(c);
1207 if (err)
1208 goto out;
1159 err = dbg_check_ltab(c); 1209 err = dbg_check_ltab(c);
1160 if (err) 1210 if (err)
1161 goto out; 1211 goto out;
@@ -1645,4 +1695,121 @@ int dbg_check_ltab(struct ubifs_info *c)
1645 return 0; 1695 return 0;
1646} 1696}
1647 1697
1698/**
1699 * dbg_chk_lpt_free_spc - check LPT free space is enough to write entire LPT.
1700 * @c: the UBIFS file-system description object
1701 *
1702 * This function returns %0 on success and a negative error code on failure.
1703 */
1704int dbg_chk_lpt_free_spc(struct ubifs_info *c)
1705{
1706 long long free = 0;
1707 int i;
1708
1709 for (i = 0; i < c->lpt_lebs; i++) {
1710 if (c->ltab[i].tgc || c->ltab[i].cmt)
1711 continue;
1712 if (i + c->lpt_first == c->nhead_lnum)
1713 free += c->leb_size - c->nhead_offs;
1714 else if (c->ltab[i].free == c->leb_size)
1715 free += c->leb_size;
1716 }
1717 if (free < c->lpt_sz) {
1718 dbg_err("LPT space error: free %lld lpt_sz %lld",
1719 free, c->lpt_sz);
1720 dbg_dump_lpt_info(c);
1721 return -EINVAL;
1722 }
1723 return 0;
1724}
1725
1726/**
1727 * dbg_chk_lpt_sz - check LPT does not write more than LPT size.
1728 * @c: the UBIFS file-system description object
1729 * @action: action
1730 * @len: length written
1731 *
1732 * This function returns %0 on success and a negative error code on failure.
1733 */
1734int dbg_chk_lpt_sz(struct ubifs_info *c, int action, int len)
1735{
1736 long long chk_lpt_sz, lpt_sz;
1737 int err = 0;
1738
1739 switch (action) {
1740 case 0:
1741 c->chk_lpt_sz = 0;
1742 c->chk_lpt_sz2 = 0;
1743 c->chk_lpt_lebs = 0;
1744 c->chk_lpt_wastage = 0;
1745 if (c->dirty_pn_cnt > c->pnode_cnt) {
1746 dbg_err("dirty pnodes %d exceed max %d",
1747 c->dirty_pn_cnt, c->pnode_cnt);
1748 err = -EINVAL;
1749 }
1750 if (c->dirty_nn_cnt > c->nnode_cnt) {
1751 dbg_err("dirty nnodes %d exceed max %d",
1752 c->dirty_nn_cnt, c->nnode_cnt);
1753 err = -EINVAL;
1754 }
1755 return err;
1756 case 1:
1757 c->chk_lpt_sz += len;
1758 return 0;
1759 case 2:
1760 c->chk_lpt_sz += len;
1761 c->chk_lpt_wastage += len;
1762 c->chk_lpt_lebs += 1;
1763 return 0;
1764 case 3:
1765 chk_lpt_sz = c->leb_size;
1766 chk_lpt_sz *= c->chk_lpt_lebs;
1767 chk_lpt_sz += len - c->nhead_offs;
1768 if (c->chk_lpt_sz != chk_lpt_sz) {
1769 dbg_err("LPT wrote %lld but space used was %lld",
1770 c->chk_lpt_sz, chk_lpt_sz);
1771 err = -EINVAL;
1772 }
1773 if (c->chk_lpt_sz > c->lpt_sz) {
1774 dbg_err("LPT wrote %lld but lpt_sz is %lld",
1775 c->chk_lpt_sz, c->lpt_sz);
1776 err = -EINVAL;
1777 }
1778 if (c->chk_lpt_sz2 && c->chk_lpt_sz != c->chk_lpt_sz2) {
1779 dbg_err("LPT layout size %lld but wrote %lld",
1780 c->chk_lpt_sz, c->chk_lpt_sz2);
1781 err = -EINVAL;
1782 }
1783 if (c->chk_lpt_sz2 && c->new_nhead_offs != len) {
1784 dbg_err("LPT new nhead offs: expected %d was %d",
1785 c->new_nhead_offs, len);
1786 err = -EINVAL;
1787 }
1788 lpt_sz = (long long)c->pnode_cnt * c->pnode_sz;
1789 lpt_sz += (long long)c->nnode_cnt * c->nnode_sz;
1790 lpt_sz += c->ltab_sz;
1791 if (c->big_lpt)
1792 lpt_sz += c->lsave_sz;
1793 if (c->chk_lpt_sz - c->chk_lpt_wastage > lpt_sz) {
1794 dbg_err("LPT chk_lpt_sz %lld + waste %lld exceeds %lld",
1795 c->chk_lpt_sz, c->chk_lpt_wastage, lpt_sz);
1796 err = -EINVAL;
1797 }
1798 if (err)
1799 dbg_dump_lpt_info(c);
1800 c->chk_lpt_sz2 = c->chk_lpt_sz;
1801 c->chk_lpt_sz = 0;
1802 c->chk_lpt_wastage = 0;
1803 c->chk_lpt_lebs = 0;
1804 c->new_nhead_offs = len;
1805 return err;
1806 case 4:
1807 c->chk_lpt_sz += len;
1808 c->chk_lpt_wastage += len;
1809 return 0;
1810 default:
1811 return -EINVAL;
1812 }
1813}
1814
1648#endif /* CONFIG_UBIFS_FS_DEBUG */ 1815#endif /* CONFIG_UBIFS_FS_DEBUG */
diff --git a/fs/ubifs/misc.h b/fs/ubifs/misc.h
index 4beccfc256d2..4fa81d867e41 100644
--- a/fs/ubifs/misc.h
+++ b/fs/ubifs/misc.h
@@ -80,20 +80,6 @@ static inline struct ubifs_inode *ubifs_inode(const struct inode *inode)
80} 80}
81 81
82/** 82/**
83 * ubifs_ro_mode - switch UBIFS to read read-only mode.
84 * @c: UBIFS file-system description object
85 * @err: error code which is the reason of switching to R/O mode
86 */
87static inline void ubifs_ro_mode(struct ubifs_info *c, int err)
88{
89 if (!c->ro_media) {
90 c->ro_media = 1;
91 ubifs_warn("switched to read-only mode, error %d", err);
92 dbg_dump_stack();
93 }
94}
95
96/**
97 * ubifs_compr_present - check if compressor was compiled in. 83 * ubifs_compr_present - check if compressor was compiled in.
98 * @compr_type: compressor type to check 84 * @compr_type: compressor type to check
99 * 85 *
@@ -298,45 +284,57 @@ static inline void *ubifs_idx_key(const struct ubifs_info *c,
298} 284}
299 285
300/** 286/**
301 * ubifs_reported_space - calculate reported free space. 287 * ubifs_current_time - round current time to time granularity.
302 * @c: the UBIFS file-system description object 288 * @inode: inode
303 * @free: amount of free space
304 *
305 * This function calculates amount of free space which will be reported to
306 * user-space. User-space application tend to expect that if the file-system
307 * (e.g., via the 'statfs()' call) reports that it has N bytes available, they
308 * are able to write a file of size N. UBIFS attaches node headers to each data
309 * node and it has to write indexind nodes as well. This introduces additional
310 * overhead, and UBIFS it has to report sligtly less free space to meet the
311 * above expectetion.
312 *
313 * This function assumes free space is made up of uncompressed data nodes and
314 * full index nodes (one per data node, doubled because we always allow enough
315 * space to write the index twice).
316 *
317 * Note, the calculation is pessimistic, which means that most of the time
318 * UBIFS reports less space than it actually has.
319 */ 289 */
320static inline long long ubifs_reported_space(const struct ubifs_info *c, 290static inline struct timespec ubifs_current_time(struct inode *inode)
321 uint64_t free)
322{ 291{
323 int divisor, factor; 292 return (inode->i_sb->s_time_gran < NSEC_PER_SEC) ?
293 current_fs_time(inode->i_sb) : CURRENT_TIME_SEC;
294}
324 295
325 divisor = UBIFS_MAX_DATA_NODE_SZ + (c->max_idx_node_sz << 1); 296/**
326 factor = UBIFS_MAX_DATA_NODE_SZ - UBIFS_DATA_NODE_SZ; 297 * ubifs_tnc_lookup - look up a file-system node.
327 do_div(free, divisor); 298 * @c: UBIFS file-system description object
299 * @key: node key to lookup
300 * @node: the node is returned here
301 *
302 * This function look up and reads node with key @key. The caller has to make
303 * sure the @node buffer is large enough to fit the node. Returns zero in case
304 * of success, %-ENOENT if the node was not found, and a negative error code in
305 * case of failure.
306 */
307static inline int ubifs_tnc_lookup(struct ubifs_info *c,
308 const union ubifs_key *key, void *node)
309{
310 return ubifs_tnc_locate(c, key, node, NULL, NULL);
311}
328 312
329 return free * factor; 313/**
314 * ubifs_get_lprops - get reference to LEB properties.
315 * @c: the UBIFS file-system description object
316 *
317 * This function locks lprops. Lprops have to be unlocked by
318 * 'ubifs_release_lprops()'.
319 */
320static inline void ubifs_get_lprops(struct ubifs_info *c)
321{
322 mutex_lock(&c->lp_mutex);
330} 323}
331 324
332/** 325/**
333 * ubifs_current_time - round current time to time granularity. 326 * ubifs_release_lprops - release lprops lock.
334 * @inode: inode 327 * @c: the UBIFS file-system description object
328 *
329 * This function has to be called after each 'ubifs_get_lprops()' call to
330 * unlock lprops.
335 */ 331 */
336static inline struct timespec ubifs_current_time(struct inode *inode) 332static inline void ubifs_release_lprops(struct ubifs_info *c)
337{ 333{
338 return (inode->i_sb->s_time_gran < NSEC_PER_SEC) ? 334 ubifs_assert(mutex_is_locked(&c->lp_mutex));
339 current_fs_time(inode->i_sb) : CURRENT_TIME_SEC; 335 ubifs_assert(c->lst.empty_lebs >= 0 &&
336 c->lst.empty_lebs <= c->main_lebs);
337 mutex_unlock(&c->lp_mutex);
340} 338}
341 339
342#endif /* __UBIFS_MISC_H__ */ 340#endif /* __UBIFS_MISC_H__ */
diff --git a/fs/ubifs/orphan.c b/fs/ubifs/orphan.c
index 3afeb9242c6a..02d3462f4d3e 100644
--- a/fs/ubifs/orphan.c
+++ b/fs/ubifs/orphan.c
@@ -310,10 +310,10 @@ static int write_orph_node(struct ubifs_info *c, int atomic)
310 c->cmt_orphans -= cnt; 310 c->cmt_orphans -= cnt;
311 spin_unlock(&c->orphan_lock); 311 spin_unlock(&c->orphan_lock);
312 if (c->cmt_orphans) 312 if (c->cmt_orphans)
313 orph->cmt_no = cpu_to_le64(c->cmt_no + 1); 313 orph->cmt_no = cpu_to_le64(c->cmt_no);
314 else 314 else
315 /* Mark the last node of the commit */ 315 /* Mark the last node of the commit */
316 orph->cmt_no = cpu_to_le64((c->cmt_no + 1) | (1ULL << 63)); 316 orph->cmt_no = cpu_to_le64((c->cmt_no) | (1ULL << 63));
317 ubifs_assert(c->ohead_offs + len <= c->leb_size); 317 ubifs_assert(c->ohead_offs + len <= c->leb_size);
318 ubifs_assert(c->ohead_lnum >= c->orph_first); 318 ubifs_assert(c->ohead_lnum >= c->orph_first);
319 ubifs_assert(c->ohead_lnum <= c->orph_last); 319 ubifs_assert(c->ohead_lnum <= c->orph_last);
diff --git a/fs/ubifs/scan.c b/fs/ubifs/scan.c
index acf5c5fffc60..0ed82479b44b 100644
--- a/fs/ubifs/scan.c
+++ b/fs/ubifs/scan.c
@@ -87,7 +87,7 @@ int ubifs_scan_a_node(const struct ubifs_info *c, void *buf, int len, int lnum,
87 87
88 dbg_scan("scanning %s", dbg_ntype(ch->node_type)); 88 dbg_scan("scanning %s", dbg_ntype(ch->node_type));
89 89
90 if (ubifs_check_node(c, buf, lnum, offs, quiet)) 90 if (ubifs_check_node(c, buf, lnum, offs, quiet, 1))
91 return SCANNED_A_CORRUPT_NODE; 91 return SCANNED_A_CORRUPT_NODE;
92 92
93 if (ch->node_type == UBIFS_PAD_NODE) { 93 if (ch->node_type == UBIFS_PAD_NODE) {
diff --git a/fs/ubifs/super.c b/fs/ubifs/super.c
index 00eb9c68ad03..8780efbf40ac 100644
--- a/fs/ubifs/super.c
+++ b/fs/ubifs/super.c
@@ -30,7 +30,6 @@
30#include <linux/slab.h> 30#include <linux/slab.h>
31#include <linux/module.h> 31#include <linux/module.h>
32#include <linux/ctype.h> 32#include <linux/ctype.h>
33#include <linux/random.h>
34#include <linux/kthread.h> 33#include <linux/kthread.h>
35#include <linux/parser.h> 34#include <linux/parser.h>
36#include <linux/seq_file.h> 35#include <linux/seq_file.h>
@@ -149,7 +148,7 @@ struct inode *ubifs_iget(struct super_block *sb, unsigned long inum)
149 if (err) 148 if (err)
150 goto out_invalid; 149 goto out_invalid;
151 150
152 /* Disable readahead */ 151 /* Disable read-ahead */
153 inode->i_mapping->backing_dev_info = &c->bdi; 152 inode->i_mapping->backing_dev_info = &c->bdi;
154 153
155 switch (inode->i_mode & S_IFMT) { 154 switch (inode->i_mode & S_IFMT) {
@@ -278,7 +277,7 @@ static void ubifs_destroy_inode(struct inode *inode)
278 */ 277 */
279static int ubifs_write_inode(struct inode *inode, int wait) 278static int ubifs_write_inode(struct inode *inode, int wait)
280{ 279{
281 int err; 280 int err = 0;
282 struct ubifs_info *c = inode->i_sb->s_fs_info; 281 struct ubifs_info *c = inode->i_sb->s_fs_info;
283 struct ubifs_inode *ui = ubifs_inode(inode); 282 struct ubifs_inode *ui = ubifs_inode(inode);
284 283
@@ -299,10 +298,18 @@ static int ubifs_write_inode(struct inode *inode, int wait)
299 return 0; 298 return 0;
300 } 299 }
301 300
302 dbg_gen("inode %lu", inode->i_ino); 301 /*
303 err = ubifs_jnl_write_inode(c, inode, 0); 302 * As an optimization, do not write orphan inodes to the media just
304 if (err) 303 * because this is not needed.
305 ubifs_err("can't write inode %lu, error %d", inode->i_ino, err); 304 */
305 dbg_gen("inode %lu, mode %#x, nlink %u",
306 inode->i_ino, (int)inode->i_mode, inode->i_nlink);
307 if (inode->i_nlink) {
308 err = ubifs_jnl_write_inode(c, inode);
309 if (err)
310 ubifs_err("can't write inode %lu, error %d",
311 inode->i_ino, err);
312 }
306 313
307 ui->dirty = 0; 314 ui->dirty = 0;
308 mutex_unlock(&ui->ui_mutex); 315 mutex_unlock(&ui->ui_mutex);
@@ -314,8 +321,9 @@ static void ubifs_delete_inode(struct inode *inode)
314{ 321{
315 int err; 322 int err;
316 struct ubifs_info *c = inode->i_sb->s_fs_info; 323 struct ubifs_info *c = inode->i_sb->s_fs_info;
324 struct ubifs_inode *ui = ubifs_inode(inode);
317 325
318 if (ubifs_inode(inode)->xattr) 326 if (ui->xattr)
319 /* 327 /*
320 * Extended attribute inode deletions are fully handled in 328 * Extended attribute inode deletions are fully handled in
321 * 'ubifs_removexattr()'. These inodes are special and have 329 * 'ubifs_removexattr()'. These inodes are special and have
@@ -323,7 +331,7 @@ static void ubifs_delete_inode(struct inode *inode)
323 */ 331 */
324 goto out; 332 goto out;
325 333
326 dbg_gen("inode %lu", inode->i_ino); 334 dbg_gen("inode %lu, mode %#x", inode->i_ino, (int)inode->i_mode);
327 ubifs_assert(!atomic_read(&inode->i_count)); 335 ubifs_assert(!atomic_read(&inode->i_count));
328 ubifs_assert(inode->i_nlink == 0); 336 ubifs_assert(inode->i_nlink == 0);
329 337
@@ -331,15 +339,19 @@ static void ubifs_delete_inode(struct inode *inode)
331 if (is_bad_inode(inode)) 339 if (is_bad_inode(inode))
332 goto out; 340 goto out;
333 341
334 ubifs_inode(inode)->ui_size = inode->i_size = 0; 342 ui->ui_size = inode->i_size = 0;
335 err = ubifs_jnl_write_inode(c, inode, 1); 343 err = ubifs_jnl_delete_inode(c, inode);
336 if (err) 344 if (err)
337 /* 345 /*
338 * Worst case we have a lost orphan inode wasting space, so a 346 * Worst case we have a lost orphan inode wasting space, so a
339 * simple error message is ok here. 347 * simple error message is OK here.
340 */ 348 */
341 ubifs_err("can't write inode %lu, error %d", inode->i_ino, err); 349 ubifs_err("can't delete inode %lu, error %d",
350 inode->i_ino, err);
351
342out: 352out:
353 if (ui->dirty)
354 ubifs_release_dirty_inode_budget(c, ui);
343 clear_inode(inode); 355 clear_inode(inode);
344} 356}
345 357
@@ -358,8 +370,9 @@ static int ubifs_statfs(struct dentry *dentry, struct kstatfs *buf)
358{ 370{
359 struct ubifs_info *c = dentry->d_sb->s_fs_info; 371 struct ubifs_info *c = dentry->d_sb->s_fs_info;
360 unsigned long long free; 372 unsigned long long free;
373 __le32 *uuid = (__le32 *)c->uuid;
361 374
362 free = ubifs_budg_get_free_space(c); 375 free = ubifs_get_free_space(c);
363 dbg_gen("free space %lld bytes (%lld blocks)", 376 dbg_gen("free space %lld bytes (%lld blocks)",
364 free, free >> UBIFS_BLOCK_SHIFT); 377 free, free >> UBIFS_BLOCK_SHIFT);
365 378
@@ -374,7 +387,8 @@ static int ubifs_statfs(struct dentry *dentry, struct kstatfs *buf)
374 buf->f_files = 0; 387 buf->f_files = 0;
375 buf->f_ffree = 0; 388 buf->f_ffree = 0;
376 buf->f_namelen = UBIFS_MAX_NLEN; 389 buf->f_namelen = UBIFS_MAX_NLEN;
377 390 buf->f_fsid.val[0] = le32_to_cpu(uuid[0]) ^ le32_to_cpu(uuid[2]);
391 buf->f_fsid.val[1] = le32_to_cpu(uuid[1]) ^ le32_to_cpu(uuid[3]);
378 return 0; 392 return 0;
379} 393}
380 394
@@ -387,6 +401,16 @@ static int ubifs_show_options(struct seq_file *s, struct vfsmount *mnt)
387 else if (c->mount_opts.unmount_mode == 1) 401 else if (c->mount_opts.unmount_mode == 1)
388 seq_printf(s, ",norm_unmount"); 402 seq_printf(s, ",norm_unmount");
389 403
404 if (c->mount_opts.bulk_read == 2)
405 seq_printf(s, ",bulk_read");
406 else if (c->mount_opts.bulk_read == 1)
407 seq_printf(s, ",no_bulk_read");
408
409 if (c->mount_opts.chk_data_crc == 2)
410 seq_printf(s, ",chk_data_crc");
411 else if (c->mount_opts.chk_data_crc == 1)
412 seq_printf(s, ",no_chk_data_crc");
413
390 return 0; 414 return 0;
391} 415}
392 416
@@ -394,13 +418,26 @@ static int ubifs_sync_fs(struct super_block *sb, int wait)
394{ 418{
395 struct ubifs_info *c = sb->s_fs_info; 419 struct ubifs_info *c = sb->s_fs_info;
396 int i, ret = 0, err; 420 int i, ret = 0, err;
421 long long bud_bytes;
397 422
398 if (c->jheads) 423 if (c->jheads) {
399 for (i = 0; i < c->jhead_cnt; i++) { 424 for (i = 0; i < c->jhead_cnt; i++) {
400 err = ubifs_wbuf_sync(&c->jheads[i].wbuf); 425 err = ubifs_wbuf_sync(&c->jheads[i].wbuf);
401 if (err && !ret) 426 if (err && !ret)
402 ret = err; 427 ret = err;
403 } 428 }
429
430 /* Commit the journal unless it has too little data */
431 spin_lock(&c->buds_lock);
432 bud_bytes = c->bud_bytes;
433 spin_unlock(&c->buds_lock);
434 if (bud_bytes > c->leb_size) {
435 err = ubifs_run_commit(c);
436 if (err)
437 return err;
438 }
439 }
440
404 /* 441 /*
405 * We ought to call sync for c->ubi but it does not have one. If it had 442 * We ought to call sync for c->ubi but it does not have one. If it had
406 * it would in turn call mtd->sync, however mtd operations are 443 * it would in turn call mtd->sync, however mtd operations are
@@ -518,6 +555,24 @@ static int init_constants_early(struct ubifs_info *c)
518 c->dead_wm = ALIGN(MIN_WRITE_SZ, c->min_io_size); 555 c->dead_wm = ALIGN(MIN_WRITE_SZ, c->min_io_size);
519 c->dark_wm = ALIGN(UBIFS_MAX_NODE_SZ, c->min_io_size); 556 c->dark_wm = ALIGN(UBIFS_MAX_NODE_SZ, c->min_io_size);
520 557
558 /*
559 * Calculate how many bytes would be wasted at the end of LEB if it was
560 * fully filled with data nodes of maximum size. This is used in
561 * calculations when reporting free space.
562 */
563 c->leb_overhead = c->leb_size % UBIFS_MAX_DATA_NODE_SZ;
564 /* Buffer size for bulk-reads */
565 c->bulk_read_buf_size = UBIFS_MAX_BULK_READ * UBIFS_MAX_DATA_NODE_SZ;
566 if (c->bulk_read_buf_size > c->leb_size)
567 c->bulk_read_buf_size = c->leb_size;
568 if (c->bulk_read_buf_size > 128 * 1024) {
569 /* Check if we can kmalloc more than 128KiB */
570 void *try = kmalloc(c->bulk_read_buf_size, GFP_KERNEL);
571
572 kfree(try);
573 if (!try)
574 c->bulk_read_buf_size = 128 * 1024;
575 }
521 return 0; 576 return 0;
522} 577}
523 578
@@ -635,13 +690,11 @@ static int init_constants_late(struct ubifs_info *c)
635 * internally because it does not make much sense for UBIFS, but it is 690 * internally because it does not make much sense for UBIFS, but it is
636 * necessary to report something for the 'statfs()' call. 691 * necessary to report something for the 'statfs()' call.
637 * 692 *
638 * Subtract the LEB reserved for GC and the LEB which is reserved for 693 * Subtract the LEB reserved for GC, the LEB which is reserved for
639 * deletions. 694 * deletions, and assume only one journal head is available.
640 *
641 * Review 'ubifs_calc_available()' if changing this calculation.
642 */ 695 */
643 tmp64 = c->main_lebs - 2; 696 tmp64 = c->main_lebs - 2 - c->jhead_cnt + 1;
644 tmp64 *= (uint64_t)c->leb_size - c->dark_wm; 697 tmp64 *= (uint64_t)c->leb_size - c->leb_overhead;
645 tmp64 = ubifs_reported_space(c, tmp64); 698 tmp64 = ubifs_reported_space(c, tmp64);
646 c->block_cnt = tmp64 >> UBIFS_BLOCK_SHIFT; 699 c->block_cnt = tmp64 >> UBIFS_BLOCK_SHIFT;
647 700
@@ -822,17 +875,29 @@ static int check_volume_empty(struct ubifs_info *c)
822 * 875 *
823 * Opt_fast_unmount: do not run a journal commit before un-mounting 876 * Opt_fast_unmount: do not run a journal commit before un-mounting
824 * Opt_norm_unmount: run a journal commit before un-mounting 877 * Opt_norm_unmount: run a journal commit before un-mounting
878 * Opt_bulk_read: enable bulk-reads
879 * Opt_no_bulk_read: disable bulk-reads
880 * Opt_chk_data_crc: check CRCs when reading data nodes
881 * Opt_no_chk_data_crc: do not check CRCs when reading data nodes
825 * Opt_err: just end of array marker 882 * Opt_err: just end of array marker
826 */ 883 */
827enum { 884enum {
828 Opt_fast_unmount, 885 Opt_fast_unmount,
829 Opt_norm_unmount, 886 Opt_norm_unmount,
887 Opt_bulk_read,
888 Opt_no_bulk_read,
889 Opt_chk_data_crc,
890 Opt_no_chk_data_crc,
830 Opt_err, 891 Opt_err,
831}; 892};
832 893
833static match_table_t tokens = { 894static const match_table_t tokens = {
834 {Opt_fast_unmount, "fast_unmount"}, 895 {Opt_fast_unmount, "fast_unmount"},
835 {Opt_norm_unmount, "norm_unmount"}, 896 {Opt_norm_unmount, "norm_unmount"},
897 {Opt_bulk_read, "bulk_read"},
898 {Opt_no_bulk_read, "no_bulk_read"},
899 {Opt_chk_data_crc, "chk_data_crc"},
900 {Opt_no_chk_data_crc, "no_chk_data_crc"},
836 {Opt_err, NULL}, 901 {Opt_err, NULL},
837}; 902};
838 903
@@ -870,6 +935,22 @@ static int ubifs_parse_options(struct ubifs_info *c, char *options,
870 c->mount_opts.unmount_mode = 1; 935 c->mount_opts.unmount_mode = 1;
871 c->fast_unmount = 0; 936 c->fast_unmount = 0;
872 break; 937 break;
938 case Opt_bulk_read:
939 c->mount_opts.bulk_read = 2;
940 c->bulk_read = 1;
941 break;
942 case Opt_no_bulk_read:
943 c->mount_opts.bulk_read = 1;
944 c->bulk_read = 0;
945 break;
946 case Opt_chk_data_crc:
947 c->mount_opts.chk_data_crc = 2;
948 c->no_chk_data_crc = 0;
949 break;
950 case Opt_no_chk_data_crc:
951 c->mount_opts.chk_data_crc = 1;
952 c->no_chk_data_crc = 1;
953 break;
873 default: 954 default:
874 ubifs_err("unrecognized mount option \"%s\" " 955 ubifs_err("unrecognized mount option \"%s\" "
875 "or missing value", p); 956 "or missing value", p);
@@ -978,6 +1059,8 @@ static int mount_ubifs(struct ubifs_info *c)
978 goto out_free; 1059 goto out_free;
979 } 1060 }
980 1061
1062 c->always_chk_crc = 1;
1063
981 err = ubifs_read_superblock(c); 1064 err = ubifs_read_superblock(c);
982 if (err) 1065 if (err)
983 goto out_free; 1066 goto out_free;
@@ -1006,17 +1089,14 @@ static int mount_ubifs(struct ubifs_info *c)
1006 goto out_dereg; 1089 goto out_dereg;
1007 } 1090 }
1008 1091
1092 sprintf(c->bgt_name, BGT_NAME_PATTERN, c->vi.ubi_num, c->vi.vol_id);
1009 if (!mounted_read_only) { 1093 if (!mounted_read_only) {
1010 err = alloc_wbufs(c); 1094 err = alloc_wbufs(c);
1011 if (err) 1095 if (err)
1012 goto out_cbuf; 1096 goto out_cbuf;
1013 1097
1014 /* Create background thread */ 1098 /* Create background thread */
1015 sprintf(c->bgt_name, BGT_NAME_PATTERN, c->vi.ubi_num,
1016 c->vi.vol_id);
1017 c->bgt = kthread_create(ubifs_bg_thread, c, c->bgt_name); 1099 c->bgt = kthread_create(ubifs_bg_thread, c, c->bgt_name);
1018 if (!c->bgt)
1019 c->bgt = ERR_PTR(-EINVAL);
1020 if (IS_ERR(c->bgt)) { 1100 if (IS_ERR(c->bgt)) {
1021 err = PTR_ERR(c->bgt); 1101 err = PTR_ERR(c->bgt);
1022 c->bgt = NULL; 1102 c->bgt = NULL;
@@ -1122,24 +1202,28 @@ static int mount_ubifs(struct ubifs_info *c)
1122 if (err) 1202 if (err)
1123 goto out_infos; 1203 goto out_infos;
1124 1204
1125 ubifs_msg("mounted UBI device %d, volume %d", c->vi.ubi_num, 1205 c->always_chk_crc = 0;
1126 c->vi.vol_id); 1206
1207 ubifs_msg("mounted UBI device %d, volume %d, name \"%s\"",
1208 c->vi.ubi_num, c->vi.vol_id, c->vi.name);
1127 if (mounted_read_only) 1209 if (mounted_read_only)
1128 ubifs_msg("mounted read-only"); 1210 ubifs_msg("mounted read-only");
1129 x = (long long)c->main_lebs * c->leb_size; 1211 x = (long long)c->main_lebs * c->leb_size;
1130 ubifs_msg("file system size: %lld bytes (%lld KiB, %lld MiB, %d LEBs)", 1212 ubifs_msg("file system size: %lld bytes (%lld KiB, %lld MiB, %d "
1131 x, x >> 10, x >> 20, c->main_lebs); 1213 "LEBs)", x, x >> 10, x >> 20, c->main_lebs);
1132 x = (long long)c->log_lebs * c->leb_size + c->max_bud_bytes; 1214 x = (long long)c->log_lebs * c->leb_size + c->max_bud_bytes;
1133 ubifs_msg("journal size: %lld bytes (%lld KiB, %lld MiB, %d LEBs)", 1215 ubifs_msg("journal size: %lld bytes (%lld KiB, %lld MiB, %d "
1134 x, x >> 10, x >> 20, c->log_lebs + c->max_bud_cnt); 1216 "LEBs)", x, x >> 10, x >> 20, c->log_lebs + c->max_bud_cnt);
1135 ubifs_msg("default compressor: %s", ubifs_compr_name(c->default_compr)); 1217 ubifs_msg("media format: %d (latest is %d)",
1136 ubifs_msg("media format %d, latest format %d",
1137 c->fmt_version, UBIFS_FORMAT_VERSION); 1218 c->fmt_version, UBIFS_FORMAT_VERSION);
1219 ubifs_msg("default compressor: %s", ubifs_compr_name(c->default_compr));
1220 ubifs_msg("reserved for root: %llu bytes (%llu KiB)",
1221 c->report_rp_size, c->report_rp_size >> 10);
1138 1222
1139 dbg_msg("compiled on: " __DATE__ " at " __TIME__); 1223 dbg_msg("compiled on: " __DATE__ " at " __TIME__);
1140 dbg_msg("min. I/O unit size: %d bytes", c->min_io_size); 1224 dbg_msg("min. I/O unit size: %d bytes", c->min_io_size);
1141 dbg_msg("LEB size: %d bytes (%d KiB)", 1225 dbg_msg("LEB size: %d bytes (%d KiB)",
1142 c->leb_size, c->leb_size / 1024); 1226 c->leb_size, c->leb_size >> 10);
1143 dbg_msg("data journal heads: %d", 1227 dbg_msg("data journal heads: %d",
1144 c->jhead_cnt - NONDATA_JHEADS_CNT); 1228 c->jhead_cnt - NONDATA_JHEADS_CNT);
1145 dbg_msg("UUID: %02X%02X%02X%02X-%02X%02X" 1229 dbg_msg("UUID: %02X%02X%02X%02X-%02X%02X"
@@ -1265,6 +1349,7 @@ static int ubifs_remount_rw(struct ubifs_info *c)
1265 1349
1266 mutex_lock(&c->umount_mutex); 1350 mutex_lock(&c->umount_mutex);
1267 c->remounting_rw = 1; 1351 c->remounting_rw = 1;
1352 c->always_chk_crc = 1;
1268 1353
1269 /* Check for enough free space */ 1354 /* Check for enough free space */
1270 if (ubifs_calc_available(c, c->min_idx_lebs) <= 0) { 1355 if (ubifs_calc_available(c, c->min_idx_lebs) <= 0) {
@@ -1328,20 +1413,20 @@ static int ubifs_remount_rw(struct ubifs_info *c)
1328 1413
1329 /* Create background thread */ 1414 /* Create background thread */
1330 c->bgt = kthread_create(ubifs_bg_thread, c, c->bgt_name); 1415 c->bgt = kthread_create(ubifs_bg_thread, c, c->bgt_name);
1331 if (!c->bgt)
1332 c->bgt = ERR_PTR(-EINVAL);
1333 if (IS_ERR(c->bgt)) { 1416 if (IS_ERR(c->bgt)) {
1334 err = PTR_ERR(c->bgt); 1417 err = PTR_ERR(c->bgt);
1335 c->bgt = NULL; 1418 c->bgt = NULL;
1336 ubifs_err("cannot spawn \"%s\", error %d", 1419 ubifs_err("cannot spawn \"%s\", error %d",
1337 c->bgt_name, err); 1420 c->bgt_name, err);
1338 return err; 1421 goto out;
1339 } 1422 }
1340 wake_up_process(c->bgt); 1423 wake_up_process(c->bgt);
1341 1424
1342 c->orph_buf = vmalloc(c->leb_size); 1425 c->orph_buf = vmalloc(c->leb_size);
1343 if (!c->orph_buf) 1426 if (!c->orph_buf) {
1344 return -ENOMEM; 1427 err = -ENOMEM;
1428 goto out;
1429 }
1345 1430
1346 /* Check for enough log space */ 1431 /* Check for enough log space */
1347 lnum = c->lhead_lnum + 1; 1432 lnum = c->lhead_lnum + 1;
@@ -1368,6 +1453,7 @@ static int ubifs_remount_rw(struct ubifs_info *c)
1368 dbg_gen("re-mounted read-write"); 1453 dbg_gen("re-mounted read-write");
1369 c->vfs_sb->s_flags &= ~MS_RDONLY; 1454 c->vfs_sb->s_flags &= ~MS_RDONLY;
1370 c->remounting_rw = 0; 1455 c->remounting_rw = 0;
1456 c->always_chk_crc = 0;
1371 mutex_unlock(&c->umount_mutex); 1457 mutex_unlock(&c->umount_mutex);
1372 return 0; 1458 return 0;
1373 1459
@@ -1383,6 +1469,7 @@ out:
1383 c->ileb_buf = NULL; 1469 c->ileb_buf = NULL;
1384 ubifs_lpt_free(c, 1); 1470 ubifs_lpt_free(c, 1);
1385 c->remounting_rw = 0; 1471 c->remounting_rw = 0;
1472 c->always_chk_crc = 0;
1386 mutex_unlock(&c->umount_mutex); 1473 mutex_unlock(&c->umount_mutex);
1387 return err; 1474 return err;
1388} 1475}
@@ -1391,12 +1478,9 @@ out:
1391 * commit_on_unmount - commit the journal when un-mounting. 1478 * commit_on_unmount - commit the journal when un-mounting.
1392 * @c: UBIFS file-system description object 1479 * @c: UBIFS file-system description object
1393 * 1480 *
1394 * This function is called during un-mounting and it commits the journal unless 1481 * This function is called during un-mounting and re-mounting, and it commits
1395 * the "fast unmount" mode is enabled. It also avoids committing the journal if 1482 * the journal unless the "fast unmount" mode is enabled. It also avoids
1396 * it contains too few data. 1483 * committing the journal if it contains too few data.
1397 *
1398 * Sometimes recovery requires the journal to be committed at least once, and
1399 * this function takes care about this.
1400 */ 1484 */
1401static void commit_on_unmount(struct ubifs_info *c) 1485static void commit_on_unmount(struct ubifs_info *c)
1402{ 1486{
@@ -1469,6 +1553,7 @@ static void ubifs_put_super(struct super_block *sb)
1469 */ 1553 */
1470 ubifs_assert(atomic_long_read(&c->dirty_pg_cnt) == 0); 1554 ubifs_assert(atomic_long_read(&c->dirty_pg_cnt) == 0);
1471 ubifs_assert(c->budg_idx_growth == 0); 1555 ubifs_assert(c->budg_idx_growth == 0);
1556 ubifs_assert(c->budg_dd_growth == 0);
1472 ubifs_assert(c->budg_data_growth == 0); 1557 ubifs_assert(c->budg_data_growth == 0);
1473 1558
1474 /* 1559 /*
@@ -1657,7 +1742,6 @@ static int ubifs_fill_super(struct super_block *sb, void *data, int silent)
1657 INIT_LIST_HEAD(&c->orph_new); 1742 INIT_LIST_HEAD(&c->orph_new);
1658 1743
1659 c->highest_inum = UBIFS_FIRST_INO; 1744 c->highest_inum = UBIFS_FIRST_INO;
1660 get_random_bytes(&c->vfs_gen, sizeof(int));
1661 c->lhead_lnum = c->ltail_lnum = UBIFS_LOG_LNUM; 1745 c->lhead_lnum = c->ltail_lnum = UBIFS_LOG_LNUM;
1662 1746
1663 ubi_get_volume_info(ubi, &c->vi); 1747 ubi_get_volume_info(ubi, &c->vi);
@@ -1671,10 +1755,10 @@ static int ubifs_fill_super(struct super_block *sb, void *data, int silent)
1671 } 1755 }
1672 1756
1673 /* 1757 /*
1674 * UBIFS provids 'backing_dev_info' in order to disable readahead. For 1758 * UBIFS provides 'backing_dev_info' in order to disable read-ahead. For
1675 * UBIFS, I/O is not deferred, it is done immediately in readpage, 1759 * UBIFS, I/O is not deferred, it is done immediately in readpage,
1676 * which means the user would have to wait not just for their own I/O 1760 * which means the user would have to wait not just for their own I/O
1677 * but the readahead I/O as well i.e. completely pointless. 1761 * but the read-ahead I/O as well i.e. completely pointless.
1678 * 1762 *
1679 * Read-ahead will be disabled because @c->bdi.ra_pages is 0. 1763 * Read-ahead will be disabled because @c->bdi.ra_pages is 0.
1680 */ 1764 */
@@ -1841,7 +1925,7 @@ static struct file_system_type ubifs_fs_type = {
1841/* 1925/*
1842 * Inode slab cache constructor. 1926 * Inode slab cache constructor.
1843 */ 1927 */
1844static void inode_slab_ctor(struct kmem_cache *cachep, void *obj) 1928static void inode_slab_ctor(void *obj)
1845{ 1929{
1846 struct ubifs_inode *ui = obj; 1930 struct ubifs_inode *ui = obj;
1847 inode_init_once(&ui->vfs_inode); 1931 inode_init_once(&ui->vfs_inode);
diff --git a/fs/ubifs/tnc.c b/fs/ubifs/tnc.c
index e909f4a96443..d27fd918b9c9 100644
--- a/fs/ubifs/tnc.c
+++ b/fs/ubifs/tnc.c
@@ -284,7 +284,7 @@ static struct ubifs_znode *dirty_cow_znode(struct ubifs_info *c,
284 } 284 }
285 285
286 zn = copy_znode(c, znode); 286 zn = copy_znode(c, znode);
287 if (unlikely(IS_ERR(zn))) 287 if (IS_ERR(zn))
288 return zn; 288 return zn;
289 289
290 if (zbr->len) { 290 if (zbr->len) {
@@ -470,6 +470,10 @@ static int try_read_node(const struct ubifs_info *c, void *buf, int type,
470 if (node_len != len) 470 if (node_len != len)
471 return 0; 471 return 0;
472 472
473 if (type == UBIFS_DATA_NODE && !c->always_chk_crc)
474 if (c->no_chk_data_crc)
475 return 0;
476
473 crc = crc32(UBIFS_CRC32_INIT, buf + 8, node_len - 8); 477 crc = crc32(UBIFS_CRC32_INIT, buf + 8, node_len - 8);
474 node_crc = le32_to_cpu(ch->crc); 478 node_crc = le32_to_cpu(ch->crc);
475 if (crc != node_crc) 479 if (crc != node_crc)
@@ -506,7 +510,7 @@ static int fallible_read_node(struct ubifs_info *c, const union ubifs_key *key,
506 if (keys_cmp(c, key, &node_key) != 0) 510 if (keys_cmp(c, key, &node_key) != 0)
507 ret = 0; 511 ret = 0;
508 } 512 }
509 if (ret == 0) 513 if (ret == 0 && c->replaying)
510 dbg_mnt("dangling branch LEB %d:%d len %d, key %s", 514 dbg_mnt("dangling branch LEB %d:%d len %d, key %s",
511 zbr->lnum, zbr->offs, zbr->len, DBGKEY(key)); 515 zbr->lnum, zbr->offs, zbr->len, DBGKEY(key));
512 return ret; 516 return ret;
@@ -1128,7 +1132,7 @@ static struct ubifs_znode *dirty_cow_bottom_up(struct ubifs_info *c,
1128 ubifs_assert(znode == c->zroot.znode); 1132 ubifs_assert(znode == c->zroot.znode);
1129 znode = dirty_cow_znode(c, &c->zroot); 1133 znode = dirty_cow_znode(c, &c->zroot);
1130 } 1134 }
1131 if (unlikely(IS_ERR(znode)) || !p) 1135 if (IS_ERR(znode) || !p)
1132 break; 1136 break;
1133 ubifs_assert(path[p - 1] >= 0); 1137 ubifs_assert(path[p - 1] >= 0);
1134 ubifs_assert(path[p - 1] < znode->child_cnt); 1138 ubifs_assert(path[p - 1] < znode->child_cnt);
@@ -1382,23 +1386,62 @@ static int lookup_level0_dirty(struct ubifs_info *c, const union ubifs_key *key,
1382} 1386}
1383 1387
1384/** 1388/**
1385 * ubifs_tnc_lookup - look up a file-system node. 1389 * maybe_leb_gced - determine if a LEB may have been garbage collected.
1390 * @c: UBIFS file-system description object
1391 * @lnum: LEB number
1392 * @gc_seq1: garbage collection sequence number
1393 *
1394 * This function determines if @lnum may have been garbage collected since
1395 * sequence number @gc_seq1. If it may have been then %1 is returned, otherwise
1396 * %0 is returned.
1397 */
1398static int maybe_leb_gced(struct ubifs_info *c, int lnum, int gc_seq1)
1399{
1400 int gc_seq2, gced_lnum;
1401
1402 gced_lnum = c->gced_lnum;
1403 smp_rmb();
1404 gc_seq2 = c->gc_seq;
1405 /* Same seq means no GC */
1406 if (gc_seq1 == gc_seq2)
1407 return 0;
1408 /* Different by more than 1 means we don't know */
1409 if (gc_seq1 + 1 != gc_seq2)
1410 return 1;
1411 /*
1412 * We have seen the sequence number has increased by 1. Now we need to
1413 * be sure we read the right LEB number, so read it again.
1414 */
1415 smp_rmb();
1416 if (gced_lnum != c->gced_lnum)
1417 return 1;
1418 /* Finally we can check lnum */
1419 if (gced_lnum == lnum)
1420 return 1;
1421 return 0;
1422}
1423
1424/**
1425 * ubifs_tnc_locate - look up a file-system node and return it and its location.
1386 * @c: UBIFS file-system description object 1426 * @c: UBIFS file-system description object
1387 * @key: node key to lookup 1427 * @key: node key to lookup
1388 * @node: the node is returned here 1428 * @node: the node is returned here
1429 * @lnum: LEB number is returned here
1430 * @offs: offset is returned here
1389 * 1431 *
1390 * This function look up and reads node with key @key. The caller has to make 1432 * This function look up and reads node with key @key. The caller has to make
1391 * sure the @node buffer is large enough to fit the node. Returns zero in case 1433 * sure the @node buffer is large enough to fit the node. Returns zero in case
1392 * of success, %-ENOENT if the node was not found, and a negative error code in 1434 * of success, %-ENOENT if the node was not found, and a negative error code in
1393 * case of failure. 1435 * case of failure. The node location can be returned in @lnum and @offs.
1394 */ 1436 */
1395int ubifs_tnc_lookup(struct ubifs_info *c, const union ubifs_key *key, 1437int ubifs_tnc_locate(struct ubifs_info *c, const union ubifs_key *key,
1396 void *node) 1438 void *node, int *lnum, int *offs)
1397{ 1439{
1398 int found, n, err; 1440 int found, n, err, safely = 0, gc_seq1;
1399 struct ubifs_znode *znode; 1441 struct ubifs_znode *znode;
1400 struct ubifs_zbranch zbr, *zt; 1442 struct ubifs_zbranch zbr, *zt;
1401 1443
1444again:
1402 mutex_lock(&c->tnc_mutex); 1445 mutex_lock(&c->tnc_mutex);
1403 found = ubifs_lookup_level0(c, key, &znode, &n); 1446 found = ubifs_lookup_level0(c, key, &znode, &n);
1404 if (!found) { 1447 if (!found) {
@@ -1409,6 +1452,10 @@ int ubifs_tnc_lookup(struct ubifs_info *c, const union ubifs_key *key,
1409 goto out; 1452 goto out;
1410 } 1453 }
1411 zt = &znode->zbranch[n]; 1454 zt = &znode->zbranch[n];
1455 if (lnum) {
1456 *lnum = zt->lnum;
1457 *offs = zt->offs;
1458 }
1412 if (is_hash_key(c, key)) { 1459 if (is_hash_key(c, key)) {
1413 /* 1460 /*
1414 * In this case the leaf node cache gets used, so we pass the 1461 * In this case the leaf node cache gets used, so we pass the
@@ -1417,11 +1464,31 @@ int ubifs_tnc_lookup(struct ubifs_info *c, const union ubifs_key *key,
1417 err = tnc_read_node_nm(c, zt, node); 1464 err = tnc_read_node_nm(c, zt, node);
1418 goto out; 1465 goto out;
1419 } 1466 }
1467 if (safely) {
1468 err = ubifs_tnc_read_node(c, zt, node);
1469 goto out;
1470 }
1471 /* Drop the TNC mutex prematurely and race with garbage collection */
1420 zbr = znode->zbranch[n]; 1472 zbr = znode->zbranch[n];
1473 gc_seq1 = c->gc_seq;
1421 mutex_unlock(&c->tnc_mutex); 1474 mutex_unlock(&c->tnc_mutex);
1422 1475
1423 err = ubifs_tnc_read_node(c, &zbr, node); 1476 if (ubifs_get_wbuf(c, zbr.lnum)) {
1424 return err; 1477 /* We do not GC journal heads */
1478 err = ubifs_tnc_read_node(c, &zbr, node);
1479 return err;
1480 }
1481
1482 err = fallible_read_node(c, key, &zbr, node);
1483 if (err <= 0 || maybe_leb_gced(c, zbr.lnum, gc_seq1)) {
1484 /*
1485 * The node may have been GC'ed out from under us so try again
1486 * while keeping the TNC mutex locked.
1487 */
1488 safely = 1;
1489 goto again;
1490 }
1491 return 0;
1425 1492
1426out: 1493out:
1427 mutex_unlock(&c->tnc_mutex); 1494 mutex_unlock(&c->tnc_mutex);
@@ -1429,58 +1496,289 @@ out:
1429} 1496}
1430 1497
1431/** 1498/**
1432 * ubifs_tnc_locate - look up a file-system node and return it and its location. 1499 * ubifs_tnc_get_bu_keys - lookup keys for bulk-read.
1433 * @c: UBIFS file-system description object 1500 * @c: UBIFS file-system description object
1434 * @key: node key to lookup 1501 * @bu: bulk-read parameters and results
1435 * @node: the node is returned here
1436 * @lnum: LEB number is returned here
1437 * @offs: offset is returned here
1438 * 1502 *
1439 * This function is the same as 'ubifs_tnc_lookup()' but it returns the node 1503 * Lookup consecutive data node keys for the same inode that reside
1440 * location also. See 'ubifs_tnc_lookup()'. 1504 * consecutively in the same LEB.
1441 */ 1505 */
1442int ubifs_tnc_locate(struct ubifs_info *c, const union ubifs_key *key, 1506int ubifs_tnc_get_bu_keys(struct ubifs_info *c, struct bu_info *bu)
1443 void *node, int *lnum, int *offs)
1444{ 1507{
1445 int found, n, err; 1508 int n, err = 0, lnum = -1, uninitialized_var(offs);
1509 int uninitialized_var(len);
1510 unsigned int block = key_block(c, &bu->key);
1446 struct ubifs_znode *znode; 1511 struct ubifs_znode *znode;
1447 struct ubifs_zbranch zbr, *zt; 1512
1513 bu->cnt = 0;
1514 bu->blk_cnt = 0;
1515 bu->eof = 0;
1448 1516
1449 mutex_lock(&c->tnc_mutex); 1517 mutex_lock(&c->tnc_mutex);
1450 found = ubifs_lookup_level0(c, key, &znode, &n); 1518 /* Find first key */
1451 if (!found) { 1519 err = ubifs_lookup_level0(c, &bu->key, &znode, &n);
1452 err = -ENOENT; 1520 if (err < 0)
1453 goto out;
1454 } else if (found < 0) {
1455 err = found;
1456 goto out; 1521 goto out;
1522 if (err) {
1523 /* Key found */
1524 len = znode->zbranch[n].len;
1525 /* The buffer must be big enough for at least 1 node */
1526 if (len > bu->buf_len) {
1527 err = -EINVAL;
1528 goto out;
1529 }
1530 /* Add this key */
1531 bu->zbranch[bu->cnt++] = znode->zbranch[n];
1532 bu->blk_cnt += 1;
1533 lnum = znode->zbranch[n].lnum;
1534 offs = ALIGN(znode->zbranch[n].offs + len, 8);
1457 } 1535 }
1458 zt = &znode->zbranch[n]; 1536 while (1) {
1459 if (is_hash_key(c, key)) { 1537 struct ubifs_zbranch *zbr;
1460 /* 1538 union ubifs_key *key;
1461 * In this case the leaf node cache gets used, so we pass the 1539 unsigned int next_block;
1462 * address of the zbranch and keep the mutex locked 1540
1463 */ 1541 /* Find next key */
1464 *lnum = zt->lnum; 1542 err = tnc_next(c, &znode, &n);
1465 *offs = zt->offs; 1543 if (err)
1466 err = tnc_read_node_nm(c, zt, node); 1544 goto out;
1467 goto out; 1545 zbr = &znode->zbranch[n];
1546 key = &zbr->key;
1547 /* See if there is another data key for this file */
1548 if (key_inum(c, key) != key_inum(c, &bu->key) ||
1549 key_type(c, key) != UBIFS_DATA_KEY) {
1550 err = -ENOENT;
1551 goto out;
1552 }
1553 if (lnum < 0) {
1554 /* First key found */
1555 lnum = zbr->lnum;
1556 offs = ALIGN(zbr->offs + zbr->len, 8);
1557 len = zbr->len;
1558 if (len > bu->buf_len) {
1559 err = -EINVAL;
1560 goto out;
1561 }
1562 } else {
1563 /*
1564 * The data nodes must be in consecutive positions in
1565 * the same LEB.
1566 */
1567 if (zbr->lnum != lnum || zbr->offs != offs)
1568 goto out;
1569 offs += ALIGN(zbr->len, 8);
1570 len = ALIGN(len, 8) + zbr->len;
1571 /* Must not exceed buffer length */
1572 if (len > bu->buf_len)
1573 goto out;
1574 }
1575 /* Allow for holes */
1576 next_block = key_block(c, key);
1577 bu->blk_cnt += (next_block - block - 1);
1578 if (bu->blk_cnt >= UBIFS_MAX_BULK_READ)
1579 goto out;
1580 block = next_block;
1581 /* Add this key */
1582 bu->zbranch[bu->cnt++] = *zbr;
1583 bu->blk_cnt += 1;
1584 /* See if we have room for more */
1585 if (bu->cnt >= UBIFS_MAX_BULK_READ)
1586 goto out;
1587 if (bu->blk_cnt >= UBIFS_MAX_BULK_READ)
1588 goto out;
1468 } 1589 }
1469 zbr = znode->zbranch[n]; 1590out:
1591 if (err == -ENOENT) {
1592 bu->eof = 1;
1593 err = 0;
1594 }
1595 bu->gc_seq = c->gc_seq;
1470 mutex_unlock(&c->tnc_mutex); 1596 mutex_unlock(&c->tnc_mutex);
1597 if (err)
1598 return err;
1599 /*
1600 * An enormous hole could cause bulk-read to encompass too many
1601 * page cache pages, so limit the number here.
1602 */
1603 if (bu->blk_cnt > UBIFS_MAX_BULK_READ)
1604 bu->blk_cnt = UBIFS_MAX_BULK_READ;
1605 /*
1606 * Ensure that bulk-read covers a whole number of page cache
1607 * pages.
1608 */
1609 if (UBIFS_BLOCKS_PER_PAGE == 1 ||
1610 !(bu->blk_cnt & (UBIFS_BLOCKS_PER_PAGE - 1)))
1611 return 0;
1612 if (bu->eof) {
1613 /* At the end of file we can round up */
1614 bu->blk_cnt += UBIFS_BLOCKS_PER_PAGE - 1;
1615 return 0;
1616 }
1617 /* Exclude data nodes that do not make up a whole page cache page */
1618 block = key_block(c, &bu->key) + bu->blk_cnt;
1619 block &= ~(UBIFS_BLOCKS_PER_PAGE - 1);
1620 while (bu->cnt) {
1621 if (key_block(c, &bu->zbranch[bu->cnt - 1].key) < block)
1622 break;
1623 bu->cnt -= 1;
1624 }
1625 return 0;
1626}
1471 1627
1472 *lnum = zbr.lnum; 1628/**
1473 *offs = zbr.offs; 1629 * read_wbuf - bulk-read from a LEB with a wbuf.
1630 * @wbuf: wbuf that may overlap the read
1631 * @buf: buffer into which to read
1632 * @len: read length
1633 * @lnum: LEB number from which to read
1634 * @offs: offset from which to read
1635 *
1636 * This functions returns %0 on success or a negative error code on failure.
1637 */
1638static int read_wbuf(struct ubifs_wbuf *wbuf, void *buf, int len, int lnum,
1639 int offs)
1640{
1641 const struct ubifs_info *c = wbuf->c;
1642 int rlen, overlap;
1474 1643
1475 err = ubifs_tnc_read_node(c, &zbr, node); 1644 dbg_io("LEB %d:%d, length %d", lnum, offs, len);
1476 return err; 1645 ubifs_assert(wbuf && lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
1646 ubifs_assert(!(offs & 7) && offs < c->leb_size);
1647 ubifs_assert(offs + len <= c->leb_size);
1648
1649 spin_lock(&wbuf->lock);
1650 overlap = (lnum == wbuf->lnum && offs + len > wbuf->offs);
1651 if (!overlap) {
1652 /* We may safely unlock the write-buffer and read the data */
1653 spin_unlock(&wbuf->lock);
1654 return ubi_read(c->ubi, lnum, buf, offs, len);
1655 }
1656
1657 /* Don't read under wbuf */
1658 rlen = wbuf->offs - offs;
1659 if (rlen < 0)
1660 rlen = 0;
1661
1662 /* Copy the rest from the write-buffer */
1663 memcpy(buf + rlen, wbuf->buf + offs + rlen - wbuf->offs, len - rlen);
1664 spin_unlock(&wbuf->lock);
1665
1666 if (rlen > 0)
1667 /* Read everything that goes before write-buffer */
1668 return ubi_read(c->ubi, lnum, buf, offs, rlen);
1669
1670 return 0;
1671}
1672
1673/**
1674 * validate_data_node - validate data nodes for bulk-read.
1675 * @c: UBIFS file-system description object
1676 * @buf: buffer containing data node to validate
1677 * @zbr: zbranch of data node to validate
1678 *
1679 * This functions returns %0 on success or a negative error code on failure.
1680 */
1681static int validate_data_node(struct ubifs_info *c, void *buf,
1682 struct ubifs_zbranch *zbr)
1683{
1684 union ubifs_key key1;
1685 struct ubifs_ch *ch = buf;
1686 int err, len;
1687
1688 if (ch->node_type != UBIFS_DATA_NODE) {
1689 ubifs_err("bad node type (%d but expected %d)",
1690 ch->node_type, UBIFS_DATA_NODE);
1691 goto out_err;
1692 }
1693
1694 err = ubifs_check_node(c, buf, zbr->lnum, zbr->offs, 0, 0);
1695 if (err) {
1696 ubifs_err("expected node type %d", UBIFS_DATA_NODE);
1697 goto out;
1698 }
1699
1700 len = le32_to_cpu(ch->len);
1701 if (len != zbr->len) {
1702 ubifs_err("bad node length %d, expected %d", len, zbr->len);
1703 goto out_err;
1704 }
1705
1706 /* Make sure the key of the read node is correct */
1707 key_read(c, buf + UBIFS_KEY_OFFSET, &key1);
1708 if (!keys_eq(c, &zbr->key, &key1)) {
1709 ubifs_err("bad key in node at LEB %d:%d",
1710 zbr->lnum, zbr->offs);
1711 dbg_tnc("looked for key %s found node's key %s",
1712 DBGKEY(&zbr->key), DBGKEY1(&key1));
1713 goto out_err;
1714 }
1477 1715
1716 return 0;
1717
1718out_err:
1719 err = -EINVAL;
1478out: 1720out:
1479 mutex_unlock(&c->tnc_mutex); 1721 ubifs_err("bad node at LEB %d:%d", zbr->lnum, zbr->offs);
1722 dbg_dump_node(c, buf);
1723 dbg_dump_stack();
1480 return err; 1724 return err;
1481} 1725}
1482 1726
1483/** 1727/**
1728 * ubifs_tnc_bulk_read - read a number of data nodes in one go.
1729 * @c: UBIFS file-system description object
1730 * @bu: bulk-read parameters and results
1731 *
1732 * This functions reads and validates the data nodes that were identified by the
1733 * 'ubifs_tnc_get_bu_keys()' function. This functions returns %0 on success,
1734 * -EAGAIN to indicate a race with GC, or another negative error code on
1735 * failure.
1736 */
1737int ubifs_tnc_bulk_read(struct ubifs_info *c, struct bu_info *bu)
1738{
1739 int lnum = bu->zbranch[0].lnum, offs = bu->zbranch[0].offs, len, err, i;
1740 struct ubifs_wbuf *wbuf;
1741 void *buf;
1742
1743 len = bu->zbranch[bu->cnt - 1].offs;
1744 len += bu->zbranch[bu->cnt - 1].len - offs;
1745 if (len > bu->buf_len) {
1746 ubifs_err("buffer too small %d vs %d", bu->buf_len, len);
1747 return -EINVAL;
1748 }
1749
1750 /* Do the read */
1751 wbuf = ubifs_get_wbuf(c, lnum);
1752 if (wbuf)
1753 err = read_wbuf(wbuf, bu->buf, len, lnum, offs);
1754 else
1755 err = ubi_read(c->ubi, lnum, bu->buf, offs, len);
1756
1757 /* Check for a race with GC */
1758 if (maybe_leb_gced(c, lnum, bu->gc_seq))
1759 return -EAGAIN;
1760
1761 if (err && err != -EBADMSG) {
1762 ubifs_err("failed to read from LEB %d:%d, error %d",
1763 lnum, offs, err);
1764 dbg_dump_stack();
1765 dbg_tnc("key %s", DBGKEY(&bu->key));
1766 return err;
1767 }
1768
1769 /* Validate the nodes read */
1770 buf = bu->buf;
1771 for (i = 0; i < bu->cnt; i++) {
1772 err = validate_data_node(c, buf, &bu->zbranch[i]);
1773 if (err)
1774 return err;
1775 buf = buf + ALIGN(bu->zbranch[i].len, 8);
1776 }
1777
1778 return 0;
1779}
1780
1781/**
1484 * do_lookup_nm- look up a "hashed" node. 1782 * do_lookup_nm- look up a "hashed" node.
1485 * @c: UBIFS file-system description object 1783 * @c: UBIFS file-system description object
1486 * @key: node key to lookup 1784 * @key: node key to lookup
@@ -1498,7 +1796,6 @@ static int do_lookup_nm(struct ubifs_info *c, const union ubifs_key *key,
1498{ 1796{
1499 int found, n, err; 1797 int found, n, err;
1500 struct ubifs_znode *znode; 1798 struct ubifs_znode *znode;
1501 struct ubifs_zbranch zbr;
1502 1799
1503 dbg_tnc("name '%.*s' key %s", nm->len, nm->name, DBGKEY(key)); 1800 dbg_tnc("name '%.*s' key %s", nm->len, nm->name, DBGKEY(key));
1504 mutex_lock(&c->tnc_mutex); 1801 mutex_lock(&c->tnc_mutex);
@@ -1522,11 +1819,7 @@ static int do_lookup_nm(struct ubifs_info *c, const union ubifs_key *key,
1522 goto out_unlock; 1819 goto out_unlock;
1523 } 1820 }
1524 1821
1525 zbr = znode->zbranch[n]; 1822 err = tnc_read_node_nm(c, &znode->zbranch[n], node);
1526 mutex_unlock(&c->tnc_mutex);
1527
1528 err = tnc_read_node_nm(c, &zbr, node);
1529 return err;
1530 1823
1531out_unlock: 1824out_unlock:
1532 mutex_unlock(&c->tnc_mutex); 1825 mutex_unlock(&c->tnc_mutex);
@@ -1669,7 +1962,7 @@ static int tnc_insert(struct ubifs_info *c, struct ubifs_znode *znode,
1669{ 1962{
1670 struct ubifs_znode *zn, *zi, *zp; 1963 struct ubifs_znode *zn, *zi, *zp;
1671 int i, keep, move, appending = 0; 1964 int i, keep, move, appending = 0;
1672 union ubifs_key *key = &zbr->key; 1965 union ubifs_key *key = &zbr->key, *key1;
1673 1966
1674 ubifs_assert(n >= 0 && n <= c->fanout); 1967 ubifs_assert(n >= 0 && n <= c->fanout);
1675 1968
@@ -1710,20 +2003,33 @@ again:
1710 zn->level = znode->level; 2003 zn->level = znode->level;
1711 2004
1712 /* Decide where to split */ 2005 /* Decide where to split */
1713 if (znode->level == 0 && n == c->fanout && 2006 if (znode->level == 0 && key_type(c, key) == UBIFS_DATA_KEY) {
1714 key_type(c, key) == UBIFS_DATA_KEY) { 2007 /* Try not to split consecutive data keys */
1715 union ubifs_key *key1; 2008 if (n == c->fanout) {
1716 2009 key1 = &znode->zbranch[n - 1].key;
1717 /* 2010 if (key_inum(c, key1) == key_inum(c, key) &&
1718 * If this is an inode which is being appended - do not split 2011 key_type(c, key1) == UBIFS_DATA_KEY)
1719 * it because no other zbranches can be inserted between 2012 appending = 1;
1720 * zbranches of consecutive data nodes anyway. 2013 } else
1721 */ 2014 goto check_split;
1722 key1 = &znode->zbranch[n - 1].key; 2015 } else if (appending && n != c->fanout) {
1723 if (key_inum(c, key1) == key_inum(c, key) && 2016 /* Try not to split consecutive data keys */
1724 key_type(c, key1) == UBIFS_DATA_KEY && 2017 appending = 0;
1725 key_block(c, key1) == key_block(c, key) - 1) 2018check_split:
1726 appending = 1; 2019 if (n >= (c->fanout + 1) / 2) {
2020 key1 = &znode->zbranch[0].key;
2021 if (key_inum(c, key1) == key_inum(c, key) &&
2022 key_type(c, key1) == UBIFS_DATA_KEY) {
2023 key1 = &znode->zbranch[n].key;
2024 if (key_inum(c, key1) != key_inum(c, key) ||
2025 key_type(c, key1) != UBIFS_DATA_KEY) {
2026 keep = n;
2027 move = c->fanout - keep;
2028 zi = znode;
2029 goto do_split;
2030 }
2031 }
2032 }
1727 } 2033 }
1728 2034
1729 if (appending) { 2035 if (appending) {
@@ -1753,6 +2059,8 @@ again:
1753 zbr->znode->parent = zn; 2059 zbr->znode->parent = zn;
1754 } 2060 }
1755 2061
2062do_split:
2063
1756 __set_bit(DIRTY_ZNODE, &zn->flags); 2064 __set_bit(DIRTY_ZNODE, &zn->flags);
1757 atomic_long_inc(&c->dirty_zn_cnt); 2065 atomic_long_inc(&c->dirty_zn_cnt);
1758 2066
@@ -1779,14 +2087,11 @@ again:
1779 2087
1780 /* Insert new znode (produced by spitting) into the parent */ 2088 /* Insert new znode (produced by spitting) into the parent */
1781 if (zp) { 2089 if (zp) {
1782 i = n; 2090 if (n == 0 && zi == znode && znode->iip == 0)
2091 correct_parent_keys(c, znode);
2092
1783 /* Locate insertion point */ 2093 /* Locate insertion point */
1784 n = znode->iip + 1; 2094 n = znode->iip + 1;
1785 if (appending && n != c->fanout)
1786 appending = 0;
1787
1788 if (i == 0 && zi == znode && znode->iip == 0)
1789 correct_parent_keys(c, znode);
1790 2095
1791 /* Tail recursion */ 2096 /* Tail recursion */
1792 zbr->key = zn->zbranch[0].key; 2097 zbr->key = zn->zbranch[0].key;
diff --git a/fs/ubifs/tnc_commit.c b/fs/ubifs/tnc_commit.c
index 8117e65ba2e9..8ac76b1c2d55 100644
--- a/fs/ubifs/tnc_commit.c
+++ b/fs/ubifs/tnc_commit.c
@@ -372,26 +372,25 @@ static int layout_in_gaps(struct ubifs_info *c, int cnt)
372 written = layout_leb_in_gaps(c, p); 372 written = layout_leb_in_gaps(c, p);
373 if (written < 0) { 373 if (written < 0) {
374 err = written; 374 err = written;
375 if (err == -ENOSPC) { 375 if (err != -ENOSPC) {
376 if (!dbg_force_in_the_gaps_enabled) { 376 kfree(c->gap_lebs);
377 /* 377 c->gap_lebs = NULL;
378 * Do not print scary warnings if the 378 return err;
379 * debugging option which forces
380 * in-the-gaps is enabled.
381 */
382 ubifs_err("out of space");
383 spin_lock(&c->space_lock);
384 dbg_dump_budg(c);
385 spin_unlock(&c->space_lock);
386 dbg_dump_lprops(c);
387 }
388 /* Try to commit anyway */
389 err = 0;
390 break;
391 } 379 }
392 kfree(c->gap_lebs); 380 if (!dbg_force_in_the_gaps_enabled) {
393 c->gap_lebs = NULL; 381 /*
394 return err; 382 * Do not print scary warnings if the debugging
383 * option which forces in-the-gaps is enabled.
384 */
385 ubifs_err("out of space");
386 spin_lock(&c->space_lock);
387 dbg_dump_budg(c);
388 spin_unlock(&c->space_lock);
389 dbg_dump_lprops(c);
390 }
391 /* Try to commit anyway */
392 err = 0;
393 break;
395 } 394 }
396 p++; 395 p++;
397 cnt -= written; 396 cnt -= written;
diff --git a/fs/ubifs/tnc_misc.c b/fs/ubifs/tnc_misc.c
index a25c1cc1f8d9..b48db999903e 100644
--- a/fs/ubifs/tnc_misc.c
+++ b/fs/ubifs/tnc_misc.c
@@ -480,8 +480,8 @@ int ubifs_tnc_read_node(struct ubifs_info *c, struct ubifs_zbranch *zbr,
480 } 480 }
481 481
482 /* Make sure the key of the read node is correct */ 482 /* Make sure the key of the read node is correct */
483 key_read(c, key, &key1); 483 key_read(c, node + UBIFS_KEY_OFFSET, &key1);
484 if (memcmp(node + UBIFS_KEY_OFFSET, &key1, c->key_len)) { 484 if (!keys_eq(c, key, &key1)) {
485 ubifs_err("bad key in node at LEB %d:%d", 485 ubifs_err("bad key in node at LEB %d:%d",
486 zbr->lnum, zbr->offs); 486 zbr->lnum, zbr->offs);
487 dbg_tnc("looked for key %s found node's key %s", 487 dbg_tnc("looked for key %s found node's key %s",
diff --git a/fs/ubifs/ubifs-media.h b/fs/ubifs/ubifs-media.h
index 0cc7da9bed47..0b378042a3a2 100644
--- a/fs/ubifs/ubifs-media.h
+++ b/fs/ubifs/ubifs-media.h
@@ -75,7 +75,6 @@
75 */ 75 */
76#define UBIFS_BLOCK_SIZE 4096 76#define UBIFS_BLOCK_SIZE 4096
77#define UBIFS_BLOCK_SHIFT 12 77#define UBIFS_BLOCK_SHIFT 12
78#define UBIFS_BLOCK_MASK 0x00000FFF
79 78
80/* UBIFS padding byte pattern (must not be first or last byte of node magic) */ 79/* UBIFS padding byte pattern (must not be first or last byte of node magic) */
81#define UBIFS_PADDING_BYTE 0xCE 80#define UBIFS_PADDING_BYTE 0xCE
@@ -87,7 +86,7 @@
87#define UBIFS_SK_LEN 8 86#define UBIFS_SK_LEN 8
88 87
89/* Minimum index tree fanout */ 88/* Minimum index tree fanout */
90#define UBIFS_MIN_FANOUT 2 89#define UBIFS_MIN_FANOUT 3
91 90
92/* Maximum number of levels in UBIFS indexing B-tree */ 91/* Maximum number of levels in UBIFS indexing B-tree */
93#define UBIFS_MAX_LEVELS 512 92#define UBIFS_MAX_LEVELS 512
@@ -228,10 +227,10 @@ enum {
228/* Minimum number of orphan area logical eraseblocks */ 227/* Minimum number of orphan area logical eraseblocks */
229#define UBIFS_MIN_ORPH_LEBS 1 228#define UBIFS_MIN_ORPH_LEBS 1
230/* 229/*
231 * Minimum number of main area logical eraseblocks (buds, 2 for the index, 1 230 * Minimum number of main area logical eraseblocks (buds, 3 for the index, 1
232 * for GC, 1 for deletions, and at least 1 for committed data). 231 * for GC, 1 for deletions, and at least 1 for committed data).
233 */ 232 */
234#define UBIFS_MIN_MAIN_LEBS (UBIFS_MIN_BUD_LEBS + 5) 233#define UBIFS_MIN_MAIN_LEBS (UBIFS_MIN_BUD_LEBS + 6)
235 234
236/* Minimum number of logical eraseblocks */ 235/* Minimum number of logical eraseblocks */
237#define UBIFS_MIN_LEB_CNT (UBIFS_SB_LEBS + UBIFS_MST_LEBS + \ 236#define UBIFS_MIN_LEB_CNT (UBIFS_SB_LEBS + UBIFS_MST_LEBS + \
diff --git a/fs/ubifs/ubifs.h b/fs/ubifs/ubifs.h
index e4f89f271827..a7bd32fa15b9 100644
--- a/fs/ubifs/ubifs.h
+++ b/fs/ubifs/ubifs.h
@@ -20,8 +20,6 @@
20 * Adrian Hunter 20 * Adrian Hunter
21 */ 21 */
22 22
23/* Implementation version 0.7 */
24
25#ifndef __UBIFS_H__ 23#ifndef __UBIFS_H__
26#define __UBIFS_H__ 24#define __UBIFS_H__
27 25
@@ -144,6 +142,9 @@
144/* Maximum expected tree height for use by bottom_up_buf */ 142/* Maximum expected tree height for use by bottom_up_buf */
145#define BOTTOM_UP_HEIGHT 64 143#define BOTTOM_UP_HEIGHT 64
146 144
145/* Maximum number of data nodes to bulk-read */
146#define UBIFS_MAX_BULK_READ 32
147
147/* 148/*
148 * Lockdep classes for UBIFS inode @ui_mutex. 149 * Lockdep classes for UBIFS inode @ui_mutex.
149 */ 150 */
@@ -322,15 +323,18 @@ struct ubifs_gced_idx_leb {
322 * struct ubifs_inode - UBIFS in-memory inode description. 323 * struct ubifs_inode - UBIFS in-memory inode description.
323 * @vfs_inode: VFS inode description object 324 * @vfs_inode: VFS inode description object
324 * @creat_sqnum: sequence number at time of creation 325 * @creat_sqnum: sequence number at time of creation
326 * @del_cmtno: commit number corresponding to the time the inode was deleted,
327 * protected by @c->commit_sem;
325 * @xattr_size: summarized size of all extended attributes in bytes 328 * @xattr_size: summarized size of all extended attributes in bytes
326 * @xattr_cnt: count of extended attributes this inode has 329 * @xattr_cnt: count of extended attributes this inode has
327 * @xattr_names: sum of lengths of all extended attribute names belonging to 330 * @xattr_names: sum of lengths of all extended attribute names belonging to
328 * this inode 331 * this inode
329 * @dirty: non-zero if the inode is dirty 332 * @dirty: non-zero if the inode is dirty
330 * @xattr: non-zero if this is an extended attribute inode 333 * @xattr: non-zero if this is an extended attribute inode
334 * @bulk_read: non-zero if bulk-read should be used
331 * @ui_mutex: serializes inode write-back with the rest of VFS operations, 335 * @ui_mutex: serializes inode write-back with the rest of VFS operations,
332 * serializes "clean <-> dirty" state changes, protects @dirty, 336 * serializes "clean <-> dirty" state changes, serializes bulk-read,
333 * @ui_size, and @xattr_size 337 * protects @dirty, @bulk_read, @ui_size, and @xattr_size
334 * @ui_lock: protects @synced_i_size 338 * @ui_lock: protects @synced_i_size
335 * @synced_i_size: synchronized size of inode, i.e. the value of inode size 339 * @synced_i_size: synchronized size of inode, i.e. the value of inode size
336 * currently stored on the flash; used only for regular file 340 * currently stored on the flash; used only for regular file
@@ -338,6 +342,8 @@ struct ubifs_gced_idx_leb {
338 * @ui_size: inode size used by UBIFS when writing to flash 342 * @ui_size: inode size used by UBIFS when writing to flash
339 * @flags: inode flags (@UBIFS_COMPR_FL, etc) 343 * @flags: inode flags (@UBIFS_COMPR_FL, etc)
340 * @compr_type: default compression type used for this inode 344 * @compr_type: default compression type used for this inode
345 * @last_page_read: page number of last page read (for bulk read)
346 * @read_in_a_row: number of consecutive pages read in a row (for bulk read)
341 * @data_len: length of the data attached to the inode 347 * @data_len: length of the data attached to the inode
342 * @data: inode's data 348 * @data: inode's data
343 * 349 *
@@ -373,17 +379,21 @@ struct ubifs_gced_idx_leb {
373struct ubifs_inode { 379struct ubifs_inode {
374 struct inode vfs_inode; 380 struct inode vfs_inode;
375 unsigned long long creat_sqnum; 381 unsigned long long creat_sqnum;
382 unsigned long long del_cmtno;
376 unsigned int xattr_size; 383 unsigned int xattr_size;
377 unsigned int xattr_cnt; 384 unsigned int xattr_cnt;
378 unsigned int xattr_names; 385 unsigned int xattr_names;
379 unsigned int dirty:1; 386 unsigned int dirty:1;
380 unsigned int xattr:1; 387 unsigned int xattr:1;
388 unsigned int bulk_read:1;
381 struct mutex ui_mutex; 389 struct mutex ui_mutex;
382 spinlock_t ui_lock; 390 spinlock_t ui_lock;
383 loff_t synced_i_size; 391 loff_t synced_i_size;
384 loff_t ui_size; 392 loff_t ui_size;
385 int flags; 393 int flags;
386 int compr_type; 394 int compr_type;
395 pgoff_t last_page_read;
396 pgoff_t read_in_a_row;
387 int data_len; 397 int data_len;
388 void *data; 398 void *data;
389}; 399};
@@ -697,8 +707,8 @@ struct ubifs_jhead {
697 * struct ubifs_zbranch - key/coordinate/length branch stored in znodes. 707 * struct ubifs_zbranch - key/coordinate/length branch stored in znodes.
698 * @key: key 708 * @key: key
699 * @znode: znode address in memory 709 * @znode: znode address in memory
700 * @lnum: LEB number of the indexing node 710 * @lnum: LEB number of the target node (indexing node or data node)
701 * @offs: offset of the indexing node within @lnum 711 * @offs: target node offset within @lnum
702 * @len: target node length 712 * @len: target node length
703 */ 713 */
704struct ubifs_zbranch { 714struct ubifs_zbranch {
@@ -743,6 +753,28 @@ struct ubifs_znode {
743}; 753};
744 754
745/** 755/**
756 * struct bu_info - bulk-read information
757 * @key: first data node key
758 * @zbranch: zbranches of data nodes to bulk read
759 * @buf: buffer to read into
760 * @buf_len: buffer length
761 * @gc_seq: GC sequence number to detect races with GC
762 * @cnt: number of data nodes for bulk read
763 * @blk_cnt: number of data blocks including holes
764 * @oef: end of file reached
765 */
766struct bu_info {
767 union ubifs_key key;
768 struct ubifs_zbranch zbranch[UBIFS_MAX_BULK_READ];
769 void *buf;
770 int buf_len;
771 int gc_seq;
772 int cnt;
773 int blk_cnt;
774 int eof;
775};
776
777/**
746 * struct ubifs_node_range - node length range description data structure. 778 * struct ubifs_node_range - node length range description data structure.
747 * @len: fixed node length 779 * @len: fixed node length
748 * @min_len: minimum possible node length 780 * @min_len: minimum possible node length
@@ -779,7 +811,7 @@ struct ubifs_compressor {
779/** 811/**
780 * struct ubifs_budget_req - budget requirements of an operation. 812 * struct ubifs_budget_req - budget requirements of an operation.
781 * 813 *
782 * @fast: non-zero if the budgeting should try to aquire budget quickly and 814 * @fast: non-zero if the budgeting should try to acquire budget quickly and
783 * should not try to call write-back 815 * should not try to call write-back
784 * @recalculate: non-zero if @idx_growth, @data_growth, and @dd_growth fields 816 * @recalculate: non-zero if @idx_growth, @data_growth, and @dd_growth fields
785 * have to be re-calculated 817 * have to be re-calculated
@@ -805,21 +837,31 @@ struct ubifs_compressor {
805 * An inode may contain 4KiB of data at max., thus the widths of @new_ino_d 837 * An inode may contain 4KiB of data at max., thus the widths of @new_ino_d
806 * is 13 bits, and @dirtied_ino_d - 15, because up to 4 inodes may be made 838 * is 13 bits, and @dirtied_ino_d - 15, because up to 4 inodes may be made
807 * dirty by the re-name operation. 839 * dirty by the re-name operation.
840 *
841 * Note, UBIFS aligns node lengths to 8-bytes boundary, so the requester has to
842 * make sure the amount of inode data which contribute to @new_ino_d and
843 * @dirtied_ino_d fields are aligned.
808 */ 844 */
809struct ubifs_budget_req { 845struct ubifs_budget_req {
810 unsigned int fast:1; 846 unsigned int fast:1;
811 unsigned int recalculate:1; 847 unsigned int recalculate:1;
848#ifndef UBIFS_DEBUG
812 unsigned int new_page:1; 849 unsigned int new_page:1;
813 unsigned int dirtied_page:1; 850 unsigned int dirtied_page:1;
814 unsigned int new_dent:1; 851 unsigned int new_dent:1;
815 unsigned int mod_dent:1; 852 unsigned int mod_dent:1;
816 unsigned int new_ino:1; 853 unsigned int new_ino:1;
817 unsigned int new_ino_d:13; 854 unsigned int new_ino_d:13;
818#ifndef UBIFS_DEBUG
819 unsigned int dirtied_ino:4; 855 unsigned int dirtied_ino:4;
820 unsigned int dirtied_ino_d:15; 856 unsigned int dirtied_ino_d:15;
821#else 857#else
822 /* Not bit-fields to check for overflows */ 858 /* Not bit-fields to check for overflows */
859 unsigned int new_page;
860 unsigned int dirtied_page;
861 unsigned int new_dent;
862 unsigned int mod_dent;
863 unsigned int new_ino;
864 unsigned int new_ino_d;
823 unsigned int dirtied_ino; 865 unsigned int dirtied_ino;
824 unsigned int dirtied_ino_d; 866 unsigned int dirtied_ino_d;
825#endif 867#endif
@@ -851,22 +893,26 @@ struct ubifs_orphan {
851/** 893/**
852 * struct ubifs_mount_opts - UBIFS-specific mount options information. 894 * struct ubifs_mount_opts - UBIFS-specific mount options information.
853 * @unmount_mode: selected unmount mode (%0 default, %1 normal, %2 fast) 895 * @unmount_mode: selected unmount mode (%0 default, %1 normal, %2 fast)
896 * @bulk_read: enable bulk-reads
897 * @chk_data_crc: check CRCs when reading data nodes
854 */ 898 */
855struct ubifs_mount_opts { 899struct ubifs_mount_opts {
856 unsigned int unmount_mode:2; 900 unsigned int unmount_mode:2;
901 unsigned int bulk_read:2;
902 unsigned int chk_data_crc:2;
857}; 903};
858 904
859/** 905/**
860 * struct ubifs_info - UBIFS file-system description data structure 906 * struct ubifs_info - UBIFS file-system description data structure
861 * (per-superblock). 907 * (per-superblock).
862 * @vfs_sb: VFS @struct super_block object 908 * @vfs_sb: VFS @struct super_block object
863 * @bdi: backing device info object to make VFS happy and disable readahead 909 * @bdi: backing device info object to make VFS happy and disable read-ahead
864 * 910 *
865 * @highest_inum: highest used inode number 911 * @highest_inum: highest used inode number
866 * @vfs_gen: VFS inode generation counter
867 * @max_sqnum: current global sequence number 912 * @max_sqnum: current global sequence number
868 * @cmt_no: commit number (last successfully completed commit) 913 * @cmt_no: commit number of the last successfully completed commit, protected
869 * @cnt_lock: protects @highest_inum, @vfs_gen, and @max_sqnum counters 914 * by @commit_sem
915 * @cnt_lock: protects @highest_inum and @max_sqnum counters
870 * @fmt_version: UBIFS on-flash format version 916 * @fmt_version: UBIFS on-flash format version
871 * @uuid: UUID from super block 917 * @uuid: UUID from super block
872 * 918 *
@@ -894,13 +940,12 @@ struct ubifs_mount_opts {
894 * @cmt_state: commit state 940 * @cmt_state: commit state
895 * @cs_lock: commit state lock 941 * @cs_lock: commit state lock
896 * @cmt_wq: wait queue to sleep on if the log is full and a commit is running 942 * @cmt_wq: wait queue to sleep on if the log is full and a commit is running
943 *
897 * @fast_unmount: do not run journal commit before un-mounting 944 * @fast_unmount: do not run journal commit before un-mounting
898 * @big_lpt: flag that LPT is too big to write whole during commit 945 * @big_lpt: flag that LPT is too big to write whole during commit
899 * @check_lpt_free: flag that indicates LPT GC may be needed 946 * @no_chk_data_crc: do not check CRCs when reading data nodes (except during
900 * @nospace: non-zero if the file-system does not have flash space (used as 947 * recovery)
901 * optimization) 948 * @bulk_read: enable bulk-reads
902 * @nospace_rp: the same as @nospace, but additionally means that even reserved
903 * pool is full
904 * 949 *
905 * @tnc_mutex: protects the Tree Node Cache (TNC), @zroot, @cnext, @enext, and 950 * @tnc_mutex: protects the Tree Node Cache (TNC), @zroot, @cnext, @enext, and
906 * @calc_idx_sz 951 * @calc_idx_sz
@@ -924,6 +969,7 @@ struct ubifs_mount_opts {
924 * @mst_node: master node 969 * @mst_node: master node
925 * @mst_offs: offset of valid master node 970 * @mst_offs: offset of valid master node
926 * @mst_mutex: protects the master node area, @mst_node, and @mst_offs 971 * @mst_mutex: protects the master node area, @mst_node, and @mst_offs
972 * @bulk_read_buf_size: buffer size for bulk-reads
927 * 973 *
928 * @log_lebs: number of logical eraseblocks in the log 974 * @log_lebs: number of logical eraseblocks in the log
929 * @log_bytes: log size in bytes 975 * @log_bytes: log size in bytes
@@ -966,12 +1012,17 @@ struct ubifs_mount_opts {
966 * but which still have to be taken into account because 1012 * but which still have to be taken into account because
967 * the index has not been committed so far 1013 * the index has not been committed so far
968 * @space_lock: protects @budg_idx_growth, @budg_data_growth, @budg_dd_growth, 1014 * @space_lock: protects @budg_idx_growth, @budg_data_growth, @budg_dd_growth,
969 * @budg_uncommited_idx, @min_idx_lebs, @old_idx_sz, and @lst; 1015 * @budg_uncommited_idx, @min_idx_lebs, @old_idx_sz, @lst,
1016 * @nospace, and @nospace_rp;
970 * @min_idx_lebs: minimum number of LEBs required for the index 1017 * @min_idx_lebs: minimum number of LEBs required for the index
971 * @old_idx_sz: size of index on flash 1018 * @old_idx_sz: size of index on flash
972 * @calc_idx_sz: temporary variable which is used to calculate new index size 1019 * @calc_idx_sz: temporary variable which is used to calculate new index size
973 * (contains accurate new index size at end of TNC commit start) 1020 * (contains accurate new index size at end of TNC commit start)
974 * @lst: lprops statistics 1021 * @lst: lprops statistics
1022 * @nospace: non-zero if the file-system does not have flash space (used as
1023 * optimization)
1024 * @nospace_rp: the same as @nospace, but additionally means that even reserved
1025 * pool is full
975 * 1026 *
976 * @page_budget: budget for a page 1027 * @page_budget: budget for a page
977 * @inode_budget: budget for an inode 1028 * @inode_budget: budget for an inode
@@ -984,6 +1035,9 @@ struct ubifs_mount_opts {
984 * @max_idx_node_sz: maximum indexing node aligned on 8-bytes boundary 1035 * @max_idx_node_sz: maximum indexing node aligned on 8-bytes boundary
985 * @max_inode_sz: maximum possible inode size in bytes 1036 * @max_inode_sz: maximum possible inode size in bytes
986 * @max_znode_sz: size of znode in bytes 1037 * @max_znode_sz: size of znode in bytes
1038 *
1039 * @leb_overhead: how many bytes are wasted in an LEB when it is filled with
1040 * data nodes of maximum size - used in free space reporting
987 * @dead_wm: LEB dead space watermark 1041 * @dead_wm: LEB dead space watermark
988 * @dark_wm: LEB dark space watermark 1042 * @dark_wm: LEB dark space watermark
989 * @block_cnt: count of 4KiB blocks on the FS 1043 * @block_cnt: count of 4KiB blocks on the FS
@@ -1017,6 +1071,8 @@ struct ubifs_mount_opts {
1017 * @sbuf: a buffer of LEB size used by GC and replay for scanning 1071 * @sbuf: a buffer of LEB size used by GC and replay for scanning
1018 * @idx_gc: list of index LEBs that have been garbage collected 1072 * @idx_gc: list of index LEBs that have been garbage collected
1019 * @idx_gc_cnt: number of elements on the idx_gc list 1073 * @idx_gc_cnt: number of elements on the idx_gc list
1074 * @gc_seq: incremented for every non-index LEB garbage collected
1075 * @gced_lnum: last non-index LEB that was garbage collected
1020 * 1076 *
1021 * @infos_list: links all 'ubifs_info' objects 1077 * @infos_list: links all 'ubifs_info' objects
1022 * @umount_mutex: serializes shrinker and un-mount 1078 * @umount_mutex: serializes shrinker and un-mount
@@ -1045,6 +1101,7 @@ struct ubifs_mount_opts {
1045 * @lpt_drty_flgs: dirty flags for LPT special nodes e.g. ltab 1101 * @lpt_drty_flgs: dirty flags for LPT special nodes e.g. ltab
1046 * @dirty_nn_cnt: number of dirty nnodes 1102 * @dirty_nn_cnt: number of dirty nnodes
1047 * @dirty_pn_cnt: number of dirty pnodes 1103 * @dirty_pn_cnt: number of dirty pnodes
1104 * @check_lpt_free: flag that indicates LPT GC may be needed
1048 * @lpt_sz: LPT size 1105 * @lpt_sz: LPT size
1049 * @lpt_nod_buf: buffer for an on-flash nnode or pnode 1106 * @lpt_nod_buf: buffer for an on-flash nnode or pnode
1050 * @lpt_buf: buffer of LEB size used by LPT 1107 * @lpt_buf: buffer of LEB size used by LPT
@@ -1086,6 +1143,7 @@ struct ubifs_mount_opts {
1086 * @rcvrd_mst_node: recovered master node to write when mounting ro to rw 1143 * @rcvrd_mst_node: recovered master node to write when mounting ro to rw
1087 * @size_tree: inode size information for recovery 1144 * @size_tree: inode size information for recovery
1088 * @remounting_rw: set while remounting from ro to rw (sb flags have MS_RDONLY) 1145 * @remounting_rw: set while remounting from ro to rw (sb flags have MS_RDONLY)
1146 * @always_chk_crc: always check CRCs (while mounting and remounting rw)
1089 * @mount_opts: UBIFS-specific mount options 1147 * @mount_opts: UBIFS-specific mount options
1090 * 1148 *
1091 * @dbg_buf: a buffer of LEB size used for debugging purposes 1149 * @dbg_buf: a buffer of LEB size used for debugging purposes
@@ -1103,7 +1161,6 @@ struct ubifs_info {
1103 struct backing_dev_info bdi; 1161 struct backing_dev_info bdi;
1104 1162
1105 ino_t highest_inum; 1163 ino_t highest_inum;
1106 unsigned int vfs_gen;
1107 unsigned long long max_sqnum; 1164 unsigned long long max_sqnum;
1108 unsigned long long cmt_no; 1165 unsigned long long cmt_no;
1109 spinlock_t cnt_lock; 1166 spinlock_t cnt_lock;
@@ -1131,11 +1188,11 @@ struct ubifs_info {
1131 int cmt_state; 1188 int cmt_state;
1132 spinlock_t cs_lock; 1189 spinlock_t cs_lock;
1133 wait_queue_head_t cmt_wq; 1190 wait_queue_head_t cmt_wq;
1191
1134 unsigned int fast_unmount:1; 1192 unsigned int fast_unmount:1;
1135 unsigned int big_lpt:1; 1193 unsigned int big_lpt:1;
1136 unsigned int check_lpt_free:1; 1194 unsigned int no_chk_data_crc:1;
1137 unsigned int nospace:1; 1195 unsigned int bulk_read:1;
1138 unsigned int nospace_rp:1;
1139 1196
1140 struct mutex tnc_mutex; 1197 struct mutex tnc_mutex;
1141 struct ubifs_zbranch zroot; 1198 struct ubifs_zbranch zroot;
@@ -1160,6 +1217,7 @@ struct ubifs_info {
1160 struct ubifs_mst_node *mst_node; 1217 struct ubifs_mst_node *mst_node;
1161 int mst_offs; 1218 int mst_offs;
1162 struct mutex mst_mutex; 1219 struct mutex mst_mutex;
1220 int bulk_read_buf_size;
1163 1221
1164 int log_lebs; 1222 int log_lebs;
1165 long long log_bytes; 1223 long long log_bytes;
@@ -1203,6 +1261,8 @@ struct ubifs_info {
1203 unsigned long long old_idx_sz; 1261 unsigned long long old_idx_sz;
1204 unsigned long long calc_idx_sz; 1262 unsigned long long calc_idx_sz;
1205 struct ubifs_lp_stats lst; 1263 struct ubifs_lp_stats lst;
1264 unsigned int nospace:1;
1265 unsigned int nospace_rp:1;
1206 1266
1207 int page_budget; 1267 int page_budget;
1208 int inode_budget; 1268 int inode_budget;
@@ -1214,6 +1274,8 @@ struct ubifs_info {
1214 int max_idx_node_sz; 1274 int max_idx_node_sz;
1215 long long max_inode_sz; 1275 long long max_inode_sz;
1216 int max_znode_sz; 1276 int max_znode_sz;
1277
1278 int leb_overhead;
1217 int dead_wm; 1279 int dead_wm;
1218 int dark_wm; 1280 int dark_wm;
1219 int block_cnt; 1281 int block_cnt;
@@ -1247,6 +1309,8 @@ struct ubifs_info {
1247 void *sbuf; 1309 void *sbuf;
1248 struct list_head idx_gc; 1310 struct list_head idx_gc;
1249 int idx_gc_cnt; 1311 int idx_gc_cnt;
1312 volatile int gc_seq;
1313 volatile int gced_lnum;
1250 1314
1251 struct list_head infos_list; 1315 struct list_head infos_list;
1252 struct mutex umount_mutex; 1316 struct mutex umount_mutex;
@@ -1275,6 +1339,7 @@ struct ubifs_info {
1275 int lpt_drty_flgs; 1339 int lpt_drty_flgs;
1276 int dirty_nn_cnt; 1340 int dirty_nn_cnt;
1277 int dirty_pn_cnt; 1341 int dirty_pn_cnt;
1342 int check_lpt_free;
1278 long long lpt_sz; 1343 long long lpt_sz;
1279 void *lpt_nod_buf; 1344 void *lpt_nod_buf;
1280 void *lpt_buf; 1345 void *lpt_buf;
@@ -1316,6 +1381,7 @@ struct ubifs_info {
1316 struct ubifs_mst_node *rcvrd_mst_node; 1381 struct ubifs_mst_node *rcvrd_mst_node;
1317 struct rb_root size_tree; 1382 struct rb_root size_tree;
1318 int remounting_rw; 1383 int remounting_rw;
1384 int always_chk_crc;
1319 struct ubifs_mount_opts mount_opts; 1385 struct ubifs_mount_opts mount_opts;
1320 1386
1321#ifdef CONFIG_UBIFS_FS_DEBUG 1387#ifdef CONFIG_UBIFS_FS_DEBUG
@@ -1328,6 +1394,12 @@ struct ubifs_info {
1328 unsigned long fail_timeout; 1394 unsigned long fail_timeout;
1329 unsigned int fail_cnt; 1395 unsigned int fail_cnt;
1330 unsigned int fail_cnt_max; 1396 unsigned int fail_cnt_max;
1397 long long chk_lpt_sz;
1398 long long chk_lpt_sz2;
1399 long long chk_lpt_wastage;
1400 int chk_lpt_lebs;
1401 int new_nhead_lnum;
1402 int new_nhead_offs;
1331#endif 1403#endif
1332}; 1404};
1333 1405
@@ -1346,6 +1418,7 @@ extern struct backing_dev_info ubifs_backing_dev_info;
1346extern struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT]; 1418extern struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
1347 1419
1348/* io.c */ 1420/* io.c */
1421void ubifs_ro_mode(struct ubifs_info *c, int err);
1349int ubifs_wbuf_write_nolock(struct ubifs_wbuf *wbuf, void *buf, int len); 1422int ubifs_wbuf_write_nolock(struct ubifs_wbuf *wbuf, void *buf, int len);
1350int ubifs_wbuf_seek_nolock(struct ubifs_wbuf *wbuf, int lnum, int offs, 1423int ubifs_wbuf_seek_nolock(struct ubifs_wbuf *wbuf, int lnum, int offs,
1351 int dtype); 1424 int dtype);
@@ -1357,7 +1430,7 @@ int ubifs_read_node_wbuf(struct ubifs_wbuf *wbuf, void *buf, int type, int len,
1357int ubifs_write_node(struct ubifs_info *c, void *node, int len, int lnum, 1430int ubifs_write_node(struct ubifs_info *c, void *node, int len, int lnum,
1358 int offs, int dtype); 1431 int offs, int dtype);
1359int ubifs_check_node(const struct ubifs_info *c, const void *buf, int lnum, 1432int ubifs_check_node(const struct ubifs_info *c, const void *buf, int lnum,
1360 int offs, int quiet); 1433 int offs, int quiet, int chk_crc);
1361void ubifs_prepare_node(struct ubifs_info *c, void *buf, int len, int pad); 1434void ubifs_prepare_node(struct ubifs_info *c, void *buf, int len, int pad);
1362void ubifs_prep_grp_node(struct ubifs_info *c, void *node, int len, int last); 1435void ubifs_prep_grp_node(struct ubifs_info *c, void *node, int len, int last);
1363int ubifs_io_init(struct ubifs_info *c); 1436int ubifs_io_init(struct ubifs_info *c);
@@ -1399,8 +1472,8 @@ int ubifs_jnl_update(struct ubifs_info *c, const struct inode *dir,
1399 int deletion, int xent); 1472 int deletion, int xent);
1400int ubifs_jnl_write_data(struct ubifs_info *c, const struct inode *inode, 1473int ubifs_jnl_write_data(struct ubifs_info *c, const struct inode *inode,
1401 const union ubifs_key *key, const void *buf, int len); 1474 const union ubifs_key *key, const void *buf, int len);
1402int ubifs_jnl_write_inode(struct ubifs_info *c, const struct inode *inode, 1475int ubifs_jnl_write_inode(struct ubifs_info *c, const struct inode *inode);
1403 int last_reference); 1476int ubifs_jnl_delete_inode(struct ubifs_info *c, const struct inode *inode);
1404int ubifs_jnl_rename(struct ubifs_info *c, const struct inode *old_dir, 1477int ubifs_jnl_rename(struct ubifs_info *c, const struct inode *old_dir,
1405 const struct dentry *old_dentry, 1478 const struct dentry *old_dentry,
1406 const struct inode *new_dir, 1479 const struct inode *new_dir,
@@ -1423,9 +1496,10 @@ void ubifs_release_ino_dirty(struct ubifs_info *c, struct inode *inode,
1423 struct ubifs_budget_req *req); 1496 struct ubifs_budget_req *req);
1424void ubifs_cancel_ino_op(struct ubifs_info *c, struct inode *inode, 1497void ubifs_cancel_ino_op(struct ubifs_info *c, struct inode *inode,
1425 struct ubifs_budget_req *req); 1498 struct ubifs_budget_req *req);
1426long long ubifs_budg_get_free_space(struct ubifs_info *c); 1499long long ubifs_get_free_space(struct ubifs_info *c);
1427int ubifs_calc_min_idx_lebs(struct ubifs_info *c); 1500int ubifs_calc_min_idx_lebs(struct ubifs_info *c);
1428void ubifs_convert_page_budget(struct ubifs_info *c); 1501void ubifs_convert_page_budget(struct ubifs_info *c);
1502long long ubifs_reported_space(const struct ubifs_info *c, uint64_t free);
1429long long ubifs_calc_available(const struct ubifs_info *c, int min_idx_lebs); 1503long long ubifs_calc_available(const struct ubifs_info *c, int min_idx_lebs);
1430 1504
1431/* find.c */ 1505/* find.c */
@@ -1440,8 +1514,6 @@ int ubifs_save_dirty_idx_lnums(struct ubifs_info *c);
1440/* tnc.c */ 1514/* tnc.c */
1441int ubifs_lookup_level0(struct ubifs_info *c, const union ubifs_key *key, 1515int ubifs_lookup_level0(struct ubifs_info *c, const union ubifs_key *key,
1442 struct ubifs_znode **zn, int *n); 1516 struct ubifs_znode **zn, int *n);
1443int ubifs_tnc_lookup(struct ubifs_info *c, const union ubifs_key *key,
1444 void *node);
1445int ubifs_tnc_lookup_nm(struct ubifs_info *c, const union ubifs_key *key, 1517int ubifs_tnc_lookup_nm(struct ubifs_info *c, const union ubifs_key *key,
1446 void *node, const struct qstr *nm); 1518 void *node, const struct qstr *nm);
1447int ubifs_tnc_locate(struct ubifs_info *c, const union ubifs_key *key, 1519int ubifs_tnc_locate(struct ubifs_info *c, const union ubifs_key *key,
@@ -1471,6 +1543,8 @@ void destroy_old_idx(struct ubifs_info *c);
1471int is_idx_node_in_tnc(struct ubifs_info *c, union ubifs_key *key, int level, 1543int is_idx_node_in_tnc(struct ubifs_info *c, union ubifs_key *key, int level,
1472 int lnum, int offs); 1544 int lnum, int offs);
1473int insert_old_idx_znode(struct ubifs_info *c, struct ubifs_znode *znode); 1545int insert_old_idx_znode(struct ubifs_info *c, struct ubifs_znode *znode);
1546int ubifs_tnc_get_bu_keys(struct ubifs_info *c, struct bu_info *bu);
1547int ubifs_tnc_bulk_read(struct ubifs_info *c, struct bu_info *bu);
1474 1548
1475/* tnc_misc.c */ 1549/* tnc_misc.c */
1476struct ubifs_znode *ubifs_tnc_levelorder_next(struct ubifs_znode *zr, 1550struct ubifs_znode *ubifs_tnc_levelorder_next(struct ubifs_znode *zr,
@@ -1567,12 +1641,10 @@ int ubifs_lpt_post_commit(struct ubifs_info *c);
1567void ubifs_lpt_free(struct ubifs_info *c, int wr_only); 1641void ubifs_lpt_free(struct ubifs_info *c, int wr_only);
1568 1642
1569/* lprops.c */ 1643/* lprops.c */
1570void ubifs_get_lprops(struct ubifs_info *c);
1571const struct ubifs_lprops *ubifs_change_lp(struct ubifs_info *c, 1644const struct ubifs_lprops *ubifs_change_lp(struct ubifs_info *c,
1572 const struct ubifs_lprops *lp, 1645 const struct ubifs_lprops *lp,
1573 int free, int dirty, int flags, 1646 int free, int dirty, int flags,
1574 int idx_gc_cnt); 1647 int idx_gc_cnt);
1575void ubifs_release_lprops(struct ubifs_info *c);
1576void ubifs_get_lp_stats(struct ubifs_info *c, struct ubifs_lp_stats *stats); 1648void ubifs_get_lp_stats(struct ubifs_info *c, struct ubifs_lp_stats *stats);
1577void ubifs_add_to_cat(struct ubifs_info *c, struct ubifs_lprops *lprops, 1649void ubifs_add_to_cat(struct ubifs_info *c, struct ubifs_lprops *lprops,
1578 int cat); 1650 int cat);
diff --git a/fs/ubifs/xattr.c b/fs/ubifs/xattr.c
index 1388a078e1a9..cfd31e229c89 100644
--- a/fs/ubifs/xattr.c
+++ b/fs/ubifs/xattr.c
@@ -61,7 +61,7 @@
61 61
62/* 62/*
63 * Limit the number of extended attributes per inode so that the total size 63 * Limit the number of extended attributes per inode so that the total size
64 * (xattr_size) is guaranteeded to fit in an 'unsigned int'. 64 * (@xattr_size) is guaranteeded to fit in an 'unsigned int'.
65 */ 65 */
66#define MAX_XATTRS_PER_INODE 65535 66#define MAX_XATTRS_PER_INODE 65535
67 67
@@ -103,14 +103,14 @@ static int create_xattr(struct ubifs_info *c, struct inode *host,
103 struct inode *inode; 103 struct inode *inode;
104 struct ubifs_inode *ui, *host_ui = ubifs_inode(host); 104 struct ubifs_inode *ui, *host_ui = ubifs_inode(host);
105 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1, 105 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
106 .new_ino_d = size, .dirtied_ino = 1, 106 .new_ino_d = ALIGN(size, 8), .dirtied_ino = 1,
107 .dirtied_ino_d = host_ui->data_len}; 107 .dirtied_ino_d = ALIGN(host_ui->data_len, 8) };
108 108
109 if (host_ui->xattr_cnt >= MAX_XATTRS_PER_INODE) 109 if (host_ui->xattr_cnt >= MAX_XATTRS_PER_INODE)
110 return -ENOSPC; 110 return -ENOSPC;
111 /* 111 /*
112 * Linux limits the maximum size of the extended attribute names list 112 * Linux limits the maximum size of the extended attribute names list
113 * to %XATTR_LIST_MAX. This means we should not allow creating more* 113 * to %XATTR_LIST_MAX. This means we should not allow creating more
114 * extended attributes if the name list becomes larger. This limitation 114 * extended attributes if the name list becomes larger. This limitation
115 * is artificial for UBIFS, though. 115 * is artificial for UBIFS, though.
116 */ 116 */
@@ -128,7 +128,6 @@ static int create_xattr(struct ubifs_info *c, struct inode *host,
128 goto out_budg; 128 goto out_budg;
129 } 129 }
130 130
131 mutex_lock(&host_ui->ui_mutex);
132 /* Re-define all operations to be "nothing" */ 131 /* Re-define all operations to be "nothing" */
133 inode->i_mapping->a_ops = &none_address_operations; 132 inode->i_mapping->a_ops = &none_address_operations;
134 inode->i_op = &none_inode_operations; 133 inode->i_op = &none_inode_operations;
@@ -141,23 +140,19 @@ static int create_xattr(struct ubifs_info *c, struct inode *host,
141 ui->data = kmalloc(size, GFP_NOFS); 140 ui->data = kmalloc(size, GFP_NOFS);
142 if (!ui->data) { 141 if (!ui->data) {
143 err = -ENOMEM; 142 err = -ENOMEM;
144 goto out_unlock; 143 goto out_free;
145 } 144 }
146
147 memcpy(ui->data, value, size); 145 memcpy(ui->data, value, size);
146 inode->i_size = ui->ui_size = size;
147 ui->data_len = size;
148
149 mutex_lock(&host_ui->ui_mutex);
148 host->i_ctime = ubifs_current_time(host); 150 host->i_ctime = ubifs_current_time(host);
149 host_ui->xattr_cnt += 1; 151 host_ui->xattr_cnt += 1;
150 host_ui->xattr_size += CALC_DENT_SIZE(nm->len); 152 host_ui->xattr_size += CALC_DENT_SIZE(nm->len);
151 host_ui->xattr_size += CALC_XATTR_BYTES(size); 153 host_ui->xattr_size += CALC_XATTR_BYTES(size);
152 host_ui->xattr_names += nm->len; 154 host_ui->xattr_names += nm->len;
153 155
154 /*
155 * We do not use i_size_write() because nobody can race with us as we
156 * are holding host @host->i_mutex - every xattr operation for this
157 * inode is serialized by it.
158 */
159 inode->i_size = ui->ui_size = size;
160 ui->data_len = size;
161 err = ubifs_jnl_update(c, host, nm, inode, 0, 1); 156 err = ubifs_jnl_update(c, host, nm, inode, 0, 1);
162 if (err) 157 if (err)
163 goto out_cancel; 158 goto out_cancel;
@@ -172,8 +167,8 @@ out_cancel:
172 host_ui->xattr_cnt -= 1; 167 host_ui->xattr_cnt -= 1;
173 host_ui->xattr_size -= CALC_DENT_SIZE(nm->len); 168 host_ui->xattr_size -= CALC_DENT_SIZE(nm->len);
174 host_ui->xattr_size -= CALC_XATTR_BYTES(size); 169 host_ui->xattr_size -= CALC_XATTR_BYTES(size);
175out_unlock:
176 mutex_unlock(&host_ui->ui_mutex); 170 mutex_unlock(&host_ui->ui_mutex);
171out_free:
177 make_bad_inode(inode); 172 make_bad_inode(inode);
178 iput(inode); 173 iput(inode);
179out_budg: 174out_budg:
@@ -200,29 +195,28 @@ static int change_xattr(struct ubifs_info *c, struct inode *host,
200 struct ubifs_inode *host_ui = ubifs_inode(host); 195 struct ubifs_inode *host_ui = ubifs_inode(host);
201 struct ubifs_inode *ui = ubifs_inode(inode); 196 struct ubifs_inode *ui = ubifs_inode(inode);
202 struct ubifs_budget_req req = { .dirtied_ino = 2, 197 struct ubifs_budget_req req = { .dirtied_ino = 2,
203 .dirtied_ino_d = size + host_ui->data_len }; 198 .dirtied_ino_d = ALIGN(size, 8) + ALIGN(host_ui->data_len, 8) };
204 199
205 ubifs_assert(ui->data_len == inode->i_size); 200 ubifs_assert(ui->data_len == inode->i_size);
206 err = ubifs_budget_space(c, &req); 201 err = ubifs_budget_space(c, &req);
207 if (err) 202 if (err)
208 return err; 203 return err;
209 204
210 mutex_lock(&host_ui->ui_mutex);
211 host->i_ctime = ubifs_current_time(host);
212 host_ui->xattr_size -= CALC_XATTR_BYTES(ui->data_len);
213 host_ui->xattr_size += CALC_XATTR_BYTES(size);
214
215 kfree(ui->data); 205 kfree(ui->data);
216 ui->data = kmalloc(size, GFP_NOFS); 206 ui->data = kmalloc(size, GFP_NOFS);
217 if (!ui->data) { 207 if (!ui->data) {
218 err = -ENOMEM; 208 err = -ENOMEM;
219 goto out_unlock; 209 goto out_free;
220 } 210 }
221
222 memcpy(ui->data, value, size); 211 memcpy(ui->data, value, size);
223 inode->i_size = ui->ui_size = size; 212 inode->i_size = ui->ui_size = size;
224 ui->data_len = size; 213 ui->data_len = size;
225 214
215 mutex_lock(&host_ui->ui_mutex);
216 host->i_ctime = ubifs_current_time(host);
217 host_ui->xattr_size -= CALC_XATTR_BYTES(ui->data_len);
218 host_ui->xattr_size += CALC_XATTR_BYTES(size);
219
226 /* 220 /*
227 * It is important to write the host inode after the xattr inode 221 * It is important to write the host inode after the xattr inode
228 * because if the host inode gets synchronized (via 'fsync()'), then 222 * because if the host inode gets synchronized (via 'fsync()'), then
@@ -240,9 +234,9 @@ static int change_xattr(struct ubifs_info *c, struct inode *host,
240out_cancel: 234out_cancel:
241 host_ui->xattr_size -= CALC_XATTR_BYTES(size); 235 host_ui->xattr_size -= CALC_XATTR_BYTES(size);
242 host_ui->xattr_size += CALC_XATTR_BYTES(ui->data_len); 236 host_ui->xattr_size += CALC_XATTR_BYTES(ui->data_len);
243 make_bad_inode(inode);
244out_unlock:
245 mutex_unlock(&host_ui->ui_mutex); 237 mutex_unlock(&host_ui->ui_mutex);
238 make_bad_inode(inode);
239out_free:
246 ubifs_release_budget(c, &req); 240 ubifs_release_budget(c, &req);
247 return err; 241 return err;
248} 242}
@@ -312,6 +306,7 @@ int ubifs_setxattr(struct dentry *dentry, const char *name,
312 306
313 dbg_gen("xattr '%s', host ino %lu ('%.*s'), size %zd", name, 307 dbg_gen("xattr '%s', host ino %lu ('%.*s'), size %zd", name,
314 host->i_ino, dentry->d_name.len, dentry->d_name.name, size); 308 host->i_ino, dentry->d_name.len, dentry->d_name.name, size);
309 ubifs_assert(mutex_is_locked(&host->i_mutex));
315 310
316 if (size > UBIFS_MAX_INO_DATA) 311 if (size > UBIFS_MAX_INO_DATA)
317 return -ERANGE; 312 return -ERANGE;
@@ -384,7 +379,6 @@ ssize_t ubifs_getxattr(struct dentry *dentry, const char *name, void *buf,
384 if (!xent) 379 if (!xent)
385 return -ENOMEM; 380 return -ENOMEM;
386 381
387 mutex_lock(&host->i_mutex);
388 xent_key_init(c, &key, host->i_ino, &nm); 382 xent_key_init(c, &key, host->i_ino, &nm);
389 err = ubifs_tnc_lookup_nm(c, &key, xent, &nm); 383 err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
390 if (err) { 384 if (err) {
@@ -419,7 +413,6 @@ ssize_t ubifs_getxattr(struct dentry *dentry, const char *name, void *buf,
419out_iput: 413out_iput:
420 iput(inode); 414 iput(inode);
421out_unlock: 415out_unlock:
422 mutex_unlock(&host->i_mutex);
423 kfree(xent); 416 kfree(xent);
424 return err; 417 return err;
425} 418}
@@ -449,13 +442,11 @@ ssize_t ubifs_listxattr(struct dentry *dentry, char *buffer, size_t size)
449 return -ERANGE; 442 return -ERANGE;
450 443
451 lowest_xent_key(c, &key, host->i_ino); 444 lowest_xent_key(c, &key, host->i_ino);
452
453 mutex_lock(&host->i_mutex);
454 while (1) { 445 while (1) {
455 int type; 446 int type;
456 447
457 xent = ubifs_tnc_next_ent(c, &key, &nm); 448 xent = ubifs_tnc_next_ent(c, &key, &nm);
458 if (unlikely(IS_ERR(xent))) { 449 if (IS_ERR(xent)) {
459 err = PTR_ERR(xent); 450 err = PTR_ERR(xent);
460 break; 451 break;
461 } 452 }
@@ -479,7 +470,6 @@ ssize_t ubifs_listxattr(struct dentry *dentry, char *buffer, size_t size)
479 pxent = xent; 470 pxent = xent;
480 key_read(c, &xent->key, &key); 471 key_read(c, &xent->key, &key);
481 } 472 }
482 mutex_unlock(&host->i_mutex);
483 473
484 kfree(pxent); 474 kfree(pxent);
485 if (err != -ENOENT) { 475 if (err != -ENOENT) {
@@ -497,8 +487,8 @@ static int remove_xattr(struct ubifs_info *c, struct inode *host,
497 int err; 487 int err;
498 struct ubifs_inode *host_ui = ubifs_inode(host); 488 struct ubifs_inode *host_ui = ubifs_inode(host);
499 struct ubifs_inode *ui = ubifs_inode(inode); 489 struct ubifs_inode *ui = ubifs_inode(inode);
500 struct ubifs_budget_req req = { .dirtied_ino = 1, .mod_dent = 1, 490 struct ubifs_budget_req req = { .dirtied_ino = 2, .mod_dent = 1,
501 .dirtied_ino_d = host_ui->data_len }; 491 .dirtied_ino_d = ALIGN(host_ui->data_len, 8) };
502 492
503 ubifs_assert(ui->data_len == inode->i_size); 493 ubifs_assert(ui->data_len == inode->i_size);
504 494