vnode.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
  3. * The President and Fellows of Harvard College.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #ifndef _VNODE_H_
  30. #define _VNODE_H_
  31. struct uio;
  32. struct stat;
  33. /*
  34. * A struct vnode is an abstract representation of a file.
  35. *
  36. * It is an interface in the Java sense that allows the kernel's
  37. * filesystem-independent code to interact usefully with multiple sets
  38. * of filesystem code.
  39. */
  40. /*
  41. * Abstract low-level file.
  42. *
  43. * Note: vn_fs may be null if the vnode refers to a device.
  44. *
  45. * vn_opencount is managed using VOP_INCOPEN and VOP_DECOPEN by
  46. * vfs_open() and vfs_close(). Code above the VFS layer should not
  47. * need to worry about it.
  48. */
  49. struct vnode {
  50. int vn_refcount; /* Reference count */
  51. int vn_opencount;
  52. struct fs *vn_fs; /* Filesystem vnode belongs to */
  53. void *vn_data; /* Filesystem-specific data */
  54. const struct vnode_ops *vn_ops; /* Functions on this vnode */
  55. };
  56. /*
  57. * Abstract operations on a vnode.
  58. *
  59. * These are used in the form VOP_FOO(vnode, args), which are macros
  60. * that expands to vnode->vn_ops->vop_foo(vnode, args). The operations
  61. * "foo" are:
  62. *
  63. * vop_open - Called on *each* open() of a file. Can be used to
  64. * reject illegal or undesired open modes. Note that
  65. * various operations can be performed without the
  66. * file actually being opened.
  67. * The vnode need not look at O_CREAT, O_EXCL, or
  68. * O_TRUNC, as these are handled in the VFS layer.
  69. *
  70. * VOP_OPEN should not be called directly from above
  71. * the VFS layer - use vfs_open() to open vnodes.
  72. * This maintains the open count so VOP_CLOSE can be
  73. * called at the right time.
  74. *
  75. * vop_close - To be called on *last* close() of a file.
  76. *
  77. * VOP_CLOSE should not be called directly from above
  78. * the VFS layer - use vfs_close() to close vnodes
  79. * opened with vfs_open().
  80. *
  81. * vop_reclaim - Called when vnode is no longer in use. Note that
  82. * this may be substantially after vop_close is
  83. * called.
  84. *
  85. *****************************************
  86. *
  87. * vop_read - Read data from file to uio, at offset specified
  88. * in the uio, updating uio_resid to reflect the
  89. * amount read, and updating uio_offset to match.
  90. * Not allowed on directories or symlinks.
  91. *
  92. * vop_readlink - Read the contents of a symlink into a uio.
  93. * Not allowed on other types of object.
  94. *
  95. * vop_getdirentry - Read a single filename from a directory into a
  96. * uio, choosing what name based on the offset
  97. * field in the uio, and updating that field.
  98. * Unlike with I/O on regular files, the value of
  99. * the offset field is not interpreted outside
  100. * the filesystem and thus need not be a byte
  101. * count. However, the uio_resid field should be
  102. * handled in the normal fashion.
  103. * On non-directory objects, return ENOTDIR.
  104. *
  105. * vop_write - Write data from uio to file at offset specified
  106. * in the uio, updating uio_resid to reflect the
  107. * amount written, and updating uio_offset to match.
  108. * Not allowed on directories or symlinks.
  109. *
  110. * vop_ioctl - Perform ioctl operation OP on file using data
  111. * DATA. The interpretation of the data is specific
  112. * to each ioctl.
  113. *
  114. * vop_stat - Return info about a file. The pointer is a
  115. * pointer to struct stat; see kern/stat.h.
  116. *
  117. * vop_gettype - Return type of file. The values for file types
  118. * are in kern/stattypes.h.
  119. *
  120. * vop_tryseek - Check if seeking to the specified position within
  121. * the file is legal. (For instance, all seeks
  122. * are illegal on serial port devices, and seeks
  123. * past EOF on files whose sizes are fixed may be
  124. * as well.)
  125. *
  126. * vop_fsync - Force any dirty buffers associated with this file
  127. * to stable storage.
  128. *
  129. * vop_mmap - Map file into memory. If you implement this
  130. * feature, you're responsible for choosing the
  131. * arguments for this operation.
  132. *
  133. * vop_truncate - Forcibly set size of file to the length passed
  134. * in, discarding any excess blocks.
  135. *
  136. * vop_namefile - Compute pathname relative to filesystem root
  137. * of the file and copy to the specified
  138. * uio. Need not work on objects that are not
  139. * directories.
  140. *
  141. *****************************************
  142. *
  143. * vop_creat - Create a regular file named NAME in the passed
  144. * directory DIR. If boolean EXCL is true, fail if
  145. * the file already exists; otherwise, use the
  146. * existing file if there is one. Hand back the
  147. * vnode for the file as per vop_lookup.
  148. *
  149. * vop_symlink - Create symlink named NAME in the passed directory,
  150. * with contents CONTENTS.
  151. *
  152. * vop_mkdir - Make directory NAME in the passed directory PARENTDIR.
  153. *
  154. * vop_link - Create hard link, with name NAME, to file FILE
  155. * in the passed directory DIR.
  156. *
  157. * vop_remove - Delete non-directory object NAME from passed
  158. * directory. If NAME refers to a directory,
  159. * return EISDIR. If passed vnode is not a
  160. * directory, return ENOTDIR.
  161. *
  162. * vop_rmdir - Delete directory object NAME from passed
  163. * directory.
  164. *
  165. * vop_rename - Rename file NAME1 in directory VN1 to be
  166. * file NAME2 in directory VN2.
  167. *
  168. *****************************************
  169. *
  170. * vop_lookup - Parse PATHNAME relative to the passed directory
  171. * DIR, and hand back the vnode for the file it
  172. * refers to. May destroy PATHNAME. Should increment
  173. * refcount on vnode handed back.
  174. *
  175. * vop_lookparent - Parse PATHNAME relative to the passed directory
  176. * DIR, and hand back (1) the vnode for the
  177. * parent directory of the file it refers to, and
  178. * (2) the last component of the filename, copied
  179. * into kernel buffer BUF with max length LEN. May
  180. * destroy PATHNAME. Should increment refcount on
  181. * vnode handed back.
  182. */
  183. #define VOP_MAGIC 0xa2b3c4d5
  184. struct vnode_ops {
  185. unsigned long vop_magic; /* should always be VOP_MAGIC */
  186. int (*vop_open)(struct vnode *object, int flags_from_open);
  187. int (*vop_close)(struct vnode *object);
  188. int (*vop_reclaim)(struct vnode *vnode);
  189. int (*vop_read)(struct vnode *file, struct uio *uio);
  190. int (*vop_readlink)(struct vnode *link, struct uio *uio);
  191. int (*vop_getdirentry)(struct vnode *dir, struct uio *uio);
  192. int (*vop_write)(struct vnode *file, struct uio *uio);
  193. int (*vop_ioctl)(struct vnode *object, int op, userptr_t data);
  194. int (*vop_stat)(struct vnode *object, struct stat *statbuf);
  195. int (*vop_gettype)(struct vnode *object, mode_t *result);
  196. int (*vop_tryseek)(struct vnode *object, off_t pos);
  197. int (*vop_fsync)(struct vnode *object);
  198. int (*vop_mmap)(struct vnode *file /* add stuff */);
  199. int (*vop_truncate)(struct vnode *file, off_t len);
  200. int (*vop_namefile)(struct vnode *file, struct uio *uio);
  201. int (*vop_creat)(struct vnode *dir,
  202. const char *name, bool excl, mode_t mode,
  203. struct vnode **result);
  204. int (*vop_symlink)(struct vnode *dir,
  205. const char *contents, const char *name);
  206. int (*vop_mkdir)(struct vnode *parentdir,
  207. const char *name, mode_t mode);
  208. int (*vop_link)(struct vnode *dir,
  209. const char *name, struct vnode *file);
  210. int (*vop_remove)(struct vnode *dir,
  211. const char *name);
  212. int (*vop_rmdir)(struct vnode *dir,
  213. const char *name);
  214. int (*vop_rename)(struct vnode *vn1, const char *name1,
  215. struct vnode *vn2, const char *name2);
  216. int (*vop_lookup)(struct vnode *dir,
  217. char *pathname, struct vnode **result);
  218. int (*vop_lookparent)(struct vnode *dir,
  219. char *pathname, struct vnode **result,
  220. char *buf, size_t len);
  221. };
  222. #define __VOP(vn, sym) (vnode_check(vn, #sym), (vn)->vn_ops->vop_##sym)
  223. #define VOP_OPEN(vn, flags) (__VOP(vn, open)(vn, flags))
  224. #define VOP_CLOSE(vn) (__VOP(vn, close)(vn))
  225. #define VOP_RECLAIM(vn) (__VOP(vn, reclaim)(vn))
  226. #define VOP_READ(vn, uio) (__VOP(vn, read)(vn, uio))
  227. #define VOP_READLINK(vn, uio) (__VOP(vn, readlink)(vn, uio))
  228. #define VOP_GETDIRENTRY(vn, uio) (__VOP(vn,getdirentry)(vn, uio))
  229. #define VOP_WRITE(vn, uio) (__VOP(vn, write)(vn, uio))
  230. #define VOP_IOCTL(vn, code, buf) (__VOP(vn, ioctl)(vn,code,buf))
  231. #define VOP_STAT(vn, ptr) (__VOP(vn, stat)(vn, ptr))
  232. #define VOP_GETTYPE(vn, result) (__VOP(vn, gettype)(vn, result))
  233. #define VOP_TRYSEEK(vn, pos) (__VOP(vn, tryseek)(vn, pos))
  234. #define VOP_FSYNC(vn) (__VOP(vn, fsync)(vn))
  235. #define VOP_MMAP(vn /*add stuff */) (__VOP(vn, mmap)(vn /*add stuff */))
  236. #define VOP_TRUNCATE(vn, pos) (__VOP(vn, truncate)(vn, pos))
  237. #define VOP_NAMEFILE(vn, uio) (__VOP(vn, namefile)(vn, uio))
  238. #define VOP_CREAT(vn,nm,excl,mode,res) (__VOP(vn, creat)(vn,nm,excl,mode,res))
  239. #define VOP_SYMLINK(vn, name, content) (__VOP(vn, symlink)(vn, name, content))
  240. #define VOP_MKDIR(vn, name, mode) (__VOP(vn, mkdir)(vn, name, mode))
  241. #define VOP_LINK(vn, name, vn2) (__VOP(vn, link)(vn, name, vn2))
  242. #define VOP_REMOVE(vn, name) (__VOP(vn, remove)(vn, name))
  243. #define VOP_RMDIR(vn, name) (__VOP(vn, rmdir)(vn, name))
  244. #define VOP_RENAME(vn1,name1,vn2,name2)(__VOP(vn1,rename)(vn1,name1,vn2,name2))
  245. #define VOP_LOOKUP(vn, name, res) (__VOP(vn, lookup)(vn, name, res))
  246. #define VOP_LOOKPARENT(vn,nm,res,bf,ln) (__VOP(vn,lookparent)(vn,nm,res,bf,ln))
  247. /*
  248. * Consistency check
  249. */
  250. void vnode_check(struct vnode *, const char *op);
  251. /*
  252. * Reference count manipulation (handled above filesystem level)
  253. */
  254. void vnode_incref(struct vnode *);
  255. void vnode_decref(struct vnode *);
  256. #define VOP_INCREF(vn) vnode_incref(vn)
  257. #define VOP_DECREF(vn) vnode_decref(vn)
  258. /*
  259. * Open count manipulation (handled above filesystem level)
  260. *
  261. * VOP_INCOPEN is called by vfs_open. VOP_DECOPEN is called by vfs_close.
  262. * Neither of these should need to be called from above the vfs layer.
  263. */
  264. void vnode_incopen(struct vnode *);
  265. void vnode_decopen(struct vnode *);
  266. #define VOP_INCOPEN(vn) vnode_incopen(vn)
  267. #define VOP_DECOPEN(vn) vnode_decopen(vn)
  268. /*
  269. * Vnode initialization (intended for use by filesystem code)
  270. * The reference count is initialized to 1.
  271. */
  272. int vnode_init(struct vnode *, const struct vnode_ops *ops,
  273. struct fs *fs, void *fsdata);
  274. #define VOP_INIT(vn, ops, fs, data) vnode_init(vn, ops, fs, data)
  275. /*
  276. * Vnode final cleanup (intended for use by filesystem code)
  277. * The reference count is asserted to be 1.
  278. */
  279. void vnode_cleanup(struct vnode *);
  280. #define VOP_CLEANUP(vn) vnode_cleanup(vn)
  281. #endif /* _VNODE_H_ */