• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /****************************************************************************
2  * fs/vfs/fs_statfs.c
3  *
4  * Copyright (c) 2023 Huawei Device Co., Ltd. All rights reserved.
5  * Based on NuttX originally from nuttx source (nuttx/fs/ and nuttx/drivers/)
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ****************************************************************************/
20 
21 /****************************************************************************
22  * Included Files
23  ****************************************************************************/
24 
25 #include "vfs_config.h"
26 
27 #include "sys/statfs.h"
28 #include "string.h"
29 #include "sched.h"
30 #include "vnode.h"
31 #include "fs/mount.h"
32 #include "errno.h"
33 #include "stdlib.h"
34 
35 /****************************************************************************
36  * Public Functions
37  ****************************************************************************/
38 
39 /****************************************************************************
40  * Name: statfs
41  *
42  * Returned Value:
43  *   Zero on success; -1 on failure with errno set:
44  *
45  *   EACCES  Search permission is denied for one of the directories in the
46  *           path prefix of path.
47  *   EFAULT  Bad address.
48  *   ENOENT  A component of the path path does not exist, or the path is an
49  *           empty string.
50  *   ENOMEM  Out of memory
51  *   ENOTDIR A component of the path is not a directory.
52  *   ENOSYS  The file system does not support this call.
53  *
54  ****************************************************************************/
55 
statfs(const char * path,struct statfs * buf)56 int statfs(const char *path, struct statfs *buf)
57 {
58   struct Vnode *vnode = NULL;
59   struct Mount *mnt = NULL;
60   int ret = OK;
61 
62   /* Sanity checks */
63   if (!path || !buf)
64     {
65       ret = -EFAULT;
66       goto errout;
67     }
68   if (!path[0])
69     {
70       ret = -ENOENT;
71       goto errout;
72     }
73   /* Get an vnode for this file */
74   VnodeHold();
75   ret = VnodeLookup(path, &vnode, 0);
76   if (ret < 0)
77     {
78       VnodeDrop();
79       goto errout;
80     }
81   vnode->useCount++;
82   VnodeDrop();
83 
84   mnt = vnode->originMount;
85   if (mnt == NULL || mnt->ops == NULL || mnt->ops->Statfs == NULL)
86     {
87       ret = -ENOSYS;
88       goto errout_with_useCount;
89     }
90   else
91     {
92       ret = mnt->ops->Statfs(mnt, buf);
93       if (ret < 0)
94         {
95           goto errout_with_useCount;
96         }
97     }
98 
99   VnodeHold();
100   vnode->useCount--;
101   VnodeDrop();
102 
103   return OK;
104 
105   /* Failure conditions always set the errno appropriately */
106 
107 errout_with_useCount:
108   VnodeHold();
109   vnode->useCount--;
110   VnodeDrop();
111 errout:
112   set_errno(-ret);
113   return VFS_ERROR;
114 }
115