Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * linux/fs/ufs/ialloc.c |
| 4 | * |
| 5 | * Copyright (c) 1998 |
| 6 | * Daniel Pirkl <daniel.pirkl@email.cz> |
| 7 | * Charles University, Faculty of Mathematics and Physics |
| 8 | * |
| 9 | * from |
| 10 | * |
| 11 | * linux/fs/ext2/ialloc.c |
| 12 | * |
| 13 | * Copyright (C) 1992, 1993, 1994, 1995 |
| 14 | * Remy Card (card@masi.ibp.fr) |
| 15 | * Laboratoire MASI - Institut Blaise Pascal |
| 16 | * Universite Pierre et Marie Curie (Paris VI) |
| 17 | * |
| 18 | * BSD ufs-inspired inode and directory allocation by |
| 19 | * Stephen Tweedie (sct@dcs.ed.ac.uk), 1993 |
| 20 | * Big-endian to little-endian byte-swapping/bitmaps by |
| 21 | * David S. Miller (davem@caip.rutgers.edu), 1995 |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 22 | * |
| 23 | * UFS2 write support added by |
| 24 | * Evgeniy Dushistov <dushistov@mail.ru>, 2007 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | */ |
| 26 | |
| 27 | #include <linux/fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/time.h> |
| 29 | #include <linux/stat.h> |
| 30 | #include <linux/string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <linux/buffer_head.h> |
| 32 | #include <linux/sched.h> |
| 33 | #include <linux/bitops.h> |
| 34 | #include <asm/byteorder.h> |
| 35 | |
Mike Frysinger | e542059 | 2008-02-08 04:21:31 -0800 | [diff] [blame] | 36 | #include "ufs_fs.h" |
Christoph Hellwig | bcd6d4e | 2007-10-16 23:26:51 -0700 | [diff] [blame] | 37 | #include "ufs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include "swab.h" |
| 39 | #include "util.h" |
| 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | /* |
| 42 | * NOTE! When we get the inode, we're the only people |
| 43 | * that have access to it, and as such there are no |
| 44 | * race conditions we have to worry about. The inode |
| 45 | * is not on the hash-lists, and it cannot be reached |
| 46 | * through the filesystem because the directory entry |
| 47 | * has been deleted earlier. |
| 48 | * |
| 49 | * HOWEVER: we must make sure that we get no aliases, |
| 50 | * which means that we have to call "clear_inode()" |
| 51 | * _before_ we mark the inode not in use in the inode |
| 52 | * bitmaps. Otherwise a newly created file might use |
| 53 | * the same inode number (not actually the same pointer |
| 54 | * though), and then we'd have two inodes sharing the |
| 55 | * same inode number and space on the harddisk. |
| 56 | */ |
| 57 | void ufs_free_inode (struct inode * inode) |
| 58 | { |
| 59 | struct super_block * sb; |
| 60 | struct ufs_sb_private_info * uspi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | struct ufs_cg_private_info * ucpi; |
| 62 | struct ufs_cylinder_group * ucg; |
| 63 | int is_directory; |
| 64 | unsigned ino, cg, bit; |
| 65 | |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 66 | UFSD("ENTER, ino %lu\n", inode->i_ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
| 68 | sb = inode->i_sb; |
| 69 | uspi = UFS_SB(sb)->s_uspi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
| 71 | ino = inode->i_ino; |
| 72 | |
Fabian Frederick | cdd9eef | 2015-06-10 10:09:32 +1000 | [diff] [blame] | 73 | mutex_lock(&UFS_SB(sb)->s_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
| 75 | if (!((ino > 1) && (ino < (uspi->s_ncg * uspi->s_ipg )))) { |
| 76 | 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] | 77 | mutex_unlock(&UFS_SB(sb)->s_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | return; |
| 79 | } |
| 80 | |
| 81 | cg = ufs_inotocg (ino); |
| 82 | bit = ufs_inotocgoff (ino); |
| 83 | ucpi = ufs_load_cylinder (sb, cg); |
| 84 | if (!ucpi) { |
Fabian Frederick | cdd9eef | 2015-06-10 10:09:32 +1000 | [diff] [blame] | 85 | mutex_unlock(&UFS_SB(sb)->s_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | return; |
| 87 | } |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 88 | ucg = ubh_get_ucg(UCPI_UBH(ucpi)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | if (!ufs_cg_chkmagic(sb, ucg)) |
| 90 | ufs_panic (sb, "ufs_free_fragments", "internal error, bad cg magic number"); |
| 91 | |
Arnd Bergmann | a3fda0f | 2018-08-17 15:43:47 -0700 | [diff] [blame] | 92 | ucg->cg_time = ufs_get_seconds(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
| 94 | is_directory = S_ISDIR(inode->i_mode); |
| 95 | |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 96 | if (ubh_isclr (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | ufs_error(sb, "ufs_free_inode", "bit already cleared for inode %u", ino); |
| 98 | else { |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 99 | ubh_clrbit (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | if (ino < ucpi->c_irotor) |
| 101 | ucpi->c_irotor = ino; |
| 102 | fs32_add(sb, &ucg->cg_cs.cs_nifree, 1); |
Evgeniy Dushistov | ee3ffd6 | 2006-06-25 05:47:30 -0700 | [diff] [blame] | 103 | uspi->cs_total.cs_nifree++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | fs32_add(sb, &UFS_SB(sb)->fs_cs(cg).cs_nifree, 1); |
| 105 | |
| 106 | if (is_directory) { |
| 107 | fs32_sub(sb, &ucg->cg_cs.cs_ndir, 1); |
Evgeniy Dushistov | ee3ffd6 | 2006-06-25 05:47:30 -0700 | [diff] [blame] | 108 | uspi->cs_total.cs_ndir--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | fs32_sub(sb, &UFS_SB(sb)->fs_cs(cg).cs_ndir, 1); |
| 110 | } |
| 111 | } |
| 112 | |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 113 | ubh_mark_buffer_dirty (USPI_UBH(uspi)); |
| 114 | ubh_mark_buffer_dirty (UCPI_UBH(ucpi)); |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 115 | if (sb->s_flags & SB_SYNCHRONOUS) |
Christoph Hellwig | 9cb569d | 2010-08-11 17:06:24 +0200 | [diff] [blame] | 116 | ubh_sync_block(UCPI_UBH(ucpi)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
Artem Bityutskiy | 9e9ad5f | 2012-07-12 16:28:08 +0300 | [diff] [blame] | 118 | ufs_mark_sb_dirty(sb); |
Fabian Frederick | cdd9eef | 2015-06-10 10:09:32 +1000 | [diff] [blame] | 119 | mutex_unlock(&UFS_SB(sb)->s_lock); |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 120 | UFSD("EXIT\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /* |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 124 | * Nullify new chunk of inodes, |
| 125 | * BSD people also set ui_gen field of inode |
| 126 | * during nullification, but we not care about |
| 127 | * that because of linux ufs do not support NFS |
| 128 | */ |
| 129 | static void ufs2_init_inodes_chunk(struct super_block *sb, |
| 130 | struct ufs_cg_private_info *ucpi, |
| 131 | struct ufs_cylinder_group *ucg) |
| 132 | { |
| 133 | struct buffer_head *bh; |
| 134 | struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi; |
| 135 | sector_t beg = uspi->s_sbbase + |
| 136 | ufs_inotofsba(ucpi->c_cgx * uspi->s_ipg + |
| 137 | fs32_to_cpu(sb, ucg->cg_u.cg_u2.cg_initediblk)); |
| 138 | sector_t end = beg + uspi->s_fpb; |
| 139 | |
| 140 | UFSD("ENTER cgno %d\n", ucpi->c_cgx); |
| 141 | |
| 142 | for (; beg < end; ++beg) { |
| 143 | bh = sb_getblk(sb, beg); |
| 144 | lock_buffer(bh); |
| 145 | memset(bh->b_data, 0, sb->s_blocksize); |
| 146 | set_buffer_uptodate(bh); |
| 147 | mark_buffer_dirty(bh); |
| 148 | unlock_buffer(bh); |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 149 | if (sb->s_flags & SB_SYNCHRONOUS) |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 150 | sync_dirty_buffer(bh); |
| 151 | brelse(bh); |
| 152 | } |
| 153 | |
| 154 | fs32_add(sb, &ucg->cg_u.cg_u2.cg_initediblk, uspi->s_inopb); |
| 155 | ubh_mark_buffer_dirty(UCPI_UBH(ucpi)); |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 156 | if (sb->s_flags & SB_SYNCHRONOUS) |
Christoph Hellwig | 9cb569d | 2010-08-11 17:06:24 +0200 | [diff] [blame] | 157 | ubh_sync_block(UCPI_UBH(ucpi)); |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 158 | |
| 159 | UFSD("EXIT\n"); |
| 160 | } |
| 161 | |
| 162 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | * There are two policies for allocating an inode. If the new inode is |
| 164 | * a directory, then a forward search is made for a block group with both |
| 165 | * free space and a low directory-to-inode ratio; if that fails, then of |
| 166 | * the groups with above-average free space, that group with the fewest |
| 167 | * directories already is chosen. |
| 168 | * |
| 169 | * For other inodes, search forward from the parent directory's block |
| 170 | * group to find a free inode. |
| 171 | */ |
Al Viro | 6a9a06d | 2011-07-26 02:49:13 -0400 | [diff] [blame] | 172 | struct inode *ufs_new_inode(struct inode *dir, umode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | { |
| 174 | struct super_block * sb; |
| 175 | struct ufs_sb_info * sbi; |
| 176 | struct ufs_sb_private_info * uspi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | struct ufs_cg_private_info * ucpi; |
| 178 | struct ufs_cylinder_group * ucg; |
| 179 | struct inode * inode; |
Deepa Dinamani | a88e99e | 2017-05-08 15:59:22 -0700 | [diff] [blame] | 180 | struct timespec64 ts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | unsigned cg, bit, i, j, start; |
| 182 | struct ufs_inode_info *ufsi; |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 183 | int err = -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 185 | UFSD("ENTER\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | |
| 187 | /* Cannot create files in a deleted directory */ |
| 188 | if (!dir || !dir->i_nlink) |
| 189 | return ERR_PTR(-EPERM); |
| 190 | sb = dir->i_sb; |
| 191 | inode = new_inode(sb); |
| 192 | if (!inode) |
| 193 | return ERR_PTR(-ENOMEM); |
| 194 | ufsi = UFS_I(inode); |
| 195 | sbi = UFS_SB(sb); |
| 196 | uspi = sbi->s_uspi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
Fabian Frederick | cdd9eef | 2015-06-10 10:09:32 +1000 | [diff] [blame] | 198 | mutex_lock(&sbi->s_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
| 200 | /* |
| 201 | * Try to place the inode in its parent directory |
| 202 | */ |
| 203 | i = ufs_inotocg(dir->i_ino); |
| 204 | if (sbi->fs_cs(i).cs_nifree) { |
| 205 | cg = i; |
| 206 | goto cg_found; |
| 207 | } |
| 208 | |
| 209 | /* |
| 210 | * Use a quadratic hash to find a group with a free inode |
| 211 | */ |
| 212 | for ( j = 1; j < uspi->s_ncg; j <<= 1 ) { |
| 213 | i += j; |
| 214 | if (i >= uspi->s_ncg) |
| 215 | i -= uspi->s_ncg; |
| 216 | if (sbi->fs_cs(i).cs_nifree) { |
| 217 | cg = i; |
| 218 | goto cg_found; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | /* |
| 223 | * That failed: try linear search for a free inode |
| 224 | */ |
| 225 | i = ufs_inotocg(dir->i_ino) + 1; |
| 226 | for (j = 2; j < uspi->s_ncg; j++) { |
| 227 | i++; |
| 228 | if (i >= uspi->s_ncg) |
| 229 | i = 0; |
| 230 | if (sbi->fs_cs(i).cs_nifree) { |
| 231 | cg = i; |
| 232 | goto cg_found; |
| 233 | } |
| 234 | } |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 235 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | goto failed; |
| 237 | |
| 238 | cg_found: |
| 239 | ucpi = ufs_load_cylinder (sb, cg); |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 240 | if (!ucpi) { |
| 241 | err = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | goto failed; |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 243 | } |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 244 | ucg = ubh_get_ucg(UCPI_UBH(ucpi)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | if (!ufs_cg_chkmagic(sb, ucg)) |
| 246 | ufs_panic (sb, "ufs_new_inode", "internal error, bad cg magic number"); |
| 247 | |
| 248 | start = ucpi->c_irotor; |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 249 | 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] | 250 | if (!(bit < uspi->s_ipg)) { |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 251 | 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] | 252 | if (!(bit < start)) { |
| 253 | ufs_error (sb, "ufs_new_inode", |
| 254 | "cylinder group %u corrupted - error in inode bitmap\n", cg); |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 255 | err = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | goto failed; |
| 257 | } |
| 258 | } |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 259 | 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] | 260 | if (ubh_isclr (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit)) |
| 261 | ubh_setbit (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | else { |
| 263 | ufs_panic (sb, "ufs_new_inode", "internal error"); |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 264 | err = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | goto failed; |
| 266 | } |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 267 | |
| 268 | if (uspi->fs_magic == UFS2_MAGIC) { |
| 269 | u32 initediblk = fs32_to_cpu(sb, ucg->cg_u.cg_u2.cg_initediblk); |
| 270 | |
| 271 | if (bit + uspi->s_inopb > initediblk && |
| 272 | initediblk < fs32_to_cpu(sb, ucg->cg_u.cg_u2.cg_niblk)) |
| 273 | ufs2_init_inodes_chunk(sb, ucpi, ucg); |
| 274 | } |
| 275 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | fs32_sub(sb, &ucg->cg_cs.cs_nifree, 1); |
Evgeniy Dushistov | ee3ffd6 | 2006-06-25 05:47:30 -0700 | [diff] [blame] | 277 | uspi->cs_total.cs_nifree--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | fs32_sub(sb, &sbi->fs_cs(cg).cs_nifree, 1); |
| 279 | |
| 280 | if (S_ISDIR(mode)) { |
| 281 | fs32_add(sb, &ucg->cg_cs.cs_ndir, 1); |
Evgeniy Dushistov | ee3ffd6 | 2006-06-25 05:47:30 -0700 | [diff] [blame] | 282 | uspi->cs_total.cs_ndir++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | fs32_add(sb, &sbi->fs_cs(cg).cs_ndir, 1); |
| 284 | } |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 285 | ubh_mark_buffer_dirty (USPI_UBH(uspi)); |
| 286 | ubh_mark_buffer_dirty (UCPI_UBH(ucpi)); |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 287 | if (sb->s_flags & SB_SYNCHRONOUS) |
Christoph Hellwig | 9cb569d | 2010-08-11 17:06:24 +0200 | [diff] [blame] | 288 | ubh_sync_block(UCPI_UBH(ucpi)); |
Artem Bityutskiy | 9e9ad5f | 2012-07-12 16:28:08 +0300 | [diff] [blame] | 289 | ufs_mark_sb_dirty(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 291 | inode->i_ino = cg * uspi->s_ipg + bit; |
Christian Brauner | 21cb47b | 2021-01-21 14:19:25 +0100 | [diff] [blame] | 292 | inode_init_owner(&init_user_ns, inode, dir, mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | inode->i_blocks = 0; |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 294 | inode->i_generation = 0; |
Deepa Dinamani | 02027d4 | 2016-09-14 07:48:05 -0700 | [diff] [blame] | 295 | inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | ufsi->i_flags = UFS_I(dir)->i_flags; |
| 297 | ufsi->i_lastfrag = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | ufsi->i_shadow = 0; |
| 299 | ufsi->i_osync = 0; |
| 300 | ufsi->i_oeftflag = 0; |
Evgeniy Dushistov | dd187a2 | 2006-06-25 05:47:25 -0700 | [diff] [blame] | 301 | ufsi->i_dir_start_lookup = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | memset(&ufsi->i_u1, 0, sizeof(ufsi->i_u1)); |
Al Viro | e4502c6 | 2014-09-26 21:17:52 -0400 | [diff] [blame] | 303 | if (insert_inode_locked(inode) < 0) { |
| 304 | err = -EIO; |
| 305 | goto failed; |
| 306 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | mark_inode_dirty(inode); |
| 308 | |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 309 | if (uspi->fs_magic == UFS2_MAGIC) { |
| 310 | struct buffer_head *bh; |
| 311 | struct ufs2_inode *ufs2_inode; |
| 312 | |
| 313 | /* |
| 314 | * setup birth date, we do it here because of there is no sense |
| 315 | * to hold it in struct ufs_inode_info, and lose 64 bit |
| 316 | */ |
| 317 | bh = sb_bread(sb, uspi->s_sbbase + ufs_inotofsba(inode->i_ino)); |
| 318 | if (!bh) { |
| 319 | ufs_warning(sb, "ufs_read_inode", |
| 320 | "unable to read inode %lu\n", |
| 321 | inode->i_ino); |
| 322 | err = -EIO; |
| 323 | goto fail_remove_inode; |
| 324 | } |
| 325 | lock_buffer(bh); |
| 326 | ufs2_inode = (struct ufs2_inode *)bh->b_data; |
| 327 | ufs2_inode += ufs_inotofsbo(inode->i_ino); |
Deepa Dinamani | a88e99e | 2017-05-08 15:59:22 -0700 | [diff] [blame] | 328 | ktime_get_real_ts64(&ts); |
| 329 | ufs2_inode->ui_birthtime = cpu_to_fs64(sb, ts.tv_sec); |
| 330 | ufs2_inode->ui_birthnsec = cpu_to_fs32(sb, ts.tv_nsec); |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 331 | mark_buffer_dirty(bh); |
| 332 | unlock_buffer(bh); |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 333 | if (sb->s_flags & SB_SYNCHRONOUS) |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 334 | sync_dirty_buffer(bh); |
| 335 | brelse(bh); |
| 336 | } |
Fabian Frederick | cdd9eef | 2015-06-10 10:09:32 +1000 | [diff] [blame] | 337 | mutex_unlock(&sbi->s_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 339 | UFSD("allocating inode %lu\n", inode->i_ino); |
| 340 | UFSD("EXIT\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | return inode; |
| 342 | |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 343 | fail_remove_inode: |
Fabian Frederick | cdd9eef | 2015-06-10 10:09:32 +1000 | [diff] [blame] | 344 | mutex_unlock(&sbi->s_lock); |
Miklos Szeredi | 6d6b77f | 2011-10-28 14:13:28 +0200 | [diff] [blame] | 345 | clear_nlink(inode); |
Al Viro | dd54992 | 2018-05-16 12:22:50 -0400 | [diff] [blame] | 346 | discard_new_inode(inode); |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 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 | } |