fs_umount.c 5.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
/****************************************************************************
 * fs/mount/fs_umount.c
 *
 *   Copyright (C) 2007-2009, 2015 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 "errno.h"
#include "fs/fs.h"
W
wangchenyang 已提交
45
#include "fs/vnode.h"
W
wenjun 已提交
46
#include "stdlib.h"
W
wangchenyang 已提交
47
#include "unistd.h"
W
wenjun 已提交
48 49
#include "string.h"
#include "disk.h"
W
wangchenyang 已提交
50
#include "fs_other.h"
W
wenjun 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

/****************************************************************************
 * Public Functions
 ****************************************************************************/

/****************************************************************************
 * Name: umount
 *
 * Description:
 *   umount() detaches the filesystem mounted 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 The target could not be unmounted because it is busy.
 *   EFAULT The pointer argument points outside the user address space.
 *
 ****************************************************************************/

W
wangchenyang 已提交
74 75 76 77 78 79 80 81 82 83 84
BOOL fs_in_use(struct Mount *mnt, const char *target)
{
  char cwd[PATH_MAX];
  char *pret = getcwd(cwd, PATH_MAX);
  if (pret != NULL)
    {
      if (!strncmp(target, cwd, strlen(target)))
        {
          return TRUE;
        }
    }
85
    return VnodeInUseIter(mnt);
W
wangchenyang 已提交
86 87
}

W
wenjun 已提交
88 89
int umount(const char *target)
{
W
wangchenyang 已提交
90 91 92 93
  struct Vnode *mountpt_vnode = NULL;
  struct Vnode *blkdrvr_vnode = NULL;
  struct Vnode *covered_vnode = NULL;
  struct Mount *mnt = NULL;
W
wenjun 已提交
94
  int ret;
M
mamingshuai 已提交
95 96 97 98 99
#ifdef LOSCFG_FS_ZPFS
  bool isZpfs = false;
  struct inode zpfsInode;
#endif

W
wenjun 已提交
100 101 102 103
  /* Verify required pointer arguments */

  if (target == NULL)
    {
W
wangchenyang 已提交
104
      ret = -EFAULT;
W
wenjun 已提交
105 106 107
      goto errout;
    }

W
wangchenyang 已提交
108 109 110 111
  /* Find the mountpt */
  VnodeHold();
  ret = VnodeLookup(target, &mountpt_vnode, 0);
  if (ret != OK || !mountpt_vnode)
W
wenjun 已提交
112 113 114
    {
      goto errout;
    }
W
wangchenyang 已提交
115
  /* Verify that the vnode is a mountpoint */
116
  if (!mountpt_vnode || !(mountpt_vnode->flag & VNODE_FLAG_MOUNT_NEW))
W
wenjun 已提交
117
    {
W
wangchenyang 已提交
118
      ret = -EINVAL;
W
wenjun 已提交
119 120 121
      goto errout;
    }

W
wangchenyang 已提交
122 123 124
  /* Get mount point covered vnode and mount structure */
  mnt = mountpt_vnode->originMount;
  if (!mnt)
W
wenjun 已提交
125
    {
W
wangchenyang 已提交
126 127
      ret = -EINVAL;
      goto errout;
W
wenjun 已提交
128
    }
W
wangchenyang 已提交
129
  covered_vnode = mnt->vnodeBeCovered;
130
  if (!covered_vnode || !(covered_vnode->flag & VNODE_FLAG_MOUNT_ORIGIN))
W
wenjun 已提交
131
    {
W
wangchenyang 已提交
132 133
      ret = -EINVAL;
      goto errout;
W
wenjun 已提交
134 135 136 137 138 139
    }

  /* Unbind the block driver from the file system (destroying any fs
   * private data.
   */

W
wangchenyang 已提交
140
  if (mnt->ops == NULL || mnt->ops->Unmount == NULL)
W
wenjun 已提交
141 142 143
    {
      /* The filesystem does not support the unbind operation ??? */

W
wangchenyang 已提交
144 145
      ret = -EINVAL;
      goto errout;
W
wenjun 已提交
146 147
    }

M
mamingshuai 已提交
148
#ifdef LOSCFG_FS_ZPFS
W
wangchenyang 已提交
149
  if (IsZpfsFileSystem(mountpt_vnode))
M
mamingshuai 已提交
150 151
    {
      isZpfs = true;
W
wangchenyang 已提交
152 153 154
      zpfsInode.i_private = mountpt_vnode->i_private;
      zpfsInode.u.i_ops = mountpt_vnode->u.i_ops;
      zpfsInode.i_flags = mountpt_vnode->i_flags;
M
mamingshuai 已提交
155 156 157
    }
#endif

W
wangchenyang 已提交
158 159
  /* Release the vnode under the mount point */
  if (fs_in_use(mnt, target))
W
wenjun 已提交
160
    {
W
wangchenyang 已提交
161 162
      ret = -EBUSY;
      goto errout;
W
wenjun 已提交
163
    }
W
wangchenyang 已提交
164 165 166

  ret = VnodeFreeAll(mnt);
  if (ret != OK)
W
wenjun 已提交
167
    {
W
wangchenyang 已提交
168
      goto errout;
W
wenjun 已提交
169
    }
W
wangchenyang 已提交
170 171 172
  /* Umount the filesystem */
  ret = mnt->ops->Unmount(mnt, &blkdrvr_vnode);
  if (ret != OK)
W
wenjun 已提交
173
    {
W
wangchenyang 已提交
174
      goto errout;
W
wenjun 已提交
175 176
    }

W
wangchenyang 已提交
177 178 179
  VnodeFree(mountpt_vnode);
  LOS_ListDelete(&mnt->mountList);
  free(mnt);
W
wenjun 已提交
180 181

  /* Did the unbind method return a contained block driver */
W
wangchenyang 已提交
182
  if (blkdrvr_vnode)
W
wenjun 已提交
183
    {
W
wangchenyang 已提交
184
      ; /* block driver operations after umount */
W
wenjun 已提交
185 186
    }

M
mamingshuai 已提交
187 188 189 190 191 192
#ifdef LOSCFG_FS_ZPFS
  if (isZpfs)
    {
      ZpfsCleanUp((void*)&zpfsInode, fullpath);
    }
#endif
W
wangchenyang 已提交
193
  covered_vnode->newMount = NULL;
194
  covered_vnode->flag &= ~(VNODE_FLAG_MOUNT_ORIGIN);
W
wangchenyang 已提交
195
  VnodeDrop();
M
mamingshuai 已提交
196

W
wenjun 已提交
197 198 199 200
  return OK;

  /* A lot of goto's!  But they make the error handling much simpler */
errout:
W
wangchenyang 已提交
201 202
  VnodeDrop();
  set_errno(-ret);
W
wenjun 已提交
203 204 205 206 207 208 209 210 211 212 213 214
  return VFS_ERROR;
}

int umount2(const char* __target, int __flags)
{
    /* TODO: __flags need to be support */
    if (__flags) {
        set_errno(ENOSYS);
        return VFS_ERROR;
    }
    return umount(__target);
}