• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /****************************************************************************
2  * fs/vfs/fs_statfs.c
3  *
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.  The
7  * ASF licenses this file to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance with the
9  * License.  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, WITHOUT
15  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
16  * License for the specific language governing permissions and limitations
17  * 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