2 * linux/fs/ufs/ialloc.c
5 * Daniel Pirkl <daniel.pirkl@email.cz>
6 * Charles University, Faculty of Mathematics and Physics
10 * linux/fs/ext2/ialloc.c
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)
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
24 #include <linux/ufs_fs.h>
25 #include <linux/time.h>
26 #include <linux/stat.h>
27 #include <linux/string.h>
28 #include <linux/quotaops.h>
29 #include <linux/buffer_head.h>
30 #include <linux/sched.h>
31 #include <linux/bitops.h>
32 #include <asm/byteorder.h>
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.
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.
53 void ufs_free_inode (struct inode * inode)
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;
61 unsigned ino, cg, bit;
63 UFSD("ENTER, ino %lu\n", inode->i_ino);
66 uspi = UFS_SB(sb)->s_uspi;
67 usb1 = ubh_get_usb_first(uspi);
73 if (!((ino > 1) && (ino < (uspi->s_ncg * uspi->s_ipg )))) {
74 ufs_warning(sb, "ufs_free_inode", "reserved inode or nonexistent inode %u\n", ino);
79 cg = ufs_inotocg (ino);
80 bit = ufs_inotocgoff (ino);
81 ucpi = ufs_load_cylinder (sb, cg);
86 ucg = ubh_get_ucg(UCPI_UBH(ucpi));
87 if (!ufs_cg_chkmagic(sb, ucg))
88 ufs_panic (sb, "ufs_free_fragments", "internal error, bad cg magic number");
90 ucg->cg_time = cpu_to_fs32(sb, get_seconds());
92 is_directory = S_ISDIR(inode->i_mode);
94 DQUOT_FREE_INODE(inode);
99 if (ubh_isclr (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit))
100 ufs_error(sb, "ufs_free_inode", "bit already cleared for inode %u", ino);
102 ubh_clrbit (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit);
103 if (ino < ucpi->c_irotor)
104 ucpi->c_irotor = ino;
105 fs32_add(sb, &ucg->cg_cs.cs_nifree, 1);
106 uspi->cs_total.cs_nifree++;
107 fs32_add(sb, &UFS_SB(sb)->fs_cs(cg).cs_nifree, 1);
110 fs32_sub(sb, &ucg->cg_cs.cs_ndir, 1);
111 uspi->cs_total.cs_ndir--;
112 fs32_sub(sb, &UFS_SB(sb)->fs_cs(cg).cs_ndir, 1);
116 ubh_mark_buffer_dirty (USPI_UBH(uspi));
117 ubh_mark_buffer_dirty (UCPI_UBH(ucpi));
118 if (sb->s_flags & MS_SYNCHRONOUS) {
119 ubh_ll_rw_block(SWRITE, UCPI_UBH(ucpi));
120 ubh_wait_on_buffer (UCPI_UBH(ucpi));
129 * There are two policies for allocating an inode. If the new inode is
130 * a directory, then a forward search is made for a block group with both
131 * free space and a low directory-to-inode ratio; if that fails, then of
132 * the groups with above-average free space, that group with the fewest
133 * directories already is chosen.
135 * For other inodes, search forward from the parent directory's block
136 * group to find a free inode.
138 struct inode * ufs_new_inode(struct inode * dir, int mode)
140 struct super_block * sb;
141 struct ufs_sb_info * sbi;
142 struct ufs_sb_private_info * uspi;
143 struct ufs_super_block_first * usb1;
144 struct ufs_cg_private_info * ucpi;
145 struct ufs_cylinder_group * ucg;
146 struct inode * inode;
147 unsigned cg, bit, i, j, start;
148 struct ufs_inode_info *ufsi;
152 /* Cannot create files in a deleted directory */
153 if (!dir || !dir->i_nlink)
154 return ERR_PTR(-EPERM);
156 inode = new_inode(sb);
158 return ERR_PTR(-ENOMEM);
162 usb1 = ubh_get_usb_first(uspi);
167 * Try to place the inode in its parent directory
169 i = ufs_inotocg(dir->i_ino);
170 if (sbi->fs_cs(i).cs_nifree) {
176 * Use a quadratic hash to find a group with a free inode
178 for ( j = 1; j < uspi->s_ncg; j <<= 1 ) {
180 if (i >= uspi->s_ncg)
182 if (sbi->fs_cs(i).cs_nifree) {
189 * That failed: try linear search for a free inode
191 i = ufs_inotocg(dir->i_ino) + 1;
192 for (j = 2; j < uspi->s_ncg; j++) {
194 if (i >= uspi->s_ncg)
196 if (sbi->fs_cs(i).cs_nifree) {
205 ucpi = ufs_load_cylinder (sb, cg);
208 ucg = ubh_get_ucg(UCPI_UBH(ucpi));
209 if (!ufs_cg_chkmagic(sb, ucg))
210 ufs_panic (sb, "ufs_new_inode", "internal error, bad cg magic number");
212 start = ucpi->c_irotor;
213 bit = ubh_find_next_zero_bit (UCPI_UBH(ucpi), ucpi->c_iusedoff, uspi->s_ipg, start);
214 if (!(bit < uspi->s_ipg)) {
215 bit = ubh_find_first_zero_bit (UCPI_UBH(ucpi), ucpi->c_iusedoff, start);
216 if (!(bit < start)) {
217 ufs_error (sb, "ufs_new_inode",
218 "cylinder group %u corrupted - error in inode bitmap\n", cg);
222 UFSD("start = %u, bit = %u, ipg = %u\n", start, bit, uspi->s_ipg);
223 if (ubh_isclr (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit))
224 ubh_setbit (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit);
226 ufs_panic (sb, "ufs_new_inode", "internal error");
230 fs32_sub(sb, &ucg->cg_cs.cs_nifree, 1);
231 uspi->cs_total.cs_nifree--;
232 fs32_sub(sb, &sbi->fs_cs(cg).cs_nifree, 1);
235 fs32_add(sb, &ucg->cg_cs.cs_ndir, 1);
236 uspi->cs_total.cs_ndir++;
237 fs32_add(sb, &sbi->fs_cs(cg).cs_ndir, 1);
240 ubh_mark_buffer_dirty (USPI_UBH(uspi));
241 ubh_mark_buffer_dirty (UCPI_UBH(ucpi));
242 if (sb->s_flags & MS_SYNCHRONOUS) {
243 ubh_ll_rw_block(SWRITE, UCPI_UBH(ucpi));
244 ubh_wait_on_buffer (UCPI_UBH(ucpi));
248 inode->i_mode = mode;
249 inode->i_uid = current->fsuid;
250 if (dir->i_mode & S_ISGID) {
251 inode->i_gid = dir->i_gid;
253 inode->i_mode |= S_ISGID;
255 inode->i_gid = current->fsgid;
257 inode->i_ino = cg * uspi->s_ipg + bit;
258 inode->i_blksize = PAGE_SIZE; /* This is the optimal IO size (for stat), not the fs block size */
260 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
261 ufsi->i_flags = UFS_I(dir)->i_flags;
262 ufsi->i_lastfrag = 0;
266 ufsi->i_oeftflag = 0;
267 ufsi->i_dir_start_lookup = 0;
268 memset(&ufsi->i_u1, 0, sizeof(ufsi->i_u1));
270 insert_inode_hash(inode);
271 mark_inode_dirty(inode);
275 if (DQUOT_ALLOC_INODE(inode)) {
277 inode->i_flags |= S_NOQUOTA;
280 return ERR_PTR(-EDQUOT);
283 UFSD("allocating inode %lu\n", inode->i_ino);
289 make_bad_inode(inode);
291 UFSD("EXIT (FAILED)\n");
292 return ERR_PTR(-ENOSPC);