ialloc.c 9.3 KB

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