fs_mount.c 9.9 KB
Newer Older
W
wenjun 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
/****************************************************************************
 * fs/mount/fs_mount.c
 *
 *   Copyright (C) 2007-2009, 2011-2013, 2015, 2017-2019 Gregory Nutt. All
 *     rights reserved.
 *   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 "vfs_config.h"

#include "sys/mount.h"
#include "string.h"
#include "errno.h"
#include "assert.h"
M
mucor 已提交
47
#include "vnode.h"
W
wenjun 已提交
48
#include "stdlib.h"
M
mamingshuai 已提交
49
#ifdef LOSCFG_DRIVERS_MTD
W
wenjun 已提交
50 51 52 53 54 55
#include "mtd_partition.h"
#endif
#ifdef LOSCFG_FS_FAT_VIRTUAL_PARTITION
#include "errcode_fat.h"
#endif
#include "los_tables.h"
W
wangchenyang 已提交
56 57 58 59
#ifdef LOSCFG_DRIVERS_RANDOM
#include "hisoc/random.h"
#else
#include "stdlib.h"
M
mamingshuai 已提交
60
#endif
M
mucor 已提交
61
#include "path_cache.h"
W
wangchenyang 已提交
62
#include "fs/mount.h"
M
mucor 已提交
63 64
#include "fs/driver.h"
#include "fs/fs.h"
W
wangchenyang 已提交
65

M
mamingshuai 已提交
66

W
wenjun 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
/* At least one filesystem must be defined, or this file will not compile.
 * It may be desire-able to make filesystems dynamically registered at
 * some time in the future, but at present, this file needs to know about
 * every configured filesystem.
 */

#ifdef CONFIG_FS_READABLE

/****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/
/* Configuration ************************************************************/
/* In the canonical case, a file system is bound to a block driver.  However,
 * some less typical cases a block driver is not required.  Examples are
 * pseudo file systems (like BINFS or PROCFS) and MTD file systems (like NXFFS).
 *
 * These file systems all require block drivers:
 */

#define BLKDRVR_NOT_MOUNTED 2

extern struct fsmap_t g_fsmap[];
LOS_HAL_TABLE_BEGIN(g_fsmap, fsmap);

extern struct fsmap_t g_fsmap_end;
LOS_HAL_TABLE_END(g_fsmap_end, fsmap);

/****************************************************************************
 * Public Data
 ****************************************************************************/

/****************************************************************************
 * Private Functions
 ****************************************************************************/

/****************************************************************************
 * Name: mount_findfs
 *
 * Description:
 *    find the specified filesystem
 *
 ****************************************************************************/

static const struct fsmap_t *mount_findfs(const char *filesystemtype)
{
  struct fsmap_t *m = NULL;

  for (m = &g_fsmap[0]; m != &g_fsmap_end; ++m)
    {
      if (m->fs_filesystemtype &&
          strcmp(filesystemtype, m->fs_filesystemtype) == 0)
        {
          return m;
        }
    }

  return (const struct fsmap_t *)NULL;
}

W
wangchenyang 已提交
126

W
wenjun 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
/****************************************************************************
 * Public Functions
 ****************************************************************************/

/****************************************************************************
 * Name: mount
 *
 * Description:
 *   mount() attaches the filesystem specified by the 'source' block device
 *   name into the root file system at the path specified by 'target.'
 *
 * Return:
 *   Zero is returned on success; -1 is returned on an error and errno is
 *   set appropriately:
 *
 *   EACCES A component of a path was not searchable or mounting a read-only
 *      filesystem was attempted without giving the MS_RDONLY flag.
 *   EBUSY 'source' is already  mounted.
 *   EFAULT One of the pointer arguments points outside the user address
 *      space.
 *   EINVAL 'source' had an invalid superblock.
 *   ENODEV 'filesystemtype' not configured
 *   ENOENT A pathname was empty or had a nonexistent component.
 *   ENOMEM Could not allocate a memory to copy filenames or data into.
 *   ENOTBLK 'source' is not a block device
 *
 ****************************************************************************/

