ialloc.c 9.3 KB

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