fstools: backport regression fix for volume_identify
[openwrt/openwrt.git] / package / system / fstools / patches / 0001-libfstools-fix-multiple-volume_identify-usages-with-.patch
1 From 633a8d0981fed0c90f6d16ee2257858b04514dc8 Mon Sep 17 00:00:00 2001
2 From: Pieter Smith <pieter.smith@philips.com>
3 Date: Wed, 29 Mar 2017 18:21:56 +0200
4 Subject: [PATCH] libfstools: fix multiple volume_identify usages with the same
5 volume
6 MIME-Version: 1.0
7 Content-Type: text/plain; charset=UTF-8
8 Content-Transfer-Encoding: 8bit
9
10 This fixes e.g. factory-flashed startup issue with jffs2 on ubi overlay
11
12 Commit ba019965 ("libfstools: accept volume as argument in most calls")
13 broke startup for factory-flashed jffs2 on ubi systems, causing substantial
14 slowdown in factory environments.
15
16 When starting up with a factory-flashed jffs2 on ubi system, the "rootfs_data"
17 volume contains a deadcode marker. In the start phase, mount_root then mounts a
18 tmpfs overlay, and postpones remounting of the jffs2 overlay until the done
19 phase of the startup.
20
21 The refactoring in ba019965 eliminated an unneeded call to volume_find() when
22 done() called jffs2_switch(). Unfortunately the refactoring did not take into
23 account that volume_identify() does not function correctly when called twice in
24 a row on the same struct volume when using an mtd driver.
25
26 mtd_volume_identify() uses mtd_volume_load() to open an fd to the mtd device
27 and reads a potential deadcode marker from the fd. The first time this works,
28 and FS_DEADCODE is returned.
29
30 When volume_identify() is called a second time however, mtd_volume_load()
31 notices that we already have an open fd, does nothing further and returns 0
32 without resetting the file offset to 0. mtd_volume_identify() now reads past
33 the deadcode marker and now returns FS_JFFS2 if the mtd device is a UBIVOLUME.
34
35 jffs2_switch() then handles the wrong case, either pulling the root out from
36 under user-space in Chaos Calmer, or indefinitely sticking to a tmpfs overlay
37 in later OpenWRT builds.
38
39 Signed-off-by: Pieter Smith <pieter.smith@philips.com>
40 Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
41 ---
42
43 --- a/libfstools/mtd.c
44 +++ b/libfstools/mtd.c
45 @@ -76,8 +76,10 @@ static int mtd_volume_load(struct mtd_vo
46 struct mtd_info_user mtdInfo;
47 struct erase_info_user mtdLockInfo;
48
49 - if (p->fd)
50 + if (p->fd) {
51 + lseek(p->fd, 0, SEEK_SET);
52 return 0;
53 + }
54
55 if (!p->chr)
56 return -1;