Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 63 | UFSD("ENTER, ino %lu\n", inode->i_ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
| 65 | sb = inode->i_sb; |
| 66 | uspi = UFS_SB(sb)->s_uspi; |
Evgeniy | 7b4ee73 | 2006-01-14 11:42:06 +0300 | [diff] [blame] | 67 | usb1 = ubh_get_usb_first(uspi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 86 | ucg = ubh_get_ucg(UCPI_UBH(ucpi)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 99 | if (ubh_isclr (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | ufs_error(sb, "ufs_free_inode", "bit already cleared for inode %u", ino); |
| 101 | else { |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 102 | ubh_clrbit (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | if (ino < ucpi->c_irotor) |
| 104 | ucpi->c_irotor = ino; |
| 105 | fs32_add(sb, &ucg->cg_cs.cs_nifree, 1); |
Evgeniy Dushistov | ee3ffd6 | 2006-06-25 05:47:30 -0700 | [diff] [blame^] | 106 | uspi->cs_total.cs_nifree++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Evgeniy Dushistov | ee3ffd6 | 2006-06-25 05:47:30 -0700 | [diff] [blame^] | 111 | uspi->cs_total.cs_ndir--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | fs32_sub(sb, &UFS_SB(sb)->fs_cs(cg).cs_ndir, 1); |
| 113 | } |
| 114 | } |
| 115 | |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 116 | ubh_mark_buffer_dirty (USPI_UBH(uspi)); |
| 117 | ubh_mark_buffer_dirty (UCPI_UBH(ucpi)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | if (sb->s_flags & MS_SYNCHRONOUS) { |
Jan Kara | 096125f3 | 2005-09-06 15:19:15 -0700 | [diff] [blame] | 119 | ubh_ll_rw_block (SWRITE, 1, (struct ufs_buffer_head **) &ucpi); |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 120 | ubh_wait_on_buffer (UCPI_UBH(ucpi)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | sb->s_dirt = 1; |
| 124 | unlock_super (sb); |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 125 | UFSD("EXIT\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 150 | UFSD("ENTER\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Evgeniy | 7b4ee73 | 2006-01-14 11:42:06 +0300 | [diff] [blame] | 162 | usb1 = ubh_get_usb_first(uspi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 208 | ucg = ubh_get_ucg(UCPI_UBH(ucpi)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 213 | bit = ubh_find_next_zero_bit (UCPI_UBH(ucpi), ucpi->c_iusedoff, uspi->s_ipg, start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | if (!(bit < uspi->s_ipg)) { |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 215 | bit = ubh_find_first_zero_bit (UCPI_UBH(ucpi), ucpi->c_iusedoff, start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 222 | UFSD("start = %u, bit = %u, ipg = %u\n", start, bit, uspi->s_ipg); |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 223 | if (ubh_isclr (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit)) |
| 224 | ubh_setbit (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Evgeniy Dushistov | ee3ffd6 | 2006-06-25 05:47:30 -0700 | [diff] [blame^] | 231 | uspi->cs_total.cs_nifree--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Evgeniy Dushistov | ee3ffd6 | 2006-06-25 05:47:30 -0700 | [diff] [blame^] | 236 | uspi->cs_total.cs_ndir++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | fs32_add(sb, &sbi->fs_cs(cg).cs_ndir, 1); |
| 238 | } |
| 239 | |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 240 | ubh_mark_buffer_dirty (USPI_UBH(uspi)); |
| 241 | ubh_mark_buffer_dirty (UCPI_UBH(ucpi)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | if (sb->s_flags & MS_SYNCHRONOUS) { |
Jan Kara | 096125f3 | 2005-09-06 15:19:15 -0700 | [diff] [blame] | 243 | ubh_ll_rw_block (SWRITE, 1, (struct ufs_buffer_head **) &ucpi); |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 244 | ubh_wait_on_buffer (UCPI_UBH(ucpi)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Evgeniy Dushistov | dd187a2 | 2006-06-25 05:47:25 -0700 | [diff] [blame] | 267 | ufsi->i_dir_start_lookup = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 283 | UFSD("allocating inode %lu\n", inode->i_ino); |
| 284 | UFSD("EXIT\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | return inode; |
| 286 | |
| 287 | failed: |
| 288 | unlock_super (sb); |
| 289 | make_bad_inode(inode); |
| 290 | iput (inode); |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 291 | UFSD("EXIT (FAILED)\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | return ERR_PTR(-ENOSPC); |
| 293 | } |