2015-06-05 21:18:06 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* fs/unionfs/fs_unionfs.c
|
|
|
|
*
|
2018-01-03 23:03:56 +01:00
|
|
|
* Copyright (C) 2015, 2017-2018 Gregory Nutt. All rights reserved.
|
2015-06-05 21:18:06 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/statfs.h>
|
|
|
|
#include <sys/stat.h>
|
2015-06-07 03:48:38 +02:00
|
|
|
#include <sys/mount.h>
|
2015-06-05 21:18:06 +02:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
2015-06-06 17:49:13 +02:00
|
|
|
#include <stdio.h>
|
2015-06-05 21:18:06 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <semaphore.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fixedmath.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
#include <nuttx/fs/unionfs.h>
|
|
|
|
#include <nuttx/fs/dirent.h>
|
|
|
|
#include <nuttx/fs/ioctl.h>
|
|
|
|
#include <nuttx/binfmt/builtin.h>
|
|
|
|
|
|
|
|
#include "inode/inode.h"
|
|
|
|
|
|
|
|
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_FS_UNIONFS)
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#undef MIN
|
|
|
|
#undef MAX
|
|
|
|
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
|
|
|
|
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
/* This structure describes one contained file system mountpoint */
|
|
|
|
|
|
|
|
struct unionfs_mountpt_s
|
|
|
|
{
|
|
|
|
FAR struct inode *um_node; /* Filesystem inode */
|
|
|
|
FAR char *um_prefix; /* Path prefix to filesystem */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* This structure describes the union file system */
|
|
|
|
|
|
|
|
struct unionfs_inode_s
|
|
|
|
{
|
|
|
|
struct unionfs_mountpt_s ui_fs[2]; /* Contained file systems */
|
|
|
|
sem_t ui_exclsem; /* Enforces mutually exclusive access */
|
|
|
|
int16_t ui_nopen; /* Number of open references */
|
2015-06-06 19:33:09 +02:00
|
|
|
bool ui_unmounted; /* File system has been unmounted */
|
2015-06-05 21:18:06 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/* This structure descries one opened file */
|
|
|
|
|
|
|
|
struct unionfs_file_s
|
|
|
|
{
|
|
|
|
uint8_t uf_ndx; /* Filesystem index */
|
|
|
|
FAR struct file uf_file; /* Filesystem open file description */
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
/* Helper functions */
|
|
|
|
|
|
|
|
static int unionfs_semtake(FAR struct unionfs_inode_s *ui, bool noint);
|
2017-10-03 23:35:24 +02:00
|
|
|
#define unionfs_semgive(ui) (void)nxsem_post(&(ui)->ui_exclsem)
|
2015-06-05 21:18:06 +02:00
|
|
|
|
2015-06-24 17:07:13 +02:00
|
|
|
static FAR const char *unionfs_offsetpath(FAR const char *relpath,
|
|
|
|
FAR const char *prefix);
|
|
|
|
static bool unionfs_ispartprefix(FAR const char *partprefix,
|
2015-06-05 21:18:06 +02:00
|
|
|
FAR const char *prefix);
|
|
|
|
static int unionfs_tryopen(FAR struct file *filep,
|
|
|
|
FAR const char *relpath, FAR const char *prefix, int oflags,
|
|
|
|
mode_t mode);
|
|
|
|
static int unionfs_tryopendir(FAR struct inode *inode,
|
|
|
|
FAR const char *relpath, FAR const char *prefix,
|
|
|
|
FAR struct fs_dirent_s *dir);
|
|
|
|
static int unionfs_trymkdir(FAR struct inode *inode,
|
|
|
|
FAR const char *relpath, FAR const char *prefix,
|
|
|
|
mode_t mode);
|
|
|
|
static int unionfs_tryrmdir(FAR struct inode *inode,
|
|
|
|
FAR const char *relpath, FAR const char *prefix);
|
2015-06-06 19:33:09 +02:00
|
|
|
static int unionfs_tryunlink(FAR struct inode *inode,
|
|
|
|
FAR const char *relpath, FAR const char *prefix);
|
2015-06-05 21:18:06 +02:00
|
|
|
static int unionfs_tryrename(FAR struct inode *mountpt,
|
|
|
|
FAR const char *oldrelpath, FAR const char *newrelpath,
|
|
|
|
FAR const char *prefix);
|
|
|
|
static int unionfs_trystat(FAR struct inode *inode,
|
|
|
|
FAR const char *relpath, FAR const char *prefix,
|
|
|
|
FAR struct stat *buf);
|
|
|
|
static int unionfs_trystatdir(FAR struct inode *inode,
|
|
|
|
FAR const char *relpath, FAR const char *prefix);
|
|
|
|
static int unionfs_trystatfile(FAR struct inode *inode,
|
|
|
|
FAR const char *relpath, FAR const char *prefix);
|
2015-06-06 17:49:13 +02:00
|
|
|
static FAR char *unionfs_relpath(FAR const char *path,
|
|
|
|
FAR const char *name);
|
2015-06-05 21:18:06 +02:00
|
|
|
|
2015-06-07 03:48:38 +02:00
|
|
|
static int unionfs_unbind_child(FAR struct unionfs_mountpt_s *um);
|
2015-06-05 21:18:06 +02:00
|
|
|
static void unionfs_destroy(FAR struct unionfs_inode_s *ui);
|
|
|
|
|
|
|
|
/* Operations on opened files (with struct file) */
|
|
|
|
|
|
|
|
static int unionfs_open(FAR struct file *filep, const char *relpath,
|
|
|
|
int oflags, mode_t mode);
|
|
|
|
static int unionfs_close(FAR struct file *filep);
|
|
|
|
static ssize_t unionfs_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen);
|
|
|
|
static ssize_t unionfs_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen);
|
|
|
|
static off_t unionfs_seek(FAR struct file *filep, off_t offset, int whence);
|
|
|
|
static int unionfs_ioctl(FAR struct file *filep, int cmd,
|
|
|
|
unsigned long arg);
|
|
|
|
static int unionfs_sync(FAR struct file *filep);
|
|
|
|
static int unionfs_dup(FAR const struct file *oldp,
|
|
|
|
FAR struct file *newp);
|
2017-02-12 20:42:27 +01:00
|
|
|
static int unionfs_fstat(FAR const struct file *filep,
|
|
|
|
FAR struct stat *buf);
|
2018-01-03 23:03:56 +01:00
|
|
|
static int unionfs_truncate(FAR struct file *filep, off_t length);
|
2015-06-05 21:18:06 +02:00
|
|
|
|
|
|
|
/* Operations on directories */
|
|
|
|
|
|
|
|
static int unionfs_opendir(struct inode *mountpt, const char *relpath,
|
|
|
|
FAR struct fs_dirent_s *dir);
|
|
|
|
static int unionfs_closedir(FAR struct inode *mountpt,
|
|
|
|
FAR struct fs_dirent_s *dir);
|
|
|
|
static int unionfs_readdir(FAR struct inode *mountpt,
|
|
|
|
FAR struct fs_dirent_s *dir);
|
|
|
|
static int unionfs_rewinddir(FAR struct inode *mountpt,
|
|
|
|
FAR struct fs_dirent_s *dir);
|
|
|
|
|
|
|
|
static int unionfs_unbind(FAR void *handle, FAR struct inode **blkdriver,
|
|
|
|
unsigned int flags);
|
|
|
|
static int unionfs_statfs(FAR struct inode *mountpt,
|
|
|
|
FAR struct statfs *buf);
|
|
|
|
|
|
|
|
/* Operations on paths */
|
|
|
|
|
|
|
|
static int unionfs_unlink(FAR struct inode *mountpt,
|
|
|
|
FAR const char *relpath);
|
|
|
|
static int unionfs_mkdir(FAR struct inode *mountpt,
|
|
|
|
FAR const char *relpath, mode_t mode);
|
|
|
|
static int unionfs_rmdir(FAR struct inode *mountpt,
|
|
|
|
FAR const char *relpath);
|
|
|
|
static int unionfs_rename(FAR struct inode *mountpt,
|
|
|
|
FAR const char *oldrelpath, FAR const char *newrelpath);
|
|
|
|
static int unionfs_stat(FAR struct inode *mountpt,
|
|
|
|
FAR const char *relpath, FAR struct stat *buf);
|
|
|
|
|
|
|
|
/* Initialization */
|
|
|
|
|
|
|
|
static int unionfs_getmount(FAR const char *path,
|
|
|
|
FAR struct inode **inode);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* See fs_mount.c -- this structure is explicitly extern'ed there.
|
|
|
|
* We use the old-fashioned kind of initializers so that this will compile
|
|
|
|
* with any compiler.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static const struct mountpt_operations g_unionfs_mops =
|
|
|
|
{
|
|
|
|
unionfs_open, /* open */
|
|
|
|
unionfs_close, /* close */
|
|
|
|
unionfs_read, /* read */
|
|
|
|
unionfs_write, /* write */
|
|
|
|
unionfs_seek, /* seek */
|
|
|
|
unionfs_ioctl, /* ioctl */
|
|
|
|
|
|
|
|
unionfs_sync, /* sync */
|
|
|
|
unionfs_dup, /* dup */
|
2017-02-12 20:42:27 +01:00
|
|
|
unionfs_fstat, /* fstat */
|
2018-01-03 23:03:56 +01:00
|
|
|
unionfs_truncate, /* truncate */
|
2015-06-05 21:18:06 +02:00
|
|
|
|
|
|
|
unionfs_opendir, /* opendir */
|
|
|
|
unionfs_closedir, /* closedir */
|
|
|
|
unionfs_readdir, /* readdir */
|
|
|
|
unionfs_rewinddir, /* rewinddir */
|
|
|
|
|
|
|
|
NULL, /* bind */
|
|
|
|
unionfs_unbind, /* unbind */
|
|
|
|
unionfs_statfs, /* statfs */
|
|
|
|
|
|
|
|
unionfs_unlink, /* unlink */
|
|
|
|
unionfs_mkdir, /* mkdir */
|
|
|
|
unionfs_rmdir, /* rmdir */
|
|
|
|
unionfs_rename, /* rename */
|
|
|
|
unionfs_stat /* stat */
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: unionfs_semtake
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int unionfs_semtake(FAR struct unionfs_inode_s *ui, bool noint)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(&ui->ui_exclsem);
|
2015-06-05 21:18:06 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
DEBUGASSERT(ret == -EINTR);
|
2015-06-05 21:18:06 +02:00
|
|
|
if (!noint)
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
return ret;
|
2015-06-05 21:18:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-04 23:22:27 +02:00
|
|
|
while (ret == -EINTR);
|
2015-06-05 21:18:06 +02:00
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
return ret;
|
2015-06-05 21:18:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-06-24 17:07:13 +02:00
|
|
|
* Name: unionfs_offsetpath
|
2015-06-05 21:18:06 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2015-06-24 17:07:13 +02:00
|
|
|
static FAR const char *unionfs_offsetpath(FAR const char *relpath,
|
|
|
|
FAR const char *prefix)
|
2015-06-05 21:18:06 +02:00
|
|
|
{
|
|
|
|
FAR const char *trypath;
|
|
|
|
int pfxlen;
|
|
|
|
|
2017-05-11 21:15:13 +02:00
|
|
|
/* Is there a prefix on the path to this file system? */
|
2015-06-05 21:18:06 +02:00
|
|
|
|
|
|
|
if (prefix && (pfxlen = strlen(prefix)) > 0)
|
|
|
|
{
|
|
|
|
/* Does the prefix match? */
|
|
|
|
|
|
|
|
if (strncmp(prefix, relpath, pfxlen) != 0)
|
|
|
|
{
|
|
|
|
/* No, then this relative cannot be within this file system */
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Skip over the prefix */
|
|
|
|
|
|
|
|
trypath = relpath + pfxlen;
|
|
|
|
|
|
|
|
/* Make sure that what is left is a valid, relative path */
|
|
|
|
|
|
|
|
for (; *trypath == '/'; trypath++);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No.. use the full, relative path */
|
|
|
|
|
|
|
|
trypath = relpath;
|
|
|
|
}
|
|
|
|
|
|
|
|
return trypath;
|
|
|
|
}
|
|
|
|
|
2015-06-24 17:07:13 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: unionfs_ispartprefix
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static bool unionfs_ispartprefix(FAR const char *partprefix,
|
|
|
|
FAR const char *prefix)
|
|
|
|
{
|
|
|
|
int partlen = 0;
|
|
|
|
int pfxlen = 0;
|
|
|
|
|
|
|
|
/* Trim any '/' characters in the partial prefix */
|
|
|
|
|
|
|
|
if (partprefix != NULL)
|
|
|
|
{
|
|
|
|
/* Skip over any leading '/' */
|
|
|
|
|
|
|
|
for (; *partprefix == '/'; partprefix++);
|
|
|
|
|
|
|
|
/* Skip over any tailing '/' */
|
|
|
|
|
|
|
|
partlen = strlen(partprefix);
|
|
|
|
for (; partlen > 1 && partprefix[partlen - 1] == '/'; partlen--);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for NUL or empty partial prefix */
|
|
|
|
|
|
|
|
if (partprefix == NULL || *partprefix == '\0')
|
|
|
|
{
|
|
|
|
/* A NUL partial prefix is always contained in the full prefix, even
|
|
|
|
* if there is no prefix.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Trim any '/' characters in the partial prefix */
|
|
|
|
|
|
|
|
if (prefix != NULL)
|
|
|
|
{
|
|
|
|
/* Skip over any leading '/' */
|
|
|
|
|
|
|
|
for (; *prefix == '/'; prefix++);
|
|
|
|
|
|
|
|
/* Skip over any tailing '/' */
|
|
|
|
|
|
|
|
pfxlen = strlen(prefix);
|
|
|
|
for (; pfxlen > 1 && prefix[pfxlen - 1] == '/'; pfxlen--);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for NUL or empty full prefix */
|
|
|
|
|
|
|
|
|
|
|
|
if (prefix == NULL || *prefix == '\0')
|
|
|
|
{
|
2015-06-24 18:19:04 +02:00
|
|
|
/* A non-NUL partial path cannot be a contained in a NUL prefix */
|
2015-06-24 17:07:13 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-06-24 18:19:04 +02:00
|
|
|
#if 0 /* Only whole offset is currently supported */
|
2015-06-24 17:07:13 +02:00
|
|
|
/* Both the partial path and the prefix are non-NULL. Check if the partial
|
|
|
|
* path is contained in the prefix.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (partlen > pfxlen)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2015-06-24 18:19:04 +02:00
|
|
|
#else
|
|
|
|
/* Check if the trimmed offsets are identical */
|
|
|
|
|
|
|
|
if (partlen != pfxlen)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
2015-06-24 17:07:13 +02:00
|
|
|
|
|
|
|
if (strncmp(partprefix, prefix, partlen) == 0)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-05 21:18:06 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: unionfs_tryopen
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int unionfs_tryopen(FAR struct file *filep, FAR const char *relpath,
|
|
|
|
FAR const char *prefix, int oflags, mode_t mode)
|
|
|
|
{
|
|
|
|
FAR const struct mountpt_operations *ops;
|
|
|
|
FAR const char *trypath;
|
|
|
|
|
|
|
|
/* Is this path valid on this file system? */
|
|
|
|
|
2015-06-24 17:07:13 +02:00
|
|
|
trypath = unionfs_offsetpath(relpath, prefix);
|
2015-06-05 21:18:06 +02:00
|
|
|
if (trypath == NULL)
|
|
|
|
{
|
|
|
|
/* No.. return -ENOENT */
|
|
|
|
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Yes.. try to open this directory */
|
|
|
|
|
|
|
|
DEBUGASSERT(filep->f_inode != NULL && filep->f_inode->u.i_mops != NULL);
|
|
|
|
ops = filep->f_inode->u.i_mops;
|
|
|
|
|
|
|
|
if (!ops->open)
|
|
|
|
{
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ops->open(filep, trypath, oflags, mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: unionfs_tryopendir
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int unionfs_tryopendir(FAR struct inode *inode,
|
|
|
|
FAR const char *relpath, FAR const char *prefix,
|
|
|
|
FAR struct fs_dirent_s *dir)
|
|
|
|
{
|
|
|
|
FAR const struct mountpt_operations *ops;
|
|
|
|
FAR const char *trypath;
|
|
|
|
|
|
|
|
/* Is this path valid on this file system? */
|
|
|
|
|
2015-06-24 17:07:13 +02:00
|
|
|
trypath = unionfs_offsetpath(relpath, prefix);
|
2015-06-05 21:18:06 +02:00
|
|
|
if (trypath == NULL)
|
|
|
|
{
|
|
|
|
/* No.. return -ENOENT */
|
|
|
|
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Yes.. Try to open this directory */
|
|
|
|
|
|
|
|
ops = inode->u.i_mops;
|
|
|
|
DEBUGASSERT(ops && ops->opendir);
|
|
|
|
|
|
|
|
if (!ops->opendir)
|
|
|
|
{
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ops->opendir(inode, trypath, dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: unionfs_trymkdir
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int unionfs_trymkdir(FAR struct inode *inode, FAR const char *relpath,
|
|
|
|
FAR const char *prefix, mode_t mode)
|
|
|
|
{
|
|
|
|
FAR const struct mountpt_operations *ops;
|
|
|
|
FAR const char *trypath;
|
|
|
|
|
|
|
|
/* Is this path valid on this file system? */
|
|
|
|
|
2015-06-24 17:07:13 +02:00
|
|
|
trypath = unionfs_offsetpath(relpath, prefix);
|
2015-06-05 21:18:06 +02:00
|
|
|
if (trypath == NULL)
|
|
|
|
{
|
|
|
|
/* No.. return -ENOENT */
|
|
|
|
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Yes.. Try to create the directory */
|
|
|
|
|
|
|
|
ops = inode->u.i_mops;
|
|
|
|
if (!ops->mkdir)
|
|
|
|
{
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ops->mkdir(inode, trypath, mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-02-13 00:05:47 +01:00
|
|
|
* Name: unionfs_tryrename
|
2015-06-05 21:18:06 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int unionfs_tryrename(FAR struct inode *mountpt,
|
|
|
|
FAR const char *oldrelpath,
|
|
|
|
FAR const char *newrelpath,
|
|
|
|
FAR const char *prefix)
|
|
|
|
{
|
|
|
|
FAR const struct mountpt_operations *ops;
|
|
|
|
FAR const char *tryoldpath;
|
|
|
|
FAR const char *trynewpath;
|
|
|
|
|
|
|
|
/* Is source path valid on this file system? */
|
|
|
|
|
2015-06-24 17:07:13 +02:00
|
|
|
tryoldpath = unionfs_offsetpath(oldrelpath, prefix);
|
2015-06-05 21:18:06 +02:00
|
|
|
if (tryoldpath == NULL)
|
|
|
|
{
|
|
|
|
/* No.. return -ENOENT. This should not fail because the existence
|
|
|
|
* of the file has already been verified.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Is source path valid on this file system?
|
|
|
|
* REVISIT: There is no logic currently to rename the file by copying i
|
|
|
|
* to a different file system. So we just fail if the destination name
|
|
|
|
* is not within the same file system. I might, however, be on the other
|
|
|
|
* file system and that rename should be supported as a file copy and
|
|
|
|
* delete.
|
|
|
|
*/
|
|
|
|
|
2015-06-24 17:07:13 +02:00
|
|
|
trynewpath = unionfs_offsetpath(newrelpath, prefix);
|
2015-06-05 21:18:06 +02:00
|
|
|
if (trynewpath == NULL)
|
|
|
|
{
|
|
|
|
/* No.. return -ENOSYS. We should be able to do this, but we can't
|
|
|
|
* yet.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Yes.. Try to rename the file */
|
|
|
|
|
|
|
|
ops = mountpt->u.i_mops;
|
|
|
|
if (!ops->rename)
|
|
|
|
{
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ops->rename(mountpt, tryoldpath, trynewpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: unionfs_trystat
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int unionfs_trystat(FAR struct inode *inode, FAR const char *relpath,
|
|
|
|
FAR const char *prefix, FAR struct stat *buf)
|
|
|
|
{
|
|
|
|
FAR const struct mountpt_operations *ops;
|
|
|
|
FAR const char *trypath;
|
|
|
|
|
|
|
|
/* Is this path valid on this file system? */
|
|
|
|
|
2015-06-24 17:07:13 +02:00
|
|
|
trypath = unionfs_offsetpath(relpath, prefix);
|
2015-06-05 21:18:06 +02:00
|
|
|
if (trypath == NULL)
|
|
|
|
{
|
|
|
|
/* No.. return -ENOENT */
|
|
|
|
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Yes.. Try to create the directory */
|
|
|
|
|
|
|
|
ops = inode->u.i_mops;
|
|
|
|
if (!ops->stat)
|
|
|
|
{
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ops->stat(inode, trypath, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: unionfs_trystatdir
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int unionfs_trystatdir(FAR struct inode *inode,
|
|
|
|
FAR const char *relpath,
|
|
|
|
FAR const char *prefix)
|
|
|
|
{
|
|
|
|
FAR struct stat buf;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Check if relative path refers to a directory. */
|
|
|
|
|
|
|
|
ret = unionfs_trystat(inode, relpath, prefix, &buf);
|
|
|
|
if (ret >= 0 && !S_ISDIR(buf.st_mode))
|
|
|
|
{
|
|
|
|
return -ENOTDIR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-06-24 17:07:13 +02:00
|
|
|
* Name: unionfs_trystatfile
|
2015-06-05 21:18:06 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int unionfs_trystatfile(FAR struct inode *inode,
|
|
|
|
FAR const char *relpath,
|
|
|
|
FAR const char *prefix)
|
|
|
|
{
|
|
|
|
FAR struct stat buf;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Check if relative path refers to a regular file. We specifically
|
|
|
|
* exclude directories but neither do we expect any kind of special file
|
|
|
|
* to reside on the mounted filesystem.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = unionfs_trystat(inode, relpath, prefix, &buf);
|
|
|
|
if (ret >= 0 && !S_ISREG(buf.st_mode))
|
|
|
|
{
|
|
|
|
return -EISDIR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: unionfs_tryrmdir
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int unionfs_tryrmdir(FAR struct inode *inode, FAR const char *relpath,
|
|
|
|
FAR const char *prefix)
|
|
|
|
{
|
|
|
|
FAR const struct mountpt_operations *ops;
|
|
|
|
FAR const char *trypath;
|
|
|
|
|
|
|
|
/* Is this path valid on this file system? */
|
|
|
|
|
2015-06-24 17:07:13 +02:00
|
|
|
trypath = unionfs_offsetpath(relpath, prefix);
|
2015-06-05 21:18:06 +02:00
|
|
|
if (trypath == NULL)
|
|
|
|
{
|
|
|
|
/* No.. return -ENOENT */
|
|
|
|
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
2015-06-06 19:33:09 +02:00
|
|
|
/* Yes.. Try to remove the directory */
|
2015-06-05 21:18:06 +02:00
|
|
|
|
|
|
|
ops = inode->u.i_mops;
|
|
|
|
if (!ops->rmdir)
|
|
|
|
{
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ops->rmdir(inode, trypath);
|
|
|
|
}
|
|
|
|
|
2015-06-06 19:33:09 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: unionfs_tryunlink
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int unionfs_tryunlink(FAR struct inode *inode,
|
|
|
|
FAR const char *relpath,
|
|
|
|
FAR const char *prefix)
|
|
|
|
{
|
|
|
|
FAR const struct mountpt_operations *ops;
|
|
|
|
FAR const char *trypath;
|
|
|
|
|
|
|
|
/* Is this path valid on this file system? */
|
|
|
|
|
2015-06-24 17:07:13 +02:00
|
|
|
trypath = unionfs_offsetpath(relpath, prefix);
|
2015-06-06 19:33:09 +02:00
|
|
|
if (trypath == NULL)
|
|
|
|
{
|
|
|
|
/* No.. return -ENOENT */
|
|
|
|
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Yes.. Try to unlink the file */
|
|
|
|
|
|
|
|
ops = inode->u.i_mops;
|
|
|
|
if (!ops->unlink)
|
|
|
|
{
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ops->unlink(inode, trypath);
|
|
|
|
}
|
|
|
|
|
2015-06-06 17:49:13 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: unionfs_relpath
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static FAR char *unionfs_relpath(FAR const char *path, FAR const char *name)
|
|
|
|
{
|
|
|
|
FAR char *relpath;
|
|
|
|
int pathlen;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Check if there is a valid, non-zero-legnth path */
|
|
|
|
|
|
|
|
if (path && (pathlen = strlen(path)) > 0)
|
|
|
|
{
|
|
|
|
/* Yes.. extend the file name by prepending the path */
|
|
|
|
|
|
|
|
if (path[pathlen-1] == '/')
|
|
|
|
{
|
|
|
|
ret = asprintf(&relpath, "%s%s", path, name);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = asprintf(&relpath, "%s/%s", path, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle errors */
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return relpath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* There is no path... just duplicate the name (so that kmm_free()
|
|
|
|
* will work later).
|
|
|
|
*/
|
|
|
|
|
|
|
|
return strdup(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-07 03:48:38 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: unionfs_unbind_child
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int unionfs_unbind_child(FAR struct unionfs_mountpt_s *um)
|
|
|
|
{
|
|
|
|
FAR struct inode *mpinode = um->um_node;
|
|
|
|
FAR struct inode *bdinode = NULL;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Unbind the block driver from the file system (destroying any fs
|
2015-06-08 22:16:32 +02:00
|
|
|
* private data). This logic is essentially the same as the logic in
|
|
|
|
* nuttx/fs/mount/fs_umount2.c.
|
2015-06-07 03:48:38 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (!mpinode->u.i_mops->unbind)
|
|
|
|
{
|
|
|
|
/* The filesystem does not support the unbind operation ??? */
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The unbind method returns the number of references to the file system
|
|
|
|
* (i.e., open files), zero if the unbind was performed, or a negated
|
|
|
|
* error code on a failure.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = mpinode->u.i_mops->unbind(mpinode->i_private, &bdinode, MNT_FORCE);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* Some failure occurred */
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else if (ret > 0)
|
|
|
|
{
|
|
|
|
/* REVISIT: This is bad if the file sysem cannot support a deferred
|
|
|
|
* unmount. Ideally it would perform the unmount when the last file
|
|
|
|
* is closed. But I don't think any file system do that.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Successfully unbound */
|
|
|
|
|
|
|
|
mpinode->i_private = NULL;
|
|
|
|
|
|
|
|
/* Release the mountpoint inode and any block driver inode
|
|
|
|
* returned by the file system unbind above. This should cause
|
|
|
|
* the inode to be deleted (unless there are other references)
|
|
|
|
*/
|
|
|
|
|
|
|
|
inode_release(mpinode);
|
|
|
|
|
|
|
|
/* Did the unbind method return a contained block driver */
|
|
|
|
|
|
|
|
if (bdinode)
|
|
|
|
{
|
|
|
|
inode_release(bdinode);
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2015-06-05 21:18:06 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: unionfs_destroy
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void unionfs_destroy(FAR struct unionfs_inode_s *ui)
|
|
|
|
{
|
|
|
|
DEBUGASSERT(ui != NULL && ui->ui_fs[0].um_node != NULL &&
|
|
|
|
ui->ui_fs[1].um_node != NULL && ui->ui_nopen == 0);
|
|
|
|
|
2015-06-07 03:48:38 +02:00
|
|
|
/* Unbind the contained file systems */
|
2015-06-05 21:18:06 +02:00
|
|
|
|
2015-06-07 03:48:38 +02:00
|
|
|
(void)unionfs_unbind_child(&ui->ui_fs[0]);
|
|
|
|
(void)unionfs_unbind_child(&ui->ui_fs[1]);
|
2015-06-05 21:18:06 +02:00
|
|
|
|
|
|
|
/* Free any allocated prefix strings */
|
|
|
|
|
2015-06-24 17:07:13 +02:00
|
|
|
if (ui->ui_fs[0].um_prefix)
|
2015-06-05 21:18:06 +02:00
|
|
|
{
|
|
|
|
kmm_free(ui->ui_fs[0].um_prefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ui->ui_fs[1].um_prefix)
|
|
|
|
{
|
|
|
|
kmm_free(ui->ui_fs[1].um_prefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* And finally free the allocated unionfs state structure as well */
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_destroy(&ui->ui_exclsem);
|
2015-06-05 21:18:06 +02:00
|
|
|
kmm_free(ui);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: unionfs_open
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int unionfs_open(FAR struct file *filep, FAR const char *relpath,
|
|
|
|
int oflags, mode_t mode)
|
|
|
|
{
|
|
|
|
FAR struct unionfs_inode_s *ui;
|
|
|
|
FAR struct unionfs_file_s *uf;
|
|
|
|
FAR struct unionfs_mountpt_s *um;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Recover the open file data from the struct file instance */
|
|
|
|
|
|
|
|
DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
|
|
|
|
ui = (FAR struct unionfs_inode_s *)filep->f_inode->i_private;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Opening: ui_nopen=%d\n", ui->ui_nopen);
|
2015-06-05 21:18:06 +02:00
|
|
|
|
|
|
|
/* Get exclusive access to the file system data structures */
|
|
|
|
|
|
|
|
ret = unionfs_semtake(ui, false);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-06-06 21:53:36 +02:00
|
|
|
/* Allocate a container to hold the open file system information */
|
2015-06-05 21:18:06 +02:00
|
|
|
|
|
|
|
uf = (FAR struct unionfs_file_s *)kmm_malloc(sizeof(struct unionfs_file_s));
|
|
|
|
if (uf == NULL)
|
|
|
|
{
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout_with_semaphore;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try to open the file on file system 1 */
|
|
|
|
|
|
|
|
um = &ui->ui_fs[0];
|
2015-06-05 22:23:07 +02:00
|
|
|
DEBUGASSERT(um != NULL && um->um_node != NULL && um->um_node->u.i_mops != NULL);
|
2015-06-05 21:18:06 +02:00
|
|
|
|
|
|
|
uf->uf_file.f_oflags = filep->f_oflags;
|
|
|
|
uf->uf_file.f_pos = 0;
|
|
|
|
uf->uf_file.f_inode = um->um_node;
|
|
|
|
uf->uf_file.f_priv = NULL;
|
|
|
|
|
|
|
|
ret = unionfs_tryopen(&uf->uf_file, relpath, um->um_prefix, oflags, mode);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
/* Successfully opened on file system 1 */
|
|
|
|
|
|
|
|
uf->uf_ndx = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Try to open the file on file system 1 */
|
|
|
|
|
|
|
|
um = &ui->ui_fs[1];
|
|
|
|
|
|
|
|
uf->uf_file.f_oflags = filep->f_oflags;
|
|
|
|
uf->uf_file.f_pos = 0;
|
|
|
|
uf->uf_file.f_inode = um->um_node;
|
|
|
|
uf->uf_file.f_priv = NULL;
|
|
|
|
|
|
|
|
ret = unionfs_tryopen(&uf->uf_file, relpath, um->um_prefix, oflags, mode);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_semaphore;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Successfully opened on file system 1 */
|
|
|
|
|
|
|
|
uf->uf_ndx = 1;
|
|
|
|
}
|
|
|
|
|
2015-06-06 21:53:36 +02:00
|
|
|
/* Increment the open reference count */
|
|
|
|
|
|
|
|
ui->ui_nopen++;
|
|
|
|
DEBUGASSERT(ui->ui_nopen > 0);
|
|
|
|
|
|
|
|
/* Save our private data in the file structure */
|
|
|
|
|
2015-06-05 21:18:06 +02:00
|
|
|
filep->f_priv = (FAR void *)uf;
|
|
|
|
ret = OK;
|
|
|
|
|
|
|
|
errout_with_semaphore:
|
|
|
|
unionfs_semgive(ui);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: unionfs_close
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int unionfs_close(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct unionfs_inode_s *ui;
|
|
|
|
FAR struct unionfs_file_s *uf;
|
|
|
|
FAR struct unionfs_mountpt_s *um;
|
|
|
|
FAR const struct mountpt_operations *ops;
|
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
/* Recover the open file data from the struct file instance */
|
|
|
|
|
|
|
|
DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
|
|
|
|
ui = (FAR struct unionfs_inode_s *)filep->f_inode->i_private;
|
|
|
|
|
|
|
|
/* Get exclusive access to the file system data structures */
|
|
|
|
|
|
|
|
(void)unionfs_semtake(ui, false);
|
|
|
|
|
|
|
|
DEBUGASSERT(ui != NULL && filep->f_priv != NULL);
|
|
|
|
uf = (FAR struct unionfs_file_s *)filep->f_priv;
|
|
|
|
|
|
|
|
DEBUGASSERT(uf->uf_ndx == 0 || uf->uf_ndx == 1);
|
|
|
|
um = &ui->ui_fs[uf->uf_ndx];
|
|
|
|
|
|
|
|
DEBUGASSERT(um != NULL && um->um_node != NULL && um->um_node->u.i_mops != NULL);
|
|
|
|
ops = um->um_node->u.i_mops;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Closing: ui_nopen=%d\n", ui->ui_nopen);
|
2015-06-05 21:18:06 +02:00
|
|
|
|
|
|
|
/* Perform the lower level close operation */
|
|
|
|
|
2018-01-03 23:03:56 +01:00
|
|
|
if (ops->close != NULL)
|
2015-06-05 21:18:06 +02:00
|
|
|
{
|
|
|
|
ret = ops->close(&uf->uf_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Decrement the count of open reference. If that count would go to zero
|
2015-06-06 19:33:09 +02:00
|
|
|
* and if the file system has been unmounted, then destroy the file system
|
|
|
|
* now.
|
2015-06-05 21:18:06 +02:00
|
|
|
*/
|
|
|
|
|
2015-06-06 19:33:09 +02:00
|
|
|
if (--ui->ui_nopen <= 0 && ui->ui_unmounted)
|
2015-06-05 21:18:06 +02:00
|
|
|
{
|
|
|
|
unionfs_destroy(ui);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free the open file container */
|
|
|
|
|
|
|
|
kmm_free(uf);
|
|
|
|
filep->f_priv = NULL;
|
|
|
|
unionfs_semgive(ui);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: unionfs_read
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t unionfs_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen)
|
|
|
|
{
|
|
|
|
FAR struct unionfs_inode_s *ui;
|
|
|
|
FAR struct unionfs_file_s *uf;
|
|
|
|
FAR struct unionfs_mountpt_s *um;
|
|
|
|
FAR const struct mountpt_operations *ops;
|
|
|
|
int ret = -EPERM;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("buflen: %lu\n", (unsigned long)buflen);
|
2015-06-05 21:18:06 +02:00
|
|
|
|
|
|
|
/* Recover the open file data from the struct file instance */
|
|
|
|
|
|
|
|
DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
|
|
|
|
ui = (FAR struct unionfs_inode_s *)filep->f_inode->i_private;
|
|
|
|
|
|
|
|
/* Get exclusive access to the file system data structures */
|
|
|
|
|
|
|
|
ret = unionfs_semtake(ui, false);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUGASSERT(ui != NULL && filep->f_priv != NULL);
|
|
|
|
uf = (FAR struct unionfs_file_s *)filep->f_priv;
|
|
|
|
|
|
|
|
DEBUGASSERT(uf->uf_ndx == 0 || uf->uf_ndx == 1);
|
|
|
|
um = &ui->ui_fs[uf->uf_ndx];
|
|
|
|
|
|
|
|
DEBUGASSERT(um != NULL && um->um_node != NULL && um->um_node->u.i_mops != NULL);
|
|
|
|
ops = um->um_node->u.i_mops;
|
|
|
|
|
2018-01-03 23:03:56 +01:00
|
|
|
/* Perform the lower level read operation */
|
2015-06-05 21:18:06 +02:00
|
|
|
|
2018-01-03 23:03:56 +01:00
|
|
|
if (ops->read != NULL)
|
2015-06-05 21:18:06 +02:00
|
|
|
{
|
|
|
|
ret = ops->read(&uf->uf_file, buffer, buflen);
|
|
|
|
}
|
|
|
|
|
|
|
|
unionfs_semgive(ui);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: unionfs_write
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t unionfs_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen)
|
|
|
|
{
|
|
|
|
FAR struct unionfs_inode_s *ui;
|
|
|
|
FAR struct unionfs_file_s *uf;
|
|
|
|
FAR struct unionfs_mountpt_s *um;
|
|
|
|
FAR const struct mountpt_operations *ops;
|
|
|
|
int ret = -EPERM;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("buflen: %lu\n", (unsigned long)buflen);
|
2015-06-05 21:18:06 +02:00
|
|
|
|
|
|
|
/* Recover the open file data from the struct file instance */
|
|
|
|
|
|
|
|
DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
|
|
|
|
ui = (FAR struct unionfs_inode_s *)filep->f_inode->i_private;
|
|
|
|
|
|
|
|
/* Get exclusive access to the file system data structures */
|
|
|
|
|
|
|
|
ret = unionfs_semtake(ui, false);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUGASSERT(ui != NULL && filep->f_priv != NULL);
|
|
|
|
uf = (FAR struct unionfs_file_s *)filep->f_priv;
|
|
|
|
|
|
|
|
DEBUGASSERT(uf->uf_ndx == 0 || uf->uf_ndx == 1);
|
|
|
|
um = &ui->ui_fs[uf->uf_ndx];
|
|
|
|
|
|
|
|
DEBUGASSERT(um != NULL && um->um_node != NULL && um->um_node->u.i_mops != NULL);
|
|
|
|
ops = um->um_node->u.i_mops;
|
|
|
|
|
|
|
|
/* Perform the lower level write operation */
|
|
|
|
|
2018-01-03 23:03:56 +01:00
|
|
|
if (ops->write != NULL)
|
2015-06-05 21:18:06 +02:00
|
|
|
{
|
|
|
|
ret = ops->write(&uf->uf_file, buffer, buflen);
|
|
|
|
}
|
|
|
|
|
|
|
|
unionfs_semgive(ui);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: unionfs_seek
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static off_t unionfs_seek(FAR struct file *filep, off_t offset, int whence)
|
|
|
|
{
|
|
|
|
FAR struct unionfs_inode_s *ui;
|
|
|
|
FAR struct unionfs_file_s *uf;
|
|
|
|
FAR struct unionfs_mountpt_s *um;
|
|
|
|
FAR const struct mountpt_operations *ops;
|
|
|
|
int ret;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("offset: %lu whence: %d\n", (unsigned long)offset, whence);
|
2015-06-05 21:18:06 +02:00
|
|
|
|
|
|
|
/* Recover the open file data from the struct file instance */
|
|
|
|
|
|
|
|
DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
|
|
|
|
ui = (FAR struct unionfs_inode_s *)filep->f_inode->i_private;
|
|
|
|
|
|
|
|
/* Get exclusive access to the file system data structures */
|
|
|
|
|
|
|
|
ret = unionfs_semtake(ui, false);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUGASSERT(ui != NULL && filep->f_priv != NULL);
|
|
|
|
uf = (FAR struct unionfs_file_s *)filep->f_priv;
|
|
|
|
|
|
|
|
DEBUGASSERT(uf->uf_ndx == 0 || uf->uf_ndx == 1);
|
|
|
|
um = &ui->ui_fs[uf->uf_ndx];
|
|
|
|
|
|
|
|
DEBUGASSERT(um != NULL && um->um_node != NULL && um->um_node->u.i_mops != NULL);
|
|
|
|
ops = um->um_node->u.i_mops;
|
|
|
|
|
|
|
|
/* Invoke the file seek method if available */
|
|
|
|
|
2018-01-03 23:03:56 +01:00
|
|
|
if (ops->seek != NULL)
|
2015-06-05 21:18:06 +02:00
|
|
|
{
|
|
|
|
offset = ops->seek(&uf->uf_file, offset, whence);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No... Just set the common file position value */
|
|
|
|
|
|
|
|
switch (whence)
|
|
|
|
{
|
|
|
|
case SEEK_CUR:
|
|
|
|
offset += filep->f_pos;
|
|
|
|
|
|
|
|
case SEEK_SET:
|
|
|
|
if (offset >= 0)
|
|
|
|
{
|
|
|
|
filep->f_pos = offset; /* Might be beyond the end-of-file */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
offset = (off_t)-EINVAL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SEEK_END:
|
|
|
|
offset = (off_t)-ENOSYS;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
offset = (off_t)-EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unionfs_semgive(ui);
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: unionfs_ioctl
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int unionfs_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
FAR struct unionfs_inode_s *ui;
|
|
|
|
FAR struct unionfs_file_s *uf;
|
|
|
|
FAR struct unionfs_mountpt_s *um;
|
|
|
|
FAR const struct mountpt_operations *ops;
|
|
|
|
int ret = -ENOTTY;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("cmd: %d arg: %lu\n", cmd, arg);
|
2015-06-05 21:18:06 +02:00
|
|
|
|
|
|
|
/* Recover the open file data from the struct file instance */
|
|
|
|
|
|
|
|
DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
|
|
|
|
ui = (FAR struct unionfs_inode_s *)filep->f_inode->i_private;
|
|
|
|
|
|
|
|
/* Get exclusive access to the file system data structures */
|
|
|
|
|
|
|
|
ret = unionfs_semtake(ui, false);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUGASSERT(ui != NULL && filep->f_priv != NULL);
|
|
|
|
uf = (FAR struct unionfs_file_s *)filep->f_priv;
|
|
|
|
|
|
|
|
DEBUGASSERT(uf->uf_ndx == 0 || uf->uf_ndx == 1);
|
|
|
|
um = &ui->ui_fs[uf->uf_ndx];
|
|
|
|
|
|
|
|
DEBUGASSERT(um != NULL && um->um_node != NULL && um->um_node->u.i_mops != NULL);
|
|
|
|
ops = um->um_node->u.i_mops;
|
|
|
|
|
|
|
|
/* Perform the lower level ioctl operation */
|
|
|
|
|
2018-01-03 23:03:56 +01:00
|
|
|
if (ops->ioctl != NULL)
|
2015-06-05 21:18:06 +02:00
|
|
|
{
|
|
|
|
ret = ops->ioctl(&uf->uf_file, cmd, arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
unionfs_semgive(ui);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: unionfs_sync
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int unionfs_sync(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct unionfs_inode_s *ui;
|
|
|
|
FAR struct unionfs_file_s *uf;
|
|
|
|
FAR struct unionfs_mountpt_s *um;
|
|
|
|
FAR const struct mountpt_operations *ops;
|
|
|
|
int ret = -EINVAL;
|
|
|
|
|
2018-01-03 23:03:56 +01:00
|
|
|
finfo("filep=%p\n", filep);
|
2015-06-05 21:18:06 +02:00
|
|
|
|
|
|
|
/* Recover the open file data from the struct file instance */
|
|
|
|
|
|
|
|
DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
|
|
|
|
ui = (FAR struct unionfs_inode_s *)filep->f_inode->i_private;
|
|
|
|
|
|
|
|
/* Get exclusive access to the file system data structures */
|
|
|
|
|
|
|
|
ret = unionfs_semtake(ui, false);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUGASSERT(ui != NULL && filep->f_priv != NULL);
|
|
|
|
uf = (FAR struct unionfs_file_s *)filep->f_priv;
|
|
|
|
|
|
|
|
DEBUGASSERT(uf->uf_ndx == 0 || uf->uf_ndx == 1);
|
|
|
|
um = &ui->ui_fs[uf->uf_ndx];
|
|
|
|
|
|
|
|
DEBUGASSERT(um != NULL && um->um_node != NULL && um->um_node->u.i_mops != NULL);
|
|
|
|
ops = um->um_node->u.i_mops;
|
|
|
|
|
|
|
|
/* Perform the lower level sync operation */
|
|
|
|
|
2018-01-03 23:03:56 +01:00
|
|
|
if (ops->sync != NULL)
|
2015-06-05 21:18:06 +02:00
|
|
|
{
|
|
|
|
ret = ops->sync(&uf->uf_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
unionfs_semgive(ui);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: unionfs_dup
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int unionfs_dup(FAR const struct file *oldp, FAR struct file *newp)
|
|
|
|
{
|
|
|
|
FAR struct unionfs_file_s *oldpriv;
|
|
|
|
FAR struct unionfs_file_s *newpriv;
|
|
|
|
FAR struct unionfs_inode_s *ui;
|
|
|
|
FAR struct unionfs_mountpt_s *um;
|
|
|
|
FAR const struct mountpt_operations *ops;
|
|
|
|
int ret = -ENOMEM;
|
|
|
|
|
2018-01-03 23:03:56 +01:00
|
|
|
finfo("oldp=%p newp=%p\n", oldp, newp);
|
2015-06-05 21:18:06 +02:00
|
|
|
|
|
|
|
/* Recover the open file data from the struct file instance */
|
|
|
|
|
|
|
|
DEBUGASSERT(oldp != NULL && oldp->f_inode != NULL);
|
|
|
|
ui = (FAR struct unionfs_inode_s *)oldp->f_inode->i_private;
|
|
|
|
|
|
|
|
/* Get exclusive access to the file system data structures */
|
|
|
|
|
|
|
|
ret = unionfs_semtake(ui, false);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUGASSERT(ui != NULL && oldp->f_priv != NULL);
|
|
|
|
oldpriv = (FAR struct unionfs_file_s *)oldp->f_priv;
|
|
|
|
|
|
|
|
DEBUGASSERT(oldpriv->uf_ndx == 0 || oldpriv->uf_ndx == 1);
|
|
|
|
um = &ui->ui_fs[oldpriv->uf_ndx];
|
|
|
|
|
|
|
|
DEBUGASSERT(um != NULL && um->um_node != NULL && um->um_node->u.i_mops != NULL);
|
|
|
|
ops = um->um_node->u.i_mops;
|
|
|
|
|
|
|
|
DEBUGASSERT(newp != NULL && newp->f_priv == NULL);
|
|
|
|
|
|
|
|
/* Allocate a new container for the union FS open file */
|
|
|
|
|
|
|
|
newpriv = (FAR struct unionfs_file_s *)kmm_malloc(sizeof(struct unionfs_file_s));
|
|
|
|
if (newpriv != NULL)
|
|
|
|
{
|
|
|
|
/* Clone the old file structure into the newly allocated one */
|
|
|
|
|
|
|
|
memcpy(newpriv, oldpriv, sizeof(struct unionfs_file_s));
|
|
|
|
newpriv->uf_file.f_priv = NULL;
|
|
|
|
|
|
|
|
/* Then perform the lower lowel dup operation */
|
|
|
|
|
|
|
|
ret = OK;
|
2018-01-03 23:03:56 +01:00
|
|
|
if (ops->dup != NULL)
|
2015-06-05 21:18:06 +02:00
|
|
|
{
|
|
|
|
ret = ops->dup(&oldpriv->uf_file, &newpriv->uf_file);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
kmm_free(newpriv);
|
|
|
|
newpriv = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the new container in the new file structure */
|
|
|
|
|
|
|
|
newp->f_priv = newpriv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-02-12 20:42:27 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: unionfs_fstat
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Obtain information about an open file associated with the file
|
|
|
|
* descriptor 'fd', and will write it to the area pointed to by 'buf'.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int unionfs_fstat(FAR const struct file *filep, FAR struct stat *buf)
|
|
|
|
{
|
2017-02-13 00:05:47 +01:00
|
|
|
FAR struct unionfs_inode_s *ui;
|
|
|
|
FAR struct unionfs_file_s *uf;
|
|
|
|
FAR struct unionfs_mountpt_s *um;
|
|
|
|
FAR const struct mountpt_operations *ops;
|
|
|
|
int ret = -EPERM;
|
|
|
|
|
2018-01-03 23:03:56 +01:00
|
|
|
finfo("filep=%p buf=%p\n");
|
2017-02-13 00:05:47 +01:00
|
|
|
|
|
|
|
/* Recover the open file data from the struct file instance */
|
|
|
|
|
|
|
|
DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
|
|
|
|
ui = (FAR struct unionfs_inode_s *)filep->f_inode->i_private;
|
|
|
|
|
|
|
|
/* Get exclusive access to the file system data structures */
|
|
|
|
|
|
|
|
ret = unionfs_semtake(ui, false);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUGASSERT(ui != NULL && filep->f_priv != NULL);
|
|
|
|
uf = (FAR struct unionfs_file_s *)filep->f_priv;
|
|
|
|
|
|
|
|
DEBUGASSERT(uf->uf_ndx == 0 || uf->uf_ndx == 1);
|
|
|
|
um = &ui->ui_fs[uf->uf_ndx];
|
|
|
|
|
|
|
|
DEBUGASSERT(um != NULL && um->um_node != NULL && um->um_node->u.i_mops != NULL);
|
|
|
|
ops = um->um_node->u.i_mops;
|
|
|
|
|
|
|
|
/* Perform the lower level write operation */
|
|
|
|
|
2018-01-03 23:03:56 +01:00
|
|
|
if (ops->fstat != NULL)
|
2017-02-13 00:05:47 +01:00
|
|
|
{
|
|
|
|
ret = ops->fstat(&uf->uf_file, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
unionfs_semgive(ui);
|
|
|
|
return ret;
|
2017-02-12 20:42:27 +01:00
|
|
|
}
|
|
|
|
|
2018-01-03 23:03:56 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: unionfs_truncate
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Set the size of the file references by 'filep' to 'length'.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int unionfs_truncate(FAR struct file *filep, off_t length)
|
|
|
|
{
|
|
|
|
FAR struct unionfs_inode_s *ui;
|
|
|
|
FAR struct unionfs_file_s *uf;
|
|
|
|
FAR struct unionfs_mountpt_s *um;
|
|
|
|
FAR const struct mountpt_operations *ops;
|
|
|
|
int ret = -EPERM;
|
|
|
|
|
|
|
|
finfo("filep=%p length=%ld\n", filep, (long)length);
|
|
|
|
|
|
|
|
/* Recover the open file data from the struct file instance */
|
|
|
|
|
|
|
|
DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
|
|
|
|
ui = (FAR struct unionfs_inode_s *)filep->f_inode->i_private;
|
|
|
|
|
|
|
|
/* Get exclusive access to the file system data structures */
|
|
|
|
|
|
|
|
ret = unionfs_semtake(ui, false);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUGASSERT(ui != NULL && filep->f_priv != NULL);
|
|
|
|
uf = (FAR struct unionfs_file_s *)filep->f_priv;
|
|
|
|
|
|
|
|
DEBUGASSERT(uf->uf_ndx == 0 || uf->uf_ndx == 1);
|
|
|
|
um = &ui->ui_fs[uf->uf_ndx];
|
|
|
|
|
|
|
|
DEBUGASSERT(um != NULL && um->um_node != NULL && um->um_node->u.i_mops != NULL);
|
|
|
|
ops = um->um_node->u.i_mops;
|
|
|
|
|
|
|
|
/* Perform the lower level write operation */
|
|
|
|
|
|
|
|
if (ops->truncate != NULL)
|
|
|
|
{
|
|
|
|
ret = ops->truncate(&uf->uf_file, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
unionfs_semgive(ui);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-06-05 21:18:06 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: unionfs_opendir
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int unionfs_opendir(FAR struct inode *mountpt, FAR const char *relpath,
|
|
|
|
FAR struct fs_dirent_s *dir)
|
|
|
|
{
|
|
|
|
FAR struct unionfs_inode_s *ui;
|
|
|
|
FAR struct unionfs_mountpt_s *um;
|
|
|
|
FAR struct fs_unionfsdir_s *fu;
|
|
|
|
FAR const struct mountpt_operations *ops;
|
|
|
|
FAR struct fs_dirent_s *lowerdir;
|
|
|
|
int ret;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("relpath: \"%s\"\n", relpath ? relpath : "NULL");
|
2015-06-05 21:18:06 +02:00
|
|
|
|
|
|
|
/* Recover the filesystem data from the struct inode instance */
|
|
|
|
|
|
|
|
DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
|
|
|
|
ui = (FAR struct unionfs_inode_s *)mountpt->i_private;
|
|
|
|
|
|
|
|
/* Get exclusive access to the file system data structures */
|
|
|
|
|
|
|
|
ret = unionfs_semtake(ui, false);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2015-06-06 15:36:38 +02:00
|
|
|
return ret;
|
2015-06-05 21:18:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DEBUGASSERT(dir);
|
|
|
|
fu = &dir->u.unionfs;
|
|
|
|
|
2015-06-06 17:49:13 +02:00
|
|
|
/* Clone the path. We will need this when we traverse file system 2 to
|
|
|
|
* omit duplicates on file system 1.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (relpath && strlen(relpath) > 0)
|
|
|
|
{
|
|
|
|
fu->fu_relpath = strdup(relpath);
|
|
|
|
if (!fu->fu_relpath)
|
|
|
|
{
|
|
|
|
goto errout_with_semaphore;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-05 21:18:06 +02:00
|
|
|
/* Allocate another dirent structure for the lower file system */
|
|
|
|
|
|
|
|
lowerdir = (FAR struct fs_dirent_s *)kmm_zalloc(sizeof(struct fs_dirent_s));
|
|
|
|
if (lowerdir == NULL)
|
|
|
|
{
|
2015-06-06 15:36:38 +02:00
|
|
|
ret = -ENOMEM;
|
2015-06-06 17:49:13 +02:00
|
|
|
goto errout_with_relpath;
|
2015-06-05 21:18:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check file system 2 first. */
|
|
|
|
|
|
|
|
um = &ui->ui_fs[1];
|
|
|
|
lowerdir->fd_root = um->um_node;
|
|
|
|
ret = unionfs_tryopendir(um->um_node, relpath, um->um_prefix, lowerdir);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
2015-06-06 15:58:23 +02:00
|
|
|
/* Save the file system 2 access info */
|
2015-06-05 21:18:06 +02:00
|
|
|
|
|
|
|
fu->fu_ndx = 1;
|
|
|
|
fu->fu_lower[1] = lowerdir;
|
|
|
|
|
|
|
|
/* Allocate yet another dirent structure for the lower file system 1 */
|
|
|
|
|
|
|
|
lowerdir = (FAR struct fs_dirent_s *)kmm_zalloc(sizeof(struct fs_dirent_s));
|
|
|
|
if (lowerdir == NULL)
|
|
|
|
{
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout_with_fs2open;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-24 17:07:13 +02:00
|
|
|
/* Check if the user is stat'ing some "fake" node between the unionfs root and
|
|
|
|
* the file system 1/2 root directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
else if (unionfs_ispartprefix(relpath, ui->ui_fs[1].um_prefix))
|
|
|
|
{
|
|
|
|
/* File system 2 prefix includes this relpath */
|
|
|
|
|
|
|
|
fu->fu_ndx = 1;
|
|
|
|
fu->fu_prefix[1] = true;
|
|
|
|
}
|
|
|
|
|
2015-06-05 21:18:06 +02:00
|
|
|
/* Check file system 1 last, possibly overwriting fu_ndx */
|
|
|
|
|
2015-06-06 15:36:38 +02:00
|
|
|
um = &ui->ui_fs[0];
|
2015-06-05 21:18:06 +02:00
|
|
|
lowerdir->fd_root = um->um_node;
|
|
|
|
ret = unionfs_tryopendir(um->um_node, relpath, um->um_prefix, lowerdir);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
2015-06-06 15:58:23 +02:00
|
|
|
/* Save the file system 1 access info */
|
2015-06-05 21:18:06 +02:00
|
|
|
|
|
|
|
fu->fu_ndx = 0;
|
|
|
|
fu->fu_lower[0] = lowerdir;
|
|
|
|
}
|
2015-06-06 15:58:23 +02:00
|
|
|
else
|
2015-06-05 21:18:06 +02:00
|
|
|
{
|
2015-06-06 15:58:23 +02:00
|
|
|
/* File system 1 was not opened... then we won't be needing that last
|
|
|
|
* localdir allocation after all.
|
|
|
|
*/
|
|
|
|
|
|
|
|
kmm_free(lowerdir);
|
|
|
|
|
2015-06-24 17:07:13 +02:00
|
|
|
/* Check if the user is stat'ing some "fake" node between the unionfs
|
|
|
|
* root and the file system 1 root directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (unionfs_ispartprefix(relpath, ui->ui_fs[0].um_prefix))
|
|
|
|
{
|
|
|
|
/* File system 1 offset includes this relpath. Make sure that only one */
|
|
|
|
|
|
|
|
fu->fu_ndx = 0;
|
|
|
|
fu->fu_prefix[0] = true;
|
|
|
|
fu->fu_prefix[1] = false;
|
|
|
|
}
|
|
|
|
|
2015-06-06 15:58:23 +02:00
|
|
|
/* If the directory was not found on either file system, then we have
|
2015-06-24 17:07:13 +02:00
|
|
|
* failed to open this path on either file system.
|
2015-06-06 15:58:23 +02:00
|
|
|
*/
|
2015-06-05 21:18:06 +02:00
|
|
|
|
2015-06-24 17:07:13 +02:00
|
|
|
else if (fu->fu_lower[1] == NULL && !fu->fu_prefix[1])
|
2015-06-06 15:58:23 +02:00
|
|
|
{
|
2015-06-24 17:07:13 +02:00
|
|
|
/* Neither of the two path file systems include this relpath */
|
2015-06-06 15:58:23 +02:00
|
|
|
|
2015-06-24 17:07:13 +02:00
|
|
|
ret = -ENOENT;
|
2015-06-06 17:49:13 +02:00
|
|
|
goto errout_with_relpath;
|
2015-06-06 15:58:23 +02:00
|
|
|
}
|
2015-06-05 21:18:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Increment the number of open references and return success */
|
|
|
|
|
|
|
|
ui->ui_nopen++;
|
|
|
|
DEBUGASSERT(ui->ui_nopen > 0);
|
|
|
|
|
|
|
|
unionfs_semgive(ui);
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout_with_fs2open:
|
|
|
|
ops = ui->ui_fs[1].um_node->u.i_mops;
|
|
|
|
DEBUGASSERT(ops != NULL);
|
2018-01-03 23:03:56 +01:00
|
|
|
if (ops->closedir != NULL)
|
2015-06-05 21:18:06 +02:00
|
|
|
{
|
2015-06-06 15:36:38 +02:00
|
|
|
ret = ops->closedir(um->um_node, fu->fu_lower[1]);
|
2015-06-05 21:18:06 +02:00
|
|
|
}
|
|
|
|
|
2015-06-06 15:36:38 +02:00
|
|
|
kmm_free(fu->fu_lower[1]);
|
|
|
|
|
2015-06-06 17:49:13 +02:00
|
|
|
errout_with_relpath:
|
|
|
|
if (fu->fu_relpath != NULL)
|
|
|
|
{
|
|
|
|
kmm_free(fu->fu_relpath);
|
|
|
|
}
|
|
|
|
|
2015-06-06 15:36:38 +02:00
|
|
|
errout_with_semaphore:
|
2015-06-05 21:18:06 +02:00
|
|
|
unionfs_semgive(ui);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: unionfs_closedir
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int unionfs_closedir(FAR struct inode *mountpt,
|
|
|
|
FAR struct fs_dirent_s *dir)
|
|
|
|
{
|
|
|
|
FAR struct unionfs_inode_s *ui;
|
|
|
|
FAR struct unionfs_mountpt_s *um;
|
|
|
|
FAR const struct mountpt_operations *ops;
|
|
|
|
FAR struct fs_unionfsdir_s *fu;
|
|
|
|
int ret = OK;
|
|
|
|
int i;
|
|
|
|
|
2018-01-03 23:03:56 +01:00
|
|
|
finfo("mountpt=%p dir=%p\n", mountpt, dir);
|
2015-06-05 21:18:06 +02:00
|
|
|
|
|
|
|
/* Recover the union file system data from the struct inode instance */
|
|
|
|
|
|
|
|
DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
|
|
|
|
ui = (FAR struct unionfs_inode_s *)mountpt->i_private;
|
|
|
|
|
|
|
|
/* Get exclusive access to the file system data structures */
|
|
|
|
|
|
|
|
(void)unionfs_semtake(ui, true);
|
|
|
|
|
|
|
|
DEBUGASSERT(dir);
|
|
|
|
fu = &dir->u.unionfs;
|
|
|
|
|
|
|
|
/* Close both contained file systems */
|
|
|
|
|
|
|
|
for (i = 0; i < 2; i++)
|
|
|
|
{
|
|
|
|
/* Was this file system opened? */
|
|
|
|
|
2016-05-12 01:42:59 +02:00
|
|
|
if (fu->fu_lower[i] != NULL)
|
2015-06-05 21:18:06 +02:00
|
|
|
{
|
|
|
|
um = &ui->ui_fs[i];
|
|
|
|
|
|
|
|
DEBUGASSERT(um != NULL && um->um_node != NULL && um->um_node->u.i_mops != NULL);
|
|
|
|
ops = um->um_node->u.i_mops;
|
|
|
|
|
|
|
|
/* Perform the lower level closedir operation */
|
|
|
|
|
2018-01-03 23:03:56 +01:00
|
|
|
if (ops->closedir != NULL)
|
2015-06-05 21:18:06 +02:00
|
|
|
{
|
|
|
|
ret = ops->closedir(um->um_node, fu->fu_lower[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free the lower dirent structure */
|
|
|
|
|
|
|
|
kmm_free(fu->fu_lower[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-06 17:49:13 +02:00
|
|
|
/* Free any allocated path */
|
|
|
|
|
|
|
|
if (fu->fu_relpath != NULL)
|
|
|
|
{
|
|
|
|
kmm_free(fu->fu_relpath);
|
|
|
|
}
|
|
|
|
|
2015-06-05 21:18:06 +02:00
|
|
|
fu->fu_ndx = 0;
|
2015-06-06 17:49:13 +02:00
|
|
|
fu->fu_relpath = NULL;
|
2015-06-05 21:18:06 +02:00
|
|
|
fu->fu_lower[0] = NULL;
|
|
|
|
fu->fu_lower[1] = NULL;
|
|
|
|
|
|
|
|
/* Decrement the count of open reference. If that count would go to zero
|
2015-06-06 19:33:09 +02:00
|
|
|
* and if the file system has been unmounted, then destroy the file system
|
|
|
|
* now.
|
2015-06-05 21:18:06 +02:00
|
|
|
*/
|
|
|
|
|
2015-06-06 19:33:09 +02:00
|
|
|
if (--ui->ui_nopen <= 0 && ui->ui_unmounted)
|
2015-06-05 21:18:06 +02:00
|
|
|
{
|
|
|
|
unionfs_destroy(ui);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
unionfs_semgive(ui);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: unionfs_readdir
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int unionfs_readdir(struct inode *mountpt, struct fs_dirent_s *dir)
|
|
|
|
{
|
|
|
|
FAR struct unionfs_inode_s *ui;
|
|
|
|
FAR struct unionfs_mountpt_s *um;
|
2015-06-06 17:49:13 +02:00
|
|
|
FAR struct unionfs_mountpt_s *um0;
|
2015-06-05 21:18:06 +02:00
|
|
|
FAR const struct mountpt_operations *ops;
|
|
|
|
FAR struct fs_unionfsdir_s *fu;
|
2015-06-06 17:49:13 +02:00
|
|
|
FAR char *relpath;
|
|
|
|
struct stat buf;
|
|
|
|
bool duplicate;
|
2015-06-05 21:18:06 +02:00
|
|
|
int ret = -ENOSYS;
|
|
|
|
|
|
|
|
/* Recover the union file system data from the struct inode instance */
|
|
|
|
|
|
|
|
DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
|
|
|
|
ui = (FAR struct unionfs_inode_s *)mountpt->i_private;
|
|
|
|
|
|
|
|
DEBUGASSERT(dir);
|
|
|
|
fu = &dir->u.unionfs;
|
|
|
|
|
2017-05-11 21:15:13 +02:00
|
|
|
/* Check if we are at the end of the directory listing. */
|
2015-06-24 17:07:13 +02:00
|
|
|
|
|
|
|
if (fu->fu_eod)
|
|
|
|
{
|
|
|
|
/* End of file and error conditions are not distinguishable
|
|
|
|
* with readdir. Here we return -ENOENT to signal the end
|
|
|
|
* of the directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
2015-06-05 21:18:06 +02:00
|
|
|
DEBUGASSERT(fu->fu_ndx == 0 || fu->fu_ndx == 1);
|
|
|
|
um = &ui->ui_fs[fu->fu_ndx];
|
|
|
|
|
2015-06-24 17:07:13 +02:00
|
|
|
/* Special case: If the open directory is a 'fake' node in the prefix on
|
|
|
|
* one of the mounted file system, then we must also fake the return value.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (fu->fu_prefix[fu->fu_ndx])
|
|
|
|
{
|
|
|
|
DEBUGASSERT(fu->fu_lower[fu->fu_ndx] == NULL && um->um_prefix != NULL);
|
|
|
|
|
|
|
|
/* Copy the file system offset into the dirent structure.
|
|
|
|
* REVISIT: This will not handle the case where the prefix contains
|
|
|
|
* the '/' character the so the offset appears to be multiple
|
|
|
|
* directories.
|
|
|
|
*/
|
|
|
|
|
|
|
|
strncpy(dir->fd_dir.d_name, um->um_prefix, NAME_MAX+1);
|
|
|
|
|
|
|
|
/* Describe this as a read only directory */
|
|
|
|
|
|
|
|
dir->fd_dir.d_type = DTYPE_DIRECTORY;
|
|
|
|
|
|
|
|
/* Increment the index to file system 2 (maybe) */
|
|
|
|
|
|
|
|
if (fu->fu_ndx == 0 && (fu->fu_prefix[1] || fu->fu_lower[1] != NULL))
|
|
|
|
{
|
|
|
|
/* Yes.. set up to do file system 2 next time */
|
|
|
|
|
|
|
|
fu->fu_ndx++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No.. we are finished */
|
|
|
|
|
|
|
|
fu->fu_eod = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This is a normal, mediated file system readdir() */
|
|
|
|
|
|
|
|
DEBUGASSERT(fu->fu_lower[fu->fu_ndx] != NULL);
|
|
|
|
DEBUGASSERT(um->um_node != NULL && um->um_node->u.i_mops != NULL);
|
2015-06-05 21:18:06 +02:00
|
|
|
ops = um->um_node->u.i_mops;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("fu_ndx: %d\n", fu->fu_ndx);
|
2015-06-06 16:36:24 +02:00
|
|
|
|
2015-06-05 21:18:06 +02:00
|
|
|
/* Perform the lower level readdir operation */
|
|
|
|
|
2018-01-03 23:03:56 +01:00
|
|
|
if (ops->readdir != NULL)
|
2015-06-05 21:18:06 +02:00
|
|
|
{
|
2015-06-06 17:49:13 +02:00
|
|
|
/* Loop if we discard duplicate directory entries in filey system 2 */
|
2015-06-05 21:18:06 +02:00
|
|
|
|
2015-06-06 17:49:13 +02:00
|
|
|
do
|
2015-06-05 21:18:06 +02:00
|
|
|
{
|
2015-06-06 17:49:13 +02:00
|
|
|
/* Read the directory entry */
|
2015-06-05 21:18:06 +02:00
|
|
|
|
2015-06-06 17:49:13 +02:00
|
|
|
ret = ops->readdir(um->um_node, fu->fu_lower[fu->fu_ndx]);
|
2015-06-05 21:18:06 +02:00
|
|
|
|
2015-06-06 17:49:13 +02:00
|
|
|
/* Did the read operation fail because we reached the end of the
|
|
|
|
* directory? In that case, the error would be -ENOENT. If we
|
|
|
|
* hit the end-of-directory on file system, we need to seamlessly
|
|
|
|
* move to the second file system (if there is one).
|
2015-06-05 21:18:06 +02:00
|
|
|
*/
|
|
|
|
|
2015-06-24 17:07:13 +02:00
|
|
|
if (ret == -ENOENT && fu->fu_ndx == 0)
|
2015-06-05 21:18:06 +02:00
|
|
|
{
|
2015-06-24 17:07:13 +02:00
|
|
|
/* Special case: If the open directory is a 'fake' node in the
|
|
|
|
* prefix on file system2, then we must also fake the return
|
|
|
|
* value.
|
|
|
|
*/
|
2015-06-06 17:49:13 +02:00
|
|
|
|
2015-06-24 17:07:13 +02:00
|
|
|
if (fu->fu_prefix[1])
|
|
|
|
{
|
|
|
|
DEBUGASSERT(fu->fu_lower[1] == NULL);
|
2015-06-06 17:49:13 +02:00
|
|
|
|
2015-06-24 17:07:13 +02:00
|
|
|
/* Switch to the second file system */
|
2015-06-06 17:49:13 +02:00
|
|
|
|
2015-06-24 17:07:13 +02:00
|
|
|
fu->fu_ndx = 1;
|
|
|
|
um = &ui->ui_fs[1];
|
|
|
|
|
|
|
|
DEBUGASSERT(um != NULL && um->um_prefix != NULL);
|
|
|
|
|
|
|
|
/* Copy the file system offset into the dirent structure.
|
|
|
|
* REVISIT: This will not handle the case where the prefix
|
|
|
|
* contains the '/' character the so the offset appears to
|
|
|
|
* be multiple directories.
|
|
|
|
*/
|
|
|
|
|
|
|
|
strncpy(dir->fd_dir.d_name, um->um_prefix, NAME_MAX+1);
|
|
|
|
|
|
|
|
/* Describe this as a read only directory */
|
|
|
|
|
|
|
|
dir->fd_dir.d_type = DTYPE_DIRECTORY;
|
|
|
|
|
|
|
|
/* Mark the end of the directory listing */
|
|
|
|
|
|
|
|
fu->fu_eod = true;
|
2015-06-06 17:49:13 +02:00
|
|
|
|
2015-06-24 17:07:13 +02:00
|
|
|
/* Check if have already reported something of this name in file system 1. */
|
|
|
|
|
|
|
|
relpath = unionfs_relpath(fu->fu_relpath, um->um_prefix);
|
|
|
|
if (relpath)
|
|
|
|
{
|
|
|
|
int tmp;
|
|
|
|
|
|
|
|
/* Check if anything exists at this path on file system 1 */
|
|
|
|
|
|
|
|
um0 = &ui->ui_fs[0];
|
|
|
|
tmp = unionfs_trystat(um0->um_node, relpath,
|
|
|
|
um0->um_prefix, &buf);
|
|
|
|
|
|
|
|
/* Free the allocated relpath */
|
|
|
|
|
|
|
|
kmm_free(relpath);
|
|
|
|
|
|
|
|
/* Check for a duplicate */
|
|
|
|
|
|
|
|
if (tmp >= 0)
|
|
|
|
{
|
|
|
|
/* There is something there!
|
|
|
|
* REVISIT: We could allow files and directories to
|
|
|
|
* have duplicat names.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No.. check for a normal directory access */
|
|
|
|
|
|
|
|
else if (fu->fu_lower[1] != NULL)
|
2015-06-06 17:49:13 +02:00
|
|
|
{
|
2015-06-24 17:07:13 +02:00
|
|
|
/* Switch to the second file system */
|
|
|
|
|
|
|
|
fu->fu_ndx = 1;
|
|
|
|
um = &ui->ui_fs[1];
|
|
|
|
|
|
|
|
DEBUGASSERT(um != NULL && um->um_node != NULL &&
|
|
|
|
um->um_node->u.i_mops != NULL);
|
|
|
|
ops = um->um_node->u.i_mops;
|
|
|
|
|
|
|
|
/* Make sure that the second file system directory enumeration
|
|
|
|
* is rewound to the beginning of the directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ops->rewinddir != NULL)
|
|
|
|
{
|
|
|
|
ret = ops->rewinddir(um->um_node, fu->fu_lower[1]);
|
|
|
|
}
|
2015-06-06 17:49:13 +02:00
|
|
|
|
2015-06-24 17:07:13 +02:00
|
|
|
/* Then try the read operation again */
|
2015-06-06 17:49:13 +02:00
|
|
|
|
2015-06-24 17:07:13 +02:00
|
|
|
ret = ops->readdir(um->um_node, fu->fu_lower[1]);
|
|
|
|
}
|
2015-06-05 21:18:06 +02:00
|
|
|
}
|
|
|
|
|
2015-06-06 17:49:13 +02:00
|
|
|
/* Did we successfully read a directory from file system 2? If
|
|
|
|
* so, we need to omit an duplicates that should be occluded by
|
|
|
|
* the matching file on file system 1 (if we are enumerating
|
|
|
|
* file system 1).
|
|
|
|
*/
|
2015-06-05 21:18:06 +02:00
|
|
|
|
2015-06-06 17:49:13 +02:00
|
|
|
duplicate = false;
|
|
|
|
if (ret >= 0 && fu->fu_ndx == 1 && fu->fu_lower[0] != NULL)
|
|
|
|
{
|
|
|
|
/* Get the relative path to the same file on file system 1.
|
|
|
|
* NOTE: the on any failures we just assume that the filep
|
|
|
|
* is not a duplicate.
|
|
|
|
*/
|
|
|
|
|
|
|
|
relpath = unionfs_relpath(fu->fu_relpath,
|
|
|
|
fu->fu_lower[1]->fd_dir.d_name);
|
|
|
|
if (relpath)
|
|
|
|
{
|
|
|
|
int tmp;
|
|
|
|
|
|
|
|
/* Check if anything exists at this path on file system 1 */
|
|
|
|
|
|
|
|
um0 = &ui->ui_fs[0];
|
|
|
|
tmp = unionfs_trystat(um0->um_node, relpath,
|
|
|
|
um0->um_prefix, &buf);
|
|
|
|
if (tmp >= 0)
|
|
|
|
{
|
|
|
|
/* There is something there!
|
|
|
|
* REVISIT: We could allow files and directories to
|
|
|
|
* have duplicat names.
|
|
|
|
*/
|
|
|
|
|
|
|
|
duplicate = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free the allocated relpath */
|
|
|
|
|
|
|
|
kmm_free(relpath);
|
|
|
|
}
|
|
|
|
}
|
2015-06-05 21:18:06 +02:00
|
|
|
}
|
2015-06-06 17:49:13 +02:00
|
|
|
while (duplicate);
|
2015-06-05 21:18:06 +02:00
|
|
|
|
2015-06-06 17:49:13 +02:00
|
|
|
/* Copy the return information into the dirent structure that the
|
2015-06-05 21:18:06 +02:00
|
|
|
* application will see.
|
|
|
|
*/
|
|
|
|
|
|
|
|
dir->fd_position = fu->fu_lower[fu->fu_ndx]->fd_position;
|
|
|
|
memcpy(&dir->fd_dir, &fu->fu_lower[fu->fu_ndx]->fd_dir, sizeof(struct dirent));
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: unionfs_rewindir
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int unionfs_rewinddir(struct inode *mountpt, struct fs_dirent_s *dir)
|
|
|
|
{
|
|
|
|
FAR struct unionfs_inode_s *ui;
|
|
|
|
FAR struct unionfs_mountpt_s *um;
|
|
|
|
FAR const struct mountpt_operations *ops;
|
|
|
|
FAR struct fs_unionfsdir_s *fu;
|
|
|
|
int ret;
|
|
|
|
|
2018-01-03 23:03:56 +01:00
|
|
|
finfo("mountpt=%p dir=%p\n", mountpt, dir);
|
2015-06-05 21:18:06 +02:00
|
|
|
|
|
|
|
/* Recover the union file system data from the struct inode instance */
|
|
|
|
|
|
|
|
DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
|
|
|
|
ui = (FAR struct unionfs_inode_s *)mountpt->i_private;
|
|
|
|
|
|
|
|
/* Get exclusive access to the file system data structures */
|
|
|
|
|
|
|
|
ret = unionfs_semtake(ui, false);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUGASSERT(dir);
|
|
|
|
fu = &dir->u.unionfs;
|
|
|
|
|
|
|
|
/* Were we currently enumerating on file system 1? If not, is an
|
|
|
|
* enumeration possible on file system 1?
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(fu->fu_ndx == 0 || fu->fu_ndx == 1);
|
2015-06-24 17:07:13 +02:00
|
|
|
if (/* fu->fu_ndx != 0 && */ fu->fu_prefix[0] || fu->fu_lower[0] != NULL)
|
2015-06-05 21:18:06 +02:00
|
|
|
{
|
|
|
|
/* Yes.. switch to file system 1 */
|
|
|
|
|
|
|
|
fu->fu_ndx = 0;
|
|
|
|
}
|
|
|
|
|
2015-06-24 17:07:13 +02:00
|
|
|
if (!fu->fu_prefix[fu->fu_ndx])
|
|
|
|
{
|
|
|
|
DEBUGASSERT(fu->fu_lower[fu->fu_ndx] != NULL);
|
|
|
|
um = &ui->ui_fs[fu->fu_ndx];
|
2015-06-05 21:18:06 +02:00
|
|
|
|
2015-06-24 17:07:13 +02:00
|
|
|
DEBUGASSERT(um != NULL && um->um_node != NULL && um->um_node->u.i_mops != NULL);
|
|
|
|
ops = um->um_node->u.i_mops;
|
2015-06-05 21:18:06 +02:00
|
|
|
|
2015-06-24 17:07:13 +02:00
|
|
|
/* Perform the file system rewind operation */
|
2015-06-05 21:18:06 +02:00
|
|
|
|
2015-06-24 17:07:13 +02:00
|
|
|
if (ops->rewinddir != NULL)
|
|
|
|
{
|
|
|
|
ret = ops->rewinddir(um->um_node, fu->fu_lower[fu->fu_ndx]);
|
|
|
|
dir->fd_position = fu->fu_lower[fu->fu_ndx]->fd_position;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
2015-06-05 21:18:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
unionfs_semgive(ui);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: unionfs_unbind
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int unionfs_unbind(FAR void *handle, FAR struct inode **blkdriver,
|
|
|
|
unsigned int flags)
|
|
|
|
{
|
2015-06-06 19:33:09 +02:00
|
|
|
FAR struct unionfs_inode_s *ui;
|
|
|
|
|
2018-01-03 23:03:56 +01:00
|
|
|
finfo("handle=%p blkdriver=%p flags=%x\n", handle, blkdriver, flags);
|
2015-06-06 19:33:09 +02:00
|
|
|
|
|
|
|
/* Recover the union file system data from the struct inode instance */
|
|
|
|
|
|
|
|
DEBUGASSERT(handle != NULL);
|
|
|
|
ui = (FAR struct unionfs_inode_s *)handle;
|
|
|
|
|
|
|
|
/* Get exclusive access to the file system data structures */
|
|
|
|
|
|
|
|
(void)unionfs_semtake(ui, true);
|
|
|
|
|
|
|
|
/* Mark the file system as unmounted. */
|
|
|
|
|
|
|
|
ui->ui_unmounted = true;
|
|
|
|
|
|
|
|
/* If there are no open references, then we can destroy the file system
|
|
|
|
* now.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ui->ui_nopen <= 0)
|
|
|
|
{
|
|
|
|
unionfs_destroy(ui);
|
|
|
|
}
|
|
|
|
|
|
|
|
unionfs_semgive(ui);
|
2015-06-05 21:18:06 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: unionfs_statfs
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int unionfs_statfs(FAR struct inode *mountpt, FAR struct statfs *buf)
|
|
|
|
{
|
|
|
|
FAR struct unionfs_inode_s *ui;
|
2015-06-07 16:17:28 +02:00
|
|
|
FAR struct unionfs_mountpt_s *um1;
|
|
|
|
FAR struct unionfs_mountpt_s *um2;
|
|
|
|
FAR const struct mountpt_operations *ops1;
|
|
|
|
FAR const struct mountpt_operations *ops2;
|
2015-06-05 21:18:06 +02:00
|
|
|
FAR struct statfs *adj;
|
|
|
|
struct statfs buf1;
|
|
|
|
struct statfs buf2;
|
|
|
|
uint64_t tmp;
|
|
|
|
uint32_t ratiob16;
|
|
|
|
int ret;
|
|
|
|
|
2018-01-03 23:03:56 +01:00
|
|
|
finfo("mountpt=%p buf=%p\n", mountpt, buf);
|
2015-06-05 21:18:06 +02:00
|
|
|
|
|
|
|
/* Recover the union file system data from the struct inode instance */
|
|
|
|
|
2015-06-07 16:17:28 +02:00
|
|
|
DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL && buf != NULL);
|
2015-06-05 21:18:06 +02:00
|
|
|
ui = (FAR struct unionfs_inode_s *)mountpt->i_private;
|
|
|
|
|
2015-06-07 16:17:28 +02:00
|
|
|
memset(buf, 0, sizeof(struct statfs));
|
|
|
|
|
2015-06-05 21:18:06 +02:00
|
|
|
/* Get exclusive access to the file system data structures */
|
|
|
|
|
|
|
|
ret = unionfs_semtake(ui, false);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-06-07 16:17:28 +02:00
|
|
|
/* Get statfs info from file system 1.
|
|
|
|
*
|
|
|
|
* REVISIT: What would it mean if one file system did not support statfs?
|
|
|
|
* Perhaps we could simplify the following by simply insisting that both
|
|
|
|
* file systems support the statfs method.
|
|
|
|
*/
|
2015-06-05 21:18:06 +02:00
|
|
|
|
2015-06-07 16:17:28 +02:00
|
|
|
um1 = &ui->ui_fs[0];
|
|
|
|
DEBUGASSERT(um1 != NULL && um1->um_node != NULL && um1->um_node->u.i_mops != NULL);
|
|
|
|
ops1 = um1->um_node->u.i_mops;
|
|
|
|
|
|
|
|
um2 = &ui->ui_fs[1];
|
|
|
|
DEBUGASSERT(um2 != NULL && um2->um_node != NULL && um2->um_node->u.i_mops != NULL);
|
|
|
|
ops2 = um2->um_node->u.i_mops;
|
2015-06-05 21:18:06 +02:00
|
|
|
|
2015-06-07 16:17:28 +02:00
|
|
|
if (ops1->statfs != NULL && ops2->statfs != NULL)
|
2015-06-05 21:18:06 +02:00
|
|
|
{
|
2015-06-07 16:17:28 +02:00
|
|
|
ret = ops1->statfs(um1->um_node, &buf1);
|
2015-06-05 21:18:06 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_semaphore;
|
|
|
|
}
|
|
|
|
|
2015-06-07 16:17:28 +02:00
|
|
|
/* Get stafs info from file system 2 */
|
2015-06-05 21:18:06 +02:00
|
|
|
|
2015-06-07 16:17:28 +02:00
|
|
|
ret = ops2->statfs(um2->um_node, &buf2);
|
2015-06-05 21:18:06 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_semaphore;
|
|
|
|
}
|
|
|
|
}
|
2015-06-07 16:17:28 +02:00
|
|
|
else if (ops1->statfs != NULL)
|
|
|
|
{
|
|
|
|
/* We have statfs for file system 1 only */
|
2015-06-05 21:18:06 +02:00
|
|
|
|
2015-06-07 16:17:28 +02:00
|
|
|
ret = ops1->statfs(um1->um_node, buf);
|
|
|
|
goto errout_with_semaphore;
|
|
|
|
}
|
|
|
|
else if (ops2->statfs != NULL)
|
|
|
|
{
|
|
|
|
/* We have statfs for file system 2 only */
|
|
|
|
|
|
|
|
ret = ops2->statfs(um2->um_node, buf);
|
|
|
|
goto errout_with_semaphore;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* We could not get stafs info from either file system */
|
|
|
|
|
|
|
|
ret = -ENOSYS;
|
|
|
|
goto errout_with_semaphore;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We get here is we successfully obtained statfs info from both file
|
|
|
|
* systems. Now combine those results into one statfs report, trying to
|
|
|
|
* reconcile any conflicts between the file system geometries.
|
|
|
|
*/
|
2015-06-05 21:18:06 +02:00
|
|
|
|
|
|
|
buf->f_type = UNIONFS_MAGIC;
|
|
|
|
buf->f_namelen = MIN(buf1.f_namelen, buf2.f_namelen);
|
|
|
|
buf->f_files = buf1.f_files + buf2.f_files;
|
|
|
|
buf->f_ffree = buf1.f_ffree + buf2.f_ffree;
|
|
|
|
|
|
|
|
/* Things expressed in units of blocks are the only tricky ones. We will
|
|
|
|
* depend on a uint64_t * temporary to avoid arithmetic overflow.
|
|
|
|
*/
|
|
|
|
|
2015-06-07 15:39:07 +02:00
|
|
|
buf->f_bsize = buf1.f_bsize;
|
2015-06-05 21:18:06 +02:00
|
|
|
if (buf1.f_bsize != buf2.f_bsize)
|
|
|
|
{
|
|
|
|
if (buf1.f_bsize < buf2.f_bsize)
|
|
|
|
{
|
|
|
|
tmp = (((uint64_t)buf2.f_blocks * (uint64_t)buf2.f_bsize) << 16);
|
|
|
|
ratiob16 = (uint32_t)(tmp / buf1.f_bsize);
|
|
|
|
adj = &buf2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buf->f_bsize = buf2.f_bsize;
|
|
|
|
tmp = (((uint64_t)buf1.f_blocks * (uint64_t)buf1.f_bsize) << 16);
|
|
|
|
ratiob16 = (uint32_t)(tmp / buf2.f_bsize);
|
2015-06-07 15:39:07 +02:00
|
|
|
adj = &buf1;
|
2015-06-05 21:18:06 +02:00
|
|
|
}
|
|
|
|
|
2015-06-07 15:39:07 +02:00
|
|
|
tmp = (uint64_t)adj->f_blocks * ratiob16;
|
2015-06-05 21:18:06 +02:00
|
|
|
adj->f_blocks = (off_t)(tmp >> 16);
|
|
|
|
|
2015-06-07 15:39:07 +02:00
|
|
|
tmp = (uint64_t)adj->f_bfree * ratiob16;
|
2015-06-05 21:18:06 +02:00
|
|
|
adj->f_bfree = (off_t)(tmp >> 16);
|
|
|
|
|
2015-06-07 15:39:07 +02:00
|
|
|
tmp = (uint64_t)adj->f_bavail * ratiob16;
|
2015-06-05 21:18:06 +02:00
|
|
|
adj->f_bavail = (off_t)(tmp >> 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then we can just sum the adjusted sizes */
|
|
|
|
|
|
|
|
buf->f_blocks = buf1.f_blocks + buf2.f_blocks;
|
2015-06-07 15:39:07 +02:00
|
|
|
buf->f_bfree = buf1.f_bfree + buf2.f_bfree;
|
2015-06-05 21:18:06 +02:00
|
|
|
buf->f_bavail = buf1.f_bavail + buf2.f_bavail;
|
|
|
|
|
|
|
|
ret = OK;
|
|
|
|
|
|
|
|
errout_with_semaphore:
|
|
|
|
unionfs_semgive(ui);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: unionfs_unlink
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int unionfs_unlink(FAR struct inode *mountpt,
|
|
|
|
FAR const char *relpath)
|
|
|
|
{
|
|
|
|
FAR struct unionfs_inode_s *ui;
|
2015-06-06 19:33:09 +02:00
|
|
|
FAR struct unionfs_mountpt_s *um;
|
|
|
|
struct stat buf;
|
2015-06-05 21:18:06 +02:00
|
|
|
int ret;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("relpath: %s\n", relpath);
|
2015-06-05 21:18:06 +02:00
|
|
|
|
|
|
|
/* Recover the union file system data from the struct inode instance */
|
|
|
|
|
|
|
|
DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL && relpath != NULL);
|
|
|
|
ui = (FAR struct unionfs_inode_s *)mountpt->i_private;
|
|
|
|
|
|
|
|
/* Get exclusive access to the file system data structures */
|
|
|
|
|
|
|
|
ret = unionfs_semtake(ui, false);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-06-06 19:33:09 +02:00
|
|
|
/* Check if some exists at this path on file system 1. This might be
|
2015-10-11 19:39:29 +02:00
|
|
|
* a file or a directory
|
|
|
|
*/
|
2015-06-06 19:33:09 +02:00
|
|
|
|
|
|
|
um = &ui->ui_fs[0];
|
|
|
|
ret = unionfs_trystat(um->um_node, relpath, um->um_prefix, &buf);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
/* Yes.. Try to unlink the file on file system 1 (perhaps exposing
|
|
|
|
* a file of the same name on file system 2). This would fail
|
|
|
|
* with -ENOSYS if file system 1 is a read-only only file system or
|
|
|
|
* -EISDIR if the path is not a file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = unionfs_tryunlink(um->um_node, relpath, um->um_prefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* There is nothing at this path on file system 1 */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Check if the file exists with with name on file system 2. The only
|
|
|
|
* reason that we check here is so that we can return the more
|
|
|
|
* meaningful -ENOSYS if file system 2 is a read-only file system.
|
|
|
|
*/
|
|
|
|
|
|
|
|
um = &ui->ui_fs[1];
|
|
|
|
ret = unionfs_trystat(um->um_node, relpath, um->um_prefix, &buf);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
/* Yes.. Try to unlink the file on file system 1. This would fail
|
|
|
|
* with -ENOSYS if file system 2 is a read-only only file system or
|
|
|
|
* -EISDIR if the path is not a file.
|
|
|
|
* */
|
|
|
|
|
|
|
|
ret = unionfs_tryunlink(um->um_node, relpath, um->um_prefix);
|
|
|
|
}
|
|
|
|
}
|
2015-06-05 21:18:06 +02:00
|
|
|
|
|
|
|
unionfs_semgive(ui);
|
2015-06-06 19:33:09 +02:00
|
|
|
return ret;
|
2015-06-05 21:18:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: unionfs_mkdir
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int unionfs_mkdir(FAR struct inode *mountpt, FAR const char *relpath,
|
|
|
|
mode_t mode)
|
|
|
|
{
|
|
|
|
FAR struct unionfs_inode_s *ui;
|
|
|
|
FAR struct unionfs_mountpt_s *um;
|
|
|
|
struct stat buf;
|
|
|
|
int ret1;
|
|
|
|
int ret2;
|
|
|
|
int ret;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("relpath: %s\n", relpath);
|
2015-06-05 21:18:06 +02:00
|
|
|
|
|
|
|
/* Recover the union file system data from the struct inode instance */
|
|
|
|
|
|
|
|
DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL && relpath != NULL);
|
|
|
|
ui = (FAR struct unionfs_inode_s *)mountpt->i_private;
|
|
|
|
|
|
|
|
/* Get exclusive access to the file system data structures */
|
|
|
|
|
|
|
|
ret = unionfs_semtake(ui, false);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Is there anything with this name on either file system? */
|
|
|
|
|
|
|
|
um = &ui->ui_fs[0];
|
|
|
|
ret = unionfs_trystat(um->um_node, relpath, um->um_prefix, &buf);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
2015-06-05 23:02:24 +02:00
|
|
|
ret = -EEXIST;
|
|
|
|
goto errout_with_semaphore;
|
2015-06-05 21:18:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
um = &ui->ui_fs[1];
|
|
|
|
ret = unionfs_trystat(um->um_node, relpath, um->um_prefix, &buf);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
2015-06-05 23:02:24 +02:00
|
|
|
ret = -EEXIST;
|
|
|
|
goto errout_with_semaphore;
|
2015-06-05 21:18:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Try to create the directory on both file systems. */
|
|
|
|
|
|
|
|
um = &ui->ui_fs[0];
|
|
|
|
ret1 = unionfs_trymkdir(um->um_node, relpath, um->um_prefix, mode);
|
|
|
|
|
|
|
|
um = &ui->ui_fs[1];
|
2015-06-05 22:23:07 +02:00
|
|
|
ret2 = unionfs_trymkdir(um->um_node, relpath, um->um_prefix, mode);
|
2015-06-05 21:18:06 +02:00
|
|
|
|
|
|
|
/* We will say we were successful if we were able to create the
|
|
|
|
* directory on either file system. Perhaps one file system is
|
|
|
|
* read-only and the other is write-able?
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ret1 >= 0 || ret2 >= 0)
|
|
|
|
{
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Otherwise, pick one */
|
|
|
|
|
|
|
|
ret = ret1;
|
|
|
|
}
|
|
|
|
|
2015-06-05 23:02:24 +02:00
|
|
|
errout_with_semaphore:
|
2015-06-05 21:18:06 +02:00
|
|
|
unionfs_semgive(ui);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: unionfs_rmdir
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int unionfs_rmdir(FAR struct inode *mountpt, FAR const char *relpath)
|
|
|
|
{
|
|
|
|
FAR struct unionfs_inode_s *ui;
|
|
|
|
FAR struct unionfs_mountpt_s *um;
|
|
|
|
int tmp;
|
2015-06-06 19:33:09 +02:00
|
|
|
int ret;
|
2015-06-05 21:18:06 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("relpath: %s\n", relpath);
|
2015-06-05 21:18:06 +02:00
|
|
|
|
|
|
|
/* Recover the union file system data from the struct inode instance */
|
|
|
|
|
|
|
|
DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL && relpath != NULL);
|
|
|
|
ui = (FAR struct unionfs_inode_s *)mountpt->i_private;
|
|
|
|
|
|
|
|
/* Get exclusive access to the file system data structures */
|
|
|
|
|
|
|
|
ret = unionfs_semtake(ui, false);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-06-06 19:33:09 +02:00
|
|
|
ret = -ENOENT;
|
|
|
|
|
2015-06-05 21:18:06 +02:00
|
|
|
/* We really don't know any better so we will try to remove the directory
|
|
|
|
* from both file systems.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Is there a directory with this name on file system 1 */
|
|
|
|
|
|
|
|
um = &ui->ui_fs[0];
|
|
|
|
tmp = unionfs_trystatdir(um->um_node, relpath, um->um_prefix);
|
|
|
|
if (tmp >= 0)
|
|
|
|
{
|
|
|
|
/* Yes.. remove it. Since we know that the directory exists, any
|
|
|
|
* failure to remove it is a showstopper.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = unionfs_tryrmdir(um->um_node, relpath, um->um_prefix);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
unionfs_semgive(ui);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Either the directory does not exist on file system 1, or we
|
|
|
|
* successfully removed it. Try again on file system 2.
|
|
|
|
*/
|
|
|
|
|
|
|
|
um = &ui->ui_fs[1];
|
|
|
|
tmp = unionfs_trystatdir(um->um_node, relpath, um->um_prefix);
|
|
|
|
if (tmp >= 0)
|
|
|
|
{
|
|
|
|
/* Yes.. remove it. Since we know that the directory exists, any
|
|
|
|
* failure to remove it is a showstopper.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = unionfs_tryrmdir(um->um_node, relpath, um->um_prefix);
|
|
|
|
|
|
|
|
/* REVISIT: Should we try to restore the directory on file system 1
|
|
|
|
* if we failure to removed the directory on file system 2?
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
unionfs_semgive(ui);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: unionfs_rename
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int unionfs_rename(FAR struct inode *mountpt,
|
|
|
|
FAR const char *oldrelpath,
|
|
|
|
FAR const char *newrelpath)
|
|
|
|
{
|
|
|
|
FAR struct unionfs_inode_s *ui;
|
|
|
|
FAR struct unionfs_mountpt_s *um;
|
|
|
|
int tmp;
|
|
|
|
int ret = -ENOENT;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("oldrelpath: %s newrelpath\n", oldrelpath, newrelpath);
|
2015-06-05 21:18:06 +02:00
|
|
|
|
|
|
|
/* Recover the union file system data from the struct inode instance */
|
|
|
|
|
|
|
|
DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
|
|
|
|
ui = (FAR struct unionfs_inode_s *)mountpt->i_private;
|
|
|
|
|
|
|
|
/* Get exclusive access to the file system data structures */
|
|
|
|
|
|
|
|
ret = unionfs_semtake(ui, false);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUGASSERT(oldrelpath != NULL && oldrelpath != NULL);
|
|
|
|
|
|
|
|
/* Is there a file with this name on file system 1 */
|
|
|
|
|
|
|
|
um = &ui->ui_fs[0];
|
|
|
|
tmp = unionfs_trystatfile(um->um_node, oldrelpath, um->um_prefix);
|
|
|
|
if (tmp >= 0)
|
|
|
|
{
|
|
|
|
/* Yes.. rename it. Since we know that the directory exists, any
|
|
|
|
* failure to remove it is a showstopper.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = unionfs_tryrename(um->um_node, oldrelpath, newrelpath,
|
|
|
|
um->um_prefix);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
/* Return immediately on success. In the event that the file
|
|
|
|
* exists in both file systems, this will produce the odd behavior
|
|
|
|
* that one file on file system 1 was renamed but another obscured
|
|
|
|
* file of the same relative path will become visible.
|
|
|
|
*/
|
|
|
|
|
|
|
|
unionfs_semgive(ui);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Either the file does not exist on file system 1, or we failed to rename
|
|
|
|
* it (perhaps because the file system was read-only). Try again on file
|
|
|
|
* system 2.
|
|
|
|
*/
|
|
|
|
|
|
|
|
um = &ui->ui_fs[1];
|
|
|
|
tmp = unionfs_trystatfile(um->um_node, oldrelpath, um->um_prefix);
|
|
|
|
if (tmp >= 0)
|
|
|
|
{
|
|
|
|
/* Yes.. remove it. Since we know that the directory exists, any
|
|
|
|
* failure to remove it is a showstopper.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = unionfs_tryrename(um->um_node, oldrelpath, newrelpath,
|
|
|
|
um->um_prefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
unionfs_semgive(ui);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: unionfs_stat
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int unionfs_stat(FAR struct inode *mountpt, FAR const char *relpath,
|
|
|
|
FAR struct stat *buf)
|
|
|
|
{
|
|
|
|
FAR struct unionfs_inode_s *ui;
|
|
|
|
FAR struct unionfs_mountpt_s *um;
|
|
|
|
int ret;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("relpath: %s\n", relpath);
|
2015-06-05 21:18:06 +02:00
|
|
|
|
|
|
|
/* Recover the union file system data from the struct inode instance */
|
|
|
|
|
|
|
|
DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL && relpath != NULL);
|
|
|
|
ui = (FAR struct unionfs_inode_s *)mountpt->i_private;
|
|
|
|
|
|
|
|
/* Get exclusive access to the file system data structures */
|
|
|
|
|
|
|
|
ret = unionfs_semtake(ui, false);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* stat this path on file system 1 */
|
|
|
|
|
|
|
|
um = &ui->ui_fs[0];
|
|
|
|
ret = unionfs_trystat(um->um_node, relpath, um->um_prefix, buf);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
/* Return on the first success. The first instance of the file will
|
|
|
|
* shadow the second anyway.
|
|
|
|
*/
|
|
|
|
|
2015-06-05 23:02:24 +02:00
|
|
|
unionfs_semgive(ui);
|
2015-06-05 21:18:06 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* stat failed on the file system 1. Try again on file system 2. */
|
|
|
|
|
2015-06-05 23:02:24 +02:00
|
|
|
um = &ui->ui_fs[1];
|
2015-06-05 21:18:06 +02:00
|
|
|
ret = unionfs_trystat(um->um_node, relpath, um->um_prefix, buf);
|
2015-06-24 17:07:13 +02:00
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
/* Return on the first success. The first instance of the file will
|
|
|
|
* shadow the second anyway.
|
|
|
|
*/
|
|
|
|
|
|
|
|
unionfs_semgive(ui);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Special case the unionfs root directory when both file systems are offset.
|
|
|
|
* In that case, both of the above trystat calls will fail.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ui->ui_fs[0].um_prefix != NULL && ui->ui_fs[1].um_prefix != NULL)
|
|
|
|
{
|
|
|
|
/* Most of the stat entries just do not apply */
|
|
|
|
|
|
|
|
memset(buf, 0, sizeof(struct stat));
|
|
|
|
|
|
|
|
/* Claim that this is a read-only directory */
|
|
|
|
|
|
|
|
buf->st_mode = S_IFDIR | S_IROTH | S_IRGRP | S_IRUSR;
|
|
|
|
|
|
|
|
/* Check if the user is stat'ing some "fake" node between the unionfs
|
|
|
|
* root and the file system 1 root directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (unionfs_ispartprefix(relpath, ui->ui_fs[0].um_prefix) ||
|
|
|
|
unionfs_ispartprefix(relpath, ui->ui_fs[1].um_prefix))
|
|
|
|
{
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = -ENOENT;
|
|
|
|
}
|
|
|
|
}
|
2015-06-05 21:18:06 +02:00
|
|
|
|
|
|
|
unionfs_semgive(ui);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: unionfs_getmount
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int unionfs_getmount(FAR const char *path, FAR struct inode **inode)
|
|
|
|
{
|
2015-10-04 23:04:00 +02:00
|
|
|
FAR struct inode *minode;
|
2017-02-05 16:51:42 +01:00
|
|
|
struct inode_search_s desc;
|
2017-02-13 00:05:47 +01:00
|
|
|
int ret;
|
2015-06-05 21:18:06 +02:00
|
|
|
|
|
|
|
/* Find the mountpt */
|
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
SETUP_SEARCH(&desc, path, false);
|
2017-02-05 16:51:42 +01:00
|
|
|
|
|
|
|
ret = inode_find(&desc);
|
|
|
|
if (ret < 0)
|
2015-06-05 21:18:06 +02:00
|
|
|
{
|
|
|
|
/* Mountpoint inode not found */
|
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
goto errout_with_search;
|
2015-06-05 21:18:06 +02:00
|
|
|
}
|
|
|
|
|
2017-02-05 16:51:42 +01:00
|
|
|
/* Get the search results */
|
|
|
|
|
|
|
|
minode = desc.node;
|
|
|
|
DEBUGASSERT(minode != NULL);
|
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
/* Verify that the inode is a mountpoint.
|
|
|
|
*
|
|
|
|
* REVISIT: If desc.relpath points to a non-empty string, then the path
|
|
|
|
* does not really refer to a mountpoint but, rather, to a some entity
|
|
|
|
* within the mounted volume.
|
|
|
|
*/
|
2015-06-05 21:18:06 +02:00
|
|
|
|
|
|
|
if (!INODE_IS_MOUNTPT(minode))
|
|
|
|
{
|
2017-02-05 21:25:45 +01:00
|
|
|
/* Inode was found, but is it is not a mounpoint */
|
2015-06-05 21:18:06 +02:00
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto errout_with_inode;
|
2015-06-05 21:18:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Success! */
|
|
|
|
|
|
|
|
*inode = minode;
|
2017-02-05 21:25:45 +01:00
|
|
|
RELEASE_SEARCH(&desc);
|
2015-06-05 21:18:06 +02:00
|
|
|
return OK;
|
2017-02-05 21:25:45 +01:00
|
|
|
|
|
|
|
errout_with_inode:
|
|
|
|
inode_release(minode);
|
|
|
|
|
|
|
|
errout_with_search:
|
|
|
|
RELEASE_SEARCH(&desc);
|
|
|
|
return ret;
|
2015-06-05 21:18:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: unionfs_mount
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Create and mount a union file system
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* fspath1 - The full path to the first file system mountpoint
|
|
|
|
* prefix1 - An optiona prefix that may be applied to make the first
|
|
|
|
* file system appear a some path below the unionfs mountpoint,
|
|
|
|
* fspath2 - The full path to the second file system mountpoint
|
|
|
|
* prefix2 - An optiona prefix that may be applied to make the first
|
|
|
|
* file system appear a some path below the unionfs mountpoint,
|
|
|
|
* mountpt - The full path to the mountpoint for the union file system
|
|
|
|
*
|
|
|
|
* Returned value:
|
|
|
|
* Zero (OK) is returned if the union file system was correctly created and
|
|
|
|
* mounted. On any failure, a negated error value will be returned to
|
|
|
|
* indicate the nature of the failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int unionfs_mount(FAR const char *fspath1, FAR const char *prefix1,
|
|
|
|
FAR const char *fspath2, FAR const char *prefix2,
|
|
|
|
FAR const char *mountpt)
|
|
|
|
{
|
|
|
|
FAR struct unionfs_inode_s *ui;
|
|
|
|
FAR struct inode *mpinode;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT(fspath1 != NULL && fspath2 != NULL && mountpt != NULL);
|
|
|
|
|
|
|
|
/* Allocate a structure a structure that will describe the union file
|
|
|
|
* system.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ui = (FAR struct unionfs_inode_s *)kmm_zalloc(sizeof(struct unionfs_inode_s));
|
|
|
|
if (!ui)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Failed to allocated union FS state structure\n");
|
2015-06-05 21:18:06 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2017-10-03 20:51:15 +02:00
|
|
|
nxsem_init(&ui->ui_exclsem, 0, 1);
|
2015-06-05 21:18:06 +02:00
|
|
|
|
|
|
|
/* Get the inodes associated with fspath1 and fspath2 */
|
|
|
|
|
|
|
|
ret = unionfs_getmount(fspath1, &ui->ui_fs[0].um_node);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: unionfs_getmount(fspath1) failed: %d\n", ret);
|
2015-06-05 21:18:06 +02:00
|
|
|
goto errout_with_uinode;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = unionfs_getmount(fspath2, &ui->ui_fs[1].um_node);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: unionfs_getmount(fspath2) failed: %d\n", ret);
|
2015-06-05 21:18:06 +02:00
|
|
|
goto errout_with_fs1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Duplicate the prefix strings */
|
|
|
|
|
|
|
|
if (prefix1 && strlen(prefix1) > 0)
|
|
|
|
{
|
|
|
|
ui->ui_fs[0].um_prefix = strdup(prefix1);
|
|
|
|
if (ui->ui_fs[0].um_prefix == NULL)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: strdup(prefix1) failed\n");
|
2015-06-05 21:18:06 +02:00
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout_with_fs2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (prefix2 && strlen(prefix2) > 0)
|
|
|
|
{
|
|
|
|
ui->ui_fs[1].um_prefix = strdup(prefix2);
|
|
|
|
if (ui->ui_fs[1].um_prefix == NULL)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: strdup(prefix2) failed\n");
|
2015-06-05 21:18:06 +02:00
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout_with_prefix1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Finally, mount the union FS. We should adapt the standard mount to do
|
|
|
|
* this using optional parameters. This custom mount should do the job
|
|
|
|
* for now, however.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Insert a dummy node -- we need to hold the inode semaphore
|
|
|
|
* to do this because we will have a momentarily bad structure.
|
2015-09-23 18:34:08 +02:00
|
|
|
* NOTE that the inode will be created with a refernce count of zero.
|
2015-06-05 21:18:06 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
inode_semtake();
|
|
|
|
ret = inode_reserve(mountpt, &mpinode);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* inode_reserve can fail for a couple of reasons, but the most likely
|
|
|
|
* one is that the inode already exists. inode_reserve may return:
|
|
|
|
*
|
|
|
|
* -EINVAL - 'path' is invalid for this operation
|
|
|
|
* -EEXIST - An inode already exists at 'path'
|
|
|
|
* -ENOMEM - Failed to allocate in-memory resources for the operation
|
|
|
|
*/
|
|
|
|
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Failed to reserve inode\n");
|
2015-06-05 21:18:06 +02:00
|
|
|
goto errout_with_semaphore;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Populate the inode with driver specific information. */
|
|
|
|
|
|
|
|
INODE_SET_MOUNTPT(mpinode);
|
|
|
|
|
|
|
|
mpinode->u.i_mops = &g_unionfs_mops;
|
|
|
|
#ifdef CONFIG_FILE_MODE
|
|
|
|
mpinode->i_mode = 0755;
|
|
|
|
#endif
|
|
|
|
mpinode->i_private = ui;
|
|
|
|
|
|
|
|
/* Unlink the contained mountpoint inodes from the pseudo file system.
|
|
|
|
* The inodes will be marked as deleted so that they will be removed when
|
|
|
|
* the reference count decrements to zero in inode_release(). Because we
|
|
|
|
* hold a reference count on the inodes, they will not be deleted until
|
|
|
|
* this logic calls inode_release() in the unionfs_destroy() function.
|
|
|
|
*/
|
|
|
|
|
|
|
|
(void)inode_remove(fspath1);
|
|
|
|
(void)inode_remove(fspath2);
|
|
|
|
inode_semgive();
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout_with_semaphore:
|
|
|
|
inode_semgive();
|
|
|
|
|
|
|
|
if (ui->ui_fs[1].um_prefix != NULL)
|
|
|
|
{
|
|
|
|
kmm_free(ui->ui_fs[1].um_prefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
errout_with_prefix1:
|
|
|
|
if (ui->ui_fs[0].um_prefix != NULL)
|
|
|
|
{
|
|
|
|
kmm_free(ui->ui_fs[0].um_prefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
errout_with_fs2:
|
|
|
|
inode_release(ui->ui_fs[1].um_node);
|
|
|
|
|
|
|
|
errout_with_fs1:
|
|
|
|
inode_release(ui->ui_fs[0].um_node);
|
|
|
|
|
|
|
|
errout_with_uinode:
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_destroy(&ui->ui_exclsem);
|
2015-06-05 21:18:06 +02:00
|
|
|
kmm_free(ui);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif /* !CONFIG_DISABLE_MOUNTPOINT && CONFIG_FS_UNIONFS */
|