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
);
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_ll_rw_block (SWRITE
, 1, (struct ufs_buffer_head
**) &ucpi
);
128 ubh_wait_on_buffer (UCPI_UBH
);
137 * There are two policies for allocating an inode. If the new inode is
138 * a directory, then a forward search is made for a block group with both
139 * free space and a low directory-to-inode ratio; if that fails, then of
140 * the groups with above-average free space, that group with the fewest
141 * directories already is chosen.
143 * For other inodes, search forward from the parent directory's block
144 * group to find a free inode.
146 struct inode
* ufs_new_inode(struct inode
* dir
, int mode
)
148 struct super_block
* sb
;
149 struct ufs_sb_info
* sbi
;
150 struct ufs_sb_private_info
* uspi
;
151 struct ufs_super_block_first
* usb1
;
152 struct ufs_cg_private_info
* ucpi
;
153 struct ufs_cylinder_group
* ucg
;
154 struct inode
* inode
;
155 unsigned cg
, bit
, i
, j
, start
;
156 struct ufs_inode_info
*ufsi
;
160 /* Cannot create files in a deleted directory */
161 if (!dir
|| !dir
->i_nlink
)
162 return ERR_PTR(-EPERM
);
164 inode
= new_inode(sb
);
166 return ERR_PTR(-ENOMEM
);
170 usb1
= ubh_get_usb_first(uspi
);
175 * Try to place the inode in its parent directory
177 i
= ufs_inotocg(dir
->i_ino
);
178 if (sbi
->fs_cs(i
).cs_nifree
) {
184 * Use a quadratic hash to find a group with a free inode
186 for ( j
= 1; j
< uspi
->s_ncg
; j
<<= 1 ) {
188 if (i
>= uspi
->s_ncg
)
190 if (sbi
->fs_cs(i
).cs_nifree
) {
197 * That failed: try linear search for a free inode
199 i
= ufs_inotocg(dir
->i_ino
) + 1;
200 for (j
= 2; j
< uspi
->s_ncg
; j
++) {
202 if (i
>= uspi
->s_ncg
)
204 if (sbi
->fs_cs(i
).cs_nifree
) {
213 ucpi
= ufs_load_cylinder (sb
, cg
);
216 ucg
= ubh_get_ucg(UCPI_UBH
);
217 if (!ufs_cg_chkmagic(sb
, ucg
))
218 ufs_panic (sb
, "ufs_new_inode", "internal error, bad cg magic number");
220 start
= ucpi
->c_irotor
;
221 bit
= ubh_find_next_zero_bit (UCPI_UBH
, ucpi
->c_iusedoff
, uspi
->s_ipg
, start
);
222 if (!(bit
< uspi
->s_ipg
)) {
223 bit
= ubh_find_first_zero_bit (UCPI_UBH
, ucpi
->c_iusedoff
, start
);
224 if (!(bit
< start
)) {
225 ufs_error (sb
, "ufs_new_inode",
226 "cylinder group %u corrupted - error in inode bitmap\n", cg
);
230 UFSD(("start = %u, bit = %u, ipg = %u\n", start
, bit
, uspi
->s_ipg
))
231 if (ubh_isclr (UCPI_UBH
, ucpi
->c_iusedoff
, bit
))
232 ubh_setbit (UCPI_UBH
, ucpi
->c_iusedoff
, bit
);
234 ufs_panic (sb
, "ufs_new_inode", "internal error");
238 fs32_sub(sb
, &ucg
->cg_cs
.cs_nifree
, 1);
239 fs32_sub(sb
, &usb1
->fs_cstotal
.cs_nifree
, 1);
240 fs32_sub(sb
, &sbi
->fs_cs(cg
).cs_nifree
, 1);
243 fs32_add(sb
, &ucg
->cg_cs
.cs_ndir
, 1);
244 fs32_add(sb
, &usb1
->fs_cstotal
.cs_ndir
, 1);
245 fs32_add(sb
, &sbi
->fs_cs(cg
).cs_ndir
, 1);
248 ubh_mark_buffer_dirty (USPI_UBH
);
249 ubh_mark_buffer_dirty (UCPI_UBH
);
250 if (sb
->s_flags
& MS_SYNCHRONOUS
) {
251 ubh_ll_rw_block (SWRITE
, 1, (struct ufs_buffer_head
**) &ucpi
);
252 ubh_wait_on_buffer (UCPI_UBH
);
256 inode
->i_mode
= mode
;
257 inode
->i_uid
= current
->fsuid
;
258 if (dir
->i_mode
& S_ISGID
) {
259 inode
->i_gid
= dir
->i_gid
;
261 inode
->i_mode
|= S_ISGID
;
263 inode
->i_gid
= current
->fsgid
;
265 inode
->i_ino
= cg
* uspi
->s_ipg
+ bit
;
266 inode
->i_blksize
= PAGE_SIZE
; /* This is the optimal IO size (for stat), not the fs block size */
268 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME_SEC
;
269 ufsi
->i_flags
= UFS_I(dir
)->i_flags
;
270 ufsi
->i_lastfrag
= 0;
274 ufsi
->i_oeftflag
= 0;
275 memset(&ufsi
->i_u1
, 0, sizeof(ufsi
->i_u1
));
277 insert_inode_hash(inode
);
278 mark_inode_dirty(inode
);
282 if (DQUOT_ALLOC_INODE(inode
)) {
284 inode
->i_flags
|= S_NOQUOTA
;
287 return ERR_PTR(-EDQUOT
);
290 UFSD(("allocating inode %lu\n", inode
->i_ino
))
296 make_bad_inode(inode
);
298 UFSD(("EXIT (FAILED)\n"))
299 return ERR_PTR(-ENOSPC
);