ialloc.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * linux/fs/ufs/ialloc.c
  3. *
  4. * Copyright (c) 1998
  5. * Daniel Pirkl <daniel.pirkl@email.cz>
  6. * Charles University, Faculty of Mathematics and Physics
  7. *
  8. * from
  9. *
  10. * linux/fs/ext2/ialloc.c
  11. *
  12. * Copyright (C) 1992, 1993, 1994, 1995
  13. * Remy Card (card@masi.ibp.fr)
  14. * Laboratoire MASI - Institut Blaise Pascal
  15. * Universite Pierre et Marie Curie (Paris VI)
  16. *
  17. * BSD ufs-inspired inode and directory allocation by
  18. * Stephen Tweedie (sct@dcs.ed.ac.uk), 1993
  19. * Big-endian to little-endian byte-swapping/bitmaps by
  20. * David S. Miller (davem@caip.rutgers.edu), 1995
  21. */
  22. #include <linux/fs.h>
  23. #include <linux/ufs_fs.h>
  24. #include <linux/time.h>
  25. #include <linux/stat.h>
  26. #include <linux/string.h>
  27. #include <linux/quotaops.h>
  28. #include <linux/buffer_head.h>
  29. #include <linux/sched.h>
  30. #include <linux/bitops.h>
  31. #include <asm/byteorder.h>
  32. #include "swab.h"
  33. #include "util.h"
  34. #undef UFS_IALLOC_DEBUG
  35. #ifdef UFS_IALLOC_DEBUG
  36. #define UFSD(x) printk("(%s, %d), %s: ", __FILE__, __LINE__, __FUNCTION__); printk x;
  37. #else
  38. #define UFSD(x)
  39. #endif
  40. /*
  41. * NOTE! When we get the inode, we're the only people
  42. * that have access to it, and as such there are no
  43. * race conditions we have to worry about. The inode
  44. * is not on the hash-lists, and it cannot be reached
  45. * through the filesystem because the directory entry
  46. * has been deleted earlier.
  47. *
  48. * HOWEVER: we must make sure that we get no aliases,
  49. * which means that we have to call "clear_inode()"
  50. * _before_ we mark the inode not in use in the inode
  51. * bitmaps. Otherwise a newly created file might use
  52. * the same inode number (not actually the same pointer
  53. * though), and then we'd have two inodes sharing the
  54. * same inode number and space on the harddisk.
  55. */
  56. void ufs_free_inode (struct inode * inode)
  57. {
  58. struct super_block * sb;
  59. struct ufs_sb_private_info * uspi;
  60. struct ufs_super_block_first * usb1;
  61. struct ufs_cg_private_info * ucpi;
  62. struct ufs_cylinder_group * ucg;
  63. int is_directory;
  64. unsigned ino, cg, bit;
  65. UFSD(("ENTER, ino %lu\n", inode->i_ino))
  66. sb = inode->i_sb;
  67. uspi = UFS_SB(sb)->s_uspi;
  68. usb1 = ubh_get_usb_first(USPI_UBH);
  69. ino = inode->i_ino;
  70. lock_super (sb);
  71. if (!((ino > 1) && (ino < (uspi->s_ncg * uspi->s_ipg )))) {
  72. ufs_warning(sb, "ufs_free_inode", "reserved inode or nonexistent inode %u\n", ino);
  73. unlock_super (sb);
  74. return;
  75. }
  76. cg = ufs_inotocg (ino);
  77. bit = ufs_inotocgoff (ino);
  78. ucpi = ufs_load_cylinder (sb, cg);
  79. if (!ucpi) {
  80. unlock_super (sb);
  81. return;
  82. }
  83. ucg = ubh_get_ucg(UCPI_UBH);
  84. if (!ufs_cg_chkmagic(sb, ucg))
  85. ufs_panic (sb, "ufs_free_fragments", "internal error, bad cg magic number");
  86. ucg->cg_time = cpu_to_fs32(sb, get_seconds());
  87. is_directory = S_ISDIR(inode->i_mode);
  88. DQUOT_FREE_INODE(inode);
  89. DQUOT_DROP(inode);
  90. clear_inode (inode);
  91. if (ubh_isclr (UCPI_UBH, ucpi->c_iusedoff, bit))
  92. ufs_error(sb, "ufs_free_inode", "bit already cleared for inode %u", ino);
  93. else {
  94. ubh_clrbit (UCPI_UBH, ucpi->c_iusedoff, bit);
  95. if (ino < ucpi->c_irotor)
  96. ucpi->c_irotor = ino;
  97. fs32_add(sb, &ucg->cg_cs.cs_nifree, 1);
  98. fs32_add(sb, &usb1->fs_cstotal.cs_nifree, 1);
  99. fs32_add(sb, &UFS_SB(sb)->fs_cs(cg).cs_nifree, 1);
  100. if (is_directory) {
  101. fs32_sub(sb, &ucg->cg_cs.cs_ndir, 1);
  102. fs32_sub(sb, &usb1->fs_cstotal.cs_ndir, 1);
  103. fs32_sub(sb, &UFS_SB(sb)->fs_cs(cg).cs_ndir, 1);
  104. }
  105. }
  106. ubh_mark_buffer_dirty (USPI_UBH);
  107. ubh_mark_buffer_dirty (UCPI_UBH);
  108. if (sb->s_flags & MS_SYNCHRONOUS) {
  109. ubh_wait_on_buffer (UCPI_UBH);
  110. ubh_ll_rw_block (WRITE, 1, (struct ufs_buffer_head **) &ucpi);
  111. ubh_wait_on_buffer (UCPI_UBH);
  112. }
  113. sb->s_dirt = 1;
  114. unlock_super (sb);
  115. UFSD(("EXIT\n"))
  116. }
  117. /*
  118. * There are two policies for allocating an inode. If the new inode is
  119. * a directory, then a forward search is made for a block group with both
  120. * free space and a low directory-to-inode ratio; if that fails, then of
  121. * the groups with above-average free space, that group with the fewest
  122. * directories already is chosen.
  123. *
  124. * For other inodes, search forward from the parent directory's block
  125. * group to find a free inode.
  126. */
  127. struct inode * ufs_new_inode(struct inode * dir, int mode)
  128. {
  129. struct super_block * sb;
  130. struct ufs_sb_info * sbi;
  131. struct ufs_sb_private_info * uspi;
  132. struct ufs_super_block_first * usb1;
  133. struct ufs_cg_private_info * ucpi;
  134. struct ufs_cylinder_group * ucg;
  135. struct inode * inode;
  136. unsigned cg, bit, i, j, start;
  137. struct ufs_inode_info *ufsi;
  138. UFSD(("ENTER\n"))
  139. /* Cannot create files in a deleted directory */
  140. if (!dir || !dir->i_nlink)
  141. return ERR_PTR(-EPERM);
  142. sb = dir->i_sb;
  143. inode = new_inode(sb);
  144. if (!inode)
  145. return ERR_PTR(-ENOMEM);
  146. ufsi = UFS_I(inode);
  147. sbi = UFS_SB(sb);
  148. uspi = sbi->s_uspi;
  149. usb1 = ubh_get_usb_first(USPI_UBH);
  150. lock_super (sb);
  151. /*
  152. * Try to place the inode in its parent directory
  153. */
  154. i = ufs_inotocg(dir->i_ino);
  155. if (sbi->fs_cs(i).cs_nifree) {
  156. cg = i;
  157. goto cg_found;
  158. }
  159. /*
  160. * Use a quadratic hash to find a group with a free inode
  161. */
  162. for ( j = 1; j < uspi->s_ncg; j <<= 1 ) {
  163. i += j;
  164. if (i >= uspi->s_ncg)
  165. i -= uspi->s_ncg;
  166. if (sbi->fs_cs(i).cs_nifree) {
  167. cg = i;
  168. goto cg_found;
  169. }
  170. }
  171. /*
  172. * That failed: try linear search for a free inode
  173. */
  174. i = ufs_inotocg(dir->i_ino) + 1;
  175. for (j = 2; j < uspi->s_ncg; j++) {
  176. i++;
  177. if (i >= uspi->s_ncg)
  178. i = 0;
  179. if (sbi->fs_cs(i).cs_nifree) {
  180. cg = i;
  181. goto cg_found;
  182. }
  183. }
  184. goto failed;
  185. cg_found:
  186. ucpi = ufs_load_cylinder (sb, cg);
  187. if (!ucpi)
  188. goto failed;
  189. ucg = ubh_get_ucg(UCPI_UBH);
  190. if (!ufs_cg_chkmagic(sb, ucg))
  191. ufs_panic (sb, "ufs_new_inode", "internal error, bad cg magic number");
  192. start = ucpi->c_irotor;
  193. bit = ubh_find_next_zero_bit (UCPI_UBH, ucpi->c_iusedoff, uspi->s_ipg, start);
  194. if (!(bit < uspi->s_ipg)) {
  195. bit = ubh_find_first_zero_bit (UCPI_UBH, ucpi->c_iusedoff, start);
  196. if (!(bit < start)) {
  197. ufs_error (sb, "ufs_new_inode",
  198. "cylinder group %u corrupted - error in inode bitmap\n", cg);
  199. goto failed;
  200. }
  201. }
  202. UFSD(("start = %u, bit = %u, ipg = %u\n", start, bit, uspi->s_ipg))
  203. if (ubh_isclr (UCPI_UBH, ucpi->c_iusedoff, bit))
  204. ubh_setbit (UCPI_UBH, ucpi->c_iusedoff, bit);
  205. else {
  206. ufs_panic (sb, "ufs_new_inode", "internal error");
  207. goto failed;
  208. }
  209. fs32_sub(sb, &ucg->cg_cs.cs_nifree, 1);
  210. fs32_sub(sb, &usb1->fs_cstotal.cs_nifree, 1);
  211. fs32_sub(sb, &sbi->fs_cs(cg).cs_nifree, 1);
  212. if (S_ISDIR(mode)) {
  213. fs32_add(sb, &ucg->cg_cs.cs_ndir, 1);
  214. fs32_add(sb, &usb1->fs_cstotal.cs_ndir, 1);
  215. fs32_add(sb, &sbi->fs_cs(cg).cs_ndir, 1);
  216. }
  217. ubh_mark_buffer_dirty (USPI_UBH);
  218. ubh_mark_buffer_dirty (UCPI_UBH);
  219. if (sb->s_flags & MS_SYNCHRONOUS) {
  220. ubh_wait_on_buffer (UCPI_UBH);
  221. ubh_ll_rw_block (WRITE, 1, (struct ufs_buffer_head **) &ucpi);
  222. ubh_wait_on_buffer (UCPI_UBH);
  223. }
  224. sb->s_dirt = 1;
  225. inode->i_mode = mode;
  226. inode->i_uid = current->fsuid;
  227. if (dir->i_mode & S_ISGID) {
  228. inode->i_gid = dir->i_gid;
  229. if (S_ISDIR(mode))
  230. inode->i_mode |= S_ISGID;
  231. } else
  232. inode->i_gid = current->fsgid;
  233. inode->i_ino = cg * uspi->s_ipg + bit;
  234. inode->i_blksize = PAGE_SIZE; /* This is the optimal IO size (for stat), not the fs block size */
  235. inode->i_blocks = 0;
  236. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
  237. ufsi->i_flags = UFS_I(dir)->i_flags;
  238. ufsi->i_lastfrag = 0;
  239. ufsi->i_gen = 0;
  240. ufsi->i_shadow = 0;
  241. ufsi->i_osync = 0;
  242. ufsi->i_oeftflag = 0;
  243. memset(&ufsi->i_u1, 0, sizeof(ufsi->i_u1));
  244. insert_inode_hash(inode);
  245. mark_inode_dirty(inode);
  246. unlock_super (sb);
  247. if (DQUOT_ALLOC_INODE(inode)) {
  248. DQUOT_DROP(inode);
  249. inode->i_flags |= S_NOQUOTA;
  250. inode->i_nlink = 0;
  251. iput(inode);
  252. return ERR_PTR(-EDQUOT);
  253. }
  254. UFSD(("allocating inode %lu\n", inode->i_ino))
  255. UFSD(("EXIT\n"))
  256. return inode;
  257. failed:
  258. unlock_super (sb);
  259. make_bad_inode(inode);
  260. iput (inode);
  261. UFSD(("EXIT (FAILED)\n"))
  262. return ERR_PTR(-ENOSPC);
  263. }