You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by GitBox <gi...@apache.org> on 2023/01/13 12:14:13 UTC

[GitHub] [nuttx] jlaitine opened a new pull request, #8109: Add shmfs

jlaitine opened a new pull request, #8109:
URL: https://github.com/apache/nuttx/pull/8109

   ## Summary
   
   This adds a simple driver for implementing posix SHM interface, which can be used in all NuttX memory protection modes (flat, protected & kernel). This is the initial implementation, which doesn't yet support protection flags, mmap with offset or chaning shared object size with ftruncate (only initial setting of size).
   
   The basic usage of shm_open, shm_unlink, ftruncate, mmap, munmap and close are supported.
   
   The initial implementation supports the most common use case of inter-process communication via shared memory. A common example of use would be:
   
   1) One process creates the SHM object and truncates it to correct size:
   
   ```
   int shm_fd;
   struct my_struct_s *ptr = MAP_FAILED;
   
   shm_fd = shm_open("my_object", O_CREAT | O_RDWR, 0666);
   
   if (shm_fd >= 0)
     {
       /* truncate size of my_struct_s */
   
       if (ftruncate(shm_fd, sizeof(my_struct_s)) == 0)
         {
           /* Succesfull creation of the object, mmap it */
   
           ptr = mmap(NULL, sizeof(my_struct_s), PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
   
         }
     }
   
   if (ptr != MAP_FAILED)
     {
           /* The file is no longer needed, close it */
           close(shm_fd);
   
           /* use "ptr" to read/write data to the object */
   
           ...
           
     }
   
   ```
   
   2) Other processes open and use the same shared data:
   
   ```
   struct my_struct_s *ptr = MAP_FAILED;
   int shm_fd;
   
   shm_fd = shm_open("my_object", O_RDWR, 0666);
   if (shm_fd >= 0)
     {
       ptr = mmap(NULL, sizeof(my_struct_s), PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
     }
   
   if (ptr != MAP_FAILED)
     {
           /* The file is no longer needed, close it */
           close(shm_fd);
   
           /* use "ptr" to read/write data to the object */
   
           ...
           
     }
   ```
   
   ## Impact
   
   Add a new driver
   
   ## Testing
   
   Tested in custom environment on STM32F7 in flat and protected builds, and on RISC-V MPFS target in kernel build. The same application has also been executed on linux.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072506437


##########
fs/shm/shm_unlink.c:
##########
@@ -0,0 +1,167 @@
+/****************************************************************************
+ * fs/shm/shm_unlink.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 <stdio.h>
+#include <assert.h>
+#include <errno.h>
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int file_shm_unlink(FAR const char *name)
+{
+  FAR struct inode *inode;
+  struct inode_search_s desc;
+  char fullpath[sizeof(CONFIG_FS_SHMFS_VFS_PATH) + CONFIG_NAME_MAX + 2];
+  int ret;
+
+  /* Make sure that a non-NULL name is supplied */
+
+  if (!name)
+    {
+      return -ENOENT;
+    }
+
+  /* Remove any number of leading '/' */
+
+  while (*name == '/')
+    {
+      name++;
+    }
+
+  /* Empty name supplied? */
+
+  if (*name == '\0')
+    {
+      return -ENOENT;
+    }
+
+  /* Name too long? */
+
+  if (strnlen(name, CONFIG_NAME_MAX) > CONFIG_NAME_MAX)
+    {
+      return -ENAMETOOLONG;
+    }
+
+  /* Get the full path to the shm object */
+
+  snprintf(fullpath, sizeof(fullpath),
+           CONFIG_FS_SHMFS_VFS_PATH "/%s", name);
+
+  /* Get the inode for this shm object */
+
+  SETUP_SEARCH(&desc, fullpath, false);
+
+  ret = inode_lock();
+  if (ret < 0)
+    {
+      goto errout_with_search;
+    }
+
+  ret = inode_find(&desc);
+  if (ret < 0)
+    {
+      /* There is no inode that includes in this path */
+
+      goto errout_with_sem;
+    }
+
+  /* Get the search results */
+
+  inode = desc.node;
+  DEBUGASSERT(inode != NULL);
+
+  /* Verify that what we found is, indeed, an shm inode */
+
+  if (!INODE_IS_SHM(inode))
+    {
+      ret = -ENOENT;
+      inode_release(inode);
+      goto errout_with_sem;
+    }
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  if (inode->u.i_ops->unlink)
+    {
+      /* Notify the shmfs driver that it has been unlinked */
+
+      ret = inode->u.i_ops->unlink(inode);
+      if (ret < 0)
+        {
+          goto errout_with_inode;
+        }
+    }
+#endif
+
+  /* Remove the old inode from the tree. If we hold a reference count
+   * on the inode, it will not be deleted now.  This will set the
+   * FSNODEFLAG_DELETED bit in the inode flags.
+   */
+
+  inode_release(inode);

Review Comment:
   yes. removed



