ialloc.c 5.8 KB

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