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 * UFS2 write support added by 23 * Evgeniy Dushistov <dushistov@mail.ru>, 2007 24 */ 25 26 #include <linux/fs.h> 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> 34 35 #include "ufs_fs.h" 36 #include "ufs.h" 37 #include "swab.h" 38 #include "util.h" 39 40 /* 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. 47 * 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. 55 */ 56 void ufs_free_inode (struct inode * inode) 57 { 58 struct super_block * sb; 59 struct ufs_sb_private_info * uspi; 60 struct ufs_super_block_first * usb1; 61 struct ufs_cg_private_info * ucpi; 62 struct ufs_cylinder_group * ucg; 63 int is_directory; 64 unsigned ino, cg, bit; 65 66 UFSD("ENTER, ino %lu\n", inode->i_ino); 67 68 sb = inode->i_sb; 69 uspi = UFS_SB(sb)->s_uspi; 70 usb1 = ubh_get_usb_first(uspi); 71 72 ino = inode->i_ino; 73 74 lock_super (sb); 75 76 if (!((ino > 1) && (ino < (uspi->s_ncg * uspi->s_ipg )))) { 77 ufs_warning(sb, "ufs_free_inode", "reserved inode or nonexistent inode %u\n", ino); 78 unlock_super (sb); 79 return; 80 } 81 82 cg = ufs_inotocg (ino); 83 bit = ufs_inotocgoff (ino); 84 ucpi = ufs_load_cylinder (sb, cg); 85 if (!ucpi) { 86 unlock_super (sb); 87 return; 88 } 89 ucg = ubh_get_ucg(UCPI_UBH(ucpi)); 90 if (!ufs_cg_chkmagic(sb, ucg)) 91 ufs_panic (sb, "ufs_free_fragments", "internal error, bad cg magic number"); 92 93 ucg->cg_time = cpu_to_fs32(sb, get_seconds()); 94 95 is_directory = S_ISDIR(inode->i_mode); 96 97 if (ubh_isclr (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit)) 98 ufs_error(sb, "ufs_free_inode", "bit already cleared for inode %u", ino); 99 else { 100 ubh_clrbit (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit); 101 if (ino < ucpi->c_irotor) 102 ucpi->c_irotor = ino; 103 fs32_add(sb, &ucg->cg_cs.cs_nifree, 1); 104 uspi->cs_total.cs_nifree++; 105 fs32_add(sb, &UFS_SB(sb)->fs_cs(cg).cs_nifree, 1); 106 107 if (is_directory) { 108 fs32_sub(sb, &ucg->cg_cs.cs_ndir, 1); 109 uspi->cs_total.cs_ndir--; 110 fs32_sub(sb, &UFS_SB(sb)->fs_cs(cg).cs_ndir, 1); 111 } 112 } 113 114 ubh_mark_buffer_dirty (USPI_UBH(uspi)); 115 ubh_mark_buffer_dirty (UCPI_UBH(ucpi)); 116 if (sb->s_flags & MS_SYNCHRONOUS) { 117 ubh_ll_rw_block(SWRITE, UCPI_UBH(ucpi)); 118 ubh_wait_on_buffer (UCPI_UBH(ucpi)); 119 } 120 121 sb->s_dirt = 1; 122 unlock_super (sb); 123 UFSD("EXIT\n"); 124 } 125 126 /* 127 * Nullify new chunk of inodes, 128 * BSD people also set ui_gen field of inode 129 * during nullification, but we not care about 130 * that because of linux ufs do not support NFS 131 */ 132 static void ufs2_init_inodes_chunk(struct super_block *sb, 133 struct ufs_cg_private_info *ucpi, 134 struct ufs_cylinder_group *ucg) 135 { 136 struct buffer_head *bh; 137 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi; 138 sector_t beg = uspi->s_sbbase + 139 ufs_inotofsba(ucpi->c_cgx * uspi->s_ipg + 140 fs32_to_cpu(sb, ucg->cg_u.cg_u2.cg_initediblk)); 141 sector_t end = beg + uspi->s_fpb; 142 143 UFSD("ENTER cgno %d\n", ucpi->c_cgx); 144 145 for (; beg < end; ++beg) { 146 bh = sb_getblk(sb, beg); 147 lock_buffer(bh); 148 memset(bh->b_data, 0, sb->s_blocksize); 149 set_buffer_uptodate(bh); 150 mark_buffer_dirty(bh); 151 unlock_buffer(bh); 152 if (sb->s_flags & MS_SYNCHRONOUS) 153 sync_dirty_buffer(bh); 154 brelse(bh); 155 } 156 157 fs32_add(sb, &ucg->cg_u.cg_u2.cg_initediblk, uspi->s_inopb); 158 ubh_mark_buffer_dirty(UCPI_UBH(ucpi)); 159 if (sb->s_flags & MS_SYNCHRONOUS) { 160 ubh_ll_rw_block(SWRITE, UCPI_UBH(ucpi)); 161 ubh_wait_on_buffer(UCPI_UBH(ucpi)); 162 } 163 164 UFSD("EXIT\n"); 165 } 166 167 /* 168 * There are two policies for allocating an inode. If the new inode is 169 * a directory, then a forward search is made for a block group with both 170 * free space and a low directory-to-inode ratio; if that fails, then of 171 * the groups with above-average free space, that group with the fewest 172 * directories already is chosen. 173 * 174 * For other inodes, search forward from the parent directory's block 175 * group to find a free inode. 176 */ 177 struct inode * ufs_new_inode(struct inode * dir, int mode) 178 { 179 struct super_block * sb; 180 struct ufs_sb_info * sbi; 181 struct ufs_sb_private_info * uspi; 182 struct ufs_super_block_first * usb1; 183 struct ufs_cg_private_info * ucpi; 184 struct ufs_cylinder_group * ucg; 185 struct inode * inode; 186 unsigned cg, bit, i, j, start; 187 struct ufs_inode_info *ufsi; 188 int err = -ENOSPC; 189 190 UFSD("ENTER\n"); 191 192 /* Cannot create files in a deleted directory */ 193 if (!dir || !dir->i_nlink) 194 return ERR_PTR(-EPERM); 195 sb = dir->i_sb; 196 inode = new_inode(sb); 197 if (!inode) 198 return ERR_PTR(-ENOMEM); 199 ufsi = UFS_I(inode); 200 sbi = UFS_SB(sb); 201 uspi = sbi->s_uspi; 202 usb1 = ubh_get_usb_first(uspi); 203 204 lock_super (sb); 205 206 /* 207 * Try to place the inode in its parent directory 208 */ 209 i = ufs_inotocg(dir->i_ino); 210 if (sbi->fs_cs(i).cs_nifree) { 211 cg = i; 212 goto cg_found; 213 } 214 215 /* 216 * Use a quadratic hash to find a group with a free inode 217 */ 218 for ( j = 1; j < uspi->s_ncg; j <<= 1 ) { 219 i += j; 220 if (i >= uspi->s_ncg) 221 i -= uspi->s_ncg; 222 if (sbi->fs_cs(i).cs_nifree) { 223 cg = i; 224 goto cg_found; 225 } 226 } 227 228 /* 229 * That failed: try linear search for a free inode 230 */ 231 i = ufs_inotocg(dir->i_ino) + 1; 232 for (j = 2; j < uspi->s_ncg; j++) { 233 i++; 234 if (i >= uspi->s_ncg) 235 i = 0; 236 if (sbi->fs_cs(i).cs_nifree) { 237 cg = i; 238 goto cg_found; 239 } 240 } 241 242 goto failed; 243 244 cg_found: 245 ucpi = ufs_load_cylinder (sb, cg); 246 if (!ucpi) { 247 err = -EIO; 248 goto failed; 249 } 250 ucg = ubh_get_ucg(UCPI_UBH(ucpi)); 251 if (!ufs_cg_chkmagic(sb, ucg)) 252 ufs_panic (sb, "ufs_new_inode", "internal error, bad cg magic number"); 253 254 start = ucpi->c_irotor; 255 bit = ubh_find_next_zero_bit (UCPI_UBH(ucpi), ucpi->c_iusedoff, uspi->s_ipg, start); 256 if (!(bit < uspi->s_ipg)) { 257 bit = ubh_find_first_zero_bit (UCPI_UBH(ucpi), ucpi->c_iusedoff, start); 258 if (!(bit < start)) { 259 ufs_error (sb, "ufs_new_inode", 260 "cylinder group %u corrupted - error in inode bitmap\n", cg); 261 err = -EIO; 262 goto failed; 263 } 264 } 265 UFSD("start = %u, bit = %u, ipg = %u\n", start, bit, uspi->s_ipg); 266 if (ubh_isclr (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit)) 267 ubh_setbit (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit); 268 else { 269 ufs_panic (sb, "ufs_new_inode", "internal error"); 270 err = -EIO; 271 goto failed; 272 } 273 274 if (uspi->fs_magic == UFS2_MAGIC) { 275 u32 initediblk = fs32_to_cpu(sb, ucg->cg_u.cg_u2.cg_initediblk); 276 277 if (bit + uspi->s_inopb > initediblk && 278 initediblk < fs32_to_cpu(sb, ucg->cg_u.cg_u2.cg_niblk)) 279 ufs2_init_inodes_chunk(sb, ucpi, ucg); 280 } 281 282 fs32_sub(sb, &ucg->cg_cs.cs_nifree, 1); 283 uspi->cs_total.cs_nifree--; 284 fs32_sub(sb, &sbi->fs_cs(cg).cs_nifree, 1); 285 286 if (S_ISDIR(mode)) { 287 fs32_add(sb, &ucg->cg_cs.cs_ndir, 1); 288 uspi->cs_total.cs_ndir++; 289 fs32_add(sb, &sbi->fs_cs(cg).cs_ndir, 1); 290 } 291 ubh_mark_buffer_dirty (USPI_UBH(uspi)); 292 ubh_mark_buffer_dirty (UCPI_UBH(ucpi)); 293 if (sb->s_flags & MS_SYNCHRONOUS) { 294 ubh_ll_rw_block(SWRITE, UCPI_UBH(ucpi)); 295 ubh_wait_on_buffer (UCPI_UBH(ucpi)); 296 } 297 sb->s_dirt = 1; 298 299 inode->i_ino = cg * uspi->s_ipg + bit; 300 inode_init_owner(inode, dir, mode); 301 inode->i_blocks = 0; 302 inode->i_generation = 0; 303 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC; 304 ufsi->i_flags = UFS_I(dir)->i_flags; 305 ufsi->i_lastfrag = 0; 306 ufsi->i_shadow = 0; 307 ufsi->i_osync = 0; 308 ufsi->i_oeftflag = 0; 309 ufsi->i_dir_start_lookup = 0; 310 memset(&ufsi->i_u1, 0, sizeof(ufsi->i_u1)); 311 insert_inode_hash(inode); 312 mark_inode_dirty(inode); 313 314 if (uspi->fs_magic == UFS2_MAGIC) { 315 struct buffer_head *bh; 316 struct ufs2_inode *ufs2_inode; 317 318 /* 319 * setup birth date, we do it here because of there is no sense 320 * to hold it in struct ufs_inode_info, and lose 64 bit 321 */ 322 bh = sb_bread(sb, uspi->s_sbbase + ufs_inotofsba(inode->i_ino)); 323 if (!bh) { 324 ufs_warning(sb, "ufs_read_inode", 325 "unable to read inode %lu\n", 326 inode->i_ino); 327 err = -EIO; 328 goto fail_remove_inode; 329 } 330 lock_buffer(bh); 331 ufs2_inode = (struct ufs2_inode *)bh->b_data; 332 ufs2_inode += ufs_inotofsbo(inode->i_ino); 333 ufs2_inode->ui_birthtime = cpu_to_fs64(sb, CURRENT_TIME.tv_sec); 334 ufs2_inode->ui_birthnsec = cpu_to_fs32(sb, CURRENT_TIME.tv_nsec); 335 mark_buffer_dirty(bh); 336 unlock_buffer(bh); 337 if (sb->s_flags & MS_SYNCHRONOUS) 338 sync_dirty_buffer(bh); 339 brelse(bh); 340 } 341 342 unlock_super (sb); 343 344 UFSD("allocating inode %lu\n", inode->i_ino); 345 UFSD("EXIT\n"); 346 return inode; 347 348 fail_remove_inode: 349 unlock_super(sb); 350 inode->i_nlink = 0; 351 iput(inode); 352 UFSD("EXIT (FAILED): err %d\n", err); 353 return ERR_PTR(err); 354 failed: 355 unlock_super (sb); 356 make_bad_inode(inode); 357 iput (inode); 358 UFSD("EXIT (FAILED): err %d\n", err); 359 return ERR_PTR(err); 360 } 361