《操作系统》的实验代码。
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.

97 lines
1.9 KiB

12 years ago
  1. #include <defs.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <vfs.h>
  5. #include <inode.h>
  6. #include <sem.h>
  7. #include <kmalloc.h>
  8. #include <error.h>
  9. static semaphore_t bootfs_sem;
  10. static struct inode *bootfs_node = NULL;
  11. extern void vfs_devlist_init(void);
  12. // __alloc_fs - allocate memory for fs, and set fs type
  13. struct fs *
  14. __alloc_fs(int type) {
  15. struct fs *fs;
  16. if ((fs = kmalloc(sizeof(struct fs))) != NULL) {
  17. fs->fs_type = type;
  18. }
  19. return fs;
  20. }
  21. // vfs_init - vfs initialize
  22. void
  23. vfs_init(void) {
  24. sem_init(&bootfs_sem, 1);
  25. vfs_devlist_init();
  26. }
  27. // lock_bootfs - lock for bootfs
  28. static void
  29. lock_bootfs(void) {
  30. down(&bootfs_sem);
  31. }
  32. // ulock_bootfs - ulock for bootfs
  33. static void
  34. unlock_bootfs(void) {
  35. up(&bootfs_sem);
  36. }
  37. // change_bootfs - set the new fs inode
  38. static void
  39. change_bootfs(struct inode *node) {
  40. struct inode *old;
  41. lock_bootfs();
  42. {
  43. old = bootfs_node, bootfs_node = node;
  44. }
  45. unlock_bootfs();
  46. if (old != NULL) {
  47. vop_ref_dec(old);
  48. }
  49. }
  50. // vfs_set_bootfs - change the dir of file system
  51. int
  52. vfs_set_bootfs(char *fsname) {
  53. struct inode *node = NULL;
  54. if (fsname != NULL) {
  55. char *s;
  56. if ((s = strchr(fsname, ':')) == NULL || s[1] != '\0') {
  57. return -E_INVAL;
  58. }
  59. int ret;
  60. if ((ret = vfs_chdir(fsname)) != 0) {
  61. return ret;
  62. }
  63. if ((ret = vfs_get_curdir(&node)) != 0) {
  64. return ret;
  65. }
  66. }
  67. change_bootfs(node);
  68. return 0;
  69. }
  70. // vfs_get_bootfs - get the inode of bootfs
  71. int
  72. vfs_get_bootfs(struct inode **node_store) {
  73. struct inode *node = NULL;
  74. if (bootfs_node != NULL) {
  75. lock_bootfs();
  76. {
  77. if ((node = bootfs_node) != NULL) {
  78. vop_ref_inc(bootfs_node);
  79. }
  80. }
  81. unlock_bootfs();
  82. }
  83. if (node == NULL) {
  84. return -E_NOENT;
  85. }
  86. *node_store = node;
  87. return 0;
  88. }