mount: validate mounts only once on dump (v3)

mntinfo contains mounts from all namespaces, so we can validate it only
once after collecting mounts.

v2: add a fake comment about goto
v3: add a real comment about goto
Signed-off-by: Andrey Vagin <avagin@openvz.org>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
cg: proc_parse -- Don't compare cgroup paths

When we compare sets in cg_set_compare() we presume that controller
names are properly sorted but because of use of strcmp(cc->path, path)
it's not true. In particular in case if there are two same sets which
differ in paths only

(00.126812) cg:  `- New css ID 2
(00.127051) cg:     `- [memory] -> [/vz-1]
(00.127079) cg:     `- [name=systemd] -> [/vz-1]
(00.127108) cg:     `- [net_cls] -> [/vz-1]

(00.239829) cg:  `- New css ID 3
(00.240067) cg:     `- [memory] -> [/vz-1]
(00.240096) cg:     `- [net_cls] -> [/vz-1]
(00.240154) cg:     `- [name=systemd] -> [/vz-1/system.slice/dbus.service]

we currently refuse to dump such configuretion. Thus remove
path comparision from the first place.

CC: Tycho Andersen <tycho.andersen@canonical.com>
Signed-off-by: Cyrill Gorcunov <gorcunov@openvz.org>
Acked-by: Tycho Andersen <tycho.andersen@canonical.com>
Acked-by: Andrew Vagin <avagin@parallels.com>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
rst: Don't allocate PATH_MAX for /proc/self realink

Pid is 10 chars maximum.

Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
mount: handle a circular reference in mount tree

$ cat /proc/self/mountinfo
...
1 1 0:2 / / rw - rootfs rootfs rw,size=373396k,nr_inodes=93349
...

You can see that mnt_id and parent_mnt_id are equals here.
This patch interpretes this case as a root mount in a tree.

0'th mount is rootfs, which is mounted in init_mount_tree().

We don't see it in cases when system makes chroot, because of

static int show_mountinfo(struct seq_file *m, struct vfsmount *mnt)
	...
	/* mountpoints outside of chroot jail will give SEQ_SKIP on this */
	err = seq_path_root(m, &mnt_path, &root, " \t\n\\");

Cc: beproject criu <beprojectcriu@gmail.com>
Cc: Christopher Covington <cov@codeaurora.org>
Reported-by: beproject criu <beprojectcriu@gmail.com>
Reviewed-by: Christopher Covington <cov@codeaurora.org>
Signed-off-by: Andrey Vagin <avagin@openvz.org>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
remap: add remap_type field and use it

Maintain backwards compatibility for old images, but don't set the REMAP_GHOST
bit going forward, only use the remap_type field.

v2: * preserve remap_id in GHOST_REMAP case
    * protobuf field is remap_type enum not u32

Signed-off-by: Tycho Andersen <tycho.andersen@canonical.com>
restore: TASK_HELPERs live until RESTORE stage ends

In order to use TASK_HELPERS to open files from dead processes, they should
persist until criu is done restoring the filesystem, which happens in the
RESTORE stage. To do this, we need to pass each helper's PIDs to the restorer
blob, so that it can wait() on them when the restore stage is done.

This commit is in preparation for the remap_dead_pid commits.

v2: wait() on helpers after restore stage is over
v3: add CR_STATE_RESTORE_FS stage
v4: CR_STATE_RESTORE_FS waits for nr_tasks + nr_helpers, not nr_threads
v5: ditch CR_STATE_RESTORE_FS in favor of passing helpers to restorer blob

Signed-off-by: Tycho Andersen <tycho.andersen@canonical.com>
remap: add a dead pid /proc remap

If a file like /proc/20/mountinfo is open, but 20 is a zombie (or doesn't exist
any more), we can't read this file at all, so a link remap won't work. Instead,
we add a new remap, called the dead process remap, which forks a TASK_HELPER as
that dead pid so that the restore task can open the new /proc/20/mountinfo
instead.

This commit also adds a new stage CR_STATE_RESTORE_SHARED. Since new
TASK_HELPERS are added when loading the shared resource images, we need to wait
to start forking tasks until after these resources are loaded.

v2: fix a mutex bug

Signed-off-by: Tycho Andersen <tycho.andersen@canonical.com>
test: add a test for remap_dead_pid

Signed-off-by: Tycho Andersen <tycho.andersen@canonical.com>