fs_lock:Implementing file locks

Signed-off-by: chenrun1 <chenrun1@xiaomi.com>
This commit is contained in:
chenrun1
2023-10-23 15:55:00 +08:00
committed by Alan Carvalho de Assis
parent 17ede2fde4
commit 5c6bd833ed
10 changed files with 1038 additions and 2 deletions
+4
View File
@@ -64,6 +64,10 @@ config FS_NEPOLL_DESCRIPTORS
---help---
The maximum number of default epoll descriptors for epoll_create1(2)
config FS_LOCK_BUCKET_SIZE
int "Maximum number of hash bucket using file locks"
default 0
config DISABLE_PSEUDOFS_OPERATIONS
bool "Disable pseudo-filesystem operations"
default DEFAULT_SMALL
+3
View File
@@ -29,6 +29,7 @@
#include "rpmsgfs/rpmsgfs.h"
#include "inode/inode.h"
#include "aio/aio.h"
#include "vfs/lock.h"
/****************************************************************************
* Private Functions
@@ -83,6 +84,8 @@ void fs_initialize(void)
inode_initialize();
file_initlk();
#ifdef CONFIG_FS_AIO
/* Initialize for asynchronous I/O */
+4
View File
@@ -29,6 +29,10 @@ CSRCS += fs_syncfs.c fs_truncate.c
# Certain interfaces are not available if there is no mountpoint support
ifneq ($(CONFIG_FS_LOCK_BUCKET_SIZE),0)
CSRCS += fs_lock.c
endif
ifneq ($(CONFIG_PSEUDOFS_SOFTLINKS),0)
CSRCS += fs_link.c fs_symlink.c fs_readlink.c
endif
+3
View File
@@ -32,6 +32,7 @@
#include <nuttx/fs/fs.h>
#include "inode/inode.h"
#include "vfs/lock.h"
/****************************************************************************
* Public Functions
@@ -65,6 +66,8 @@ int file_close(FAR struct file *filep)
if (inode)
{
file_closelk(filep);
/* Close the file, driver, or mountpoint. */
if (inode->u.i_ops && inode->u.i_ops->close)
+18 -2
View File
@@ -35,6 +35,7 @@
#include <nuttx/fs/fs.h>
#include "inode/inode.h"
#include "lock.h"
/****************************************************************************
* Private Functions
@@ -195,6 +196,12 @@ static int file_vfcntl(FAR struct file *filep, int cmd, va_list ap)
* for the lock type which shall be set to F_UNLCK.
*/
{
FAR struct flock *flock = va_arg(ap, FAR struct flock *);
ret = file_getlk(filep, flock);
}
break;
case F_SETLK:
/* Set or clear a file segment lock according to the lock
* description pointed to by the third argument, arg, taken as a
@@ -206,6 +213,12 @@ static int file_vfcntl(FAR struct file *filep, int cmd, va_list ap)
* shall return immediately with a return value of -1.
*/
{
FAR struct flock *flock = va_arg(ap, FAR struct flock *);
ret = file_setlk(filep, flock, true);
}
break;
case F_SETLKW:
/* This command shall be equivalent to F_SETLK except that if a
* shared or exclusive lock is blocked by other locks, the thread
@@ -216,9 +229,12 @@ static int file_vfcntl(FAR struct file *filep, int cmd, va_list ap)
* the lock operation shall not be done.
*/
ret = -ENOSYS; /* Not implemented */
break;
{
FAR struct flock *flock = va_arg(ap, FAR struct flock *);
ret = file_setlk(filep, flock, false);
}
break;
case F_GETPATH:
/* Get the path of the file descriptor. The argument must be a buffer
* of size PATH_MAX or greater.
+769
View File
File diff suppressed because it is too large Load Diff
+106
View File
@@ -0,0 +1,106 @@
/****************************************************************************
* fs/vfs/lock.h
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
#ifndef __FS_VFS_LOCK_H
#define __FS_VFS_LOCK_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <fcntl.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#if CONFIG_FS_LOCK_BUCKET_SIZE == 0
# define file_initlk()
# define file_closelk(filep)
# define file_getlk(filep, flock) ((void)flock, -ENOSYS)
# define file_setlk(filep, flock, nonblock) ((void)flock, -ENOSYS)
#else
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/****************************************************************************
* Name: file_initlk
*
* Description:
* Initializing file locks
*
****************************************************************************/
void file_initlk(void);
/****************************************************************************
* Name: file_closelk
*
* Description:
* Remove all locks associated with the filep when call close is applied.
*
* Input Parameters:
* filep - The filep that corresponds to the shutdown.
*
****************************************************************************/
void file_closelk(FAR struct file *filep);
/****************************************************************************
* Name: file_getlk
*
* Description:
* Attempts to lock the region (not a real lock), and if there is a
* conflict then returns information about the conflicting locks
*
* Input Parameters:
* filep - File structure instance
* flock - Lock types to be converted
*
* Returned Value:
* The resulting 0 on success. A errno value is returned on any failure.
*
****************************************************************************/
int file_getlk(FAR struct file *filep, FAR struct flock *flock);
/****************************************************************************
* Name: file_setlk
*
* Description:
* Actual execution of locking and unlocking behaviors
*
* Input Parameters:
* filep - File structure instance
* flock - Lock types to be converted
* nonblock - Waiting for lock
*
* Returned Value:
* The resulting 0 on success. A errno value is returned on any failure.
*
****************************************************************************/
int file_setlk(FAR struct file *filep, FAR struct flock *flock,
bool nonblock);
#endif /* CONFIG_FS_LOCK_BUCKET_SIZE */
#endif /* __FS_VFS_LOCK_H */
+13
View File
@@ -289,6 +289,18 @@
# define execvpe execve
#endif
/* Commands for lockf()
* F_ULOCK - Unlock
* F_LOCK - Blocking Exclusive Lock
* F_TLOCK - Attempted Exclusive Locking
* F_TEST - Test Locked Status
*/
#define F_ULOCK 0
#define F_LOCK 1
#define F_TLOCK 2
#define F_TEST 3
/****************************************************************************
* Public Data
****************************************************************************/
@@ -337,6 +349,7 @@ ssize_t pread(int fd, FAR void *buf, size_t nbytes, off_t offset);
ssize_t pwrite(int fd, FAR const void *buf, size_t nbytes, off_t offset);
int ftruncate(int fd, off_t length);
int fchown(int fd, uid_t owner, gid_t group);
int lockf(int fd, int cmd, off_t len);
/* Check if a file descriptor corresponds to a terminal I/O file */
+1
View File
@@ -30,6 +30,7 @@ CSRCS += lib_setrlimit.c lib_getrlimit.c lib_setpriority.c lib_getpriority.c
CSRCS += lib_futimes.c lib_lutimes.c lib_gethostname.c lib_sethostname.c
CSRCS += lib_fchownat.c lib_linkat.c lib_readlinkat.c lib_symlinkat.c
CSRCS += lib_unlinkat.c lib_usleep.c lib_getpgrp.c lib_getpgid.c
CSRCS += lib_lockf.c
ifneq ($(CONFIG_SCHED_USER_IDENTITY),y)
CSRCS += lib_setuid.c lib_setgid.c lib_getuid.c lib_getgid.c
+117
View File
@@ -0,0 +1,117 @@
/****************************************************************************
* libs/libc/unistd/lib_lockf.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: lockf
*
* Description:
* lockf() is a function that allows a process to apply or remove an
* advisory lock on an open file. The lock can be either a shared (read)
* lock or an exclusive (write) lock. This lock is advisory, meaning that
* it is not enforced by the system and it is up to cooperating processes
* to honor the lock.
*
* Input Parameters:
* fd - File descriptor of the open file.
* cmd - Specifies the type of lock operation to be performed.
* It can be one of the following:
* F_LOCK: Request an exclusive (write) lock. If the lock is not
* available, the call may block until it can be acquired.
* F_TLOCK: Try to request an exclusive (write) lock. If the lock is not
* available, the call will not block and will return
* immediately.
* F_ULOCK: Unlock an existing lock.
* F_TEST: Test the lock. Returns 0 if the file is unlocked or locked by
* the calling process, or -1 with errno set to EACCES if another
* process holds the lock.
* len - Length of the locked region, relative to the current file
* position.
*
* Returned Value:
* The returned value of lockf() depends on the success or failure of the
* operation. On success, the return value is 0. On failure, the return
* value is -1, and the errno variable is set to indicate the specific
* error.
*
****************************************************************************/
int lockf(int fd, int cmd, off_t len)
{
struct flock lock;
lock.l_whence = SEEK_CUR;
lock.l_start = 0;
lock.l_len = len;
switch (cmd)
{
case F_LOCK:
{
lock.l_type = F_WRLCK;
return fcntl(fd, F_SETLKW, &lock);
}
case F_TLOCK:
{
lock.l_type = F_WRLCK;
return fcntl(fd, F_SETLK, &lock);
}
case F_ULOCK:
{
lock.l_type = F_UNLCK;
return fcntl(fd, F_SETLK, &lock);
}
case F_TEST:
{
lock.l_type = F_RDLCK;
if (fcntl(fd, F_GETLK, &lock) < 0)
{
return ERROR;
}
/* Check result */
if (lock.l_type == F_UNLCK || lock.l_pid == getpid())
{
return OK;
}
set_errno(EACCES);
return ERROR;
}
}
set_errno(EINVAL);
return ERROR;
}