ialloc.c 9.7 KB

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