Skip to content

Commit 14602be

Browse files
wengzheacassis
authored andcommitted
fs/lock: Limit inode type for file locks
When we close a socket fd, it will call get path on sockets. `close(socket_fd)` -> `file_closelk(filep)` -> `file_fcntl(F_GETPATH)` It causes a heavy stack load for each socket close operation. (We have `GETPATH` for sockets to be used for `fdinfo`) But the socket fds are not intended to be used for file locks. And so do some other file types, so we may just limit the usage of flock. Signed-off-by: Zhe Weng <[email protected]>
1 parent 8cdec83 commit 14602be

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

fs/vfs/fs_lock.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,22 @@ static mutex_t g_protect_lock = NXMUTEX_INITIALIZER;
8888
* Private Functions
8989
****************************************************************************/
9090

91+
/****************************************************************************
92+
* Name: file_lock_get_path
93+
****************************************************************************/
94+
95+
static int file_lock_get_path(FAR struct file *filep, FAR char *path)
96+
{
97+
/* We only apply file lock on mount points (f_inode won't be NULL). */
98+
99+
if (!INODE_IS_MOUNTPT(filep->f_inode))
100+
{
101+
return -EBADF;
102+
}
103+
104+
return file_fcntl(filep, F_GETPATH, path);
105+
}
106+
91107
/****************************************************************************
92108
* Name: file_lock_normalize
93109
****************************************************************************/
@@ -534,7 +550,7 @@ int file_getlk(FAR struct file *filep, FAR struct flock *flock)
534550

535551
/* We need to get the unique identifier (Path) via filep */
536552

537-
ret = file_fcntl(filep, F_GETPATH, path);
553+
ret = file_lock_get_path(filep, path);
538554
if (ret < 0)
539555
{
540556
return ret;
@@ -613,7 +629,7 @@ int file_setlk(FAR struct file *filep, FAR struct flock *flock,
613629

614630
/* We need to get the unique identifier (Path) via filep */
615631

616-
ret = file_fcntl(filep, F_GETPATH, path);
632+
ret = file_lock_get_path(filep, path);
617633
if (ret < 0)
618634
{
619635
return ret;
@@ -716,7 +732,7 @@ void file_closelk(FAR struct file *filep)
716732
bool deleted = false;
717733
int ret;
718734

719-
ret = file_fcntl(filep, F_GETPATH, path);
735+
ret = file_lock_get_path(filep, path);
720736
if (ret < 0)
721737
{
722738
/* It isn't an error if fs doesn't support F_GETPATH, so we just end

0 commit comments

Comments
 (0)