##########
fs/shm/shm_unlink.c:
##########
@@ -0,0 +1,167 @@
+/****************************************************************************
+ * fs/shm/shm_unlink.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 <stdio.h>
+#include <assert.h>
+#include <errno.h>
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int file_shm_unlink(FAR const char *name)
+{
+  FAR struct inode *inode;
+  struct inode_search_s desc;
+  char fullpath[sizeof(CONFIG_FS_SHMFS_VFS_PATH) + CONFIG_NAME_MAX + 2];
+  int ret;
+
+  /* Make sure that a non-NULL name is supplied */
+
+  if (!name)
+    {
+      return -ENOENT;
+    }
+
+  /* Remove any number of leading '/' */
+
+  while (*name == '/')
+    {
+      name++;
+    }
+
+  /* Empty name supplied? */
+
+  if (*name == '\0')
+    {
+      return -ENOENT;
+    }
+
+  /* Name too long? */
+
+  if (strnlen(name, CONFIG_NAME_MAX) > CONFIG_NAME_MAX)
+    {
+      return -ENAMETOOLONG;
+    }
+
+  /* Get the full path to the shm object */
+
+  snprintf(fullpath, sizeof(fullpath),
+           CONFIG_FS_SHMFS_VFS_PATH "/%s", name);
+
+  /* Get the inode for this shm object */
+
+  SETUP_SEARCH(&desc, fullpath, false);
+
+  ret = inode_lock();
+  if (ret < 0)
+    {
+      goto errout_with_search;
+    }
+
+  ret = inode_find(&desc);
+  if (ret < 0)
+    {
+      /* There is no inode that includes in this path */
+
+      goto errout_with_sem;
+    }
+
+  /* Get the search results */
+
+  inode = desc.node;
+  DEBUGASSERT(inode != NULL);
+
+  /* Verify that what we found is, indeed, an shm inode */
+
+  if (!INODE_IS_SHM(inode))
+    {
+      ret = -ENOENT;
+      inode_release(inode);

Review Comment:
   done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072034398


##########
fs/shm/shmfs.c:
##########
@@ -0,0 +1,384 @@
+/****************************************************************************
+ * fs/shm/shmfs.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 <assert.h>
+
+#include <nuttx/fs/ioctl.h>
+#include <nuttx/mm/map.h>
+
+#if defined (CONFIG_BUILD_KERNEL)
+#include <nuttx/arch.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
+#endif
+
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep);

Review Comment:
   this I decided not to do, I like the naming as is



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072867226


##########
fs/shm/shmfs.c:
##########
@@ -0,0 +1,384 @@
+/****************************************************************************
+ * fs/shm/shmfs.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 <assert.h>
+
+#include <nuttx/fs/ioctl.h>
+#include <nuttx/mm/map.h>
+
+#if defined (CONFIG_BUILD_KERNEL)
+#include <nuttx/arch.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
+#endif
+
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep);
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen);
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen);
+static int shmfs_truncate(FAR struct file *filep, off_t length);
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode);
+#endif
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry);
+static int shmfs_munmap(FAR struct task_group_s *group,
+                        FAR struct mm_map_entry_s *entry,
+                        FAR void *start,
+                        size_t length);
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+const struct file_operations shmfs_operations =
+{
+  NULL,             /* open */
+  shmfs_close,      /* close */
+  shmfs_read,       /* read */
+  shmfs_write,      /* write */
+  NULL,             /* seek */
+  NULL,             /* ioctl */
+  shmfs_mmap,       /* mmap */
+  shmfs_truncate,   /* truncate */
+  NULL,             /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  shmfs_unlink      /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: shmfs_read
+ ****************************************************************************/
+
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_write
+ ****************************************************************************/
+
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_release
+ ****************************************************************************/
+
+static int shmfs_release(FAR struct inode *inode)
+{
+  /* If the file has been unlinked previously, delete the contents.
+   * The inode is released after this call, hence checking if i_crefs <= 1.
+   */
+
+  int ret = inode_lock();
+  if (ret >= 0)
+    {
+      if (inode->i_parent == NULL &&
+          inode->i_crefs <= 1)
+        {
+          delete_shm_object(inode->i_private);
+          inode->i_private = NULL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_close
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep)
+{
+  /* Release the shmfs object. The object gets deleted if no-one has
+   * reference to it (either mmap or open file) and the object has been
+   * unlinked
+   */
+
+  return shmfs_release(filep->f_inode);
+}
+
+/****************************************************************************
+ * Name: shmfs_truncate
+ ****************************************************************************/
+
+static int shmfs_truncate(FAR struct file *filep, off_t length)
+{
+  FAR struct shmfs_object_s *object;
+  int ret;
+
+  if (length == 0)
+    {
+      return -EINVAL;
+    }
+
+  ret = inode_lock();
+  if (ret >= 0)
+    {
+      object = filep->f_inode->i_private;
+      if (!object)
+        {
+          filep->f_inode->i_private = alloc_shm_object(length);
+        }
+      else if (object->length != length)
+        {
+          /* This doesn't support resize */
+
+          ret = -EINVAL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_unlink
+ ****************************************************************************/
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode)
+{
+  if (inode->i_crefs <= 1)
+    {
+      delete_shm_object(inode->i_private);
+      inode->i_private = NULL;
+    }
+
+  return OK;
+}
+#endif
+
+/****************************************************************************
+ * Name: shmfs_map_object
+ ****************************************************************************/
+
+static int shmfs_map_object(FAR struct shmfs_object_s *object,
+                            FAR void **vaddr)
+{
+  int ret = OK;
+
+#ifdef CONFIG_BUILD_KERNEL
+  /* Map the physical pages of the shm object with MMU. */
+
+  FAR struct tcb_s        *tcb   = nxsched_self();
+  FAR struct task_group_s *group = tcb->group;
+  FAR uintptr_t *pages =
+    (FAR uintptr_t *)&object->paddr;
+  uintptr_t                mapaddr;
+  unsigned int             npages;
+
+  /* Find a free vaddr space that satisfies length */
+
+  mapaddr = (uintptr_t)vm_alloc_region(get_group_mm(group), 0,
+                                       object->length);
+  if (mapaddr == 0)
+    {
+      return -ENOMEM;
+    }
+
+  /* Convert the region size to pages */
+
+  npages = MM_NPAGES(object->length);
+
+  /* Map the memory to user virtual address space */
+
+  ret = up_shmat(pages, npages, mapaddr);
+  if (ret < 0)
+    {
+      vm_release_region(get_group_mm(group), (FAR void *)mapaddr,
+                        object->length);
+    }
+  else
+    {
+      *vaddr = (FAR void *)mapaddr;
+    }
+#else
+  /* Use the physical address directly */
+
+  *vaddr = object->paddr;
+#endif
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_mmap
+ ****************************************************************************/
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry)
+{
+  FAR struct shmfs_object_s *object;
+  int ret = -EINVAL;
+
+  DEBUGASSERT(filep->f_inode != NULL);
+
+  /* We don't support offset at the moment, just mapping the whole object */
+
+  if (entry->offset != 0)
+    {
+      return ret;
+    }
+
+  /* Keep the inode when mmapped, increase refcount */
+
+  ret = inode_addref(filep->f_inode);
+  if (ret >= 0)
+    {
+      object = (FAR struct shmfs_object_s *)filep->f_inode->i_private;
+
+      if (object)

Review Comment:
   yes of course. The above code is actually the typical way to use posix shm, why keep the fd when you have already mmapped it. This wasn't about not bumping the refcounter in mmap at all, that you have to do always. It was just the place where you use the "object" ptr (inode->i_private). I think that is is supposed to be after refcount bump. otherwise crazy multithreaded application can close & unlink the fd in the middle of other thread doing mmap on it. A bit far fetched maybe, but better safe than sorry.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072501026


##########
fs/shm/shmfs_alloc.c:
##########
@@ -0,0 +1,143 @@
+/****************************************************************************
+ * fs/shm/shmfs_alloc.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 <stdlib.h>
+#include <nuttx/pgalloc.h>
+
+#include "inode/inode.h"
+#include "shm/shmfs.h"
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+FAR struct shmfs_object_s *shmfs_alloc_object(size_t length)
+{
+  FAR struct shmfs_object_s *object;
+  bool allocated = false;
+
+#if defined(CONFIG_BUILD_FLAT)
+  /* in FLAT build, allocate the object metadata and the data in the same
+   * chunk in kernel heap
+   */
+
+  object = kmm_zalloc(sizeof(struct shmfs_object_s) + length);
+  if (object)
+    {
+      object->paddr = (&object->paddr) + 1;
+      allocated = true;
+    }
+
+#elif defined(CONFIG_BUILD_PROTECTED)
+  /* in PROTECTED build, allocate the shm object in kernel heap, and shared
+   * memory in user heap
+   */
+
+  object = kmm_zalloc(sizeof(struct shmfs_object_s));
+  if (object)
+    {
+      object->paddr = kumm_zalloc(length);
+
+      if (object->paddr)
+        {
+           allocated = true;
+        }
+    }
+
+#elif defined(CONFIG_BUILD_KERNEL)
+  /* in KERNEL build, allocate the shared memory from page pool and store the
+   * physical address
+   */
+
+  size_t i = 0;
+  void **pages;
+  size_t n_pages = MM_NPAGES(length);
+
+  object = kmm_zalloc(sizeof(struct shmfs_object_s) +
+                      (n_pages - 1) * sizeof(object->paddr));
+
+  if (object)
+    {
+      pages = &object->paddr;
+      for (; i < n_pages; i++)
+        {
+          pages[i] = (void *)mm_pgalloc(1);

Review Comment:
   done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072072768


##########
fs/shm/shm_unlink.c:
##########
@@ -0,0 +1,140 @@
+/****************************************************************************
+ * fs/shm/shm_unlink.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 <stdio.h>
+#include <assert.h>
+#include <errno.h>
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int file_shm_unlink(FAR const char *shm_name)
+{
+  FAR struct inode *inode;
+  struct inode_search_s desc;
+  char fullpath[sizeof(CONFIG_FS_SHMFS_VFS_PATH) + CONFIG_NAME_MAX + 2];
+  int ret;
+
+  /* Get the full path to the shm object */
+
+  snprintf(fullpath, sizeof(fullpath),
+           CONFIG_FS_SHMFS_VFS_PATH "/%s", shm_name);
+
+  /* Get the inode for this shm object */
+
+  SETUP_SEARCH(&desc, fullpath, false);
+
+  ret = inode_lock();
+  if (ret < 0)
+    {
+      goto errout_with_search;
+    }
+
+  ret = inode_find(&desc);
+  if (ret < 0)
+    {
+      /* There is no inode that includes in this path */
+
+      goto errout_with_sem;
+    }
+
+  /* Get the search results */
+
+  inode = desc.node;
+  DEBUGASSERT(inode != NULL);
+
+  /* Verify that what we found is, indeed, an shm inode */
+
+  if (!INODE_IS_SHM(inode))
+    {
+      ret = -ENOENT;
+      inode_release(inode);
+      goto errout_with_sem;
+    }
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  if (inode->u.i_ops->unlink)
+    {
+      /* Notify the shmfs driver that it has been unlinked */
+
+      ret = inode->u.i_ops->unlink(inode);
+      if (ret < 0)
+        {
+          goto errout_with_inode;
+        }
+    }
+#endif
+
+  /* Remove the old inode from the tree. If we hold a reference count
+   * on the inode, it will not be deleted now.  This will set the
+   * FSNODEFLAG_DELETED bit in the inode flags.
+   */
+
+  inode_release(inode);
+  ret = inode_remove(fullpath);
+
+  /* inode_remove() should always fail with -EBUSY because we have a
+   * reference on the inode.  -EBUSY means that the inode was, indeed,
+   * unlinked but it could not be freed because there are references.
+   */
+
+  if (ret == -EBUSY)
+    {
+      ret = OK;
+    }
+
+  DEBUGASSERT(ret == OK);
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+errout_with_inode:
+  inode_release(inode);
+#endif
+
+errout_with_sem:
+  inode_unlock();
+
+errout_with_search:
+  RELEASE_SEARCH(&desc);
+

Review Comment:
   done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1070661398


##########
fs/shm/Kconfig:
##########
@@ -3,16 +3,16 @@
 # see the file kconfig-language.txt in the NuttX tools repository.
 #
 
-config FS_SHM
+config FS_SHMFS

Review Comment:
   SHMFS is consistent with other FS config flags (ROMFS ....) Let's rather keep it consistent



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072505900


##########
fs/shm/shm_open.c:
##########
@@ -0,0 +1,181 @@
+/****************************************************************************
+ * fs/shm/shm_open.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 <nuttx/config.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <errno.h>
+
+#include "inode/inode.h"
+#include "shm/shmfs.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int file_shm_open(FAR struct file *shm, FAR const char *name,
+                         int oflags, mode_t mode)
+{
+  FAR struct inode *inode;
+  struct inode_search_s desc;
+  char fullpath[sizeof(CONFIG_FS_SHMFS_VFS_PATH) + CONFIG_NAME_MAX + 2];
+  int ret;
+
+  /* Make sure that a non-NULL name is supplied */
+
+  if (!shm || !name)
+    {
+      return -EINVAL;
+    }
+
+  /* Remove any number of leading '/' */
+
+  while (*name == '/')

Review Comment:
   see previous



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072867226


##########
fs/shm/shmfs.c:
##########
@@ -0,0 +1,384 @@
+/****************************************************************************
+ * fs/shm/shmfs.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 <assert.h>
+
+#include <nuttx/fs/ioctl.h>
+#include <nuttx/mm/map.h>
+
+#if defined (CONFIG_BUILD_KERNEL)
+#include <nuttx/arch.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
+#endif
+
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep);
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen);
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen);
+static int shmfs_truncate(FAR struct file *filep, off_t length);
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode);
+#endif
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry);
+static int shmfs_munmap(FAR struct task_group_s *group,
+                        FAR struct mm_map_entry_s *entry,
+                        FAR void *start,
+                        size_t length);
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+const struct file_operations shmfs_operations =
+{
+  NULL,             /* open */
+  shmfs_close,      /* close */
+  shmfs_read,       /* read */
+  shmfs_write,      /* write */
+  NULL,             /* seek */
+  NULL,             /* ioctl */
+  shmfs_mmap,       /* mmap */
+  shmfs_truncate,   /* truncate */
+  NULL,             /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  shmfs_unlink      /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: shmfs_read
+ ****************************************************************************/
+
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_write
+ ****************************************************************************/
+
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_release
+ ****************************************************************************/
+
+static int shmfs_release(FAR struct inode *inode)
+{
+  /* If the file has been unlinked previously, delete the contents.
+   * The inode is released after this call, hence checking if i_crefs <= 1.
+   */
+
+  int ret = inode_lock();
+  if (ret >= 0)
+    {
+      if (inode->i_parent == NULL &&
+          inode->i_crefs <= 1)
+        {
+          delete_shm_object(inode->i_private);
+          inode->i_private = NULL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_close
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep)
+{
+  /* Release the shmfs object. The object gets deleted if no-one has
+   * reference to it (either mmap or open file) and the object has been
+   * unlinked
+   */
+
+  return shmfs_release(filep->f_inode);
+}
+
+/****************************************************************************
+ * Name: shmfs_truncate
+ ****************************************************************************/
+
+static int shmfs_truncate(FAR struct file *filep, off_t length)
+{
+  FAR struct shmfs_object_s *object;
+  int ret;
+
+  if (length == 0)
+    {
+      return -EINVAL;
+    }
+
+  ret = inode_lock();
+  if (ret >= 0)
+    {
+      object = filep->f_inode->i_private;
+      if (!object)
+        {
+          filep->f_inode->i_private = alloc_shm_object(length);
+        }
+      else if (object->length != length)
+        {
+          /* This doesn't support resize */
+
+          ret = -EINVAL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_unlink
+ ****************************************************************************/
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode)
+{
+  if (inode->i_crefs <= 1)
+    {
+      delete_shm_object(inode->i_private);
+      inode->i_private = NULL;
+    }
+
+  return OK;
+}
+#endif
+
+/****************************************************************************
+ * Name: shmfs_map_object
+ ****************************************************************************/
+
+static int shmfs_map_object(FAR struct shmfs_object_s *object,
+                            FAR void **vaddr)
+{
+  int ret = OK;
+
+#ifdef CONFIG_BUILD_KERNEL
+  /* Map the physical pages of the shm object with MMU. */
+
+  FAR struct tcb_s        *tcb   = nxsched_self();
+  FAR struct task_group_s *group = tcb->group;
+  FAR uintptr_t *pages =
+    (FAR uintptr_t *)&object->paddr;
+  uintptr_t                mapaddr;
+  unsigned int             npages;
+
+  /* Find a free vaddr space that satisfies length */
+
+  mapaddr = (uintptr_t)vm_alloc_region(get_group_mm(group), 0,
+                                       object->length);
+  if (mapaddr == 0)
+    {
+      return -ENOMEM;
+    }
+
+  /* Convert the region size to pages */
+
+  npages = MM_NPAGES(object->length);
+
+  /* Map the memory to user virtual address space */
+
+  ret = up_shmat(pages, npages, mapaddr);
+  if (ret < 0)
+    {
+      vm_release_region(get_group_mm(group), (FAR void *)mapaddr,
+                        object->length);
+    }
+  else
+    {
+      *vaddr = (FAR void *)mapaddr;
+    }
+#else
+  /* Use the physical address directly */
+
+  *vaddr = object->paddr;
+#endif
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_mmap
+ ****************************************************************************/
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry)
+{
+  FAR struct shmfs_object_s *object;
+  int ret = -EINVAL;
+
+  DEBUGASSERT(filep->f_inode != NULL);
+
+  /* We don't support offset at the moment, just mapping the whole object */
+
+  if (entry->offset != 0)
+    {
+      return ret;
+    }
+
+  /* Keep the inode when mmapped, increase refcount */
+
+  ret = inode_addref(filep->f_inode);
+  if (ret >= 0)
+    {
+      object = (FAR struct shmfs_object_s *)filep->f_inode->i_private;
+
+      if (object)

Review Comment:
   yes of course. This wasn't about not bumping the refcounter in mmap at all, that you have to do always. It was just the place where you use the "object" ptr (inode->i_private). I think that is is supposed to be after refcount bump. otherwise crazy multithreaded application can unlink the fd in the middle of other thread doing mmap on it.
   



##########
fs/shm/shmfs.c:
##########
@@ -0,0 +1,384 @@
+/****************************************************************************
+ * fs/shm/shmfs.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 <assert.h>
+
+#include <nuttx/fs/ioctl.h>
+#include <nuttx/mm/map.h>
+
+#if defined (CONFIG_BUILD_KERNEL)
+#include <nuttx/arch.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
+#endif
+
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep);
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen);
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen);
+static int shmfs_truncate(FAR struct file *filep, off_t length);
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode);
+#endif
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry);
+static int shmfs_munmap(FAR struct task_group_s *group,
+                        FAR struct mm_map_entry_s *entry,
+                        FAR void *start,
+                        size_t length);
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+const struct file_operations shmfs_operations =
+{
+  NULL,             /* open */
+  shmfs_close,      /* close */
+  shmfs_read,       /* read */
+  shmfs_write,      /* write */
+  NULL,             /* seek */
+  NULL,             /* ioctl */
+  shmfs_mmap,       /* mmap */
+  shmfs_truncate,   /* truncate */
+  NULL,             /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  shmfs_unlink      /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: shmfs_read
+ ****************************************************************************/
+
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_write
+ ****************************************************************************/
+
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_release
+ ****************************************************************************/
+
+static int shmfs_release(FAR struct inode *inode)
+{
+  /* If the file has been unlinked previously, delete the contents.
+   * The inode is released after this call, hence checking if i_crefs <= 1.
+   */
+
+  int ret = inode_lock();
+  if (ret >= 0)
+    {
+      if (inode->i_parent == NULL &&
+          inode->i_crefs <= 1)
+        {
+          delete_shm_object(inode->i_private);
+          inode->i_private = NULL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_close
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep)
+{
+  /* Release the shmfs object. The object gets deleted if no-one has
+   * reference to it (either mmap or open file) and the object has been
+   * unlinked
+   */
+
+  return shmfs_release(filep->f_inode);
+}
+
+/****************************************************************************
+ * Name: shmfs_truncate
+ ****************************************************************************/
+
+static int shmfs_truncate(FAR struct file *filep, off_t length)
+{
+  FAR struct shmfs_object_s *object;
+  int ret;
+
+  if (length == 0)
+    {
+      return -EINVAL;
+    }
+
+  ret = inode_lock();
+  if (ret >= 0)
+    {
+      object = filep->f_inode->i_private;
+      if (!object)
+        {
+          filep->f_inode->i_private = alloc_shm_object(length);
+        }
+      else if (object->length != length)
+        {
+          /* This doesn't support resize */
+
+          ret = -EINVAL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_unlink
+ ****************************************************************************/
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode)
+{
+  if (inode->i_crefs <= 1)
+    {
+      delete_shm_object(inode->i_private);
+      inode->i_private = NULL;
+    }
+
+  return OK;
+}
+#endif
+
+/****************************************************************************
+ * Name: shmfs_map_object
+ ****************************************************************************/
+
+static int shmfs_map_object(FAR struct shmfs_object_s *object,
+                            FAR void **vaddr)
+{
+  int ret = OK;
+
+#ifdef CONFIG_BUILD_KERNEL
+  /* Map the physical pages of the shm object with MMU. */
+
+  FAR struct tcb_s        *tcb   = nxsched_self();
+  FAR struct task_group_s *group = tcb->group;
+  FAR uintptr_t *pages =
+    (FAR uintptr_t *)&object->paddr;
+  uintptr_t                mapaddr;
+  unsigned int             npages;
+
+  /* Find a free vaddr space that satisfies length */
+
+  mapaddr = (uintptr_t)vm_alloc_region(get_group_mm(group), 0,
+                                       object->length);
+  if (mapaddr == 0)
+    {
+      return -ENOMEM;
+    }
+
+  /* Convert the region size to pages */
+
+  npages = MM_NPAGES(object->length);
+
+  /* Map the memory to user virtual address space */
+
+  ret = up_shmat(pages, npages, mapaddr);
+  if (ret < 0)
+    {
+      vm_release_region(get_group_mm(group), (FAR void *)mapaddr,
+                        object->length);
+    }
+  else
+    {
+      *vaddr = (FAR void *)mapaddr;
+    }
+#else
+  /* Use the physical address directly */
+
+  *vaddr = object->paddr;
+#endif
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_mmap
+ ****************************************************************************/
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry)
+{
+  FAR struct shmfs_object_s *object;
+  int ret = -EINVAL;
+
+  DEBUGASSERT(filep->f_inode != NULL);
+
+  /* We don't support offset at the moment, just mapping the whole object */
+
+  if (entry->offset != 0)
+    {
+      return ret;
+    }
+
+  /* Keep the inode when mmapped, increase refcount */
+
+  ret = inode_addref(filep->f_inode);
+  if (ret >= 0)
+    {
+      object = (FAR struct shmfs_object_s *)filep->f_inode->i_private;
+
+      if (object)

Review Comment:
   yes of course. This wasn't about not bumping the refcounter in mmap at all, that you have to do always. It was just the place where you use the "object" ptr (inode->i_private). I think that is is supposed to be after refcount bump. otherwise crazy multithreaded application can close & unlink the fd in the middle of other thread doing mmap on it.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1070432444


##########
sched/init/nx_start.c:
##########
@@ -588,6 +589,12 @@ void nx_start(void)
   nxmsg_initialize();
 #endif
 
+#ifdef CONFIG_FS_SHMFS
+  /* Initialize posix shm support */
+
+  shmfs_initialize();

Review Comment:
   Done, thanks for pointing this out. Also cleaned away now unnecessary header files.



##########
include/sys/syscall_lookup.h:
##########
@@ -333,6 +333,11 @@ SYSCALL_LOOKUP(munmap,                     2)
   SYSCALL_LOOKUP(mq_unlink,                1)
 #endif
 
+#ifdef CONFIG_FS_SHMFS

Review Comment:
   done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] xiaoxiang781216 commented on pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#issuecomment-1383197665

   @jlaitine do you plan to implement 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072035881


##########
fs/shm/shmfs.c:
##########
@@ -0,0 +1,384 @@
+/****************************************************************************
+ * fs/shm/shmfs.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 <assert.h>
+
+#include <nuttx/fs/ioctl.h>
+#include <nuttx/mm/map.h>
+
+#if defined (CONFIG_BUILD_KERNEL)
+#include <nuttx/arch.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
+#endif
+
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep);
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen);
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen);
+static int shmfs_truncate(FAR struct file *filep, off_t length);
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode);
+#endif
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry);
+static int shmfs_munmap(FAR struct task_group_s *group,
+                        FAR struct mm_map_entry_s *entry,
+                        FAR void *start,
+                        size_t length);
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+const struct file_operations shmfs_operations =
+{
+  NULL,             /* open */
+  shmfs_close,      /* close */
+  shmfs_read,       /* read */
+  shmfs_write,      /* write */
+  NULL,             /* seek */
+  NULL,             /* ioctl */
+  shmfs_mmap,       /* mmap */
+  shmfs_truncate,   /* truncate */
+  NULL,             /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  shmfs_unlink      /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: shmfs_read
+ ****************************************************************************/
+
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_write
+ ****************************************************************************/
+
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_release
+ ****************************************************************************/
+
+static int shmfs_release(FAR struct inode *inode)
+{
+  /* If the file has been unlinked previously, delete the contents.
+   * The inode is released after this call, hence checking if i_crefs <= 1.
+   */
+
+  int ret = inode_lock();
+  if (ret >= 0)
+    {
+      if (inode->i_parent == NULL &&
+          inode->i_crefs <= 1)
+        {
+          delete_shm_object(inode->i_private);
+          inode->i_private = NULL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_close
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep)
+{
+  /* Release the shmfs object. The object gets deleted if no-one has
+   * reference to it (either mmap or open file) and the object has been
+   * unlinked
+   */
+
+  return shmfs_release(filep->f_inode);
+}
+
+/****************************************************************************
+ * Name: shmfs_truncate
+ ****************************************************************************/
+
+static int shmfs_truncate(FAR struct file *filep, off_t length)
+{
+  FAR struct shmfs_object_s *object;
+  int ret;
+
+  if (length == 0)
+    {
+      return -EINVAL;
+    }
+
+  ret = inode_lock();

Review Comment:
   reading/writing filep->f_inode->i_private is not atomic; you might even have two truncate calls ongoing at the same time (although not very reasonable application design).
   



##########
fs/shm/shmfs.c:
##########
@@ -0,0 +1,384 @@
+/****************************************************************************
+ * fs/shm/shmfs.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 <assert.h>
+
+#include <nuttx/fs/ioctl.h>
+#include <nuttx/mm/map.h>
+
+#if defined (CONFIG_BUILD_KERNEL)
+#include <nuttx/arch.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
+#endif
+
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep);
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen);
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen);
+static int shmfs_truncate(FAR struct file *filep, off_t length);
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode);
+#endif
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry);
+static int shmfs_munmap(FAR struct task_group_s *group,
+                        FAR struct mm_map_entry_s *entry,
+                        FAR void *start,
+                        size_t length);
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+const struct file_operations shmfs_operations =
+{
+  NULL,             /* open */
+  shmfs_close,      /* close */
+  shmfs_read,       /* read */
+  shmfs_write,      /* write */
+  NULL,             /* seek */
+  NULL,             /* ioctl */
+  shmfs_mmap,       /* mmap */
+  shmfs_truncate,   /* truncate */
+  NULL,             /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  shmfs_unlink      /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: shmfs_read
+ ****************************************************************************/
+
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_write
+ ****************************************************************************/
+
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_release
+ ****************************************************************************/
+
+static int shmfs_release(FAR struct inode *inode)
+{
+  /* If the file has been unlinked previously, delete the contents.
+   * The inode is released after this call, hence checking if i_crefs <= 1.
+   */
+
+  int ret = inode_lock();
+  if (ret >= 0)
+    {
+      if (inode->i_parent == NULL &&
+          inode->i_crefs <= 1)
+        {
+          delete_shm_object(inode->i_private);
+          inode->i_private = NULL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_close
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep)
+{
+  /* Release the shmfs object. The object gets deleted if no-one has
+   * reference to it (either mmap or open file) and the object has been
+   * unlinked
+   */
+
+  return shmfs_release(filep->f_inode);
+}
+
+/****************************************************************************
+ * Name: shmfs_truncate
+ ****************************************************************************/
+
+static int shmfs_truncate(FAR struct file *filep, off_t length)
+{
+  FAR struct shmfs_object_s *object;
+  int ret;
+
+  if (length == 0)
+    {
+      return -EINVAL;
+    }
+
+  ret = inode_lock();
+  if (ret >= 0)
+    {
+      object = filep->f_inode->i_private;
+      if (!object)
+        {
+          filep->f_inode->i_private = alloc_shm_object(length);
+        }
+      else if (object->length != length)
+        {
+          /* This doesn't support resize */
+
+          ret = -EINVAL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_unlink
+ ****************************************************************************/
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode)
+{
+  if (inode->i_crefs <= 1)
+    {
+      delete_shm_object(inode->i_private);
+      inode->i_private = NULL;
+    }
+
+  return OK;
+}
+#endif
+
+/****************************************************************************
+ * Name: shmfs_map_object
+ ****************************************************************************/
+
+static int shmfs_map_object(FAR struct shmfs_object_s *object,
+                            FAR void **vaddr)
+{
+  int ret = OK;
+
+#ifdef CONFIG_BUILD_KERNEL
+  /* Map the physical pages of the shm object with MMU. */
+
+  FAR struct tcb_s        *tcb   = nxsched_self();
+  FAR struct task_group_s *group = tcb->group;
+  FAR uintptr_t *pages =
+    (FAR uintptr_t *)&object->paddr;
+  uintptr_t                mapaddr;
+  unsigned int             npages;
+
+  /* Find a free vaddr space that satisfies length */
+
+  mapaddr = (uintptr_t)vm_alloc_region(get_group_mm(group), 0,
+                                       object->length);
+  if (mapaddr == 0)
+    {
+      return -ENOMEM;
+    }
+
+  /* Convert the region size to pages */
+
+  npages = MM_NPAGES(object->length);
+
+  /* Map the memory to user virtual address space */
+
+  ret = up_shmat(pages, npages, mapaddr);
+  if (ret < 0)
+    {
+      vm_release_region(get_group_mm(group), (FAR void *)mapaddr,
+                        object->length);
+    }
+  else
+    {
+      *vaddr = (FAR void *)mapaddr;
+    }
+#else
+  /* Use the physical address directly */
+
+  *vaddr = object->paddr;
+#endif
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_mmap
+ ****************************************************************************/
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry)
+{
+  FAR struct shmfs_object_s *object;
+  int ret = -EINVAL;
+
+  DEBUGASSERT(filep->f_inode != NULL);
+
+  /* We don't support offset at the moment, just mapping the whole object */
+
+  if (entry->offset != 0)
+    {
+      return ret;
+    }
+
+  /* Keep the inode when mmapped, increase refcount */
+
+  ret = inode_addref(filep->f_inode);
+  if (ret >= 0)
+    {
+      object = (FAR struct shmfs_object_s *)filep->f_inode->i_private;
+
+      if (object)
+        {
+          ret = shmfs_map_object(object, &entry->vaddr);
+        }
+
+      if (!object || ret < 0)
+        {
+          /* Oops, the file has not been truncated yet or
+           * there was error in up_shmat
+           */
+
+          inode_release(filep->f_inode);
+        }
+      else
+        {
+          entry->munmap = shmfs_munmap;
+          entry->priv.p = (FAR void *)filep->f_inode;
+          mm_map_add(entry);
+        }
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_unmap_object
+ ****************************************************************************/
+
+static int shmfs_unmap_area(FAR struct task_group_s *group,
+                            FAR void *vaddr, size_t length)
+{
+  int ret = OK;
+
+#ifdef CONFIG_BUILD_KERNEL
+  unsigned int npages;
+
+  /* Convert the region size to pages */
+
+  if (group)
+    {
+      npages = MM_NPAGES(length);
+
+      /* Unmap the memory from user virtual address space */
+
+      ret = up_shmdt((uintptr_t)vaddr, npages);
+
+      /* Add the virtual memory back to the shared memory pool */
+
+      vm_release_region(get_group_mm(group), vaddr, length);
+    }
+#endif
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_munmap
+ ****************************************************************************/
+
+static int shmfs_munmap(FAR struct task_group_s *group,
+                        FAR struct mm_map_entry_s *entry,
+                        FAR void *start,
+                        size_t length)
+{
+  int                      ret;

Review Comment:
   done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#issuecomment-1385188657

   @xiaoxiang781216 , I walked through your earlier comments. I also fixed the errno return values from shm_open and shm_unlink to conform to posix


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] xiaoxiang781216 commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072105324


##########
fs/shm/shmfs.h:
##########
@@ -0,0 +1,72 @@
+/****************************************************************************
+ * fs/shm/shmfs.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_SHM_SHMFS_H
+#define __FS_SHM_SHMFS_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdlib.h>
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+extern const struct file_operations shmfs_operations;
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+struct shmfs_object_s

Review Comment:
   ok, let's unify the prefix to shmfs_ except shm_open/shm_unlink



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072032103


##########
boards/risc-v/mpfs/icicle/configs/knsh/defconfig:
##########
@@ -68,7 +69,6 @@ CONFIG_LIBM=y
 CONFIG_MEMSET_64BIT=y
 CONFIG_MEMSET_OPTSPEED=y
 CONFIG_MM_PGALLOC=y
-CONFIG_MM_SHM=y

Review Comment:
   I left the removal of that, we just don't need it atm.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] pussuw commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
pussuw commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072513899


##########
fs/shm/shmfs.c:
##########
@@ -0,0 +1,384 @@
+/****************************************************************************
+ * fs/shm/shmfs.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 <assert.h>
+
+#include <nuttx/fs/ioctl.h>
+#include <nuttx/mm/map.h>
+
+#if defined (CONFIG_BUILD_KERNEL)
+#include <nuttx/arch.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
+#endif
+
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep);
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen);
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen);
+static int shmfs_truncate(FAR struct file *filep, off_t length);
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode);
+#endif
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry);
+static int shmfs_munmap(FAR struct task_group_s *group,
+                        FAR struct mm_map_entry_s *entry,
+                        FAR void *start,
+                        size_t length);
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+const struct file_operations shmfs_operations =
+{
+  NULL,             /* open */
+  shmfs_close,      /* close */
+  shmfs_read,       /* read */
+  shmfs_write,      /* write */
+  NULL,             /* seek */
+  NULL,             /* ioctl */
+  shmfs_mmap,       /* mmap */
+  shmfs_truncate,   /* truncate */
+  NULL,             /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  shmfs_unlink      /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: shmfs_read
+ ****************************************************************************/
+
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_write
+ ****************************************************************************/
+
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_release
+ ****************************************************************************/
+
+static int shmfs_release(FAR struct inode *inode)
+{
+  /* If the file has been unlinked previously, delete the contents.
+   * The inode is released after this call, hence checking if i_crefs <= 1.
+   */
+
+  int ret = inode_lock();
+  if (ret >= 0)
+    {
+      if (inode->i_parent == NULL &&
+          inode->i_crefs <= 1)
+        {
+          delete_shm_object(inode->i_private);
+          inode->i_private = NULL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_close
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep)
+{
+  /* Release the shmfs object. The object gets deleted if no-one has
+   * reference to it (either mmap or open file) and the object has been
+   * unlinked
+   */
+
+  return shmfs_release(filep->f_inode);
+}
+
+/****************************************************************************
+ * Name: shmfs_truncate
+ ****************************************************************************/
+
+static int shmfs_truncate(FAR struct file *filep, off_t length)
+{
+  FAR struct shmfs_object_s *object;
+  int ret;
+
+  if (length == 0)
+    {
+      return -EINVAL;
+    }
+
+  ret = inode_lock();
+  if (ret >= 0)
+    {
+      object = filep->f_inode->i_private;
+      if (!object)
+        {
+          filep->f_inode->i_private = alloc_shm_object(length);
+        }
+      else if (object->length != length)
+        {
+          /* This doesn't support resize */
+
+          ret = -EINVAL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_unlink
+ ****************************************************************************/
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode)
+{
+  if (inode->i_crefs <= 1)
+    {
+      delete_shm_object(inode->i_private);
+      inode->i_private = NULL;
+    }
+
+  return OK;
+}
+#endif
+
+/****************************************************************************
+ * Name: shmfs_map_object
+ ****************************************************************************/
+
+static int shmfs_map_object(FAR struct shmfs_object_s *object,
+                            FAR void **vaddr)
+{
+  int ret = OK;
+
+#ifdef CONFIG_BUILD_KERNEL
+  /* Map the physical pages of the shm object with MMU. */
+
+  FAR struct tcb_s        *tcb   = nxsched_self();
+  FAR struct task_group_s *group = tcb->group;
+  FAR uintptr_t *pages =
+    (FAR uintptr_t *)&object->paddr;
+  uintptr_t                mapaddr;
+  unsigned int             npages;
+
+  /* Find a free vaddr space that satisfies length */
+
+  mapaddr = (uintptr_t)vm_alloc_region(get_group_mm(group), 0,
+                                       object->length);
+  if (mapaddr == 0)
+    {
+      return -ENOMEM;
+    }
+
+  /* Convert the region size to pages */
+
+  npages = MM_NPAGES(object->length);
+
+  /* Map the memory to user virtual address space */
+
+  ret = up_shmat(pages, npages, mapaddr);
+  if (ret < 0)
+    {
+      vm_release_region(get_group_mm(group), (FAR void *)mapaddr,
+                        object->length);
+    }
+  else
+    {
+      *vaddr = (FAR void *)mapaddr;
+    }
+#else
+  /* Use the physical address directly */
+
+  *vaddr = object->paddr;
+#endif
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_mmap
+ ****************************************************************************/
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry)
+{
+  FAR struct shmfs_object_s *object;
+  int ret = -EINVAL;
+
+  DEBUGASSERT(filep->f_inode != NULL);
+
+  /* We don't support offset at the moment, just mapping the whole object */
+
+  if (entry->offset != 0)
+    {
+      return ret;
+    }
+
+  /* Keep the inode when mmapped, increase refcount */
+
+  ret = inode_addref(filep->f_inode);
+  if (ret >= 0)
+    {
+      object = (FAR struct shmfs_object_s *)filep->f_inode->i_private;
+
+      if (object)

Review Comment:
   Is it the opened file (descriptor) that keeps the reference ? What happens now when user calls close(fd) ? Does the private data get also destroyed, even if someone has it mapped ?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] pussuw commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
pussuw commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072861357


##########
fs/shm/shmfs.c:
##########
@@ -0,0 +1,384 @@
+/****************************************************************************
+ * fs/shm/shmfs.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 <assert.h>
+
+#include <nuttx/fs/ioctl.h>
+#include <nuttx/mm/map.h>
+
+#if defined (CONFIG_BUILD_KERNEL)
+#include <nuttx/arch.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
+#endif
+
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep);
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen);
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen);
+static int shmfs_truncate(FAR struct file *filep, off_t length);
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode);
+#endif
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry);
+static int shmfs_munmap(FAR struct task_group_s *group,
+                        FAR struct mm_map_entry_s *entry,
+                        FAR void *start,
+                        size_t length);
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+const struct file_operations shmfs_operations =
+{
+  NULL,             /* open */
+  shmfs_close,      /* close */
+  shmfs_read,       /* read */
+  shmfs_write,      /* write */
+  NULL,             /* seek */
+  NULL,             /* ioctl */
+  shmfs_mmap,       /* mmap */
+  shmfs_truncate,   /* truncate */
+  NULL,             /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  shmfs_unlink      /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: shmfs_read
+ ****************************************************************************/
+
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_write
+ ****************************************************************************/
+
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_release
+ ****************************************************************************/
+
+static int shmfs_release(FAR struct inode *inode)
+{
+  /* If the file has been unlinked previously, delete the contents.
+   * The inode is released after this call, hence checking if i_crefs <= 1.
+   */
+
+  int ret = inode_lock();
+  if (ret >= 0)
+    {
+      if (inode->i_parent == NULL &&
+          inode->i_crefs <= 1)
+        {
+          delete_shm_object(inode->i_private);
+          inode->i_private = NULL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_close
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep)
+{
+  /* Release the shmfs object. The object gets deleted if no-one has
+   * reference to it (either mmap or open file) and the object has been
+   * unlinked
+   */
+
+  return shmfs_release(filep->f_inode);
+}
+
+/****************************************************************************
+ * Name: shmfs_truncate
+ ****************************************************************************/
+
+static int shmfs_truncate(FAR struct file *filep, off_t length)
+{
+  FAR struct shmfs_object_s *object;
+  int ret;
+
+  if (length == 0)
+    {
+      return -EINVAL;
+    }
+
+  ret = inode_lock();
+  if (ret >= 0)
+    {
+      object = filep->f_inode->i_private;
+      if (!object)
+        {
+          filep->f_inode->i_private = alloc_shm_object(length);
+        }
+      else if (object->length != length)
+        {
+          /* This doesn't support resize */
+
+          ret = -EINVAL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_unlink
+ ****************************************************************************/
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode)
+{
+  if (inode->i_crefs <= 1)
+    {
+      delete_shm_object(inode->i_private);
+      inode->i_private = NULL;
+    }
+
+  return OK;
+}
+#endif
+
+/****************************************************************************
+ * Name: shmfs_map_object
+ ****************************************************************************/
+
+static int shmfs_map_object(FAR struct shmfs_object_s *object,
+                            FAR void **vaddr)
+{
+  int ret = OK;
+
+#ifdef CONFIG_BUILD_KERNEL
+  /* Map the physical pages of the shm object with MMU. */
+
+  FAR struct tcb_s        *tcb   = nxsched_self();
+  FAR struct task_group_s *group = tcb->group;
+  FAR uintptr_t *pages =
+    (FAR uintptr_t *)&object->paddr;
+  uintptr_t                mapaddr;
+  unsigned int             npages;
+
+  /* Find a free vaddr space that satisfies length */
+
+  mapaddr = (uintptr_t)vm_alloc_region(get_group_mm(group), 0,
+                                       object->length);
+  if (mapaddr == 0)
+    {
+      return -ENOMEM;
+    }
+
+  /* Convert the region size to pages */
+
+  npages = MM_NPAGES(object->length);
+
+  /* Map the memory to user virtual address space */
+
+  ret = up_shmat(pages, npages, mapaddr);
+  if (ret < 0)
+    {
+      vm_release_region(get_group_mm(group), (FAR void *)mapaddr,
+                        object->length);
+    }
+  else
+    {
+      *vaddr = (FAR void *)mapaddr;
+    }
+#else
+  /* Use the physical address directly */
+
+  *vaddr = object->paddr;
+#endif
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_mmap
+ ****************************************************************************/
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry)
+{
+  FAR struct shmfs_object_s *object;
+  int ret = -EINVAL;
+
+  DEBUGASSERT(filep->f_inode != NULL);
+
+  /* We don't support offset at the moment, just mapping the whole object */
+
+  if (entry->offset != 0)
+    {
+      return ret;
+    }
+
+  /* Keep the inode when mmapped, increase refcount */
+
+  ret = inode_addref(filep->f_inode);
+  if (ret >= 0)
+    {
+      object = (FAR struct shmfs_object_s *)filep->f_inode->i_private;
+
+      if (object)

Review Comment:
   I think the following is perfectly legal code:
   ```
   
   int fd = open("myfile", XX);
   void *ptr = mmap(NULL, 1024, PROT_XX, MAP_XX, fd, 0);
   close(fd);
   
   sprintf(ptr, "Foobar!\n",);
   printf("%s" (const char *)ptr);
   
   ```
   etc
   
   So if the open file descriptor is the only one keeping the mapped data alive, the above code will fail.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] xiaoxiang781216 commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1070660030


##########
fs/shm/shm_open.c:
##########
@@ -0,0 +1,163 @@
+/****************************************************************************
+ * fs/shm/shm_open.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 <nuttx/config.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <nuttx/fs/fs.h>
+#include "inode/inode.h"
+#include "shm/shmfs.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+int file_shm_vopen(FAR struct file *shm, FAR const char *shm_name,

Review Comment:
   match the shm_open in sys/mmap.h



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#issuecomment-1383220259

   Most of the comments provided here are just making things look different, not better. Also commented parts are not violating any coding standard, nuttx inviolables nor they are bugs. I'll come back to this PR if I have time again at some point later this week. Right now it seems that now many of the comments are just disrespectful pushing around.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072481800


##########
fs/shm/shm_open.c:
##########
@@ -0,0 +1,181 @@
+/****************************************************************************
+ * fs/shm/shm_open.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 <nuttx/config.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <errno.h>
+
+#include "inode/inode.h"
+#include "shm/shmfs.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int file_shm_open(FAR struct file *shm, FAR const char *name,
+                         int oflags, mode_t mode)
+{
+  FAR struct inode *inode;
+  struct inode_search_s desc;
+  char fullpath[sizeof(CONFIG_FS_SHMFS_VFS_PATH) + CONFIG_NAME_MAX + 2];
+  int ret;
+
+  /* Make sure that a non-NULL name is supplied */
+
+  if (!shm || !name)
+    {
+      return -EINVAL;
+    }
+
+  /* Remove any number of leading '/' */
+
+  while (*name == '/')
+    {
+      name++;
+    }
+
+  /* Empty name supplied? */
+
+  if (*name == '\0')
+    {
+      return -EINVAL;
+    }
+
+  /* Name too long? */
+
+  if (strnlen(name, CONFIG_NAME_MAX) > CONFIG_NAME_MAX)

Review Comment:
   of course not. This is a silly bug, +1 was missing



##########
fs/shm/shm_unlink.c:
##########
@@ -0,0 +1,167 @@
+/****************************************************************************
+ * fs/shm/shm_unlink.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 <stdio.h>
+#include <assert.h>
+#include <errno.h>
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int file_shm_unlink(FAR const char *name)
+{
+  FAR struct inode *inode;
+  struct inode_search_s desc;
+  char fullpath[sizeof(CONFIG_FS_SHMFS_VFS_PATH) + CONFIG_NAME_MAX + 2];
+  int ret;
+
+  /* Make sure that a non-NULL name is supplied */
+
+  if (!name)
+    {
+      return -ENOENT;
+    }
+
+  /* Remove any number of leading '/' */
+
+  while (*name == '/')
+    {
+      name++;
+    }
+
+  /* Empty name supplied? */
+
+  if (*name == '\0')
+    {
+      return -ENOENT;
+    }
+
+  /* Name too long? */
+
+  if (strnlen(name, CONFIG_NAME_MAX) > CONFIG_NAME_MAX)

Review Comment:
   fixe



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072842261


##########
fs/shm/shmfs_alloc.c:
##########
@@ -0,0 +1,143 @@
+/****************************************************************************
+ * fs/shm/shmfs_alloc.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 <stdlib.h>
+#include <nuttx/pgalloc.h>
+
+#include "inode/inode.h"
+#include "shm/shmfs.h"
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+FAR struct shmfs_object_s *shmfs_alloc_object(size_t length)
+{
+  FAR struct shmfs_object_s *object;
+  bool allocated = false;
+
+#if defined(CONFIG_BUILD_FLAT)
+  /* in FLAT build, allocate the object metadata and the data in the same
+   * chunk in kernel heap
+   */
+
+  object = kmm_zalloc(sizeof(struct shmfs_object_s) + length);
+  if (object)
+    {
+      object->paddr = (&object->paddr) + 1;

Review Comment:
   shortest, but not simplest IMHO. The long version is more descriptive, sizeof(shmfs_object_s) was used above in malloc, so it is nicer to read when you just add that many chars in here.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072845379


##########
fs/shm/shm_open.c:
##########
@@ -0,0 +1,181 @@
+/****************************************************************************
+ * fs/shm/shm_open.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 <nuttx/config.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <errno.h>
+
+#include "inode/inode.h"
+#include "shm/shmfs.h"

Review Comment:
   done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#issuecomment-1383217432

   > @jlaitine do you plan to implement SystemV share memory(mm/shm) on top of POSIX share memory(fs/shm)?
   
   No


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] xiaoxiang781216 commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1070814386


##########
fs/shm/shmfs_alloc.c:
##########
@@ -0,0 +1,142 @@
+/****************************************************************************
+ * fs/shm/shmfs_alloc.c

Review Comment:
   Ok.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1070659381


##########
fs/shm/shm_open.c:
##########
@@ -0,0 +1,163 @@
+/****************************************************************************
+ * fs/shm/shm_open.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 <nuttx/config.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <nuttx/fs/fs.h>
+#include "inode/inode.h"
+#include "shm/shmfs.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+int file_shm_vopen(FAR struct file *shm, FAR const char *shm_name,

Review Comment:
   Why?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] xiaoxiang781216 commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1070632394


##########
fs/shm/shm_unlink.c:
##########
@@ -0,0 +1,140 @@
+/****************************************************************************
+ * fs/shm/shm_unlink.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 <stdio.h>
+#include <assert.h>
+#include <errno.h>

Review Comment:
   add blank line



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] pussuw commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
pussuw commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072863660


##########
fs/shm/shmfs.c:
##########
@@ -0,0 +1,377 @@
+/****************************************************************************
+ * fs/shm/shmfs.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 <assert.h>
+
+#include <nuttx/fs/ioctl.h>
+#include <nuttx/mm/map.h>
+
+#if defined (CONFIG_BUILD_KERNEL)
+#include <nuttx/arch.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
+#endif
+
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep);
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen);
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen);
+static int shmfs_truncate(FAR struct file *filep, off_t length);
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode);
+#endif
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry);
+static int shmfs_munmap(FAR struct task_group_s *group,
+                        FAR struct mm_map_entry_s *entry,
+                        FAR void *start,
+                        size_t length);
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+const struct file_operations shmfs_operations =
+{
+  NULL,             /* open */
+  shmfs_close,      /* close */
+  shmfs_read,       /* read */
+  shmfs_write,      /* write */
+  NULL,             /* seek */
+  NULL,             /* ioctl */
+  shmfs_mmap,       /* mmap */
+  shmfs_truncate,   /* truncate */
+  NULL,             /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  shmfs_unlink      /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: shmfs_read
+ ****************************************************************************/
+
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_write
+ ****************************************************************************/
+
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_release
+ ****************************************************************************/
+
+static int shmfs_release(FAR struct inode *inode)
+{
+  /* If the file has been unlinked previously, delete the contents.
+   * The inode is released after this call, hence checking if i_crefs <= 1.
+   */
+
+  int ret = inode_lock();
+  if (ret >= 0)
+    {
+      if (inode->i_parent == NULL &&
+          inode->i_crefs <= 1)
+        {
+          shmfs_free_object(inode->i_private);
+          inode->i_private = NULL;
+          ret = OK;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_close
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep)
+{
+  /* Release the shmfs object. The object gets deleted if no-one has
+   * reference to it (either mmap or open file) and the object has been
+   * unlinked
+   */
+
+  return shmfs_release(filep->f_inode);
+}
+
+/****************************************************************************
+ * Name: shmfs_truncate
+ ****************************************************************************/
+
+static int shmfs_truncate(FAR struct file *filep, off_t length)
+{
+  FAR struct shmfs_object_s *object;
+  int ret;
+
+  if (length == 0)
+    {
+      return -EINVAL;
+    }
+
+  ret = inode_lock();
+  if (ret >= 0)
+    {
+      object = filep->f_inode->i_private;
+      if (!object)
+        {
+          filep->f_inode->i_private = shmfs_alloc_object(length);
+          if (!filep->f_inode->i_private)
+            {
+              ret = -EFAULT;
+            }
+        }
+      else if (object->length != length)
+        {
+          /* This doesn't support resize */
+
+          ret = -EINVAL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_unlink
+ ****************************************************************************/
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode)
+{
+  int ret = inode_lock();
+
+  if (ret >= 0)
+    {
+      if (inode->i_crefs <= 1)
+        {
+          shmfs_free_object(inode->i_private);
+          inode->i_private = NULL;
+          ret = OK;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+#endif
+
+/****************************************************************************
+ * Name: shmfs_map_object
+ ****************************************************************************/
+
+static int shmfs_map_object(FAR struct shmfs_object_s *object,
+                            FAR void **vaddr)
+{
+  int ret = OK;
+
+#ifdef CONFIG_BUILD_KERNEL
+  /* Map the physical pages of the shm object with MMU. */
+
+  FAR struct tcb_s        *tcb   = nxsched_self();
+  FAR struct task_group_s *group = tcb->group;
+  FAR uintptr_t *pages =

Review Comment:
   The alignment was my doing, I prefer it for readability, but it is better to keep a consistent style, and this is the only place that uses the alignment.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] xiaoxiang781216 commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072105555


##########
fs/shm/shmfs.c:
##########
@@ -0,0 +1,384 @@
+/****************************************************************************
+ * fs/shm/shmfs.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 <assert.h>
+
+#include <nuttx/fs/ioctl.h>
+#include <nuttx/mm/map.h>
+
+#if defined (CONFIG_BUILD_KERNEL)
+#include <nuttx/arch.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
+#endif
+
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep);
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen);
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen);
+static int shmfs_truncate(FAR struct file *filep, off_t length);
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode);
+#endif
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry);
+static int shmfs_munmap(FAR struct task_group_s *group,
+                        FAR struct mm_map_entry_s *entry,
+                        FAR void *start,
+                        size_t length);
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+const struct file_operations shmfs_operations =
+{
+  NULL,             /* open */
+  shmfs_close,      /* close */
+  shmfs_read,       /* read */
+  shmfs_write,      /* write */
+  NULL,             /* seek */
+  NULL,             /* ioctl */
+  shmfs_mmap,       /* mmap */
+  shmfs_truncate,   /* truncate */
+  NULL,             /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  shmfs_unlink      /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: shmfs_read
+ ****************************************************************************/
+
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_write
+ ****************************************************************************/
+
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_release
+ ****************************************************************************/
+
+static int shmfs_release(FAR struct inode *inode)
+{
+  /* If the file has been unlinked previously, delete the contents.
+   * The inode is released after this call, hence checking if i_crefs <= 1.
+   */
+
+  int ret = inode_lock();
+  if (ret >= 0)
+    {
+      if (inode->i_parent == NULL &&
+          inode->i_crefs <= 1)
+        {
+          delete_shm_object(inode->i_private);
+          inode->i_private = NULL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_close
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep)
+{
+  /* Release the shmfs object. The object gets deleted if no-one has
+   * reference to it (either mmap or open file) and the object has been
+   * unlinked
+   */
+
+  return shmfs_release(filep->f_inode);
+}
+
+/****************************************************************************
+ * Name: shmfs_truncate
+ ****************************************************************************/
+
+static int shmfs_truncate(FAR struct file *filep, off_t length)
+{
+  FAR struct shmfs_object_s *object;
+  int ret;
+
+  if (length == 0)
+    {
+      return -EINVAL;
+    }
+
+  ret = inode_lock();
+  if (ret >= 0)
+    {
+      object = filep->f_inode->i_private;
+      if (!object)
+        {
+          filep->f_inode->i_private = alloc_shm_object(length);
+        }
+      else if (object->length != length)
+        {
+          /* This doesn't support resize */
+
+          ret = -EINVAL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_unlink
+ ****************************************************************************/
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode)
+{
+  if (inode->i_crefs <= 1)
+    {
+      delete_shm_object(inode->i_private);
+      inode->i_private = NULL;
+    }
+
+  return OK;
+}
+#endif
+
+/****************************************************************************
+ * Name: shmfs_map_object
+ ****************************************************************************/
+
+static int shmfs_map_object(FAR struct shmfs_object_s *object,
+                            FAR void **vaddr)
+{
+  int ret = OK;
+
+#ifdef CONFIG_BUILD_KERNEL
+  /* Map the physical pages of the shm object with MMU. */
+
+  FAR struct tcb_s        *tcb   = nxsched_self();
+  FAR struct task_group_s *group = tcb->group;
+  FAR uintptr_t *pages =
+    (FAR uintptr_t *)&object->paddr;
+  uintptr_t                mapaddr;
+  unsigned int             npages;
+
+  /* Find a free vaddr space that satisfies length */
+
+  mapaddr = (uintptr_t)vm_alloc_region(get_group_mm(group), 0,
+                                       object->length);
+  if (mapaddr == 0)
+    {
+      return -ENOMEM;
+    }
+
+  /* Convert the region size to pages */
+
+  npages = MM_NPAGES(object->length);
+
+  /* Map the memory to user virtual address space */
+
+  ret = up_shmat(pages, npages, mapaddr);
+  if (ret < 0)
+    {
+      vm_release_region(get_group_mm(group), (FAR void *)mapaddr,
+                        object->length);
+    }
+  else
+    {
+      *vaddr = (FAR void *)mapaddr;
+    }
+#else
+  /* Use the physical address directly */
+
+  *vaddr = object->paddr;
+#endif
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_mmap
+ ****************************************************************************/
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry)
+{
+  FAR struct shmfs_object_s *object;
+  int ret = -EINVAL;
+
+  DEBUGASSERT(filep->f_inode != NULL);
+
+  /* We don't support offset at the moment, just mapping the whole object */
+
+  if (entry->offset != 0)
+    {
+      return ret;
+    }
+
+  /* Keep the inode when mmapped, increase refcount */
+
+  ret = inode_addref(filep->f_inode);
+  if (ret >= 0)
+    {
+      object = (FAR struct shmfs_object_s *)filep->f_inode->i_private;
+
+      if (object)
+        {
+          ret = shmfs_map_object(object, &entry->vaddr);
+        }
+
+      if (!object || ret < 0)
+        {
+          /* Oops, the file has not been truncated yet or
+           * there was error in up_shmat
+           */
+
+          inode_release(filep->f_inode);
+        }
+      else
+        {
+          entry->munmap = shmfs_munmap;
+          entry->priv.p = (FAR void *)filep->f_inode;
+          mm_map_add(entry);
+        }
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_unmap_object
+ ****************************************************************************/
+
+static int shmfs_unmap_area(FAR struct task_group_s *group,

Review Comment:
   Ok



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072844834


##########
fs/shm/shm_unlink.c:
##########
@@ -0,0 +1,163 @@
+/****************************************************************************
+ * fs/shm/shm_unlink.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 <stdio.h>
+#include <assert.h>
+#include <errno.h>
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int file_shm_unlink(FAR const char *name)
+{
+  FAR struct inode *inode;
+  struct inode_search_s desc;
+  char fullpath[sizeof(CONFIG_FS_SHMFS_VFS_PATH) + CONFIG_NAME_MAX + 2];
+  int ret;
+
+  /* Make sure that a non-NULL name is supplied */
+
+  if (!name)
+    {
+      return -ENOENT;
+    }
+
+  /* Remove any number of leading '/' */
+
+  while (*name == '/')
+    {
+      name++;
+    }
+
+  /* Empty name supplied? */
+
+  if (*name == '\0')
+    {
+      return -ENOENT;
+    }
+
+  /* Name too long? */
+
+  if (strnlen(name, CONFIG_NAME_MAX + 1) > CONFIG_NAME_MAX)
+    {
+      return -ENAMETOOLONG;
+    }
+
+  /* Get the full path to the shm object */
+
+  snprintf(fullpath, sizeof(fullpath),
+           CONFIG_FS_SHMFS_VFS_PATH "/%s", name);
+
+  /* Get the inode for this shm object */
+
+  SETUP_SEARCH(&desc, fullpath, false);
+
+  ret = inode_lock();
+  if (ret < 0)
+    {
+      goto errout_with_search;
+    }
+
+  ret = inode_find(&desc);
+  if (ret < 0)
+    {
+      /* There is no inode that includes in this path */
+
+      goto errout_with_sem;
+    }
+
+  /* Get the search results */
+
+  inode = desc.node;
+  DEBUGASSERT(inode != NULL);
+
+  /* Verify that what we found is, indeed, an shm inode */
+
+  if (!INODE_IS_SHM(inode))
+    {
+      ret = -ENOENT;
+      goto errout_with_inode;
+    }
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  if (inode->u.i_ops->unlink)
+    {
+      /* Notify the shmfs driver that it has been unlinked */
+
+      ret = inode->u.i_ops->unlink(inode);
+      if (ret < 0)
+        {
+          goto errout_with_inode;
+        }
+    }
+#endif
+
+  /* Remove the old inode from the tree. If we hold a reference count
+   * on the inode, it will not be deleted now.  This will set the
+   * FSNODEFLAG_DELETED bit in the inode flags.
+   */
+
+  ret = inode_remove(fullpath);
+
+  /* inode_remove() should always fail with -EBUSY because we have a
+   * reference on the inode.  -EBUSY means that the inode was, indeed,
+   * unlinked but it could not be freed because there are references.
+   */
+
+  if (ret == -EBUSY)
+    {
+      ret = OK;
+    }
+
+  DEBUGASSERT(ret == OK);
+
+errout_with_inode:
+  inode_release(inode);
+

Review Comment:
   removed, don't understand this either though.



##########
fs/shm/shm_unlink.c:
##########
@@ -0,0 +1,163 @@
+/****************************************************************************
+ * fs/shm/shm_unlink.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 <stdio.h>
+#include <assert.h>
+#include <errno.h>
+#include "shm/shmfs.h"

Review Comment:
   done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] xiaoxiang781216 commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072614810


##########
fs/shm/shmfs_alloc.c:
##########
@@ -0,0 +1,143 @@
+/****************************************************************************
+ * fs/shm/shmfs_alloc.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 <stdlib.h>
+#include <nuttx/pgalloc.h>
+
+#include "inode/inode.h"
+#include "shm/shmfs.h"
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+FAR struct shmfs_object_s *shmfs_alloc_object(size_t length)
+{
+  FAR struct shmfs_object_s *object;
+  bool allocated = false;
+
+#if defined(CONFIG_BUILD_FLAT)
+  /* in FLAT build, allocate the object metadata and the data in the same
+   * chunk in kernel heap
+   */
+
+  object = kmm_zalloc(sizeof(struct shmfs_object_s) + length);
+  if (object)
+    {
+      object->paddr = (&object->paddr) + 1;

Review Comment:
   sorry, I mean:
   object->paddr = object + 1
   which is the simplest approach.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072497862


##########
fs/shm/shmfs_alloc.c:
##########
@@ -0,0 +1,143 @@
+/****************************************************************************
+ * fs/shm/shmfs_alloc.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 <stdlib.h>
+#include <nuttx/pgalloc.h>
+
+#include "inode/inode.h"
+#include "shm/shmfs.h"
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+FAR struct shmfs_object_s *shmfs_alloc_object(size_t length)
+{
+  FAR struct shmfs_object_s *object;
+  bool allocated = false;
+
+#if defined(CONFIG_BUILD_FLAT)
+  /* in FLAT build, allocate the object metadata and the data in the same
+   * chunk in kernel heap
+   */
+
+  object = kmm_zalloc(sizeof(struct shmfs_object_s) + length);
+  if (object)
+    {
+      object->paddr = (&object->paddr) + 1;

Review Comment:
   no, original is correct. in the flat build the actual data is allocated with same malloc call to the end of shmfs object. this takes the address of the last item (paddr) and adds sizeof(void*) to that.
   
   but, I agree that this is confusing. I make it cleaner. maybe
   
    object->paddr = (FAR char *)object + sizeof(struct shmfs_object_s);
   
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#issuecomment-1382691997

   Still fixed one comment and a silly mistake in the previous modification.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072065543


##########
fs/shm/shm_open.c:
##########
@@ -0,0 +1,163 @@
+/****************************************************************************
+ * fs/shm/shm_open.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 <nuttx/config.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <nuttx/fs/fs.h>
+#include "inode/inode.h"
+#include "shm/shmfs.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+int file_shm_vopen(FAR struct file *shm, FAR const char *shm_name,
+                   int oflags, mode_t mode)
+{
+  FAR struct inode *inode;
+  struct inode_search_s desc;
+  char fullpath[sizeof(CONFIG_FS_SHMFS_VFS_PATH) + CONFIG_NAME_MAX + 2];
+  int ret;
+
+  /* Make sure that a non-NULL name is supplied */
+
+  if (!shm || !shm_name || *shm_name == '/' || *shm_name == '\0')
+    {
+      return -EINVAL;
+    }
+
+  /* Get the full path to the shm object */
+
+  snprintf(fullpath, sizeof(fullpath),
+           CONFIG_FS_SHMFS_VFS_PATH "/%s", shm_name);
+
+  /* Get the inode for this shm object */
+
+  SETUP_SEARCH(&desc, fullpath, false);
+
+  ret = inode_lock();
+  if (ret < 0)
+    {
+      goto errout_with_search;
+    }
+
+  ret = inode_find(&desc);
+  if (ret >= 0)
+    {
+      /* Something exists at this path.  Get the search results */
+
+      inode = desc.node;
+
+      /* Verify that the inode is an shm object */
+
+      if (!INODE_IS_SHM(inode))
+        {
+          ret = -EINVAL;
+          inode_release(inode);
+          goto errout_with_sem;
+        }
+
+      /* It exists and is an shm object.  Check if the caller wanted to
+       * create a new object with this name.
+       */
+
+      if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
+        {
+          ret = -EEXIST;
+          inode_release(inode);
+          goto errout_with_sem;
+        }
+    }
+  else
+    {
+      /* The shm does not exist. Were we asked to create it? */
+
+      if ((oflags & O_CREAT) == 0)
+        {
+          /* The shm does not exist and O_CREAT is not set */
+
+          ret = -ENOENT;
+          goto errout_with_sem;
+        }
+
+      /* Create an inode in the pseudo-filesystem at this path */
+
+      ret = inode_reserve(fullpath, mode, &inode);
+
+      if (ret < 0)
+        {
+          goto errout_with_sem;
+        }
+
+      INODE_SET_SHM(inode);
+      inode->u.i_ops    = &shmfs_operations;
+      inode->i_private  = NULL;
+      inode->i_crefs    = 1;
+    }
+
+  /* Associate the inode with a file structure */
+
+  shm->f_oflags  = oflags;

Review Comment:
   done



##########
fs/shm/shm_open.c:
##########
@@ -0,0 +1,163 @@
+/****************************************************************************
+ * fs/shm/shm_open.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 <nuttx/config.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <nuttx/fs/fs.h>
+#include "inode/inode.h"
+#include "shm/shmfs.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+int file_shm_vopen(FAR struct file *shm, FAR const char *shm_name,
+                   int oflags, mode_t mode)
+{
+  FAR struct inode *inode;
+  struct inode_search_s desc;
+  char fullpath[sizeof(CONFIG_FS_SHMFS_VFS_PATH) + CONFIG_NAME_MAX + 2];
+  int ret;
+
+  /* Make sure that a non-NULL name is supplied */
+
+  if (!shm || !shm_name || *shm_name == '/' || *shm_name == '\0')
+    {
+      return -EINVAL;
+    }
+
+  /* Get the full path to the shm object */
+
+  snprintf(fullpath, sizeof(fullpath),
+           CONFIG_FS_SHMFS_VFS_PATH "/%s", shm_name);
+
+  /* Get the inode for this shm object */
+
+  SETUP_SEARCH(&desc, fullpath, false);
+
+  ret = inode_lock();
+  if (ret < 0)
+    {
+      goto errout_with_search;
+    }
+
+  ret = inode_find(&desc);
+  if (ret >= 0)
+    {
+      /* Something exists at this path.  Get the search results */
+
+      inode = desc.node;
+
+      /* Verify that the inode is an shm object */
+
+      if (!INODE_IS_SHM(inode))
+        {
+          ret = -EINVAL;
+          inode_release(inode);
+          goto errout_with_sem;
+        }
+
+      /* It exists and is an shm object.  Check if the caller wanted to
+       * create a new object with this name.
+       */
+
+      if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
+        {
+          ret = -EEXIST;
+          inode_release(inode);
+          goto errout_with_sem;
+        }
+    }
+  else
+    {
+      /* The shm does not exist. Were we asked to create it? */
+
+      if ((oflags & O_CREAT) == 0)
+        {
+          /* The shm does not exist and O_CREAT is not set */
+
+          ret = -ENOENT;
+          goto errout_with_sem;
+        }
+
+      /* Create an inode in the pseudo-filesystem at this path */
+
+      ret = inode_reserve(fullpath, mode, &inode);
+
+      if (ret < 0)
+        {
+          goto errout_with_sem;
+        }
+
+      INODE_SET_SHM(inode);
+      inode->u.i_ops    = &shmfs_operations;
+      inode->i_private  = NULL;
+      inode->i_crefs    = 1;
+    }
+
+  /* Associate the inode with a file structure */
+
+  shm->f_oflags  = oflags;
+  shm->f_pos     = 0;
+  shm->f_inode   = inode;
+  shm->f_priv    = NULL;
+  ret = OK;
+
+errout_with_sem:
+  inode_unlock();
+errout_with_search:
+  RELEASE_SEARCH(&desc);
+

Review Comment:
   done



##########
fs/shm/shm_open.c:
##########
@@ -0,0 +1,163 @@
+/****************************************************************************
+ * fs/shm/shm_open.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 <nuttx/config.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <nuttx/fs/fs.h>

Review Comment:
   done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072852145


##########
fs/shm/shmfs.c:
##########
@@ -0,0 +1,377 @@
+/****************************************************************************
+ * fs/shm/shmfs.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 <assert.h>
+
+#include <nuttx/fs/ioctl.h>
+#include <nuttx/mm/map.h>
+
+#if defined (CONFIG_BUILD_KERNEL)
+#include <nuttx/arch.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
+#endif
+
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep);
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen);
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen);
+static int shmfs_truncate(FAR struct file *filep, off_t length);
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode);
+#endif
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry);
+static int shmfs_munmap(FAR struct task_group_s *group,
+                        FAR struct mm_map_entry_s *entry,
+                        FAR void *start,
+                        size_t length);
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+const struct file_operations shmfs_operations =
+{
+  NULL,             /* open */
+  shmfs_close,      /* close */
+  shmfs_read,       /* read */
+  shmfs_write,      /* write */
+  NULL,             /* seek */
+  NULL,             /* ioctl */
+  shmfs_mmap,       /* mmap */
+  shmfs_truncate,   /* truncate */
+  NULL,             /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  shmfs_unlink      /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: shmfs_read
+ ****************************************************************************/
+
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_write
+ ****************************************************************************/
+
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_release
+ ****************************************************************************/
+
+static int shmfs_release(FAR struct inode *inode)
+{
+  /* If the file has been unlinked previously, delete the contents.
+   * The inode is released after this call, hence checking if i_crefs <= 1.
+   */
+
+  int ret = inode_lock();
+  if (ret >= 0)
+    {
+      if (inode->i_parent == NULL &&
+          inode->i_crefs <= 1)
+        {
+          shmfs_free_object(inode->i_private);
+          inode->i_private = NULL;
+          ret = OK;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_close
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep)
+{
+  /* Release the shmfs object. The object gets deleted if no-one has
+   * reference to it (either mmap or open file) and the object has been
+   * unlinked
+   */
+
+  return shmfs_release(filep->f_inode);
+}
+
+/****************************************************************************
+ * Name: shmfs_truncate
+ ****************************************************************************/
+
+static int shmfs_truncate(FAR struct file *filep, off_t length)
+{
+  FAR struct shmfs_object_s *object;
+  int ret;
+
+  if (length == 0)
+    {
+      return -EINVAL;
+    }
+
+  ret = inode_lock();
+  if (ret >= 0)
+    {
+      object = filep->f_inode->i_private;
+      if (!object)
+        {
+          filep->f_inode->i_private = shmfs_alloc_object(length);
+          if (!filep->f_inode->i_private)
+            {
+              ret = -EFAULT;
+            }
+        }
+      else if (object->length != length)
+        {
+          /* This doesn't support resize */
+
+          ret = -EINVAL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_unlink
+ ****************************************************************************/
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode)
+{
+  int ret = inode_lock();
+
+  if (ret >= 0)
+    {
+      if (inode->i_crefs <= 1)
+        {
+          shmfs_free_object(inode->i_private);
+          inode->i_private = NULL;
+          ret = OK;

Review Comment:
   OK



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072844366


##########
fs/shm/shm_unlink.c:
##########
@@ -0,0 +1,163 @@
+/****************************************************************************
+ * fs/shm/shm_unlink.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 <stdio.h>
+#include <assert.h>
+#include <errno.h>

Review Comment:
   Done, I have no clue why you insist on these blank lines, they make absolutely no difference in code beauty.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072850302


##########
fs/shm/shmfs_alloc.c:
##########
@@ -0,0 +1,144 @@
+/****************************************************************************
+ * fs/shm/shmfs_alloc.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 <stdlib.h>
+#include <stdbool.h>

Review Comment:
   stdbool I didn't remove, there is a "bool" variable in the code. removed stdlib



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1069571383


##########
fs/shm/shmfs_private.h:
##########
@@ -0,0 +1,60 @@
+/****************************************************************************
+ * fs/shm/shmfs_private.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_SHM_SHMFS_PRIVATE_H
+#define __FS_SHM_SHMFS_PRIVATE_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdlib.h>
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+extern const struct file_operations shmfs_operations;
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+struct shmfs_object_s
+{
+  /* Total number of bytes needed from physical memory. */
+
+  size_t length;
+
+  /* Vector of allocations from physical memory.
+   * - In flat and protected builds there is just one pointer to the
+   *   allocated memory in paddr[0].
+   * - In kernel build each address points to one page in page pool
+   *   and the length of the vector is MM_NPAGES(length)
+   */
+
+  FAR void *paddr[];

Review Comment:
   Sure! I'll modify within few days



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1070659291


##########
fs/shm/shmfs_alloc.c:
##########
@@ -0,0 +1,142 @@
+/****************************************************************************
+ * fs/shm/shmfs_alloc.c

Review Comment:
   shm_open and shm_close are named acc. to the standard functions they implement. Everything else is "shmfs", as that is the name of this driver



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#issuecomment-1385369507

   I guess this is now done, I will re-base & push again after the CI is fixed with #8158
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072503741


##########
fs/shm/shmfs.c:
##########
@@ -0,0 +1,380 @@
+/****************************************************************************
+ * fs/shm/shmfs.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 <assert.h>
+
+#include <nuttx/fs/ioctl.h>
+#include <nuttx/mm/map.h>
+
+#if defined (CONFIG_BUILD_KERNEL)
+#include <nuttx/arch.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
+#endif
+
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep);
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen);
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen);
+static int shmfs_truncate(FAR struct file *filep, off_t length);
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode);
+#endif
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry);
+static int shmfs_munmap(FAR struct task_group_s *group,
+                        FAR struct mm_map_entry_s *entry,
+                        FAR void *start,
+                        size_t length);
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+const struct file_operations shmfs_operations =
+{
+  NULL,             /* open */
+  shmfs_close,      /* close */
+  shmfs_read,       /* read */
+  shmfs_write,      /* write */
+  NULL,             /* seek */
+  NULL,             /* ioctl */
+  shmfs_mmap,       /* mmap */
+  shmfs_truncate,   /* truncate */
+  NULL,             /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  shmfs_unlink      /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: shmfs_read
+ ****************************************************************************/
+
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_write
+ ****************************************************************************/
+
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_release
+ ****************************************************************************/
+
+static int shmfs_release(FAR struct inode *inode)
+{
+  /* If the file has been unlinked previously, delete the contents.
+   * The inode is released after this call, hence checking if i_crefs <= 1.
+   */
+
+  int ret = inode_lock();
+  if (ret >= 0)
+    {
+      if (inode->i_parent == NULL &&
+          inode->i_crefs <= 1)
+        {
+          shmfs_free_object(inode->i_private);
+          inode->i_private = NULL;
+          ret = OK;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_close
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep)
+{
+  /* Release the shmfs object. The object gets deleted if no-one has
+   * reference to it (either mmap or open file) and the object has been
+   * unlinked
+   */
+
+  return shmfs_release(filep->f_inode);
+}
+
+/****************************************************************************
+ * Name: shmfs_truncate
+ ****************************************************************************/
+
+static int shmfs_truncate(FAR struct file *filep, off_t length)
+{
+  FAR struct shmfs_object_s *object;
+  int ret;
+
+  if (length == 0)
+    {
+      return -EINVAL;
+    }
+
+  ret = inode_lock();
+  if (ret >= 0)
+    {
+      object = filep->f_inode->i_private;
+      if (!object)
+        {
+          filep->f_inode->i_private = shmfs_alloc_object(length);

Review Comment:
   done, although it is not clear exactly from posix spec what might be the most proper errno value if this fails. I used EFAULT, it would be correct at least in most cases.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] xiaoxiang781216 commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1070815827


##########
fs/shm/shmfs.h:
##########
@@ -0,0 +1,72 @@
+/****************************************************************************
+ * fs/shm/shmfs.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_SHM_SHMFS_H
+#define __FS_SHM_SHMFS_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdlib.h>
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+extern const struct file_operations shmfs_operations;
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+struct shmfs_object_s
+{
+  /* Total number of bytes needed from physical memory. */
+
+  size_t length;
+
+  /* Vector of allocations from physical memory.
+   *
+   * - In flat and protected builds this is a pointer to the
+   *   allocated memory.
+   *
+   * - In kernel build this is start of a malloc'd vector of void pointers
+   *   and the length of the vector is MM_NPAGES(length).
+   */
+
+  FAR void *paddr;
+
+  /* The struct continues here as malloc'd array of void pointers
+   * if CONFIG_BUILD_KERNEL
+   */
+};
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+FAR struct shmfs_object_s *alloc_shm_object(size_t length);

Review Comment:
   let's change to shmfs_alloc_object



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on pull request #8109: Add shmfs

Posted by "jlaitine (via GitHub)" <gi...@apache.org>.
jlaitine commented on PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#issuecomment-1405618172

   Is it a breaking change, when the previous Kconfig option was not implemented and not usable? It was merely a placeholder, no-one could have used it...
   
   Petro Karashchenko kirjoitti torstai 26. tammikuuta 2023:
   > Adding breaking change label since Kconfig option was renamed
   > 
   > -- 
   > Reply to this email directly or view it on GitHub:
   > https://github.com/apache/nuttx/pull/8109#issuecomment-1404293102
   > You are receiving this because you were mentioned.
   > 
   > Message ID: ***@***.***


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] xiaoxiang781216 commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1070627942


##########
fs/shm/shmfs_alloc.c:
##########
@@ -0,0 +1,142 @@
+/****************************************************************************
+ * fs/shm/shmfs_alloc.c

Review Comment:
   let's use the same prefix shm_ for all files.



##########
boards/risc-v/mpfs/icicle/configs/knsh/defconfig:
##########
@@ -68,7 +69,6 @@ CONFIG_LIBM=y
 CONFIG_MEMSET_64BIT=y
 CONFIG_MEMSET_OPTSPEED=y
 CONFIG_MM_PGALLOC=y
-CONFIG_MM_SHM=y

Review Comment:
   let's keep the SystemV API too?



##########
fs/shm/shm_open.c:
##########
@@ -0,0 +1,163 @@
+/****************************************************************************
+ * fs/shm/shm_open.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 <nuttx/config.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <nuttx/fs/fs.h>
+#include "inode/inode.h"
+#include "shm/shmfs.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+int file_shm_vopen(FAR struct file *shm, FAR const char *shm_name,

Review Comment:
   file_shm_vopen->file_shm_open
   let's change it to public function and add prototype to include/nuttx/fs/fs.h



##########
fs/shm/shm_open.c:
##########
@@ -0,0 +1,163 @@
+/****************************************************************************
+ * fs/shm/shm_open.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 <nuttx/config.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <nuttx/fs/fs.h>
+#include "inode/inode.h"
+#include "shm/shmfs.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+int file_shm_vopen(FAR struct file *shm, FAR const char *shm_name,
+                   int oflags, mode_t mode)
+{
+  FAR struct inode *inode;
+  struct inode_search_s desc;
+  char fullpath[sizeof(CONFIG_FS_SHMFS_VFS_PATH) + CONFIG_NAME_MAX + 2];
+  int ret;
+
+  /* Make sure that a non-NULL name is supplied */
+
+  if (!shm || !shm_name || *shm_name == '/' || *shm_name == '\0')
+    {
+      return -EINVAL;
+    }
+
+  /* Get the full path to the shm object */
+
+  snprintf(fullpath, sizeof(fullpath),
+           CONFIG_FS_SHMFS_VFS_PATH "/%s", shm_name);
+
+  /* Get the inode for this shm object */
+
+  SETUP_SEARCH(&desc, fullpath, false);
+
+  ret = inode_lock();
+  if (ret < 0)
+    {
+      goto errout_with_search;
+    }
+
+  ret = inode_find(&desc);
+  if (ret >= 0)
+    {
+      /* Something exists at this path.  Get the search results */
+
+      inode = desc.node;
+
+      /* Verify that the inode is an shm object */
+
+      if (!INODE_IS_SHM(inode))
+        {
+          ret = -EINVAL;
+          inode_release(inode);
+          goto errout_with_sem;
+        }
+
+      /* It exists and is an shm object.  Check if the caller wanted to
+       * create a new object with this name.
+       */
+
+      if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
+        {
+          ret = -EEXIST;
+          inode_release(inode);
+          goto errout_with_sem;
+        }
+    }
+  else
+    {
+      /* The shm does not exist. Were we asked to create it? */
+
+      if ((oflags & O_CREAT) == 0)
+        {
+          /* The shm does not exist and O_CREAT is not set */
+
+          ret = -ENOENT;
+          goto errout_with_sem;
+        }
+
+      /* Create an inode in the pseudo-filesystem at this path */
+
+      ret = inode_reserve(fullpath, mode, &inode);
+
+      if (ret < 0)
+        {
+          goto errout_with_sem;
+        }
+
+      INODE_SET_SHM(inode);
+      inode->u.i_ops    = &shmfs_operations;
+      inode->i_private  = NULL;

Review Comment:
   remove the extra space before =



##########
fs/shm/shm_open.c:
##########
@@ -0,0 +1,163 @@
+/****************************************************************************
+ * fs/shm/shm_open.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 <nuttx/config.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <nuttx/fs/fs.h>
+#include "inode/inode.h"
+#include "shm/shmfs.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+int file_shm_vopen(FAR struct file *shm, FAR const char *shm_name,
+                   int oflags, mode_t mode)
+{
+  FAR struct inode *inode;
+  struct inode_search_s desc;
+  char fullpath[sizeof(CONFIG_FS_SHMFS_VFS_PATH) + CONFIG_NAME_MAX + 2];
+  int ret;
+
+  /* Make sure that a non-NULL name is supplied */
+
+  if (!shm || !shm_name || *shm_name == '/' || *shm_name == '\0')
+    {
+      return -EINVAL;
+    }
+
+  /* Get the full path to the shm object */
+
+  snprintf(fullpath, sizeof(fullpath),
+           CONFIG_FS_SHMFS_VFS_PATH "/%s", shm_name);
+
+  /* Get the inode for this shm object */
+
+  SETUP_SEARCH(&desc, fullpath, false);
+
+  ret = inode_lock();
+  if (ret < 0)
+    {
+      goto errout_with_search;
+    }
+
+  ret = inode_find(&desc);
+  if (ret >= 0)
+    {
+      /* Something exists at this path.  Get the search results */
+
+      inode = desc.node;
+
+      /* Verify that the inode is an shm object */
+
+      if (!INODE_IS_SHM(inode))
+        {
+          ret = -EINVAL;
+          inode_release(inode);
+          goto errout_with_sem;
+        }
+
+      /* It exists and is an shm object.  Check if the caller wanted to
+       * create a new object with this name.
+       */
+
+      if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
+        {
+          ret = -EEXIST;
+          inode_release(inode);
+          goto errout_with_sem;
+        }
+    }
+  else
+    {
+      /* The shm does not exist. Were we asked to create it? */
+
+      if ((oflags & O_CREAT) == 0)
+        {
+          /* The shm does not exist and O_CREAT is not set */
+
+          ret = -ENOENT;
+          goto errout_with_sem;
+        }
+
+      /* Create an inode in the pseudo-filesystem at this path */
+
+      ret = inode_reserve(fullpath, mode, &inode);
+
+      if (ret < 0)
+        {
+          goto errout_with_sem;
+        }
+
+      INODE_SET_SHM(inode);
+      inode->u.i_ops    = &shmfs_operations;
+      inode->i_private  = NULL;
+      inode->i_crefs    = 1;
+    }
+
+  /* Associate the inode with a file structure */
+
+  shm->f_oflags  = oflags;
+  shm->f_pos     = 0;
+  shm->f_inode   = inode;
+  shm->f_priv    = NULL;
+  ret = OK;

Review Comment:
   remove, don't need



##########
fs/shm/shm_open.c:
##########
@@ -0,0 +1,163 @@
+/****************************************************************************
+ * fs/shm/shm_open.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 <nuttx/config.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <nuttx/fs/fs.h>
+#include "inode/inode.h"
+#include "shm/shmfs.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+int file_shm_vopen(FAR struct file *shm, FAR const char *shm_name,
+                   int oflags, mode_t mode)
+{
+  FAR struct inode *inode;
+  struct inode_search_s desc;
+  char fullpath[sizeof(CONFIG_FS_SHMFS_VFS_PATH) + CONFIG_NAME_MAX + 2];
+  int ret;
+
+  /* Make sure that a non-NULL name is supplied */
+
+  if (!shm || !shm_name || *shm_name == '/' || *shm_name == '\0')
+    {
+      return -EINVAL;
+    }
+
+  /* Get the full path to the shm object */
+
+  snprintf(fullpath, sizeof(fullpath),
+           CONFIG_FS_SHMFS_VFS_PATH "/%s", shm_name);
+
+  /* Get the inode for this shm object */
+
+  SETUP_SEARCH(&desc, fullpath, false);
+
+  ret = inode_lock();
+  if (ret < 0)
+    {
+      goto errout_with_search;
+    }
+
+  ret = inode_find(&desc);
+  if (ret >= 0)
+    {
+      /* Something exists at this path.  Get the search results */
+
+      inode = desc.node;
+
+      /* Verify that the inode is an shm object */
+
+      if (!INODE_IS_SHM(inode))
+        {
+          ret = -EINVAL;
+          inode_release(inode);
+          goto errout_with_sem;
+        }
+
+      /* It exists and is an shm object.  Check if the caller wanted to
+       * create a new object with this name.
+       */
+
+      if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
+        {
+          ret = -EEXIST;
+          inode_release(inode);
+          goto errout_with_sem;
+        }
+    }
+  else
+    {
+      /* The shm does not exist. Were we asked to create it? */
+
+      if ((oflags & O_CREAT) == 0)
+        {
+          /* The shm does not exist and O_CREAT is not set */
+
+          ret = -ENOENT;
+          goto errout_with_sem;
+        }
+
+      /* Create an inode in the pseudo-filesystem at this path */
+
+      ret = inode_reserve(fullpath, mode, &inode);
+

Review Comment:
   remove the blank line



##########
fs/shm/shm_open.c:
##########
@@ -0,0 +1,163 @@
+/****************************************************************************
+ * fs/shm/shm_open.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 <nuttx/config.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <nuttx/fs/fs.h>
+#include "inode/inode.h"
+#include "shm/shmfs.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+int file_shm_vopen(FAR struct file *shm, FAR const char *shm_name,
+                   int oflags, mode_t mode)
+{
+  FAR struct inode *inode;
+  struct inode_search_s desc;
+  char fullpath[sizeof(CONFIG_FS_SHMFS_VFS_PATH) + CONFIG_NAME_MAX + 2];
+  int ret;
+
+  /* Make sure that a non-NULL name is supplied */
+
+  if (!shm || !shm_name || *shm_name == '/' || *shm_name == '\0')

Review Comment:
   remove *shm_name == '/'?



##########
fs/shm/shm_unlink.c:
##########
@@ -0,0 +1,140 @@
+/****************************************************************************
+ * fs/shm/shm_unlink.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 <stdio.h>
+#include <assert.h>
+#include <errno.h>

Review Comment:
   add blank line



##########
fs/shm/shm_open.c:
##########
@@ -0,0 +1,163 @@
+/****************************************************************************
+ * fs/shm/shm_open.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 <nuttx/config.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <nuttx/fs/fs.h>

Review Comment:
   add blank line and remove the unused header files



##########
fs/shm/shm_unlink.c:
##########
@@ -0,0 +1,140 @@
+/****************************************************************************
+ * fs/shm/shm_unlink.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 <stdio.h>
+#include <assert.h>
+#include <errno.h>
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int file_shm_unlink(FAR const char *shm_name)
+{
+  FAR struct inode *inode;
+  struct inode_search_s desc;
+  char fullpath[sizeof(CONFIG_FS_SHMFS_VFS_PATH) + CONFIG_NAME_MAX + 2];
+  int ret;
+
+  /* Get the full path to the shm object */
+
+  snprintf(fullpath, sizeof(fullpath),
+           CONFIG_FS_SHMFS_VFS_PATH "/%s", shm_name);
+
+  /* Get the inode for this shm object */
+
+  SETUP_SEARCH(&desc, fullpath, false);
+
+  ret = inode_lock();
+  if (ret < 0)
+    {
+      goto errout_with_search;
+    }
+
+  ret = inode_find(&desc);
+  if (ret < 0)
+    {
+      /* There is no inode that includes in this path */
+
+      goto errout_with_sem;
+    }
+
+  /* Get the search results */
+
+  inode = desc.node;
+  DEBUGASSERT(inode != NULL);
+
+  /* Verify that what we found is, indeed, an shm inode */
+
+  if (!INODE_IS_SHM(inode))
+    {
+      ret = -ENOENT;
+      inode_release(inode);
+      goto errout_with_sem;
+    }
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  if (inode->u.i_ops->unlink)
+    {
+      /* Notify the shmfs driver that it has been unlinked */
+
+      ret = inode->u.i_ops->unlink(inode);
+      if (ret < 0)
+        {
+          goto errout_with_inode;
+        }
+    }
+#endif
+
+  /* Remove the old inode from the tree. If we hold a reference count
+   * on the inode, it will not be deleted now.  This will set the
+   * FSNODEFLAG_DELETED bit in the inode flags.
+   */
+
+  inode_release(inode);
+  ret = inode_remove(fullpath);
+
+  /* inode_remove() should always fail with -EBUSY because we have a
+   * reference on the inode.  -EBUSY means that the inode was, indeed,
+   * unlinked but it could not be freed because there are references.
+   */
+
+  if (ret == -EBUSY)
+    {
+      ret = OK;
+    }
+
+  DEBUGASSERT(ret == OK);
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+errout_with_inode:
+  inode_release(inode);
+#endif
+
+errout_with_sem:
+  inode_unlock();
+
+errout_with_search:
+  RELEASE_SEARCH(&desc);
+
+  return ret;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+int shm_unlink(FAR const char *shm_name)
+{
+  int ret = file_shm_unlink(shm_name);
+
+  if (ret < 0)
+    {
+      set_errno(ENOENT);

Review Comment:
   set_errno(-ret);
   ret = ERROR;



##########
fs/shm/shm_unlink.c:
##########
@@ -0,0 +1,140 @@
+/****************************************************************************
+ * fs/shm/shm_unlink.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 <stdio.h>
+#include <assert.h>
+#include <errno.h>
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int file_shm_unlink(FAR const char *shm_name)

Review Comment:
   change to public function and add the prototype to include/nuttx/fs/fs.h



##########
fs/shm/shm_open.c:
##########
@@ -0,0 +1,163 @@
+/****************************************************************************
+ * fs/shm/shm_open.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 <nuttx/config.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <nuttx/fs/fs.h>
+#include "inode/inode.h"
+#include "shm/shmfs.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+int file_shm_vopen(FAR struct file *shm, FAR const char *shm_name,
+                   int oflags, mode_t mode)
+{
+  FAR struct inode *inode;
+  struct inode_search_s desc;
+  char fullpath[sizeof(CONFIG_FS_SHMFS_VFS_PATH) + CONFIG_NAME_MAX + 2];
+  int ret;
+
+  /* Make sure that a non-NULL name is supplied */
+
+  if (!shm || !shm_name || *shm_name == '/' || *shm_name == '\0')
+    {
+      return -EINVAL;
+    }
+
+  /* Get the full path to the shm object */
+
+  snprintf(fullpath, sizeof(fullpath),
+           CONFIG_FS_SHMFS_VFS_PATH "/%s", shm_name);
+
+  /* Get the inode for this shm object */
+
+  SETUP_SEARCH(&desc, fullpath, false);
+
+  ret = inode_lock();
+  if (ret < 0)
+    {
+      goto errout_with_search;
+    }
+
+  ret = inode_find(&desc);
+  if (ret >= 0)
+    {
+      /* Something exists at this path.  Get the search results */
+
+      inode = desc.node;
+
+      /* Verify that the inode is an shm object */
+
+      if (!INODE_IS_SHM(inode))
+        {
+          ret = -EINVAL;
+          inode_release(inode);
+          goto errout_with_sem;
+        }
+
+      /* It exists and is an shm object.  Check if the caller wanted to
+       * create a new object with this name.
+       */
+
+      if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
+        {
+          ret = -EEXIST;
+          inode_release(inode);
+          goto errout_with_sem;
+        }
+    }
+  else
+    {
+      /* The shm does not exist. Were we asked to create it? */
+
+      if ((oflags & O_CREAT) == 0)
+        {
+          /* The shm does not exist and O_CREAT is not set */
+
+          ret = -ENOENT;
+          goto errout_with_sem;
+        }
+
+      /* Create an inode in the pseudo-filesystem at this path */
+
+      ret = inode_reserve(fullpath, mode, &inode);
+
+      if (ret < 0)
+        {
+          goto errout_with_sem;
+        }
+
+      INODE_SET_SHM(inode);
+      inode->u.i_ops    = &shmfs_operations;
+      inode->i_private  = NULL;
+      inode->i_crefs    = 1;
+    }
+
+  /* Associate the inode with a file structure */
+
+  shm->f_oflags  = oflags;
+  shm->f_pos     = 0;
+  shm->f_inode   = inode;
+  shm->f_priv    = NULL;
+  ret = OK;
+
+errout_with_sem:
+  inode_unlock();
+errout_with_search:
+  RELEASE_SEARCH(&desc);
+

Review Comment:
   remove the blank line



##########
fs/shm/shm_open.c:
##########
@@ -0,0 +1,163 @@
+/****************************************************************************
+ * fs/shm/shm_open.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 <nuttx/config.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <nuttx/fs/fs.h>
+#include "inode/inode.h"
+#include "shm/shmfs.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+int file_shm_vopen(FAR struct file *shm, FAR const char *shm_name,
+                   int oflags, mode_t mode)
+{
+  FAR struct inode *inode;
+  struct inode_search_s desc;
+  char fullpath[sizeof(CONFIG_FS_SHMFS_VFS_PATH) + CONFIG_NAME_MAX + 2];
+  int ret;
+
+  /* Make sure that a non-NULL name is supplied */
+
+  if (!shm || !shm_name || *shm_name == '/' || *shm_name == '\0')
+    {
+      return -EINVAL;
+    }
+
+  /* Get the full path to the shm object */
+
+  snprintf(fullpath, sizeof(fullpath),
+           CONFIG_FS_SHMFS_VFS_PATH "/%s", shm_name);
+
+  /* Get the inode for this shm object */
+
+  SETUP_SEARCH(&desc, fullpath, false);
+
+  ret = inode_lock();
+  if (ret < 0)
+    {
+      goto errout_with_search;
+    }
+
+  ret = inode_find(&desc);
+  if (ret >= 0)
+    {
+      /* Something exists at this path.  Get the search results */
+
+      inode = desc.node;
+
+      /* Verify that the inode is an shm object */
+
+      if (!INODE_IS_SHM(inode))
+        {
+          ret = -EINVAL;
+          inode_release(inode);
+          goto errout_with_sem;
+        }
+
+      /* It exists and is an shm object.  Check if the caller wanted to
+       * create a new object with this name.
+       */
+
+      if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
+        {
+          ret = -EEXIST;
+          inode_release(inode);
+          goto errout_with_sem;
+        }
+    }
+  else
+    {
+      /* The shm does not exist. Were we asked to create it? */
+
+      if ((oflags & O_CREAT) == 0)
+        {
+          /* The shm does not exist and O_CREAT is not set */
+
+          ret = -ENOENT;
+          goto errout_with_sem;
+        }
+
+      /* Create an inode in the pseudo-filesystem at this path */
+
+      ret = inode_reserve(fullpath, mode, &inode);
+
+      if (ret < 0)
+        {
+          goto errout_with_sem;
+        }
+
+      INODE_SET_SHM(inode);
+      inode->u.i_ops    = &shmfs_operations;
+      inode->i_private  = NULL;
+      inode->i_crefs    = 1;
+    }
+
+  /* Associate the inode with a file structure */
+
+  shm->f_oflags  = oflags;

Review Comment:
   remove the extra space before = 



##########
fs/shm/shm_unlink.c:
##########
@@ -0,0 +1,140 @@
+/****************************************************************************
+ * fs/shm/shm_unlink.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 <stdio.h>
+#include <assert.h>
+#include <errno.h>
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int file_shm_unlink(FAR const char *shm_name)
+{
+  FAR struct inode *inode;
+  struct inode_search_s desc;
+  char fullpath[sizeof(CONFIG_FS_SHMFS_VFS_PATH) + CONFIG_NAME_MAX + 2];
+  int ret;
+
+  /* Get the full path to the shm object */
+
+  snprintf(fullpath, sizeof(fullpath),
+           CONFIG_FS_SHMFS_VFS_PATH "/%s", shm_name);
+
+  /* Get the inode for this shm object */
+
+  SETUP_SEARCH(&desc, fullpath, false);
+
+  ret = inode_lock();
+  if (ret < 0)
+    {
+      goto errout_with_search;
+    }
+
+  ret = inode_find(&desc);
+  if (ret < 0)
+    {
+      /* There is no inode that includes in this path */
+
+      goto errout_with_sem;
+    }
+
+  /* Get the search results */
+
+  inode = desc.node;
+  DEBUGASSERT(inode != NULL);
+
+  /* Verify that what we found is, indeed, an shm inode */
+
+  if (!INODE_IS_SHM(inode))
+    {
+      ret = -ENOENT;
+      inode_release(inode);
+      goto errout_with_sem;
+    }
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  if (inode->u.i_ops->unlink)
+    {
+      /* Notify the shmfs driver that it has been unlinked */
+
+      ret = inode->u.i_ops->unlink(inode);
+      if (ret < 0)
+        {
+          goto errout_with_inode;
+        }
+    }
+#endif
+
+  /* Remove the old inode from the tree. If we hold a reference count
+   * on the inode, it will not be deleted now.  This will set the
+   * FSNODEFLAG_DELETED bit in the inode flags.
+   */
+
+  inode_release(inode);
+  ret = inode_remove(fullpath);
+
+  /* inode_remove() should always fail with -EBUSY because we have a
+   * reference on the inode.  -EBUSY means that the inode was, indeed,
+   * unlinked but it could not be freed because there are references.
+   */
+
+  if (ret == -EBUSY)
+    {
+      ret = OK;
+    }
+
+  DEBUGASSERT(ret == OK);
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+errout_with_inode:
+  inode_release(inode);
+#endif
+
+errout_with_sem:
+  inode_unlock();
+
+errout_with_search:
+  RELEASE_SEARCH(&desc);
+
+  return ret;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+int shm_unlink(FAR const char *shm_name)

Review Comment:
   all shm_name to name to match the prototype in sys/mmap.h



##########
fs/shm/shm_unlink.c:
##########
@@ -0,0 +1,140 @@
+/****************************************************************************
+ * fs/shm/shm_unlink.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 <stdio.h>
+#include <assert.h>
+#include <errno.h>
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int file_shm_unlink(FAR const char *shm_name)
+{
+  FAR struct inode *inode;
+  struct inode_search_s desc;
+  char fullpath[sizeof(CONFIG_FS_SHMFS_VFS_PATH) + CONFIG_NAME_MAX + 2];
+  int ret;
+
+  /* Get the full path to the shm object */
+
+  snprintf(fullpath, sizeof(fullpath),
+           CONFIG_FS_SHMFS_VFS_PATH "/%s", shm_name);
+
+  /* Get the inode for this shm object */
+
+  SETUP_SEARCH(&desc, fullpath, false);
+
+  ret = inode_lock();
+  if (ret < 0)
+    {
+      goto errout_with_search;
+    }
+
+  ret = inode_find(&desc);
+  if (ret < 0)
+    {
+      /* There is no inode that includes in this path */
+
+      goto errout_with_sem;
+    }
+
+  /* Get the search results */
+
+  inode = desc.node;
+  DEBUGASSERT(inode != NULL);
+
+  /* Verify that what we found is, indeed, an shm inode */
+
+  if (!INODE_IS_SHM(inode))
+    {
+      ret = -ENOENT;
+      inode_release(inode);
+      goto errout_with_sem;
+    }
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  if (inode->u.i_ops->unlink)
+    {
+      /* Notify the shmfs driver that it has been unlinked */
+
+      ret = inode->u.i_ops->unlink(inode);
+      if (ret < 0)
+        {
+          goto errout_with_inode;
+        }
+    }
+#endif
+
+  /* Remove the old inode from the tree. If we hold a reference count
+   * on the inode, it will not be deleted now.  This will set the
+   * FSNODEFLAG_DELETED bit in the inode flags.
+   */
+
+  inode_release(inode);
+  ret = inode_remove(fullpath);
+
+  /* inode_remove() should always fail with -EBUSY because we have a
+   * reference on the inode.  -EBUSY means that the inode was, indeed,
+   * unlinked but it could not be freed because there are references.
+   */
+
+  if (ret == -EBUSY)
+    {
+      ret = OK;
+    }
+
+  DEBUGASSERT(ret == OK);
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+errout_with_inode:
+  inode_release(inode);
+#endif
+
+errout_with_sem:
+  inode_unlock();
+
+errout_with_search:
+  RELEASE_SEARCH(&desc);
+

Review Comment:
   remove the blank line



##########
fs/shm/shm_unlink.c:
##########
@@ -0,0 +1,140 @@
+/****************************************************************************
+ * fs/shm/shm_unlink.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 <stdio.h>
+#include <assert.h>
+#include <errno.h>
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int file_shm_unlink(FAR const char *shm_name)
+{
+  FAR struct inode *inode;
+  struct inode_search_s desc;
+  char fullpath[sizeof(CONFIG_FS_SHMFS_VFS_PATH) + CONFIG_NAME_MAX + 2];
+  int ret;
+
+  /* Get the full path to the shm object */
+
+  snprintf(fullpath, sizeof(fullpath),
+           CONFIG_FS_SHMFS_VFS_PATH "/%s", shm_name);
+
+  /* Get the inode for this shm object */
+
+  SETUP_SEARCH(&desc, fullpath, false);
+
+  ret = inode_lock();
+  if (ret < 0)
+    {
+      goto errout_with_search;
+    }
+
+  ret = inode_find(&desc);
+  if (ret < 0)
+    {
+      /* There is no inode that includes in this path */
+
+      goto errout_with_sem;
+    }
+
+  /* Get the search results */
+
+  inode = desc.node;
+  DEBUGASSERT(inode != NULL);
+
+  /* Verify that what we found is, indeed, an shm inode */
+
+  if (!INODE_IS_SHM(inode))
+    {
+      ret = -ENOENT;

Review Comment:
   -EINVAL



##########
fs/shm/shmfs_alloc.c:
##########
@@ -0,0 +1,142 @@
+/****************************************************************************
+ * fs/shm/shmfs_alloc.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 <stdlib.h>
+#include <nuttx/pgalloc.h>

Review Comment:
   add blank line



##########
fs/shm/shmfs.c:
##########
@@ -0,0 +1,384 @@
+/****************************************************************************
+ * fs/shm/shmfs.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 <assert.h>
+
+#include <nuttx/fs/ioctl.h>
+#include <nuttx/mm/map.h>
+
+#if defined (CONFIG_BUILD_KERNEL)
+#include <nuttx/arch.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
+#endif
+
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep);

Review Comment:
   let's unify the prefix from shmfs_ to shm_ and rename shmfs.[h|c] to shm.[h|c].



##########
fs/shm/shmfs.c:
##########
@@ -0,0 +1,384 @@
+/****************************************************************************
+ * fs/shm/shmfs.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 <assert.h>
+
+#include <nuttx/fs/ioctl.h>
+#include <nuttx/mm/map.h>
+
+#if defined (CONFIG_BUILD_KERNEL)
+#include <nuttx/arch.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
+#endif
+
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep);
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen);
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen);
+static int shmfs_truncate(FAR struct file *filep, off_t length);
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode);
+#endif
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry);
+static int shmfs_munmap(FAR struct task_group_s *group,
+                        FAR struct mm_map_entry_s *entry,
+                        FAR void *start,
+                        size_t length);
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+const struct file_operations shmfs_operations =
+{
+  NULL,             /* open */
+  shmfs_close,      /* close */
+  shmfs_read,       /* read */
+  shmfs_write,      /* write */
+  NULL,             /* seek */
+  NULL,             /* ioctl */
+  shmfs_mmap,       /* mmap */
+  shmfs_truncate,   /* truncate */
+  NULL,             /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  shmfs_unlink      /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: shmfs_read
+ ****************************************************************************/
+
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_write
+ ****************************************************************************/
+
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_release
+ ****************************************************************************/
+
+static int shmfs_release(FAR struct inode *inode)
+{
+  /* If the file has been unlinked previously, delete the contents.
+   * The inode is released after this call, hence checking if i_crefs <= 1.
+   */
+
+  int ret = inode_lock();
+  if (ret >= 0)
+    {
+      if (inode->i_parent == NULL &&
+          inode->i_crefs <= 1)
+        {
+          delete_shm_object(inode->i_private);
+          inode->i_private = NULL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_close
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep)
+{
+  /* Release the shmfs object. The object gets deleted if no-one has
+   * reference to it (either mmap or open file) and the object has been
+   * unlinked
+   */
+
+  return shmfs_release(filep->f_inode);
+}
+
+/****************************************************************************
+ * Name: shmfs_truncate
+ ****************************************************************************/
+
+static int shmfs_truncate(FAR struct file *filep, off_t length)
+{
+  FAR struct shmfs_object_s *object;
+  int ret;
+
+  if (length == 0)
+    {
+      return -EINVAL;
+    }
+
+  ret = inode_lock();
+  if (ret >= 0)
+    {
+      object = filep->f_inode->i_private;
+      if (!object)
+        {
+          filep->f_inode->i_private = alloc_shm_object(length);
+        }
+      else if (object->length != length)
+        {
+          /* This doesn't support resize */
+
+          ret = -EINVAL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_unlink
+ ****************************************************************************/
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode)
+{
+  if (inode->i_crefs <= 1)
+    {
+      delete_shm_object(inode->i_private);
+      inode->i_private = NULL;
+    }
+
+  return OK;
+}
+#endif
+
+/****************************************************************************
+ * Name: shmfs_map_object
+ ****************************************************************************/
+
+static int shmfs_map_object(FAR struct shmfs_object_s *object,
+                            FAR void **vaddr)
+{
+  int ret = OK;
+
+#ifdef CONFIG_BUILD_KERNEL
+  /* Map the physical pages of the shm object with MMU. */
+
+  FAR struct tcb_s        *tcb   = nxsched_self();
+  FAR struct task_group_s *group = tcb->group;
+  FAR uintptr_t *pages =
+    (FAR uintptr_t *)&object->paddr;
+  uintptr_t                mapaddr;
+  unsigned int             npages;
+
+  /* Find a free vaddr space that satisfies length */
+
+  mapaddr = (uintptr_t)vm_alloc_region(get_group_mm(group), 0,
+                                       object->length);
+  if (mapaddr == 0)
+    {
+      return -ENOMEM;
+    }
+
+  /* Convert the region size to pages */
+
+  npages = MM_NPAGES(object->length);
+
+  /* Map the memory to user virtual address space */
+
+  ret = up_shmat(pages, npages, mapaddr);
+  if (ret < 0)
+    {
+      vm_release_region(get_group_mm(group), (FAR void *)mapaddr,
+                        object->length);
+    }
+  else
+    {
+      *vaddr = (FAR void *)mapaddr;
+    }
+#else
+  /* Use the physical address directly */
+
+  *vaddr = object->paddr;
+#endif
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_mmap
+ ****************************************************************************/
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry)
+{
+  FAR struct shmfs_object_s *object;
+  int ret = -EINVAL;
+
+  DEBUGASSERT(filep->f_inode != NULL);
+
+  /* We don't support offset at the moment, just mapping the whole object */
+
+  if (entry->offset != 0)
+    {
+      return ret;
+    }
+
+  /* Keep the inode when mmapped, increase refcount */
+
+  ret = inode_addref(filep->f_inode);
+  if (ret >= 0)
+    {
+      object = (FAR struct shmfs_object_s *)filep->f_inode->i_private;
+
+      if (object)
+        {
+          ret = shmfs_map_object(object, &entry->vaddr);
+        }
+
+      if (!object || ret < 0)
+        {
+          /* Oops, the file has not been truncated yet or
+           * there was error in up_shmat
+           */
+
+          inode_release(filep->f_inode);
+        }
+      else
+        {
+          entry->munmap = shmfs_munmap;
+          entry->priv.p = (FAR void *)filep->f_inode;
+          mm_map_add(entry);
+        }
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_unmap_object
+ ****************************************************************************/
+
+static int shmfs_unmap_area(FAR struct task_group_s *group,
+                            FAR void *vaddr, size_t length)
+{
+  int ret = OK;
+
+#ifdef CONFIG_BUILD_KERNEL
+  unsigned int npages;
+
+  /* Convert the region size to pages */
+
+  if (group)
+    {
+      npages = MM_NPAGES(length);
+
+      /* Unmap the memory from user virtual address space */
+
+      ret = up_shmdt((uintptr_t)vaddr, npages);
+
+      /* Add the virtual memory back to the shared memory pool */
+
+      vm_release_region(get_group_mm(group), vaddr, length);
+    }
+#endif
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_munmap
+ ****************************************************************************/
+
+static int shmfs_munmap(FAR struct task_group_s *group,
+                        FAR struct mm_map_entry_s *entry,
+                        FAR void *start,
+                        size_t length)
+{
+  int                      ret;
+
+  /* Partial unmap is not supported yet */
+
+  if (start != entry->vaddr || length != entry->length)
+    {
+      return -EINVAL;
+    }
+
+  /* Unmap the virtual memory area from the user's address space */
+
+  ret = shmfs_unmap_area(group, entry->vaddr, entry->length);
+
+  /* Release the shmfs object. The object gets deleted if no-one has
+   * reference to it (either mmap or open file) and the object has been
+   * unlinked
+   */
+
+  if (ret == OK)
+    {
+      ret = shmfs_release((FAR struct inode *)entry->priv.p);
+    }
+
+  /* Remove the mapping. */
+
+  if (ret == OK)
+    {
+      ret = mm_map_remove(get_group_mm(group), entry);
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: shmfs_initialize
+ ****************************************************************************/
+
+void shmfs_initialize(void)
+{
+  register_driver(CONFIG_FS_SHMFS_VFS_PATH, &shmfs_operations, 0666, NULL);

Review Comment:
   why need register driver here?



##########
fs/shm/shmfs.c:
##########
@@ -0,0 +1,384 @@
+/****************************************************************************
+ * fs/shm/shmfs.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 <assert.h>
+
+#include <nuttx/fs/ioctl.h>
+#include <nuttx/mm/map.h>
+
+#if defined (CONFIG_BUILD_KERNEL)
+#include <nuttx/arch.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
+#endif
+
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep);
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen);
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen);
+static int shmfs_truncate(FAR struct file *filep, off_t length);
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode);
+#endif
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry);
+static int shmfs_munmap(FAR struct task_group_s *group,
+                        FAR struct mm_map_entry_s *entry,
+                        FAR void *start,
+                        size_t length);
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+const struct file_operations shmfs_operations =
+{
+  NULL,             /* open */
+  shmfs_close,      /* close */
+  shmfs_read,       /* read */
+  shmfs_write,      /* write */
+  NULL,             /* seek */
+  NULL,             /* ioctl */
+  shmfs_mmap,       /* mmap */
+  shmfs_truncate,   /* truncate */
+  NULL,             /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  shmfs_unlink      /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: shmfs_read
+ ****************************************************************************/
+
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_write
+ ****************************************************************************/
+
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_release
+ ****************************************************************************/
+
+static int shmfs_release(FAR struct inode *inode)
+{
+  /* If the file has been unlinked previously, delete the contents.
+   * The inode is released after this call, hence checking if i_crefs <= 1.
+   */
+
+  int ret = inode_lock();
+  if (ret >= 0)
+    {
+      if (inode->i_parent == NULL &&
+          inode->i_crefs <= 1)
+        {
+          delete_shm_object(inode->i_private);
+          inode->i_private = NULL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_close
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep)
+{
+  /* Release the shmfs object. The object gets deleted if no-one has
+   * reference to it (either mmap or open file) and the object has been
+   * unlinked
+   */
+
+  return shmfs_release(filep->f_inode);
+}
+
+/****************************************************************************
+ * Name: shmfs_truncate
+ ****************************************************************************/
+
+static int shmfs_truncate(FAR struct file *filep, off_t length)
+{
+  FAR struct shmfs_object_s *object;
+  int ret;
+
+  if (length == 0)
+    {
+      return -EINVAL;
+    }
+
+  ret = inode_lock();
+  if (ret >= 0)
+    {
+      object = filep->f_inode->i_private;
+      if (!object)
+        {
+          filep->f_inode->i_private = alloc_shm_object(length);
+        }
+      else if (object->length != length)
+        {
+          /* This doesn't support resize */
+
+          ret = -EINVAL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_unlink
+ ****************************************************************************/
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode)
+{
+  if (inode->i_crefs <= 1)
+    {
+      delete_shm_object(inode->i_private);
+      inode->i_private = NULL;
+    }
+
+  return OK;
+}
+#endif
+
+/****************************************************************************
+ * Name: shmfs_map_object
+ ****************************************************************************/
+
+static int shmfs_map_object(FAR struct shmfs_object_s *object,
+                            FAR void **vaddr)
+{
+  int ret = OK;
+
+#ifdef CONFIG_BUILD_KERNEL
+  /* Map the physical pages of the shm object with MMU. */
+
+  FAR struct tcb_s        *tcb   = nxsched_self();
+  FAR struct task_group_s *group = tcb->group;
+  FAR uintptr_t *pages =
+    (FAR uintptr_t *)&object->paddr;
+  uintptr_t                mapaddr;
+  unsigned int             npages;
+
+  /* Find a free vaddr space that satisfies length */
+
+  mapaddr = (uintptr_t)vm_alloc_region(get_group_mm(group), 0,
+                                       object->length);
+  if (mapaddr == 0)
+    {
+      return -ENOMEM;
+    }
+
+  /* Convert the region size to pages */
+
+  npages = MM_NPAGES(object->length);
+
+  /* Map the memory to user virtual address space */
+
+  ret = up_shmat(pages, npages, mapaddr);
+  if (ret < 0)
+    {
+      vm_release_region(get_group_mm(group), (FAR void *)mapaddr,
+                        object->length);
+    }
+  else
+    {
+      *vaddr = (FAR void *)mapaddr;
+    }
+#else
+  /* Use the physical address directly */
+
+  *vaddr = object->paddr;
+#endif
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_mmap
+ ****************************************************************************/
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry)
+{
+  FAR struct shmfs_object_s *object;
+  int ret = -EINVAL;
+
+  DEBUGASSERT(filep->f_inode != NULL);
+
+  /* We don't support offset at the moment, just mapping the whole object */
+
+  if (entry->offset != 0)
+    {
+      return ret;
+    }
+
+  /* Keep the inode when mmapped, increase refcount */
+
+  ret = inode_addref(filep->f_inode);
+  if (ret >= 0)
+    {
+      object = (FAR struct shmfs_object_s *)filep->f_inode->i_private;
+
+      if (object)
+        {
+          ret = shmfs_map_object(object, &entry->vaddr);
+        }
+
+      if (!object || ret < 0)
+        {
+          /* Oops, the file has not been truncated yet or
+           * there was error in up_shmat
+           */
+
+          inode_release(filep->f_inode);
+        }
+      else
+        {
+          entry->munmap = shmfs_munmap;
+          entry->priv.p = (FAR void *)filep->f_inode;
+          mm_map_add(entry);
+        }
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_unmap_object
+ ****************************************************************************/
+
+static int shmfs_unmap_area(FAR struct task_group_s *group,
+                            FAR void *vaddr, size_t length)
+{
+  int ret = OK;
+
+#ifdef CONFIG_BUILD_KERNEL
+  unsigned int npages;
+
+  /* Convert the region size to pages */
+
+  if (group)
+    {
+      npages = MM_NPAGES(length);
+
+      /* Unmap the memory from user virtual address space */
+
+      ret = up_shmdt((uintptr_t)vaddr, npages);
+
+      /* Add the virtual memory back to the shared memory pool */
+
+      vm_release_region(get_group_mm(group), vaddr, length);
+    }
+#endif
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_munmap
+ ****************************************************************************/
+
+static int shmfs_munmap(FAR struct task_group_s *group,
+                        FAR struct mm_map_entry_s *entry,
+                        FAR void *start,
+                        size_t length)
+{
+  int                      ret;

Review Comment:
   remove the extra space



##########
fs/shm/shm_open.c:
##########
@@ -0,0 +1,163 @@
+/****************************************************************************
+ * fs/shm/shm_open.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 <nuttx/config.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <nuttx/fs/fs.h>
+#include "inode/inode.h"
+#include "shm/shmfs.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+int file_shm_vopen(FAR struct file *shm, FAR const char *shm_name,

Review Comment:
   shm_name to name



##########
fs/shm/shmfs.c:
##########
@@ -0,0 +1,384 @@
+/****************************************************************************
+ * fs/shm/shmfs.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 <assert.h>
+
+#include <nuttx/fs/ioctl.h>
+#include <nuttx/mm/map.h>
+
+#if defined (CONFIG_BUILD_KERNEL)
+#include <nuttx/arch.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
+#endif
+
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep);
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen);
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen);
+static int shmfs_truncate(FAR struct file *filep, off_t length);
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode);
+#endif
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry);
+static int shmfs_munmap(FAR struct task_group_s *group,
+                        FAR struct mm_map_entry_s *entry,
+                        FAR void *start,
+                        size_t length);
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+const struct file_operations shmfs_operations =
+{
+  NULL,             /* open */
+  shmfs_close,      /* close */
+  shmfs_read,       /* read */
+  shmfs_write,      /* write */
+  NULL,             /* seek */
+  NULL,             /* ioctl */
+  shmfs_mmap,       /* mmap */
+  shmfs_truncate,   /* truncate */
+  NULL,             /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  shmfs_unlink      /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: shmfs_read
+ ****************************************************************************/
+
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_write
+ ****************************************************************************/
+
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_release
+ ****************************************************************************/
+
+static int shmfs_release(FAR struct inode *inode)
+{
+  /* If the file has been unlinked previously, delete the contents.
+   * The inode is released after this call, hence checking if i_crefs <= 1.
+   */
+
+  int ret = inode_lock();
+  if (ret >= 0)
+    {
+      if (inode->i_parent == NULL &&
+          inode->i_crefs <= 1)
+        {
+          delete_shm_object(inode->i_private);
+          inode->i_private = NULL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_close
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep)
+{
+  /* Release the shmfs object. The object gets deleted if no-one has
+   * reference to it (either mmap or open file) and the object has been
+   * unlinked
+   */
+
+  return shmfs_release(filep->f_inode);
+}
+
+/****************************************************************************
+ * Name: shmfs_truncate
+ ****************************************************************************/
+
+static int shmfs_truncate(FAR struct file *filep, off_t length)
+{
+  FAR struct shmfs_object_s *object;
+  int ret;
+
+  if (length == 0)
+    {
+      return -EINVAL;
+    }
+
+  ret = inode_lock();

Review Comment:
   why hold inode lock here?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072074656


##########
fs/shm/shm_unlink.c:
##########
@@ -0,0 +1,140 @@
+/****************************************************************************
+ * fs/shm/shm_unlink.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 <stdio.h>
+#include <assert.h>
+#include <errno.h>
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int file_shm_unlink(FAR const char *shm_name)
+{
+  FAR struct inode *inode;
+  struct inode_search_s desc;
+  char fullpath[sizeof(CONFIG_FS_SHMFS_VFS_PATH) + CONFIG_NAME_MAX + 2];
+  int ret;
+
+  /* Get the full path to the shm object */
+
+  snprintf(fullpath, sizeof(fullpath),
+           CONFIG_FS_SHMFS_VFS_PATH "/%s", shm_name);
+
+  /* Get the inode for this shm object */
+
+  SETUP_SEARCH(&desc, fullpath, false);
+
+  ret = inode_lock();
+  if (ret < 0)
+    {
+      goto errout_with_search;
+    }
+
+  ret = inode_find(&desc);
+  if (ret < 0)
+    {
+      /* There is no inode that includes in this path */
+
+      goto errout_with_sem;
+    }
+
+  /* Get the search results */
+
+  inode = desc.node;
+  DEBUGASSERT(inode != NULL);
+
+  /* Verify that what we found is, indeed, an shm inode */
+
+  if (!INODE_IS_SHM(inode))
+    {
+      ret = -ENOENT;

Review Comment:
   I don't think so. Acc. to posix spec, shm_unlink can only return errnos :
   
   ...
   [EACCES]    <- this is not currently supported by nuttx fs in general
   Permission is denied to unlink the named shared memory object.
   
   [ENAMETOOLONG]
   The length of the name argument exceeds {PATH_MAX} or a pathname component is longer than {NAME_MAX}.
   
   [ENOENT]
   The named shared memory object does not exist.
   ...
   
   But, I added the ENAMETOOLONG, missing that was actually a bug
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072850302


##########
fs/shm/shmfs_alloc.c:
##########
@@ -0,0 +1,144 @@
+/****************************************************************************
+ * fs/shm/shmfs_alloc.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 <stdlib.h>
+#include <stdbool.h>

Review Comment:
   stdbool I didn't remove, there is a "bool" variable in the code



##########
fs/shm/shmfs.h:
##########
@@ -0,0 +1,70 @@
+/****************************************************************************
+ * fs/shm/shmfs.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_SHM_SHMFS_H
+#define __FS_SHM_SHMFS_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdlib.h>

Review Comment:
   ok done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072034070


##########
fs/shm/shmfs.c:
##########
@@ -0,0 +1,384 @@
+/****************************************************************************
+ * fs/shm/shmfs.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 <assert.h>
+
+#include <nuttx/fs/ioctl.h>
+#include <nuttx/mm/map.h>
+
+#if defined (CONFIG_BUILD_KERNEL)
+#include <nuttx/arch.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
+#endif
+
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep);
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen);
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen);
+static int shmfs_truncate(FAR struct file *filep, off_t length);
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode);
+#endif
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry);
+static int shmfs_munmap(FAR struct task_group_s *group,
+                        FAR struct mm_map_entry_s *entry,
+                        FAR void *start,
+                        size_t length);
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+const struct file_operations shmfs_operations =
+{
+  NULL,             /* open */
+  shmfs_close,      /* close */
+  shmfs_read,       /* read */
+  shmfs_write,      /* write */
+  NULL,             /* seek */
+  NULL,             /* ioctl */
+  shmfs_mmap,       /* mmap */
+  shmfs_truncate,   /* truncate */
+  NULL,             /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  shmfs_unlink      /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: shmfs_read
+ ****************************************************************************/
+
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_write
+ ****************************************************************************/
+
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_release
+ ****************************************************************************/
+
+static int shmfs_release(FAR struct inode *inode)
+{
+  /* If the file has been unlinked previously, delete the contents.
+   * The inode is released after this call, hence checking if i_crefs <= 1.
+   */
+
+  int ret = inode_lock();
+  if (ret >= 0)
+    {
+      if (inode->i_parent == NULL &&
+          inode->i_crefs <= 1)
+        {
+          delete_shm_object(inode->i_private);
+          inode->i_private = NULL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_close
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep)
+{
+  /* Release the shmfs object. The object gets deleted if no-one has
+   * reference to it (either mmap or open file) and the object has been
+   * unlinked
+   */
+
+  return shmfs_release(filep->f_inode);
+}
+
+/****************************************************************************
+ * Name: shmfs_truncate
+ ****************************************************************************/
+
+static int shmfs_truncate(FAR struct file *filep, off_t length)
+{
+  FAR struct shmfs_object_s *object;
+  int ret;
+
+  if (length == 0)
+    {
+      return -EINVAL;
+    }
+
+  ret = inode_lock();
+  if (ret >= 0)
+    {
+      object = filep->f_inode->i_private;
+      if (!object)
+        {
+          filep->f_inode->i_private = alloc_shm_object(length);
+        }
+      else if (object->length != length)
+        {
+          /* This doesn't support resize */
+
+          ret = -EINVAL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_unlink
+ ****************************************************************************/
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode)
+{
+  if (inode->i_crefs <= 1)
+    {
+      delete_shm_object(inode->i_private);
+      inode->i_private = NULL;
+    }
+
+  return OK;
+}
+#endif
+
+/****************************************************************************
+ * Name: shmfs_map_object
+ ****************************************************************************/
+
+static int shmfs_map_object(FAR struct shmfs_object_s *object,
+                            FAR void **vaddr)
+{
+  int ret = OK;
+
+#ifdef CONFIG_BUILD_KERNEL
+  /* Map the physical pages of the shm object with MMU. */
+
+  FAR struct tcb_s        *tcb   = nxsched_self();
+  FAR struct task_group_s *group = tcb->group;
+  FAR uintptr_t *pages =
+    (FAR uintptr_t *)&object->paddr;
+  uintptr_t                mapaddr;
+  unsigned int             npages;
+
+  /* Find a free vaddr space that satisfies length */
+
+  mapaddr = (uintptr_t)vm_alloc_region(get_group_mm(group), 0,
+                                       object->length);
+  if (mapaddr == 0)
+    {
+      return -ENOMEM;
+    }
+
+  /* Convert the region size to pages */
+
+  npages = MM_NPAGES(object->length);
+
+  /* Map the memory to user virtual address space */
+
+  ret = up_shmat(pages, npages, mapaddr);
+  if (ret < 0)
+    {
+      vm_release_region(get_group_mm(group), (FAR void *)mapaddr,
+                        object->length);
+    }
+  else
+    {
+      *vaddr = (FAR void *)mapaddr;
+    }
+#else
+  /* Use the physical address directly */
+
+  *vaddr = object->paddr;
+#endif
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_mmap
+ ****************************************************************************/
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry)
+{
+  FAR struct shmfs_object_s *object;
+  int ret = -EINVAL;
+
+  DEBUGASSERT(filep->f_inode != NULL);
+
+  /* We don't support offset at the moment, just mapping the whole object */
+
+  if (entry->offset != 0)
+    {
+      return ret;
+    }
+
+  /* Keep the inode when mmapped, increase refcount */
+
+  ret = inode_addref(filep->f_inode);
+  if (ret >= 0)
+    {
+      object = (FAR struct shmfs_object_s *)filep->f_inode->i_private;
+
+      if (object)
+        {
+          ret = shmfs_map_object(object, &entry->vaddr);
+        }
+
+      if (!object || ret < 0)
+        {
+          /* Oops, the file has not been truncated yet or
+           * there was error in up_shmat
+           */
+
+          inode_release(filep->f_inode);
+        }
+      else
+        {
+          entry->munmap = shmfs_munmap;
+          entry->priv.p = (FAR void *)filep->f_inode;
+          mm_map_add(entry);
+        }
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_unmap_object
+ ****************************************************************************/
+
+static int shmfs_unmap_area(FAR struct task_group_s *group,
+                            FAR void *vaddr, size_t length)
+{
+  int ret = OK;
+
+#ifdef CONFIG_BUILD_KERNEL
+  unsigned int npages;
+
+  /* Convert the region size to pages */
+
+  if (group)
+    {
+      npages = MM_NPAGES(length);
+
+      /* Unmap the memory from user virtual address space */
+
+      ret = up_shmdt((uintptr_t)vaddr, npages);
+
+      /* Add the virtual memory back to the shared memory pool */
+
+      vm_release_region(get_group_mm(group), vaddr, length);
+    }
+#endif
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_munmap
+ ****************************************************************************/
+
+static int shmfs_munmap(FAR struct task_group_s *group,
+                        FAR struct mm_map_entry_s *entry,
+                        FAR void *start,
+                        size_t length)
+{
+  int                      ret;
+
+  /* Partial unmap is not supported yet */
+
+  if (start != entry->vaddr || length != entry->length)
+    {
+      return -EINVAL;
+    }
+
+  /* Unmap the virtual memory area from the user's address space */
+
+  ret = shmfs_unmap_area(group, entry->vaddr, entry->length);
+
+  /* Release the shmfs object. The object gets deleted if no-one has
+   * reference to it (either mmap or open file) and the object has been
+   * unlinked
+   */
+
+  if (ret == OK)
+    {
+      ret = shmfs_release((FAR struct inode *)entry->priv.p);
+    }
+
+  /* Remove the mapping. */
+
+  if (ret == OK)
+    {
+      ret = mm_map_remove(get_group_mm(group), entry);
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: shmfs_initialize
+ ****************************************************************************/
+
+void shmfs_initialize(void)
+{
+  register_driver(CONFIG_FS_SHMFS_VFS_PATH, &shmfs_operations, 0666, NULL);

Review Comment:
   You are right, there is no need any longer. This was an artifact from some earlier version. Removed the whole shmfs_initialize.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072037841


##########
fs/shm/shmfs.c:
##########
@@ -0,0 +1,384 @@
+/****************************************************************************
+ * fs/shm/shmfs.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 <assert.h>
+
+#include <nuttx/fs/ioctl.h>
+#include <nuttx/mm/map.h>
+
+#if defined (CONFIG_BUILD_KERNEL)
+#include <nuttx/arch.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
+#endif
+
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep);
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen);
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen);
+static int shmfs_truncate(FAR struct file *filep, off_t length);
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode);
+#endif
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry);
+static int shmfs_munmap(FAR struct task_group_s *group,
+                        FAR struct mm_map_entry_s *entry,
+                        FAR void *start,
+                        size_t length);
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+const struct file_operations shmfs_operations =
+{
+  NULL,             /* open */
+  shmfs_close,      /* close */
+  shmfs_read,       /* read */
+  shmfs_write,      /* write */
+  NULL,             /* seek */
+  NULL,             /* ioctl */
+  shmfs_mmap,       /* mmap */
+  shmfs_truncate,   /* truncate */
+  NULL,             /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  shmfs_unlink      /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: shmfs_read
+ ****************************************************************************/
+
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_write
+ ****************************************************************************/
+
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_release
+ ****************************************************************************/
+
+static int shmfs_release(FAR struct inode *inode)
+{
+  /* If the file has been unlinked previously, delete the contents.
+   * The inode is released after this call, hence checking if i_crefs <= 1.
+   */
+
+  int ret = inode_lock();
+  if (ret >= 0)
+    {
+      if (inode->i_parent == NULL &&
+          inode->i_crefs <= 1)
+        {
+          delete_shm_object(inode->i_private);
+          inode->i_private = NULL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_close
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep)
+{
+  /* Release the shmfs object. The object gets deleted if no-one has
+   * reference to it (either mmap or open file) and the object has been
+   * unlinked
+   */
+
+  return shmfs_release(filep->f_inode);
+}
+
+/****************************************************************************
+ * Name: shmfs_truncate
+ ****************************************************************************/
+
+static int shmfs_truncate(FAR struct file *filep, off_t length)
+{
+  FAR struct shmfs_object_s *object;
+  int ret;
+
+  if (length == 0)
+    {
+      return -EINVAL;
+    }
+
+  ret = inode_lock();
+  if (ret >= 0)
+    {
+      object = filep->f_inode->i_private;
+      if (!object)
+        {
+          filep->f_inode->i_private = alloc_shm_object(length);
+        }
+      else if (object->length != length)
+        {
+          /* This doesn't support resize */
+
+          ret = -EINVAL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_unlink
+ ****************************************************************************/
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode)
+{
+  if (inode->i_crefs <= 1)
+    {
+      delete_shm_object(inode->i_private);
+      inode->i_private = NULL;
+    }
+
+  return OK;
+}
+#endif
+
+/****************************************************************************
+ * Name: shmfs_map_object
+ ****************************************************************************/
+
+static int shmfs_map_object(FAR struct shmfs_object_s *object,
+                            FAR void **vaddr)
+{
+  int ret = OK;
+
+#ifdef CONFIG_BUILD_KERNEL
+  /* Map the physical pages of the shm object with MMU. */
+
+  FAR struct tcb_s        *tcb   = nxsched_self();
+  FAR struct task_group_s *group = tcb->group;
+  FAR uintptr_t *pages =
+    (FAR uintptr_t *)&object->paddr;
+  uintptr_t                mapaddr;
+  unsigned int             npages;
+
+  /* Find a free vaddr space that satisfies length */
+
+  mapaddr = (uintptr_t)vm_alloc_region(get_group_mm(group), 0,
+                                       object->length);
+  if (mapaddr == 0)
+    {
+      return -ENOMEM;
+    }
+
+  /* Convert the region size to pages */
+
+  npages = MM_NPAGES(object->length);
+
+  /* Map the memory to user virtual address space */
+
+  ret = up_shmat(pages, npages, mapaddr);
+  if (ret < 0)
+    {
+      vm_release_region(get_group_mm(group), (FAR void *)mapaddr,
+                        object->length);
+    }
+  else
+    {
+      *vaddr = (FAR void *)mapaddr;
+    }
+#else
+  /* Use the physical address directly */
+
+  *vaddr = object->paddr;
+#endif
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_mmap
+ ****************************************************************************/
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry)
+{
+  FAR struct shmfs_object_s *object;
+  int ret = -EINVAL;
+
+  DEBUGASSERT(filep->f_inode != NULL);
+
+  /* We don't support offset at the moment, just mapping the whole object */
+
+  if (entry->offset != 0)
+    {
+      return ret;
+    }
+
+  /* Keep the inode when mmapped, increase refcount */
+
+  ret = inode_addref(filep->f_inode);
+  if (ret >= 0)
+    {
+      object = (FAR struct shmfs_object_s *)filep->f_inode->i_private;
+
+      if (object)

Review Comment:
   I think that the c_refs need to be bumped first before tinkering with filep->f_inode



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] xiaoxiang781216 commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072099847


##########
boards/risc-v/mpfs/icicle/configs/knsh/defconfig:
##########
@@ -68,7 +69,6 @@ CONFIG_LIBM=y
 CONFIG_MEMSET_64BIT=y
 CONFIG_MEMSET_OPTSPEED=y
 CONFIG_MM_PGALLOC=y
-CONFIG_MM_SHM=y

Review Comment:
   This is only defconfig which enable CONFIG_MM_SHM, keep it could ensure we don't break the related code in future.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072876124


##########
fs/shm/shmfs.c:
##########
@@ -0,0 +1,377 @@
+/****************************************************************************
+ * fs/shm/shmfs.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 <assert.h>
+
+#include <nuttx/fs/ioctl.h>
+#include <nuttx/mm/map.h>
+
+#if defined (CONFIG_BUILD_KERNEL)
+#include <nuttx/arch.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
+#endif
+
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep);
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen);
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen);
+static int shmfs_truncate(FAR struct file *filep, off_t length);
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode);
+#endif
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry);
+static int shmfs_munmap(FAR struct task_group_s *group,
+                        FAR struct mm_map_entry_s *entry,
+                        FAR void *start,
+                        size_t length);
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+const struct file_operations shmfs_operations =
+{
+  NULL,             /* open */
+  shmfs_close,      /* close */
+  shmfs_read,       /* read */
+  shmfs_write,      /* write */
+  NULL,             /* seek */
+  NULL,             /* ioctl */
+  shmfs_mmap,       /* mmap */
+  shmfs_truncate,   /* truncate */
+  NULL,             /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  shmfs_unlink      /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: shmfs_read
+ ****************************************************************************/
+
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_write
+ ****************************************************************************/
+
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_release
+ ****************************************************************************/
+
+static int shmfs_release(FAR struct inode *inode)
+{
+  /* If the file has been unlinked previously, delete the contents.
+   * The inode is released after this call, hence checking if i_crefs <= 1.
+   */
+
+  int ret = inode_lock();
+  if (ret >= 0)
+    {
+      if (inode->i_parent == NULL &&
+          inode->i_crefs <= 1)
+        {
+          shmfs_free_object(inode->i_private);
+          inode->i_private = NULL;
+          ret = OK;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_close
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep)
+{
+  /* Release the shmfs object. The object gets deleted if no-one has
+   * reference to it (either mmap or open file) and the object has been
+   * unlinked
+   */
+
+  return shmfs_release(filep->f_inode);
+}
+
+/****************************************************************************
+ * Name: shmfs_truncate
+ ****************************************************************************/
+
+static int shmfs_truncate(FAR struct file *filep, off_t length)
+{
+  FAR struct shmfs_object_s *object;
+  int ret;
+
+  if (length == 0)
+    {
+      return -EINVAL;
+    }
+
+  ret = inode_lock();
+  if (ret >= 0)
+    {
+      object = filep->f_inode->i_private;
+      if (!object)
+        {
+          filep->f_inode->i_private = shmfs_alloc_object(length);
+          if (!filep->f_inode->i_private)
+            {
+              ret = -EFAULT;
+            }
+        }
+      else if (object->length != length)
+        {
+          /* This doesn't support resize */
+
+          ret = -EINVAL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_unlink
+ ****************************************************************************/
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode)
+{
+  int ret = inode_lock();
+
+  if (ret >= 0)
+    {
+      if (inode->i_crefs <= 1)
+        {
+          shmfs_free_object(inode->i_private);
+          inode->i_private = NULL;
+          ret = OK;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+#endif
+
+/****************************************************************************
+ * Name: shmfs_map_object
+ ****************************************************************************/
+
+static int shmfs_map_object(FAR struct shmfs_object_s *object,
+                            FAR void **vaddr)
+{
+  int ret = OK;
+
+#ifdef CONFIG_BUILD_KERNEL
+  /* Map the physical pages of the shm object with MMU. */
+
+  FAR struct tcb_s        *tcb   = nxsched_self();
+  FAR struct task_group_s *group = tcb->group;
+  FAR uintptr_t *pages =

Review Comment:
   Sorry about that. Above there was a bunch of orders to remove the aligning spaces here and there, so I tried to be nice and remove them elsewhere, and as you said this is the only place where they were left in this file. I just frankly don't care any more, but I'll soon hit close on this PR rather than add/remove any space or blank line. It does pass nxstyle.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072481048


##########
fs/shm/shmfs.c:
##########
@@ -0,0 +1,384 @@
+/****************************************************************************
+ * fs/shm/shmfs.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 <assert.h>
+
+#include <nuttx/fs/ioctl.h>
+#include <nuttx/mm/map.h>
+
+#if defined (CONFIG_BUILD_KERNEL)
+#include <nuttx/arch.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
+#endif
+
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep);
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen);
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen);
+static int shmfs_truncate(FAR struct file *filep, off_t length);
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode);
+#endif
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry);
+static int shmfs_munmap(FAR struct task_group_s *group,
+                        FAR struct mm_map_entry_s *entry,
+                        FAR void *start,
+                        size_t length);
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+const struct file_operations shmfs_operations =
+{
+  NULL,             /* open */
+  shmfs_close,      /* close */
+  shmfs_read,       /* read */
+  shmfs_write,      /* write */
+  NULL,             /* seek */
+  NULL,             /* ioctl */
+  shmfs_mmap,       /* mmap */
+  shmfs_truncate,   /* truncate */
+  NULL,             /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  shmfs_unlink      /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: shmfs_read
+ ****************************************************************************/
+
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_write
+ ****************************************************************************/
+
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_release
+ ****************************************************************************/
+
+static int shmfs_release(FAR struct inode *inode)
+{
+  /* If the file has been unlinked previously, delete the contents.
+   * The inode is released after this call, hence checking if i_crefs <= 1.
+   */
+
+  int ret = inode_lock();
+  if (ret >= 0)
+    {
+      if (inode->i_parent == NULL &&
+          inode->i_crefs <= 1)
+        {
+          delete_shm_object(inode->i_private);
+          inode->i_private = NULL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_close
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep)
+{
+  /* Release the shmfs object. The object gets deleted if no-one has
+   * reference to it (either mmap or open file) and the object has been
+   * unlinked
+   */
+
+  return shmfs_release(filep->f_inode);
+}
+
+/****************************************************************************
+ * Name: shmfs_truncate
+ ****************************************************************************/
+
+static int shmfs_truncate(FAR struct file *filep, off_t length)
+{
+  FAR struct shmfs_object_s *object;
+  int ret;
+
+  if (length == 0)
+    {
+      return -EINVAL;
+    }
+
+  ret = inode_lock();
+  if (ret >= 0)
+    {
+      object = filep->f_inode->i_private;
+      if (!object)
+        {
+          filep->f_inode->i_private = alloc_shm_object(length);
+        }
+      else if (object->length != length)
+        {
+          /* This doesn't support resize */
+
+          ret = -EINVAL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_unlink
+ ****************************************************************************/
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode)
+{
+  if (inode->i_crefs <= 1)
+    {
+      delete_shm_object(inode->i_private);
+      inode->i_private = NULL;
+    }
+
+  return OK;
+}
+#endif
+
+/****************************************************************************
+ * Name: shmfs_map_object
+ ****************************************************************************/
+
+static int shmfs_map_object(FAR struct shmfs_object_s *object,
+                            FAR void **vaddr)
+{
+  int ret = OK;
+
+#ifdef CONFIG_BUILD_KERNEL
+  /* Map the physical pages of the shm object with MMU. */
+
+  FAR struct tcb_s        *tcb   = nxsched_self();
+  FAR struct task_group_s *group = tcb->group;
+  FAR uintptr_t *pages =
+    (FAR uintptr_t *)&object->paddr;
+  uintptr_t                mapaddr;
+  unsigned int             npages;
+
+  /* Find a free vaddr space that satisfies length */
+
+  mapaddr = (uintptr_t)vm_alloc_region(get_group_mm(group), 0,
+                                       object->length);
+  if (mapaddr == 0)
+    {
+      return -ENOMEM;
+    }
+
+  /* Convert the region size to pages */
+
+  npages = MM_NPAGES(object->length);
+
+  /* Map the memory to user virtual address space */
+
+  ret = up_shmat(pages, npages, mapaddr);
+  if (ret < 0)
+    {
+      vm_release_region(get_group_mm(group), (FAR void *)mapaddr,
+                        object->length);
+    }
+  else
+    {
+      *vaddr = (FAR void *)mapaddr;
+    }
+#else
+  /* Use the physical address directly */
+
+  *vaddr = object->paddr;
+#endif
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_mmap
+ ****************************************************************************/
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry)
+{
+  FAR struct shmfs_object_s *object;
+  int ret = -EINVAL;
+
+  DEBUGASSERT(filep->f_inode != NULL);
+
+  /* We don't support offset at the moment, just mapping the whole object */
+
+  if (entry->offset != 0)
+    {
+      return ret;
+    }
+
+  /* Keep the inode when mmapped, increase refcount */
+
+  ret = inode_addref(filep->f_inode);
+  if (ret >= 0)
+    {
+      object = (FAR struct shmfs_object_s *)filep->f_inode->i_private;
+
+      if (object)

Review Comment:
   ah true. updated according to your recommendation. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] xiaoxiang781216 commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072643535


##########
fs/shm/shm_unlink.c:
##########
@@ -0,0 +1,163 @@
+/****************************************************************************
+ * fs/shm/shm_unlink.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 <stdio.h>
+#include <assert.h>
+#include <errno.h>
+#include "shm/shmfs.h"

Review Comment:
   remove shm/shmfs.h



##########
fs/shm/shm_unlink.c:
##########
@@ -0,0 +1,163 @@
+/****************************************************************************
+ * fs/shm/shm_unlink.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 <stdio.h>
+#include <assert.h>
+#include <errno.h>

Review Comment:
   add blank line



##########
fs/shm/shm_unlink.c:
##########
@@ -0,0 +1,163 @@
+/****************************************************************************
+ * fs/shm/shm_unlink.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 <stdio.h>
+#include <assert.h>
+#include <errno.h>
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int file_shm_unlink(FAR const char *name)
+{
+  FAR struct inode *inode;
+  struct inode_search_s desc;
+  char fullpath[sizeof(CONFIG_FS_SHMFS_VFS_PATH) + CONFIG_NAME_MAX + 2];
+  int ret;
+
+  /* Make sure that a non-NULL name is supplied */
+
+  if (!name)
+    {
+      return -ENOENT;
+    }
+
+  /* Remove any number of leading '/' */
+
+  while (*name == '/')
+    {
+      name++;
+    }
+
+  /* Empty name supplied? */
+
+  if (*name == '\0')
+    {
+      return -ENOENT;
+    }
+
+  /* Name too long? */
+
+  if (strnlen(name, CONFIG_NAME_MAX + 1) > CONFIG_NAME_MAX)
+    {
+      return -ENAMETOOLONG;
+    }
+
+  /* Get the full path to the shm object */
+
+  snprintf(fullpath, sizeof(fullpath),
+           CONFIG_FS_SHMFS_VFS_PATH "/%s", name);
+
+  /* Get the inode for this shm object */
+
+  SETUP_SEARCH(&desc, fullpath, false);
+
+  ret = inode_lock();
+  if (ret < 0)
+    {
+      goto errout_with_search;
+    }
+
+  ret = inode_find(&desc);
+  if (ret < 0)
+    {
+      /* There is no inode that includes in this path */
+
+      goto errout_with_sem;
+    }
+
+  /* Get the search results */
+
+  inode = desc.node;
+  DEBUGASSERT(inode != NULL);
+
+  /* Verify that what we found is, indeed, an shm inode */
+
+  if (!INODE_IS_SHM(inode))
+    {
+      ret = -ENOENT;
+      goto errout_with_inode;
+    }
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  if (inode->u.i_ops->unlink)
+    {
+      /* Notify the shmfs driver that it has been unlinked */
+
+      ret = inode->u.i_ops->unlink(inode);
+      if (ret < 0)
+        {
+          goto errout_with_inode;
+        }
+    }
+#endif
+
+  /* Remove the old inode from the tree. If we hold a reference count
+   * on the inode, it will not be deleted now.  This will set the
+   * FSNODEFLAG_DELETED bit in the inode flags.
+   */
+
+  ret = inode_remove(fullpath);
+
+  /* inode_remove() should always fail with -EBUSY because we have a
+   * reference on the inode.  -EBUSY means that the inode was, indeed,
+   * unlinked but it could not be freed because there are references.
+   */
+
+  if (ret == -EBUSY)
+    {
+      ret = OK;
+    }
+
+  DEBUGASSERT(ret == OK);
+
+errout_with_inode:
+  inode_release(inode);
+

Review Comment:
   remove blank line



##########
fs/shm/shmfs_alloc.c:
##########
@@ -0,0 +1,144 @@
+/****************************************************************************
+ * fs/shm/shmfs_alloc.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 <stdlib.h>
+#include <stdbool.h>

Review Comment:
   remove stdbool.h and stdlib.h



##########
fs/shm/shmfs.h:
##########
@@ -0,0 +1,70 @@
+/****************************************************************************
+ * fs/shm/shmfs.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_SHM_SHMFS_H
+#define __FS_SHM_SHMFS_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdlib.h>

Review Comment:
   change to nuttx/fs/fs.h for file_operations



##########
fs/shm/shm_open.c:
##########
@@ -0,0 +1,181 @@
+/****************************************************************************
+ * fs/shm/shm_open.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 <nuttx/config.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <errno.h>
+
+#include "inode/inode.h"
+#include "shm/shmfs.h"

Review Comment:
   remove shm/shmfs.h



##########
fs/shm/shmfs.c:
##########
@@ -0,0 +1,377 @@
+/****************************************************************************
+ * fs/shm/shmfs.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 <assert.h>
+
+#include <nuttx/fs/ioctl.h>
+#include <nuttx/mm/map.h>
+
+#if defined (CONFIG_BUILD_KERNEL)
+#include <nuttx/arch.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
+#endif
+
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep);
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen);
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen);
+static int shmfs_truncate(FAR struct file *filep, off_t length);
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode);
+#endif
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry);
+static int shmfs_munmap(FAR struct task_group_s *group,
+                        FAR struct mm_map_entry_s *entry,
+                        FAR void *start,
+                        size_t length);
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+const struct file_operations shmfs_operations =
+{
+  NULL,             /* open */
+  shmfs_close,      /* close */
+  shmfs_read,       /* read */
+  shmfs_write,      /* write */
+  NULL,             /* seek */
+  NULL,             /* ioctl */
+  shmfs_mmap,       /* mmap */
+  shmfs_truncate,   /* truncate */
+  NULL,             /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  shmfs_unlink      /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: shmfs_read
+ ****************************************************************************/
+
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_write
+ ****************************************************************************/
+
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_release
+ ****************************************************************************/
+
+static int shmfs_release(FAR struct inode *inode)
+{
+  /* If the file has been unlinked previously, delete the contents.
+   * The inode is released after this call, hence checking if i_crefs <= 1.
+   */
+
+  int ret = inode_lock();
+  if (ret >= 0)
+    {
+      if (inode->i_parent == NULL &&
+          inode->i_crefs <= 1)
+        {
+          shmfs_free_object(inode->i_private);
+          inode->i_private = NULL;
+          ret = OK;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_close
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep)
+{
+  /* Release the shmfs object. The object gets deleted if no-one has
+   * reference to it (either mmap or open file) and the object has been
+   * unlinked
+   */
+
+  return shmfs_release(filep->f_inode);
+}
+
+/****************************************************************************
+ * Name: shmfs_truncate
+ ****************************************************************************/
+
+static int shmfs_truncate(FAR struct file *filep, off_t length)
+{
+  FAR struct shmfs_object_s *object;
+  int ret;
+
+  if (length == 0)
+    {
+      return -EINVAL;
+    }
+
+  ret = inode_lock();
+  if (ret >= 0)
+    {
+      object = filep->f_inode->i_private;
+      if (!object)
+        {
+          filep->f_inode->i_private = shmfs_alloc_object(length);
+          if (!filep->f_inode->i_private)
+            {
+              ret = -EFAULT;
+            }
+        }
+      else if (object->length != length)
+        {
+          /* This doesn't support resize */
+
+          ret = -EINVAL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_unlink
+ ****************************************************************************/
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode)
+{
+  int ret = inode_lock();
+
+  if (ret >= 0)
+    {
+      if (inode->i_crefs <= 1)
+        {
+          shmfs_free_object(inode->i_private);
+          inode->i_private = NULL;
+          ret = OK;

Review Comment:
   remove, don't need



##########
fs/shm/shmfs.c:
##########
@@ -0,0 +1,377 @@
+/****************************************************************************
+ * fs/shm/shmfs.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 <assert.h>
+
+#include <nuttx/fs/ioctl.h>
+#include <nuttx/mm/map.h>
+
+#if defined (CONFIG_BUILD_KERNEL)
+#include <nuttx/arch.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
+#endif
+
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep);
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen);
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen);
+static int shmfs_truncate(FAR struct file *filep, off_t length);
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode);
+#endif
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry);
+static int shmfs_munmap(FAR struct task_group_s *group,
+                        FAR struct mm_map_entry_s *entry,
+                        FAR void *start,
+                        size_t length);
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+const struct file_operations shmfs_operations =
+{
+  NULL,             /* open */
+  shmfs_close,      /* close */
+  shmfs_read,       /* read */
+  shmfs_write,      /* write */
+  NULL,             /* seek */
+  NULL,             /* ioctl */
+  shmfs_mmap,       /* mmap */
+  shmfs_truncate,   /* truncate */
+  NULL,             /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  shmfs_unlink      /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: shmfs_read
+ ****************************************************************************/
+
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_write
+ ****************************************************************************/
+
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_release
+ ****************************************************************************/
+
+static int shmfs_release(FAR struct inode *inode)
+{
+  /* If the file has been unlinked previously, delete the contents.
+   * The inode is released after this call, hence checking if i_crefs <= 1.
+   */
+
+  int ret = inode_lock();
+  if (ret >= 0)
+    {
+      if (inode->i_parent == NULL &&
+          inode->i_crefs <= 1)
+        {
+          shmfs_free_object(inode->i_private);
+          inode->i_private = NULL;
+          ret = OK;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_close
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep)
+{
+  /* Release the shmfs object. The object gets deleted if no-one has
+   * reference to it (either mmap or open file) and the object has been
+   * unlinked
+   */
+
+  return shmfs_release(filep->f_inode);
+}
+
+/****************************************************************************
+ * Name: shmfs_truncate
+ ****************************************************************************/
+
+static int shmfs_truncate(FAR struct file *filep, off_t length)
+{
+  FAR struct shmfs_object_s *object;
+  int ret;
+
+  if (length == 0)
+    {
+      return -EINVAL;
+    }
+
+  ret = inode_lock();
+  if (ret >= 0)
+    {
+      object = filep->f_inode->i_private;
+      if (!object)
+        {
+          filep->f_inode->i_private = shmfs_alloc_object(length);
+          if (!filep->f_inode->i_private)
+            {
+              ret = -EFAULT;
+            }
+        }
+      else if (object->length != length)
+        {
+          /* This doesn't support resize */
+
+          ret = -EINVAL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_unlink
+ ****************************************************************************/
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode)
+{
+  int ret = inode_lock();
+
+  if (ret >= 0)
+    {
+      if (inode->i_crefs <= 1)
+        {
+          shmfs_free_object(inode->i_private);
+          inode->i_private = NULL;
+          ret = OK;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+#endif
+
+/****************************************************************************
+ * Name: shmfs_map_object
+ ****************************************************************************/
+
+static int shmfs_map_object(FAR struct shmfs_object_s *object,
+                            FAR void **vaddr)
+{
+  int ret = OK;
+
+#ifdef CONFIG_BUILD_KERNEL
+  /* Map the physical pages of the shm object with MMU. */
+
+  FAR struct tcb_s        *tcb   = nxsched_self();
+  FAR struct task_group_s *group = tcb->group;
+  FAR uintptr_t *pages =

Review Comment:
   add space before *pages to align with other lines



##########
fs/shm/shmfs_alloc.c:
##########
@@ -0,0 +1,143 @@
+/****************************************************************************
+ * fs/shm/shmfs_alloc.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 <stdlib.h>
+#include <nuttx/pgalloc.h>
+
+#include "inode/inode.h"
+#include "shm/shmfs.h"
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+FAR struct shmfs_object_s *shmfs_alloc_object(size_t length)
+{
+  FAR struct shmfs_object_s *object;
+  bool allocated = false;
+
+#if defined(CONFIG_BUILD_FLAT)
+  /* in FLAT build, allocate the object metadata and the data in the same
+   * chunk in kernel heap
+   */
+
+  object = kmm_zalloc(sizeof(struct shmfs_object_s) + length);
+  if (object)
+    {
+      object->paddr = (&object->paddr) + 1;
+      allocated = true;
+    }
+
+#elif defined(CONFIG_BUILD_PROTECTED)
+  /* in PROTECTED build, allocate the shm object in kernel heap, and shared
+   * memory in user heap
+   */
+
+  object = kmm_zalloc(sizeof(struct shmfs_object_s));
+  if (object)
+    {
+      object->paddr = kumm_zalloc(length);
+
+      if (object->paddr)
+        {
+           allocated = true;
+        }
+    }
+
+#elif defined(CONFIG_BUILD_KERNEL)
+  /* in KERNEL build, allocate the shared memory from page pool and store the
+   * physical address
+   */
+
+  size_t i = 0;
+  void **pages;
+  size_t n_pages = MM_NPAGES(length);
+
+  object = kmm_zalloc(sizeof(struct shmfs_object_s) +
+                      (n_pages - 1) * sizeof(object->paddr));
+
+  if (object)
+    {
+      pages = &object->paddr;
+      for (; i < n_pages; i++)
+        {
+          pages[i] = (void *)mm_pgalloc(1);
+          if (!pages[i])
+            {
+              break;
+            }
+        }
+    }
+
+  if (i == n_pages)
+    {
+      allocated = true;
+    }
+#endif
+
+  if (allocated)
+    {
+      object->length = length;
+    }
+  else
+    {
+      /* delete any partial allocation */
+
+      shmfs_free_object(object);
+      object = NULL;
+    }
+
+  return object;
+}
+
+void shmfs_free_object(FAR struct shmfs_object_s *object)
+{
+#if defined(CONFIG_BUILD_KERNEL)
+  size_t i;
+  size_t n_pages = MM_NPAGES(object->length);
+  void **pages;

Review Comment:
   but not change?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1070659615


##########
fs/shm/shm_open.c:
##########
@@ -0,0 +1,163 @@
+/****************************************************************************
+ * fs/shm/shm_open.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 <nuttx/config.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <nuttx/fs/fs.h>
+#include "inode/inode.h"
+#include "shm/shmfs.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+int file_shm_vopen(FAR struct file *shm, FAR const char *shm_name,
+                   int oflags, mode_t mode)
+{
+  FAR struct inode *inode;
+  struct inode_search_s desc;
+  char fullpath[sizeof(CONFIG_FS_SHMFS_VFS_PATH) + CONFIG_NAME_MAX + 2];
+  int ret;
+
+  /* Make sure that a non-NULL name is supplied */
+
+  if (!shm || !shm_name || *shm_name == '/' || *shm_name == '\0')

Review Comment:
   Why? Name cannot be absolute path



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] xiaoxiang781216 commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1070815320


##########
fs/shm/Kconfig:
##########
@@ -3,16 +3,16 @@
 # see the file kconfig-language.txt in the NuttX tools repository.
 #
 
-config FS_SHM
+config FS_SHMFS

Review Comment:
   Ok.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] pussuw commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
pussuw commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072051035


##########
fs/shm/shmfs.c:
##########
@@ -0,0 +1,384 @@
+/****************************************************************************
+ * fs/shm/shmfs.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 <assert.h>
+
+#include <nuttx/fs/ioctl.h>
+#include <nuttx/mm/map.h>
+
+#if defined (CONFIG_BUILD_KERNEL)
+#include <nuttx/arch.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
+#endif
+
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep);
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen);
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen);
+static int shmfs_truncate(FAR struct file *filep, off_t length);
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode);
+#endif
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry);
+static int shmfs_munmap(FAR struct task_group_s *group,
+                        FAR struct mm_map_entry_s *entry,
+                        FAR void *start,
+                        size_t length);
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+const struct file_operations shmfs_operations =
+{
+  NULL,             /* open */
+  shmfs_close,      /* close */
+  shmfs_read,       /* read */
+  shmfs_write,      /* write */
+  NULL,             /* seek */
+  NULL,             /* ioctl */
+  shmfs_mmap,       /* mmap */
+  shmfs_truncate,   /* truncate */
+  NULL,             /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  shmfs_unlink      /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: shmfs_read
+ ****************************************************************************/
+
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_write
+ ****************************************************************************/
+
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_release
+ ****************************************************************************/
+
+static int shmfs_release(FAR struct inode *inode)
+{
+  /* If the file has been unlinked previously, delete the contents.
+   * The inode is released after this call, hence checking if i_crefs <= 1.
+   */
+
+  int ret = inode_lock();
+  if (ret >= 0)
+    {
+      if (inode->i_parent == NULL &&
+          inode->i_crefs <= 1)
+        {
+          delete_shm_object(inode->i_private);
+          inode->i_private = NULL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_close
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep)
+{
+  /* Release the shmfs object. The object gets deleted if no-one has
+   * reference to it (either mmap or open file) and the object has been
+   * unlinked
+   */
+
+  return shmfs_release(filep->f_inode);
+}
+
+/****************************************************************************
+ * Name: shmfs_truncate
+ ****************************************************************************/
+
+static int shmfs_truncate(FAR struct file *filep, off_t length)
+{
+  FAR struct shmfs_object_s *object;
+  int ret;
+
+  if (length == 0)
+    {
+      return -EINVAL;
+    }
+
+  ret = inode_lock();
+  if (ret >= 0)
+    {
+      object = filep->f_inode->i_private;
+      if (!object)
+        {
+          filep->f_inode->i_private = alloc_shm_object(length);
+        }
+      else if (object->length != length)
+        {
+          /* This doesn't support resize */
+
+          ret = -EINVAL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_unlink
+ ****************************************************************************/
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode)
+{
+  if (inode->i_crefs <= 1)
+    {
+      delete_shm_object(inode->i_private);
+      inode->i_private = NULL;
+    }
+
+  return OK;
+}
+#endif
+
+/****************************************************************************
+ * Name: shmfs_map_object
+ ****************************************************************************/
+
+static int shmfs_map_object(FAR struct shmfs_object_s *object,
+                            FAR void **vaddr)
+{
+  int ret = OK;
+
+#ifdef CONFIG_BUILD_KERNEL
+  /* Map the physical pages of the shm object with MMU. */
+
+  FAR struct tcb_s        *tcb   = nxsched_self();
+  FAR struct task_group_s *group = tcb->group;
+  FAR uintptr_t *pages =
+    (FAR uintptr_t *)&object->paddr;
+  uintptr_t                mapaddr;
+  unsigned int             npages;
+
+  /* Find a free vaddr space that satisfies length */
+
+  mapaddr = (uintptr_t)vm_alloc_region(get_group_mm(group), 0,
+                                       object->length);
+  if (mapaddr == 0)
+    {
+      return -ENOMEM;
+    }
+
+  /* Convert the region size to pages */
+
+  npages = MM_NPAGES(object->length);
+
+  /* Map the memory to user virtual address space */
+
+  ret = up_shmat(pages, npages, mapaddr);
+  if (ret < 0)
+    {
+      vm_release_region(get_group_mm(group), (FAR void *)mapaddr,
+                        object->length);
+    }
+  else
+    {
+      *vaddr = (FAR void *)mapaddr;
+    }
+#else
+  /* Use the physical address directly */
+
+  *vaddr = object->paddr;
+#endif
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_mmap
+ ****************************************************************************/
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry)
+{
+  FAR struct shmfs_object_s *object;
+  int ret = -EINVAL;
+
+  DEBUGASSERT(filep->f_inode != NULL);
+
+  /* We don't support offset at the moment, just mapping the whole object */
+
+  if (entry->offset != 0)
+    {
+      return ret;
+    }
+
+  /* Keep the inode when mmapped, increase refcount */
+
+  ret = inode_addref(filep->f_inode);
+  if (ret >= 0)
+    {
+      object = (FAR struct shmfs_object_s *)filep->f_inode->i_private;
+
+      if (object)
+        {
+          ret = shmfs_map_object(object, &entry->vaddr);
+        }
+
+      if (!object || ret < 0)
+        {
+          /* Oops, the file has not been truncated yet or
+           * there was error in up_shmat
+           */
+
+          inode_release(filep->f_inode);
+        }
+      else
+        {
+          entry->munmap = shmfs_munmap;
+          entry->priv.p = (FAR void *)filep->f_inode;
+          mm_map_add(entry);
+        }
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_unmap_object
+ ****************************************************************************/
+
+static int shmfs_unmap_area(FAR struct task_group_s *group,

Review Comment:
   I did this part and I prefer to have the static method here. It abstracts the build type quite nicely IMO, same with the counterpart (map area).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072068941


##########
fs/shm/shm_unlink.c:
##########
@@ -0,0 +1,140 @@
+/****************************************************************************
+ * fs/shm/shm_unlink.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 <stdio.h>
+#include <assert.h>
+#include <errno.h>
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int file_shm_unlink(FAR const char *shm_name)
+{
+  FAR struct inode *inode;
+  struct inode_search_s desc;
+  char fullpath[sizeof(CONFIG_FS_SHMFS_VFS_PATH) + CONFIG_NAME_MAX + 2];
+  int ret;
+
+  /* Get the full path to the shm object */
+
+  snprintf(fullpath, sizeof(fullpath),
+           CONFIG_FS_SHMFS_VFS_PATH "/%s", shm_name);
+
+  /* Get the inode for this shm object */
+
+  SETUP_SEARCH(&desc, fullpath, false);
+
+  ret = inode_lock();
+  if (ret < 0)
+    {
+      goto errout_with_search;
+    }
+
+  ret = inode_find(&desc);
+  if (ret < 0)
+    {
+      /* There is no inode that includes in this path */
+
+      goto errout_with_sem;
+    }
+
+  /* Get the search results */
+
+  inode = desc.node;
+  DEBUGASSERT(inode != NULL);
+
+  /* Verify that what we found is, indeed, an shm inode */
+
+  if (!INODE_IS_SHM(inode))
+    {
+      ret = -ENOENT;
+      inode_release(inode);
+      goto errout_with_sem;
+    }
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  if (inode->u.i_ops->unlink)
+    {
+      /* Notify the shmfs driver that it has been unlinked */
+
+      ret = inode->u.i_ops->unlink(inode);
+      if (ret < 0)
+        {
+          goto errout_with_inode;
+        }
+    }
+#endif
+
+  /* Remove the old inode from the tree. If we hold a reference count
+   * on the inode, it will not be deleted now.  This will set the
+   * FSNODEFLAG_DELETED bit in the inode flags.
+   */
+
+  inode_release(inode);
+  ret = inode_remove(fullpath);
+
+  /* inode_remove() should always fail with -EBUSY because we have a
+   * reference on the inode.  -EBUSY means that the inode was, indeed,
+   * unlinked but it could not be freed because there are references.
+   */
+
+  if (ret == -EBUSY)
+    {
+      ret = OK;
+    }
+
+  DEBUGASSERT(ret == OK);
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+errout_with_inode:
+  inode_release(inode);
+#endif
+
+errout_with_sem:
+  inode_unlock();
+
+errout_with_search:
+  RELEASE_SEARCH(&desc);
+
+  return ret;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+int shm_unlink(FAR const char *shm_name)

Review Comment:
   done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] xiaoxiang781216 commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072103870


##########
fs/shm/shm_unlink.c:
##########
@@ -0,0 +1,140 @@
+/****************************************************************************
+ * fs/shm/shm_unlink.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 <stdio.h>
+#include <assert.h>
+#include <errno.h>
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int file_shm_unlink(FAR const char *shm_name)

Review Comment:
   If you want the kernel driver communicate with user space application through shared memory. file_shm_unlink and file_shm_open is must have since kernel components is forbidden to call shm_open/shm_unlink directly.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072482358


##########
fs/shm/shmfs_alloc.c:
##########
@@ -0,0 +1,143 @@
+/****************************************************************************
+ * fs/shm/shmfs_alloc.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 <stdlib.h>
+#include <nuttx/pgalloc.h>
+
+#include "inode/inode.h"
+#include "shm/shmfs.h"
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+FAR struct shmfs_object_s *shmfs_alloc_object(size_t length)
+{
+  FAR struct shmfs_object_s *object;
+  bool allocated = false;
+
+#if defined(CONFIG_BUILD_FLAT)
+  /* in FLAT build, allocate the object metadata and the data in the same
+   * chunk in kernel heap
+   */
+
+  object = kmm_zalloc(sizeof(struct shmfs_object_s) + length);
+  if (object)
+    {
+      object->paddr = (&object->paddr) + 1;
+      allocated = true;
+    }
+
+#elif defined(CONFIG_BUILD_PROTECTED)
+  /* in PROTECTED build, allocate the shm object in kernel heap, and shared
+   * memory in user heap
+   */
+
+  object = kmm_zalloc(sizeof(struct shmfs_object_s));
+  if (object)
+    {
+      object->paddr = kumm_zalloc(length);
+
+      if (object->paddr)
+        {
+           allocated = true;
+        }
+    }
+
+#elif defined(CONFIG_BUILD_KERNEL)
+  /* in KERNEL build, allocate the shared memory from page pool and store the
+   * physical address
+   */
+
+  size_t i = 0;
+  void **pages;
+  size_t n_pages = MM_NPAGES(length);
+
+  object = kmm_zalloc(sizeof(struct shmfs_object_s) +
+                      (n_pages - 1) * sizeof(object->paddr));
+
+  if (object)
+    {
+      pages = &object->paddr;
+      for (; i < n_pages; i++)
+        {
+          pages[i] = (void *)mm_pgalloc(1);
+          if (!pages[i])
+            {
+              break;
+            }
+        }
+    }
+
+  if (i == n_pages)
+    {
+      allocated = true;
+    }
+#endif
+
+  if (allocated)
+    {
+      object->length = length;
+    }
+  else
+    {
+      /* delete any partial allocation */
+
+      shmfs_free_object(object);
+      object = NULL;
+    }
+
+  return object;
+}
+
+void shmfs_free_object(FAR struct shmfs_object_s *object)
+{
+#if defined(CONFIG_BUILD_KERNEL)
+  size_t i;
+  size_t n_pages = MM_NPAGES(object->length);
+  void **pages;

Review Comment:
   done



##########
fs/shm/shmfs_alloc.c:
##########
@@ -0,0 +1,143 @@
+/****************************************************************************
+ * fs/shm/shmfs_alloc.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 <stdlib.h>
+#include <nuttx/pgalloc.h>
+
+#include "inode/inode.h"

Review Comment:
   done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] pussuw commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
pussuw commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1073201935


##########
fs/shm/shmfs.c:
##########
@@ -0,0 +1,384 @@
+/****************************************************************************
+ * fs/shm/shmfs.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 <assert.h>
+
+#include <nuttx/fs/ioctl.h>
+#include <nuttx/mm/map.h>
+
+#if defined (CONFIG_BUILD_KERNEL)
+#include <nuttx/arch.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
+#endif
+
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep);
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen);
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen);
+static int shmfs_truncate(FAR struct file *filep, off_t length);
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode);
+#endif
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry);
+static int shmfs_munmap(FAR struct task_group_s *group,
+                        FAR struct mm_map_entry_s *entry,
+                        FAR void *start,
+                        size_t length);
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+const struct file_operations shmfs_operations =
+{
+  NULL,             /* open */
+  shmfs_close,      /* close */
+  shmfs_read,       /* read */
+  shmfs_write,      /* write */
+  NULL,             /* seek */
+  NULL,             /* ioctl */
+  shmfs_mmap,       /* mmap */
+  shmfs_truncate,   /* truncate */
+  NULL,             /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  shmfs_unlink      /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: shmfs_read
+ ****************************************************************************/
+
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_write
+ ****************************************************************************/
+
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_release
+ ****************************************************************************/
+
+static int shmfs_release(FAR struct inode *inode)
+{
+  /* If the file has been unlinked previously, delete the contents.
+   * The inode is released after this call, hence checking if i_crefs <= 1.
+   */
+
+  int ret = inode_lock();
+  if (ret >= 0)
+    {
+      if (inode->i_parent == NULL &&
+          inode->i_crefs <= 1)
+        {
+          delete_shm_object(inode->i_private);
+          inode->i_private = NULL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_close
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep)
+{
+  /* Release the shmfs object. The object gets deleted if no-one has
+   * reference to it (either mmap or open file) and the object has been
+   * unlinked
+   */
+
+  return shmfs_release(filep->f_inode);
+}
+
+/****************************************************************************
+ * Name: shmfs_truncate
+ ****************************************************************************/
+
+static int shmfs_truncate(FAR struct file *filep, off_t length)
+{
+  FAR struct shmfs_object_s *object;
+  int ret;
+
+  if (length == 0)
+    {
+      return -EINVAL;
+    }
+
+  ret = inode_lock();
+  if (ret >= 0)
+    {
+      object = filep->f_inode->i_private;
+      if (!object)
+        {
+          filep->f_inode->i_private = alloc_shm_object(length);
+        }
+      else if (object->length != length)
+        {
+          /* This doesn't support resize */
+
+          ret = -EINVAL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_unlink
+ ****************************************************************************/
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode)
+{
+  if (inode->i_crefs <= 1)
+    {
+      delete_shm_object(inode->i_private);
+      inode->i_private = NULL;
+    }
+
+  return OK;
+}
+#endif
+
+/****************************************************************************
+ * Name: shmfs_map_object
+ ****************************************************************************/
+
+static int shmfs_map_object(FAR struct shmfs_object_s *object,
+                            FAR void **vaddr)
+{
+  int ret = OK;
+
+#ifdef CONFIG_BUILD_KERNEL
+  /* Map the physical pages of the shm object with MMU. */
+
+  FAR struct tcb_s        *tcb   = nxsched_self();
+  FAR struct task_group_s *group = tcb->group;
+  FAR uintptr_t *pages =
+    (FAR uintptr_t *)&object->paddr;
+  uintptr_t                mapaddr;
+  unsigned int             npages;
+
+  /* Find a free vaddr space that satisfies length */
+
+  mapaddr = (uintptr_t)vm_alloc_region(get_group_mm(group), 0,
+                                       object->length);
+  if (mapaddr == 0)
+    {
+      return -ENOMEM;
+    }
+
+  /* Convert the region size to pages */
+
+  npages = MM_NPAGES(object->length);
+
+  /* Map the memory to user virtual address space */
+
+  ret = up_shmat(pages, npages, mapaddr);
+  if (ret < 0)
+    {
+      vm_release_region(get_group_mm(group), (FAR void *)mapaddr,
+                        object->length);
+    }
+  else
+    {
+      *vaddr = (FAR void *)mapaddr;
+    }
+#else
+  /* Use the physical address directly */
+
+  *vaddr = object->paddr;
+#endif
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_mmap
+ ****************************************************************************/
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry)
+{
+  FAR struct shmfs_object_s *object;
+  int ret = -EINVAL;
+
+  DEBUGASSERT(filep->f_inode != NULL);
+
+  /* We don't support offset at the moment, just mapping the whole object */
+
+  if (entry->offset != 0)
+    {
+      return ret;
+    }
+
+  /* Keep the inode when mmapped, increase refcount */
+
+  ret = inode_addref(filep->f_inode);
+  if (ret >= 0)
+    {
+      object = (FAR struct shmfs_object_s *)filep->f_inode->i_private;
+
+      if (object)

Review Comment:
   Now I get it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072825950


##########
fs/shm/shmfs.c:
##########
@@ -0,0 +1,384 @@
+/****************************************************************************
+ * fs/shm/shmfs.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 <assert.h>
+
+#include <nuttx/fs/ioctl.h>
+#include <nuttx/mm/map.h>
+
+#if defined (CONFIG_BUILD_KERNEL)
+#include <nuttx/arch.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
+#endif
+
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep);
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen);
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen);
+static int shmfs_truncate(FAR struct file *filep, off_t length);
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode);
+#endif
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry);
+static int shmfs_munmap(FAR struct task_group_s *group,
+                        FAR struct mm_map_entry_s *entry,
+                        FAR void *start,
+                        size_t length);
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+const struct file_operations shmfs_operations =
+{
+  NULL,             /* open */
+  shmfs_close,      /* close */
+  shmfs_read,       /* read */
+  shmfs_write,      /* write */
+  NULL,             /* seek */
+  NULL,             /* ioctl */
+  shmfs_mmap,       /* mmap */
+  shmfs_truncate,   /* truncate */
+  NULL,             /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  shmfs_unlink      /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: shmfs_read
+ ****************************************************************************/
+
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_write
+ ****************************************************************************/
+
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_release
+ ****************************************************************************/
+
+static int shmfs_release(FAR struct inode *inode)
+{
+  /* If the file has been unlinked previously, delete the contents.
+   * The inode is released after this call, hence checking if i_crefs <= 1.
+   */
+
+  int ret = inode_lock();
+  if (ret >= 0)
+    {
+      if (inode->i_parent == NULL &&
+          inode->i_crefs <= 1)
+        {
+          delete_shm_object(inode->i_private);
+          inode->i_private = NULL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_close
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep)
+{
+  /* Release the shmfs object. The object gets deleted if no-one has
+   * reference to it (either mmap or open file) and the object has been
+   * unlinked
+   */
+
+  return shmfs_release(filep->f_inode);
+}
+
+/****************************************************************************
+ * Name: shmfs_truncate
+ ****************************************************************************/
+
+static int shmfs_truncate(FAR struct file *filep, off_t length)
+{
+  FAR struct shmfs_object_s *object;
+  int ret;
+
+  if (length == 0)
+    {
+      return -EINVAL;
+    }
+
+  ret = inode_lock();
+  if (ret >= 0)
+    {
+      object = filep->f_inode->i_private;
+      if (!object)
+        {
+          filep->f_inode->i_private = alloc_shm_object(length);
+        }
+      else if (object->length != length)
+        {
+          /* This doesn't support resize */
+
+          ret = -EINVAL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_unlink
+ ****************************************************************************/
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode)
+{
+  if (inode->i_crefs <= 1)
+    {
+      delete_shm_object(inode->i_private);
+      inode->i_private = NULL;
+    }
+
+  return OK;
+}
+#endif
+
+/****************************************************************************
+ * Name: shmfs_map_object
+ ****************************************************************************/
+
+static int shmfs_map_object(FAR struct shmfs_object_s *object,
+                            FAR void **vaddr)
+{
+  int ret = OK;
+
+#ifdef CONFIG_BUILD_KERNEL
+  /* Map the physical pages of the shm object with MMU. */
+
+  FAR struct tcb_s        *tcb   = nxsched_self();
+  FAR struct task_group_s *group = tcb->group;
+  FAR uintptr_t *pages =
+    (FAR uintptr_t *)&object->paddr;
+  uintptr_t                mapaddr;
+  unsigned int             npages;
+
+  /* Find a free vaddr space that satisfies length */
+
+  mapaddr = (uintptr_t)vm_alloc_region(get_group_mm(group), 0,
+                                       object->length);
+  if (mapaddr == 0)
+    {
+      return -ENOMEM;
+    }
+
+  /* Convert the region size to pages */
+
+  npages = MM_NPAGES(object->length);
+
+  /* Map the memory to user virtual address space */
+
+  ret = up_shmat(pages, npages, mapaddr);
+  if (ret < 0)
+    {
+      vm_release_region(get_group_mm(group), (FAR void *)mapaddr,
+                        object->length);
+    }
+  else
+    {
+      *vaddr = (FAR void *)mapaddr;
+    }
+#else
+  /* Use the physical address directly */
+
+  *vaddr = object->paddr;
+#endif
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_mmap
+ ****************************************************************************/
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry)
+{
+  FAR struct shmfs_object_s *object;
+  int ret = -EINVAL;
+
+  DEBUGASSERT(filep->f_inode != NULL);
+
+  /* We don't support offset at the moment, just mapping the whole object */
+
+  if (entry->offset != 0)
+    {
+      return ret;
+    }
+
+  /* Keep the inode when mmapped, increase refcount */
+
+  ret = inode_addref(filep->f_inode);
+  if (ret >= 0)
+    {
+      object = (FAR struct shmfs_object_s *)filep->f_inode->i_private;
+
+      if (object)

Review Comment:
   That's right! It may be bad application design, but it is possible to share file descriptors between threads in the same application. You can't rely on refcount on the open call only here. Of course the application wouldn't work, but it is better that mmap either succeeds or fails, and doesn't crash.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] pkarashchenko commented on pull request #8109: Add shmfs

Posted by "pkarashchenko (via GitHub)" <gi...@apache.org>.
pkarashchenko commented on PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#issuecomment-1404293102

   Adding breaking change label since Kconfig option was renamed


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] pkarashchenko commented on pull request #8109: Add shmfs

Posted by "pkarashchenko (via GitHub)" <gi...@apache.org>.
pkarashchenko commented on PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#issuecomment-1405621769

   > Is it a breaking change, when the previous Kconfig option was not implemented and not usable? It was merely a placeholder, no-one could have used it...
   > 
   > Petro Karashchenko kirjoitti torstai 26. tammikuuta 2023:
   > > Adding breaking change label since Kconfig option was renamed
   > > 
   > > -- 
   > > Reply to this email directly or view it on GitHub:
   > > https://github.com/apache/nuttx/pull/8109#issuecomment-1404293102
   > > You are receiving this because you were mentioned.
   > > 
   > > Message ID: ***@***.***
   
   I didn't dive deep enough to see that the initial option was not implemented. Just removed the label. Thank you for pointing this out.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] pkarashchenko commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
pkarashchenko commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1069548965


##########
fs/shm/shmfs_private.h:
##########
@@ -0,0 +1,60 @@
+/****************************************************************************
+ * fs/shm/shmfs_private.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_SHM_SHMFS_PRIVATE_H
+#define __FS_SHM_SHMFS_PRIVATE_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdlib.h>
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+extern const struct file_operations shmfs_operations;
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+struct shmfs_object_s
+{
+  /* Total number of bytes needed from physical memory. */
+
+  size_t length;
+
+  /* Vector of allocations from physical memory.
+   * - In flat and protected builds there is just one pointer to the
+   *   allocated memory in paddr[0].
+   * - In kernel build each address points to one page in page pool
+   *   and the length of the vector is MM_NPAGES(length)
+   */
+
+  FAR void *paddr[];

Review Comment:
   Can we rework to `[1]`, to get C89 compatibility?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1070659010


##########
boards/risc-v/mpfs/icicle/configs/knsh/defconfig:
##########
@@ -68,7 +69,6 @@ CONFIG_LIBM=y
 CONFIG_MEMSET_64BIT=y
 CONFIG_MEMSET_OPTSPEED=y
 CONFIG_MM_PGALLOC=y
-CONFIG_MM_SHM=y

Review Comment:
   Sure, although I don't need it (afaik we are the only users&developers of mpfs). It is an old if...



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1070233178


##########
fs/shm/shmfs_private.h:
##########
@@ -0,0 +1,60 @@
+/****************************************************************************
+ * fs/shm/shmfs_private.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_SHM_SHMFS_PRIVATE_H
+#define __FS_SHM_SHMFS_PRIVATE_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdlib.h>
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+extern const struct file_operations shmfs_operations;
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+struct shmfs_object_s
+{
+  /* Total number of bytes needed from physical memory. */
+
+  size_t length;
+
+  /* Vector of allocations from physical memory.
+   * - In flat and protected builds there is just one pointer to the
+   *   allocated memory in paddr[0].
+   * - In kernel build each address points to one page in page pool
+   *   and the length of the vector is MM_NPAGES(length)
+   */
+
+  FAR void *paddr[];

Review Comment:
   I sort of dislike the old "struct hack" style, so I just used direct void * instead
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#issuecomment-1382699500

   Managed to push some garbage, now it is ok again. Intent was to not support truncate to 0 yet


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072033164


##########
fs/shm/shm_open.c:
##########
@@ -0,0 +1,163 @@
+/****************************************************************************
+ * fs/shm/shm_open.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 <nuttx/config.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <nuttx/fs/fs.h>
+#include "inode/inode.h"
+#include "shm/shmfs.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+int file_shm_vopen(FAR struct file *shm, FAR const char *shm_name,
+                   int oflags, mode_t mode)
+{
+  FAR struct inode *inode;
+  struct inode_search_s desc;
+  char fullpath[sizeof(CONFIG_FS_SHMFS_VFS_PATH) + CONFIG_NAME_MAX + 2];
+  int ret;
+
+  /* Make sure that a non-NULL name is supplied */
+
+  if (!shm || !shm_name || *shm_name == '/' || *shm_name == '\0')

Review Comment:
   You are right! I changed this part.



##########
fs/shm/shm_open.c:
##########
@@ -0,0 +1,163 @@
+/****************************************************************************
+ * fs/shm/shm_open.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 <nuttx/config.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <nuttx/fs/fs.h>
+#include "inode/inode.h"
+#include "shm/shmfs.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+int file_shm_vopen(FAR struct file *shm, FAR const char *shm_name,
+                   int oflags, mode_t mode)
+{
+  FAR struct inode *inode;
+  struct inode_search_s desc;
+  char fullpath[sizeof(CONFIG_FS_SHMFS_VFS_PATH) + CONFIG_NAME_MAX + 2];
+  int ret;
+
+  /* Make sure that a non-NULL name is supplied */
+
+  if (!shm || !shm_name || *shm_name == '/' || *shm_name == '\0')
+    {
+      return -EINVAL;
+    }
+
+  /* Get the full path to the shm object */
+
+  snprintf(fullpath, sizeof(fullpath),
+           CONFIG_FS_SHMFS_VFS_PATH "/%s", shm_name);
+
+  /* Get the inode for this shm object */
+
+  SETUP_SEARCH(&desc, fullpath, false);
+
+  ret = inode_lock();
+  if (ret < 0)
+    {
+      goto errout_with_search;
+    }
+
+  ret = inode_find(&desc);
+  if (ret >= 0)
+    {
+      /* Something exists at this path.  Get the search results */
+
+      inode = desc.node;
+
+      /* Verify that the inode is an shm object */
+
+      if (!INODE_IS_SHM(inode))
+        {
+          ret = -EINVAL;
+          inode_release(inode);
+          goto errout_with_sem;
+        }
+
+      /* It exists and is an shm object.  Check if the caller wanted to
+       * create a new object with this name.
+       */
+
+      if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
+        {
+          ret = -EEXIST;
+          inode_release(inode);
+          goto errout_with_sem;
+        }
+    }
+  else
+    {
+      /* The shm does not exist. Were we asked to create it? */
+
+      if ((oflags & O_CREAT) == 0)
+        {
+          /* The shm does not exist and O_CREAT is not set */
+
+          ret = -ENOENT;
+          goto errout_with_sem;
+        }
+
+      /* Create an inode in the pseudo-filesystem at this path */
+
+      ret = inode_reserve(fullpath, mode, &inode);
+

Review Comment:
   done



##########
fs/shm/shmfs_alloc.c:
##########
@@ -0,0 +1,142 @@
+/****************************************************************************
+ * fs/shm/shmfs_alloc.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 <stdlib.h>
+#include <nuttx/pgalloc.h>

Review Comment:
   done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072032850


##########
fs/shm/shm_open.c:
##########
@@ -0,0 +1,163 @@
+/****************************************************************************
+ * fs/shm/shm_open.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 <nuttx/config.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <nuttx/fs/fs.h>
+#include "inode/inode.h"
+#include "shm/shmfs.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+int file_shm_vopen(FAR struct file *shm, FAR const char *shm_name,

Review Comment:
   changed the name as per your suggestion, but made it static (it is really just specific to shm_open, I don't see it as a nuttx internal interface).
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072065337


##########
fs/shm/shm_open.c:
##########
@@ -0,0 +1,163 @@
+/****************************************************************************
+ * fs/shm/shm_open.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 <nuttx/config.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <nuttx/fs/fs.h>
+#include "inode/inode.h"
+#include "shm/shmfs.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+int file_shm_vopen(FAR struct file *shm, FAR const char *shm_name,
+                   int oflags, mode_t mode)
+{
+  FAR struct inode *inode;
+  struct inode_search_s desc;
+  char fullpath[sizeof(CONFIG_FS_SHMFS_VFS_PATH) + CONFIG_NAME_MAX + 2];
+  int ret;
+
+  /* Make sure that a non-NULL name is supplied */
+
+  if (!shm || !shm_name || *shm_name == '/' || *shm_name == '\0')
+    {
+      return -EINVAL;
+    }
+
+  /* Get the full path to the shm object */
+
+  snprintf(fullpath, sizeof(fullpath),
+           CONFIG_FS_SHMFS_VFS_PATH "/%s", shm_name);
+
+  /* Get the inode for this shm object */
+
+  SETUP_SEARCH(&desc, fullpath, false);
+
+  ret = inode_lock();
+  if (ret < 0)
+    {
+      goto errout_with_search;
+    }
+
+  ret = inode_find(&desc);
+  if (ret >= 0)
+    {
+      /* Something exists at this path.  Get the search results */
+
+      inode = desc.node;
+
+      /* Verify that the inode is an shm object */
+
+      if (!INODE_IS_SHM(inode))
+        {
+          ret = -EINVAL;
+          inode_release(inode);
+          goto errout_with_sem;
+        }
+
+      /* It exists and is an shm object.  Check if the caller wanted to
+       * create a new object with this name.
+       */
+
+      if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
+        {
+          ret = -EEXIST;
+          inode_release(inode);
+          goto errout_with_sem;
+        }
+    }
+  else
+    {
+      /* The shm does not exist. Were we asked to create it? */
+
+      if ((oflags & O_CREAT) == 0)
+        {
+          /* The shm does not exist and O_CREAT is not set */
+
+          ret = -ENOENT;
+          goto errout_with_sem;
+        }
+
+      /* Create an inode in the pseudo-filesystem at this path */
+
+      ret = inode_reserve(fullpath, mode, &inode);
+
+      if (ret < 0)
+        {
+          goto errout_with_sem;
+        }
+
+      INODE_SET_SHM(inode);
+      inode->u.i_ops    = &shmfs_operations;
+      inode->i_private  = NULL;

Review Comment:
   done



##########
fs/shm/shm_open.c:
##########
@@ -0,0 +1,163 @@
+/****************************************************************************
+ * fs/shm/shm_open.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 <nuttx/config.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <nuttx/fs/fs.h>
+#include "inode/inode.h"
+#include "shm/shmfs.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+int file_shm_vopen(FAR struct file *shm, FAR const char *shm_name,
+                   int oflags, mode_t mode)
+{
+  FAR struct inode *inode;
+  struct inode_search_s desc;
+  char fullpath[sizeof(CONFIG_FS_SHMFS_VFS_PATH) + CONFIG_NAME_MAX + 2];
+  int ret;
+
+  /* Make sure that a non-NULL name is supplied */
+
+  if (!shm || !shm_name || *shm_name == '/' || *shm_name == '\0')
+    {
+      return -EINVAL;
+    }
+
+  /* Get the full path to the shm object */
+
+  snprintf(fullpath, sizeof(fullpath),
+           CONFIG_FS_SHMFS_VFS_PATH "/%s", shm_name);
+
+  /* Get the inode for this shm object */
+
+  SETUP_SEARCH(&desc, fullpath, false);
+
+  ret = inode_lock();
+  if (ret < 0)
+    {
+      goto errout_with_search;
+    }
+
+  ret = inode_find(&desc);
+  if (ret >= 0)
+    {
+      /* Something exists at this path.  Get the search results */
+
+      inode = desc.node;
+
+      /* Verify that the inode is an shm object */
+
+      if (!INODE_IS_SHM(inode))
+        {
+          ret = -EINVAL;
+          inode_release(inode);
+          goto errout_with_sem;
+        }
+
+      /* It exists and is an shm object.  Check if the caller wanted to
+       * create a new object with this name.
+       */
+
+      if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
+        {
+          ret = -EEXIST;
+          inode_release(inode);
+          goto errout_with_sem;
+        }
+    }
+  else
+    {
+      /* The shm does not exist. Were we asked to create it? */
+
+      if ((oflags & O_CREAT) == 0)
+        {
+          /* The shm does not exist and O_CREAT is not set */
+
+          ret = -ENOENT;
+          goto errout_with_sem;
+        }
+
+      /* Create an inode in the pseudo-filesystem at this path */
+
+      ret = inode_reserve(fullpath, mode, &inode);
+
+      if (ret < 0)
+        {
+          goto errout_with_sem;
+        }
+
+      INODE_SET_SHM(inode);
+      inode->u.i_ops    = &shmfs_operations;
+      inode->i_private  = NULL;
+      inode->i_crefs    = 1;
+    }
+
+  /* Associate the inode with a file structure */
+
+  shm->f_oflags  = oflags;
+  shm->f_pos     = 0;
+  shm->f_inode   = inode;
+  shm->f_priv    = NULL;
+  ret = OK;

Review Comment:
   done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#issuecomment-1385389207

   still added the filename checks & support for leading '/' to shm_unlink


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072867226


##########
fs/shm/shmfs.c:
##########
@@ -0,0 +1,384 @@
+/****************************************************************************
+ * fs/shm/shmfs.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 <assert.h>
+
+#include <nuttx/fs/ioctl.h>
+#include <nuttx/mm/map.h>
+
+#if defined (CONFIG_BUILD_KERNEL)
+#include <nuttx/arch.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
+#endif
+
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep);
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen);
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen);
+static int shmfs_truncate(FAR struct file *filep, off_t length);
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode);
+#endif
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry);
+static int shmfs_munmap(FAR struct task_group_s *group,
+                        FAR struct mm_map_entry_s *entry,
+                        FAR void *start,
+                        size_t length);
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+const struct file_operations shmfs_operations =
+{
+  NULL,             /* open */
+  shmfs_close,      /* close */
+  shmfs_read,       /* read */
+  shmfs_write,      /* write */
+  NULL,             /* seek */
+  NULL,             /* ioctl */
+  shmfs_mmap,       /* mmap */
+  shmfs_truncate,   /* truncate */
+  NULL,             /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  shmfs_unlink      /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: shmfs_read
+ ****************************************************************************/
+
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_write
+ ****************************************************************************/
+
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_release
+ ****************************************************************************/
+
+static int shmfs_release(FAR struct inode *inode)
+{
+  /* If the file has been unlinked previously, delete the contents.
+   * The inode is released after this call, hence checking if i_crefs <= 1.
+   */
+
+  int ret = inode_lock();
+  if (ret >= 0)
+    {
+      if (inode->i_parent == NULL &&
+          inode->i_crefs <= 1)
+        {
+          delete_shm_object(inode->i_private);
+          inode->i_private = NULL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_close
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep)
+{
+  /* Release the shmfs object. The object gets deleted if no-one has
+   * reference to it (either mmap or open file) and the object has been
+   * unlinked
+   */
+
+  return shmfs_release(filep->f_inode);
+}
+
+/****************************************************************************
+ * Name: shmfs_truncate
+ ****************************************************************************/
+
+static int shmfs_truncate(FAR struct file *filep, off_t length)
+{
+  FAR struct shmfs_object_s *object;
+  int ret;
+
+  if (length == 0)
+    {
+      return -EINVAL;
+    }
+
+  ret = inode_lock();
+  if (ret >= 0)
+    {
+      object = filep->f_inode->i_private;
+      if (!object)
+        {
+          filep->f_inode->i_private = alloc_shm_object(length);
+        }
+      else if (object->length != length)
+        {
+          /* This doesn't support resize */
+
+          ret = -EINVAL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_unlink
+ ****************************************************************************/
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode)
+{
+  if (inode->i_crefs <= 1)
+    {
+      delete_shm_object(inode->i_private);
+      inode->i_private = NULL;
+    }
+
+  return OK;
+}
+#endif
+
+/****************************************************************************
+ * Name: shmfs_map_object
+ ****************************************************************************/
+
+static int shmfs_map_object(FAR struct shmfs_object_s *object,
+                            FAR void **vaddr)
+{
+  int ret = OK;
+
+#ifdef CONFIG_BUILD_KERNEL
+  /* Map the physical pages of the shm object with MMU. */
+
+  FAR struct tcb_s        *tcb   = nxsched_self();
+  FAR struct task_group_s *group = tcb->group;
+  FAR uintptr_t *pages =
+    (FAR uintptr_t *)&object->paddr;
+  uintptr_t                mapaddr;
+  unsigned int             npages;
+
+  /* Find a free vaddr space that satisfies length */
+
+  mapaddr = (uintptr_t)vm_alloc_region(get_group_mm(group), 0,
+                                       object->length);
+  if (mapaddr == 0)
+    {
+      return -ENOMEM;
+    }
+
+  /* Convert the region size to pages */
+
+  npages = MM_NPAGES(object->length);
+
+  /* Map the memory to user virtual address space */
+
+  ret = up_shmat(pages, npages, mapaddr);
+  if (ret < 0)
+    {
+      vm_release_region(get_group_mm(group), (FAR void *)mapaddr,
+                        object->length);
+    }
+  else
+    {
+      *vaddr = (FAR void *)mapaddr;
+    }
+#else
+  /* Use the physical address directly */
+
+  *vaddr = object->paddr;
+#endif
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_mmap
+ ****************************************************************************/
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry)
+{
+  FAR struct shmfs_object_s *object;
+  int ret = -EINVAL;
+
+  DEBUGASSERT(filep->f_inode != NULL);
+
+  /* We don't support offset at the moment, just mapping the whole object */
+
+  if (entry->offset != 0)
+    {
+      return ret;
+    }
+
+  /* Keep the inode when mmapped, increase refcount */
+
+  ret = inode_addref(filep->f_inode);
+  if (ret >= 0)
+    {
+      object = (FAR struct shmfs_object_s *)filep->f_inode->i_private;
+
+      if (object)

Review Comment:
   yes of course. This wasn't about not bumping the refcounter in mmap at all, that you have to do always. It was just the place where you use the "object" ptr (inode->i_private). I think that is is supposed to be after refcount bump. otherwise crazy multithreaded application can close the fd in the middle of other thread doing mmap on it.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072867226


##########
fs/shm/shmfs.c:
##########
@@ -0,0 +1,384 @@
+/****************************************************************************
+ * fs/shm/shmfs.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 <assert.h>
+
+#include <nuttx/fs/ioctl.h>
+#include <nuttx/mm/map.h>
+
+#if defined (CONFIG_BUILD_KERNEL)
+#include <nuttx/arch.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
+#endif
+
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep);
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen);
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen);
+static int shmfs_truncate(FAR struct file *filep, off_t length);
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode);
+#endif
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry);
+static int shmfs_munmap(FAR struct task_group_s *group,
+                        FAR struct mm_map_entry_s *entry,
+                        FAR void *start,
+                        size_t length);
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+const struct file_operations shmfs_operations =
+{
+  NULL,             /* open */
+  shmfs_close,      /* close */
+  shmfs_read,       /* read */
+  shmfs_write,      /* write */
+  NULL,             /* seek */
+  NULL,             /* ioctl */
+  shmfs_mmap,       /* mmap */
+  shmfs_truncate,   /* truncate */
+  NULL,             /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  shmfs_unlink      /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: shmfs_read
+ ****************************************************************************/
+
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_write
+ ****************************************************************************/
+
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_release
+ ****************************************************************************/
+
+static int shmfs_release(FAR struct inode *inode)
+{
+  /* If the file has been unlinked previously, delete the contents.
+   * The inode is released after this call, hence checking if i_crefs <= 1.
+   */
+
+  int ret = inode_lock();
+  if (ret >= 0)
+    {
+      if (inode->i_parent == NULL &&
+          inode->i_crefs <= 1)
+        {
+          delete_shm_object(inode->i_private);
+          inode->i_private = NULL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_close
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep)
+{
+  /* Release the shmfs object. The object gets deleted if no-one has
+   * reference to it (either mmap or open file) and the object has been
+   * unlinked
+   */
+
+  return shmfs_release(filep->f_inode);
+}
+
+/****************************************************************************
+ * Name: shmfs_truncate
+ ****************************************************************************/
+
+static int shmfs_truncate(FAR struct file *filep, off_t length)
+{
+  FAR struct shmfs_object_s *object;
+  int ret;
+
+  if (length == 0)
+    {
+      return -EINVAL;
+    }
+
+  ret = inode_lock();
+  if (ret >= 0)
+    {
+      object = filep->f_inode->i_private;
+      if (!object)
+        {
+          filep->f_inode->i_private = alloc_shm_object(length);
+        }
+      else if (object->length != length)
+        {
+          /* This doesn't support resize */
+
+          ret = -EINVAL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_unlink
+ ****************************************************************************/
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode)
+{
+  if (inode->i_crefs <= 1)
+    {
+      delete_shm_object(inode->i_private);
+      inode->i_private = NULL;
+    }
+
+  return OK;
+}
+#endif
+
+/****************************************************************************
+ * Name: shmfs_map_object
+ ****************************************************************************/
+
+static int shmfs_map_object(FAR struct shmfs_object_s *object,
+                            FAR void **vaddr)
+{
+  int ret = OK;
+
+#ifdef CONFIG_BUILD_KERNEL
+  /* Map the physical pages of the shm object with MMU. */
+
+  FAR struct tcb_s        *tcb   = nxsched_self();
+  FAR struct task_group_s *group = tcb->group;
+  FAR uintptr_t *pages =
+    (FAR uintptr_t *)&object->paddr;
+  uintptr_t                mapaddr;
+  unsigned int             npages;
+
+  /* Find a free vaddr space that satisfies length */
+
+  mapaddr = (uintptr_t)vm_alloc_region(get_group_mm(group), 0,
+                                       object->length);
+  if (mapaddr == 0)
+    {
+      return -ENOMEM;
+    }
+
+  /* Convert the region size to pages */
+
+  npages = MM_NPAGES(object->length);
+
+  /* Map the memory to user virtual address space */
+
+  ret = up_shmat(pages, npages, mapaddr);
+  if (ret < 0)
+    {
+      vm_release_region(get_group_mm(group), (FAR void *)mapaddr,
+                        object->length);
+    }
+  else
+    {
+      *vaddr = (FAR void *)mapaddr;
+    }
+#else
+  /* Use the physical address directly */
+
+  *vaddr = object->paddr;
+#endif
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_mmap
+ ****************************************************************************/
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry)
+{
+  FAR struct shmfs_object_s *object;
+  int ret = -EINVAL;
+
+  DEBUGASSERT(filep->f_inode != NULL);
+
+  /* We don't support offset at the moment, just mapping the whole object */
+
+  if (entry->offset != 0)
+    {
+      return ret;
+    }
+
+  /* Keep the inode when mmapped, increase refcount */
+
+  ret = inode_addref(filep->f_inode);
+  if (ret >= 0)
+    {
+      object = (FAR struct shmfs_object_s *)filep->f_inode->i_private;
+
+      if (object)

Review Comment:
   yes of course. This wasn't about not bumping the refcounter in mmap at all, it was just the place where you use the "object" ptr (inode->i_private). I think that is is supposed to be after refcount bump. otherwise crazy multithreaded application can close the fd in the middle of other thread doing mmap on it.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] pussuw commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
pussuw commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072880816


##########
fs/shm/shmfs.c:
##########
@@ -0,0 +1,377 @@
+/****************************************************************************
+ * fs/shm/shmfs.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 <assert.h>
+
+#include <nuttx/fs/ioctl.h>
+#include <nuttx/mm/map.h>
+
+#if defined (CONFIG_BUILD_KERNEL)
+#include <nuttx/arch.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
+#endif
+
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep);
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen);
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen);
+static int shmfs_truncate(FAR struct file *filep, off_t length);
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode);
+#endif
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry);
+static int shmfs_munmap(FAR struct task_group_s *group,
+                        FAR struct mm_map_entry_s *entry,
+                        FAR void *start,
+                        size_t length);
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+const struct file_operations shmfs_operations =
+{
+  NULL,             /* open */
+  shmfs_close,      /* close */
+  shmfs_read,       /* read */
+  shmfs_write,      /* write */
+  NULL,             /* seek */
+  NULL,             /* ioctl */
+  shmfs_mmap,       /* mmap */
+  shmfs_truncate,   /* truncate */
+  NULL,             /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  shmfs_unlink      /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: shmfs_read
+ ****************************************************************************/
+
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_write
+ ****************************************************************************/
+
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_release
+ ****************************************************************************/
+
+static int shmfs_release(FAR struct inode *inode)
+{
+  /* If the file has been unlinked previously, delete the contents.
+   * The inode is released after this call, hence checking if i_crefs <= 1.
+   */
+
+  int ret = inode_lock();
+  if (ret >= 0)
+    {
+      if (inode->i_parent == NULL &&
+          inode->i_crefs <= 1)
+        {
+          shmfs_free_object(inode->i_private);
+          inode->i_private = NULL;
+          ret = OK;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_close
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep)
+{
+  /* Release the shmfs object. The object gets deleted if no-one has
+   * reference to it (either mmap or open file) and the object has been
+   * unlinked
+   */
+
+  return shmfs_release(filep->f_inode);
+}
+
+/****************************************************************************
+ * Name: shmfs_truncate
+ ****************************************************************************/
+
+static int shmfs_truncate(FAR struct file *filep, off_t length)
+{
+  FAR struct shmfs_object_s *object;
+  int ret;
+
+  if (length == 0)
+    {
+      return -EINVAL;
+    }
+
+  ret = inode_lock();
+  if (ret >= 0)
+    {
+      object = filep->f_inode->i_private;
+      if (!object)
+        {
+          filep->f_inode->i_private = shmfs_alloc_object(length);
+          if (!filep->f_inode->i_private)
+            {
+              ret = -EFAULT;
+            }
+        }
+      else if (object->length != length)
+        {
+          /* This doesn't support resize */
+
+          ret = -EINVAL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_unlink
+ ****************************************************************************/
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode)
+{
+  int ret = inode_lock();
+
+  if (ret >= 0)
+    {
+      if (inode->i_crefs <= 1)
+        {
+          shmfs_free_object(inode->i_private);
+          inode->i_private = NULL;
+          ret = OK;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+#endif
+
+/****************************************************************************
+ * Name: shmfs_map_object
+ ****************************************************************************/
+
+static int shmfs_map_object(FAR struct shmfs_object_s *object,
+                            FAR void **vaddr)
+{
+  int ret = OK;
+
+#ifdef CONFIG_BUILD_KERNEL
+  /* Map the physical pages of the shm object with MMU. */
+
+  FAR struct tcb_s        *tcb   = nxsched_self();
+  FAR struct task_group_s *group = tcb->group;
+  FAR uintptr_t *pages =

Review Comment:
   I agree it was better to just remove the alignment stuff here as no other place uses it any more



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] xiaoxiang781216 commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1070638526


##########
fs/shm/shmfs.c:
##########
@@ -0,0 +1,384 @@
+/****************************************************************************
+ * fs/shm/shmfs.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 <assert.h>
+
+#include <nuttx/fs/ioctl.h>
+#include <nuttx/mm/map.h>
+
+#if defined (CONFIG_BUILD_KERNEL)
+#include <nuttx/arch.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
+#endif
+
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep);
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen);
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen);
+static int shmfs_truncate(FAR struct file *filep, off_t length);
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode);
+#endif
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry);
+static int shmfs_munmap(FAR struct task_group_s *group,
+                        FAR struct mm_map_entry_s *entry,
+                        FAR void *start,
+                        size_t length);
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+const struct file_operations shmfs_operations =
+{
+  NULL,             /* open */
+  shmfs_close,      /* close */
+  shmfs_read,       /* read */
+  shmfs_write,      /* write */
+  NULL,             /* seek */
+  NULL,             /* ioctl */
+  shmfs_mmap,       /* mmap */
+  shmfs_truncate,   /* truncate */
+  NULL,             /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  shmfs_unlink      /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: shmfs_read
+ ****************************************************************************/
+
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_write
+ ****************************************************************************/
+
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_release
+ ****************************************************************************/
+
+static int shmfs_release(FAR struct inode *inode)
+{
+  /* If the file has been unlinked previously, delete the contents.
+   * The inode is released after this call, hence checking if i_crefs <= 1.
+   */
+
+  int ret = inode_lock();
+  if (ret >= 0)
+    {
+      if (inode->i_parent == NULL &&
+          inode->i_crefs <= 1)
+        {
+          delete_shm_object(inode->i_private);
+          inode->i_private = NULL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_close
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep)
+{
+  /* Release the shmfs object. The object gets deleted if no-one has
+   * reference to it (either mmap or open file) and the object has been
+   * unlinked
+   */
+
+  return shmfs_release(filep->f_inode);
+}
+
+/****************************************************************************
+ * Name: shmfs_truncate
+ ****************************************************************************/
+
+static int shmfs_truncate(FAR struct file *filep, off_t length)
+{
+  FAR struct shmfs_object_s *object;
+  int ret;
+
+  if (length == 0)
+    {
+      return -EINVAL;
+    }
+
+  ret = inode_lock();
+  if (ret >= 0)
+    {
+      object = filep->f_inode->i_private;
+      if (!object)
+        {
+          filep->f_inode->i_private = alloc_shm_object(length);
+        }
+      else if (object->length != length)
+        {
+          /* This doesn't support resize */
+
+          ret = -EINVAL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_unlink
+ ****************************************************************************/
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode)
+{
+  if (inode->i_crefs <= 1)
+    {
+      delete_shm_object(inode->i_private);
+      inode->i_private = NULL;
+    }
+
+  return OK;
+}
+#endif
+
+/****************************************************************************
+ * Name: shmfs_map_object
+ ****************************************************************************/
+
+static int shmfs_map_object(FAR struct shmfs_object_s *object,
+                            FAR void **vaddr)
+{
+  int ret = OK;
+
+#ifdef CONFIG_BUILD_KERNEL
+  /* Map the physical pages of the shm object with MMU. */
+
+  FAR struct tcb_s        *tcb   = nxsched_self();
+  FAR struct task_group_s *group = tcb->group;
+  FAR uintptr_t *pages =
+    (FAR uintptr_t *)&object->paddr;
+  uintptr_t                mapaddr;
+  unsigned int             npages;
+
+  /* Find a free vaddr space that satisfies length */
+
+  mapaddr = (uintptr_t)vm_alloc_region(get_group_mm(group), 0,
+                                       object->length);
+  if (mapaddr == 0)
+    {
+      return -ENOMEM;
+    }
+
+  /* Convert the region size to pages */
+
+  npages = MM_NPAGES(object->length);
+
+  /* Map the memory to user virtual address space */
+
+  ret = up_shmat(pages, npages, mapaddr);
+  if (ret < 0)
+    {
+      vm_release_region(get_group_mm(group), (FAR void *)mapaddr,
+                        object->length);
+    }
+  else
+    {
+      *vaddr = (FAR void *)mapaddr;
+    }
+#else
+  /* Use the physical address directly */
+
+  *vaddr = object->paddr;
+#endif
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_mmap
+ ****************************************************************************/
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry)
+{
+  FAR struct shmfs_object_s *object;
+  int ret = -EINVAL;
+
+  DEBUGASSERT(filep->f_inode != NULL);
+
+  /* We don't support offset at the moment, just mapping the whole object */
+
+  if (entry->offset != 0)
+    {
+      return ret;
+    }
+
+  /* Keep the inode when mmapped, increase refcount */
+
+  ret = inode_addref(filep->f_inode);
+  if (ret >= 0)
+    {
+      object = (FAR struct shmfs_object_s *)filep->f_inode->i_private;
+
+      if (object)

Review Comment:
   move the check before line 271 and return fail directly



##########
fs/shm/shmfs.h:
##########
@@ -0,0 +1,72 @@
+/****************************************************************************
+ * fs/shm/shmfs.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_SHM_SHMFS_H
+#define __FS_SHM_SHMFS_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdlib.h>
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+extern const struct file_operations shmfs_operations;
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+struct shmfs_object_s
+{
+  /* Total number of bytes needed from physical memory. */
+
+  size_t length;
+
+  /* Vector of allocations from physical memory.
+   *
+   * - In flat and protected builds this is a pointer to the
+   *   allocated memory.
+   *
+   * - In kernel build this is start of a malloc'd vector of void pointers
+   *   and the length of the vector is MM_NPAGES(length).
+   */
+
+  FAR void *paddr;
+
+  /* The struct continues here as malloc'd array of void pointers
+   * if CONFIG_BUILD_KERNEL
+   */
+};
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+FAR struct shmfs_object_s *alloc_shm_object(size_t length);

Review Comment:
   shm_alloc_object



##########
fs/shm/Kconfig:
##########
@@ -3,16 +3,16 @@
 # see the file kconfig-language.txt in the NuttX tools repository.
 #
 
-config FS_SHM
+config FS_SHMFS

Review Comment:
   can we keep FS_SHM as before?



##########
fs/shm/shmfs.h:
##########
@@ -0,0 +1,72 @@
+/****************************************************************************
+ * fs/shm/shmfs.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_SHM_SHMFS_H
+#define __FS_SHM_SHMFS_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdlib.h>
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+extern const struct file_operations shmfs_operations;
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+struct shmfs_object_s

Review Comment:
   shm_object_s



##########
fs/shm/shmfs.h:
##########
@@ -0,0 +1,72 @@
+/****************************************************************************
+ * fs/shm/shmfs.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_SHM_SHMFS_H
+#define __FS_SHM_SHMFS_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdlib.h>
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+extern const struct file_operations shmfs_operations;
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+struct shmfs_object_s
+{
+  /* Total number of bytes needed from physical memory. */
+
+  size_t length;
+
+  /* Vector of allocations from physical memory.
+   *
+   * - In flat and protected builds this is a pointer to the
+   *   allocated memory.
+   *
+   * - In kernel build this is start of a malloc'd vector of void pointers
+   *   and the length of the vector is MM_NPAGES(length).
+   */
+
+  FAR void *paddr;
+
+  /* The struct continues here as malloc'd array of void pointers
+   * if CONFIG_BUILD_KERNEL
+   */
+};
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+FAR struct shmfs_object_s *alloc_shm_object(size_t length);
+
+void delete_shm_object(FAR struct shmfs_object_s *object);

Review Comment:
   shm_free_object



##########
fs/shm/shmfs.c:
##########
@@ -0,0 +1,384 @@
+/****************************************************************************
+ * fs/shm/shmfs.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 <assert.h>
+
+#include <nuttx/fs/ioctl.h>
+#include <nuttx/mm/map.h>
+
+#if defined (CONFIG_BUILD_KERNEL)
+#include <nuttx/arch.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
+#endif
+
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep);
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen);
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen);
+static int shmfs_truncate(FAR struct file *filep, off_t length);
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode);
+#endif
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry);
+static int shmfs_munmap(FAR struct task_group_s *group,
+                        FAR struct mm_map_entry_s *entry,
+                        FAR void *start,
+                        size_t length);
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+const struct file_operations shmfs_operations =
+{
+  NULL,             /* open */
+  shmfs_close,      /* close */
+  shmfs_read,       /* read */
+  shmfs_write,      /* write */
+  NULL,             /* seek */
+  NULL,             /* ioctl */
+  shmfs_mmap,       /* mmap */
+  shmfs_truncate,   /* truncate */
+  NULL,             /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  shmfs_unlink      /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: shmfs_read
+ ****************************************************************************/
+
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_write
+ ****************************************************************************/
+
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_release
+ ****************************************************************************/
+
+static int shmfs_release(FAR struct inode *inode)
+{
+  /* If the file has been unlinked previously, delete the contents.
+   * The inode is released after this call, hence checking if i_crefs <= 1.
+   */
+
+  int ret = inode_lock();
+  if (ret >= 0)
+    {
+      if (inode->i_parent == NULL &&
+          inode->i_crefs <= 1)
+        {
+          delete_shm_object(inode->i_private);
+          inode->i_private = NULL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_close
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep)
+{
+  /* Release the shmfs object. The object gets deleted if no-one has
+   * reference to it (either mmap or open file) and the object has been
+   * unlinked
+   */
+
+  return shmfs_release(filep->f_inode);
+}
+
+/****************************************************************************
+ * Name: shmfs_truncate
+ ****************************************************************************/
+
+static int shmfs_truncate(FAR struct file *filep, off_t length)
+{
+  FAR struct shmfs_object_s *object;
+  int ret;
+
+  if (length == 0)
+    {
+      return -EINVAL;
+    }
+
+  ret = inode_lock();
+  if (ret >= 0)
+    {
+      object = filep->f_inode->i_private;
+      if (!object)
+        {
+          filep->f_inode->i_private = alloc_shm_object(length);
+        }
+      else if (object->length != length)
+        {
+          /* This doesn't support resize */
+
+          ret = -EINVAL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_unlink
+ ****************************************************************************/
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode)
+{
+  if (inode->i_crefs <= 1)
+    {
+      delete_shm_object(inode->i_private);
+      inode->i_private = NULL;
+    }
+
+  return OK;
+}
+#endif
+
+/****************************************************************************
+ * Name: shmfs_map_object
+ ****************************************************************************/
+
+static int shmfs_map_object(FAR struct shmfs_object_s *object,
+                            FAR void **vaddr)
+{
+  int ret = OK;
+
+#ifdef CONFIG_BUILD_KERNEL
+  /* Map the physical pages of the shm object with MMU. */
+
+  FAR struct tcb_s        *tcb   = nxsched_self();
+  FAR struct task_group_s *group = tcb->group;
+  FAR uintptr_t *pages =
+    (FAR uintptr_t *)&object->paddr;
+  uintptr_t                mapaddr;
+  unsigned int             npages;
+
+  /* Find a free vaddr space that satisfies length */
+
+  mapaddr = (uintptr_t)vm_alloc_region(get_group_mm(group), 0,
+                                       object->length);
+  if (mapaddr == 0)
+    {
+      return -ENOMEM;
+    }
+
+  /* Convert the region size to pages */
+
+  npages = MM_NPAGES(object->length);
+
+  /* Map the memory to user virtual address space */
+
+  ret = up_shmat(pages, npages, mapaddr);
+  if (ret < 0)
+    {
+      vm_release_region(get_group_mm(group), (FAR void *)mapaddr,
+                        object->length);
+    }
+  else
+    {
+      *vaddr = (FAR void *)mapaddr;
+    }
+#else
+  /* Use the physical address directly */
+
+  *vaddr = object->paddr;
+#endif
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_mmap
+ ****************************************************************************/
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry)
+{
+  FAR struct shmfs_object_s *object;
+  int ret = -EINVAL;
+
+  DEBUGASSERT(filep->f_inode != NULL);
+
+  /* We don't support offset at the moment, just mapping the whole object */
+
+  if (entry->offset != 0)
+    {
+      return ret;
+    }
+
+  /* Keep the inode when mmapped, increase refcount */
+
+  ret = inode_addref(filep->f_inode);
+  if (ret >= 0)
+    {
+      object = (FAR struct shmfs_object_s *)filep->f_inode->i_private;
+
+      if (object)
+        {
+          ret = shmfs_map_object(object, &entry->vaddr);
+        }
+
+      if (!object || ret < 0)
+        {
+          /* Oops, the file has not been truncated yet or
+           * there was error in up_shmat
+           */
+
+          inode_release(filep->f_inode);
+        }
+      else
+        {
+          entry->munmap = shmfs_munmap;
+          entry->priv.p = (FAR void *)filep->f_inode;
+          mm_map_add(entry);
+        }
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_unmap_object
+ ****************************************************************************/
+
+static int shmfs_unmap_area(FAR struct task_group_s *group,

Review Comment:
   merge into shmfs_munmap?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] xiaoxiang781216 commented on pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#issuecomment-1383446231

   > Most of the comments provided here are just making things look different, not better. Also commented parts are not violating any coding standard, nuttx inviolables nor they are bugs. I'll come back to this PR if I have time again at some point later this week. Right now it seems that now many of the comments are just disrespectful pushing around.
   
   Some comment is just a suggestion. I think your major concern is shmfs v.s. shm, it's fine to keep shmfs for all function/struct/file except shm_open/shm_unlink.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] xiaoxiang781216 commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1070815043


##########
fs/shm/shm_open.c:
##########
@@ -0,0 +1,163 @@
+/****************************************************************************
+ * fs/shm/shm_open.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 <nuttx/config.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <nuttx/fs/fs.h>
+#include "inode/inode.h"
+#include "shm/shmfs.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+int file_shm_vopen(FAR struct file *shm, FAR const char *shm_name,
+                   int oflags, mode_t mode)
+{
+  FAR struct inode *inode;
+  struct inode_search_s desc;
+  char fullpath[sizeof(CONFIG_FS_SHMFS_VFS_PATH) + CONFIG_NAME_MAX + 2];
+  int ret;
+
+  /* Make sure that a non-NULL name is supplied */
+
+  if (!shm || !shm_name || *shm_name == '/' || *shm_name == '\0')

Review Comment:
   No, the spec said that name can start with '/’:
   https://pubs.opengroup.org/onlinepubs/007904875/functions/shm_open.html



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#issuecomment-1382693320

   fixed truncate to size 0


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#issuecomment-1381896895

   re-based against master due to some CI error which I didn't understand. The error was on esp32s3-devkit/cxx
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072867226


##########
fs/shm/shmfs.c:
##########
@@ -0,0 +1,384 @@
+/****************************************************************************
+ * fs/shm/shmfs.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 <assert.h>
+
+#include <nuttx/fs/ioctl.h>
+#include <nuttx/mm/map.h>
+
+#if defined (CONFIG_BUILD_KERNEL)
+#include <nuttx/arch.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
+#endif
+
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep);
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen);
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen);
+static int shmfs_truncate(FAR struct file *filep, off_t length);
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode);
+#endif
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry);
+static int shmfs_munmap(FAR struct task_group_s *group,
+                        FAR struct mm_map_entry_s *entry,
+                        FAR void *start,
+                        size_t length);
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+const struct file_operations shmfs_operations =
+{
+  NULL,             /* open */
+  shmfs_close,      /* close */
+  shmfs_read,       /* read */
+  shmfs_write,      /* write */
+  NULL,             /* seek */
+  NULL,             /* ioctl */
+  shmfs_mmap,       /* mmap */
+  shmfs_truncate,   /* truncate */
+  NULL,             /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  shmfs_unlink      /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: shmfs_read
+ ****************************************************************************/
+
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_write
+ ****************************************************************************/
+
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_release
+ ****************************************************************************/
+
+static int shmfs_release(FAR struct inode *inode)
+{
+  /* If the file has been unlinked previously, delete the contents.
+   * The inode is released after this call, hence checking if i_crefs <= 1.
+   */
+
+  int ret = inode_lock();
+  if (ret >= 0)
+    {
+      if (inode->i_parent == NULL &&
+          inode->i_crefs <= 1)
+        {
+          delete_shm_object(inode->i_private);
+          inode->i_private = NULL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_close
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep)
+{
+  /* Release the shmfs object. The object gets deleted if no-one has
+   * reference to it (either mmap or open file) and the object has been
+   * unlinked
+   */
+
+  return shmfs_release(filep->f_inode);
+}
+
+/****************************************************************************
+ * Name: shmfs_truncate
+ ****************************************************************************/
+
+static int shmfs_truncate(FAR struct file *filep, off_t length)
+{
+  FAR struct shmfs_object_s *object;
+  int ret;
+
+  if (length == 0)
+    {
+      return -EINVAL;
+    }
+
+  ret = inode_lock();
+  if (ret >= 0)
+    {
+      object = filep->f_inode->i_private;
+      if (!object)
+        {
+          filep->f_inode->i_private = alloc_shm_object(length);
+        }
+      else if (object->length != length)
+        {
+          /* This doesn't support resize */
+
+          ret = -EINVAL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_unlink
+ ****************************************************************************/
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode)
+{
+  if (inode->i_crefs <= 1)
+    {
+      delete_shm_object(inode->i_private);
+      inode->i_private = NULL;
+    }
+
+  return OK;
+}
+#endif
+
+/****************************************************************************
+ * Name: shmfs_map_object
+ ****************************************************************************/
+
+static int shmfs_map_object(FAR struct shmfs_object_s *object,
+                            FAR void **vaddr)
+{
+  int ret = OK;
+
+#ifdef CONFIG_BUILD_KERNEL
+  /* Map the physical pages of the shm object with MMU. */
+
+  FAR struct tcb_s        *tcb   = nxsched_self();
+  FAR struct task_group_s *group = tcb->group;
+  FAR uintptr_t *pages =
+    (FAR uintptr_t *)&object->paddr;
+  uintptr_t                mapaddr;
+  unsigned int             npages;
+
+  /* Find a free vaddr space that satisfies length */
+
+  mapaddr = (uintptr_t)vm_alloc_region(get_group_mm(group), 0,
+                                       object->length);
+  if (mapaddr == 0)
+    {
+      return -ENOMEM;
+    }
+
+  /* Convert the region size to pages */
+
+  npages = MM_NPAGES(object->length);
+
+  /* Map the memory to user virtual address space */
+
+  ret = up_shmat(pages, npages, mapaddr);
+  if (ret < 0)
+    {
+      vm_release_region(get_group_mm(group), (FAR void *)mapaddr,
+                        object->length);
+    }
+  else
+    {
+      *vaddr = (FAR void *)mapaddr;
+    }
+#else
+  /* Use the physical address directly */
+
+  *vaddr = object->paddr;
+#endif
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_mmap
+ ****************************************************************************/
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry)
+{
+  FAR struct shmfs_object_s *object;
+  int ret = -EINVAL;
+
+  DEBUGASSERT(filep->f_inode != NULL);
+
+  /* We don't support offset at the moment, just mapping the whole object */
+
+  if (entry->offset != 0)
+    {
+      return ret;
+    }
+
+  /* Keep the inode when mmapped, increase refcount */
+
+  ret = inode_addref(filep->f_inode);
+  if (ret >= 0)
+    {
+      object = (FAR struct shmfs_object_s *)filep->f_inode->i_private;
+
+      if (object)

Review Comment:
   yes of course. This wasn't about not bumping the refcounter in mmap at all, that you have to do always. It was just the place where you use the "object" ptr (inode->i_private). I think that is is supposed to be after refcount bump. otherwise crazy multithreaded application can close & unlink the fd in the middle of other thread doing mmap on it. A bit far fetched maybe, but better safe than sorry.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072039034


##########
fs/shm/shmfs.c:
##########
@@ -0,0 +1,384 @@
+/****************************************************************************
+ * fs/shm/shmfs.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 <assert.h>
+
+#include <nuttx/fs/ioctl.h>
+#include <nuttx/mm/map.h>
+
+#if defined (CONFIG_BUILD_KERNEL)
+#include <nuttx/arch.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
+#endif
+
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep);
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen);
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen);
+static int shmfs_truncate(FAR struct file *filep, off_t length);
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode);
+#endif
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry);
+static int shmfs_munmap(FAR struct task_group_s *group,
+                        FAR struct mm_map_entry_s *entry,
+                        FAR void *start,
+                        size_t length);
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+const struct file_operations shmfs_operations =
+{
+  NULL,             /* open */
+  shmfs_close,      /* close */
+  shmfs_read,       /* read */
+  shmfs_write,      /* write */
+  NULL,             /* seek */
+  NULL,             /* ioctl */
+  shmfs_mmap,       /* mmap */
+  shmfs_truncate,   /* truncate */
+  NULL,             /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  shmfs_unlink      /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: shmfs_read
+ ****************************************************************************/
+
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_write
+ ****************************************************************************/
+
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_release
+ ****************************************************************************/
+
+static int shmfs_release(FAR struct inode *inode)
+{
+  /* If the file has been unlinked previously, delete the contents.
+   * The inode is released after this call, hence checking if i_crefs <= 1.
+   */
+
+  int ret = inode_lock();
+  if (ret >= 0)
+    {
+      if (inode->i_parent == NULL &&
+          inode->i_crefs <= 1)
+        {
+          delete_shm_object(inode->i_private);
+          inode->i_private = NULL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_close
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep)
+{
+  /* Release the shmfs object. The object gets deleted if no-one has
+   * reference to it (either mmap or open file) and the object has been
+   * unlinked
+   */
+
+  return shmfs_release(filep->f_inode);
+}
+
+/****************************************************************************
+ * Name: shmfs_truncate
+ ****************************************************************************/
+
+static int shmfs_truncate(FAR struct file *filep, off_t length)
+{
+  FAR struct shmfs_object_s *object;
+  int ret;
+
+  if (length == 0)
+    {
+      return -EINVAL;
+    }
+
+  ret = inode_lock();
+  if (ret >= 0)
+    {
+      object = filep->f_inode->i_private;
+      if (!object)
+        {
+          filep->f_inode->i_private = alloc_shm_object(length);
+        }
+      else if (object->length != length)
+        {
+          /* This doesn't support resize */
+
+          ret = -EINVAL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_unlink
+ ****************************************************************************/
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode)
+{
+  if (inode->i_crefs <= 1)
+    {
+      delete_shm_object(inode->i_private);
+      inode->i_private = NULL;
+    }
+
+  return OK;
+}
+#endif
+
+/****************************************************************************
+ * Name: shmfs_map_object
+ ****************************************************************************/
+
+static int shmfs_map_object(FAR struct shmfs_object_s *object,
+                            FAR void **vaddr)
+{
+  int ret = OK;
+
+#ifdef CONFIG_BUILD_KERNEL
+  /* Map the physical pages of the shm object with MMU. */
+
+  FAR struct tcb_s        *tcb   = nxsched_self();
+  FAR struct task_group_s *group = tcb->group;
+  FAR uintptr_t *pages =
+    (FAR uintptr_t *)&object->paddr;
+  uintptr_t                mapaddr;
+  unsigned int             npages;
+
+  /* Find a free vaddr space that satisfies length */
+
+  mapaddr = (uintptr_t)vm_alloc_region(get_group_mm(group), 0,
+                                       object->length);
+  if (mapaddr == 0)
+    {
+      return -ENOMEM;
+    }
+
+  /* Convert the region size to pages */
+
+  npages = MM_NPAGES(object->length);
+
+  /* Map the memory to user virtual address space */
+
+  ret = up_shmat(pages, npages, mapaddr);
+  if (ret < 0)
+    {
+      vm_release_region(get_group_mm(group), (FAR void *)mapaddr,
+                        object->length);
+    }
+  else
+    {
+      *vaddr = (FAR void *)mapaddr;
+    }
+#else
+  /* Use the physical address directly */
+
+  *vaddr = object->paddr;
+#endif
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_mmap
+ ****************************************************************************/
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry)
+{
+  FAR struct shmfs_object_s *object;
+  int ret = -EINVAL;
+
+  DEBUGASSERT(filep->f_inode != NULL);
+
+  /* We don't support offset at the moment, just mapping the whole object */
+
+  if (entry->offset != 0)
+    {
+      return ret;
+    }
+
+  /* Keep the inode when mmapped, increase refcount */
+
+  ret = inode_addref(filep->f_inode);
+  if (ret >= 0)
+    {
+      object = (FAR struct shmfs_object_s *)filep->f_inode->i_private;
+
+      if (object)
+        {
+          ret = shmfs_map_object(object, &entry->vaddr);
+        }
+
+      if (!object || ret < 0)
+        {
+          /* Oops, the file has not been truncated yet or
+           * there was error in up_shmat
+           */
+
+          inode_release(filep->f_inode);
+        }
+      else
+        {
+          entry->munmap = shmfs_munmap;
+          entry->priv.p = (FAR void *)filep->f_inode;
+          mm_map_add(entry);
+        }
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_unmap_object
+ ****************************************************************************/
+
+static int shmfs_unmap_area(FAR struct task_group_s *group,

Review Comment:
   I liked to have this separate since it is CONFIG_BUILD_KERNEL specific, so it was nicer to have it in it's own function



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072036139


##########
fs/shm/shmfs.h:
##########
@@ -0,0 +1,72 @@
+/****************************************************************************
+ * fs/shm/shmfs.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_SHM_SHMFS_H
+#define __FS_SHM_SHMFS_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdlib.h>
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+extern const struct file_operations shmfs_operations;
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+struct shmfs_object_s
+{
+  /* Total number of bytes needed from physical memory. */
+
+  size_t length;
+
+  /* Vector of allocations from physical memory.
+   *
+   * - In flat and protected builds this is a pointer to the
+   *   allocated memory.
+   *
+   * - In kernel build this is start of a malloc'd vector of void pointers
+   *   and the length of the vector is MM_NPAGES(length).
+   */
+
+  FAR void *paddr;
+
+  /* The struct continues here as malloc'd array of void pointers
+   * if CONFIG_BUILD_KERNEL
+   */
+};
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+FAR struct shmfs_object_s *alloc_shm_object(size_t length);
+
+void delete_shm_object(FAR struct shmfs_object_s *object);

Review Comment:
   done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072037285


##########
fs/shm/shmfs.h:
##########
@@ -0,0 +1,72 @@
+/****************************************************************************
+ * fs/shm/shmfs.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_SHM_SHMFS_H
+#define __FS_SHM_SHMFS_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdlib.h>
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+extern const struct file_operations shmfs_operations;
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+struct shmfs_object_s

Review Comment:
   I think this object is just private data type for shmfs, so kept it as shmfs_object. To extend it to be something for wider scope would require also specifying the interfaces for managing these objects differently...



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072067295


##########
fs/shm/shm_unlink.c:
##########
@@ -0,0 +1,140 @@
+/****************************************************************************
+ * fs/shm/shm_unlink.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 <stdio.h>
+#include <assert.h>
+#include <errno.h>
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int file_shm_unlink(FAR const char *shm_name)
+{
+  FAR struct inode *inode;
+  struct inode_search_s desc;
+  char fullpath[sizeof(CONFIG_FS_SHMFS_VFS_PATH) + CONFIG_NAME_MAX + 2];
+  int ret;
+
+  /* Get the full path to the shm object */
+
+  snprintf(fullpath, sizeof(fullpath),
+           CONFIG_FS_SHMFS_VFS_PATH "/%s", shm_name);
+
+  /* Get the inode for this shm object */
+
+  SETUP_SEARCH(&desc, fullpath, false);
+
+  ret = inode_lock();
+  if (ret < 0)
+    {
+      goto errout_with_search;
+    }
+
+  ret = inode_find(&desc);
+  if (ret < 0)
+    {
+      /* There is no inode that includes in this path */
+
+      goto errout_with_sem;
+    }
+
+  /* Get the search results */
+
+  inode = desc.node;
+  DEBUGASSERT(inode != NULL);
+
+  /* Verify that what we found is, indeed, an shm inode */
+
+  if (!INODE_IS_SHM(inode))
+    {
+      ret = -ENOENT;
+      inode_release(inode);
+      goto errout_with_sem;
+    }
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  if (inode->u.i_ops->unlink)
+    {
+      /* Notify the shmfs driver that it has been unlinked */
+
+      ret = inode->u.i_ops->unlink(inode);
+      if (ret < 0)
+        {
+          goto errout_with_inode;
+        }
+    }
+#endif
+
+  /* Remove the old inode from the tree. If we hold a reference count
+   * on the inode, it will not be deleted now.  This will set the
+   * FSNODEFLAG_DELETED bit in the inode flags.
+   */
+
+  inode_release(inode);
+  ret = inode_remove(fullpath);
+
+  /* inode_remove() should always fail with -EBUSY because we have a
+   * reference on the inode.  -EBUSY means that the inode was, indeed,
+   * unlinked but it could not be freed because there are references.
+   */
+
+  if (ret == -EBUSY)
+    {
+      ret = OK;
+    }
+
+  DEBUGASSERT(ret == OK);
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+errout_with_inode:
+  inode_release(inode);
+#endif
+
+errout_with_sem:
+  inode_unlock();
+
+errout_with_search:
+  RELEASE_SEARCH(&desc);
+
+  return ret;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+int shm_unlink(FAR const char *shm_name)
+{
+  int ret = file_shm_unlink(shm_name);
+
+  if (ret < 0)
+    {
+      set_errno(ENOENT);

Review Comment:
   done, this was a good catch. the errnos were lost. Btw. I prefer just using "return -1" where the standard says that the function should return -1. I find it easier to read than "ERROR". I think ERROR is more nuttx internal.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072504423


##########
fs/shm/shmfs.c:
##########
@@ -0,0 +1,380 @@
+/****************************************************************************
+ * fs/shm/shmfs.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 <assert.h>
+
+#include <nuttx/fs/ioctl.h>
+#include <nuttx/mm/map.h>
+
+#if defined (CONFIG_BUILD_KERNEL)
+#include <nuttx/arch.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
+#endif
+
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep);
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen);
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen);
+static int shmfs_truncate(FAR struct file *filep, off_t length);
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode);
+#endif
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry);
+static int shmfs_munmap(FAR struct task_group_s *group,
+                        FAR struct mm_map_entry_s *entry,
+                        FAR void *start,
+                        size_t length);
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+const struct file_operations shmfs_operations =
+{
+  NULL,             /* open */
+  shmfs_close,      /* close */
+  shmfs_read,       /* read */
+  shmfs_write,      /* write */
+  NULL,             /* seek */
+  NULL,             /* ioctl */
+  shmfs_mmap,       /* mmap */
+  shmfs_truncate,   /* truncate */
+  NULL,             /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  shmfs_unlink      /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: shmfs_read
+ ****************************************************************************/
+
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_write
+ ****************************************************************************/
+
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_release
+ ****************************************************************************/
+
+static int shmfs_release(FAR struct inode *inode)
+{
+  /* If the file has been unlinked previously, delete the contents.
+   * The inode is released after this call, hence checking if i_crefs <= 1.
+   */
+
+  int ret = inode_lock();
+  if (ret >= 0)
+    {
+      if (inode->i_parent == NULL &&

Review Comment:
   this is a check if the inode is already unlnked



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072843164


##########
fs/shm/shmfs.c:
##########
@@ -0,0 +1,384 @@
+/****************************************************************************
+ * fs/shm/shmfs.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 <assert.h>
+
+#include <nuttx/fs/ioctl.h>
+#include <nuttx/mm/map.h>
+
+#if defined (CONFIG_BUILD_KERNEL)
+#include <nuttx/arch.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
+#endif
+
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep);
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen);
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen);
+static int shmfs_truncate(FAR struct file *filep, off_t length);
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode);
+#endif
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry);
+static int shmfs_munmap(FAR struct task_group_s *group,
+                        FAR struct mm_map_entry_s *entry,
+                        FAR void *start,
+                        size_t length);
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+const struct file_operations shmfs_operations =
+{
+  NULL,             /* open */
+  shmfs_close,      /* close */
+  shmfs_read,       /* read */
+  shmfs_write,      /* write */
+  NULL,             /* seek */
+  NULL,             /* ioctl */
+  shmfs_mmap,       /* mmap */
+  shmfs_truncate,   /* truncate */
+  NULL,             /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  shmfs_unlink      /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: shmfs_read
+ ****************************************************************************/
+
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_write
+ ****************************************************************************/
+
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_release
+ ****************************************************************************/
+
+static int shmfs_release(FAR struct inode *inode)
+{
+  /* If the file has been unlinked previously, delete the contents.
+   * The inode is released after this call, hence checking if i_crefs <= 1.
+   */
+
+  int ret = inode_lock();
+  if (ret >= 0)
+    {
+      if (inode->i_parent == NULL &&
+          inode->i_crefs <= 1)
+        {
+          delete_shm_object(inode->i_private);
+          inode->i_private = NULL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_close
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep)
+{
+  /* Release the shmfs object. The object gets deleted if no-one has
+   * reference to it (either mmap or open file) and the object has been
+   * unlinked
+   */
+
+  return shmfs_release(filep->f_inode);
+}
+
+/****************************************************************************
+ * Name: shmfs_truncate
+ ****************************************************************************/
+
+static int shmfs_truncate(FAR struct file *filep, off_t length)
+{
+  FAR struct shmfs_object_s *object;
+  int ret;
+
+  if (length == 0)
+    {
+      return -EINVAL;
+    }
+
+  ret = inode_lock();
+  if (ret >= 0)
+    {
+      object = filep->f_inode->i_private;
+      if (!object)
+        {
+          filep->f_inode->i_private = alloc_shm_object(length);
+        }
+      else if (object->length != length)
+        {
+          /* This doesn't support resize */
+
+          ret = -EINVAL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_unlink
+ ****************************************************************************/
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode)
+{
+  if (inode->i_crefs <= 1)
+    {
+      delete_shm_object(inode->i_private);
+      inode->i_private = NULL;
+    }
+
+  return OK;
+}
+#endif
+
+/****************************************************************************
+ * Name: shmfs_map_object
+ ****************************************************************************/
+
+static int shmfs_map_object(FAR struct shmfs_object_s *object,
+                            FAR void **vaddr)
+{
+  int ret = OK;
+
+#ifdef CONFIG_BUILD_KERNEL
+  /* Map the physical pages of the shm object with MMU. */
+
+  FAR struct tcb_s        *tcb   = nxsched_self();
+  FAR struct task_group_s *group = tcb->group;
+  FAR uintptr_t *pages =
+    (FAR uintptr_t *)&object->paddr;
+  uintptr_t                mapaddr;
+  unsigned int             npages;
+
+  /* Find a free vaddr space that satisfies length */
+
+  mapaddr = (uintptr_t)vm_alloc_region(get_group_mm(group), 0,
+                                       object->length);
+  if (mapaddr == 0)
+    {
+      return -ENOMEM;
+    }
+
+  /* Convert the region size to pages */
+
+  npages = MM_NPAGES(object->length);
+
+  /* Map the memory to user virtual address space */
+
+  ret = up_shmat(pages, npages, mapaddr);
+  if (ret < 0)
+    {
+      vm_release_region(get_group_mm(group), (FAR void *)mapaddr,
+                        object->length);
+    }
+  else
+    {
+      *vaddr = (FAR void *)mapaddr;
+    }
+#else
+  /* Use the physical address directly */
+
+  *vaddr = object->paddr;
+#endif
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_mmap
+ ****************************************************************************/
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry)
+{
+  FAR struct shmfs_object_s *object;
+  int ret = -EINVAL;
+
+  DEBUGASSERT(filep->f_inode != NULL);
+
+  /* We don't support offset at the moment, just mapping the whole object */
+
+  if (entry->offset != 0)
+    {
+      return ret;
+    }
+
+  /* Keep the inode when mmapped, increase refcount */
+
+  ret = inode_addref(filep->f_inode);
+  if (ret >= 0)
+    {
+      object = (FAR struct shmfs_object_s *)filep->f_inode->i_private;
+
+      if (object)

Review Comment:
   reverted the earlier change, now object is back inside the refcount bump



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072839724


##########
fs/shm/shmfs_alloc.c:
##########
@@ -0,0 +1,143 @@
+/****************************************************************************
+ * fs/shm/shmfs_alloc.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 <stdlib.h>
+#include <nuttx/pgalloc.h>
+
+#include "inode/inode.h"
+#include "shm/shmfs.h"
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+FAR struct shmfs_object_s *shmfs_alloc_object(size_t length)
+{
+  FAR struct shmfs_object_s *object;
+  bool allocated = false;
+
+#if defined(CONFIG_BUILD_FLAT)
+  /* in FLAT build, allocate the object metadata and the data in the same
+   * chunk in kernel heap
+   */
+
+  object = kmm_zalloc(sizeof(struct shmfs_object_s) + length);
+  if (object)
+    {
+      object->paddr = (&object->paddr) + 1;
+      allocated = true;
+    }
+
+#elif defined(CONFIG_BUILD_PROTECTED)
+  /* in PROTECTED build, allocate the shm object in kernel heap, and shared
+   * memory in user heap
+   */
+
+  object = kmm_zalloc(sizeof(struct shmfs_object_s));
+  if (object)
+    {
+      object->paddr = kumm_zalloc(length);
+
+      if (object->paddr)
+        {
+           allocated = true;
+        }
+    }
+
+#elif defined(CONFIG_BUILD_KERNEL)
+  /* in KERNEL build, allocate the shared memory from page pool and store the
+   * physical address
+   */
+
+  size_t i = 0;
+  void **pages;
+  size_t n_pages = MM_NPAGES(length);
+
+  object = kmm_zalloc(sizeof(struct shmfs_object_s) +
+                      (n_pages - 1) * sizeof(object->paddr));
+
+  if (object)
+    {
+      pages = &object->paddr;
+      for (; i < n_pages; i++)
+        {
+          pages[i] = (void *)mm_pgalloc(1);
+          if (!pages[i])
+            {
+              break;
+            }
+        }
+    }
+
+  if (i == n_pages)
+    {
+      allocated = true;
+    }
+#endif
+
+  if (allocated)
+    {
+      object->length = length;
+    }
+  else
+    {
+      /* delete any partial allocation */
+
+      shmfs_free_object(object);
+      object = NULL;
+    }
+
+  return object;
+}
+
+void shmfs_free_object(FAR struct shmfs_object_s *object)
+{
+#if defined(CONFIG_BUILD_KERNEL)
+  size_t i;
+  size_t n_pages = MM_NPAGES(object->length);
+  void **pages;

Review Comment:
   Sorry, confused by two places. Now it is done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] xiaoxiang781216 commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1070815407


##########
fs/shm/shmfs.h:
##########
@@ -0,0 +1,72 @@
+/****************************************************************************
+ * fs/shm/shmfs.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_SHM_SHMFS_H
+#define __FS_SHM_SHMFS_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdlib.h>
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+extern const struct file_operations shmfs_operations;
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+struct shmfs_object_s
+{
+  /* Total number of bytes needed from physical memory. */
+
+  size_t length;
+
+  /* Vector of allocations from physical memory.
+   *
+   * - In flat and protected builds this is a pointer to the
+   *   allocated memory.
+   *
+   * - In kernel build this is start of a malloc'd vector of void pointers
+   *   and the length of the vector is MM_NPAGES(length).
+   */
+
+  FAR void *paddr;
+
+  /* The struct continues here as malloc'd array of void pointers
+   * if CONFIG_BUILD_KERNEL
+   */
+};
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+FAR struct shmfs_object_s *alloc_shm_object(size_t length);
+
+void delete_shm_object(FAR struct shmfs_object_s *object);

Review Comment:
   le's change to shmfs_free_object



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] xiaoxiang781216 commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072103870


##########
fs/shm/shm_unlink.c:
##########
@@ -0,0 +1,140 @@
+/****************************************************************************
+ * fs/shm/shm_unlink.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 <stdio.h>
+#include <assert.h>
+#include <errno.h>
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int file_shm_unlink(FAR const char *shm_name)

Review Comment:
   If you want the kernel driver communicate with user space application through shared memory. file_shm_unlink and file_shm_open is must harve since kernel components is forbidden to call shm_open/shm_unlink directly.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072505556


##########
fs/shm/shm_unlink.c:
##########
@@ -0,0 +1,167 @@
+/****************************************************************************
+ * fs/shm/shm_unlink.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 <stdio.h>
+#include <assert.h>
+#include <errno.h>
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int file_shm_unlink(FAR const char *name)
+{
+  FAR struct inode *inode;
+  struct inode_search_s desc;
+  char fullpath[sizeof(CONFIG_FS_SHMFS_VFS_PATH) + CONFIG_NAME_MAX + 2];
+  int ret;
+
+  /* Make sure that a non-NULL name is supplied */
+
+  if (!name)
+    {
+      return -ENOENT;
+    }
+
+  /* Remove any number of leading '/' */
+
+  while (*name == '/')

Review Comment:
   if the user passes "/" or "////" as a filename, this makes the handling proper afterwards, checking that the name is empty



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] xiaoxiang781216 commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072349047


##########
fs/shm/shm_open.c:
##########
@@ -0,0 +1,181 @@
+/****************************************************************************
+ * fs/shm/shm_open.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 <nuttx/config.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <errno.h>
+
+#include "inode/inode.h"
+#include "shm/shmfs.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int file_shm_open(FAR struct file *shm, FAR const char *name,
+                         int oflags, mode_t mode)
+{
+  FAR struct inode *inode;
+  struct inode_search_s desc;
+  char fullpath[sizeof(CONFIG_FS_SHMFS_VFS_PATH) + CONFIG_NAME_MAX + 2];
+  int ret;
+
+  /* Make sure that a non-NULL name is supplied */
+
+  if (!shm || !name)
+    {
+      return -EINVAL;
+    }
+
+  /* Remove any number of leading '/' */
+
+  while (*name == '/')
+    {
+      name++;
+    }
+
+  /* Empty name supplied? */
+
+  if (*name == '\0')
+    {
+      return -EINVAL;
+    }
+
+  /* Name too long? */
+
+  if (strnlen(name, CONFIG_NAME_MAX) > CONFIG_NAME_MAX)

Review Comment:
   is it possilbe to > CONFIG_NAME_MAX with strnlen?



##########
fs/shm/shmfs_alloc.c:
##########
@@ -0,0 +1,143 @@
+/****************************************************************************
+ * fs/shm/shmfs_alloc.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 <stdlib.h>
+#include <nuttx/pgalloc.h>
+
+#include "inode/inode.h"
+#include "shm/shmfs.h"
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+FAR struct shmfs_object_s *shmfs_alloc_object(size_t length)
+{
+  FAR struct shmfs_object_s *object;
+  bool allocated = false;
+
+#if defined(CONFIG_BUILD_FLAT)
+  /* in FLAT build, allocate the object metadata and the data in the same
+   * chunk in kernel heap
+   */
+
+  object = kmm_zalloc(sizeof(struct shmfs_object_s) + length);
+  if (object)
+    {
+      object->paddr = (&object->paddr) + 1;

Review Comment:
    object->paddr = object->paddr + 1;



##########
fs/shm/shmfs_alloc.c:
##########
@@ -0,0 +1,143 @@
+/****************************************************************************
+ * fs/shm/shmfs_alloc.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 <stdlib.h>
+#include <nuttx/pgalloc.h>
+
+#include "inode/inode.h"

Review Comment:
   remove inode.h



##########
fs/shm/shm_unlink.c:
##########
@@ -0,0 +1,167 @@
+/****************************************************************************
+ * fs/shm/shm_unlink.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 <stdio.h>
+#include <assert.h>
+#include <errno.h>
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int file_shm_unlink(FAR const char *name)
+{
+  FAR struct inode *inode;
+  struct inode_search_s desc;
+  char fullpath[sizeof(CONFIG_FS_SHMFS_VFS_PATH) + CONFIG_NAME_MAX + 2];
+  int ret;
+
+  /* Make sure that a non-NULL name is supplied */
+
+  if (!name)
+    {
+      return -ENOENT;
+    }
+
+  /* Remove any number of leading '/' */
+
+  while (*name == '/')

Review Comment:
   don't need inode_nextname already handle this.



##########
fs/shm/shm_open.c:
##########
@@ -0,0 +1,181 @@
+/****************************************************************************
+ * fs/shm/shm_open.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 <nuttx/config.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <errno.h>
+
+#include "inode/inode.h"
+#include "shm/shmfs.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int file_shm_open(FAR struct file *shm, FAR const char *name,
+                         int oflags, mode_t mode)
+{
+  FAR struct inode *inode;
+  struct inode_search_s desc;
+  char fullpath[sizeof(CONFIG_FS_SHMFS_VFS_PATH) + CONFIG_NAME_MAX + 2];
+  int ret;
+
+  /* Make sure that a non-NULL name is supplied */
+
+  if (!shm || !name)
+    {
+      return -EINVAL;
+    }
+
+  /* Remove any number of leading '/' */
+
+  while (*name == '/')

Review Comment:
   don't need inode_nextname already handle this.



##########
fs/shm/shm_unlink.c:
##########
@@ -0,0 +1,167 @@
+/****************************************************************************
+ * fs/shm/shm_unlink.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 <stdio.h>
+#include <assert.h>
+#include <errno.h>
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int file_shm_unlink(FAR const char *name)
+{
+  FAR struct inode *inode;
+  struct inode_search_s desc;
+  char fullpath[sizeof(CONFIG_FS_SHMFS_VFS_PATH) + CONFIG_NAME_MAX + 2];
+  int ret;
+
+  /* Make sure that a non-NULL name is supplied */
+
+  if (!name)
+    {
+      return -ENOENT;
+    }
+
+  /* Remove any number of leading '/' */
+
+  while (*name == '/')
+    {
+      name++;
+    }
+
+  /* Empty name supplied? */
+
+  if (*name == '\0')
+    {
+      return -ENOENT;
+    }
+
+  /* Name too long? */
+
+  if (strnlen(name, CONFIG_NAME_MAX) > CONFIG_NAME_MAX)
+    {
+      return -ENAMETOOLONG;
+    }
+
+  /* Get the full path to the shm object */
+
+  snprintf(fullpath, sizeof(fullpath),
+           CONFIG_FS_SHMFS_VFS_PATH "/%s", name);
+
+  /* Get the inode for this shm object */
+
+  SETUP_SEARCH(&desc, fullpath, false);
+
+  ret = inode_lock();
+  if (ret < 0)
+    {
+      goto errout_with_search;
+    }
+
+  ret = inode_find(&desc);
+  if (ret < 0)
+    {
+      /* There is no inode that includes in this path */
+
+      goto errout_with_sem;
+    }
+
+  /* Get the search results */
+
+  inode = desc.node;
+  DEBUGASSERT(inode != NULL);
+
+  /* Verify that what we found is, indeed, an shm inode */
+
+  if (!INODE_IS_SHM(inode))
+    {
+      ret = -ENOENT;
+      inode_release(inode);
+      goto errout_with_sem;
+    }
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  if (inode->u.i_ops->unlink)
+    {
+      /* Notify the shmfs driver that it has been unlinked */
+
+      ret = inode->u.i_ops->unlink(inode);
+      if (ret < 0)
+        {
+          goto errout_with_inode;
+        }
+    }
+#endif
+
+  /* Remove the old inode from the tree. If we hold a reference count
+   * on the inode, it will not be deleted now.  This will set the
+   * FSNODEFLAG_DELETED bit in the inode flags.
+   */
+
+  inode_release(inode);

Review Comment:
   double free at line 142



##########
fs/shm/shmfs_alloc.c:
##########
@@ -0,0 +1,143 @@
+/****************************************************************************
+ * fs/shm/shmfs_alloc.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 <stdlib.h>
+#include <nuttx/pgalloc.h>
+
+#include "inode/inode.h"
+#include "shm/shmfs.h"
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+FAR struct shmfs_object_s *shmfs_alloc_object(size_t length)
+{
+  FAR struct shmfs_object_s *object;
+  bool allocated = false;
+
+#if defined(CONFIG_BUILD_FLAT)
+  /* in FLAT build, allocate the object metadata and the data in the same
+   * chunk in kernel heap
+   */
+
+  object = kmm_zalloc(sizeof(struct shmfs_object_s) + length);
+  if (object)
+    {
+      object->paddr = (&object->paddr) + 1;
+      allocated = true;
+    }
+
+#elif defined(CONFIG_BUILD_PROTECTED)
+  /* in PROTECTED build, allocate the shm object in kernel heap, and shared
+   * memory in user heap
+   */
+
+  object = kmm_zalloc(sizeof(struct shmfs_object_s));
+  if (object)
+    {
+      object->paddr = kumm_zalloc(length);
+
+      if (object->paddr)
+        {
+           allocated = true;
+        }
+    }
+
+#elif defined(CONFIG_BUILD_KERNEL)
+  /* in KERNEL build, allocate the shared memory from page pool and store the
+   * physical address
+   */
+
+  size_t i = 0;
+  void **pages;
+  size_t n_pages = MM_NPAGES(length);
+
+  object = kmm_zalloc(sizeof(struct shmfs_object_s) +
+                      (n_pages - 1) * sizeof(object->paddr));
+
+  if (object)
+    {
+      pages = &object->paddr;
+      for (; i < n_pages; i++)
+        {
+          pages[i] = (void *)mm_pgalloc(1);

Review Comment:
   FAR



##########
fs/shm/shm_unlink.c:
##########
@@ -0,0 +1,167 @@
+/****************************************************************************
+ * fs/shm/shm_unlink.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 <stdio.h>
+#include <assert.h>
+#include <errno.h>
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int file_shm_unlink(FAR const char *name)
+{
+  FAR struct inode *inode;
+  struct inode_search_s desc;
+  char fullpath[sizeof(CONFIG_FS_SHMFS_VFS_PATH) + CONFIG_NAME_MAX + 2];
+  int ret;
+
+  /* Make sure that a non-NULL name is supplied */
+
+  if (!name)
+    {
+      return -ENOENT;
+    }
+
+  /* Remove any number of leading '/' */
+
+  while (*name == '/')
+    {
+      name++;
+    }
+
+  /* Empty name supplied? */
+
+  if (*name == '\0')
+    {
+      return -ENOENT;
+    }
+
+  /* Name too long? */
+
+  if (strnlen(name, CONFIG_NAME_MAX) > CONFIG_NAME_MAX)

Review Comment:
   ditto



##########
fs/shm/shmfs.c:
##########
@@ -0,0 +1,380 @@
+/****************************************************************************
+ * fs/shm/shmfs.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 <assert.h>
+
+#include <nuttx/fs/ioctl.h>
+#include <nuttx/mm/map.h>
+
+#if defined (CONFIG_BUILD_KERNEL)
+#include <nuttx/arch.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
+#endif
+
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep);
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen);
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen);
+static int shmfs_truncate(FAR struct file *filep, off_t length);
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode);
+#endif
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry);
+static int shmfs_munmap(FAR struct task_group_s *group,
+                        FAR struct mm_map_entry_s *entry,
+                        FAR void *start,
+                        size_t length);
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+const struct file_operations shmfs_operations =
+{
+  NULL,             /* open */
+  shmfs_close,      /* close */
+  shmfs_read,       /* read */
+  shmfs_write,      /* write */
+  NULL,             /* seek */
+  NULL,             /* ioctl */
+  shmfs_mmap,       /* mmap */
+  shmfs_truncate,   /* truncate */
+  NULL,             /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  shmfs_unlink      /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: shmfs_read
+ ****************************************************************************/
+
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_write
+ ****************************************************************************/
+
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_release
+ ****************************************************************************/
+
+static int shmfs_release(FAR struct inode *inode)
+{
+  /* If the file has been unlinked previously, delete the contents.
+   * The inode is released after this call, hence checking if i_crefs <= 1.
+   */
+
+  int ret = inode_lock();
+  if (ret >= 0)
+    {
+      if (inode->i_parent == NULL &&
+          inode->i_crefs <= 1)
+        {
+          shmfs_free_object(inode->i_private);
+          inode->i_private = NULL;
+          ret = OK;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_close
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep)
+{
+  /* Release the shmfs object. The object gets deleted if no-one has
+   * reference to it (either mmap or open file) and the object has been
+   * unlinked
+   */
+
+  return shmfs_release(filep->f_inode);
+}
+
+/****************************************************************************
+ * Name: shmfs_truncate
+ ****************************************************************************/
+
+static int shmfs_truncate(FAR struct file *filep, off_t length)
+{
+  FAR struct shmfs_object_s *object;
+  int ret;
+
+  if (length == 0)
+    {
+      return -EINVAL;
+    }
+
+  ret = inode_lock();
+  if (ret >= 0)
+    {
+      object = filep->f_inode->i_private;
+      if (!object)
+        {
+          filep->f_inode->i_private = shmfs_alloc_object(length);

Review Comment:
   need update ret if fail



##########
fs/shm/shmfs_alloc.c:
##########
@@ -0,0 +1,143 @@
+/****************************************************************************
+ * fs/shm/shmfs_alloc.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 <stdlib.h>
+#include <nuttx/pgalloc.h>
+
+#include "inode/inode.h"
+#include "shm/shmfs.h"
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+FAR struct shmfs_object_s *shmfs_alloc_object(size_t length)
+{
+  FAR struct shmfs_object_s *object;
+  bool allocated = false;
+
+#if defined(CONFIG_BUILD_FLAT)
+  /* in FLAT build, allocate the object metadata and the data in the same
+   * chunk in kernel heap
+   */
+
+  object = kmm_zalloc(sizeof(struct shmfs_object_s) + length);
+  if (object)
+    {
+      object->paddr = (&object->paddr) + 1;
+      allocated = true;
+    }
+
+#elif defined(CONFIG_BUILD_PROTECTED)
+  /* in PROTECTED build, allocate the shm object in kernel heap, and shared
+   * memory in user heap
+   */
+
+  object = kmm_zalloc(sizeof(struct shmfs_object_s));
+  if (object)
+    {
+      object->paddr = kumm_zalloc(length);
+
+      if (object->paddr)
+        {
+           allocated = true;
+        }
+    }
+
+#elif defined(CONFIG_BUILD_KERNEL)
+  /* in KERNEL build, allocate the shared memory from page pool and store the
+   * physical address
+   */
+
+  size_t i = 0;
+  void **pages;
+  size_t n_pages = MM_NPAGES(length);
+
+  object = kmm_zalloc(sizeof(struct shmfs_object_s) +
+                      (n_pages - 1) * sizeof(object->paddr));
+
+  if (object)
+    {
+      pages = &object->paddr;
+      for (; i < n_pages; i++)
+        {
+          pages[i] = (void *)mm_pgalloc(1);
+          if (!pages[i])
+            {
+              break;
+            }
+        }
+    }
+
+  if (i == n_pages)
+    {
+      allocated = true;
+    }
+#endif
+
+  if (allocated)
+    {
+      object->length = length;
+    }
+  else
+    {
+      /* delete any partial allocation */
+
+      shmfs_free_object(object);
+      object = NULL;
+    }
+
+  return object;
+}
+
+void shmfs_free_object(FAR struct shmfs_object_s *object)
+{
+#if defined(CONFIG_BUILD_KERNEL)
+  size_t i;
+  size_t n_pages = MM_NPAGES(object->length);
+  void **pages;

Review Comment:
   FAR



##########
fs/shm/shmfs_alloc.c:
##########
@@ -0,0 +1,143 @@
+/****************************************************************************
+ * fs/shm/shmfs_alloc.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 <stdlib.h>
+#include <nuttx/pgalloc.h>
+
+#include "inode/inode.h"
+#include "shm/shmfs.h"
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+FAR struct shmfs_object_s *shmfs_alloc_object(size_t length)
+{
+  FAR struct shmfs_object_s *object;
+  bool allocated = false;
+
+#if defined(CONFIG_BUILD_FLAT)
+  /* in FLAT build, allocate the object metadata and the data in the same
+   * chunk in kernel heap
+   */
+
+  object = kmm_zalloc(sizeof(struct shmfs_object_s) + length);
+  if (object)
+    {
+      object->paddr = (&object->paddr) + 1;
+      allocated = true;
+    }
+
+#elif defined(CONFIG_BUILD_PROTECTED)
+  /* in PROTECTED build, allocate the shm object in kernel heap, and shared
+   * memory in user heap
+   */
+
+  object = kmm_zalloc(sizeof(struct shmfs_object_s));
+  if (object)
+    {
+      object->paddr = kumm_zalloc(length);
+
+      if (object->paddr)
+        {
+           allocated = true;
+        }
+    }
+
+#elif defined(CONFIG_BUILD_KERNEL)
+  /* in KERNEL build, allocate the shared memory from page pool and store the
+   * physical address
+   */
+
+  size_t i = 0;
+  void **pages;

Review Comment:
   FAR



##########
fs/shm/shmfs.c:
##########
@@ -0,0 +1,380 @@
+/****************************************************************************
+ * fs/shm/shmfs.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 <assert.h>
+
+#include <nuttx/fs/ioctl.h>
+#include <nuttx/mm/map.h>
+
+#if defined (CONFIG_BUILD_KERNEL)
+#include <nuttx/arch.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
+#endif
+
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep);
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen);
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen);
+static int shmfs_truncate(FAR struct file *filep, off_t length);
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode);
+#endif
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry);
+static int shmfs_munmap(FAR struct task_group_s *group,
+                        FAR struct mm_map_entry_s *entry,
+                        FAR void *start,
+                        size_t length);
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+const struct file_operations shmfs_operations =
+{
+  NULL,             /* open */
+  shmfs_close,      /* close */
+  shmfs_read,       /* read */
+  shmfs_write,      /* write */
+  NULL,             /* seek */
+  NULL,             /* ioctl */
+  shmfs_mmap,       /* mmap */
+  shmfs_truncate,   /* truncate */
+  NULL,             /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  shmfs_unlink      /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: shmfs_read
+ ****************************************************************************/
+
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_write
+ ****************************************************************************/
+
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_release
+ ****************************************************************************/
+
+static int shmfs_release(FAR struct inode *inode)
+{
+  /* If the file has been unlinked previously, delete the contents.
+   * The inode is released after this call, hence checking if i_crefs <= 1.
+   */
+
+  int ret = inode_lock();
+  if (ret >= 0)
+    {
+      if (inode->i_parent == NULL &&

Review Comment:
   why check i_parent 



##########
fs/shm/shmfs.c:
##########
@@ -0,0 +1,384 @@
+/****************************************************************************
+ * fs/shm/shmfs.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 <assert.h>
+
+#include <nuttx/fs/ioctl.h>
+#include <nuttx/mm/map.h>
+
+#if defined (CONFIG_BUILD_KERNEL)
+#include <nuttx/arch.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
+#endif
+
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep);
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen);
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen);
+static int shmfs_truncate(FAR struct file *filep, off_t length);
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode);
+#endif
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry);
+static int shmfs_munmap(FAR struct task_group_s *group,
+                        FAR struct mm_map_entry_s *entry,
+                        FAR void *start,
+                        size_t length);
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+const struct file_operations shmfs_operations =
+{
+  NULL,             /* open */
+  shmfs_close,      /* close */
+  shmfs_read,       /* read */
+  shmfs_write,      /* write */
+  NULL,             /* seek */
+  NULL,             /* ioctl */
+  shmfs_mmap,       /* mmap */
+  shmfs_truncate,   /* truncate */
+  NULL,             /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  shmfs_unlink      /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: shmfs_read
+ ****************************************************************************/
+
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_write
+ ****************************************************************************/
+
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_release
+ ****************************************************************************/
+
+static int shmfs_release(FAR struct inode *inode)
+{
+  /* If the file has been unlinked previously, delete the contents.
+   * The inode is released after this call, hence checking if i_crefs <= 1.
+   */
+
+  int ret = inode_lock();
+  if (ret >= 0)
+    {
+      if (inode->i_parent == NULL &&
+          inode->i_crefs <= 1)
+        {
+          delete_shm_object(inode->i_private);
+          inode->i_private = NULL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_close
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep)
+{
+  /* Release the shmfs object. The object gets deleted if no-one has
+   * reference to it (either mmap or open file) and the object has been
+   * unlinked
+   */
+
+  return shmfs_release(filep->f_inode);
+}
+
+/****************************************************************************
+ * Name: shmfs_truncate
+ ****************************************************************************/
+
+static int shmfs_truncate(FAR struct file *filep, off_t length)
+{
+  FAR struct shmfs_object_s *object;
+  int ret;
+
+  if (length == 0)
+    {
+      return -EINVAL;
+    }
+
+  ret = inode_lock();
+  if (ret >= 0)
+    {
+      object = filep->f_inode->i_private;
+      if (!object)
+        {
+          filep->f_inode->i_private = alloc_shm_object(length);
+        }
+      else if (object->length != length)
+        {
+          /* This doesn't support resize */
+
+          ret = -EINVAL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_unlink
+ ****************************************************************************/
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode)
+{
+  if (inode->i_crefs <= 1)
+    {
+      delete_shm_object(inode->i_private);
+      inode->i_private = NULL;
+    }
+
+  return OK;
+}
+#endif
+
+/****************************************************************************
+ * Name: shmfs_map_object
+ ****************************************************************************/
+
+static int shmfs_map_object(FAR struct shmfs_object_s *object,
+                            FAR void **vaddr)
+{
+  int ret = OK;
+
+#ifdef CONFIG_BUILD_KERNEL
+  /* Map the physical pages of the shm object with MMU. */
+
+  FAR struct tcb_s        *tcb   = nxsched_self();
+  FAR struct task_group_s *group = tcb->group;
+  FAR uintptr_t *pages =
+    (FAR uintptr_t *)&object->paddr;
+  uintptr_t                mapaddr;
+  unsigned int             npages;
+
+  /* Find a free vaddr space that satisfies length */
+
+  mapaddr = (uintptr_t)vm_alloc_region(get_group_mm(group), 0,
+                                       object->length);
+  if (mapaddr == 0)
+    {
+      return -ENOMEM;
+    }
+
+  /* Convert the region size to pages */
+
+  npages = MM_NPAGES(object->length);
+
+  /* Map the memory to user virtual address space */
+
+  ret = up_shmat(pages, npages, mapaddr);
+  if (ret < 0)
+    {
+      vm_release_region(get_group_mm(group), (FAR void *)mapaddr,
+                        object->length);
+    }
+  else
+    {
+      *vaddr = (FAR void *)mapaddr;
+    }
+#else
+  /* Use the physical address directly */
+
+  *vaddr = object->paddr;
+#endif
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_mmap
+ ****************************************************************************/
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry)
+{
+  FAR struct shmfs_object_s *object;
+  int ret = -EINVAL;
+
+  DEBUGASSERT(filep->f_inode != NULL);
+
+  /* We don't support offset at the moment, just mapping the whole object */
+
+  if (entry->offset != 0)
+    {
+      return ret;
+    }
+
+  /* Keep the inode when mmapped, increase refcount */
+
+  ret = inode_addref(filep->f_inode);
+  if (ret >= 0)
+    {
+      object = (FAR struct shmfs_object_s *)filep->f_inode->i_private;
+
+      if (object)

Review Comment:
   why? filep already hold one reference for you



##########
fs/shm/shm_unlink.c:
##########
@@ -0,0 +1,167 @@
+/****************************************************************************
+ * fs/shm/shm_unlink.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 <stdio.h>
+#include <assert.h>
+#include <errno.h>
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int file_shm_unlink(FAR const char *name)
+{
+  FAR struct inode *inode;
+  struct inode_search_s desc;
+  char fullpath[sizeof(CONFIG_FS_SHMFS_VFS_PATH) + CONFIG_NAME_MAX + 2];
+  int ret;
+
+  /* Make sure that a non-NULL name is supplied */
+
+  if (!name)
+    {
+      return -ENOENT;
+    }
+
+  /* Remove any number of leading '/' */
+
+  while (*name == '/')
+    {
+      name++;
+    }
+
+  /* Empty name supplied? */
+
+  if (*name == '\0')
+    {
+      return -ENOENT;
+    }
+
+  /* Name too long? */
+
+  if (strnlen(name, CONFIG_NAME_MAX) > CONFIG_NAME_MAX)
+    {
+      return -ENAMETOOLONG;
+    }
+
+  /* Get the full path to the shm object */
+
+  snprintf(fullpath, sizeof(fullpath),
+           CONFIG_FS_SHMFS_VFS_PATH "/%s", name);
+
+  /* Get the inode for this shm object */
+
+  SETUP_SEARCH(&desc, fullpath, false);
+
+  ret = inode_lock();
+  if (ret < 0)
+    {
+      goto errout_with_search;
+    }
+
+  ret = inode_find(&desc);
+  if (ret < 0)
+    {
+      /* There is no inode that includes in this path */
+
+      goto errout_with_sem;
+    }
+
+  /* Get the search results */
+
+  inode = desc.node;
+  DEBUGASSERT(inode != NULL);
+
+  /* Verify that what we found is, indeed, an shm inode */
+
+  if (!INODE_IS_SHM(inode))
+    {
+      ret = -ENOENT;
+      inode_release(inode);

Review Comment:
   remove and goto errout_with_inode



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072825950


##########
fs/shm/shmfs.c:
##########
@@ -0,0 +1,384 @@
+/****************************************************************************
+ * fs/shm/shmfs.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 <assert.h>
+
+#include <nuttx/fs/ioctl.h>
+#include <nuttx/mm/map.h>
+
+#if defined (CONFIG_BUILD_KERNEL)
+#include <nuttx/arch.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
+#endif
+
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep);
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen);
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen);
+static int shmfs_truncate(FAR struct file *filep, off_t length);
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode);
+#endif
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry);
+static int shmfs_munmap(FAR struct task_group_s *group,
+                        FAR struct mm_map_entry_s *entry,
+                        FAR void *start,
+                        size_t length);
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+const struct file_operations shmfs_operations =
+{
+  NULL,             /* open */
+  shmfs_close,      /* close */
+  shmfs_read,       /* read */
+  shmfs_write,      /* write */
+  NULL,             /* seek */
+  NULL,             /* ioctl */
+  shmfs_mmap,       /* mmap */
+  shmfs_truncate,   /* truncate */
+  NULL,             /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  shmfs_unlink      /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: shmfs_read
+ ****************************************************************************/
+
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_write
+ ****************************************************************************/
+
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_release
+ ****************************************************************************/
+
+static int shmfs_release(FAR struct inode *inode)
+{
+  /* If the file has been unlinked previously, delete the contents.
+   * The inode is released after this call, hence checking if i_crefs <= 1.
+   */
+
+  int ret = inode_lock();
+  if (ret >= 0)
+    {
+      if (inode->i_parent == NULL &&
+          inode->i_crefs <= 1)
+        {
+          delete_shm_object(inode->i_private);
+          inode->i_private = NULL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_close
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep)
+{
+  /* Release the shmfs object. The object gets deleted if no-one has
+   * reference to it (either mmap or open file) and the object has been
+   * unlinked
+   */
+
+  return shmfs_release(filep->f_inode);
+}
+
+/****************************************************************************
+ * Name: shmfs_truncate
+ ****************************************************************************/
+
+static int shmfs_truncate(FAR struct file *filep, off_t length)
+{
+  FAR struct shmfs_object_s *object;
+  int ret;
+
+  if (length == 0)
+    {
+      return -EINVAL;
+    }
+
+  ret = inode_lock();
+  if (ret >= 0)
+    {
+      object = filep->f_inode->i_private;
+      if (!object)
+        {
+          filep->f_inode->i_private = alloc_shm_object(length);
+        }
+      else if (object->length != length)
+        {
+          /* This doesn't support resize */
+
+          ret = -EINVAL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_unlink
+ ****************************************************************************/
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode)
+{
+  if (inode->i_crefs <= 1)
+    {
+      delete_shm_object(inode->i_private);
+      inode->i_private = NULL;
+    }
+
+  return OK;
+}
+#endif
+
+/****************************************************************************
+ * Name: shmfs_map_object
+ ****************************************************************************/
+
+static int shmfs_map_object(FAR struct shmfs_object_s *object,
+                            FAR void **vaddr)
+{
+  int ret = OK;
+
+#ifdef CONFIG_BUILD_KERNEL
+  /* Map the physical pages of the shm object with MMU. */
+
+  FAR struct tcb_s        *tcb   = nxsched_self();
+  FAR struct task_group_s *group = tcb->group;
+  FAR uintptr_t *pages =
+    (FAR uintptr_t *)&object->paddr;
+  uintptr_t                mapaddr;
+  unsigned int             npages;
+
+  /* Find a free vaddr space that satisfies length */
+
+  mapaddr = (uintptr_t)vm_alloc_region(get_group_mm(group), 0,
+                                       object->length);
+  if (mapaddr == 0)
+    {
+      return -ENOMEM;
+    }
+
+  /* Convert the region size to pages */
+
+  npages = MM_NPAGES(object->length);
+
+  /* Map the memory to user virtual address space */
+
+  ret = up_shmat(pages, npages, mapaddr);
+  if (ret < 0)
+    {
+      vm_release_region(get_group_mm(group), (FAR void *)mapaddr,
+                        object->length);
+    }
+  else
+    {
+      *vaddr = (FAR void *)mapaddr;
+    }
+#else
+  /* Use the physical address directly */
+
+  *vaddr = object->paddr;
+#endif
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_mmap
+ ****************************************************************************/
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry)
+{
+  FAR struct shmfs_object_s *object;
+  int ret = -EINVAL;
+
+  DEBUGASSERT(filep->f_inode != NULL);
+
+  /* We don't support offset at the moment, just mapping the whole object */
+
+  if (entry->offset != 0)
+    {
+      return ret;
+    }
+
+  /* Keep the inode when mmapped, increase refcount */
+
+  ret = inode_addref(filep->f_inode);
+  if (ret >= 0)
+    {
+      object = (FAR struct shmfs_object_s *)filep->f_inode->i_private;
+
+      if (object)

Review Comment:
   That's right! It may be bad application design, but it is possible to share file descriptors between threads in the same application. You can't rely on refcount of the open call only here. Of course the application wouldn't work, but it is better that mmap either succeeds or fails, and doesn't crash. I remember this case now, it was just so long ago that got confused by the review comment. sorry, reverting this part back to original.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1070432491


##########
fs/shm/Kconfig:
##########
@@ -3,16 +3,16 @@
 # see the file kconfig-language.txt in the NuttX tools repository.
 #
 
-config FS_SHM
+config FS_SHMFS
 	bool "Shared memory support"
 	default n
-	depends on MM_SHM
+	depends on ARCH_VMA_MAPPING || !BUILD_KERNEL

Review Comment:
   done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072068384


##########
fs/shm/shm_unlink.c:
##########
@@ -0,0 +1,140 @@
+/****************************************************************************
+ * fs/shm/shm_unlink.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 <stdio.h>
+#include <assert.h>
+#include <errno.h>
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int file_shm_unlink(FAR const char *shm_name)

Review Comment:
   This I didn't understand; the function was not meant to be nuttx internal if, but just internal function to this driver. So why add it there?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] xiaoxiang781216 merged pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 merged PR #8109:
URL: https://github.com/apache/nuttx/pull/8109


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] xiaoxiang781216 commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1070398118


##########
include/sys/syscall_lookup.h:
##########
@@ -333,6 +333,11 @@ SYSCALL_LOOKUP(munmap,                     2)
   SYSCALL_LOOKUP(mq_unlink,                1)
 #endif
 
+#ifdef CONFIG_FS_SHMFS

Review Comment:
   move before line 290



##########
sched/init/nx_start.c:
##########
@@ -588,6 +589,12 @@ void nx_start(void)
   nxmsg_initialize();
 #endif
 
+#ifdef CONFIG_FS_SHMFS
+  /* Initialize posix shm support */
+
+  shmfs_initialize();

Review Comment:
   move to fs_initialize



##########
fs/shm/Kconfig:
##########
@@ -3,16 +3,16 @@
 # see the file kconfig-language.txt in the NuttX tools repository.
 #
 
-config FS_SHM
+config FS_SHMFS
 	bool "Shared memory support"
 	default n
-	depends on MM_SHM
+	depends on ARCH_VMA_MAPPING || !BUILD_KERNEL

Review Comment:
   select ARCH_VMA_MAPPING like MM_SHM



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] pussuw commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
pussuw commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072861357


##########
fs/shm/shmfs.c:
##########
@@ -0,0 +1,384 @@
+/****************************************************************************
+ * fs/shm/shmfs.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 <assert.h>
+
+#include <nuttx/fs/ioctl.h>
+#include <nuttx/mm/map.h>
+
+#if defined (CONFIG_BUILD_KERNEL)
+#include <nuttx/arch.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
+#endif
+
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep);
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen);
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen);
+static int shmfs_truncate(FAR struct file *filep, off_t length);
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode);
+#endif
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry);
+static int shmfs_munmap(FAR struct task_group_s *group,
+                        FAR struct mm_map_entry_s *entry,
+                        FAR void *start,
+                        size_t length);
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+const struct file_operations shmfs_operations =
+{
+  NULL,             /* open */
+  shmfs_close,      /* close */
+  shmfs_read,       /* read */
+  shmfs_write,      /* write */
+  NULL,             /* seek */
+  NULL,             /* ioctl */
+  shmfs_mmap,       /* mmap */
+  shmfs_truncate,   /* truncate */
+  NULL,             /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  shmfs_unlink      /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: shmfs_read
+ ****************************************************************************/
+
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_write
+ ****************************************************************************/
+
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_release
+ ****************************************************************************/
+
+static int shmfs_release(FAR struct inode *inode)
+{
+  /* If the file has been unlinked previously, delete the contents.
+   * The inode is released after this call, hence checking if i_crefs <= 1.
+   */
+
+  int ret = inode_lock();
+  if (ret >= 0)
+    {
+      if (inode->i_parent == NULL &&
+          inode->i_crefs <= 1)
+        {
+          delete_shm_object(inode->i_private);
+          inode->i_private = NULL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_close
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep)
+{
+  /* Release the shmfs object. The object gets deleted if no-one has
+   * reference to it (either mmap or open file) and the object has been
+   * unlinked
+   */
+
+  return shmfs_release(filep->f_inode);
+}
+
+/****************************************************************************
+ * Name: shmfs_truncate
+ ****************************************************************************/
+
+static int shmfs_truncate(FAR struct file *filep, off_t length)
+{
+  FAR struct shmfs_object_s *object;
+  int ret;
+
+  if (length == 0)
+    {
+      return -EINVAL;
+    }
+
+  ret = inode_lock();
+  if (ret >= 0)
+    {
+      object = filep->f_inode->i_private;
+      if (!object)
+        {
+          filep->f_inode->i_private = alloc_shm_object(length);
+        }
+      else if (object->length != length)
+        {
+          /* This doesn't support resize */
+
+          ret = -EINVAL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_unlink
+ ****************************************************************************/
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode)
+{
+  if (inode->i_crefs <= 1)
+    {
+      delete_shm_object(inode->i_private);
+      inode->i_private = NULL;
+    }
+
+  return OK;
+}
+#endif
+
+/****************************************************************************
+ * Name: shmfs_map_object
+ ****************************************************************************/
+
+static int shmfs_map_object(FAR struct shmfs_object_s *object,
+                            FAR void **vaddr)
+{
+  int ret = OK;
+
+#ifdef CONFIG_BUILD_KERNEL
+  /* Map the physical pages of the shm object with MMU. */
+
+  FAR struct tcb_s        *tcb   = nxsched_self();
+  FAR struct task_group_s *group = tcb->group;
+  FAR uintptr_t *pages =
+    (FAR uintptr_t *)&object->paddr;
+  uintptr_t                mapaddr;
+  unsigned int             npages;
+
+  /* Find a free vaddr space that satisfies length */
+
+  mapaddr = (uintptr_t)vm_alloc_region(get_group_mm(group), 0,
+                                       object->length);
+  if (mapaddr == 0)
+    {
+      return -ENOMEM;
+    }
+
+  /* Convert the region size to pages */
+
+  npages = MM_NPAGES(object->length);
+
+  /* Map the memory to user virtual address space */
+
+  ret = up_shmat(pages, npages, mapaddr);
+  if (ret < 0)
+    {
+      vm_release_region(get_group_mm(group), (FAR void *)mapaddr,
+                        object->length);
+    }
+  else
+    {
+      *vaddr = (FAR void *)mapaddr;
+    }
+#else
+  /* Use the physical address directly */
+
+  *vaddr = object->paddr;
+#endif
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_mmap
+ ****************************************************************************/
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry)
+{
+  FAR struct shmfs_object_s *object;
+  int ret = -EINVAL;
+
+  DEBUGASSERT(filep->f_inode != NULL);
+
+  /* We don't support offset at the moment, just mapping the whole object */
+
+  if (entry->offset != 0)
+    {
+      return ret;
+    }
+
+  /* Keep the inode when mmapped, increase refcount */
+
+  ret = inode_addref(filep->f_inode);
+  if (ret >= 0)
+    {
+      object = (FAR struct shmfs_object_s *)filep->f_inode->i_private;
+
+      if (object)

Review Comment:
   I think the following is perfectly legal code:
   ```
   
   int fd = open("myfile", XX);
   void *ptr = mmap(NULL, 1024, PROT_XX, MAP_XX, 0);
   close(fd);
   
   sprintf(ptr, "Foobar!\n",);
   printf("%s" (const char *)ptr);
   
   ```
   etc
   
   So if the open file descriptor is the only one keeping the mapped data alive, the above code will fail.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] pussuw commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
pussuw commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072051035


##########
fs/shm/shmfs.c:
##########
@@ -0,0 +1,384 @@
+/****************************************************************************
+ * fs/shm/shmfs.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 <assert.h>
+
+#include <nuttx/fs/ioctl.h>
+#include <nuttx/mm/map.h>
+
+#if defined (CONFIG_BUILD_KERNEL)
+#include <nuttx/arch.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
+#endif
+
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep);
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen);
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen);
+static int shmfs_truncate(FAR struct file *filep, off_t length);
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode);
+#endif
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry);
+static int shmfs_munmap(FAR struct task_group_s *group,
+                        FAR struct mm_map_entry_s *entry,
+                        FAR void *start,
+                        size_t length);
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+const struct file_operations shmfs_operations =
+{
+  NULL,             /* open */
+  shmfs_close,      /* close */
+  shmfs_read,       /* read */
+  shmfs_write,      /* write */
+  NULL,             /* seek */
+  NULL,             /* ioctl */
+  shmfs_mmap,       /* mmap */
+  shmfs_truncate,   /* truncate */
+  NULL,             /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  shmfs_unlink      /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: shmfs_read
+ ****************************************************************************/
+
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_write
+ ****************************************************************************/
+
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_release
+ ****************************************************************************/
+
+static int shmfs_release(FAR struct inode *inode)
+{
+  /* If the file has been unlinked previously, delete the contents.
+   * The inode is released after this call, hence checking if i_crefs <= 1.
+   */
+
+  int ret = inode_lock();
+  if (ret >= 0)
+    {
+      if (inode->i_parent == NULL &&
+          inode->i_crefs <= 1)
+        {
+          delete_shm_object(inode->i_private);
+          inode->i_private = NULL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_close
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep)
+{
+  /* Release the shmfs object. The object gets deleted if no-one has
+   * reference to it (either mmap or open file) and the object has been
+   * unlinked
+   */
+
+  return shmfs_release(filep->f_inode);
+}
+
+/****************************************************************************
+ * Name: shmfs_truncate
+ ****************************************************************************/
+
+static int shmfs_truncate(FAR struct file *filep, off_t length)
+{
+  FAR struct shmfs_object_s *object;
+  int ret;
+
+  if (length == 0)
+    {
+      return -EINVAL;
+    }
+
+  ret = inode_lock();
+  if (ret >= 0)
+    {
+      object = filep->f_inode->i_private;
+      if (!object)
+        {
+          filep->f_inode->i_private = alloc_shm_object(length);
+        }
+      else if (object->length != length)
+        {
+          /* This doesn't support resize */
+
+          ret = -EINVAL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_unlink
+ ****************************************************************************/
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode)
+{
+  if (inode->i_crefs <= 1)
+    {
+      delete_shm_object(inode->i_private);
+      inode->i_private = NULL;
+    }
+
+  return OK;
+}
+#endif
+
+/****************************************************************************
+ * Name: shmfs_map_object
+ ****************************************************************************/
+
+static int shmfs_map_object(FAR struct shmfs_object_s *object,
+                            FAR void **vaddr)
+{
+  int ret = OK;
+
+#ifdef CONFIG_BUILD_KERNEL
+  /* Map the physical pages of the shm object with MMU. */
+
+  FAR struct tcb_s        *tcb   = nxsched_self();
+  FAR struct task_group_s *group = tcb->group;
+  FAR uintptr_t *pages =
+    (FAR uintptr_t *)&object->paddr;
+  uintptr_t                mapaddr;
+  unsigned int             npages;
+
+  /* Find a free vaddr space that satisfies length */
+
+  mapaddr = (uintptr_t)vm_alloc_region(get_group_mm(group), 0,
+                                       object->length);
+  if (mapaddr == 0)
+    {
+      return -ENOMEM;
+    }
+
+  /* Convert the region size to pages */
+
+  npages = MM_NPAGES(object->length);
+
+  /* Map the memory to user virtual address space */
+
+  ret = up_shmat(pages, npages, mapaddr);
+  if (ret < 0)
+    {
+      vm_release_region(get_group_mm(group), (FAR void *)mapaddr,
+                        object->length);
+    }
+  else
+    {
+      *vaddr = (FAR void *)mapaddr;
+    }
+#else
+  /* Use the physical address directly */
+
+  *vaddr = object->paddr;
+#endif
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_mmap
+ ****************************************************************************/
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry)
+{
+  FAR struct shmfs_object_s *object;
+  int ret = -EINVAL;
+
+  DEBUGASSERT(filep->f_inode != NULL);
+
+  /* We don't support offset at the moment, just mapping the whole object */
+
+  if (entry->offset != 0)
+    {
+      return ret;
+    }
+
+  /* Keep the inode when mmapped, increase refcount */
+
+  ret = inode_addref(filep->f_inode);
+  if (ret >= 0)
+    {
+      object = (FAR struct shmfs_object_s *)filep->f_inode->i_private;
+
+      if (object)
+        {
+          ret = shmfs_map_object(object, &entry->vaddr);
+        }
+
+      if (!object || ret < 0)
+        {
+          /* Oops, the file has not been truncated yet or
+           * there was error in up_shmat
+           */
+
+          inode_release(filep->f_inode);
+        }
+      else
+        {
+          entry->munmap = shmfs_munmap;
+          entry->priv.p = (FAR void *)filep->f_inode;
+          mm_map_add(entry);
+        }
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_unmap_object
+ ****************************************************************************/
+
+static int shmfs_unmap_area(FAR struct task_group_s *group,

Review Comment:
   I did this part and I prefer to have the static method here. It abstracts the build type quite nicely IMO.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072197871


##########
boards/risc-v/mpfs/icicle/configs/knsh/defconfig:
##########
@@ -68,7 +69,6 @@ CONFIG_LIBM=y
 CONFIG_MEMSET_64BIT=y
 CONFIG_MEMSET_OPTSPEED=y
 CONFIG_MM_PGALLOC=y
-CONFIG_MM_SHM=y

Review Comment:
   ok, now put it back



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on a diff in pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on code in PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#discussion_r1072851767


##########
fs/shm/shmfs.c:
##########
@@ -0,0 +1,377 @@
+/****************************************************************************
+ * fs/shm/shmfs.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 <assert.h>
+
+#include <nuttx/fs/ioctl.h>
+#include <nuttx/mm/map.h>
+
+#if defined (CONFIG_BUILD_KERNEL)
+#include <nuttx/arch.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
+#endif
+
+#include "shm/shmfs.h"
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep);
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen);
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen);
+static int shmfs_truncate(FAR struct file *filep, off_t length);
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode);
+#endif
+
+static int shmfs_mmap(FAR struct file *filep,
+                      FAR struct mm_map_entry_s *entry);
+static int shmfs_munmap(FAR struct task_group_s *group,
+                        FAR struct mm_map_entry_s *entry,
+                        FAR void *start,
+                        size_t length);
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+const struct file_operations shmfs_operations =
+{
+  NULL,             /* open */
+  shmfs_close,      /* close */
+  shmfs_read,       /* read */
+  shmfs_write,      /* write */
+  NULL,             /* seek */
+  NULL,             /* ioctl */
+  shmfs_mmap,       /* mmap */
+  shmfs_truncate,   /* truncate */
+  NULL,             /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  shmfs_unlink      /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: shmfs_read
+ ****************************************************************************/
+
+static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer,
+                          size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_write
+ ****************************************************************************/
+
+static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer,
+                           size_t buflen)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: shmfs_release
+ ****************************************************************************/
+
+static int shmfs_release(FAR struct inode *inode)
+{
+  /* If the file has been unlinked previously, delete the contents.
+   * The inode is released after this call, hence checking if i_crefs <= 1.
+   */
+
+  int ret = inode_lock();
+  if (ret >= 0)
+    {
+      if (inode->i_parent == NULL &&
+          inode->i_crefs <= 1)
+        {
+          shmfs_free_object(inode->i_private);
+          inode->i_private = NULL;
+          ret = OK;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_close
+ ****************************************************************************/
+
+static int shmfs_close(FAR struct file *filep)
+{
+  /* Release the shmfs object. The object gets deleted if no-one has
+   * reference to it (either mmap or open file) and the object has been
+   * unlinked
+   */
+
+  return shmfs_release(filep->f_inode);
+}
+
+/****************************************************************************
+ * Name: shmfs_truncate
+ ****************************************************************************/
+
+static int shmfs_truncate(FAR struct file *filep, off_t length)
+{
+  FAR struct shmfs_object_s *object;
+  int ret;
+
+  if (length == 0)
+    {
+      return -EINVAL;
+    }
+
+  ret = inode_lock();
+  if (ret >= 0)
+    {
+      object = filep->f_inode->i_private;
+      if (!object)
+        {
+          filep->f_inode->i_private = shmfs_alloc_object(length);
+          if (!filep->f_inode->i_private)
+            {
+              ret = -EFAULT;
+            }
+        }
+      else if (object->length != length)
+        {
+          /* This doesn't support resize */
+
+          ret = -EINVAL;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shmfs_unlink
+ ****************************************************************************/
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int shmfs_unlink(FAR struct inode *inode)
+{
+  int ret = inode_lock();
+
+  if (ret >= 0)
+    {
+      if (inode->i_crefs <= 1)
+        {
+          shmfs_free_object(inode->i_private);
+          inode->i_private = NULL;
+          ret = OK;
+        }
+
+      inode_unlock();
+    }
+
+  return ret;
+}
+#endif
+
+/****************************************************************************
+ * Name: shmfs_map_object
+ ****************************************************************************/
+
+static int shmfs_map_object(FAR struct shmfs_object_s *object,
+                            FAR void **vaddr)
+{
+  int ret = OK;
+
+#ifdef CONFIG_BUILD_KERNEL
+  /* Map the physical pages of the shm object with MMU. */
+
+  FAR struct tcb_s        *tcb   = nxsched_self();
+  FAR struct task_group_s *group = tcb->group;
+  FAR uintptr_t *pages =

Review Comment:
   I removed the alignment from all of those instead, as they have been also removed in all the other places in this file.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] jlaitine commented on pull request #8109: Add shmfs

Posted by GitBox <gi...@apache.org>.
jlaitine commented on PR #8109:
URL: https://github.com/apache/nuttx/pull/8109#issuecomment-1382704244

   Again some strange CI error which was not caused by this PR... rebased to master and pushed again


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org