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