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); |
Mark Fasheh | fef2665 | 2005-09-09 13:01:31 -0700 | [diff] [blame] | 258 | truncate_inode_pages(&inode->i_data, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | inode->i_size = 0; |
| 260 | if (S_ISREG(inode->i_mode)) |
| 261 | affs_truncate(inode); |
| 262 | clear_inode(inode); |
| 263 | affs_free_block(inode->i_sb, inode->i_ino); |
| 264 | } |
| 265 | |
| 266 | void |
| 267 | affs_clear_inode(struct inode *inode) |
| 268 | { |
| 269 | unsigned long cache_page = (unsigned long) AFFS_I(inode)->i_lc; |
| 270 | |
| 271 | pr_debug("AFFS: clear_inode(ino=%lu, nlink=%u)\n", inode->i_ino, inode->i_nlink); |
| 272 | if (cache_page) { |
| 273 | pr_debug("AFFS: freeing ext cache\n"); |
| 274 | AFFS_I(inode)->i_lc = NULL; |
| 275 | AFFS_I(inode)->i_ac = NULL; |
| 276 | free_page(cache_page); |
| 277 | } |
| 278 | affs_brelse(AFFS_I(inode)->i_ext_bh); |
| 279 | AFFS_I(inode)->i_ext_last = ~1; |
| 280 | AFFS_I(inode)->i_ext_bh = NULL; |
| 281 | } |
| 282 | |
| 283 | struct inode * |
| 284 | affs_new_inode(struct inode *dir) |
| 285 | { |
| 286 | struct super_block *sb = dir->i_sb; |
| 287 | struct inode *inode; |
| 288 | u32 block; |
| 289 | struct buffer_head *bh; |
| 290 | |
| 291 | if (!(inode = new_inode(sb))) |
| 292 | goto err_inode; |
| 293 | |
| 294 | if (!(block = affs_alloc_block(dir, dir->i_ino))) |
| 295 | goto err_block; |
| 296 | |
| 297 | bh = affs_getzeroblk(sb, block); |
| 298 | if (!bh) |
| 299 | goto err_bh; |
| 300 | mark_buffer_dirty_inode(bh, inode); |
| 301 | affs_brelse(bh); |
| 302 | |
| 303 | inode->i_uid = current->fsuid; |
| 304 | inode->i_gid = current->fsgid; |
| 305 | inode->i_ino = block; |
| 306 | inode->i_nlink = 1; |
| 307 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC; |
| 308 | AFFS_I(inode)->i_opencnt = 0; |
| 309 | AFFS_I(inode)->i_blkcnt = 0; |
| 310 | AFFS_I(inode)->i_lc = NULL; |
| 311 | AFFS_I(inode)->i_lc_size = 0; |
| 312 | AFFS_I(inode)->i_lc_shift = 0; |
| 313 | AFFS_I(inode)->i_lc_mask = 0; |
| 314 | AFFS_I(inode)->i_ac = NULL; |
| 315 | AFFS_I(inode)->i_ext_bh = NULL; |
| 316 | AFFS_I(inode)->mmu_private = 0; |
| 317 | AFFS_I(inode)->i_protect = 0; |
| 318 | AFFS_I(inode)->i_lastalloc = 0; |
| 319 | AFFS_I(inode)->i_pa_cnt = 0; |
| 320 | AFFS_I(inode)->i_extcnt = 1; |
| 321 | AFFS_I(inode)->i_ext_last = ~1; |
| 322 | |
| 323 | insert_inode_hash(inode); |
| 324 | |
| 325 | return inode; |
| 326 | |
| 327 | err_bh: |
| 328 | affs_free_block(sb, block); |
| 329 | err_block: |
| 330 | iput(inode); |
| 331 | err_inode: |
| 332 | return NULL; |
| 333 | } |
| 334 | |
| 335 | /* |
| 336 | * Add an entry to a directory. Create the header block |
| 337 | * and insert it into the hash table. |
| 338 | */ |
| 339 | |
| 340 | int |
| 341 | affs_add_entry(struct inode *dir, struct inode *inode, struct dentry *dentry, s32 type) |
| 342 | { |
| 343 | struct super_block *sb = dir->i_sb; |
| 344 | struct buffer_head *inode_bh = NULL; |
| 345 | struct buffer_head *bh = NULL; |
| 346 | u32 block = 0; |
| 347 | int retval; |
| 348 | |
| 349 | pr_debug("AFFS: add_entry(dir=%u, inode=%u, \"%*s\", type=%d)\n", (u32)dir->i_ino, |
| 350 | (u32)inode->i_ino, (int)dentry->d_name.len, dentry->d_name.name, type); |
| 351 | |
| 352 | retval = -EIO; |
| 353 | bh = affs_bread(sb, inode->i_ino); |
| 354 | if (!bh) |
| 355 | goto done; |
| 356 | |
| 357 | affs_lock_link(inode); |
| 358 | switch (type) { |
| 359 | case ST_LINKFILE: |
| 360 | case ST_LINKDIR: |
| 361 | inode_bh = bh; |
| 362 | retval = -ENOSPC; |
| 363 | block = affs_alloc_block(dir, dir->i_ino); |
| 364 | if (!block) |
| 365 | goto err; |
| 366 | retval = -EIO; |
| 367 | bh = affs_getzeroblk(sb, block); |
| 368 | if (!bh) |
| 369 | goto err; |
| 370 | break; |
| 371 | default: |
| 372 | break; |
| 373 | } |
| 374 | |
| 375 | AFFS_HEAD(bh)->ptype = cpu_to_be32(T_SHORT); |
| 376 | AFFS_HEAD(bh)->key = cpu_to_be32(bh->b_blocknr); |
| 377 | affs_copy_name(AFFS_TAIL(sb, bh)->name, dentry); |
| 378 | AFFS_TAIL(sb, bh)->stype = cpu_to_be32(type); |
| 379 | AFFS_TAIL(sb, bh)->parent = cpu_to_be32(dir->i_ino); |
| 380 | |
| 381 | if (inode_bh) { |
| 382 | __be32 chain; |
| 383 | chain = AFFS_TAIL(sb, inode_bh)->link_chain; |
| 384 | AFFS_TAIL(sb, bh)->original = cpu_to_be32(inode->i_ino); |
| 385 | AFFS_TAIL(sb, bh)->link_chain = chain; |
| 386 | AFFS_TAIL(sb, inode_bh)->link_chain = cpu_to_be32(block); |
| 387 | affs_adjust_checksum(inode_bh, block - be32_to_cpu(chain)); |
| 388 | mark_buffer_dirty_inode(inode_bh, inode); |
| 389 | inode->i_nlink = 2; |
| 390 | atomic_inc(&inode->i_count); |
| 391 | } |
| 392 | affs_fix_checksum(sb, bh); |
| 393 | mark_buffer_dirty_inode(bh, inode); |
| 394 | dentry->d_fsdata = (void *)(long)bh->b_blocknr; |
| 395 | |
| 396 | affs_lock_dir(dir); |
| 397 | retval = affs_insert_hash(dir, bh); |
| 398 | mark_buffer_dirty_inode(bh, inode); |
| 399 | affs_unlock_dir(dir); |
| 400 | affs_unlock_link(inode); |
| 401 | |
| 402 | d_instantiate(dentry, inode); |
| 403 | done: |
| 404 | affs_brelse(inode_bh); |
| 405 | affs_brelse(bh); |
| 406 | return retval; |
| 407 | err: |
| 408 | if (block) |
| 409 | affs_free_block(sb, block); |
| 410 | affs_unlock_link(inode); |
| 411 | goto done; |
| 412 | } |