1
0
Fork 0
forked from nuttx/nuttx-update

inotify: group g_inotify_xxx global variables into one struct

Signed-off-by: guohao15 <guohao15@xiaomi.com>
This commit is contained in:
guohao15 2024-06-19 15:13:08 +08:00 committed by Xiang Xiao
parent 57250a9602
commit 8731368e45

View file

@ -86,6 +86,14 @@ struct inotify_watch_s
FAR struct inotify_watch_list_s *list; /* Associated watch list */ FAR struct inotify_watch_list_s *list; /* Associated watch list */
}; };
struct inotify_global_s
{
mutex_t lock; /* Enforces global exclusive access */
int event_cookie; /* Event cookie */
int watch_cookie; /* Watch cookie */
struct hsearch_data hash; /* Hash table for watch lists */
};
/**************************************************************************** /****************************************************************************
* Private Functions Prototypes * Private Functions Prototypes
****************************************************************************/ ****************************************************************************/
@ -128,10 +136,10 @@ static struct inode g_inotify_inode =
} }
}; };
static int g_inotify_event_cookie; static struct inotify_global_s g_inotify =
static int g_inotify_watch_cookie; {
static struct hsearch_data g_inotify_hash; .lock = NXMUTEX_INITIALIZER,
static mutex_t g_inotify_hash_lock = NXMUTEX_INITIALIZER; };
/**************************************************************************** /****************************************************************************
* Private Functions * Private Functions
@ -260,7 +268,7 @@ inotify_remove_watch_no_event(FAR struct inotify_watch_s *watch)
{ {
ENTRY item; ENTRY item;
item.key = list->path; item.key = list->path;
hsearch_r(item, DELETE, NULL, &g_inotify_hash); hsearch_r(item, DELETE, NULL, &g_inotify.hash);
} }
} }
@ -498,12 +506,12 @@ static int inotify_close(FAR struct file *filep)
{ {
FAR struct inotify_device_s *dev = filep->f_priv; FAR struct inotify_device_s *dev = filep->f_priv;
nxmutex_lock(&g_inotify_hash_lock); nxmutex_lock(&g_inotify.lock);
nxmutex_lock(&dev->lock); nxmutex_lock(&dev->lock);
if (--dev->count > 0) if (--dev->count > 0)
{ {
nxmutex_unlock(&dev->lock); nxmutex_unlock(&dev->lock);
nxmutex_unlock(&g_inotify_hash_lock); nxmutex_unlock(&g_inotify.lock);
return OK; return OK;
} }
@ -527,7 +535,7 @@ static int inotify_close(FAR struct file *filep)
} }
nxmutex_unlock(&dev->lock); nxmutex_unlock(&dev->lock);
nxmutex_unlock(&g_inotify_hash_lock); nxmutex_unlock(&g_inotify.lock);
nxmutex_destroy(&dev->lock); nxmutex_destroy(&dev->lock);
nxsem_destroy(&dev->sem); nxsem_destroy(&dev->sem);
kmm_free(dev); kmm_free(dev);
@ -607,7 +615,7 @@ inotify_alloc_watch(FAR struct inotify_device_s *dev,
watch->dev = dev; watch->dev = dev;
watch->mask = mask; watch->mask = mask;
watch->wd = ++g_inotify_watch_cookie; watch->wd = ++g_inotify.watch_cookie;
watch->list = list; watch->list = list;
list_add_tail(&dev->watches, &watch->d_node); list_add_tail(&dev->watches, &watch->d_node);
list_add_tail(&list->watches, &watch->l_node); list_add_tail(&list->watches, &watch->l_node);
@ -700,7 +708,7 @@ inotify_alloc_watch_list(FAR const char *path)
item.key = list->path; item.key = list->path;
item.data = list; item.data = list;
if (hsearch_r(item, ENTER, &result, &g_inotify_hash) == 0) if (hsearch_r(item, ENTER, &result, &g_inotify.hash) == 0)
{ {
lib_free(list->path); lib_free(list->path);
kmm_free(list); kmm_free(list);
@ -725,7 +733,7 @@ inotify_get_watch_list(FAR const char *path)
ENTRY item; ENTRY item;
item.key = (FAR char *)path; item.key = (FAR char *)path;
if (hsearch_r(item, FIND, &result, &g_inotify_hash) == 0) if (hsearch_r(item, FIND, &result, &g_inotify.hash) == 0)
{ {
return NULL; return NULL;
} }
@ -841,10 +849,10 @@ static void notify_queue_path_event(FAR const char *path, uint32_t mask)
{ {
if (mask & IN_MOVED_FROM) if (mask & IN_MOVED_FROM)
{ {
++g_inotify_event_cookie; ++g_inotify.event_cookie;
} }
cookie = g_inotify_event_cookie; cookie = g_inotify.event_cookie;
} }
list = inotify_get_watch_list(abspath); list = inotify_get_watch_list(abspath);
@ -941,10 +949,10 @@ static inline void notify_queue_filep_event(FAR struct file *filep,
mask |= IN_ISDIR; mask |= IN_ISDIR;
} }
nxmutex_lock(&g_inotify_hash_lock); nxmutex_lock(&g_inotify.lock);
notify_queue_path_event(pathbuffer, mask); notify_queue_path_event(pathbuffer, mask);
lib_put_pathbuffer(pathbuffer); lib_put_pathbuffer(pathbuffer);
nxmutex_unlock(&g_inotify_hash_lock); nxmutex_unlock(&g_inotify.lock);
} }
/**************************************************************************** /****************************************************************************
@ -1013,7 +1021,7 @@ int inotify_add_watch(int fd, FAR const char *pathname, uint32_t mask)
goto out_free; goto out_free;
} }
nxmutex_lock(&g_inotify_hash_lock); nxmutex_lock(&g_inotify.lock);
nxmutex_lock(&dev->lock); nxmutex_lock(&dev->lock);
list = inotify_get_watch_list(abspath); list = inotify_get_watch_list(abspath);
if (list == NULL) if (list == NULL)
@ -1052,7 +1060,7 @@ int inotify_add_watch(int fd, FAR const char *pathname, uint32_t mask)
{ {
ENTRY item; ENTRY item;
item.key = list->path; item.key = list->path;
hsearch_r(item, DELETE, NULL, &g_inotify_hash); hsearch_r(item, DELETE, NULL, &g_inotify.hash);
ret = -ENOMEM; ret = -ENOMEM;
goto out; goto out;
} }
@ -1062,7 +1070,7 @@ int inotify_add_watch(int fd, FAR const char *pathname, uint32_t mask)
out: out:
nxmutex_unlock(&dev->lock); nxmutex_unlock(&dev->lock);
nxmutex_unlock(&g_inotify_hash_lock); nxmutex_unlock(&g_inotify.lock);
out_free: out_free:
lib_free(abspath); lib_free(abspath);
@ -1104,20 +1112,20 @@ int inotify_rm_watch(int fd, int wd)
return ERROR; return ERROR;
} }
nxmutex_lock(&g_inotify_hash_lock); nxmutex_lock(&g_inotify.lock);
nxmutex_lock(&dev->lock); nxmutex_lock(&dev->lock);
watch = inotify_find_watch(dev, wd); watch = inotify_find_watch(dev, wd);
if (watch == NULL) if (watch == NULL)
{ {
nxmutex_unlock(&dev->lock); nxmutex_unlock(&dev->lock);
nxmutex_unlock(&g_inotify_hash_lock); nxmutex_unlock(&g_inotify.lock);
set_errno(EINVAL); set_errno(EINVAL);
return ERROR; return ERROR;
} }
inotify_remove_watch(dev, watch); inotify_remove_watch(dev, watch);
nxmutex_unlock(&dev->lock); nxmutex_unlock(&dev->lock);
nxmutex_unlock(&g_inotify_hash_lock); nxmutex_unlock(&g_inotify.lock);
return OK; return OK;
} }
@ -1211,7 +1219,7 @@ void notify_initialize(void)
{ {
int ret; int ret;
ret = hcreate_r(CONFIG_FS_NOTIFY_BUCKET_SIZE, &g_inotify_hash); ret = hcreate_r(CONFIG_FS_NOTIFY_BUCKET_SIZE, &g_inotify.hash);
if (ret != 1) if (ret != 1)
{ {
ferr("Failed to create hash table\n"); ferr("Failed to create hash table\n");
@ -1240,9 +1248,9 @@ void notify_open(FAR const char *path, int oflags)
mask |= IN_CREATE; mask |= IN_CREATE;
} }
nxmutex_lock(&g_inotify_hash_lock); nxmutex_lock(&g_inotify.lock);
notify_queue_path_event(path, mask); notify_queue_path_event(path, mask);
nxmutex_unlock(&g_inotify_hash_lock); nxmutex_unlock(&g_inotify.lock);
} }
/**************************************************************************** /****************************************************************************
@ -1283,14 +1291,14 @@ void notify_close2(FAR struct inode *inode)
return; return;
} }
nxmutex_lock(&g_inotify_hash_lock); nxmutex_lock(&g_inotify.lock);
if (inode_getpath(inode, pathbuffer, PATH_MAX) >= 0) if (inode_getpath(inode, pathbuffer, PATH_MAX) >= 0)
{ {
notify_queue_path_event(pathbuffer, IN_CLOSE_WRITE); notify_queue_path_event(pathbuffer, IN_CLOSE_WRITE);
} }
lib_put_pathbuffer(pathbuffer); lib_put_pathbuffer(pathbuffer);
nxmutex_unlock(&g_inotify_hash_lock); nxmutex_unlock(&g_inotify.lock);
} }
/**************************************************************************** /****************************************************************************
@ -1342,9 +1350,9 @@ void notify_chstat(FAR struct file *filep)
void notify_unlink(FAR const char *path) void notify_unlink(FAR const char *path)
{ {
nxmutex_lock(&g_inotify_hash_lock); nxmutex_lock(&g_inotify.lock);
notify_queue_path_event(path, IN_DELETE); notify_queue_path_event(path, IN_DELETE);
nxmutex_unlock(&g_inotify_hash_lock); nxmutex_unlock(&g_inotify.lock);
} }
/**************************************************************************** /****************************************************************************
@ -1357,9 +1365,9 @@ void notify_unlink(FAR const char *path)
void notify_unmount(FAR const char *path) void notify_unmount(FAR const char *path)
{ {
nxmutex_lock(&g_inotify_hash_lock); nxmutex_lock(&g_inotify.lock);
notify_queue_path_event(path, IN_DELETE | IN_UNMOUNT); notify_queue_path_event(path, IN_DELETE | IN_UNMOUNT);
nxmutex_unlock(&g_inotify_hash_lock); nxmutex_unlock(&g_inotify.lock);
} }
/**************************************************************************** /****************************************************************************
@ -1372,9 +1380,9 @@ void notify_unmount(FAR const char *path)
void notify_mkdir(FAR const char *path) void notify_mkdir(FAR const char *path)
{ {
nxmutex_lock(&g_inotify_hash_lock); nxmutex_lock(&g_inotify.lock);
notify_queue_path_event(path, IN_CREATE | IN_ISDIR); notify_queue_path_event(path, IN_CREATE | IN_ISDIR);
nxmutex_unlock(&g_inotify_hash_lock); nxmutex_unlock(&g_inotify.lock);
} }
/**************************************************************************** /****************************************************************************
@ -1387,9 +1395,9 @@ void notify_mkdir(FAR const char *path)
void notify_create(FAR const char *path) void notify_create(FAR const char *path)
{ {
nxmutex_lock(&g_inotify_hash_lock); nxmutex_lock(&g_inotify.lock);
notify_queue_path_event(path, IN_CREATE); notify_queue_path_event(path, IN_CREATE);
nxmutex_unlock(&g_inotify_hash_lock); nxmutex_unlock(&g_inotify.lock);
} }
/**************************************************************************** /****************************************************************************
@ -1416,8 +1424,8 @@ void notify_rename(FAR const char *oldpath, bool oldisdir,
oldmask |= IN_ISDIR; oldmask |= IN_ISDIR;
} }
nxmutex_lock(&g_inotify_hash_lock); nxmutex_lock(&g_inotify.lock);
notify_queue_path_event(oldpath, oldmask); notify_queue_path_event(oldpath, oldmask);
notify_queue_path_event(newpath, newmask); notify_queue_path_event(newpath, newmask);
nxmutex_unlock(&g_inotify_hash_lock); nxmutex_unlock(&g_inotify.lock);
} }