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-unsubscr...@nuttx.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org