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/affs/inode.c |
| 4 | * |
| 5 | * (c) 1996 Hans-Joachim Widmaier - Rewritten |
| 6 | * |
| 7 | * (C) 1993 Ray Burr - Modified for Amiga FFS filesystem. |
| 8 | * |
| 9 | * (C) 1992 Eric Youngdale Modified for ISO9660 filesystem. |
| 10 | * |
| 11 | * (C) 1991 Linus Torvalds - minix filesystem |
| 12 | */ |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 13 | #include <linux/sched.h> |
Ingo Molnar | 5b825c3 | 2017-02-02 17:54:15 +0100 | [diff] [blame] | 14 | #include <linux/cred.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include "affs.h" |
| 17 | |
David Howells | 210f855 | 2008-02-07 00:15:29 -0800 | [diff] [blame] | 18 | struct inode *affs_iget(struct super_block *sb, unsigned long ino) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | struct affs_sb_info *sbi = AFFS_SB(sb); |
| 21 | struct buffer_head *bh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | struct affs_tail *tail; |
David Howells | 210f855 | 2008-02-07 00:15:29 -0800 | [diff] [blame] | 23 | struct inode *inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | u32 block; |
| 25 | u32 size; |
| 26 | u32 prot; |
| 27 | u16 id; |
| 28 | |
David Howells | 210f855 | 2008-02-07 00:15:29 -0800 | [diff] [blame] | 29 | inode = iget_locked(sb, ino); |
| 30 | if (!inode) |
| 31 | return ERR_PTR(-ENOMEM); |
| 32 | if (!(inode->i_state & I_NEW)) |
| 33 | return inode; |
| 34 | |
Fabian Frederick | 9606d9a | 2014-06-06 14:37:25 -0700 | [diff] [blame] | 35 | pr_debug("affs_iget(%lu)\n", inode->i_ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
| 37 | block = inode->i_ino; |
| 38 | bh = affs_bread(sb, block); |
| 39 | if (!bh) { |
| 40 | affs_warning(sb, "read_inode", "Cannot read block %d", block); |
| 41 | goto bad_inode; |
| 42 | } |
| 43 | if (affs_checksum_block(sb, bh) || be32_to_cpu(AFFS_HEAD(bh)->ptype) != T_SHORT) { |
| 44 | affs_warning(sb,"read_inode", |
| 45 | "Checksum or type (ptype=%d) error on inode %d", |
| 46 | AFFS_HEAD(bh)->ptype, block); |
| 47 | goto bad_inode; |
| 48 | } |
| 49 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | tail = AFFS_TAIL(sb, bh); |
| 51 | prot = be32_to_cpu(tail->protect); |
| 52 | |
| 53 | inode->i_size = 0; |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 54 | set_nlink(inode, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | inode->i_mode = 0; |
| 56 | AFFS_I(inode)->i_extcnt = 1; |
| 57 | AFFS_I(inode)->i_ext_last = ~1; |
| 58 | AFFS_I(inode)->i_protect = prot; |
Roman Zippel | dca3c33 | 2008-04-29 17:02:20 +0200 | [diff] [blame] | 59 | atomic_set(&AFFS_I(inode)->i_opencnt, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | AFFS_I(inode)->i_blkcnt = 0; |
| 61 | AFFS_I(inode)->i_lc = NULL; |
| 62 | AFFS_I(inode)->i_lc_size = 0; |
| 63 | AFFS_I(inode)->i_lc_shift = 0; |
| 64 | AFFS_I(inode)->i_lc_mask = 0; |
| 65 | AFFS_I(inode)->i_ac = NULL; |
| 66 | AFFS_I(inode)->i_ext_bh = NULL; |
| 67 | AFFS_I(inode)->mmu_private = 0; |
| 68 | AFFS_I(inode)->i_lastalloc = 0; |
| 69 | AFFS_I(inode)->i_pa_cnt = 0; |
| 70 | |
Fabian Frederick | 79bda4d | 2015-04-16 12:48:24 -0700 | [diff] [blame] | 71 | if (affs_test_opt(sbi->s_flags, SF_SETMODE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | inode->i_mode = sbi->s_mode; |
| 73 | else |
Fabian Frederick | c161820 | 2017-02-27 14:27:54 -0800 | [diff] [blame] | 74 | inode->i_mode = affs_prot_to_mode(prot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
| 76 | id = be16_to_cpu(tail->uid); |
Fabian Frederick | 79bda4d | 2015-04-16 12:48:24 -0700 | [diff] [blame] | 77 | if (id == 0 || affs_test_opt(sbi->s_flags, SF_SETUID)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | inode->i_uid = sbi->s_uid; |
Fabian Frederick | 79bda4d | 2015-04-16 12:48:24 -0700 | [diff] [blame] | 79 | else if (id == 0xFFFF && affs_test_opt(sbi->s_flags, SF_MUFS)) |
Eric W. Biederman | 8fed10b | 2012-02-07 16:20:16 -0800 | [diff] [blame] | 80 | i_uid_write(inode, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | else |
Eric W. Biederman | 8fed10b | 2012-02-07 16:20:16 -0800 | [diff] [blame] | 82 | i_uid_write(inode, id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
| 84 | id = be16_to_cpu(tail->gid); |
Fabian Frederick | 79bda4d | 2015-04-16 12:48:24 -0700 | [diff] [blame] | 85 | if (id == 0 || affs_test_opt(sbi->s_flags, SF_SETGID)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | inode->i_gid = sbi->s_gid; |
Fabian Frederick | 79bda4d | 2015-04-16 12:48:24 -0700 | [diff] [blame] | 87 | else if (id == 0xFFFF && affs_test_opt(sbi->s_flags, SF_MUFS)) |
Eric W. Biederman | 8fed10b | 2012-02-07 16:20:16 -0800 | [diff] [blame] | 88 | i_gid_write(inode, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | else |
Eric W. Biederman | 8fed10b | 2012-02-07 16:20:16 -0800 | [diff] [blame] | 90 | i_gid_write(inode, id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
| 92 | switch (be32_to_cpu(tail->stype)) { |
| 93 | case ST_ROOT: |
| 94 | inode->i_uid = sbi->s_uid; |
| 95 | inode->i_gid = sbi->s_gid; |
| 96 | /* fall through */ |
| 97 | case ST_USERDIR: |
| 98 | if (be32_to_cpu(tail->stype) == ST_USERDIR || |
Fabian Frederick | 79bda4d | 2015-04-16 12:48:24 -0700 | [diff] [blame] | 99 | affs_test_opt(sbi->s_flags, SF_SETMODE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | if (inode->i_mode & S_IRUSR) |
| 101 | inode->i_mode |= S_IXUSR; |
| 102 | if (inode->i_mode & S_IRGRP) |
| 103 | inode->i_mode |= S_IXGRP; |
| 104 | if (inode->i_mode & S_IROTH) |
| 105 | inode->i_mode |= S_IXOTH; |
| 106 | inode->i_mode |= S_IFDIR; |
| 107 | } else |
| 108 | inode->i_mode = S_IRUGO | S_IXUGO | S_IWUSR | S_IFDIR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | /* Maybe it should be controlled by mount parameter? */ |
| 110 | //inode->i_mode |= S_ISVTX; |
| 111 | inode->i_op = &affs_dir_inode_operations; |
| 112 | inode->i_fop = &affs_dir_operations; |
| 113 | break; |
| 114 | case ST_LINKDIR: |
| 115 | #if 0 |
| 116 | affs_warning(sb, "read_inode", "inode is LINKDIR"); |
| 117 | goto bad_inode; |
| 118 | #else |
| 119 | inode->i_mode |= S_IFDIR; |
Al Viro | c765d47 | 2008-12-04 09:50:55 -0500 | [diff] [blame] | 120 | /* ... and leave ->i_op and ->i_fop pointing to empty */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | break; |
| 122 | #endif |
| 123 | case ST_LINKFILE: |
| 124 | affs_warning(sb, "read_inode", "inode is LINKFILE"); |
| 125 | goto bad_inode; |
| 126 | case ST_FILE: |
| 127 | size = be32_to_cpu(tail->size); |
| 128 | inode->i_mode |= S_IFREG; |
| 129 | AFFS_I(inode)->mmu_private = inode->i_size = size; |
| 130 | if (inode->i_size) { |
| 131 | AFFS_I(inode)->i_blkcnt = (size - 1) / |
| 132 | sbi->s_data_blksize + 1; |
| 133 | AFFS_I(inode)->i_extcnt = (AFFS_I(inode)->i_blkcnt - 1) / |
| 134 | sbi->s_hashsize + 1; |
| 135 | } |
| 136 | if (tail->link_chain) |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 137 | set_nlink(inode, 2); |
Fabian Frederick | 79bda4d | 2015-04-16 12:48:24 -0700 | [diff] [blame] | 138 | inode->i_mapping->a_ops = affs_test_opt(sbi->s_flags, SF_OFS) ? |
Fabian Frederick | a0016ff | 2015-04-16 12:48:15 -0700 | [diff] [blame] | 139 | &affs_aops_ofs : &affs_aops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | inode->i_op = &affs_file_inode_operations; |
| 141 | inode->i_fop = &affs_file_operations; |
| 142 | break; |
| 143 | case ST_SOFTLINK: |
Fabian Frederick | f1bf907 | 2017-04-15 08:54:36 +0200 | [diff] [blame] | 144 | inode->i_size = strlen((char *)AFFS_HEAD(bh)->table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | inode->i_mode |= S_IFLNK; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 146 | inode_nohighmem(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | inode->i_op = &affs_symlink_inode_operations; |
| 148 | inode->i_data.a_ops = &affs_symlink_aops; |
| 149 | break; |
| 150 | } |
| 151 | |
| 152 | inode->i_mtime.tv_sec = inode->i_atime.tv_sec = inode->i_ctime.tv_sec |
| 153 | = (be32_to_cpu(tail->change.days) * (24 * 60 * 60) + |
| 154 | be32_to_cpu(tail->change.mins) * 60 + |
| 155 | be32_to_cpu(tail->change.ticks) / 50 + |
| 156 | ((8 * 365 + 2) * 24 * 60 * 60)) + |
| 157 | sys_tz.tz_minuteswest * 60; |
| 158 | inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec = inode->i_atime.tv_nsec = 0; |
| 159 | affs_brelse(bh); |
David Howells | 210f855 | 2008-02-07 00:15:29 -0800 | [diff] [blame] | 160 | unlock_new_inode(inode); |
| 161 | return inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | |
| 163 | bad_inode: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | affs_brelse(bh); |
David Howells | 210f855 | 2008-02-07 00:15:29 -0800 | [diff] [blame] | 165 | iget_failed(inode); |
| 166 | return ERR_PTR(-EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | int |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 170 | affs_write_inode(struct inode *inode, struct writeback_control *wbc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | { |
| 172 | struct super_block *sb = inode->i_sb; |
| 173 | struct buffer_head *bh; |
| 174 | struct affs_tail *tail; |
| 175 | uid_t uid; |
| 176 | gid_t gid; |
| 177 | |
Fabian Frederick | 9606d9a | 2014-06-06 14:37:25 -0700 | [diff] [blame] | 178 | pr_debug("write_inode(%lu)\n", inode->i_ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
| 180 | if (!inode->i_nlink) |
| 181 | // possibly free block |
| 182 | return 0; |
| 183 | bh = affs_bread(sb, inode->i_ino); |
| 184 | if (!bh) { |
| 185 | affs_error(sb,"write_inode","Cannot read block %lu",inode->i_ino); |
| 186 | return -EIO; |
| 187 | } |
| 188 | tail = AFFS_TAIL(sb, bh); |
| 189 | if (tail->stype == cpu_to_be32(ST_ROOT)) { |
Fabian Frederick | c161820 | 2017-02-27 14:27:54 -0800 | [diff] [blame] | 190 | affs_secs_to_datestamp(inode->i_mtime.tv_sec, |
| 191 | &AFFS_ROOT_TAIL(sb, bh)->root_change); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | } else { |
| 193 | tail->protect = cpu_to_be32(AFFS_I(inode)->i_protect); |
| 194 | tail->size = cpu_to_be32(inode->i_size); |
Fabian Frederick | c161820 | 2017-02-27 14:27:54 -0800 | [diff] [blame] | 195 | affs_secs_to_datestamp(inode->i_mtime.tv_sec, &tail->change); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | if (!(inode->i_ino == AFFS_SB(sb)->s_root_block)) { |
Eric W. Biederman | 8fed10b | 2012-02-07 16:20:16 -0800 | [diff] [blame] | 197 | uid = i_uid_read(inode); |
| 198 | gid = i_gid_read(inode); |
Fabian Frederick | 79bda4d | 2015-04-16 12:48:24 -0700 | [diff] [blame] | 199 | if (affs_test_opt(AFFS_SB(sb)->s_flags, SF_MUFS)) { |
Eric W. Biederman | 8fed10b | 2012-02-07 16:20:16 -0800 | [diff] [blame] | 200 | if (uid == 0 || uid == 0xFFFF) |
| 201 | uid = uid ^ ~0; |
| 202 | if (gid == 0 || gid == 0xFFFF) |
| 203 | gid = gid ^ ~0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | } |
Fabian Frederick | 79bda4d | 2015-04-16 12:48:24 -0700 | [diff] [blame] | 205 | if (!affs_test_opt(AFFS_SB(sb)->s_flags, SF_SETUID)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | tail->uid = cpu_to_be16(uid); |
Fabian Frederick | 79bda4d | 2015-04-16 12:48:24 -0700 | [diff] [blame] | 207 | if (!affs_test_opt(AFFS_SB(sb)->s_flags, SF_SETGID)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | tail->gid = cpu_to_be16(gid); |
| 209 | } |
| 210 | } |
| 211 | affs_fix_checksum(sb, bh); |
| 212 | mark_buffer_dirty_inode(bh, inode); |
| 213 | affs_brelse(bh); |
| 214 | affs_free_prealloc(inode); |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | int |
| 219 | affs_notify_change(struct dentry *dentry, struct iattr *attr) |
| 220 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 221 | struct inode *inode = d_inode(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | int error; |
| 223 | |
Fabian Frederick | 9606d9a | 2014-06-06 14:37:25 -0700 | [diff] [blame] | 224 | pr_debug("notify_change(%lu,0x%x)\n", inode->i_ino, attr->ia_valid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | |
Jan Kara | 31051c8 | 2016-05-26 16:55:18 +0200 | [diff] [blame] | 226 | error = setattr_prepare(dentry, attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | if (error) |
| 228 | goto out; |
| 229 | |
Fabian Frederick | a0016ff | 2015-04-16 12:48:15 -0700 | [diff] [blame] | 230 | if (((attr->ia_valid & ATTR_UID) && |
Fabian Frederick | 79bda4d | 2015-04-16 12:48:24 -0700 | [diff] [blame] | 231 | affs_test_opt(AFFS_SB(inode->i_sb)->s_flags, SF_SETUID)) || |
Fabian Frederick | a0016ff | 2015-04-16 12:48:15 -0700 | [diff] [blame] | 232 | ((attr->ia_valid & ATTR_GID) && |
Fabian Frederick | 79bda4d | 2015-04-16 12:48:24 -0700 | [diff] [blame] | 233 | affs_test_opt(AFFS_SB(inode->i_sb)->s_flags, SF_SETGID)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | ((attr->ia_valid & ATTR_MODE) && |
Fabian Frederick | a0016ff | 2015-04-16 12:48:15 -0700 | [diff] [blame] | 235 | (AFFS_SB(inode->i_sb)->s_flags & |
| 236 | (AFFS_MOUNT_SF_SETMODE | AFFS_MOUNT_SF_IMMUTABLE)))) { |
Fabian Frederick | 79bda4d | 2015-04-16 12:48:24 -0700 | [diff] [blame] | 237 | if (!affs_test_opt(AFFS_SB(inode->i_sb)->s_flags, SF_QUIET)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | error = -EPERM; |
| 239 | goto out; |
| 240 | } |
| 241 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 242 | if ((attr->ia_valid & ATTR_SIZE) && |
| 243 | attr->ia_size != i_size_read(inode)) { |
Marco Stornelli | 1dc1834 | 2012-12-15 11:51:53 +0100 | [diff] [blame] | 244 | error = inode_newsize_ok(inode, attr->ia_size); |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 245 | if (error) |
| 246 | return error; |
Marco Stornelli | 1dc1834 | 2012-12-15 11:51:53 +0100 | [diff] [blame] | 247 | |
| 248 | truncate_setsize(inode, attr->ia_size); |
| 249 | affs_truncate(inode); |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | setattr_copy(inode, attr); |
| 253 | mark_inode_dirty(inode); |
| 254 | |
| 255 | if (attr->ia_valid & ATTR_MODE) |
Fabian Frederick | c161820 | 2017-02-27 14:27:54 -0800 | [diff] [blame] | 256 | affs_mode_to_prot(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | out: |
| 258 | return error; |
| 259 | } |
| 260 | |
| 261 | void |
Al Viro | f053ddd | 2010-06-06 10:16:41 -0400 | [diff] [blame] | 262 | affs_evict_inode(struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | { |
Roman Zippel | dca3c33 | 2008-04-29 17:02:20 +0200 | [diff] [blame] | 264 | unsigned long cache_page; |
Fabian Frederick | 9606d9a | 2014-06-06 14:37:25 -0700 | [diff] [blame] | 265 | pr_debug("evict_inode(ino=%lu, nlink=%u)\n", |
| 266 | inode->i_ino, inode->i_nlink); |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 267 | truncate_inode_pages_final(&inode->i_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | |
Al Viro | f053ddd | 2010-06-06 10:16:41 -0400 | [diff] [blame] | 269 | if (!inode->i_nlink) { |
| 270 | inode->i_size = 0; |
| 271 | affs_truncate(inode); |
| 272 | } |
Roman Zippel | dca3c33 | 2008-04-29 17:02:20 +0200 | [diff] [blame] | 273 | |
Al Viro | f053ddd | 2010-06-06 10:16:41 -0400 | [diff] [blame] | 274 | invalidate_inode_buffers(inode); |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 275 | clear_inode(inode); |
Roman Zippel | dca3c33 | 2008-04-29 17:02:20 +0200 | [diff] [blame] | 276 | affs_free_prealloc(inode); |
| 277 | cache_page = (unsigned long)AFFS_I(inode)->i_lc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | if (cache_page) { |
Fabian Frederick | 9606d9a | 2014-06-06 14:37:25 -0700 | [diff] [blame] | 279 | pr_debug("freeing ext cache\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | AFFS_I(inode)->i_lc = NULL; |
| 281 | AFFS_I(inode)->i_ac = NULL; |
| 282 | free_page(cache_page); |
| 283 | } |
| 284 | affs_brelse(AFFS_I(inode)->i_ext_bh); |
| 285 | AFFS_I(inode)->i_ext_last = ~1; |
| 286 | AFFS_I(inode)->i_ext_bh = NULL; |
Al Viro | f053ddd | 2010-06-06 10:16:41 -0400 | [diff] [blame] | 287 | |
| 288 | if (!inode->i_nlink) |
| 289 | affs_free_block(inode->i_sb, inode->i_ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | struct inode * |
| 293 | affs_new_inode(struct inode *dir) |
| 294 | { |
| 295 | struct super_block *sb = dir->i_sb; |
| 296 | struct inode *inode; |
| 297 | u32 block; |
| 298 | struct buffer_head *bh; |
| 299 | |
| 300 | if (!(inode = new_inode(sb))) |
| 301 | goto err_inode; |
| 302 | |
| 303 | if (!(block = affs_alloc_block(dir, dir->i_ino))) |
| 304 | goto err_block; |
| 305 | |
| 306 | bh = affs_getzeroblk(sb, block); |
| 307 | if (!bh) |
| 308 | goto err_bh; |
| 309 | mark_buffer_dirty_inode(bh, inode); |
| 310 | affs_brelse(bh); |
| 311 | |
David Howells | 2155998 | 2008-11-14 10:38:45 +1100 | [diff] [blame] | 312 | inode->i_uid = current_fsuid(); |
| 313 | inode->i_gid = current_fsgid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | inode->i_ino = block; |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 315 | set_nlink(inode, 1); |
Deepa Dinamani | 02027d4 | 2016-09-14 07:48:05 -0700 | [diff] [blame] | 316 | inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode); |
Roman Zippel | dca3c33 | 2008-04-29 17:02:20 +0200 | [diff] [blame] | 317 | atomic_set(&AFFS_I(inode)->i_opencnt, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | AFFS_I(inode)->i_blkcnt = 0; |
| 319 | AFFS_I(inode)->i_lc = NULL; |
| 320 | AFFS_I(inode)->i_lc_size = 0; |
| 321 | AFFS_I(inode)->i_lc_shift = 0; |
| 322 | AFFS_I(inode)->i_lc_mask = 0; |
| 323 | AFFS_I(inode)->i_ac = NULL; |
| 324 | AFFS_I(inode)->i_ext_bh = NULL; |
| 325 | AFFS_I(inode)->mmu_private = 0; |
| 326 | AFFS_I(inode)->i_protect = 0; |
| 327 | AFFS_I(inode)->i_lastalloc = 0; |
| 328 | AFFS_I(inode)->i_pa_cnt = 0; |
| 329 | AFFS_I(inode)->i_extcnt = 1; |
| 330 | AFFS_I(inode)->i_ext_last = ~1; |
| 331 | |
| 332 | insert_inode_hash(inode); |
| 333 | |
| 334 | return inode; |
| 335 | |
| 336 | err_bh: |
| 337 | affs_free_block(sb, block); |
| 338 | err_block: |
| 339 | iput(inode); |
| 340 | err_inode: |
| 341 | return NULL; |
| 342 | } |
| 343 | |
| 344 | /* |
| 345 | * Add an entry to a directory. Create the header block |
| 346 | * and insert it into the hash table. |
| 347 | */ |
| 348 | |
| 349 | int |
| 350 | affs_add_entry(struct inode *dir, struct inode *inode, struct dentry *dentry, s32 type) |
| 351 | { |
| 352 | struct super_block *sb = dir->i_sb; |
| 353 | struct buffer_head *inode_bh = NULL; |
Fabian Frederick | 78f444f | 2015-06-30 14:57:55 -0700 | [diff] [blame] | 354 | struct buffer_head *bh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | u32 block = 0; |
| 356 | int retval; |
| 357 | |
Geert Uytterhoeven | 08fe100 | 2015-02-17 13:46:10 -0800 | [diff] [blame] | 358 | pr_debug("%s(dir=%lu, inode=%lu, \"%pd\", type=%d)\n", __func__, |
| 359 | dir->i_ino, inode->i_ino, dentry, type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | |
| 361 | retval = -EIO; |
| 362 | bh = affs_bread(sb, inode->i_ino); |
| 363 | if (!bh) |
| 364 | goto done; |
| 365 | |
| 366 | affs_lock_link(inode); |
| 367 | switch (type) { |
| 368 | case ST_LINKFILE: |
| 369 | case ST_LINKDIR: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | retval = -ENOSPC; |
| 371 | block = affs_alloc_block(dir, dir->i_ino); |
| 372 | if (!block) |
| 373 | goto err; |
| 374 | retval = -EIO; |
Roman Zippel | dca3c33 | 2008-04-29 17:02:20 +0200 | [diff] [blame] | 375 | inode_bh = bh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | bh = affs_getzeroblk(sb, block); |
| 377 | if (!bh) |
| 378 | goto err; |
| 379 | break; |
| 380 | default: |
| 381 | break; |
| 382 | } |
| 383 | |
| 384 | AFFS_HEAD(bh)->ptype = cpu_to_be32(T_SHORT); |
| 385 | AFFS_HEAD(bh)->key = cpu_to_be32(bh->b_blocknr); |
| 386 | affs_copy_name(AFFS_TAIL(sb, bh)->name, dentry); |
| 387 | AFFS_TAIL(sb, bh)->stype = cpu_to_be32(type); |
| 388 | AFFS_TAIL(sb, bh)->parent = cpu_to_be32(dir->i_ino); |
| 389 | |
| 390 | if (inode_bh) { |
| 391 | __be32 chain; |
| 392 | chain = AFFS_TAIL(sb, inode_bh)->link_chain; |
| 393 | AFFS_TAIL(sb, bh)->original = cpu_to_be32(inode->i_ino); |
| 394 | AFFS_TAIL(sb, bh)->link_chain = chain; |
| 395 | AFFS_TAIL(sb, inode_bh)->link_chain = cpu_to_be32(block); |
| 396 | affs_adjust_checksum(inode_bh, block - be32_to_cpu(chain)); |
| 397 | mark_buffer_dirty_inode(inode_bh, inode); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 398 | set_nlink(inode, 2); |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 399 | ihold(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | } |
| 401 | affs_fix_checksum(sb, bh); |
| 402 | mark_buffer_dirty_inode(bh, inode); |
| 403 | dentry->d_fsdata = (void *)(long)bh->b_blocknr; |
| 404 | |
| 405 | affs_lock_dir(dir); |
| 406 | retval = affs_insert_hash(dir, bh); |
| 407 | mark_buffer_dirty_inode(bh, inode); |
| 408 | affs_unlock_dir(dir); |
| 409 | affs_unlock_link(inode); |
| 410 | |
| 411 | d_instantiate(dentry, inode); |
| 412 | done: |
| 413 | affs_brelse(inode_bh); |
| 414 | affs_brelse(bh); |
| 415 | return retval; |
| 416 | err: |
| 417 | if (block) |
| 418 | affs_free_block(sb, block); |
| 419 | affs_unlock_link(inode); |
| 420 | goto done; |
| 421 | } |