tmpfs.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /* $OpenBSD: tmpfs.h,v 1.7 2014/12/17 19:42:15 tedu Exp $ */
  2. /* $NetBSD: tmpfs.h,v 1.45 2011/09/27 01:10:43 christos Exp $ */
  3. /*
  4. * Copyright (c) 2005, 2006, 2007 The NetBSD Foundation, Inc.
  5. * All rights reserved.
  6. *
  7. * This code is derived from software contributed to The NetBSD Foundation
  8. * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
  9. * 2005 program.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  24. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #ifndef _TMPFS_TMPFS_H_
  33. #define _TMPFS_TMPFS_H_
  34. #if !defined(_KERNEL) && !defined(_KMEMUSER)
  35. #error "not supposed to be exposed to userland"
  36. #endif
  37. #include <sys/dirent.h>
  38. #include <sys/mount.h>
  39. #include <sys/pool.h>
  40. #include <sys/queue.h>
  41. #include <sys/stdint.h>
  42. #include <sys/rwlock.h>
  43. #include <sys/lock.h>
  44. #include <uvm/uvm_extern.h>
  45. /*
  46. * Internal representation of a tmpfs directory entry.
  47. *
  48. * All fields are protected by vnode lock.
  49. */
  50. typedef struct tmpfs_dirent {
  51. TAILQ_ENTRY(tmpfs_dirent) td_entries;
  52. /* Pointer to the inode this entry refers to. */
  53. struct tmpfs_node * td_node;
  54. /* Sequence number, see tmpfs_dir_getseq(). */
  55. uint64_t td_seq;
  56. /* Name and its length. */
  57. char * td_name;
  58. uint16_t td_namelen;
  59. } tmpfs_dirent_t;
  60. TAILQ_HEAD(tmpfs_dir, tmpfs_dirent);
  61. /*
  62. * Internal representation of a tmpfs file system node -- inode.
  63. *
  64. * This structure is split in two parts: one holds attributes common
  65. * to all file types and the other holds data that is only applicable to
  66. * a particular type.
  67. *
  68. * All fields are protected by vnode lock. The vnode association itself
  69. * is protected by tmpfs_node_t::tn_nlock.
  70. */
  71. typedef struct tmpfs_node {
  72. LIST_ENTRY(tmpfs_node) tn_entries;
  73. /*
  74. * Each inode has a corresponding vnode. It is a bi-directional
  75. * association. Whenever vnode is allocated, its v_data field is
  76. * set to the inode it reference, and tmpfs_node_t::tn_vnode is
  77. * set to point to the said vnode.
  78. *
  79. * Further attempts to allocate a vnode for this same node will
  80. * result in returning a new reference to the value stored in
  81. * tn_vnode. It may be NULL when the node is unused (that is,
  82. * no vnode has been allocated or it has been reclaimed).
  83. */
  84. struct rwlock tn_nlock; /* node lock */
  85. struct lock tn_vlock; /* vnode lock */
  86. struct vnode * tn_vnode;
  87. /* Directory entry. Only a hint, since hard link can have multiple. */
  88. tmpfs_dirent_t * tn_dirent_hint;
  89. /* The inode type: VBLK, VCHR, VDIR, VFIFO, VLNK, VREG or VSOCK. */
  90. enum vtype tn_type;
  91. /* Inode identifier and generation number. */
  92. ino_t tn_id;
  93. unsigned long tn_gen;
  94. /* The inode size. */
  95. off_t tn_size;
  96. /* Generic node attributes. */
  97. uid_t tn_uid;
  98. gid_t tn_gid;
  99. mode_t tn_mode;
  100. int tn_flags;
  101. nlink_t tn_links;
  102. struct timespec tn_atime;
  103. struct timespec tn_mtime;
  104. struct timespec tn_ctime;
  105. struct timespec tn_birthtime;
  106. /* Head of byte-level lock list (used by tmpfs_advlock). */
  107. struct lockf * tn_lockf;
  108. union {
  109. /* Type case: VBLK or VCHR. */
  110. struct {
  111. dev_t tn_rdev;
  112. } tn_dev;
  113. /* Type case: VDIR. */
  114. struct {
  115. /* Parent directory (root inode points to itself). */
  116. struct tmpfs_node * tn_parent;
  117. /* List of directory entries. */
  118. struct tmpfs_dir tn_dir;
  119. /* Last given sequence number. */
  120. uint64_t tn_next_seq;
  121. /*
  122. * Pointer of the last directory entry returned
  123. * by the readdir(3) operation.
  124. */
  125. struct tmpfs_dirent * tn_readdir_lastp;
  126. } tn_dir;
  127. /* Type case: VLNK. */
  128. struct tn_lnk {
  129. /* The link's target. */
  130. char * tn_link;
  131. } tn_lnk;
  132. /* Type case: VREG. */
  133. struct tn_reg {
  134. /* Underlying UVM object to store contents. */
  135. struct uvm_object * tn_aobj;
  136. size_t tn_aobj_pages;
  137. vaddr_t tn_aobj_pgptr;
  138. voff_t tn_aobj_pgnum;
  139. } tn_reg;
  140. } tn_spec;
  141. #define tn_uobj tn_spec.tn_reg.tn_aobj
  142. #define tn_pgptr tn_spec.tn_reg.tn_aobj_pgptr
  143. #define tn_pgnum tn_spec.tn_reg.tn_aobj_pgnum
  144. } tmpfs_node_t;
  145. #if defined(_KERNEL)
  146. LIST_HEAD(tmpfs_node_list, tmpfs_node);
  147. #define TMPFS_MAXNAMLEN 255
  148. /* Validate maximum td_namelen length. */
  149. /* CTASSERT(TMPFS_MAXNAMLEN < UINT16_MAX); */
  150. /*
  151. * Reserved values for the virtual entries (the first must be 0) and EOF.
  152. * The start/end of the incremental range, see tmpfs_dir_getseq().
  153. */
  154. #define TMPFS_DIRSEQ_DOT 0
  155. #define TMPFS_DIRSEQ_DOTDOT 1
  156. #define TMPFS_DIRSEQ_EOF 2
  157. #define TMPFS_DIRSEQ_START 3 /* inclusive */
  158. #define TMPFS_DIRSEQ_END UINT64_MAX /* exclusive */
  159. /* Mark to indicate that the number is not set. */
  160. #define TMPFS_DIRSEQ_NONE UINT64_MAX
  161. /* Can we still append entries to a directory? */
  162. #define TMPFS_DIRSEQ_FULL(dnode) \
  163. ((dnode)->tn_spec.tn_dir.tn_next_seq == TMPFS_DIRSEQ_END)
  164. /* Status flags. */
  165. #define TMPFS_NODE_ACCESSED 0x01
  166. #define TMPFS_NODE_MODIFIED 0x02
  167. #define TMPFS_NODE_CHANGED 0x04
  168. #define TMPFS_NODE_STATUSALL \
  169. (TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED | TMPFS_NODE_CHANGED)
  170. /*
  171. * Bit indicating vnode reclamation.
  172. * We abuse tmpfs_node_t::tn_gen for that.
  173. */
  174. #define TMPFS_NODE_GEN_MASK (~0UL >> 1)
  175. #define TMPFS_RECLAIMING_BIT (~TMPFS_NODE_GEN_MASK)
  176. #define TMPFS_NODE_RECLAIMING(node) \
  177. (((node)->tn_gen & TMPFS_RECLAIMING_BIT) != 0)
  178. #define TMPFS_NODE_GEN(node) \
  179. ((node)->tn_gen & TMPFS_NODE_GEN_MASK)
  180. /*
  181. * Internal representation of a tmpfs mount point.
  182. */
  183. typedef struct tmpfs_mount {
  184. /* Limit and number of bytes in use by the file system. */
  185. uint64_t tm_mem_limit;
  186. uint64_t tm_bytes_used;
  187. /* Highest allocated inode number. */
  188. uint64_t tm_highest_inode;
  189. struct rwlock tm_acc_lock;
  190. /* Pointer to the root inode. */
  191. tmpfs_node_t * tm_root;
  192. /* Maximum number of possible nodes for this file system. */
  193. unsigned int tm_nodes_max;
  194. /* Number of nodes currently allocated. */
  195. unsigned int tm_nodes_cnt;
  196. /* List of inodes and the lock protecting it. */
  197. struct rwlock tm_lock;
  198. struct tmpfs_node_list tm_nodes;
  199. } tmpfs_mount_t;
  200. /*
  201. * This structure maps a file identifier to a tmpfs node. Used by the
  202. * NFS code.
  203. */
  204. typedef struct tmpfs_fid {
  205. uint16_t tf_len;
  206. uint16_t tf_pad;
  207. uint32_t tf_gen;
  208. ino_t tf_id;
  209. } tmpfs_fid_t;
  210. /*
  211. * Prototypes for tmpfs_subr.c.
  212. */
  213. int tmpfs_alloc_node(tmpfs_mount_t *, enum vtype, uid_t, gid_t,
  214. mode_t, char *, dev_t, tmpfs_node_t **);
  215. void tmpfs_free_node(tmpfs_mount_t *, tmpfs_node_t *);
  216. int tmpfs_alloc_file(struct vnode *, struct vnode **, struct vattr *,
  217. struct componentname *, char *);
  218. int tmpfs_vnode_get(struct mount *, tmpfs_node_t *, struct vnode **);
  219. int tmpfs_alloc_dirent(tmpfs_mount_t *, const char *, uint16_t,
  220. tmpfs_dirent_t **);
  221. void tmpfs_free_dirent(tmpfs_mount_t *, tmpfs_dirent_t *);
  222. void tmpfs_dir_attach(tmpfs_node_t *, tmpfs_dirent_t *,
  223. tmpfs_node_t *);
  224. void tmpfs_dir_detach(tmpfs_node_t *, tmpfs_dirent_t *);
  225. tmpfs_dirent_t *tmpfs_dir_lookup(tmpfs_node_t *, struct componentname *);
  226. tmpfs_dirent_t *tmpfs_dir_cached(tmpfs_node_t *);
  227. uint64_t tmpfs_dir_getseq(tmpfs_node_t *, tmpfs_dirent_t *);
  228. tmpfs_dirent_t *tmpfs_dir_lookupbyseq(tmpfs_node_t *, off_t);
  229. int tmpfs_dir_getdents(tmpfs_node_t *, struct uio *);
  230. int tmpfs_reg_resize(struct vnode *, off_t);
  231. int tmpfs_truncate(struct vnode *, off_t);
  232. int tmpfs_chflags(struct vnode *, int, struct ucred *, struct proc *);
  233. int tmpfs_chmod(struct vnode *, mode_t, struct ucred *, struct proc *);
  234. int tmpfs_chown(struct vnode *, uid_t, gid_t, struct ucred *, struct proc *);
  235. int tmpfs_chsize(struct vnode *, u_quad_t, struct ucred *, struct proc *);
  236. int tmpfs_chtimes(struct vnode *, const struct timespec *,
  237. const struct timespec *, int, struct ucred *,
  238. struct proc *);
  239. void tmpfs_update(tmpfs_node_t *, int);
  240. int tmpfs_zeropg(tmpfs_node_t *, voff_t, vaddr_t);
  241. int tmpfs_uio_cached(tmpfs_node_t *);
  242. int tmpfs_uiomove(tmpfs_node_t *, struct uio *, vsize_t);
  243. void tmpfs_uio_uncache(tmpfs_node_t *);
  244. void tmpfs_uio_cache(tmpfs_node_t *, voff_t, vaddr_t);
  245. vaddr_t tmpfs_uio_lookup(tmpfs_node_t *, voff_t);
  246. /*
  247. * Prototypes for tmpfs_mem.c.
  248. */
  249. void tmpfs_mntmem_init(tmpfs_mount_t *, uint64_t);
  250. void tmpfs_mntmem_destroy(tmpfs_mount_t *);
  251. size_t tmpfs_mem_info(int);
  252. uint64_t tmpfs_bytes_max(tmpfs_mount_t *);
  253. uint64_t tmpfs_pages_avail(tmpfs_mount_t *);
  254. int tmpfs_mem_incr(tmpfs_mount_t *, size_t);
  255. void tmpfs_mem_decr(tmpfs_mount_t *, size_t);
  256. tmpfs_dirent_t *tmpfs_dirent_get(tmpfs_mount_t *);
  257. void tmpfs_dirent_put(tmpfs_mount_t *, tmpfs_dirent_t *);
  258. tmpfs_node_t * tmpfs_node_get(tmpfs_mount_t *);
  259. void tmpfs_node_put(tmpfs_mount_t *, tmpfs_node_t *);
  260. char * tmpfs_strname_alloc(tmpfs_mount_t *, size_t);
  261. void tmpfs_strname_free(tmpfs_mount_t *, char *, size_t);
  262. int tmpfs_strname_neqlen(struct componentname *, struct componentname *);
  263. /*
  264. * Ensures that the node pointed by 'node' is a directory and that its
  265. * contents are consistent with respect to directories.
  266. */
  267. #define TMPFS_VALIDATE_DIR(node) \
  268. KASSERT((node)->tn_vnode == NULL || VOP_ISLOCKED((node)->tn_vnode)); \
  269. KASSERT((node)->tn_type == VDIR); \
  270. KASSERT((node)->tn_size % sizeof(tmpfs_dirent_t) == 0);
  271. /*
  272. * Memory management stuff.
  273. */
  274. /* Amount of memory pages to reserve for the system. */
  275. #define TMPFS_PAGES_RESERVED (4 * 1024 * 1024 / PAGE_SIZE)
  276. /*
  277. * Routines to convert VFS structures to tmpfs internal ones.
  278. */
  279. static inline tmpfs_mount_t *
  280. VFS_TO_TMPFS(struct mount *mp)
  281. {
  282. tmpfs_mount_t *tmp = mp->mnt_data;
  283. KASSERT(tmp != NULL);
  284. return tmp;
  285. }
  286. static inline tmpfs_node_t *
  287. VP_TO_TMPFS_DIR(struct vnode *vp)
  288. {
  289. tmpfs_node_t *node = vp->v_data;
  290. KASSERT(node != NULL);
  291. TMPFS_VALIDATE_DIR(node);
  292. return node;
  293. }
  294. #endif /* defined(_KERNEL) */
  295. static __inline tmpfs_node_t *
  296. VP_TO_TMPFS_NODE(struct vnode *vp)
  297. {
  298. tmpfs_node_t *node = vp->v_data;
  299. #ifdef KASSERT
  300. KASSERT(node != NULL);
  301. #endif
  302. return node;
  303. }
  304. #endif /* _TMPFS_TMPFS_H_ */