int mount(const char *source, const char *target,
          const char *filesystemtype, unsigned long mountflags,
          const void *data)
{
  int ret;
  int errcode = 0;
W
wangchenyang 已提交
161 162 163
  struct Mount* mnt = NULL;
  struct Vnode *device = NULL;
  struct Vnode *mountpt_vnode = NULL;
W
wenjun 已提交
164
  const struct fsmap_t *fsmap = NULL;
W
wangchenyang 已提交
165 166
  const struct MountOps *mops = NULL;
  LIST_HEAD *mount_list = NULL;
M
mamingshuai 已提交
167
#ifdef LOSCFG_DRIVERS_MTD
W
wenjun 已提交
168 169 170 171 172
  mtd_partition *partition = NULL;
#endif

  if (filesystemtype == NULL)
    {
W
wangchenyang 已提交
173
      errcode = -EINVAL;
W
wenjun 已提交
174 175 176 177 178 179 180 181 182
      goto errout;
    }

  /* Verify required pointer arguments */

  DEBUGASSERT(target && filesystemtype);

  /* Find the specified filesystem.  Try the block driver file systems first */

W
wangchenyang 已提交
183
  if ((fsmap = mount_findfs(filesystemtype)) == NULL || (fsmap->is_bdfs && !source))
W
wenjun 已提交
184 185
    {
      PRINT_ERR("Failed to find file system %s\n", filesystemtype);
W
wangchenyang 已提交
186
      errcode = -ENODEV;
W
wenjun 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199
      goto errout;
    }

  mops = fsmap->fs_mops;

  if (fsmap->is_bdfs && source)
    {
      /* Make sure that a block driver argument was provided */

      DEBUGASSERT(source);

      /* Find the block driver */

W
wangchenyang 已提交
200
      ret = find_blockdriver(source, mountflags, &device);
W
wenjun 已提交
201 202 203
      if (ret < 0)
        {
          PRINT_ERR("Failed to find block driver %s\n", source);
W
wangchenyang 已提交
204
          errcode = ret;
W
wenjun 已提交
205 206 207 208
          goto errout;
        }
    }

W
wangchenyang 已提交
209 210
  VnodeHold();
  ret = VnodeLookup(target, &mountpt_vnode, 0);
W
wenjun 已提交
211

W
wangchenyang 已提交
212
  /* The mount point must be an existed vnode. */
W
wenjun 已提交
213

W
wangchenyang 已提交
214
  if (ret != OK)
W
wenjun 已提交
215 216
    {
      PRINT_ERR("Failed to find valid mountpoint %s\n", target);
W
wangchenyang 已提交
217 218
      errcode = -EINVAL;
      goto errout_with_lock;
W
wenjun 已提交
219
    }
220
  if (mountpt_vnode->flag & VNODE_FLAG_MOUNT_NEW)
W
wenjun 已提交
221
    {
W
wangchenyang 已提交
222 223 224
      PRINT_ERR("can't mount to %s, already mounted.\n", target);
      errcode = -EINVAL;
      goto errout_with_lock;
W
wenjun 已提交
225 226 227 228 229 230 231
    }

  /* Bind the block driver to an instance of the file system.  The file
   * system returns a reference to some opaque, fs-dependent structure
   * that encapsulates this binding.
   */

W
wangchenyang 已提交
232
  if (mops->Mount == NULL)
W
wenjun 已提交
233 234 235
    {
      /* The filesystem does not support the bind operation ??? */

W
wangchenyang 已提交
236 237
      PRINTK("ERROR: Filesystem does not support bind\n");
      errcode = -ENOSYS;
L
li_zan 已提交
238
      goto errout_with_lock;
W
wenjun 已提交
239 240 241
    }

  /* Increment reference count for the reference we pass to the file system */
M
mamingshuai 已提交
242
#ifdef LOSCFG_DRIVERS_MTD
W
wangchenyang 已提交
243
  if (fsmap->is_mtd_support && (device != NULL))
W
wenjun 已提交
244
    {
W
wangchenyang 已提交
245
      partition = (mtd_partition *)((struct drv_data *)device->data)->priv;
W
wenjun 已提交
246 247 248
      partition->mountpoint_name = (char *)zalloc(strlen(target) + 1);
      if (partition->mountpoint_name == NULL)
        {
W
wangchenyang 已提交
249
          errcode = -ENOMEM;
L
li_zan 已提交
250
          goto errout_with_lock;
W
wenjun 已提交
251 252 253 254 255
        }
      (void)strncpy_s(partition->mountpoint_name, strlen(target) + 1, target, strlen(target));
      partition->mountpoint_name[strlen(target)] = '\0';
    }
#endif
W
wangchenyang 已提交
256 257 258

  mnt = MountAlloc(mountpt_vnode, (struct MountOps*)mops);

259
  mountpt_vnode->useCount++;
W
wangchenyang 已提交
260
  ret = mops->Mount(mnt, device, data);
261
  mountpt_vnode->useCount--;
W
wenjun 已提交
262 263
  if (ret != 0)
    {
W
wangchenyang 已提交
264
      /* The vnode is unhappy with the blkdrvr for some reason.  Back out
W
wenjun 已提交
265 266 267 268
       * the count for the reference we failed to pass and exit with an
       * error.
       */

M
mucor 已提交
269
      PRINT_ERR("Bind method failed: %d\n", ret);
W
wangchenyang 已提交
270
      errcode = ret;
M
mamingshuai 已提交
271
#ifdef LOSCFG_DRIVERS_MTD
W
wangchenyang 已提交
272
      if (fsmap->is_mtd_support && (device != NULL) && (partition != NULL))
W
wenjun 已提交
273 274 275 276 277 278 279
        {
          free(partition->mountpoint_name);
          partition->mountpoint_name = NULL;
        }
#endif
      goto errout_with_mountpt;
    }
280 281
  mnt->vnodeBeCovered->flag |= VNODE_FLAG_MOUNT_ORIGIN;
  mnt->vnodeCovered->flag |= VNODE_FLAG_MOUNT_NEW;
C
chenjing 已提交
282 283 284 285 286 287 288
  mnt->ops = mops;
  mnt->mountFlags = mountflags;
  ret = strcpy_s(mnt->pathName, PATH_MAX, target);
  if (ret != EOK)
    {
      PRINT_ERR("Failed to copy mount point pathname, errno %d\n", ret);
    }
W
wenjun 已提交
289

W
wangchenyang 已提交
290
  //* We have it, now populate it with driver specific information. */
W
wenjun 已提交
291

W
wangchenyang 已提交
292 293
  mount_list = GetMountList();
  LOS_ListAdd(mount_list, &mnt->mountList);
W
wenjun 已提交
294

W
wangchenyang 已提交
295
  if (!strcmp("/", target))
W
wenjun 已提交
296
    {
W
wangchenyang 已提交
297
      ChangeRoot(mnt->vnodeCovered);
W
wenjun 已提交
298 299
    }

W
wangchenyang 已提交
300
  VnodeDrop();
W
wenjun 已提交
301

W
wangchenyang 已提交
302 303 304
  /* We can release our reference to the blkdrver_vnode, if the filesystem
   * wants to retain the blockdriver vnode (which it should), then it must
   * have called vnode_addref().  There is one reference on mountpt_vnode
W
wenjun 已提交
305 306 307 308 309 310 311 312
   * that will persist until umount() is called.
   */

  return OK;

  /* A lot of goto's!  But they make the error handling much simpler */

errout_with_mountpt:
W
wangchenyang 已提交
313
  if (mnt)
W
wenjun 已提交
314
    {
W
wangchenyang 已提交
315
      free(mnt);
W
wenjun 已提交
316
    }
W
wangchenyang 已提交
317 318
errout_with_lock:
  VnodeDrop();
W
wenjun 已提交
319
errout:
W
wangchenyang 已提交
320
  set_errno(-errcode);
W
wenjun 已提交
321 322 323
  return VFS_ERROR;
}
#endif /* CONFIG_FS_READABLE */