inode.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /****************************************************************************
  2. * fs/inode/inode.h
  3. *
  4. * Copyright (C) 2007, 2009, 2012, 2014, 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. #ifndef __FS_INODE_H
  36. #define __FS_INODE_H
  37. /****************************************************************************
  38. * Included Files
  39. ****************************************************************************/
  40. #include <nuttx/config.h>
  41. #include <nuttx/compiler.h>
  42. #include <sys/types.h>
  43. #include <stdint.h>
  44. #include <stdbool.h>
  45. #include <dirent.h>
  46. #include <nuttx/kmalloc.h>
  47. #include <nuttx/fs/fs.h>
  48. /****************************************************************************
  49. * Pre-processor Definitions
  50. ****************************************************************************/
  51. #ifdef CONFIG_PSEUDOFS_SOFTLINKS
  52. # define SETUP_SEARCH(d,p,n) \
  53. do \
  54. { \
  55. (d)->path = (p); \
  56. (d)->node = NULL; \
  57. (d)->peer = NULL; \
  58. (d)->parent = NULL; \
  59. (d)->relpath = NULL; \
  60. (d)->linktgt = NULL; \
  61. (d)->buffer = NULL; \
  62. (d)->nofollow = (n); \
  63. } \
  64. while (0)
  65. # define RELEASE_SEARCH(d) \
  66. if ((d)->buffer != NULL) \
  67. { \
  68. kmm_free((d)->buffer); \
  69. (d)->buffer = NULL; \
  70. }
  71. #else
  72. # define SETUP_SEARCH(d,p,n) \
  73. do \
  74. { \
  75. (d)->path = (p); \
  76. (d)->node = NULL; \
  77. (d)->peer = NULL; \
  78. (d)->parent = NULL; \
  79. (d)->relpath = NULL; \
  80. } \
  81. while (0)
  82. # define RELEASE_SEARCH(d)
  83. #endif
  84. /****************************************************************************
  85. * Public Types
  86. ****************************************************************************/
  87. /* This is the type of the argument to inode_search().
  88. *
  89. * path - INPUT: Path of inode to find
  90. * OUTPUT: Residual part of path not traversed
  91. * node - INPUT: (not used)
  92. * OUTPUT: On success, holds the pointer to the inode found.
  93. * peer - INPUT: (not used)
  94. * OUTPUT: The inode to the "left" of the inode found.
  95. * parent - INPUT: (not used)
  96. * OUTPUT: The inode to the "above" of the inode found.
  97. * relpath - INPUT: (not used)
  98. * OUTPUT: If the returned inode is a mountpoint, this is the
  99. * relative path from the mountpoint.
  100. * linktgt - INPUT: (not used)
  101. * OUTPUT: If a symobolic link into a mounted file system is
  102. * detected while traversing the path, then the link
  103. * will be converted to a mountpoint inode if the
  104. * mountpoint link is in an intermediate node of the
  105. * path or at the final node of the path with nofollow=true.
  106. * nofollow - INPUT: true: terminal node is returned; false: if the
  107. * terminal is a soft link, then return the inode of
  108. * the link target.
  109. * - OUTPUT: (not used)
  110. * buffer - INPUT: Not used
  111. * - OUTPUT: May hold an allocated intermediate path which is
  112. * probably of no interest to the caller unless it holds
  113. * the relpath.
  114. */
  115. struct inode_search_s
  116. {
  117. FAR const char *path; /* Path of inode to find */
  118. FAR struct inode *node; /* Pointer to the inode found */
  119. FAR struct inode *peer; /* Node to the "left" for the found inode */
  120. FAR struct inode *parent; /* Node "above" the found inode */
  121. FAR const char *relpath; /* Relative path into the mountpoint */
  122. #ifdef CONFIG_PSEUDOFS_SOFTLINKS
  123. FAR const char *linktgt; /* Target of symbolic link if linked to a directory */
  124. FAR char *buffer; /* Path expansion buffer */
  125. bool nofollow; /* true: Don't follow terminal soft link */
  126. #endif
  127. };
  128. /* Callback used by foreach_inode to traverse all inodes in the pseudo-
  129. * file system.
  130. */
  131. typedef int (*foreach_inode_t)(FAR struct inode *node,
  132. FAR char dirpath[PATH_MAX],
  133. FAR void *arg);
  134. /****************************************************************************
  135. * Public Data
  136. ****************************************************************************/
  137. #undef EXTERN
  138. #if defined(__cplusplus)
  139. #define EXTERN extern "C"
  140. extern "C"
  141. {
  142. #else
  143. #define EXTERN extern
  144. #endif
  145. EXTERN FAR struct inode *g_root_inode;
  146. /****************************************************************************
  147. * Public Function Prototypes
  148. ****************************************************************************/
  149. /****************************************************************************
  150. * Name: inode_initialize
  151. *
  152. * Description:
  153. * This is called from the OS initialization logic to configure the file
  154. * system.
  155. *
  156. ****************************************************************************/
  157. void inode_initialize(void);
  158. /****************************************************************************
  159. * Name: inode_semtake
  160. *
  161. * Description:
  162. * Get exclusive access to the in-memory inode tree (tree_sem).
  163. *
  164. ****************************************************************************/
  165. void inode_semtake(void);
  166. /****************************************************************************
  167. * Name: inode_semgive
  168. *
  169. * Description:
  170. * Relinquish exclusive access to the in-memory inode tree (tree_sem).
  171. *
  172. ****************************************************************************/
  173. void inode_semgive(void);
  174. /****************************************************************************
  175. * Name: inode_search
  176. *
  177. * Description:
  178. * Find the inode associated with 'path' returning the inode references
  179. * and references to its companion nodes.
  180. *
  181. * If a mountpoint is encountered in the search prior to encountering the
  182. * terminal node, the search will terminate at the mountpoint inode. That
  183. * inode and the relative path from the mountpoint, 'relpath' will be
  184. * returned.
  185. *
  186. * inode_search will follow soft links in path leading up to the terminal
  187. * node. Whether or no inode_search() will deference that terminal node
  188. * depends on the 'nofollow' input.
  189. *
  190. * If a soft link is encountered that is not the terminal node in the path,
  191. * that link WILL be deferenced unconditionally.
  192. *
  193. * Assumptions:
  194. * The caller holds the g_inode_sem semaphore
  195. *
  196. ****************************************************************************/
  197. int inode_search(FAR struct inode_search_s *desc);
  198. /****************************************************************************
  199. * Name: inode_find
  200. *
  201. * Description:
  202. * This is called from the open() logic to get a reference to the inode
  203. * associated with a path. This is accomplished by calling inode_search().
  204. * inode_find() is a simple wrapper around inode_search(). The primary
  205. * difference between inode_find() and inode_search is that inode_find()
  206. * will lock the inode tree and increment the reference count on the inode.
  207. *
  208. ****************************************************************************/
  209. int inode_find(FAR struct inode_search_s *desc);
  210. /****************************************************************************
  211. * Name: inode_stat
  212. *
  213. * Description:
  214. * The inode_stat() function will obtain information about an 'inode' in
  215. * the pseudo file system and will write it to the area pointed to by 'buf'.
  216. *
  217. * The 'buf' argument is a pointer to a stat structure, as defined in
  218. * <sys/stat.h>, into which information is placed concerning the file.
  219. *
  220. * Input Parameters:
  221. * inode - The indoe of interest
  222. * buf - The caller provide location in which to return information about
  223. * the inode.
  224. *
  225. * Returned Value:
  226. * Zero (OK) returned on success. Otherwise, a negated errno value is
  227. * returned to indicate the nature of the failure.
  228. *
  229. ****************************************************************************/
  230. struct stat; /* Forward reference */
  231. int inode_stat(FAR struct inode *inode, FAR struct stat *buf);
  232. /****************************************************************************
  233. * Name: inode_free
  234. *
  235. * Description:
  236. * Free resources used by an inode
  237. *
  238. ****************************************************************************/
  239. void inode_free(FAR struct inode *node);
  240. /****************************************************************************
  241. * Name: inode_nextname
  242. *
  243. * Description:
  244. * Given a path with node names separated by '/', return the next node
  245. * name.
  246. *
  247. ****************************************************************************/
  248. const char *inode_nextname(FAR const char *name);
  249. /****************************************************************************
  250. * Name: inode_reserve
  251. *
  252. * Description:
  253. * Reserve an (initialized) inode the pseudo file system.
  254. *
  255. * NOTE: Caller must hold the inode semaphore
  256. *
  257. * Input Parameters:
  258. * path - The path to the inode to create
  259. * inode - The location to return the inode pointer
  260. *
  261. * Returned Value:
  262. * Zero on success (with the inode point in 'inode'); A negated errno
  263. * value is returned on failure:
  264. *
  265. * EINVAL - 'path' is invalid for this operation
  266. * EEXIST - An inode already exists at 'path'
  267. * ENOMEM - Failed to allocate in-memory resources for the operation
  268. *
  269. ****************************************************************************/
  270. int inode_reserve(FAR const char *path, FAR struct inode **inode);
  271. /****************************************************************************
  272. * Name: inode_unlink
  273. *
  274. * Description:
  275. * Given a path, remove a the node from the in-memory, inode tree that the
  276. * path refers to. This is normally done in preparation to removing or
  277. * moving an inode.
  278. *
  279. * Assumptions/Limitations:
  280. * The caller must hold the inode semaphore
  281. *
  282. ****************************************************************************/
  283. FAR struct inode *inode_unlink(FAR const char *path);
  284. /****************************************************************************
  285. * Name: inode_remove
  286. *
  287. * Description:
  288. * Given a path, remove a the node from the in-memory, inode tree that the
  289. * path refers to and free all resources related to the inode. If the
  290. * inode is in-use, then it will be unlinked, but will not be freed until
  291. * the last reference to the inode is released.
  292. *
  293. * Assumptions/Limitations:
  294. * The caller must hold the inode semaphore
  295. *
  296. ****************************************************************************/
  297. int inode_remove(FAR const char *path);
  298. /****************************************************************************
  299. * Name: inode_addref
  300. *
  301. * Description:
  302. * Increment the reference count on an inode (as when a file descriptor
  303. * is dup'ed).
  304. *
  305. ****************************************************************************/
  306. void inode_addref(FAR struct inode *inode);
  307. /****************************************************************************
  308. * Name: inode_release
  309. *
  310. * Description:
  311. * This is called from close() logic when it no longer refers to the inode.
  312. *
  313. ****************************************************************************/
  314. void inode_release(FAR struct inode *inode);
  315. /****************************************************************************
  316. * Name: foreach_inode
  317. *
  318. * Description:
  319. * Visit each inode in the pseudo-file system. The traversal is terminated
  320. * when the callback 'handler' returns a non-zero value, or when all of
  321. * the inodes have been visited.
  322. *
  323. * NOTE 1: Use with caution... The pseudo-file system is locked throughout
  324. * the traversal.
  325. * NOTE 2: The search algorithm is recursive and could, in principle, use
  326. * an indeterminant amount of stack space. This will not usually be a
  327. * real work issue.
  328. *
  329. ****************************************************************************/
  330. int foreach_inode(foreach_inode_t handler, FAR void *arg);
  331. /****************************************************************************
  332. * Name: files_initialize
  333. *
  334. * Description:
  335. * This is called from the FS initialization logic to configure the files.
  336. *
  337. ****************************************************************************/
  338. void weak_function files_initialize(void);
  339. /****************************************************************************
  340. * Name: files_allocate
  341. *
  342. * Description:
  343. * Allocate a struct files instance and associate it with an inode instance.
  344. * Returns the file descriptor == index into the files array.
  345. *
  346. ****************************************************************************/
  347. int files_allocate(FAR struct inode *inode, int oflags, off_t pos, int minfd);
  348. /****************************************************************************
  349. * Name: files_close
  350. *
  351. * Description:
  352. * Close an inode (if open)
  353. *
  354. * Assumuptions:
  355. * Caller holds the list semaphore because the file descriptor will be freed.
  356. *
  357. ****************************************************************************/
  358. int files_close(int fd);
  359. /****************************************************************************
  360. * Name: files_release
  361. *
  362. * Assumuptions:
  363. * Similar to files_close(). Called only from open() logic on error
  364. * conditions.
  365. *
  366. ****************************************************************************/
  367. void files_release(int fd);
  368. #undef EXTERN
  369. #if defined(__cplusplus)
  370. }
  371. #endif
  372. #endif /* __FS_INODE_H */