fs_statfs.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /****************************************************************************
  2. * fs/vfs/fs_statfs.c
  3. *
  4. * Copyright (C) 2007-2009, 2012, 2017 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <sys/statfs.h>
  40. #include <string.h>
  41. #include <limits.h>
  42. #include <sched.h>
  43. #include <errno.h>
  44. #include "inode/inode.h"
  45. /****************************************************************************
  46. * Private Functions
  47. ****************************************************************************/
  48. /****************************************************************************
  49. * Name: statpseudo
  50. ****************************************************************************/
  51. static int statpseudofs(FAR struct inode *inode, FAR struct statfs *buf)
  52. {
  53. memset(buf, 0, sizeof(struct statfs));
  54. buf->f_type = PROC_SUPER_MAGIC;
  55. buf->f_namelen = NAME_MAX;
  56. return OK;
  57. }
  58. /****************************************************************************
  59. * Public Functions
  60. ****************************************************************************/
  61. /****************************************************************************
  62. * Name: statfs
  63. *
  64. * Returned Value:
  65. * Zero on success; -1 on failure with errno set:
  66. *
  67. * EACCES Search permission is denied for one of the directories in the
  68. * path prefix of path.
  69. * EFAULT Bad address.
  70. * ENOENT A component of the path path does not exist, or the path is an
  71. * empty string.
  72. * ENOMEM Out of memory
  73. * ENOTDIR A component of the path is not a directory.
  74. * ENOSYS The file system does not support this call.
  75. *
  76. ****************************************************************************/
  77. int statfs(FAR const char *path, FAR struct statfs *buf)
  78. {
  79. struct inode_search_s desc;
  80. FAR struct inode *inode;
  81. int ret = OK;
  82. /* Sanity checks */
  83. if (path == NULL || buf == NULL)
  84. {
  85. ret = EFAULT;
  86. goto errout;
  87. }
  88. if (*path == '\0')
  89. {
  90. ret = ENOENT;
  91. goto errout;
  92. }
  93. /* Get an inode for this file */
  94. SETUP_SEARCH(&desc, path, false);
  95. ret = inode_find(&desc);
  96. if (ret < 0)
  97. {
  98. /* This name does not refer to a psudeo-inode and there is no
  99. * mountpoint that includes in this path.
  100. */
  101. ret = -ret;
  102. goto errout_with_search;
  103. }
  104. /* Get the search results */
  105. inode = desc.node;
  106. DEBUGASSERT(inode != NULL);
  107. /* The way we handle the statfs depends on the type of inode that we
  108. * are dealing with.
  109. */
  110. #ifndef CONFIG_DISABLE_MOUNTPOINT
  111. if (INODE_IS_MOUNTPT(inode))
  112. {
  113. /* The node is a file system mointpoint. Verify that the mountpoint
  114. * supports the statfs() method
  115. */
  116. if (inode->u.i_mops && inode->u.i_mops->statfs)
  117. {
  118. /* Perform the statfs() operation */
  119. ret = inode->u.i_mops->statfs(inode, buf);
  120. }
  121. }
  122. else
  123. #endif
  124. {
  125. /* The node is part of the root pseudo file system */
  126. ret = statpseudofs(inode, buf);
  127. }
  128. /* Check if the statfs operation was successful */
  129. if (ret < 0)
  130. {
  131. ret = -ret;
  132. goto errout_with_inode;
  133. }
  134. /* Successfully statfs'ed the file */
  135. inode_release(inode);
  136. RELEASE_SEARCH(&desc);
  137. return OK;
  138. /* Failure conditions always set the errno appropriately */
  139. errout_with_inode:
  140. inode_release(inode);
  141. errout_with_search:
  142. RELEASE_SEARCH(&desc);
  143. errout:
  144. set_errno(ret);
  145. return ERROR;
  146. }