《操作系统》的实验代码。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

191 lines
7.7 KiB

12 years ago
  1. #ifndef __KERN_FS_VFS_VFS_H__
  2. #define __KERN_FS_VFS_VFS_H__
  3. #include <defs.h>
  4. #include <fs.h>
  5. #include <sfs.h>
  6. struct inode; // abstract structure for an on-disk file (inode.h)
  7. struct device; // abstract structure for a device (dev.h)
  8. struct iobuf; // kernel or userspace I/O buffer (iobuf.h)
  9. /*
  10. * Abstract filesystem. (Or device accessible as a file.)
  11. *
  12. * Information:
  13. * fs_info : filesystem-specific data (sfs_fs)
  14. * fs_type : filesystem type
  15. * Operations:
  16. *
  17. * fs_sync - Flush all dirty buffers to disk.
  18. * fs_get_root - Return root inode of filesystem.
  19. * fs_unmount - Attempt unmount of filesystem.
  20. * fs_cleanup - Cleanup of filesystem.???
  21. *
  22. *
  23. * fs_get_root should increment the refcount of the inode returned.
  24. * It should not ever return NULL.
  25. *
  26. * If fs_unmount returns an error, the filesystem stays mounted, and
  27. * consequently the struct fs instance should remain valid. On success,
  28. * however, the filesystem object and all storage associated with the
  29. * filesystem should have been discarded/released.
  30. *
  31. */
  32. struct fs {
  33. union {
  34. struct sfs_fs __sfs_info;
  35. } fs_info; // filesystem-specific data
  36. enum {
  37. fs_type_sfs_info,
  38. } fs_type; // filesystem type
  39. int (*fs_sync)(struct fs *fs); // Flush all dirty buffers to disk
  40. struct inode *(*fs_get_root)(struct fs *fs); // Return root inode of filesystem.
  41. int (*fs_unmount)(struct fs *fs); // Attempt unmount of filesystem.
  42. void (*fs_cleanup)(struct fs *fs); // Cleanup of filesystem.???
  43. };
  44. #define __fs_type(type) fs_type_##type##_info
  45. #define check_fs_type(fs, type) ((fs)->fs_type == __fs_type(type))
  46. #define __fsop_info(_fs, type) ({ \
  47. struct fs *__fs = (_fs); \
  48. assert(__fs != NULL && check_fs_type(__fs, type)); \
  49. &(__fs->fs_info.__##type##_info); \
  50. })
  51. #define fsop_info(fs, type) __fsop_info(fs, type)
  52. #define info2fs(info, type) \
  53. to_struct((info), struct fs, fs_info.__##type##_info)
  54. struct fs *__alloc_fs(int type);
  55. #define alloc_fs(type) __alloc_fs(__fs_type(type))
  56. // Macros to shorten the calling sequences.
  57. #define fsop_sync(fs) ((fs)->fs_sync(fs))
  58. #define fsop_get_root(fs) ((fs)->fs_get_root(fs))
  59. #define fsop_unmount(fs) ((fs)->fs_unmount(fs))
  60. #define fsop_cleanup(fs) ((fs)->fs_cleanup(fs))
  61. /*
  62. * Virtual File System layer functions.
  63. *
  64. * The VFS layer translates operations on abstract on-disk files or
  65. * pathnames to operations on specific files on specific filesystems.
  66. */
  67. void vfs_init(void);
  68. void vfs_cleanup(void);
  69. void vfs_devlist_init(void);
  70. /*
  71. * VFS layer low-level operations.
  72. * See inode.h for direct operations on inodes.
  73. * See fs.h for direct operations on filesystems/devices.
  74. *
  75. * vfs_set_curdir - change current directory of current thread by inode
  76. * vfs_get_curdir - retrieve inode of current directory of current thread
  77. * vfs_get_root - get root inode for the filesystem named DEVNAME
  78. * vfs_get_devname - get mounted device name for the filesystem passed in
  79. */
  80. int vfs_set_curdir(struct inode *dir);
  81. int vfs_get_curdir(struct inode **dir_store);
  82. int vfs_get_root(const char *devname, struct inode **root_store);
  83. const char *vfs_get_devname(struct fs *fs);
  84. /*
  85. * VFS layer high-level operations on pathnames
  86. * Because namei may destroy pathnames, these all may too.
  87. *
  88. * vfs_open - Open or create a file. FLAGS/MODE per the syscall.
  89. * vfs_close - Close a inode opened with vfs_open. Does not fail.
  90. * (See vfspath.c for a discussion of why.)
  91. * vfs_link - Create a hard link to a file.
  92. * vfs_symlink - Create a symlink PATH containing contents CONTENTS.
  93. * vfs_readlink - Read contents of a symlink into a uio.
  94. * vfs_mkdir - Create a directory. MODE per the syscall.
  95. * vfs_unlink - Delete a file/directory.
  96. * vfs_rename - rename a file.
  97. * vfs_chdir - Change current directory of current thread by name.
  98. * vfs_getcwd - Retrieve name of current directory of current thread.
  99. *
  100. */
  101. int vfs_open(char *path, uint32_t open_flags, struct inode **inode_store);
  102. int vfs_close(struct inode *node);
  103. int vfs_link(char *old_path, char *new_path);
  104. int vfs_symlink(char *old_path, char *new_path);
  105. int vfs_readlink(char *path, struct iobuf *iob);
  106. int vfs_mkdir(char *path);
  107. int vfs_unlink(char *path);
  108. int vfs_rename(char *old_path, char *new_path);
  109. int vfs_chdir(char *path);
  110. int vfs_getcwd(struct iobuf *iob);
  111. /*
  112. * VFS layer mid-level operations.
  113. *
  114. * vfs_lookup - Like VOP_LOOKUP, but takes a full device:path name,
  115. * or a name relative to the current directory, and
  116. * goes to the correct filesystem.
  117. * vfs_lookparent - Likewise, for VOP_LOOKPARENT.
  118. *
  119. * Both of these may destroy the path passed in.
  120. */
  121. int vfs_lookup(char *path, struct inode **node_store);
  122. int vfs_lookup_parent(char *path, struct inode **node_store, char **endp);
  123. /*
  124. * Misc
  125. *
  126. * vfs_set_bootfs - Set the filesystem that paths beginning with a
  127. * slash are sent to. If not set, these paths fail
  128. * with ENOENT. The argument should be the device
  129. * name or volume name for the filesystem (such as
  130. * "lhd0:") but need not have the trailing colon.
  131. *
  132. * vfs_get_bootfs - return the inode of the bootfs filesystem.
  133. *
  134. * vfs_add_fs - Add a hardwired filesystem to the VFS named device
  135. * list. It will be accessible as "devname:". This is
  136. * intended for filesystem-devices like emufs, and
  137. * gizmos like Linux procfs or BSD kernfs, not for
  138. * mounting filesystems on disk devices.
  139. *
  140. * vfs_add_dev - Add a device to the VFS named device list. If
  141. * MOUNTABLE is zero, the device will be accessible
  142. * as "DEVNAME:". If the mountable flag is set, the
  143. * device will be accessible as "DEVNAMEraw:" and
  144. * mountable under the name "DEVNAME". Thus, the
  145. * console, added with MOUNTABLE not set, would be
  146. * accessed by pathname as "con:", and lhd0, added
  147. * with mountable set, would be accessed by
  148. * pathname as "lhd0raw:" and mounted by passing
  149. * "lhd0" to vfs_mount.
  150. *
  151. * vfs_mount - Attempt to mount a filesystem on a device. The
  152. * device named by DEVNAME will be looked up and
  153. * passed, along with DATA, to the supplied function
  154. * MOUNTFUNC, which should create a struct fs and
  155. * return it in RESULT.
  156. *
  157. * vfs_unmount - Unmount the filesystem presently mounted on the
  158. * specified device.
  159. *
  160. * vfs_unmountall - Unmount all mounted filesystems.
  161. */
  162. int vfs_set_bootfs(char *fsname);
  163. int vfs_get_bootfs(struct inode **node_store);
  164. int vfs_add_fs(const char *devname, struct fs *fs);
  165. int vfs_add_dev(const char *devname, struct inode *devnode, bool mountable);
  166. int vfs_mount(const char *devname, int (*mountfunc)(struct device *dev, struct fs **fs_store));
  167. int vfs_unmount(const char *devname);
  168. int vfs_unmount_all(void);
  169. #endif /* !__KERN_FS_VFS_VFS_H__ */