Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Filelock #11724

Merged
merged 8 commits into from
Feb 21, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions fs/vfs/fs_lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,6 @@ static int file_lock_normalize(FAR struct file *filep,
off_t start;
off_t end;

/* Check the legality of incoming flocks */

if (flock->l_len - 1 > OFFSET_MAX - flock->l_start)
{
return -EOVERFLOW;
}

/* Check that the type brought in the flock is correct */

switch (flock->l_type)
Expand Down Expand Up @@ -161,8 +154,18 @@ static int file_lock_normalize(FAR struct file *filep,
}

start += flock->l_start;
if (start < 0)
{
return -EINVAL;
}

if (flock->l_len > 0)
{
if (flock->l_len - 1 > OFFSET_MAX - start)
{
return -EOVERFLOW;
}

end = start + flock->l_len - 1;
}
else if (flock->l_len < 0)
Expand All @@ -181,7 +184,6 @@ static int file_lock_normalize(FAR struct file *filep,
}

out->l_whence = SEEK_SET;
out->l_pid = getpid();
acassis marked this conversation as resolved.
Show resolved Hide resolved
out->l_type = flock->l_type;
out->l_start = start;
out->l_end = end;
Expand Down Expand Up @@ -546,8 +548,6 @@ int file_getlk(FAR struct file *filep, FAR struct flock *flock)
return ret;
}

flock->l_type = F_UNLCK;

nxmutex_lock(&g_protect_lock);

bucket = file_lock_find_bucket(path);
Expand All @@ -559,19 +559,21 @@ int file_getlk(FAR struct file *filep, FAR struct flock *flock)
if (file_lock_is_conflict(flock, &file_lock->fl_lock))
{
memcpy(flock, &file_lock->fl_lock, sizeof(*flock));
break;
goto out;
}
}
}

nxmutex_unlock(&g_protect_lock);
flock->l_type = F_UNLCK;

/* Convert back to flock
* The flock information saved in filelock is used as an offset
* to the relative position. And for upper level applications,
* l_len should be converted to cover the data quantity
*/

out:
nxmutex_unlock(&g_protect_lock);
if (flock->l_end == OFFSET_MAX)
{
flock->l_len = 0;
Expand Down Expand Up @@ -625,6 +627,8 @@ int file_setlk(FAR struct file *filep, FAR struct flock *flock,
return ret;
}

request.l_pid = getpid();

nxmutex_lock(&g_protect_lock);

bucket = file_lock_find_bucket(path);
Expand Down