ialloc.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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);
  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_ll_rw_block (SWRITE, 1, (struct ufs_buffer_head **) &ucpi);
  110. ubh_wait_on_buffer (UCPI_UBH);
  111. }
  112. sb->s_dirt = 1;
  113. unlock_super (sb);
  114. UFSD(("EXIT\n"))
  115. }
  116. /*
  117. * There are two policies for allocating an inode. If the new inode is
  118. * a directory, then a forward search is made for a block group with both
  119. * free space and a low directory-to-inode ratio; if that fails, then of
  120. * the groups with above-average free space, that group with the fewest
  121. * directories already is chosen.
  122. *
  123. * For other inodes, search forward from the parent directory's block
  124. * group to find a free inode.
  125. */
  126. struct inode * ufs_new_inode(struct inode * dir, int mode)
  127. {
  128. struct super_block * sb;
  129. struct ufs_sb_info * sbi;
  130. struct ufs_sb_private_info * uspi;
  131. struct ufs_super_block_first * usb1;
  132. struct ufs_cg_private_info * ucpi;
  133. struct ufs_cylinder_group * ucg;
  134. struct inode * inode;
  135. unsigned cg, bit, i, j, start;
  136. struct ufs_inode_info *ufsi;
  137. UFSD(("ENTER\n"))
  138. /* Cannot create files in a deleted directory */
  139. if (!dir || !dir->i_nlink)
  140. return ERR_PTR(-EPERM);
  141. sb = dir->i_sb;
  142. inode = new_inode(sb);
  143. if (!inode)
  144. return ERR_PTR(-ENOMEM);
  145. ufsi = UFS_I(inode);
  146. sbi = UFS_SB(sb);
  147. uspi = sbi->s_uspi;
  148. usb1 = ubh_get_usb_first(uspi);
  149. lock_super (sb);
  150. /*
  151. * Try to place the inode in its parent directory
  152. */
  153. i = ufs_inotocg(dir->i_ino);
  154. if (sbi->fs_cs(i).cs_nifree) {
  155. cg = i;
  156. goto cg_found;
  157. }
  158. /*
  159. * Use a quadratic hash to find a group with a free inode
  160. */
  161. for ( j = 1; j < uspi->s_ncg; j <<= 1 ) {
  162. i += j;
  163. if (i >= uspi->s_ncg)
  164. i -= uspi->s_ncg;
  165. if (sbi->fs_cs(i).cs_nifree) {
  166. cg = i;
  167. goto cg_found;
  168. }
  169. }
  170. /*
  171. * That failed: try linear search for a free inode
  172. */
  173. i = ufs_inotocg(dir->i_ino) + 1;
  174. for (j = 2; j < uspi->s_ncg; j++) {
  175. i++;
  176. if (i >= uspi->s_ncg)
  177. i = 0;
  178. if (sbi->fs_cs(i).cs_nifree) {
  179. cg = i;
  180. goto cg_found;
  181. }
  182. }
  183. goto failed;
  184. cg_found:
  185. ucpi = ufs_load_cylinder (sb, cg);
  186. if (!ucpi)
  187. goto failed;
  188. ucg = ubh_get_ucg(UCPI_UBH);
  189. if (!ufs_cg_chkmagic(sb, ucg))
  190. ufs_panic (sb, "ufs_new_inode", "internal error, bad cg magic number");
  191. start = ucpi->c_irotor;
  192. bit = ubh_find_next_zero_bit (UCPI_UBH, ucpi->c_iusedoff, uspi->s_ipg, start);
  193. if (!(bit < uspi->s_ipg)) {
  194. bit = ubh_find_first_zero_bit (UCPI_UBH, ucpi->c_iusedoff, start);
  195. if (!(bit < start)) {
  196. ufs_error (sb, "ufs_new_inode",
  197. "cylinder group %u corrupted - error in inode bitmap\n", cg);
  198. goto failed;
  199. }
  200. }
  201. UFSD(("start = %u, bit = %u, ipg = %u\n", start, bit, uspi->s_ipg))
  202. if (ubh_isclr (UCPI_UBH, ucpi->c_iusedoff, bit))
  203. ubh_setbit (UCPI_UBH, ucpi->c_iusedoff, bit);
  204. else {
  205. ufs_panic (sb, "ufs_new_inode", "internal error");
  206. goto failed;
  207. }
  208. fs32_sub(sb, &ucg->cg_cs.cs_nifree, 1);
  209. fs32_sub(sb, &usb1->fs_cstotal.cs_nifree, 1);
  210. fs32_sub(sb, &sbi->fs_cs(cg).cs_nifree, 1);
  211. if (S_ISDIR(mode)) {
  212. fs32_add(sb, &ucg->cg_cs.cs_ndir, 1);
  213. fs32_add(sb, &usb1->fs_cstotal.cs_ndir, 1);
  214. fs32_add(sb, &sbi->fs_cs(cg).cs_ndir, 1);
  215. }
  216. ubh_mark_buffer_dirty (USPI_UBH);
  217. ubh_mark_buffer_dirty (UCPI_UBH);
  218. if (sb->s_flags & MS_SYNCHRONOUS) {
  219. ubh_ll_rw_block (SWRITE, 1, (struct ufs_buffer_head **) &ucpi);
  220. ubh_wait_on_buffer (UCPI_UBH);
  221. }
  222. sb->s_dirt = 1;
  223. inode->i_mode = mode;
  224. inode->i_uid = current->fsuid;
  225. if (dir->i_mode & S_ISGID) {
  226. inode->i_gid = dir->i_gid;
  227. if (S_ISDIR(mode))
  228. inode->i_mode |= S_ISGID;
  229. } else
  230. inode->i_gid = current->fsgid;
  231. inode->i_ino = cg * uspi->s_ipg + bit;
  232. inode->i_blksize = PAGE_SIZE; /* This is the optimal IO size (for stat), not the fs block size */
  233. inode->i_blocks = 0;
  234. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
  235. ufsi->i_flags = UFS_I(dir)->i_flags;
  236. ufsi->i_lastfrag = 0;
  237. ufsi->i_gen = 0;
  238. ufsi->i_shadow = 0;
  239. ufsi->i_osync = 0;
  240. ufsi->i_oeftflag = 0;
  241. memset(&ufsi->i_u1, 0, sizeof(ufsi->i_u1));
  242. insert_inode_hash(inode);
  243. mark_inode_dirty(inode);
  244. unlock_super (sb);
  245. if (DQUOT_ALLOC_INODE(inode)) {
  246. DQUOT_DROP(inode);
  247. inode->i_flags |= S_NOQUOTA;
  248. inode->i_nlink = 0;
  249. iput(inode);
  250. return ERR_PTR(-EDQUOT);
  251. }
  252. UFSD(("allocating inode %lu\n", inode->i_ino))
  253. UFSD(("EXIT\n"))
  254. return inode;
  255. failed:
  256. unlock_super (sb);
  257. make_bad_inode(inode);
  258. iput (inode);
  259. UFSD(("EXIT (FAILED)\n"))
  260. return ERR_PTR(-ENOSPC);
  261. }