ialloc.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. * UFS2 write support added by
  23. * Evgeniy Dushistov <dushistov@mail.ru>, 2007
  24. */
  25. #include <linux/fs.h>
  26. #include <linux/time.h>
  27. #include <linux/stat.h>
  28. #include <linux/string.h>
  29. #include <linux/buffer_head.h>
  30. #include <linux/sched.h>
  31. #include <linux/bitops.h>
  32. #include <asm/byteorder.h>
  33. #include "ufs_fs.h"
  34. #include "ufs.h"
  35. #include "swab.h"
  36. #include "util.h"
  37. /*
  38. * NOTE! When we get the inode, we're the only people
  39. * that have access to it, and as such there are no
  40. * race conditions we have to worry about. The inode
  41. * is not on the hash-lists, and it cannot be reached
  42. * through the filesystem because the directory entry
  43. * has been deleted earlier.
  44. *
  45. * HOWEVER: we must make sure that we get no aliases,
  46. * which means that we have to call "clear_inode()"
  47. * _before_ we mark the inode not in use in the inode
  48. * bitmaps. Otherwise a newly created file might use
  49. * the same inode number (not actually the same pointer
  50. * though), and then we'd have two inodes sharing the
  51. * same inode number and space on the harddisk.
  52. */
  53. void ufs_free_inode (struct inode * inode)
  54. {
  55. struct super_block * sb;
  56. struct ufs_sb_private_info * uspi;
  57. struct ufs_cg_private_info * ucpi;
  58. struct ufs_cylinder_group * ucg;
  59. int is_directory;
  60. unsigned ino, cg, bit;
  61. UFSD("ENTER, ino %lu\n", inode->i_ino);
  62. sb = inode->i_sb;
  63. uspi = UFS_SB(sb)->s_uspi;
  64. ino = inode->i_ino;
  65. mutex_lock(&UFS_SB(sb)->s_lock);
  66. if (!((ino > 1) && (ino < (uspi->s_ncg * uspi->s_ipg )))) {
  67. ufs_warning(sb, "ufs_free_inode", "reserved inode or nonexistent inode %u\n", ino);
  68. mutex_unlock(&UFS_SB(sb)->s_lock);
  69. return;
  70. }
  71. cg = ufs_inotocg (ino);
  72. bit = ufs_inotocgoff (ino);
  73. ucpi = ufs_load_cylinder (sb, cg);
  74. if (!ucpi) {
  75. mutex_unlock(&UFS_SB(sb)->s_lock);
  76. return;
  77. }
  78. ucg = ubh_get_ucg(UCPI_UBH(ucpi));
  79. if (!ufs_cg_chkmagic(sb, ucg))
  80. ufs_panic (sb, "ufs_free_fragments", "internal error, bad cg magic number");
  81. ucg->cg_time = cpu_to_fs32(sb, get_seconds());
  82. is_directory = S_ISDIR(inode->i_mode);
  83. if (ubh_isclr (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit))
  84. ufs_error(sb, "ufs_free_inode", "bit already cleared for inode %u", ino);
  85. else {
  86. ubh_clrbit (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit);
  87. if (ino < ucpi->c_irotor)
  88. ucpi->c_irotor = ino;
  89. fs32_add(sb, &ucg->cg_cs.cs_nifree, 1);
  90. uspi->cs_total.cs_nifree++;
  91. fs32_add(sb, &UFS_SB(sb)->fs_cs(cg).cs_nifree, 1);
  92. if (is_directory) {
  93. fs32_sub(sb, &ucg->cg_cs.cs_ndir, 1);
  94. uspi->cs_total.cs_ndir--;
  95. fs32_sub(sb, &UFS_SB(sb)->fs_cs(cg).cs_ndir, 1);
  96. }
  97. }
  98. ubh_mark_buffer_dirty (USPI_UBH(uspi));
  99. ubh_mark_buffer_dirty (UCPI_UBH(ucpi));
  100. if (sb->s_flags & MS_SYNCHRONOUS)
  101. ubh_sync_block(UCPI_UBH(ucpi));
  102. ufs_mark_sb_dirty(sb);
  103. mutex_unlock(&UFS_SB(sb)->s_lock);
  104. UFSD("EXIT\n");
  105. }
  106. /*
  107. * Nullify new chunk of inodes,
  108. * BSD people also set ui_gen field of inode
  109. * during nullification, but we not care about
  110. * that because of linux ufs do not support NFS
  111. */
  112. static void ufs2_init_inodes_chunk(struct super_block *sb,
  113. struct ufs_cg_private_info *ucpi,
  114. struct ufs_cylinder_group *ucg)
  115. {
  116. struct buffer_head *bh;
  117. struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
  118. sector_t beg = uspi->s_sbbase +
  119. ufs_inotofsba(ucpi->c_cgx * uspi->s_ipg +
  120. fs32_to_cpu(sb, ucg->cg_u.cg_u2.cg_initediblk));
  121. sector_t end = beg + uspi->s_fpb;
  122. UFSD("ENTER cgno %d\n", ucpi->c_cgx);
  123. for (; beg < end; ++beg) {
  124. bh = sb_getblk(sb, beg);
  125. lock_buffer(bh);
  126. memset(bh->b_data, 0, sb->s_blocksize);
  127. set_buffer_uptodate(bh);
  128. mark_buffer_dirty(bh);
  129. unlock_buffer(bh);
  130. if (sb->s_flags & MS_SYNCHRONOUS)
  131. sync_dirty_buffer(bh);
  132. brelse(bh);
  133. }
  134. fs32_add(sb, &ucg->cg_u.cg_u2.cg_initediblk, uspi->s_inopb);
  135. ubh_mark_buffer_dirty(UCPI_UBH(ucpi));
  136. if (sb->s_flags & MS_SYNCHRONOUS)
  137. ubh_sync_block(UCPI_UBH(ucpi));
  138. UFSD("EXIT\n");
  139. }
  140. /*
  141. * There are two policies for allocating an inode. If the new inode is
  142. * a directory, then a forward search is made for a block group with both
  143. * free space and a low directory-to-inode ratio; if that fails, then of
  144. * the groups with above-average free space, that group with the fewest
  145. * directories already is chosen.
  146. *
  147. * For other inodes, search forward from the parent directory's block
  148. * group to find a free inode.
  149. */
  150. struct inode *ufs_new_inode(struct inode *dir, umode_t mode)
  151. {
  152. struct super_block * sb;
  153. struct ufs_sb_info * sbi;
  154. struct ufs_sb_private_info * uspi;
  155. struct ufs_cg_private_info * ucpi;
  156. struct ufs_cylinder_group * ucg;
  157. struct inode * inode;
  158. unsigned cg, bit, i, j, start;
  159. struct ufs_inode_info *ufsi;
  160. int err = -ENOSPC;
  161. UFSD("ENTER\n");
  162. /* Cannot create files in a deleted directory */
  163. if (!dir || !dir->i_nlink)
  164. return ERR_PTR(-EPERM);
  165. sb = dir->i_sb;
  166. inode = new_inode(sb);
  167. if (!inode)
  168. return ERR_PTR(-ENOMEM);
  169. ufsi = UFS_I(inode);
  170. sbi = UFS_SB(sb);
  171. uspi = sbi->s_uspi;
  172. mutex_lock(&sbi->s_lock);
  173. /*
  174. * Try to place the inode in its parent directory
  175. */
  176. i = ufs_inotocg(dir->i_ino);
  177. if (sbi->fs_cs(i).cs_nifree) {
  178. cg = i;
  179. goto cg_found;
  180. }
  181. /*
  182. * Use a quadratic hash to find a group with a free inode
  183. */
  184. for ( j = 1; j < uspi->s_ncg; j <<= 1 ) {
  185. i += j;
  186. if (i >= uspi->s_ncg)
  187. i -= uspi->s_ncg;
  188. if (sbi->fs_cs(i).cs_nifree) {
  189. cg = i;
  190. goto cg_found;
  191. }
  192. }
  193. /*
  194. * That failed: try linear search for a free inode
  195. */
  196. i = ufs_inotocg(dir->i_ino) + 1;
  197. for (j = 2; j < uspi->s_ncg; j++) {
  198. i++;
  199. if (i >= uspi->s_ncg)
  200. i = 0;
  201. if (sbi->fs_cs(i).cs_nifree) {
  202. cg = i;
  203. goto cg_found;
  204. }
  205. }
  206. goto failed;
  207. cg_found:
  208. ucpi = ufs_load_cylinder (sb, cg);
  209. if (!ucpi) {
  210. err = -EIO;
  211. goto failed;
  212. }
  213. ucg = ubh_get_ucg(UCPI_UBH(ucpi));
  214. if (!ufs_cg_chkmagic(sb, ucg))
  215. ufs_panic (sb, "ufs_new_inode", "internal error, bad cg magic number");
  216. start = ucpi->c_irotor;
  217. bit = ubh_find_next_zero_bit (UCPI_UBH(ucpi), ucpi->c_iusedoff, uspi->s_ipg, start);
  218. if (!(bit < uspi->s_ipg)) {
  219. bit = ubh_find_first_zero_bit (UCPI_UBH(ucpi), ucpi->c_iusedoff, start);
  220. if (!(bit < start)) {
  221. ufs_error (sb, "ufs_new_inode",
  222. "cylinder group %u corrupted - error in inode bitmap\n", cg);
  223. err = -EIO;
  224. goto failed;
  225. }
  226. }
  227. UFSD("start = %u, bit = %u, ipg = %u\n", start, bit, uspi->s_ipg);
  228. if (ubh_isclr (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit))
  229. ubh_setbit (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit);
  230. else {
  231. ufs_panic (sb, "ufs_new_inode", "internal error");
  232. err = -EIO;
  233. goto failed;
  234. }
  235. if (uspi->fs_magic == UFS2_MAGIC) {
  236. u32 initediblk = fs32_to_cpu(sb, ucg->cg_u.cg_u2.cg_initediblk);
  237. if (bit + uspi->s_inopb > initediblk &&
  238. initediblk < fs32_to_cpu(sb, ucg->cg_u.cg_u2.cg_niblk))
  239. ufs2_init_inodes_chunk(sb, ucpi, ucg);
  240. }
  241. fs32_sub(sb, &ucg->cg_cs.cs_nifree, 1);
  242. uspi->cs_total.cs_nifree--;
  243. fs32_sub(sb, &sbi->fs_cs(cg).cs_nifree, 1);
  244. if (S_ISDIR(mode)) {
  245. fs32_add(sb, &ucg->cg_cs.cs_ndir, 1);
  246. uspi->cs_total.cs_ndir++;
  247. fs32_add(sb, &sbi->fs_cs(cg).cs_ndir, 1);
  248. }
  249. ubh_mark_buffer_dirty (USPI_UBH(uspi));
  250. ubh_mark_buffer_dirty (UCPI_UBH(ucpi));
  251. if (sb->s_flags & MS_SYNCHRONOUS)
  252. ubh_sync_block(UCPI_UBH(ucpi));
  253. ufs_mark_sb_dirty(sb);
  254. inode->i_ino = cg * uspi->s_ipg + bit;
  255. inode_init_owner(inode, dir, mode);
  256. inode->i_blocks = 0;
  257. inode->i_generation = 0;
  258. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
  259. ufsi->i_flags = UFS_I(dir)->i_flags;
  260. ufsi->i_lastfrag = 0;
  261. ufsi->i_shadow = 0;
  262. ufsi->i_osync = 0;
  263. ufsi->i_oeftflag = 0;
  264. ufsi->i_dir_start_lookup = 0;
  265. memset(&ufsi->i_u1, 0, sizeof(ufsi->i_u1));
  266. if (insert_inode_locked(inode) < 0) {
  267. err = -EIO;
  268. goto failed;
  269. }
  270. mark_inode_dirty(inode);
  271. if (uspi->fs_magic == UFS2_MAGIC) {
  272. struct buffer_head *bh;
  273. struct ufs2_inode *ufs2_inode;
  274. /*
  275. * setup birth date, we do it here because of there is no sense
  276. * to hold it in struct ufs_inode_info, and lose 64 bit
  277. */
  278. bh = sb_bread(sb, uspi->s_sbbase + ufs_inotofsba(inode->i_ino));
  279. if (!bh) {
  280. ufs_warning(sb, "ufs_read_inode",
  281. "unable to read inode %lu\n",
  282. inode->i_ino);
  283. err = -EIO;
  284. goto fail_remove_inode;
  285. }
  286. lock_buffer(bh);
  287. ufs2_inode = (struct ufs2_inode *)bh->b_data;
  288. ufs2_inode += ufs_inotofsbo(inode->i_ino);
  289. ufs2_inode->ui_birthtime = cpu_to_fs64(sb, CURRENT_TIME.tv_sec);
  290. ufs2_inode->ui_birthnsec = cpu_to_fs32(sb, CURRENT_TIME.tv_nsec);
  291. mark_buffer_dirty(bh);
  292. unlock_buffer(bh);
  293. if (sb->s_flags & MS_SYNCHRONOUS)
  294. sync_dirty_buffer(bh);
  295. brelse(bh);
  296. }
  297. mutex_unlock(&sbi->s_lock);
  298. UFSD("allocating inode %lu\n", inode->i_ino);
  299. UFSD("EXIT\n");
  300. return inode;
  301. fail_remove_inode:
  302. mutex_unlock(&sbi->s_lock);
  303. clear_nlink(inode);
  304. unlock_new_inode(inode);
  305. iput(inode);
  306. UFSD("EXIT (FAILED): err %d\n", err);
  307. return ERR_PTR(err);
  308. failed:
  309. mutex_unlock(&sbi->s_lock);
  310. make_bad_inode(inode);
  311. iput (inode);
  312. UFSD("EXIT (FAILED): err %d\n", err);
  313. return ERR_PTR(err);
  314. }