Commit 6c86fcb5 authored by Pavel Begunkov's avatar Pavel Begunkov Committed by Andrei Vagin

locks: Remove duplicated locks

CRIU creates dictinct lock record for each file descriptor on the same
OFD. The patch removes this duplicates. To do so, it adds new field into
struct file_lock, which stores pid of fd, on which lock was found.
'owner pid' is not actually helpful, because the original fd, on which
lock have been set, can be already closed.

Also it purges crutches doing the same stuff but only for file leases.
Signed-off-by: 's avatarPavel Begunkov <asml.silence@gmail.com>
Signed-off-by: 's avatarAndrei Vagin <avagin@virtuozzo.com>
parent 0781cb33
......@@ -58,6 +58,7 @@ struct file_lock *alloc_file_lock(void)
INIT_LIST_HEAD(&flock->list);
flock->real_owner = -1;
flock->owners_fd = -1;
flock->fl_holder = -1;
return flock;
}
......@@ -106,8 +107,6 @@ int dump_file_locks(void)
continue;
}
if (fl->fl_kind == FL_LEASE && !fl->updated)
continue;
file_lock_entry__init(&fle);
fle.pid = fl->real_owner;
......@@ -410,6 +409,7 @@ int note_file_lock(struct pid *pid, int fd, int lfd, struct fd_parms *p)
continue;
}
fl->fl_holder = pid->real;
fl->real_owner = pid->ns[0].virt;
fl->owners_fd = fd;
......@@ -421,6 +421,19 @@ int note_file_lock(struct pid *pid, int fd, int lfd, struct fd_parms *p)
return 0;
}
void discard_dup_locks_tail(pid_t pid, int fd)
{
struct file_lock *fl, *p;
list_for_each_entry_safe_reverse(fl, p, &file_lock_list, list) {
if (fl->owners_fd != fd || pid != fl->fl_holder)
break;
list_del(&fl->list);
xfree(fl);
}
}
int correct_file_leases_type(struct pid *pid, int fd, int lfd)
{
struct file_lock *fl;
......@@ -428,10 +441,9 @@ int correct_file_leases_type(struct pid *pid, int fd, int lfd)
list_for_each_entry(fl, &file_lock_list, list) {
/* owners_fd should be set before usage */
if (fl->fl_owner != pid->real || fl->owners_fd != fd)
if (fl->fl_holder != pid->real || fl->owners_fd != fd)
continue;
fl->updated = true;
if (fl->fl_kind == FL_LEASE &&
(fl->fl_ltype & LEASE_BREAKING)) {
/*
......
......@@ -309,6 +309,9 @@ int do_dump_gen_file(struct fd_parms *p, int lfd,
ret = fd_id_generate(p->pid, e, p);
if (ret == 1) /* new ID generated */
ret = ops->dump(lfd, e->id, p);
else
/* Remove locks generated by the fd before going to the next */
discard_dup_locks_tail(p->pid, e->fd);
return ret;
}
......
......@@ -46,7 +46,8 @@ struct file_lock {
int fl_kind;
int fl_ltype;
pid_t fl_owner;
pid_t fl_owner; /* process, which created the lock */
pid_t fl_holder; /* pid of fd on whose the lock is found */
int maj, min;
unsigned long i_no;
long long start;
......@@ -56,8 +57,6 @@ struct file_lock {
int real_owner;
int owners_fd;
bool updated; /* used to remove duplicate leases */
};
extern struct list_head file_lock_list;
......@@ -70,6 +69,7 @@ extern struct collect_image_info file_locks_cinfo;
struct pid;
struct fd_parms;
extern void discard_dup_locks_tail(pid_t pid, int fd);
extern int correct_file_leases_type(struct pid *, int fd, int lfd);
extern int note_file_lock(struct pid *, int fd, int lfd, struct fd_parms *);
extern int dump_file_locks(void);
......
......@@ -1715,6 +1715,7 @@ static int parse_fdinfo_pid_s(int pid, int fd, int type, void *arg)
}
fl->real_owner = fdinfo->owner;
fl->fl_holder = pid;
fl->owners_fd = fd;
list_add_tail(&fl->list, &file_lock_list);
}
......@@ -2008,8 +2009,6 @@ static int parse_file_lock_buf(char *buf, struct file_lock *fl,
return -1;
}
fl->updated = false;
if (!strcmp(fl_flag, "POSIX"))
fl->fl_kind = FL_POSIX;
else if (!strcmp(fl_flag, "FLOCK"))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment