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
22 * UFS2 write support added by
23 * Evgeniy Dushistov <dushistov@mail.ru>, 2007
27 #include <linux/time.h>
28 #include <linux/stat.h>
29 #include <linux/string.h>
30 #include <linux/buffer_head.h>
31 #include <linux/sched.h>
32 #include <linux/bitops.h>
33 #include <asm/byteorder.h>
41 * NOTE! When we get the inode, we're the only people
42 * that have access to it, and as such there are no
43 * race conditions we have to worry about. The inode
44 * is not on the hash-lists, and it cannot be reached
45 * through the filesystem because the directory entry
46 * has been deleted earlier.
48 * HOWEVER: we must make sure that we get no aliases,
49 * which means that we have to call "clear_inode()"
50 * _before_ we mark the inode not in use in the inode
51 * bitmaps. Otherwise a newly created file might use
52 * the same inode number (not actually the same pointer
53 * though), and then we'd have two inodes sharing the
54 * same inode number and space on the harddisk.
56 void ufs_free_inode (struct inode
* inode
)
58 struct super_block
* sb
;
59 struct ufs_sb_private_info
* uspi
;
60 struct ufs_cg_private_info
* ucpi
;
61 struct ufs_cylinder_group
* ucg
;
63 unsigned ino
, cg
, bit
;
65 UFSD("ENTER, ino %lu\n", inode
->i_ino
);
68 uspi
= UFS_SB(sb
)->s_uspi
;
72 mutex_lock(&UFS_SB(sb
)->s_lock
);
74 if (!((ino
> 1) && (ino
< (uspi
->s_ncg
* uspi
->s_ipg
)))) {
75 ufs_warning(sb
, "ufs_free_inode", "reserved inode or nonexistent inode %u\n", ino
);
76 mutex_unlock(&UFS_SB(sb
)->s_lock
);
80 cg
= ufs_inotocg (ino
);
81 bit
= ufs_inotocgoff (ino
);
82 ucpi
= ufs_load_cylinder (sb
, cg
);
84 mutex_unlock(&UFS_SB(sb
)->s_lock
);
87 ucg
= ubh_get_ucg(UCPI_UBH(ucpi
));
88 if (!ufs_cg_chkmagic(sb
, ucg
))
89 ufs_panic (sb
, "ufs_free_fragments", "internal error, bad cg magic number");
91 ucg
->cg_time
= cpu_to_fs32(sb
, get_seconds());
93 is_directory
= S_ISDIR(inode
->i_mode
);
95 if (ubh_isclr (UCPI_UBH(ucpi
), ucpi
->c_iusedoff
, bit
))
96 ufs_error(sb
, "ufs_free_inode", "bit already cleared for inode %u", ino
);
98 ubh_clrbit (UCPI_UBH(ucpi
), ucpi
->c_iusedoff
, bit
);
99 if (ino
< ucpi
->c_irotor
)
100 ucpi
->c_irotor
= ino
;
101 fs32_add(sb
, &ucg
->cg_cs
.cs_nifree
, 1);
102 uspi
->cs_total
.cs_nifree
++;
103 fs32_add(sb
, &UFS_SB(sb
)->fs_cs(cg
).cs_nifree
, 1);
106 fs32_sub(sb
, &ucg
->cg_cs
.cs_ndir
, 1);
107 uspi
->cs_total
.cs_ndir
--;
108 fs32_sub(sb
, &UFS_SB(sb
)->fs_cs(cg
).cs_ndir
, 1);
112 ubh_mark_buffer_dirty (USPI_UBH(uspi
));
113 ubh_mark_buffer_dirty (UCPI_UBH(ucpi
));
114 if (sb
->s_flags
& MS_SYNCHRONOUS
)
115 ubh_sync_block(UCPI_UBH(ucpi
));
117 ufs_mark_sb_dirty(sb
);
118 mutex_unlock(&UFS_SB(sb
)->s_lock
);
123 * Nullify new chunk of inodes,
124 * BSD people also set ui_gen field of inode
125 * during nullification, but we not care about
126 * that because of linux ufs do not support NFS
128 static void ufs2_init_inodes_chunk(struct super_block
*sb
,
129 struct ufs_cg_private_info
*ucpi
,
130 struct ufs_cylinder_group
*ucg
)
132 struct buffer_head
*bh
;
133 struct ufs_sb_private_info
*uspi
= UFS_SB(sb
)->s_uspi
;
134 sector_t beg
= uspi
->s_sbbase
+
135 ufs_inotofsba(ucpi
->c_cgx
* uspi
->s_ipg
+
136 fs32_to_cpu(sb
, ucg
->cg_u
.cg_u2
.cg_initediblk
));
137 sector_t end
= beg
+ uspi
->s_fpb
;
139 UFSD("ENTER cgno %d\n", ucpi
->c_cgx
);
141 for (; beg
< end
; ++beg
) {
142 bh
= sb_getblk(sb
, beg
);
144 memset(bh
->b_data
, 0, sb
->s_blocksize
);
145 set_buffer_uptodate(bh
);
146 mark_buffer_dirty(bh
);
148 if (sb
->s_flags
& MS_SYNCHRONOUS
)
149 sync_dirty_buffer(bh
);
153 fs32_add(sb
, &ucg
->cg_u
.cg_u2
.cg_initediblk
, uspi
->s_inopb
);
154 ubh_mark_buffer_dirty(UCPI_UBH(ucpi
));
155 if (sb
->s_flags
& MS_SYNCHRONOUS
)
156 ubh_sync_block(UCPI_UBH(ucpi
));
162 * There are two policies for allocating an inode. If the new inode is
163 * a directory, then a forward search is made for a block group with both
164 * free space and a low directory-to-inode ratio; if that fails, then of
165 * the groups with above-average free space, that group with the fewest
166 * directories already is chosen.
168 * For other inodes, search forward from the parent directory's block
169 * group to find a free inode.
171 struct inode
*ufs_new_inode(struct inode
*dir
, umode_t mode
)
173 struct super_block
* sb
;
174 struct ufs_sb_info
* sbi
;
175 struct ufs_sb_private_info
* uspi
;
176 struct ufs_cg_private_info
* ucpi
;
177 struct ufs_cylinder_group
* ucg
;
178 struct inode
* inode
;
179 unsigned cg
, bit
, i
, j
, start
;
180 struct ufs_inode_info
*ufsi
;
185 /* Cannot create files in a deleted directory */
186 if (!dir
|| !dir
->i_nlink
)
187 return ERR_PTR(-EPERM
);
189 inode
= new_inode(sb
);
191 return ERR_PTR(-ENOMEM
);
196 mutex_lock(&sbi
->s_lock
);
199 * Try to place the inode in its parent directory
201 i
= ufs_inotocg(dir
->i_ino
);
202 if (sbi
->fs_cs(i
).cs_nifree
) {
208 * Use a quadratic hash to find a group with a free inode
210 for ( j
= 1; j
< uspi
->s_ncg
; j
<<= 1 ) {
212 if (i
>= uspi
->s_ncg
)
214 if (sbi
->fs_cs(i
).cs_nifree
) {
221 * That failed: try linear search for a free inode
223 i
= ufs_inotocg(dir
->i_ino
) + 1;
224 for (j
= 2; j
< uspi
->s_ncg
; j
++) {
226 if (i
>= uspi
->s_ncg
)
228 if (sbi
->fs_cs(i
).cs_nifree
) {
237 ucpi
= ufs_load_cylinder (sb
, cg
);
242 ucg
= ubh_get_ucg(UCPI_UBH(ucpi
));
243 if (!ufs_cg_chkmagic(sb
, ucg
))
244 ufs_panic (sb
, "ufs_new_inode", "internal error, bad cg magic number");
246 start
= ucpi
->c_irotor
;
247 bit
= ubh_find_next_zero_bit (UCPI_UBH(ucpi
), ucpi
->c_iusedoff
, uspi
->s_ipg
, start
);
248 if (!(bit
< uspi
->s_ipg
)) {
249 bit
= ubh_find_first_zero_bit (UCPI_UBH(ucpi
), ucpi
->c_iusedoff
, start
);
250 if (!(bit
< start
)) {
251 ufs_error (sb
, "ufs_new_inode",
252 "cylinder group %u corrupted - error in inode bitmap\n", cg
);
257 UFSD("start = %u, bit = %u, ipg = %u\n", start
, bit
, uspi
->s_ipg
);
258 if (ubh_isclr (UCPI_UBH(ucpi
), ucpi
->c_iusedoff
, bit
))
259 ubh_setbit (UCPI_UBH(ucpi
), ucpi
->c_iusedoff
, bit
);
261 ufs_panic (sb
, "ufs_new_inode", "internal error");
266 if (uspi
->fs_magic
== UFS2_MAGIC
) {
267 u32 initediblk
= fs32_to_cpu(sb
, ucg
->cg_u
.cg_u2
.cg_initediblk
);
269 if (bit
+ uspi
->s_inopb
> initediblk
&&
270 initediblk
< fs32_to_cpu(sb
, ucg
->cg_u
.cg_u2
.cg_niblk
))
271 ufs2_init_inodes_chunk(sb
, ucpi
, ucg
);
274 fs32_sub(sb
, &ucg
->cg_cs
.cs_nifree
, 1);
275 uspi
->cs_total
.cs_nifree
--;
276 fs32_sub(sb
, &sbi
->fs_cs(cg
).cs_nifree
, 1);
279 fs32_add(sb
, &ucg
->cg_cs
.cs_ndir
, 1);
280 uspi
->cs_total
.cs_ndir
++;
281 fs32_add(sb
, &sbi
->fs_cs(cg
).cs_ndir
, 1);
283 ubh_mark_buffer_dirty (USPI_UBH(uspi
));
284 ubh_mark_buffer_dirty (UCPI_UBH(ucpi
));
285 if (sb
->s_flags
& MS_SYNCHRONOUS
)
286 ubh_sync_block(UCPI_UBH(ucpi
));
287 ufs_mark_sb_dirty(sb
);
289 inode
->i_ino
= cg
* uspi
->s_ipg
+ bit
;
290 inode_init_owner(inode
, dir
, mode
);
292 inode
->i_generation
= 0;
293 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= current_time(inode
);
294 ufsi
->i_flags
= UFS_I(dir
)->i_flags
;
295 ufsi
->i_lastfrag
= 0;
298 ufsi
->i_oeftflag
= 0;
299 ufsi
->i_dir_start_lookup
= 0;
300 memset(&ufsi
->i_u1
, 0, sizeof(ufsi
->i_u1
));
301 if (insert_inode_locked(inode
) < 0) {
305 mark_inode_dirty(inode
);
307 if (uspi
->fs_magic
== UFS2_MAGIC
) {
308 struct buffer_head
*bh
;
309 struct ufs2_inode
*ufs2_inode
;
312 * setup birth date, we do it here because of there is no sense
313 * to hold it in struct ufs_inode_info, and lose 64 bit
315 bh
= sb_bread(sb
, uspi
->s_sbbase
+ ufs_inotofsba(inode
->i_ino
));
317 ufs_warning(sb
, "ufs_read_inode",
318 "unable to read inode %lu\n",
321 goto fail_remove_inode
;
324 ufs2_inode
= (struct ufs2_inode
*)bh
->b_data
;
325 ufs2_inode
+= ufs_inotofsbo(inode
->i_ino
);
326 ufs2_inode
->ui_birthtime
= cpu_to_fs64(sb
, CURRENT_TIME
.tv_sec
);
327 ufs2_inode
->ui_birthnsec
= cpu_to_fs32(sb
, CURRENT_TIME
.tv_nsec
);
328 mark_buffer_dirty(bh
);
330 if (sb
->s_flags
& MS_SYNCHRONOUS
)
331 sync_dirty_buffer(bh
);
334 mutex_unlock(&sbi
->s_lock
);
336 UFSD("allocating inode %lu\n", inode
->i_ino
);
341 mutex_unlock(&sbi
->s_lock
);
343 unlock_new_inode(inode
);
345 UFSD("EXIT (FAILED): err %d\n", err
);
348 mutex_unlock(&sbi
->s_lock
);
349 make_bad_inode(inode
);
351 UFSD("EXIT (FAILED): err %d\n", err
);