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 #include <linux/sched.h> 13 #include "affs.h" 14 15 extern const 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 } 247 248 void 249 affs_drop_inode(struct inode *inode) 250 { 251 mutex_lock(&inode->i_mutex); 252 if (inode->i_size != AFFS_I(inode)->mmu_private) 253 affs_truncate(inode); 254 mutex_unlock(&inode->i_mutex); 255 256 generic_drop_inode(inode); 257 } 258 259 void 260 affs_delete_inode(struct inode *inode) 261 { 262 pr_debug("AFFS: delete_inode(ino=%lu, nlink=%u)\n", inode->i_ino, inode->i_nlink); 263 truncate_inode_pages(&inode->i_data, 0); 264 inode->i_size = 0; 265 if (S_ISREG(inode->i_mode)) 266 affs_truncate(inode); 267 clear_inode(inode); 268 affs_free_block(inode->i_sb, inode->i_ino); 269 } 270 271 void 272 affs_clear_inode(struct inode *inode) 273 { 274 unsigned long cache_page = (unsigned long) AFFS_I(inode)->i_lc; 275 276 pr_debug("AFFS: clear_inode(ino=%lu, nlink=%u)\n", inode->i_ino, inode->i_nlink); 277 if (cache_page) { 278 pr_debug("AFFS: freeing ext cache\n"); 279 AFFS_I(inode)->i_lc = NULL; 280 AFFS_I(inode)->i_ac = NULL; 281 free_page(cache_page); 282 } 283 affs_brelse(AFFS_I(inode)->i_ext_bh); 284 AFFS_I(inode)->i_ext_last = ~1; 285 AFFS_I(inode)->i_ext_bh = NULL; 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 308 inode->i_uid = current->fsuid; 309 inode->i_gid = current->fsgid; 310 inode->i_ino = block; 311 inode->i_nlink = 1; 312 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC; 313 AFFS_I(inode)->i_opencnt = 0; 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 354 pr_debug("AFFS: add_entry(dir=%u, inode=%u, \"%*s\", type=%d)\n", (u32)dir->i_ino, 355 (u32)inode->i_ino, (int)dentry->d_name.len, dentry->d_name.name, type); 356 357 retval = -EIO; 358 bh = affs_bread(sb, inode->i_ino); 359 if (!bh) 360 goto done; 361 362 affs_lock_link(inode); 363 switch (type) { 364 case ST_LINKFILE: 365 case ST_LINKDIR: 366 inode_bh = bh; 367 retval = -ENOSPC; 368 block = affs_alloc_block(dir, dir->i_ino); 369 if (!block) 370 goto err; 371 retval = -EIO; 372 bh = affs_getzeroblk(sb, block); 373 if (!bh) 374 goto err; 375 break; 376 default: 377 break; 378 } 379 380 AFFS_HEAD(bh)->ptype = cpu_to_be32(T_SHORT); 381 AFFS_HEAD(bh)->key = cpu_to_be32(bh->b_blocknr); 382 affs_copy_name(AFFS_TAIL(sb, bh)->name, dentry); 383 AFFS_TAIL(sb, bh)->stype = cpu_to_be32(type); 384 AFFS_TAIL(sb, bh)->parent = cpu_to_be32(dir->i_ino); 385 386 if (inode_bh) { 387 __be32 chain; 388 chain = AFFS_TAIL(sb, inode_bh)->link_chain; 389 AFFS_TAIL(sb, bh)->original = cpu_to_be32(inode->i_ino); 390 AFFS_TAIL(sb, bh)->link_chain = chain; 391 AFFS_TAIL(sb, inode_bh)->link_chain = cpu_to_be32(block); 392 affs_adjust_checksum(inode_bh, block - be32_to_cpu(chain)); 393 mark_buffer_dirty_inode(inode_bh, inode); 394 inode->i_nlink = 2; 395 atomic_inc(&inode->i_count); 396 } 397 affs_fix_checksum(sb, bh); 398 mark_buffer_dirty_inode(bh, inode); 399 dentry->d_fsdata = (void *)(long)bh->b_blocknr; 400 401 affs_lock_dir(dir); 402 retval = affs_insert_hash(dir, bh); 403 mark_buffer_dirty_inode(bh, inode); 404 affs_unlock_dir(dir); 405 affs_unlock_link(inode); 406 407 d_instantiate(dentry, inode); 408 done: 409 affs_brelse(inode_bh); 410 affs_brelse(bh); 411 return retval; 412 err: 413 if (block) 414 affs_free_block(sb, block); 415 affs_unlock_link(inode); 416 goto done; 417 } 418