51 #include <linux/slab.h>
62 static int is_empty(
void *
buf,
int len)
67 for (i = 0; i < len; i++)
81 static int first_non_ff(
void *buf,
int len)
86 for (i = 0; i < len; i++)
109 static int get_master_node(
const struct ubifs_info *
c,
int lnum,
void **pbuf,
128 while (offs + UBIFS_MST_NODE_SZ <= c->leb_size) {
164 dbg_rcvry(
"found a master node at %d:%d", lnum, offs);
172 if (offs < c->leb_size) {
173 if (!is_empty(buf,
min_t(
int, len, sz))) {
175 dbg_rcvry(
"found corruption at %d:%d", lnum, offs);
182 if (offs < c->leb_size)
183 if (!is_empty(buf, len))
204 static int write_rcvrd_mst_node(
struct ubifs_info *c,
212 save_flags = mst->
flags;
223 mst->
flags = save_flags;
241 int err, offs1, offs2;
249 err = get_master_node(c,
UBIFS_MST_LNUM + 1, &buf2, &mst2, &cor2);
254 offs1 = (
void *)mst1 - buf1;
256 (offs1 == 0 && !cor1)) {
264 offs2 = (
void *)mst2 - buf2;
265 if (offs1 == offs2) {
272 }
else if (offs2 + sz == offs1) {
277 }
else if (offs1 == 0 &&
291 if (offs1 != 0 || cor1)
302 offs2 = (
void *)mst2 - buf2;
303 if (offs2 + sz + sz <= c->leb_size)
308 ubifs_msg(
"recovered master node from LEB %d",
350 err = write_rcvrd_mst_node(c, c->
mst_node);
363 ubifs_err(
"failed to recover master node");
413 static int is_last_write(
const struct ubifs_info *c,
void *buf,
int offs)
415 int empty_offs, check_len;
423 check_len = c->
leb_size - empty_offs;
424 p = buf + empty_offs -
offs;
425 return is_empty(p, check_len);
440 static void clean_buf(
const struct ubifs_info *c,
void **buf,
int lnum,
446 dbg_rcvry(
"cleaning corruption at %d:%d", lnum, *offs);
450 pad_len = empty_offs - *
offs;
470 static int no_more_nodes(
const struct ubifs_info *c,
void *buf,
int len,
478 if (is_empty(buf + skip, len - skip))
485 dbg_rcvry(
"unexpected bad common header at %d:%d", lnum, offs);
491 if (is_empty(buf + skip, len - skip))
493 dbg_rcvry(
"unexpected data at %d:%d", lnum, offs + skip);
509 if (!list_empty(&sleb->
nodes)) {
514 endpt = snod->
offs + snod->
len;
521 dbg_rcvry(
"need to fix LEB %d start %d endpt %d",
522 lnum, start, sleb->
endpt);
533 dbg_rcvry(
"fixing LEB %d start %d endpt %d",
534 lnum, start, sleb->
endpt);
574 static void drop_last_group(
struct ubifs_scan_leb *sleb,
int *offs)
576 while (!list_empty(&sleb->
nodes)) {
586 dbg_rcvry(
"dropping grouped node at %d:%d",
604 static void drop_last_node(
struct ubifs_scan_leb *sleb,
int *offs)
608 if (!list_empty(&sleb->
nodes)) {
636 int offs,
void *sbuf,
int jhead)
639 int grouped = jhead == -1 ? 0 : c->
jheads[jhead].grouped;
641 void *buf = sbuf +
offs;
643 dbg_rcvry(
"%d:%d, jhead %d, grouped %d", lnum, offs, jhead, grouped);
651 dbg_scan(
"look at LEB %d:%d (%d bytes left)",
673 }
else if (ret > 0) {
682 dbg_rcvry(
"found corruption (%d) at %d:%d",
686 ubifs_err(
"unexpected return value %d", ret);
693 if (!is_last_write(c, buf, offs))
694 goto corrupted_rescan;
696 if (!no_more_nodes(c, buf, len, lnum, offs))
697 goto corrupted_rescan;
698 }
else if (!is_empty(buf, len)) {
699 if (!is_last_write(c, buf, offs)) {
700 int corruption = first_non_ff(buf, len);
706 ubifs_err(
"corrupt empty space LEB %d:%d, corruption starts at %d",
707 lnum, offs, corruption);
721 drop_last_group(sleb, &offs);
774 while (offs > min_io_unit)
775 drop_last_node(sleb, &offs);
781 clean_buf(c, &buf, lnum, &offs, &len);
784 err = fix_unclean_leb(c, sleb, start);
798 ubifs_err(
"LEB %d scanning failed", lnum);
812 static int get_cs_sqnum(
struct ubifs_info *c,
int lnum,
int offs,
813 unsigned long long *cs_sqnum)
834 ubifs_err(
"Node a CS node, type is %d", cs_node->
ch.node_type);
838 ubifs_err(
"CS node cmt_no %llu != current cmt_no %llu",
844 dbg_rcvry(
"commit start sqnum %llu", *cs_sqnum);
870 int offs,
void *sbuf)
876 next_lnum = lnum + 1;
889 unsigned long long cs_sqnum = c->
cs_sqnum;
896 err = get_cs_sqnum(c, lnum, offs, &cs_sqnum);
902 if (snod->
sqnum > cs_sqnum) {
903 ubifs_err(
"unrecoverable log corruption in LEB %d",
925 static int recover_head(
struct ubifs_info *c,
int lnum,
int offs,
void *sbuf)
937 if (err || !is_empty(sbuf, len)) {
938 dbg_rcvry(
"cleaning head at %d:%d", lnum, offs);
998 static int clean_an_unclean_leb(
struct ubifs_info *c,
1047 ubifs_err(
"unexpected empty space at %d:%d",
1064 if (len > ucleb->
endpt) {
1103 err = clean_an_unclean_leb(c, ucleb, sbuf);
1120 static int grab_empty_leb(
struct ubifs_info *c)
1141 ubifs_err(
"could not find an empty LEB");
1154 dbg_rcvry(
"found empty LEB %d, run commit", lnum);
1187 return grab_empty_leb(c);
1194 dbg_rcvry(
"could not find a dirty LEB");
1195 return grab_empty_leb(c);
1266 loff_t d_size,
int exists)
1289 rb_link_node(&e->
rb, parent, p);
1309 else if (inum > e->
inum)
1390 int deletion, loff_t new_size)
1392 ino_t inum = key_inum(c, key);
1396 switch (key_type(c, key)) {
1399 remove_ino(c, inum);
1401 e = find_ino(c, inum);
1406 err = add_ino(c, inum, new_size, 0, 1);
1413 e = find_ino(c, inum);
1415 if (new_size > e->
d_size)
1418 err = add_ino(c, inum, 0, new_size, 0);
1424 e = find_ino(c, inum);
1447 ino_key_init(c, &
key, e->
inum);
1471 while (p[len] == 0xff)
1478 dbg_rcvry(
"inode %lu at %d:%d size %lld -> %lld",
1479 (
unsigned long)e->
inum, lnum, offs, i_size, e->
d_size);
1483 ubifs_warn(
"inode %lu failed to fix size %lld -> %lld error %d",
1509 ino_key_init(c, &key, e->
inum);
1510 err = ubifs_tnc_lookup(c, &key, c->
sbuf);
1511 if (err && err != -
ENOENT)
1516 (
unsigned long)e->
inum);
1538 return PTR_ERR(inode);
1543 (
unsigned long)e->
inum,
1555 err = fix_size_in_place(c, e);