xref: /linux/fs/ufs/ialloc.c (revision 14b42963f64b98ab61fa9723c03d71aa5ef4f862)
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 
23 #include <linux/fs.h>
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>
33 
34 #include "swab.h"
35 #include "util.h"
36 
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 
63 	UFSD("ENTER, ino %lu\n", inode->i_ino);
64 
65 	sb = inode->i_sb;
66 	uspi = UFS_SB(sb)->s_uspi;
67 	usb1 = ubh_get_usb_first(uspi);
68 
69 	ino = inode->i_ino;
70 
71 	lock_super (sb);
72 
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);
75 		unlock_super (sb);
76 		return;
77 	}
78 
79 	cg = ufs_inotocg (ino);
80 	bit = ufs_inotocgoff (ino);
81 	ucpi = ufs_load_cylinder (sb, cg);
82 	if (!ucpi) {
83 		unlock_super (sb);
84 		return;
85 	}
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");
89 
90 	ucg->cg_time = cpu_to_fs32(sb, get_seconds());
91 
92 	is_directory = S_ISDIR(inode->i_mode);
93 
94 	DQUOT_FREE_INODE(inode);
95 	DQUOT_DROP(inode);
96 
97 	clear_inode (inode);
98 
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);
101 	else {
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);
108 
109 		if (is_directory) {
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);
113 		}
114 	}
115 
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));
121 	}
122 
123 	sb->s_dirt = 1;
124 	unlock_super (sb);
125 	UFSD("EXIT\n");
126 }
127 
128 /*
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.
134  *
135  * For other inodes, search forward from the parent directory's block
136  * group to find a free inode.
137  */
138 struct inode * ufs_new_inode(struct inode * dir, int mode)
139 {
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;
149 
150 	UFSD("ENTER\n");
151 
152 	/* Cannot create files in a deleted directory */
153 	if (!dir || !dir->i_nlink)
154 		return ERR_PTR(-EPERM);
155 	sb = dir->i_sb;
156 	inode = new_inode(sb);
157 	if (!inode)
158 		return ERR_PTR(-ENOMEM);
159 	ufsi = UFS_I(inode);
160 	sbi = UFS_SB(sb);
161 	uspi = sbi->s_uspi;
162 	usb1 = ubh_get_usb_first(uspi);
163 
164 	lock_super (sb);
165 
166 	/*
167 	 * Try to place the inode in its parent directory
168 	 */
169 	i = ufs_inotocg(dir->i_ino);
170 	if (sbi->fs_cs(i).cs_nifree) {
171 		cg = i;
172 		goto cg_found;
173 	}
174 
175 	/*
176 	 * Use a quadratic hash to find a group with a free inode
177 	 */
178 	for ( j = 1; j < uspi->s_ncg; j <<= 1 ) {
179 		i += j;
180 		if (i >= uspi->s_ncg)
181 			i -= uspi->s_ncg;
182 		if (sbi->fs_cs(i).cs_nifree) {
183 			cg = i;
184 			goto cg_found;
185 		}
186 	}
187 
188 	/*
189 	 * That failed: try linear search for a free inode
190 	 */
191 	i = ufs_inotocg(dir->i_ino) + 1;
192 	for (j = 2; j < uspi->s_ncg; j++) {
193 		i++;
194 		if (i >= uspi->s_ncg)
195 			i = 0;
196 		if (sbi->fs_cs(i).cs_nifree) {
197 			cg = i;
198 			goto cg_found;
199 		}
200 	}
201 
202 	goto failed;
203 
204 cg_found:
205 	ucpi = ufs_load_cylinder (sb, cg);
206 	if (!ucpi)
207 		goto failed;
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");
211 
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);
219 			goto failed;
220 		}
221 	}
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);
225 	else {
226 		ufs_panic (sb, "ufs_new_inode", "internal error");
227 		goto failed;
228 	}
229 
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);
233 
234 	if (S_ISDIR(mode)) {
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);
238 	}
239 
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));
245 	}
246 	sb->s_dirt = 1;
247 
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;
252 		if (S_ISDIR(mode))
253 			inode->i_mode |= S_ISGID;
254 	} else
255 		inode->i_gid = current->fsgid;
256 
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 */
259 	inode->i_blocks = 0;
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;
263 	ufsi->i_gen = 0;
264 	ufsi->i_shadow = 0;
265 	ufsi->i_osync = 0;
266 	ufsi->i_oeftflag = 0;
267 	ufsi->i_dir_start_lookup = 0;
268 	memset(&ufsi->i_u1, 0, sizeof(ufsi->i_u1));
269 
270 	insert_inode_hash(inode);
271 	mark_inode_dirty(inode);
272 
273 	unlock_super (sb);
274 
275 	if (DQUOT_ALLOC_INODE(inode)) {
276 		DQUOT_DROP(inode);
277 		inode->i_flags |= S_NOQUOTA;
278 		inode->i_nlink = 0;
279 		iput(inode);
280 		return ERR_PTR(-EDQUOT);
281 	}
282 
283 	UFSD("allocating inode %lu\n", inode->i_ino);
284 	UFSD("EXIT\n");
285 	return inode;
286 
287 failed:
288 	unlock_super (sb);
289 	make_bad_inode(inode);
290 	iput (inode);
291 	UFSD("EXIT (FAILED)\n");
292 	return ERR_PTR(-ENOSPC);
293 }
294