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 |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 21 | * |
| 22 | * UFS2 write support added by |
| 23 | * Evgeniy Dushistov <dushistov@mail.ru>, 2007 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | */ |
| 25 | |
| 26 | #include <linux/fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/time.h> |
| 28 | #include <linux/stat.h> |
| 29 | #include <linux/string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/buffer_head.h> |
| 31 | #include <linux/sched.h> |
| 32 | #include <linux/bitops.h> |
| 33 | #include <asm/byteorder.h> |
| 34 | |
Mike Frysinger | e542059 | 2008-02-08 04:21:31 -0800 | [diff] [blame] | 35 | #include "ufs_fs.h" |
Christoph Hellwig | bcd6d4e | 2007-10-16 23:26:51 -0700 | [diff] [blame] | 36 | #include "ufs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include "swab.h" |
| 38 | #include "util.h" |
| 39 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | struct ufs_cg_private_info * ucpi; |
| 61 | struct ufs_cylinder_group * ucg; |
| 62 | int is_directory; |
| 63 | unsigned ino, cg, bit; |
| 64 | |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 65 | UFSD("ENTER, ino %lu\n", inode->i_ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
| 67 | sb = inode->i_sb; |
| 68 | uspi = UFS_SB(sb)->s_uspi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
| 70 | ino = inode->i_ino; |
| 71 | |
Fabian Frederick | cdd9eef | 2015-06-10 10:09:32 +1000 | [diff] [blame] | 72 | mutex_lock(&UFS_SB(sb)->s_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
| 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); |
Fabian Frederick | cdd9eef | 2015-06-10 10:09:32 +1000 | [diff] [blame] | 76 | mutex_unlock(&UFS_SB(sb)->s_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | return; |
| 78 | } |
| 79 | |
| 80 | cg = ufs_inotocg (ino); |
| 81 | bit = ufs_inotocgoff (ino); |
| 82 | ucpi = ufs_load_cylinder (sb, cg); |
| 83 | if (!ucpi) { |
Fabian Frederick | cdd9eef | 2015-06-10 10:09:32 +1000 | [diff] [blame] | 84 | mutex_unlock(&UFS_SB(sb)->s_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | return; |
| 86 | } |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 87 | ucg = ubh_get_ucg(UCPI_UBH(ucpi)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | if (!ufs_cg_chkmagic(sb, ucg)) |
| 89 | ufs_panic (sb, "ufs_free_fragments", "internal error, bad cg magic number"); |
| 90 | |
| 91 | ucg->cg_time = cpu_to_fs32(sb, get_seconds()); |
| 92 | |
| 93 | is_directory = S_ISDIR(inode->i_mode); |
| 94 | |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 95 | if (ubh_isclr (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | ufs_error(sb, "ufs_free_inode", "bit already cleared for inode %u", ino); |
| 97 | else { |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 98 | ubh_clrbit (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | if (ino < ucpi->c_irotor) |
| 100 | ucpi->c_irotor = ino; |
| 101 | fs32_add(sb, &ucg->cg_cs.cs_nifree, 1); |
Evgeniy Dushistov | ee3ffd6 | 2006-06-25 05:47:30 -0700 | [diff] [blame] | 102 | uspi->cs_total.cs_nifree++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | fs32_add(sb, &UFS_SB(sb)->fs_cs(cg).cs_nifree, 1); |
| 104 | |
| 105 | if (is_directory) { |
| 106 | fs32_sub(sb, &ucg->cg_cs.cs_ndir, 1); |
Evgeniy Dushistov | ee3ffd6 | 2006-06-25 05:47:30 -0700 | [diff] [blame] | 107 | uspi->cs_total.cs_ndir--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | fs32_sub(sb, &UFS_SB(sb)->fs_cs(cg).cs_ndir, 1); |
| 109 | } |
| 110 | } |
| 111 | |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 112 | ubh_mark_buffer_dirty (USPI_UBH(uspi)); |
| 113 | ubh_mark_buffer_dirty (UCPI_UBH(ucpi)); |
Christoph Hellwig | 9cb569d | 2010-08-11 17:06:24 +0200 | [diff] [blame] | 114 | if (sb->s_flags & MS_SYNCHRONOUS) |
| 115 | ubh_sync_block(UCPI_UBH(ucpi)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
Artem Bityutskiy | 9e9ad5f | 2012-07-12 16:28:08 +0300 | [diff] [blame] | 117 | ufs_mark_sb_dirty(sb); |
Fabian Frederick | cdd9eef | 2015-06-10 10:09:32 +1000 | [diff] [blame] | 118 | mutex_unlock(&UFS_SB(sb)->s_lock); |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 119 | UFSD("EXIT\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /* |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 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 |
| 127 | */ |
| 128 | static void ufs2_init_inodes_chunk(struct super_block *sb, |
| 129 | struct ufs_cg_private_info *ucpi, |
| 130 | struct ufs_cylinder_group *ucg) |
| 131 | { |
| 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; |
| 138 | |
| 139 | UFSD("ENTER cgno %d\n", ucpi->c_cgx); |
| 140 | |
| 141 | for (; beg < end; ++beg) { |
| 142 | bh = sb_getblk(sb, beg); |
| 143 | lock_buffer(bh); |
| 144 | memset(bh->b_data, 0, sb->s_blocksize); |
| 145 | set_buffer_uptodate(bh); |
| 146 | mark_buffer_dirty(bh); |
| 147 | unlock_buffer(bh); |
| 148 | if (sb->s_flags & MS_SYNCHRONOUS) |
| 149 | sync_dirty_buffer(bh); |
| 150 | brelse(bh); |
| 151 | } |
| 152 | |
| 153 | fs32_add(sb, &ucg->cg_u.cg_u2.cg_initediblk, uspi->s_inopb); |
| 154 | ubh_mark_buffer_dirty(UCPI_UBH(ucpi)); |
Christoph Hellwig | 9cb569d | 2010-08-11 17:06:24 +0200 | [diff] [blame] | 155 | if (sb->s_flags & MS_SYNCHRONOUS) |
| 156 | ubh_sync_block(UCPI_UBH(ucpi)); |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 157 | |
| 158 | UFSD("EXIT\n"); |
| 159 | } |
| 160 | |
| 161 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
| 167 | * |
| 168 | * For other inodes, search forward from the parent directory's block |
| 169 | * group to find a free inode. |
| 170 | */ |
Al Viro | 6a9a06d | 2011-07-26 02:49:13 -0400 | [diff] [blame] | 171 | struct inode *ufs_new_inode(struct inode *dir, umode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | { |
| 173 | struct super_block * sb; |
| 174 | struct ufs_sb_info * sbi; |
| 175 | struct ufs_sb_private_info * uspi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | struct ufs_cg_private_info * ucpi; |
| 177 | struct ufs_cylinder_group * ucg; |
| 178 | struct inode * inode; |
Deepa Dinamani | a88e99e | 2017-05-08 15:59:22 -0700 | [diff] [blame] | 179 | struct timespec64 ts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | unsigned cg, bit, i, j, start; |
| 181 | struct ufs_inode_info *ufsi; |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 182 | int err = -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 184 | UFSD("ENTER\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
| 186 | /* Cannot create files in a deleted directory */ |
| 187 | if (!dir || !dir->i_nlink) |
| 188 | return ERR_PTR(-EPERM); |
| 189 | sb = dir->i_sb; |
| 190 | inode = new_inode(sb); |
| 191 | if (!inode) |
| 192 | return ERR_PTR(-ENOMEM); |
| 193 | ufsi = UFS_I(inode); |
| 194 | sbi = UFS_SB(sb); |
| 195 | uspi = sbi->s_uspi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | |
Fabian Frederick | cdd9eef | 2015-06-10 10:09:32 +1000 | [diff] [blame] | 197 | mutex_lock(&sbi->s_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | |
| 199 | /* |
| 200 | * Try to place the inode in its parent directory |
| 201 | */ |
| 202 | i = ufs_inotocg(dir->i_ino); |
| 203 | if (sbi->fs_cs(i).cs_nifree) { |
| 204 | cg = i; |
| 205 | goto cg_found; |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | * Use a quadratic hash to find a group with a free inode |
| 210 | */ |
| 211 | for ( j = 1; j < uspi->s_ncg; j <<= 1 ) { |
| 212 | i += j; |
| 213 | if (i >= uspi->s_ncg) |
| 214 | i -= uspi->s_ncg; |
| 215 | if (sbi->fs_cs(i).cs_nifree) { |
| 216 | cg = i; |
| 217 | goto cg_found; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /* |
| 222 | * That failed: try linear search for a free inode |
| 223 | */ |
| 224 | i = ufs_inotocg(dir->i_ino) + 1; |
| 225 | for (j = 2; j < uspi->s_ncg; j++) { |
| 226 | i++; |
| 227 | if (i >= uspi->s_ncg) |
| 228 | i = 0; |
| 229 | if (sbi->fs_cs(i).cs_nifree) { |
| 230 | cg = i; |
| 231 | goto cg_found; |
| 232 | } |
| 233 | } |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 234 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | goto failed; |
| 236 | |
| 237 | cg_found: |
| 238 | ucpi = ufs_load_cylinder (sb, cg); |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 239 | if (!ucpi) { |
| 240 | err = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | goto failed; |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 242 | } |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 243 | ucg = ubh_get_ucg(UCPI_UBH(ucpi)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | if (!ufs_cg_chkmagic(sb, ucg)) |
| 245 | ufs_panic (sb, "ufs_new_inode", "internal error, bad cg magic number"); |
| 246 | |
| 247 | start = ucpi->c_irotor; |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 248 | 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] | 249 | if (!(bit < uspi->s_ipg)) { |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 250 | 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] | 251 | if (!(bit < start)) { |
| 252 | ufs_error (sb, "ufs_new_inode", |
| 253 | "cylinder group %u corrupted - error in inode bitmap\n", cg); |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 254 | err = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | goto failed; |
| 256 | } |
| 257 | } |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 258 | 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] | 259 | if (ubh_isclr (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit)) |
| 260 | ubh_setbit (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | else { |
| 262 | ufs_panic (sb, "ufs_new_inode", "internal error"); |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 263 | err = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | goto failed; |
| 265 | } |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 266 | |
| 267 | if (uspi->fs_magic == UFS2_MAGIC) { |
| 268 | u32 initediblk = fs32_to_cpu(sb, ucg->cg_u.cg_u2.cg_initediblk); |
| 269 | |
| 270 | if (bit + uspi->s_inopb > initediblk && |
| 271 | initediblk < fs32_to_cpu(sb, ucg->cg_u.cg_u2.cg_niblk)) |
| 272 | ufs2_init_inodes_chunk(sb, ucpi, ucg); |
| 273 | } |
| 274 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | fs32_sub(sb, &ucg->cg_cs.cs_nifree, 1); |
Evgeniy Dushistov | ee3ffd6 | 2006-06-25 05:47:30 -0700 | [diff] [blame] | 276 | uspi->cs_total.cs_nifree--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | fs32_sub(sb, &sbi->fs_cs(cg).cs_nifree, 1); |
| 278 | |
| 279 | if (S_ISDIR(mode)) { |
| 280 | fs32_add(sb, &ucg->cg_cs.cs_ndir, 1); |
Evgeniy Dushistov | ee3ffd6 | 2006-06-25 05:47:30 -0700 | [diff] [blame] | 281 | uspi->cs_total.cs_ndir++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | fs32_add(sb, &sbi->fs_cs(cg).cs_ndir, 1); |
| 283 | } |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 284 | ubh_mark_buffer_dirty (USPI_UBH(uspi)); |
| 285 | ubh_mark_buffer_dirty (UCPI_UBH(ucpi)); |
Christoph Hellwig | 9cb569d | 2010-08-11 17:06:24 +0200 | [diff] [blame] | 286 | if (sb->s_flags & MS_SYNCHRONOUS) |
| 287 | ubh_sync_block(UCPI_UBH(ucpi)); |
Artem Bityutskiy | 9e9ad5f | 2012-07-12 16:28:08 +0300 | [diff] [blame] | 288 | ufs_mark_sb_dirty(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 290 | inode->i_ino = cg * uspi->s_ipg + bit; |
Dmitry Monakhov | be8ded5 | 2010-03-04 17:32:23 +0300 | [diff] [blame] | 291 | inode_init_owner(inode, dir, mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | inode->i_blocks = 0; |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 293 | inode->i_generation = 0; |
Deepa Dinamani | 02027d4 | 2016-09-14 07:48:05 -0700 | [diff] [blame] | 294 | inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | ufsi->i_flags = UFS_I(dir)->i_flags; |
| 296 | ufsi->i_lastfrag = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | ufsi->i_shadow = 0; |
| 298 | ufsi->i_osync = 0; |
| 299 | ufsi->i_oeftflag = 0; |
Evgeniy Dushistov | dd187a2 | 2006-06-25 05:47:25 -0700 | [diff] [blame] | 300 | ufsi->i_dir_start_lookup = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | memset(&ufsi->i_u1, 0, sizeof(ufsi->i_u1)); |
Al Viro | e4502c6 | 2014-09-26 21:17:52 -0400 | [diff] [blame] | 302 | if (insert_inode_locked(inode) < 0) { |
| 303 | err = -EIO; |
| 304 | goto failed; |
| 305 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | mark_inode_dirty(inode); |
| 307 | |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 308 | if (uspi->fs_magic == UFS2_MAGIC) { |
| 309 | struct buffer_head *bh; |
| 310 | struct ufs2_inode *ufs2_inode; |
| 311 | |
| 312 | /* |
| 313 | * setup birth date, we do it here because of there is no sense |
| 314 | * to hold it in struct ufs_inode_info, and lose 64 bit |
| 315 | */ |
| 316 | bh = sb_bread(sb, uspi->s_sbbase + ufs_inotofsba(inode->i_ino)); |
| 317 | if (!bh) { |
| 318 | ufs_warning(sb, "ufs_read_inode", |
| 319 | "unable to read inode %lu\n", |
| 320 | inode->i_ino); |
| 321 | err = -EIO; |
| 322 | goto fail_remove_inode; |
| 323 | } |
| 324 | lock_buffer(bh); |
| 325 | ufs2_inode = (struct ufs2_inode *)bh->b_data; |
| 326 | ufs2_inode += ufs_inotofsbo(inode->i_ino); |
Deepa Dinamani | a88e99e | 2017-05-08 15:59:22 -0700 | [diff] [blame] | 327 | ktime_get_real_ts64(&ts); |
| 328 | ufs2_inode->ui_birthtime = cpu_to_fs64(sb, ts.tv_sec); |
| 329 | ufs2_inode->ui_birthnsec = cpu_to_fs32(sb, ts.tv_nsec); |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 330 | mark_buffer_dirty(bh); |
| 331 | unlock_buffer(bh); |
| 332 | if (sb->s_flags & MS_SYNCHRONOUS) |
| 333 | sync_dirty_buffer(bh); |
| 334 | brelse(bh); |
| 335 | } |
Fabian Frederick | cdd9eef | 2015-06-10 10:09:32 +1000 | [diff] [blame] | 336 | mutex_unlock(&sbi->s_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 338 | UFSD("allocating inode %lu\n", inode->i_ino); |
| 339 | UFSD("EXIT\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | return inode; |
| 341 | |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 342 | fail_remove_inode: |
Fabian Frederick | cdd9eef | 2015-06-10 10:09:32 +1000 | [diff] [blame] | 343 | mutex_unlock(&sbi->s_lock); |
Miklos Szeredi | 6d6b77f | 2011-10-28 14:13:28 +0200 | [diff] [blame] | 344 | clear_nlink(inode); |
Al Viro | e4502c6 | 2014-09-26 21:17:52 -0400 | [diff] [blame] | 345 | unlock_new_inode(inode); |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 346 | iput(inode); |
| 347 | UFSD("EXIT (FAILED): err %d\n", err); |
| 348 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | failed: |
Fabian Frederick | cdd9eef | 2015-06-10 10:09:32 +1000 | [diff] [blame] | 350 | mutex_unlock(&sbi->s_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | make_bad_inode(inode); |
| 352 | iput (inode); |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 353 | UFSD("EXIT (FAILED): err %d\n", err); |
| 354 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | } |