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 | */ |
| 12 | |
| 13 | #include "affs.h" |
| 14 | |
| 15 | extern struct inode_operations affs_symlink_inode_operations; |
| 16 | extern struct timezone sys_tz; |
| 17 | |
| 18 | void |
| 19 | affs_read_inode(struct inode *inode) |
| 20 | { |
| 21 | struct super_block *sb = inode->i_sb; |
| 22 | struct affs_sb_info *sbi = AFFS_SB(sb); |
| 23 | struct buffer_head *bh; |
| 24 | struct affs_head *head; |
| 25 | struct affs_tail *tail; |
| 26 | u32 block; |
| 27 | u32 size; |
| 28 | u32 prot; |
| 29 | u16 id; |
| 30 | |
| 31 | pr_debug("AFFS: read_inode(%lu)\n",inode->i_ino); |
| 32 | |
| 33 | block = inode->i_ino; |
| 34 | bh = affs_bread(sb, block); |
| 35 | if (!bh) { |
| 36 | affs_warning(sb, "read_inode", "Cannot read block %d", block); |
| 37 | goto bad_inode; |
| 38 | } |
| 39 | if (affs_checksum_block(sb, bh) || be32_to_cpu(AFFS_HEAD(bh)->ptype) != T_SHORT) { |
| 40 | affs_warning(sb,"read_inode", |
| 41 | "Checksum or type (ptype=%d) error on inode %d", |
| 42 | AFFS_HEAD(bh)->ptype, block); |
| 43 | goto bad_inode; |
| 44 | } |
| 45 | |
| 46 | head = AFFS_HEAD(bh); |
| 47 | tail = AFFS_TAIL(sb, bh); |
| 48 | prot = be32_to_cpu(tail->protect); |
| 49 | |
| 50 | inode->i_size = 0; |
| 51 | inode->i_nlink = 1; |
| 52 | inode->i_mode = 0; |
| 53 | AFFS_I(inode)->i_extcnt = 1; |
| 54 | AFFS_I(inode)->i_ext_last = ~1; |
| 55 | AFFS_I(inode)->i_protect = prot; |
| 56 | AFFS_I(inode)->i_opencnt = 0; |
| 57 | AFFS_I(inode)->i_blkcnt = 0; |
| 58 | AFFS_I(inode)->i_lc = NULL; |
| 59 | AFFS_I(inode)->i_lc_size = 0; |
| 60 | AFFS_I(inode)->i_lc_shift = 0; |
| 61 | AFFS_I(inode)->i_lc_mask = 0; |
| 62 | AFFS_I(inode)->i_ac = NULL; |
| 63 | AFFS_I(inode)->i_ext_bh = NULL; |
| 64 | AFFS_I(inode)->mmu_private = 0; |
| 65 | AFFS_I(inode)->i_lastalloc = 0; |
| 66 | AFFS_I(inode)->i_pa_cnt = 0; |
| 67 | |
| 68 | if (sbi->s_flags & SF_SETMODE) |
| 69 | inode->i_mode = sbi->s_mode; |
| 70 | else |
| 71 | inode->i_mode = prot_to_mode(prot); |
| 72 | |
| 73 | id = be16_to_cpu(tail->uid); |
| 74 | if (id == 0 || sbi->s_flags & SF_SETUID) |
| 75 | inode->i_uid = sbi->s_uid; |
| 76 | else if (id == 0xFFFF && sbi->s_flags & SF_MUFS) |
| 77 | inode->i_uid = 0; |
| 78 | else |
| 79 | inode->i_uid = id; |
| 80 | |
| 81 | id = be16_to_cpu(tail->gid); |
| 82 | if (id == 0 || sbi->s_flags & SF_SETGID) |
| 83 | inode->i_gid = sbi->s_gid; |
| 84 | else if (id == 0xFFFF && sbi->s_flags & SF_MUFS) |
| 85 | inode->i_gid = 0; |
| 86 | else |
| 87 | inode->i_gid = id; |
| 88 | |
| 89 | switch (be32_to_cpu(tail->stype)) { |
| 90 | case ST_ROOT: |
| 91 | inode->i_uid = sbi->s_uid; |
| 92 | inode->i_gid = sbi->s_gid; |
| 93 | /* fall through */ |
| 94 | case ST_USERDIR: |
| 95 | if (be32_to_cpu(tail->stype) == ST_USERDIR || |
| 96 | sbi->s_flags & SF_SETMODE) { |
| 97 | if (inode->i_mode & S_IRUSR) |
| 98 | inode->i_mode |= S_IXUSR; |
| 99 | if (inode->i_mode & S_IRGRP) |
| 100 | inode->i_mode |= S_IXGRP; |
| 101 | if (inode->i_mode & S_IROTH) |
| 102 | inode->i_mode |= S_IXOTH; |
| 103 | inode->i_mode |= S_IFDIR; |
| 104 | } else |
| 105 | inode->i_mode = S_IRUGO | S_IXUGO | S_IWUSR | S_IFDIR; |
| 106 | if (tail->link_chain) |
| 107 | inode->i_nlink = 2; |
| 108 | /* Maybe it should be controlled by mount parameter? */ |
| 109 | //inode->i_mode |= S_ISVTX; |
| 110 | inode->i_op = &affs_dir_inode_operations; |
| 111 | inode->i_fop = &affs_dir_operations; |
| 112 | break; |
| 113 | case ST_LINKDIR: |
| 114 | #if 0 |
| 115 | affs_warning(sb, "read_inode", "inode is LINKDIR"); |
| 116 | goto bad_inode; |
| 117 | #else |
| 118 | inode->i_mode |= S_IFDIR; |
| 119 | inode->i_op = NULL; |
| 120 | inode->i_fop = NULL; |
| 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) |
| 137 | inode->i_nlink = 2; |
| 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); |
| 157 | return; |
| 158 | |
| 159 | bad_inode: |
| 160 | make_bad_inode(inode); |
| 161 | affs_brelse(bh); |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | int |
| 166 | affs_write_inode(struct inode *inode, int unused) |
| 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 | |
| 174 | pr_debug("AFFS: write_inode(%lu)\n",inode->i_ino); |
| 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)) { |
| 192 | uid = inode->i_uid; |
| 193 | gid = inode->i_gid; |
| 194 | if (AFFS_SB(sb)->s_flags & SF_MUFS) { |
| 195 | if (inode->i_uid == 0 || inode->i_uid == 0xFFFF) |
| 196 | uid = inode->i_uid ^ ~0; |
| 197 | if (inode->i_gid == 0 || inode->i_gid == 0xFFFF) |
| 198 | gid = inode->i_gid ^ ~0; |
| 199 | } |
| 200 | if (!(AFFS_SB(sb)->s_flags & SF_SETUID)) |
| 201 | tail->uid = cpu_to_be16(uid); |
| 202 | if (!(AFFS_SB(sb)->s_flags & SF_SETGID)) |
| 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 | { |
| 216 | struct inode *inode = dentry->d_inode; |
| 217 | int error; |
| 218 | |
| 219 | pr_debug("AFFS: notify_change(%lu,0x%x)\n",inode->i_ino,attr->ia_valid); |
| 220 | |
| 221 | error = inode_change_ok(inode,attr); |
| 222 | if (error) |
| 223 | goto out; |
| 224 | |
| 225 | if (((attr->ia_valid & ATTR_UID) && (AFFS_SB(inode->i_sb)->s_flags & SF_SETUID)) || |
| 226 | ((attr->ia_valid & ATTR_GID) && (AFFS_SB(inode->i_sb)->s_flags & SF_SETGID)) || |
| 227 | ((attr->ia_valid & ATTR_MODE) && |
| 228 | (AFFS_SB(inode->i_sb)->s_flags & (SF_SETMODE | SF_IMMUTABLE)))) { |
| 229 | if (!(AFFS_SB(inode->i_sb)->s_flags & SF_QUIET)) |
| 230 | error = -EPERM; |
| 231 | goto out; |
| 232 | } |
| 233 | |
| 234 | error = inode_setattr(inode, attr); |
| 235 | if (!error && (attr->ia_valid & ATTR_MODE)) |
| 236 | mode_to_prot(inode); |
| 237 | out: |
| 238 | return error; |
| 239 | } |
| 240 | |
| 241 | void |
| 242 | affs_put_inode(struct inode *inode) |
| 243 | { |
| 244 | pr_debug("AFFS: put_inode(ino=%lu, nlink=%u)\n", inode->i_ino, inode->i_nlink); |
| 245 | affs_free_prealloc(inode); |
| 246 | if (atomic_read(&inode->i_count) == 1) { |
| 247 | down(&inode->i_sem); |
| 248 | if (inode->i_size != AFFS_I(inode)->mmu_private) |
| 249 | affs_truncate(inode); |
| 250 | up(&inode->i_sem); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | void |
| 255 | affs_delete_inode(struct inode *inode) |
| 256 | { |
| 257 | pr_debug("AFFS: delete_inode(ino=%lu, nlink=%u)\n", inode->i_ino, inode->i_nlink); |
| 258 | inode->i_size = 0; |
| 259 | if (S_ISREG(inode->i_mode)) |
| 260 | affs_truncate(inode); |
| 261 | clear_inode(inode); |
| 262 | affs_free_block(inode->i_sb, inode->i_ino); |
| 263 | } |
| 264 | |
| 265 | void |
| 266 | affs_clear_inode(struct inode *inode) |
| 267 | { |
| 268 | unsigned long cache_page = (unsigned long) AFFS_I(inode)->i_lc; |
| 269 | |
| 270 | pr_debug("AFFS: clear_inode(ino=%lu, nlink=%u)\n", inode->i_ino, inode->i_nlink); |
| 271 | if (cache_page) { |
| 272 | pr_debug("AFFS: freeing ext cache\n"); |
| 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; |
| 280 | } |
| 281 | |
| 282 | struct inode * |
| 283 | affs_new_inode(struct inode *dir) |
| 284 | { |
| 285 | struct super_block *sb = dir->i_sb; |
| 286 | struct inode *inode; |
| 287 | u32 block; |
| 288 | struct buffer_head *bh; |
| 289 | |
| 290 | if (!(inode = new_inode(sb))) |
| 291 | goto err_inode; |
| 292 | |
| 293 | if (!(block = affs_alloc_block(dir, dir->i_ino))) |
| 294 | goto err_block; |
| 295 | |
| 296 | bh = affs_getzeroblk(sb, block); |
| 297 | if (!bh) |
| 298 | goto err_bh; |
| 299 | mark_buffer_dirty_inode(bh, inode); |
| 300 | affs_brelse(bh); |
| 301 | |
| 302 | inode->i_uid = current->fsuid; |
| 303 | inode->i_gid = current->fsgid; |
| 304 | inode->i_ino = block; |
| 305 | inode->i_nlink = 1; |
| 306 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC; |
| 307 | AFFS_I(inode)->i_opencnt = 0; |
| 308 | AFFS_I(inode)->i_blkcnt = 0; |
| 309 | AFFS_I(inode)->i_lc = NULL; |
| 310 | AFFS_I(inode)->i_lc_size = 0; |
| 311 | AFFS_I(inode)->i_lc_shift = 0; |
| 312 | AFFS_I(inode)->i_lc_mask = 0; |
| 313 | AFFS_I(inode)->i_ac = NULL; |
| 314 | AFFS_I(inode)->i_ext_bh = NULL; |
| 315 | AFFS_I(inode)->mmu_private = 0; |
| 316 | AFFS_I(inode)->i_protect = 0; |
| 317 | AFFS_I(inode)->i_lastalloc = 0; |
| 318 | AFFS_I(inode)->i_pa_cnt = 0; |
| 319 | AFFS_I(inode)->i_extcnt = 1; |
| 320 | AFFS_I(inode)->i_ext_last = ~1; |
| 321 | |
| 322 | insert_inode_hash(inode); |
| 323 | |
| 324 | return inode; |
| 325 | |
| 326 | err_bh: |
| 327 | affs_free_block(sb, block); |
| 328 | err_block: |
| 329 | iput(inode); |
| 330 | err_inode: |
| 331 | return NULL; |
| 332 | } |
| 333 | |
| 334 | /* |
| 335 | * Add an entry to a directory. Create the header block |
| 336 | * and insert it into the hash table. |
| 337 | */ |
| 338 | |
| 339 | int |
| 340 | affs_add_entry(struct inode *dir, struct inode *inode, struct dentry *dentry, s32 type) |
| 341 | { |
| 342 | struct super_block *sb = dir->i_sb; |
| 343 | struct buffer_head *inode_bh = NULL; |
| 344 | struct buffer_head *bh = NULL; |
| 345 | u32 block = 0; |
| 346 | int retval; |
| 347 | |
| 348 | pr_debug("AFFS: add_entry(dir=%u, inode=%u, \"%*s\", type=%d)\n", (u32)dir->i_ino, |
| 349 | (u32)inode->i_ino, (int)dentry->d_name.len, dentry->d_name.name, type); |
| 350 | |
| 351 | retval = -EIO; |
| 352 | bh = affs_bread(sb, inode->i_ino); |
| 353 | if (!bh) |
| 354 | goto done; |
| 355 | |
| 356 | affs_lock_link(inode); |
| 357 | switch (type) { |
| 358 | case ST_LINKFILE: |
| 359 | case ST_LINKDIR: |
| 360 | inode_bh = bh; |
| 361 | retval = -ENOSPC; |
| 362 | block = affs_alloc_block(dir, dir->i_ino); |
| 363 | if (!block) |
| 364 | goto err; |
| 365 | retval = -EIO; |
| 366 | bh = affs_getzeroblk(sb, block); |
| 367 | if (!bh) |
| 368 | goto err; |
| 369 | break; |
| 370 | default: |
| 371 | break; |
| 372 | } |
| 373 | |
| 374 | AFFS_HEAD(bh)->ptype = cpu_to_be32(T_SHORT); |
| 375 | AFFS_HEAD(bh)->key = cpu_to_be32(bh->b_blocknr); |
| 376 | affs_copy_name(AFFS_TAIL(sb, bh)->name, dentry); |
| 377 | AFFS_TAIL(sb, bh)->stype = cpu_to_be32(type); |
| 378 | AFFS_TAIL(sb, bh)->parent = cpu_to_be32(dir->i_ino); |
| 379 | |
| 380 | if (inode_bh) { |
| 381 | __be32 chain; |
| 382 | chain = AFFS_TAIL(sb, inode_bh)->link_chain; |
| 383 | AFFS_TAIL(sb, bh)->original = cpu_to_be32(inode->i_ino); |
| 384 | AFFS_TAIL(sb, bh)->link_chain = chain; |
| 385 | AFFS_TAIL(sb, inode_bh)->link_chain = cpu_to_be32(block); |
| 386 | affs_adjust_checksum(inode_bh, block - be32_to_cpu(chain)); |
| 387 | mark_buffer_dirty_inode(inode_bh, inode); |
| 388 | inode->i_nlink = 2; |
| 389 | atomic_inc(&inode->i_count); |
| 390 | } |
| 391 | affs_fix_checksum(sb, bh); |
| 392 | mark_buffer_dirty_inode(bh, inode); |
| 393 | dentry->d_fsdata = (void *)(long)bh->b_blocknr; |
| 394 | |
| 395 | affs_lock_dir(dir); |
| 396 | retval = affs_insert_hash(dir, bh); |
| 397 | mark_buffer_dirty_inode(bh, inode); |
| 398 | affs_unlock_dir(dir); |
| 399 | affs_unlock_link(inode); |
| 400 | |
| 401 | d_instantiate(dentry, inode); |
| 402 | done: |
| 403 | affs_brelse(inode_bh); |
| 404 | affs_brelse(bh); |
| 405 | return retval; |
| 406 | err: |
| 407 | if (block) |
| 408 | affs_free_block(sb, block); |
| 409 | affs_unlock_link(inode); |
| 410 | goto done; |
| 411 | } |