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>
37 #undef UFS_IALLOC_DEBUG
39 #ifdef UFS_IALLOC_DEBUG
40 #define UFSD(x) printk("(%s, %d), %s: ", __FILE__, __LINE__, __FUNCTION__); printk x;
46 * NOTE! When we get the inode, we're the only people
47 * that have access to it, and as such there are no
48 * race conditions we have to worry about. The inode
49 * is not on the hash-lists, and it cannot be reached
50 * through the filesystem because the directory entry
51 * has been deleted earlier.
53 * HOWEVER: we must make sure that we get no aliases,
54 * which means that we have to call "clear_inode()"
55 * _before_ we mark the inode not in use in the inode
56 * bitmaps. Otherwise a newly created file might use
57 * the same inode number (not actually the same pointer
58 * though), and then we'd have two inodes sharing the
59 * same inode number and space on the harddisk.
61 void ufs_free_inode (struct inode * inode)
63 struct super_block * sb;
64 struct ufs_sb_private_info * uspi;
65 struct ufs_super_block_first * usb1;
66 struct ufs_cg_private_info * ucpi;
67 struct ufs_cylinder_group * ucg;
69 unsigned ino, cg, bit;
71 UFSD(("ENTER, ino %lu\n", inode->i_ino))
74 uspi = UFS_SB(sb)->s_uspi;
75 usb1 = ubh_get_usb_first(USPI_UBH);
81 if (!((ino > 1) && (ino < (uspi->s_ncg * uspi->s_ipg )))) {
82 ufs_warning(sb, "ufs_free_inode", "reserved inode or nonexistent inode %u\n", ino);
87 cg = ufs_inotocg (ino);
88 bit = ufs_inotocgoff (ino);
89 ucpi = ufs_load_cylinder (sb, cg);
94 ucg = ubh_get_ucg(UCPI_UBH);
95 if (!ufs_cg_chkmagic(sb, ucg))
96 ufs_panic (sb, "ufs_free_fragments", "internal error, bad cg magic number");
98 ucg->cg_time = cpu_to_fs32(sb, get_seconds());
100 is_directory = S_ISDIR(inode->i_mode);
102 DQUOT_FREE_INODE(inode);
107 if (ubh_isclr (UCPI_UBH, ucpi->c_iusedoff, bit))
108 ufs_error(sb, "ufs_free_inode", "bit already cleared for inode %u", ino);
110 ubh_clrbit (UCPI_UBH, ucpi->c_iusedoff, bit);
111 if (ino < ucpi->c_irotor)
112 ucpi->c_irotor = ino;
113 fs32_add(sb, &ucg->cg_cs.cs_nifree, 1);
114 fs32_add(sb, &usb1->fs_cstotal.cs_nifree, 1);
115 fs32_add(sb, &UFS_SB(sb)->fs_cs(cg).cs_nifree, 1);
118 fs32_sub(sb, &ucg->cg_cs.cs_ndir, 1);
119 fs32_sub(sb, &usb1->fs_cstotal.cs_ndir, 1);
120 fs32_sub(sb, &UFS_SB(sb)->fs_cs(cg).cs_ndir, 1);
124 ubh_mark_buffer_dirty (USPI_UBH);
125 ubh_mark_buffer_dirty (UCPI_UBH);
126 if (sb->s_flags & MS_SYNCHRONOUS) {
127 ubh_wait_on_buffer (UCPI_UBH);
128 ubh_ll_rw_block (WRITE, 1, (struct ufs_buffer_head **) &ucpi);
129 ubh_wait_on_buffer (UCPI_UBH);
138 * There are two policies for allocating an inode. If the new inode is
139 * a directory, then a forward search is made for a block group with both
140 * free space and a low directory-to-inode ratio; if that fails, then of
141 * the groups with above-average free space, that group with the fewest
142 * directories already is chosen.
144 * For other inodes, search forward from the parent directory's block
145 * group to find a free inode.
147 struct inode * ufs_new_inode(struct inode * dir, int mode)
149 struct super_block * sb;
150 struct ufs_sb_info * sbi;
151 struct ufs_sb_private_info * uspi;
152 struct ufs_super_block_first * usb1;
153 struct ufs_cg_private_info * ucpi;
154 struct ufs_cylinder_group * ucg;
155 struct inode * inode;
156 unsigned cg, bit, i, j, start;
157 struct ufs_inode_info *ufsi;
161 /* Cannot create files in a deleted directory */
162 if (!dir || !dir->i_nlink)
163 return ERR_PTR(-EPERM);
165 inode = new_inode(sb);
167 return ERR_PTR(-ENOMEM);
171 usb1 = ubh_get_usb_first(USPI_UBH);
176 * Try to place the inode in its parent directory
178 i = ufs_inotocg(dir->i_ino);
179 if (sbi->fs_cs(i).cs_nifree) {
185 * Use a quadratic hash to find a group with a free inode
187 for ( j = 1; j < uspi->s_ncg; j <<= 1 ) {
189 if (i >= uspi->s_ncg)
191 if (sbi->fs_cs(i).cs_nifree) {
198 * That failed: try linear search for a free inode
200 i = ufs_inotocg(dir->i_ino) + 1;
201 for (j = 2; j < uspi->s_ncg; j++) {
203 if (i >= uspi->s_ncg)
205 if (sbi->fs_cs(i).cs_nifree) {
214 ucpi = ufs_load_cylinder (sb, cg);
217 ucg = ubh_get_ucg(UCPI_UBH);
218 if (!ufs_cg_chkmagic(sb, ucg))
219 ufs_panic (sb, "ufs_new_inode", "internal error, bad cg magic number");
221 start = ucpi->c_irotor;
222 bit = ubh_find_next_zero_bit (UCPI_UBH, ucpi->c_iusedoff, uspi->s_ipg, start);
223 if (!(bit < uspi->s_ipg)) {
224 bit = ubh_find_first_zero_bit (UCPI_UBH, ucpi->c_iusedoff, start);
225 if (!(bit < start)) {
226 ufs_error (sb, "ufs_new_inode",
227 "cylinder group %u corrupted - error in inode bitmap\n", cg);
231 UFSD(("start = %u, bit = %u, ipg = %u\n", start, bit, uspi->s_ipg))
232 if (ubh_isclr (UCPI_UBH, ucpi->c_iusedoff, bit))
233 ubh_setbit (UCPI_UBH, ucpi->c_iusedoff, bit);
235 ufs_panic (sb, "ufs_new_inode", "internal error");
239 fs32_sub(sb, &ucg->cg_cs.cs_nifree, 1);
240 fs32_sub(sb, &usb1->fs_cstotal.cs_nifree, 1);
241 fs32_sub(sb, &sbi->fs_cs(cg).cs_nifree, 1);
244 fs32_add(sb, &ucg->cg_cs.cs_ndir, 1);
245 fs32_add(sb, &usb1->fs_cstotal.cs_ndir, 1);
246 fs32_add(sb, &sbi->fs_cs(cg).cs_ndir, 1);
249 ubh_mark_buffer_dirty (USPI_UBH);
250 ubh_mark_buffer_dirty (UCPI_UBH);
251 if (sb->s_flags & MS_SYNCHRONOUS) {
252 ubh_wait_on_buffer (UCPI_UBH);
253 ubh_ll_rw_block (WRITE, 1, (struct ufs_buffer_head **) &ucpi);
254 ubh_wait_on_buffer (UCPI_UBH);
258 inode->i_mode = mode;
259 inode->i_uid = current->fsuid;
260 if (dir->i_mode & S_ISGID) {
261 inode->i_gid = dir->i_gid;
263 inode->i_mode |= S_ISGID;
265 inode->i_gid = current->fsgid;
267 inode->i_ino = cg * uspi->s_ipg + bit;
268 inode->i_blksize = PAGE_SIZE; /* This is the optimal IO size (for stat), not the fs block size */
270 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
271 ufsi->i_flags = UFS_I(dir)->i_flags;
272 ufsi->i_lastfrag = 0;
276 ufsi->i_oeftflag = 0;
277 memset(&ufsi->i_u1, 0, sizeof(ufsi->i_u1));
279 insert_inode_hash(inode);
280 mark_inode_dirty(inode);
284 if (DQUOT_ALLOC_INODE(inode)) {
286 inode->i_flags |= S_NOQUOTA;
289 return ERR_PTR(-EDQUOT);
292 UFSD(("allocating inode %lu\n", inode->i_ino))
298 make_bad_inode(inode);
300 UFSD(("EXIT (FAILED)\n"))
301 return ERR_PTR(-ENOSPC);