ialloc.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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_super_block_first * usb1;
  58. struct ufs_cg_private_info * ucpi;
  59. struct ufs_cylinder_group * ucg;
  60. int is_directory;
  61. unsigned ino, cg, bit;
  62. UFSD("ENTER, ino %lu\n", inode->i_ino);
  63. sb = inode->i_sb;
  64. uspi = UFS_SB(sb)->s_uspi;
  65. usb1 = ubh_get_usb_first(uspi);
  66. ino = inode->i_ino;
  67. lock_super (sb);
  68. if (!((ino > 1) && (ino < (uspi->s_ncg * uspi->s_ipg )))) {
  69. ufs_warning(sb, "ufs_free_inode", "reserved inode or nonexistent inode %u\n", ino);
  70. unlock_super (sb);
  71. return;
  72. }
  73. cg = ufs_inotocg (ino);
  74. bit = ufs_inotocgoff (ino);
  75. ucpi = ufs_load_cylinder (sb, cg);
  76. if (!ucpi) {
  77. unlock_super (sb);
  78. return;
  79. }
  80. ucg = ubh_get_ucg(UCPI_UBH(ucpi));
  81. if (!ufs_cg_chkmagic(sb, ucg))
  82. ufs_panic (sb, "ufs_free_fragments", "internal error, bad cg magic number");
  83. ucg->cg_time = cpu_to_fs32(sb, get_seconds());
  84. is_directory = S_ISDIR(inode->i_mode);
  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_sync_block(UCPI_UBH(ucpi));
  104. sb->s_dirt = 1;
  105. unlock_super (sb);
  106. UFSD("EXIT\n");
  107. }
  108. /*
  109. * Nullify new chunk of inodes,
  110. * BSD people also set ui_gen field of inode
  111. * during nullification, but we not care about
  112. * that because of linux ufs do not support NFS
  113. */
  114. static void ufs2_init_inodes_chunk(struct super_block *sb,
  115. struct ufs_cg_private_info *ucpi,
  116. struct ufs_cylinder_group *ucg)
  117. {
  118. struct buffer_head *bh;
  119. struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
  120. sector_t beg = uspi->s_sbbase +
  121. ufs_inotofsba(ucpi->c_cgx * uspi->s_ipg +
  122. fs32_to_cpu(sb, ucg->cg_u.cg_u2.cg_initediblk));
  123. sector_t end = beg + uspi->s_fpb;
  124. UFSD("ENTER cgno %d\n", ucpi->c_cgx);
  125. for (; beg < end; ++beg) {
  126. bh = sb_getblk(sb, beg);
  127. lock_buffer(bh);
  128. memset(bh->b_data, 0, sb->s_blocksize);
  129. set_buffer_uptodate(bh);
  130. mark_buffer_dirty(bh);
  131. unlock_buffer(bh);
  132. if (sb->s_flags & MS_SYNCHRONOUS)
  133. sync_dirty_buffer(bh);
  134. brelse(bh);
  135. }
  136. fs32_add(sb, &ucg->cg_u.cg_u2.cg_initediblk, uspi->s_inopb);
  137. ubh_mark_buffer_dirty(UCPI_UBH(ucpi));
  138. if (sb->s_flags & MS_SYNCHRONOUS)
  139. ubh_sync_block(UCPI_UBH(ucpi));
  140. UFSD("EXIT\n");
  141. }
  142. /*
  143. * There are two policies for allocating an inode. If the new inode is
  144. * a directory, then a forward search is made for a block group with both
  145. * free space and a low directory-to-inode ratio; if that fails, then of
  146. * the groups with above-average free space, that group with the fewest
  147. * directories already is chosen.
  148. *
  149. * For other inodes, search forward from the parent directory's block
  150. * group to find a free inode.
  151. */
  152. struct inode * ufs_new_inode(struct inode * dir, int mode)
  153. {
  154. struct super_block * sb;
  155. struct ufs_sb_info * sbi;
  156. struct ufs_sb_private_info * uspi;
  157. struct ufs_super_block_first * usb1;
  158. struct ufs_cg_private_info * ucpi;
  159. struct ufs_cylinder_group * ucg;
  160. struct inode * inode;
  161. unsigned cg, bit, i, j, start;
  162. struct ufs_inode_info *ufsi;
  163. int err = -ENOSPC;
  164. UFSD("ENTER\n");
  165. /* Cannot create files in a deleted directory */
  166. if (!dir || !dir->i_nlink)
  167. return ERR_PTR(-EPERM);
  168. sb = dir->i_sb;
  169. inode = new_inode(sb);
  170. if (!inode)
  171. return ERR_PTR(-ENOMEM);
  172. ufsi = UFS_I(inode);
  173. sbi = UFS_SB(sb);
  174. uspi = sbi->s_uspi;
  175. usb1 = ubh_get_usb_first(uspi);
  176. lock_super (sb);
  177. /*
  178. * Try to place the inode in its parent directory
  179. */
  180. i = ufs_inotocg(dir->i_ino);
  181. if (sbi->fs_cs(i).cs_nifree) {
  182. cg = i;
  183. goto cg_found;
  184. }
  185. /*
  186. * Use a quadratic hash to find a group with a free inode
  187. */
  188. for ( j = 1; j < uspi->s_ncg; j <<= 1 ) {
  189. i += j;
  190. if (i >= uspi->s_ncg)
  191. i -= uspi->s_ncg;
  192. if (sbi->fs_cs(i).cs_nifree) {
  193. cg = i;
  194. goto cg_found;
  195. }
  196. }
  197. /*
  198. * That failed: try linear search for a free inode
  199. */
  200. i = ufs_inotocg(dir->i_ino) + 1;
  201. for (j = 2; j < uspi->s_ncg; j++) {
  202. i++;
  203. if (i >= uspi->s_ncg)
  204. i = 0;
  205. if (sbi->fs_cs(i).cs_nifree) {
  206. cg = i;
  207. goto cg_found;
  208. }
  209. }
  210. goto failed;
  211. cg_found:
  212. ucpi = ufs_load_cylinder (sb, cg);
  213. if (!ucpi) {
  214. err = -EIO;
  215. goto failed;
  216. }
  217. ucg = ubh_get_ucg(UCPI_UBH(ucpi));
  218. if (!ufs_cg_chkmagic(sb, ucg))
  219. ufs_panic (sb, "ufs_new_inode", "internal error, bad cg magic number");
  220. start = ucpi->c_irotor;
  221. bit = ubh_find_next_zero_bit (UCPI_UBH(ucpi), ucpi->c_iusedoff, uspi->s_ipg, start);
  222. if (!(bit < uspi->s_ipg)) {
  223. bit = ubh_find_first_zero_bit (UCPI_UBH(ucpi), ucpi->c_iusedoff, start);
  224. if (!(bit < start)) {
  225. ufs_error (sb, "ufs_new_inode",
  226. "cylinder group %u corrupted - error in inode bitmap\n", cg);
  227. err = -EIO;
  228. goto failed;
  229. }
  230. }
  231. UFSD("start = %u, bit = %u, ipg = %u\n", start, bit, uspi->s_ipg);
  232. if (ubh_isclr (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit))
  233. ubh_setbit (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit);
  234. else {
  235. ufs_panic (sb, "ufs_new_inode", "internal error");
  236. err = -EIO;
  237. goto failed;
  238. }
  239. if (uspi->fs_magic == UFS2_MAGIC) {
  240. u32 initediblk = fs32_to_cpu(sb, ucg->cg_u.cg_u2.cg_initediblk);
  241. if (bit + uspi->s_inopb > initediblk &&
  242. initediblk < fs32_to_cpu(sb, ucg->cg_u.cg_u2.cg_niblk))
  243. ufs2_init_inodes_chunk(sb, ucpi, ucg);
  244. }
  245. fs32_sub(sb, &ucg->cg_cs.cs_nifree, 1);
  246. uspi->cs_total.cs_nifree--;
  247. fs32_sub(sb, &sbi->fs_cs(cg).cs_nifree, 1);
  248. if (S_ISDIR(mode)) {
  249. fs32_add(sb, &ucg->cg_cs.cs_ndir, 1);
  250. uspi->cs_total.cs_ndir++;
  251. fs32_add(sb, &sbi->fs_cs(cg).cs_ndir, 1);
  252. }
  253. ubh_mark_buffer_dirty (USPI_UBH(uspi));
  254. ubh_mark_buffer_dirty (UCPI_UBH(ucpi));
  255. if (sb->s_flags & MS_SYNCHRONOUS)
  256. ubh_sync_block(UCPI_UBH(ucpi));
  257. sb->s_dirt = 1;
  258. inode->i_ino = cg * uspi->s_ipg + bit;
  259. inode_init_owner(inode, dir, mode);
  260. inode->i_blocks = 0;
  261. inode->i_generation = 0;
  262. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
  263. ufsi->i_flags = UFS_I(dir)->i_flags;
  264. ufsi->i_lastfrag = 0;
  265. ufsi->i_shadow = 0;
  266. ufsi->i_osync = 0;
  267. ufsi->i_oeftflag = 0;
  268. ufsi->i_dir_start_lookup = 0;
  269. memset(&ufsi->i_u1, 0, sizeof(ufsi->i_u1));
  270. insert_inode_hash(inode);
  271. mark_inode_dirty(inode);
  272. if (uspi->fs_magic == UFS2_MAGIC) {
  273. struct buffer_head *bh;
  274. struct ufs2_inode *ufs2_inode;
  275. /*
  276. * setup birth date, we do it here because of there is no sense
  277. * to hold it in struct ufs_inode_info, and lose 64 bit
  278. */
  279. bh = sb_bread(sb, uspi->s_sbbase + ufs_inotofsba(inode->i_ino));
  280. if (!bh) {
  281. ufs_warning(sb, "ufs_read_inode",
  282. "unable to read inode %lu\n",
  283. inode->i_ino);
  284. err = -EIO;
  285. goto fail_remove_inode;
  286. }
  287. lock_buffer(bh);
  288. ufs2_inode = (struct ufs2_inode *)bh->b_data;
  289. ufs2_inode += ufs_inotofsbo(inode->i_ino);
  290. ufs2_inode->ui_birthtime = cpu_to_fs64(sb, CURRENT_TIME.tv_sec);
  291. ufs2_inode->ui_birthnsec = cpu_to_fs32(sb, CURRENT_TIME.tv_nsec);
  292. mark_buffer_dirty(bh);
  293. unlock_buffer(bh);
  294. if (sb->s_flags & MS_SYNCHRONOUS)
  295. sync_dirty_buffer(bh);
  296. brelse(bh);
  297. }
  298. unlock_super (sb);
  299. UFSD("allocating inode %lu\n", inode->i_ino);
  300. UFSD("EXIT\n");
  301. return inode;
  302. fail_remove_inode:
  303. unlock_super(sb);
  304. inode->i_nlink = 0;
  305. iput(inode);
  306. UFSD("EXIT (FAILED): err %d\n", err);
  307. return ERR_PTR(err);
  308. failed:
  309. unlock_super (sb);
  310. make_bad_inode(inode);
  311. iput (inode);
  312. UFSD("EXIT (FAILED): err %d\n", err);
  313. return ERR_PTR(err);
  314. }