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