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 | |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 16 | extern const struct inode_operations affs_symlink_inode_operations; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 71 | if (sbi->s_flags & SF_SETMODE) |
| 72 | inode->i_mode = sbi->s_mode; |
| 73 | else |
| 74 | inode->i_mode = prot_to_mode(prot); |
| 75 | |
| 76 | id = be16_to_cpu(tail->uid); |
| 77 | if (id == 0 || sbi->s_flags & SF_SETUID) |
| 78 | inode->i_uid = sbi->s_uid; |
| 79 | else if (id == 0xFFFF && 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); |
| 85 | if (id == 0 || sbi->s_flags & SF_SETGID) |
| 86 | inode->i_gid = sbi->s_gid; |
| 87 | else if (id == 0xFFFF && 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 || |
| 99 | sbi->s_flags & SF_SETMODE) { |
| 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | inode->i_mapping->a_ops = (sbi->s_flags & SF_OFS) ? &affs_aops_ofs : &affs_aops; |
| 139 | inode->i_op = &affs_file_inode_operations; |
| 140 | inode->i_fop = &affs_file_operations; |
| 141 | break; |
| 142 | case ST_SOFTLINK: |
| 143 | inode->i_mode |= S_IFLNK; |
| 144 | inode->i_op = &affs_symlink_inode_operations; |
| 145 | inode->i_data.a_ops = &affs_symlink_aops; |
| 146 | break; |
| 147 | } |
| 148 | |
| 149 | inode->i_mtime.tv_sec = inode->i_atime.tv_sec = inode->i_ctime.tv_sec |
| 150 | = (be32_to_cpu(tail->change.days) * (24 * 60 * 60) + |
| 151 | be32_to_cpu(tail->change.mins) * 60 + |
| 152 | be32_to_cpu(tail->change.ticks) / 50 + |
| 153 | ((8 * 365 + 2) * 24 * 60 * 60)) + |
| 154 | sys_tz.tz_minuteswest * 60; |
| 155 | inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec = inode->i_atime.tv_nsec = 0; |
| 156 | affs_brelse(bh); |
David Howells | 210f855 | 2008-02-07 00:15:29 -0800 | [diff] [blame] | 157 | unlock_new_inode(inode); |
| 158 | return inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | |
| 160 | bad_inode: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | affs_brelse(bh); |
David Howells | 210f855 | 2008-02-07 00:15:29 -0800 | [diff] [blame] | 162 | iget_failed(inode); |
| 163 | return ERR_PTR(-EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | int |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 167 | affs_write_inode(struct inode *inode, struct writeback_control *wbc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | { |
| 169 | struct super_block *sb = inode->i_sb; |
| 170 | struct buffer_head *bh; |
| 171 | struct affs_tail *tail; |
| 172 | uid_t uid; |
| 173 | gid_t gid; |
| 174 | |
Fabian Frederick | 9606d9a | 2014-06-06 14:37:25 -0700 | [diff] [blame] | 175 | pr_debug("write_inode(%lu)\n", inode->i_ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
| 177 | if (!inode->i_nlink) |
| 178 | // possibly free block |
| 179 | return 0; |
| 180 | bh = affs_bread(sb, inode->i_ino); |
| 181 | if (!bh) { |
| 182 | affs_error(sb,"write_inode","Cannot read block %lu",inode->i_ino); |
| 183 | return -EIO; |
| 184 | } |
| 185 | tail = AFFS_TAIL(sb, bh); |
| 186 | if (tail->stype == cpu_to_be32(ST_ROOT)) { |
| 187 | secs_to_datestamp(inode->i_mtime.tv_sec,&AFFS_ROOT_TAIL(sb, bh)->root_change); |
| 188 | } else { |
| 189 | tail->protect = cpu_to_be32(AFFS_I(inode)->i_protect); |
| 190 | tail->size = cpu_to_be32(inode->i_size); |
| 191 | secs_to_datestamp(inode->i_mtime.tv_sec,&tail->change); |
| 192 | if (!(inode->i_ino == AFFS_SB(sb)->s_root_block)) { |
Eric W. Biederman | 8fed10b | 2012-02-07 16:20:16 -0800 | [diff] [blame] | 193 | uid = i_uid_read(inode); |
| 194 | gid = i_gid_read(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | if (AFFS_SB(sb)->s_flags & SF_MUFS) { |
Eric W. Biederman | 8fed10b | 2012-02-07 16:20:16 -0800 | [diff] [blame] | 196 | if (uid == 0 || uid == 0xFFFF) |
| 197 | uid = uid ^ ~0; |
| 198 | if (gid == 0 || gid == 0xFFFF) |
| 199 | gid = gid ^ ~0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | } |
| 201 | if (!(AFFS_SB(sb)->s_flags & SF_SETUID)) |
| 202 | tail->uid = cpu_to_be16(uid); |
| 203 | if (!(AFFS_SB(sb)->s_flags & SF_SETGID)) |
| 204 | tail->gid = cpu_to_be16(gid); |
| 205 | } |
| 206 | } |
| 207 | affs_fix_checksum(sb, bh); |
| 208 | mark_buffer_dirty_inode(bh, inode); |
| 209 | affs_brelse(bh); |
| 210 | affs_free_prealloc(inode); |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | int |
| 215 | affs_notify_change(struct dentry *dentry, struct iattr *attr) |
| 216 | { |
| 217 | struct inode *inode = dentry->d_inode; |
| 218 | int error; |
| 219 | |
Fabian Frederick | 9606d9a | 2014-06-06 14:37:25 -0700 | [diff] [blame] | 220 | 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] | 221 | |
| 222 | error = inode_change_ok(inode,attr); |
| 223 | if (error) |
| 224 | goto out; |
| 225 | |
| 226 | if (((attr->ia_valid & ATTR_UID) && (AFFS_SB(inode->i_sb)->s_flags & SF_SETUID)) || |
| 227 | ((attr->ia_valid & ATTR_GID) && (AFFS_SB(inode->i_sb)->s_flags & SF_SETGID)) || |
| 228 | ((attr->ia_valid & ATTR_MODE) && |
| 229 | (AFFS_SB(inode->i_sb)->s_flags & (SF_SETMODE | SF_IMMUTABLE)))) { |
| 230 | if (!(AFFS_SB(inode->i_sb)->s_flags & SF_QUIET)) |
| 231 | error = -EPERM; |
| 232 | goto out; |
| 233 | } |
| 234 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 235 | if ((attr->ia_valid & ATTR_SIZE) && |
| 236 | attr->ia_size != i_size_read(inode)) { |
Marco Stornelli | 1dc1834 | 2012-12-15 11:51:53 +0100 | [diff] [blame] | 237 | error = inode_newsize_ok(inode, attr->ia_size); |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 238 | if (error) |
| 239 | return error; |
Marco Stornelli | 1dc1834 | 2012-12-15 11:51:53 +0100 | [diff] [blame] | 240 | |
| 241 | truncate_setsize(inode, attr->ia_size); |
| 242 | affs_truncate(inode); |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | setattr_copy(inode, attr); |
| 246 | mark_inode_dirty(inode); |
| 247 | |
| 248 | if (attr->ia_valid & ATTR_MODE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | mode_to_prot(inode); |
| 250 | out: |
| 251 | return error; |
| 252 | } |
| 253 | |
| 254 | void |
Al Viro | f053ddd | 2010-06-06 10:16:41 -0400 | [diff] [blame] | 255 | affs_evict_inode(struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | { |
Roman Zippel | dca3c33 | 2008-04-29 17:02:20 +0200 | [diff] [blame] | 257 | unsigned long cache_page; |
Fabian Frederick | 9606d9a | 2014-06-06 14:37:25 -0700 | [diff] [blame] | 258 | pr_debug("evict_inode(ino=%lu, nlink=%u)\n", |
| 259 | inode->i_ino, inode->i_nlink); |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 260 | truncate_inode_pages_final(&inode->i_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | |
Al Viro | f053ddd | 2010-06-06 10:16:41 -0400 | [diff] [blame] | 262 | if (!inode->i_nlink) { |
| 263 | inode->i_size = 0; |
| 264 | affs_truncate(inode); |
| 265 | } |
Roman Zippel | dca3c33 | 2008-04-29 17:02:20 +0200 | [diff] [blame] | 266 | |
Al Viro | f053ddd | 2010-06-06 10:16:41 -0400 | [diff] [blame] | 267 | invalidate_inode_buffers(inode); |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 268 | clear_inode(inode); |
Roman Zippel | dca3c33 | 2008-04-29 17:02:20 +0200 | [diff] [blame] | 269 | affs_free_prealloc(inode); |
| 270 | cache_page = (unsigned long)AFFS_I(inode)->i_lc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | if (cache_page) { |
Fabian Frederick | 9606d9a | 2014-06-06 14:37:25 -0700 | [diff] [blame] | 272 | pr_debug("freeing ext cache\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | AFFS_I(inode)->i_lc = NULL; |
| 274 | AFFS_I(inode)->i_ac = NULL; |
| 275 | free_page(cache_page); |
| 276 | } |
| 277 | affs_brelse(AFFS_I(inode)->i_ext_bh); |
| 278 | AFFS_I(inode)->i_ext_last = ~1; |
| 279 | AFFS_I(inode)->i_ext_bh = NULL; |
Al Viro | f053ddd | 2010-06-06 10:16:41 -0400 | [diff] [blame] | 280 | |
| 281 | if (!inode->i_nlink) |
| 282 | affs_free_block(inode->i_sb, inode->i_ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | struct inode * |
| 286 | affs_new_inode(struct inode *dir) |
| 287 | { |
| 288 | struct super_block *sb = dir->i_sb; |
| 289 | struct inode *inode; |
| 290 | u32 block; |
| 291 | struct buffer_head *bh; |
| 292 | |
| 293 | if (!(inode = new_inode(sb))) |
| 294 | goto err_inode; |
| 295 | |
| 296 | if (!(block = affs_alloc_block(dir, dir->i_ino))) |
| 297 | goto err_block; |
| 298 | |
| 299 | bh = affs_getzeroblk(sb, block); |
| 300 | if (!bh) |
| 301 | goto err_bh; |
| 302 | mark_buffer_dirty_inode(bh, inode); |
| 303 | affs_brelse(bh); |
| 304 | |
David Howells | 2155998 | 2008-11-14 10:38:45 +1100 | [diff] [blame] | 305 | inode->i_uid = current_fsuid(); |
| 306 | inode->i_gid = current_fsgid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | inode->i_ino = block; |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 308 | set_nlink(inode, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC; |
Roman Zippel | dca3c33 | 2008-04-29 17:02:20 +0200 | [diff] [blame] | 310 | atomic_set(&AFFS_I(inode)->i_opencnt, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | AFFS_I(inode)->i_blkcnt = 0; |
| 312 | AFFS_I(inode)->i_lc = NULL; |
| 313 | AFFS_I(inode)->i_lc_size = 0; |
| 314 | AFFS_I(inode)->i_lc_shift = 0; |
| 315 | AFFS_I(inode)->i_lc_mask = 0; |
| 316 | AFFS_I(inode)->i_ac = NULL; |
| 317 | AFFS_I(inode)->i_ext_bh = NULL; |
| 318 | AFFS_I(inode)->mmu_private = 0; |
| 319 | AFFS_I(inode)->i_protect = 0; |
| 320 | AFFS_I(inode)->i_lastalloc = 0; |
| 321 | AFFS_I(inode)->i_pa_cnt = 0; |
| 322 | AFFS_I(inode)->i_extcnt = 1; |
| 323 | AFFS_I(inode)->i_ext_last = ~1; |
| 324 | |
| 325 | insert_inode_hash(inode); |
| 326 | |
| 327 | return inode; |
| 328 | |
| 329 | err_bh: |
| 330 | affs_free_block(sb, block); |
| 331 | err_block: |
| 332 | iput(inode); |
| 333 | err_inode: |
| 334 | return NULL; |
| 335 | } |
| 336 | |
| 337 | /* |
| 338 | * Add an entry to a directory. Create the header block |
| 339 | * and insert it into the hash table. |
| 340 | */ |
| 341 | |
| 342 | int |
| 343 | affs_add_entry(struct inode *dir, struct inode *inode, struct dentry *dentry, s32 type) |
| 344 | { |
| 345 | struct super_block *sb = dir->i_sb; |
| 346 | struct buffer_head *inode_bh = NULL; |
| 347 | struct buffer_head *bh = NULL; |
| 348 | u32 block = 0; |
| 349 | int retval; |
| 350 | |
Fabian Frederick | 9606d9a | 2014-06-06 14:37:25 -0700 | [diff] [blame] | 351 | pr_debug("%s(dir=%u, inode=%u, \"%*s\", type=%d)\n", |
| 352 | __func__, (u32)dir->i_ino, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | (u32)inode->i_ino, (int)dentry->d_name.len, dentry->d_name.name, type); |
| 354 | |
| 355 | retval = -EIO; |
| 356 | bh = affs_bread(sb, inode->i_ino); |
| 357 | if (!bh) |
| 358 | goto done; |
| 359 | |
| 360 | affs_lock_link(inode); |
| 361 | switch (type) { |
| 362 | case ST_LINKFILE: |
| 363 | case ST_LINKDIR: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | retval = -ENOSPC; |
| 365 | block = affs_alloc_block(dir, dir->i_ino); |
| 366 | if (!block) |
| 367 | goto err; |
| 368 | retval = -EIO; |
Roman Zippel | dca3c33 | 2008-04-29 17:02:20 +0200 | [diff] [blame] | 369 | inode_bh = bh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | bh = affs_getzeroblk(sb, block); |
| 371 | if (!bh) |
| 372 | goto err; |
| 373 | break; |
| 374 | default: |
| 375 | break; |
| 376 | } |
| 377 | |
| 378 | AFFS_HEAD(bh)->ptype = cpu_to_be32(T_SHORT); |
| 379 | AFFS_HEAD(bh)->key = cpu_to_be32(bh->b_blocknr); |
| 380 | affs_copy_name(AFFS_TAIL(sb, bh)->name, dentry); |
| 381 | AFFS_TAIL(sb, bh)->stype = cpu_to_be32(type); |
| 382 | AFFS_TAIL(sb, bh)->parent = cpu_to_be32(dir->i_ino); |
| 383 | |
| 384 | if (inode_bh) { |
| 385 | __be32 chain; |
| 386 | chain = AFFS_TAIL(sb, inode_bh)->link_chain; |
| 387 | AFFS_TAIL(sb, bh)->original = cpu_to_be32(inode->i_ino); |
| 388 | AFFS_TAIL(sb, bh)->link_chain = chain; |
| 389 | AFFS_TAIL(sb, inode_bh)->link_chain = cpu_to_be32(block); |
| 390 | affs_adjust_checksum(inode_bh, block - be32_to_cpu(chain)); |
| 391 | mark_buffer_dirty_inode(inode_bh, inode); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 392 | set_nlink(inode, 2); |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 393 | ihold(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | } |
| 395 | affs_fix_checksum(sb, bh); |
| 396 | mark_buffer_dirty_inode(bh, inode); |
| 397 | dentry->d_fsdata = (void *)(long)bh->b_blocknr; |
| 398 | |
| 399 | affs_lock_dir(dir); |
| 400 | retval = affs_insert_hash(dir, bh); |
| 401 | mark_buffer_dirty_inode(bh, inode); |
| 402 | affs_unlock_dir(dir); |
| 403 | affs_unlock_link(inode); |
| 404 | |
| 405 | d_instantiate(dentry, inode); |
| 406 | done: |
| 407 | affs_brelse(inode_bh); |
| 408 | affs_brelse(bh); |
| 409 | return retval; |
| 410 | err: |
| 411 | if (block) |
| 412 | affs_free_block(sb, block); |
| 413 | affs_unlock_link(inode); |
| 414 | goto done; |
| 415 | } |