ialloc.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. /*
  35. * NOTE! When we get the inode, we're the only people
  36. * that have access to it, and as such there are no
  37. * race conditions we have to worry about. The inode
  38. * is not on the hash-lists, and it cannot be reached
  39. * through the filesystem because the directory entry
  40. * has been deleted earlier.
  41. *
  42. * HOWEVER: we must make sure that we get no aliases,
  43. * which means that we have to call "clear_inode()"
  44. * _before_ we mark the inode not in use in the inode
  45. * bitmaps. Otherwise a newly created file might use
  46. * the same inode number (not actually the same pointer
  47. * though), and then we'd have two inodes sharing the
  48. * same inode number and space on the harddisk.
  49. */
  50. void ufs_free_inode (struct inode * inode)
  51. {
  52. struct super_block * sb;
  53. struct ufs_sb_private_info * uspi;
  54. struct ufs_super_block_first * usb1;
  55. struct ufs_cg_private_info * ucpi;
  56. struct ufs_cylinder_group * ucg;
  57. int is_directory;
  58. unsigned ino, cg, bit;
  59. UFSD("ENTER, ino %lu\n", inode->i_ino);
  60. sb = inode->i_sb;
  61. uspi = UFS_SB(sb)->s_uspi;
  62. usb1 = ubh_get_usb_first(uspi);
  63. ino = inode->i_ino;
  64. lock_super (sb);
  65. if (!((ino > 1) && (ino < (uspi->s_ncg * uspi->s_ipg )))) {
  66. ufs_warning(sb, "ufs_free_inode", "reserved inode or nonexistent inode %u\n", ino);
  67. unlock_super (sb);
  68. return;
  69. }
  70. cg = ufs_inotocg (ino);
  71. bit = ufs_inotocgoff (ino);
  72. ucpi = ufs_load_cylinder (sb, cg);
  73. if (!ucpi) {
  74. unlock_super (sb);
  75. return;
  76. }
  77. ucg = ubh_get_ucg(UCPI_UBH(ucpi));
  78. if (!ufs_cg_chkmagic(sb, ucg))
  79. ufs_panic (sb, "ufs_free_fragments", "internal error, bad cg magic number");
  80. ucg->cg_time = cpu_to_fs32(sb, get_seconds());
  81. is_directory = S_ISDIR(inode->i_mode);
  82. DQUOT_FREE_INODE(inode);
  83. DQUOT_DROP(inode);
  84. clear_inode (inode);
  85. if (ubh_isclr (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit))
  86. ufs_error(sb, "ufs_free_inode", "bit already cleared for inode %u", ino);
  87. else {
  88. ubh_clrbit (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit);
  89. if (ino < ucpi->c_irotor)
  90. ucpi->c_irotor = ino;
  91. fs32_add(sb, &ucg->cg_cs.cs_nifree, 1);
  92. uspi->cs_total.cs_nifree++;
  93. fs32_add(sb, &UFS_SB(sb)->fs_cs(cg).cs_nifree, 1);
  94. if (is_directory) {
  95. fs32_sub(sb, &ucg->cg_cs.cs_ndir, 1);
  96. uspi->cs_total.cs_ndir--;
  97. fs32_sub(sb, &UFS_SB(sb)->fs_cs(cg).cs_ndir, 1);
  98. }
  99. }
  100. ubh_mark_buffer_dirty (USPI_UBH(uspi));
  101. ubh_mark_buffer_dirty (UCPI_UBH(ucpi));
  102. if (sb->s_flags & MS_SYNCHRONOUS) {
  103. ubh_ll_rw_block(SWRITE, UCPI_UBH(ucpi));
  104. ubh_wait_on_buffer (UCPI_UBH(ucpi));
  105. }
  106. sb->s_dirt = 1;
  107. unlock_super (sb);
  108. UFSD("EXIT\n");
  109. }
  110. /*
  111. * There are two policies for allocating an inode. If the new inode is
  112. * a directory, then a forward search is made for a block group with both
  113. * free space and a low directory-to-inode ratio; if that fails, then of
  114. * the groups with above-average free space, that group with the fewest
  115. * directories already is chosen.
  116. *
  117. * For other inodes, search forward from the parent directory's block
  118. * group to find a free inode.
  119. */
  120. struct inode * ufs_new_inode(struct inode * dir, int mode)
  121. {
  122. struct super_block * sb;
  123. struct ufs_sb_info * sbi;
  124. struct ufs_sb_private_info * uspi;
  125. struct ufs_super_block_first * usb1;
  126. struct ufs_cg_private_info * ucpi;
  127. struct ufs_cylinder_group * ucg;
  128. struct inode * inode;
  129. unsigned cg, bit, i, j, start;
  130. struct ufs_inode_info *ufsi;
  131. UFSD("ENTER\n");
  132. /* Cannot create files in a deleted directory */
  133. if (!dir || !dir->i_nlink)
  134. return ERR_PTR(-EPERM);
  135. sb = dir->i_sb;
  136. inode = new_inode(sb);
  137. if (!inode)
  138. return ERR_PTR(-ENOMEM);
  139. ufsi = UFS_I(inode);
  140. sbi = UFS_SB(sb);
  141. uspi = sbi->s_uspi;
  142. usb1 = ubh_get_usb_first(uspi);
  143. lock_super (sb);
  144. /*
  145. * Try to place the inode in its parent directory
  146. */
  147. i = ufs_inotocg(dir->i_ino);
  148. if (sbi->fs_cs(i).cs_nifree) {
  149. cg = i;
  150. goto cg_found;
  151. }
  152. /*
  153. * Use a quadratic hash to find a group with a free inode
  154. */
  155. for ( j = 1; j < uspi->s_ncg; j <<= 1 ) {
  156. i += j;
  157. if (i >= uspi->s_ncg)
  158. i -= uspi->s_ncg;
  159. if (sbi->fs_cs(i).cs_nifree) {
  160. cg = i;
  161. goto cg_found;
  162. }
  163. }
  164. /*
  165. * That failed: try linear search for a free inode
  166. */
  167. i = ufs_inotocg(dir->i_ino) + 1;
  168. for (j = 2; j < uspi->s_ncg; j++) {
  169. i++;
  170. if (i >= uspi->s_ncg)
  171. i = 0;
  172. if (sbi->fs_cs(i).cs_nifree) {
  173. cg = i;
  174. goto cg_found;
  175. }
  176. }
  177. goto failed;
  178. cg_found:
  179. ucpi = ufs_load_cylinder (sb, cg);
  180. if (!ucpi)
  181. goto failed;
  182. ucg = ubh_get_ucg(UCPI_UBH(ucpi));
  183. if (!ufs_cg_chkmagic(sb, ucg))
  184. ufs_panic (sb, "ufs_new_inode", "internal error, bad cg magic number");
  185. start = ucpi->c_irotor;
  186. bit = ubh_find_next_zero_bit (UCPI_UBH(ucpi), ucpi->c_iusedoff, uspi->s_ipg, start);
  187. if (!(bit < uspi->s_ipg)) {
  188. bit = ubh_find_first_zero_bit (UCPI_UBH(ucpi), ucpi->c_iusedoff, start);
  189. if (!(bit < start)) {
  190. ufs_error (sb, "ufs_new_inode",
  191. "cylinder group %u corrupted - error in inode bitmap\n", cg);
  192. goto failed;
  193. }
  194. }
  195. UFSD("start = %u, bit = %u, ipg = %u\n", start, bit, uspi->s_ipg);
  196. if (ubh_isclr (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit))
  197. ubh_setbit (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit);
  198. else {
  199. ufs_panic (sb, "ufs_new_inode", "internal error");
  200. goto failed;
  201. }
  202. fs32_sub(sb, &ucg->cg_cs.cs_nifree, 1);
  203. uspi->cs_total.cs_nifree--;
  204. fs32_sub(sb, &sbi->fs_cs(cg).cs_nifree, 1);
  205. if (S_ISDIR(mode)) {
  206. fs32_add(sb, &ucg->cg_cs.cs_ndir, 1);
  207. uspi->cs_total.cs_ndir++;
  208. fs32_add(sb, &sbi->fs_cs(cg).cs_ndir, 1);
  209. }
  210. ubh_mark_buffer_dirty (USPI_UBH(uspi));
  211. ubh_mark_buffer_dirty (UCPI_UBH(ucpi));
  212. if (sb->s_flags & MS_SYNCHRONOUS) {
  213. ubh_ll_rw_block(SWRITE, UCPI_UBH(ucpi));
  214. ubh_wait_on_buffer (UCPI_UBH(ucpi));
  215. }
  216. sb->s_dirt = 1;
  217. inode->i_mode = mode;
  218. inode->i_uid = current->fsuid;
  219. if (dir->i_mode & S_ISGID) {
  220. inode->i_gid = dir->i_gid;
  221. if (S_ISDIR(mode))
  222. inode->i_mode |= S_ISGID;
  223. } else
  224. inode->i_gid = current->fsgid;
  225. inode->i_ino = cg * uspi->s_ipg + bit;
  226. inode->i_blocks = 0;
  227. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
  228. ufsi->i_flags = UFS_I(dir)->i_flags;
  229. ufsi->i_lastfrag = 0;
  230. ufsi->i_gen = 0;
  231. ufsi->i_shadow = 0;
  232. ufsi->i_osync = 0;
  233. ufsi->i_oeftflag = 0;
  234. ufsi->i_dir_start_lookup = 0;
  235. memset(&ufsi->i_u1, 0, sizeof(ufsi->i_u1));
  236. insert_inode_hash(inode);
  237. mark_inode_dirty(inode);
  238. unlock_super (sb);
  239. if (DQUOT_ALLOC_INODE(inode)) {
  240. DQUOT_DROP(inode);
  241. inode->i_flags |= S_NOQUOTA;
  242. inode->i_nlink = 0;
  243. iput(inode);
  244. return ERR_PTR(-EDQUOT);
  245. }
  246. UFSD("allocating inode %lu\n", inode->i_ino);
  247. UFSD("EXIT\n");
  248. return inode;
  249. failed:
  250. unlock_super (sb);
  251. make_bad_inode(inode);
  252. iput (inode);
  253. UFSD("EXIT (FAILED)\n");
  254. return ERR_PTR(-ENOSPC);
  255. }