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/sched.h>
26 #include <linux/stat.h>
27 #include <linux/string.h>
28 #include <linux/locks.h>
29 #include <linux/quotaops.h>
30 #include <asm/bitops.h>
31 #include <asm/byteorder.h>
36 #undef UFS_IALLOC_DEBUG
38 #ifdef UFS_IALLOC_DEBUG
39 #define UFSD(x) printk("(%s, %d), %s: ", __FILE__, __LINE__, __FUNCTION__); printk x;
45 * NOTE! When we get the inode, we're the only people
46 * that have access to it, and as such there are no
47 * race conditions we have to worry about. The inode
48 * is not on the hash-lists, and it cannot be reached
49 * through the filesystem because the directory entry
50 * has been deleted earlier.
52 * HOWEVER: we must make sure that we get no aliases,
53 * which means that we have to call "clear_inode()"
54 * _before_ we mark the inode not in use in the inode
55 * bitmaps. Otherwise a newly created file might use
56 * the same inode number (not actually the same pointer
57 * though), and then we'd have two inodes sharing the
58 * same inode number and space on the harddisk.
60 void ufs_free_inode (struct inode
* inode
)
62 struct super_block
* sb
;
63 struct ufs_sb_private_info
* uspi
;
64 struct ufs_super_block_first
* usb1
;
65 struct ufs_cg_private_info
* ucpi
;
66 struct ufs_cylinder_group
* ucg
;
68 unsigned ino
, cg
, bit
;
71 UFSD(("ENTER, ino %lu\n", inode
->i_ino
))
76 swab
= sb
->u
.ufs_sb
.s_swab
;
77 uspi
= sb
->u
.ufs_sb
.s_uspi
;
78 usb1
= ubh_get_usb_first(USPI_UBH
);
80 if (inode
->i_count
> 1) {
81 ufs_warning(sb
, "ufs_free_inode", "inode has count=%d\n", inode
->i_count
);
85 ufs_warning(sb
, "ufs_free_inode", "inode has nlink=%d\n", inode
->i_nlink
);
93 if (!((ino
> 1) && (ino
< (uspi
->s_ncg
* uspi
->s_ipg
)))) {
94 ufs_warning(sb
, "ufs_free_inode", "reserved inode or nonexistent inode %u\n", ino
);
99 cg
= ufs_inotocg (ino
);
100 bit
= ufs_inotocgoff (ino
);
101 ucpi
= ufs_load_cylinder (sb
, cg
);
106 ucg
= ubh_get_ucg(UCPI_UBH
);
107 if (!ufs_cg_chkmagic(ucg
))
108 ufs_panic (sb
, "ufs_free_fragments", "internal error, bad cg magic number");
110 ucg
->cg_time
= SWAB32(CURRENT_TIME
);
112 is_directory
= S_ISDIR(inode
->i_mode
);
114 DQUOT_FREE_INODE(sb
, inode
);
118 if (ubh_isclr (UCPI_UBH
, ucpi
->c_iusedoff
, bit
))
119 ufs_error(sb
, "ufs_free_inode", "bit already cleared for inode %u", ino
);
121 ubh_clrbit (UCPI_UBH
, ucpi
->c_iusedoff
, bit
);
122 if (ino
< ucpi
->c_irotor
)
123 ucpi
->c_irotor
= ino
;
124 INC_SWAB32(ucg
->cg_cs
.cs_nifree
);
125 INC_SWAB32(usb1
->fs_cstotal
.cs_nifree
);
126 INC_SWAB32(sb
->fs_cs(cg
).cs_nifree
);
129 DEC_SWAB32(ucg
->cg_cs
.cs_ndir
);
130 DEC_SWAB32(usb1
->fs_cstotal
.cs_ndir
);
131 DEC_SWAB32(sb
->fs_cs(cg
).cs_ndir
);
134 ubh_mark_buffer_dirty (USPI_UBH
, 1);
135 ubh_mark_buffer_dirty (UCPI_UBH
, 1);
136 if (sb
->s_flags
& MS_SYNCHRONOUS
) {
137 ubh_ll_rw_block (WRITE
, 1, (struct ufs_buffer_head
**) &ucpi
);
138 ubh_wait_on_buffer (UCPI_UBH
);
147 * There are two policies for allocating an inode. If the new inode is
148 * a directory, then a forward search is made for a block group with both
149 * free space and a low directory-to-inode ratio; if that fails, then of
150 * the groups with above-average free space, that group with the fewest
151 * directories already is chosen.
153 * For other inodes, search forward from the parent directory's block
154 * group to find a free inode.
156 struct inode
* ufs_new_inode (const struct inode
* dir
, int mode
, int * err
)
158 struct super_block
* sb
;
159 struct ufs_sb_private_info
* uspi
;
160 struct ufs_super_block_first
* usb1
;
161 struct ufs_cg_private_info
* ucpi
;
162 struct ufs_cylinder_group
* ucg
;
163 struct inode
* inode
;
164 unsigned cg
, bit
, i
, j
, start
;
169 /* Cannot create files in a deleted directory */
170 if (!dir
|| !dir
->i_nlink
) {
174 inode
= get_empty_inode ();
180 swab
= sb
->u
.ufs_sb
.s_swab
;
181 uspi
= sb
->u
.ufs_sb
.s_uspi
;
182 usb1
= ubh_get_usb_first(USPI_UBH
);
192 * Try to place the inode in its parent directory
194 i
= ufs_inotocg(dir
->i_ino
);
195 if (SWAB32(sb
->fs_cs(i
).cs_nifree
)) {
201 * Use a quadratic hash to find a group with a free inode
203 for ( j
= 1; j
< uspi
->s_ncg
; j
<<= 1 ) {
205 if (i
>= uspi
->s_ncg
)
207 if (SWAB32(sb
->fs_cs(i
).cs_nifree
)) {
214 * That failed: try linear search for a free inode
216 i
= ufs_inotocg(dir
->i_ino
) + 1;
217 for (j
= 2; j
< uspi
->s_ncg
; j
++) {
219 if (i
>= uspi
->s_ncg
)
221 if (SWAB32(sb
->fs_cs(i
).cs_nifree
)) {
230 ucpi
= ufs_load_cylinder (sb
, cg
);
233 ucg
= ubh_get_ucg(UCPI_UBH
);
234 if (!ufs_cg_chkmagic(ucg
))
235 ufs_panic (sb
, "ufs_new_inode", "internal error, bad cg magic number");
237 start
= ucpi
->c_irotor
;
238 bit
= ubh_find_next_zero_bit (UCPI_UBH
, ucpi
->c_iusedoff
, uspi
->s_ipg
, start
);
239 if (!(bit
< uspi
->s_ipg
)) {
240 bit
= ubh_find_first_zero_bit (UCPI_UBH
, ucpi
->c_iusedoff
, start
);
241 if (!(bit
< start
)) {
242 ufs_error (sb
, "ufs_new_inode",
243 "cylinder group %u corrupted - error in inode bitmap\n", cg
);
247 UFSD(("start = %u, bit = %u, ipg = %u\n", start
, bit
, uspi
->s_ipg
))
248 if (ubh_isclr (UCPI_UBH
, ucpi
->c_iusedoff
, bit
))
249 ubh_setbit (UCPI_UBH
, ucpi
->c_iusedoff
, bit
);
251 ufs_panic (sb
, "ufs_new_inode", "internal error");
255 DEC_SWAB32(ucg
->cg_cs
.cs_nifree
);
256 DEC_SWAB32(usb1
->fs_cstotal
.cs_nifree
);
257 DEC_SWAB32(sb
->fs_cs(cg
).cs_nifree
);
260 INC_SWAB32(ucg
->cg_cs
.cs_ndir
);
261 INC_SWAB32(usb1
->fs_cstotal
.cs_ndir
);
262 INC_SWAB32(sb
->fs_cs(cg
).cs_ndir
);
265 ubh_mark_buffer_dirty (USPI_UBH
, 1);
266 ubh_mark_buffer_dirty (UCPI_UBH
, 1);
267 if (sb
->s_flags
& MS_SYNCHRONOUS
) {
268 ubh_ll_rw_block (WRITE
, 1, (struct ufs_buffer_head
**) &ucpi
);
269 ubh_wait_on_buffer (UCPI_UBH
);
273 inode
->i_mode
= mode
;
276 inode
->i_dev
= sb
->s_dev
;
277 inode
->i_uid
= current
->fsuid
;
278 if (test_opt (sb
, GRPID
))
279 inode
->i_gid
= dir
->i_gid
;
280 else if (dir
->i_mode
& S_ISGID
) {
281 inode
->i_gid
= dir
->i_gid
;
285 inode
->i_gid
= current
->fsgid
;
287 inode
->i_ino
= cg
* uspi
->s_ipg
+ bit
;
288 inode
->i_blksize
= PAGE_SIZE
; /* This is the optimal IO size (for stat), not the fs block size */
290 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
291 inode
->u
.ufs_i
.i_flags
= dir
->u
.ufs_i
.i_flags
;
292 inode
->u
.ufs_i
.i_uid
= inode
->i_uid
;
293 inode
->u
.ufs_i
.i_gid
= inode
->i_gid
;
294 inode
->u
.ufs_i
.i_lastfrag
= 0;
297 insert_inode_hash(inode
);
298 mark_inode_dirty(inode
);
302 if(DQUOT_ALLOC_INODE(sb
, inode
)) {
303 sb
->dq_op
->drop(inode
);
310 UFSD(("allocating inode %lu\n", inode
->i_ino
))
318 UFSD(("EXIT (FAILED)\n"))