mount.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include "fs.h"
  2. #include "inode.h"
  3. #include "super.h"
  4. #include <minix/vfsif.h>
  5. #include <minix/bdev.h>
  6. /*===========================================================================*
  7. * fs_mount *
  8. *===========================================================================*/
  9. int fs_mount(dev_t dev, unsigned int flags, struct fsdriver_node *root_node,
  10. unsigned int *res_flags)
  11. {
  12. /* This function reads the superblock of the partition, gets the root inode
  13. * and sends back the details of them.
  14. */
  15. struct inode *root_ip;
  16. int r, readonly;
  17. fs_dev = dev;
  18. readonly = (flags & REQ_RDONLY) ? 1 : 0;
  19. /* Open the device the file system lives on. */
  20. if (bdev_open(fs_dev, readonly ? BDEV_R_BIT : (BDEV_R_BIT|BDEV_W_BIT) ) !=
  21. OK) {
  22. return(EINVAL);
  23. }
  24. /* Fill in the super block. */
  25. superblock.s_dev = fs_dev; /* read_super() needs to know which dev */
  26. r = read_super(&superblock);
  27. /* Is it recognized as a Minix filesystem? */
  28. if (r != OK) {
  29. superblock.s_dev = NO_DEV;
  30. bdev_close(fs_dev);
  31. return(r);
  32. }
  33. /* clean check: if rw and not clean, switch to readonly */
  34. if(!(superblock.s_flags & MFSFLAG_CLEAN) && !readonly) {
  35. if(bdev_close(fs_dev) != OK)
  36. panic("couldn't bdev_close after found unclean FS");
  37. readonly = 1;
  38. if (bdev_open(fs_dev, BDEV_R_BIT) != OK) {
  39. panic("couldn't bdev_open after found unclean FS");
  40. return(EINVAL);
  41. }
  42. printf("MFS: WARNING: FS 0x%llx unclean, mounting readonly\n", fs_dev);
  43. }
  44. lmfs_set_blocksize(superblock.s_block_size);
  45. /* Compute the current number of used zones, and report it to libminixfs.
  46. * Note that libminixfs really wants numbers of *blocks*, but this MFS
  47. * implementation dropped support for differing zone/block sizes a while ago.
  48. */
  49. used_zones = superblock.s_zones - count_free_bits(&superblock, ZMAP);
  50. lmfs_set_blockusage(superblock.s_zones, used_zones);
  51. /* Get the root inode of the mounted file system. */
  52. if( (root_ip = get_inode(fs_dev, ROOT_INODE)) == NULL) {
  53. printf("MFS: couldn't get root inode\n");
  54. superblock.s_dev = NO_DEV;
  55. bdev_close(fs_dev);
  56. return(EINVAL);
  57. }
  58. if(root_ip->i_mode == 0) {
  59. printf("%s:%d zero mode for root inode?\n", __FILE__, __LINE__);
  60. put_inode(root_ip);
  61. superblock.s_dev = NO_DEV;
  62. bdev_close(fs_dev);
  63. return(EINVAL);
  64. }
  65. superblock.s_rd_only = readonly;
  66. /* Root inode properties */
  67. root_node->fn_ino_nr = root_ip->i_num;
  68. root_node->fn_mode = root_ip->i_mode;
  69. root_node->fn_size = root_ip->i_size;
  70. root_node->fn_uid = root_ip->i_uid;
  71. root_node->fn_gid = root_ip->i_gid;
  72. root_node->fn_dev = NO_DEV;
  73. *res_flags = RES_NOFLAGS;
  74. /* Mark it dirty */
  75. if(!superblock.s_rd_only) {
  76. superblock.s_flags &= ~MFSFLAG_CLEAN;
  77. if(write_super(&superblock) != OK)
  78. panic("mounting: couldn't write dirty superblock");
  79. }
  80. return(r);
  81. }
  82. /*===========================================================================*
  83. * fs_mountpt *
  84. *===========================================================================*/
  85. int fs_mountpt(ino_t ino_nr)
  86. {
  87. /* This function looks up the mount point, it checks the condition whether
  88. * the partition can be mounted on the inode or not.
  89. */
  90. register struct inode *rip;
  91. int r = OK;
  92. mode_t bits;
  93. /* Temporarily open the file. */
  94. if( (rip = get_inode(fs_dev, ino_nr)) == NULL)
  95. return(EINVAL);
  96. if(rip->i_mountpoint) r = EBUSY;
  97. /* It may not be special. */
  98. bits = rip->i_mode & I_TYPE;
  99. if (bits == I_BLOCK_SPECIAL || bits == I_CHAR_SPECIAL) r = ENOTDIR;
  100. put_inode(rip);
  101. if(r == OK) rip->i_mountpoint = TRUE;
  102. return(r);
  103. }
  104. /*===========================================================================*
  105. * fs_unmount *
  106. *===========================================================================*/
  107. void fs_unmount(void)
  108. {
  109. /* Unmount a file system. */
  110. int count;
  111. struct inode *rip, *root_ip;
  112. /* See if the mounted device is busy. Only 1 inode using it should be
  113. * open --the root inode-- and that inode only 1 time. This is an integrity
  114. * check only: VFS expects the unmount to succeed either way.
  115. */
  116. count = 0;
  117. for (rip = &inode[0]; rip < &inode[NR_INODES]; rip++)
  118. if (rip->i_count > 0 && rip->i_dev == fs_dev) count += rip->i_count;
  119. if (count != 1)
  120. printf("MFS: file system has %d in-use inodes!\n", count);
  121. if ((root_ip = find_inode(fs_dev, ROOT_INODE)) == NULL)
  122. panic("MFS: couldn't find root inode\n");
  123. put_inode(root_ip);
  124. /* force any cached blocks out of memory */
  125. fs_sync();
  126. /* Mark it clean if we're allowed to write _and_ it was clean originally. */
  127. if (!superblock.s_rd_only) {
  128. superblock.s_flags |= MFSFLAG_CLEAN;
  129. write_super(&superblock);
  130. }
  131. /* Close the device the file system lives on. */
  132. bdev_close(fs_dev);
  133. /* Throw out blocks out of the VM cache, to prevent corruption later. */
  134. lmfs_invalidate(fs_dev);
  135. /* Finish off the unmount. */
  136. superblock.s_dev = NO_DEV;
  137. }