ialloc.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * linux/fs/sysv/ialloc.c
  3. *
  4. * minix/bitmap.c
  5. * Copyright (C) 1991, 1992 Linus Torvalds
  6. *
  7. * ext/freelists.c
  8. * Copyright (C) 1992 Remy Card (card@masi.ibp.fr)
  9. *
  10. * xenix/alloc.c
  11. * Copyright (C) 1992 Doug Evans
  12. *
  13. * coh/alloc.c
  14. * Copyright (C) 1993 Pascal Haible, Bruno Haible
  15. *
  16. * sysv/ialloc.c
  17. * Copyright (C) 1993 Bruno Haible
  18. *
  19. * This file contains code for allocating/freeing inodes.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/stddef.h>
  23. #include <linux/sched.h>
  24. #include <linux/stat.h>
  25. #include <linux/string.h>
  26. #include <linux/buffer_head.h>
  27. #include <linux/writeback.h>
  28. #include "sysv.h"
  29. /* We don't trust the value of
  30. sb->sv_sbd2->s_tinode = *sb->sv_sb_total_free_inodes
  31. but we nevertheless keep it up to date. */
  32. /* An inode on disk is considered free if both i_mode == 0 and i_nlink == 0. */
  33. /* return &sb->sv_sb_fic_inodes[i] = &sbd->s_inode[i]; */
  34. static inline sysv_ino_t *
  35. sv_sb_fic_inode(struct super_block * sb, unsigned int i)
  36. {
  37. struct sysv_sb_info *sbi = SYSV_SB(sb);
  38. if (sbi->s_bh1 == sbi->s_bh2)
  39. return &sbi->s_sb_fic_inodes[i];
  40. else {
  41. /* 512 byte Xenix FS */
  42. unsigned int offset = offsetof(struct xenix_super_block, s_inode[i]);
  43. if (offset < 512)
  44. return (sysv_ino_t*)(sbi->s_sbd1 + offset);
  45. else
  46. return (sysv_ino_t*)(sbi->s_sbd2 + offset);
  47. }
  48. }
  49. struct sysv_inode *
  50. sysv_raw_inode(struct super_block *sb, unsigned ino, struct buffer_head **bh)
  51. {
  52. struct sysv_sb_info *sbi = SYSV_SB(sb);
  53. struct sysv_inode *res;
  54. int block = sbi->s_firstinodezone + sbi->s_block_base;
  55. block += (ino-1) >> sbi->s_inodes_per_block_bits;
  56. *bh = sb_bread(sb, block);
  57. if (!*bh)
  58. return NULL;
  59. res = (struct sysv_inode *)(*bh)->b_data;
  60. return res + ((ino-1) & sbi->s_inodes_per_block_1);
  61. }
  62. static int refill_free_cache(struct super_block *sb)
  63. {
  64. struct sysv_sb_info *sbi = SYSV_SB(sb);
  65. struct buffer_head * bh;
  66. struct sysv_inode * raw_inode;
  67. int i = 0, ino;
  68. ino = SYSV_ROOT_INO+1;
  69. raw_inode = sysv_raw_inode(sb, ino, &bh);
  70. if (!raw_inode)
  71. goto out;
  72. while (ino <= sbi->s_ninodes) {
  73. if (raw_inode->i_mode == 0 && raw_inode->i_nlink == 0) {
  74. *sv_sb_fic_inode(sb,i++) = cpu_to_fs16(SYSV_SB(sb), ino);
  75. if (i == sbi->s_fic_size)
  76. break;
  77. }
  78. if ((ino++ & sbi->s_inodes_per_block_1) == 0) {
  79. brelse(bh);
  80. raw_inode = sysv_raw_inode(sb, ino, &bh);
  81. if (!raw_inode)
  82. goto out;
  83. } else
  84. raw_inode++;
  85. }
  86. brelse(bh);
  87. out:
  88. return i;
  89. }
  90. void sysv_free_inode(struct inode * inode)
  91. {
  92. struct super_block *sb = inode->i_sb;
  93. struct sysv_sb_info *sbi = SYSV_SB(sb);
  94. unsigned int ino;
  95. struct buffer_head * bh;
  96. struct sysv_inode * raw_inode;
  97. unsigned count;
  98. sb = inode->i_sb;
  99. ino = inode->i_ino;
  100. if (ino <= SYSV_ROOT_INO || ino > sbi->s_ninodes) {
  101. printk("sysv_free_inode: inode 0,1,2 or nonexistent inode\n");
  102. return;
  103. }
  104. raw_inode = sysv_raw_inode(sb, ino, &bh);
  105. if (!raw_inode) {
  106. printk("sysv_free_inode: unable to read inode block on device "
  107. "%s\n", inode->i_sb->s_id);
  108. return;
  109. }
  110. mutex_lock(&sbi->s_lock);
  111. count = fs16_to_cpu(sbi, *sbi->s_sb_fic_count);
  112. if (count < sbi->s_fic_size) {
  113. *sv_sb_fic_inode(sb,count++) = cpu_to_fs16(sbi, ino);
  114. *sbi->s_sb_fic_count = cpu_to_fs16(sbi, count);
  115. }
  116. fs16_add(sbi, sbi->s_sb_total_free_inodes, 1);
  117. dirty_sb(sb);
  118. memset(raw_inode, 0, sizeof(struct sysv_inode));
  119. mark_buffer_dirty(bh);
  120. mutex_unlock(&sbi->s_lock);
  121. brelse(bh);
  122. }
  123. struct inode * sysv_new_inode(const struct inode * dir, umode_t mode)
  124. {
  125. struct super_block *sb = dir->i_sb;
  126. struct sysv_sb_info *sbi = SYSV_SB(sb);
  127. struct inode *inode;
  128. sysv_ino_t ino;
  129. unsigned count;
  130. struct writeback_control wbc = {
  131. .sync_mode = WB_SYNC_NONE
  132. };
  133. inode = new_inode(sb);
  134. if (!inode)
  135. return ERR_PTR(-ENOMEM);
  136. mutex_lock(&sbi->s_lock);
  137. count = fs16_to_cpu(sbi, *sbi->s_sb_fic_count);
  138. if (count == 0 || (*sv_sb_fic_inode(sb,count-1) == 0)) {
  139. count = refill_free_cache(sb);
  140. if (count == 0) {
  141. iput(inode);
  142. mutex_unlock(&sbi->s_lock);
  143. return ERR_PTR(-ENOSPC);
  144. }
  145. }
  146. /* Now count > 0. */
  147. ino = *sv_sb_fic_inode(sb,--count);
  148. *sbi->s_sb_fic_count = cpu_to_fs16(sbi, count);
  149. fs16_add(sbi, sbi->s_sb_total_free_inodes, -1);
  150. dirty_sb(sb);
  151. inode_init_owner(inode, dir, mode);
  152. inode->i_ino = fs16_to_cpu(sbi, ino);
  153. inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
  154. inode->i_blocks = 0;
  155. memset(SYSV_I(inode)->i_data, 0, sizeof(SYSV_I(inode)->i_data));
  156. SYSV_I(inode)->i_dir_start_lookup = 0;
  157. insert_inode_hash(inode);
  158. mark_inode_dirty(inode);
  159. sysv_write_inode(inode, &wbc); /* ensure inode not allocated again */
  160. mark_inode_dirty(inode); /* cleared by sysv_write_inode() */
  161. /* That's it. */
  162. mutex_unlock(&sbi->s_lock);
  163. return inode;
  164. }
  165. unsigned long sysv_count_free_inodes(struct super_block * sb)
  166. {
  167. struct sysv_sb_info *sbi = SYSV_SB(sb);
  168. struct buffer_head * bh;
  169. struct sysv_inode * raw_inode;
  170. int ino, count, sb_count;
  171. mutex_lock(&sbi->s_lock);
  172. sb_count = fs16_to_cpu(sbi, *sbi->s_sb_total_free_inodes);
  173. if (0)
  174. goto trust_sb;
  175. /* this causes a lot of disk traffic ... */
  176. count = 0;
  177. ino = SYSV_ROOT_INO+1;
  178. raw_inode = sysv_raw_inode(sb, ino, &bh);
  179. if (!raw_inode)
  180. goto Eio;
  181. while (ino <= sbi->s_ninodes) {
  182. if (raw_inode->i_mode == 0 && raw_inode->i_nlink == 0)
  183. count++;
  184. if ((ino++ & sbi->s_inodes_per_block_1) == 0) {
  185. brelse(bh);
  186. raw_inode = sysv_raw_inode(sb, ino, &bh);
  187. if (!raw_inode)
  188. goto Eio;
  189. } else
  190. raw_inode++;
  191. }
  192. brelse(bh);
  193. if (count != sb_count)
  194. goto Einval;
  195. out:
  196. mutex_unlock(&sbi->s_lock);
  197. return count;
  198. Einval:
  199. printk("sysv_count_free_inodes: "
  200. "free inode count was %d, correcting to %d\n",
  201. sb_count, count);
  202. if (!(sb->s_flags & MS_RDONLY)) {
  203. *sbi->s_sb_total_free_inodes = cpu_to_fs16(SYSV_SB(sb), count);
  204. dirty_sb(sb);
  205. }
  206. goto out;
  207. Eio:
  208. printk("sysv_count_free_inodes: unable to read inode table\n");
  209. trust_sb:
  210. count = sb_count;
  211. goto out;
  212. }