ialloc.c 9.7 KB

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