2 * linux/fs/affs/inode.c
4 * (c) 1996 Hans-Joachim Widmaier - Rewritten
6 * (C) 1993 Ray Burr - Modified for Amiga FFS filesystem.
8 * (C) 1992 Eric Youngdale Modified for ISO9660 filesystem.
10 * (C) 1991 Linus Torvalds - minix filesystem
15 extern struct inode_operations affs_symlink_inode_operations;
16 extern struct timezone sys_tz;
19 affs_read_inode(struct inode *inode)
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;
31 pr_debug("AFFS: read_inode(%lu)\n",inode->i_ino);
34 bh = affs_bread(sb, block);
36 affs_warning(sb, "read_inode", "Cannot read block %d", block);
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);
47 tail = AFFS_TAIL(sb, bh);
48 prot = be32_to_cpu(tail->protect);
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;
68 if (sbi->s_flags & SF_SETMODE)
69 inode->i_mode = sbi->s_mode;
71 inode->i_mode = prot_to_mode(prot);
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)
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)
89 switch (be32_to_cpu(tail->stype)) {
91 inode->i_uid = sbi->s_uid;
92 inode->i_gid = sbi->s_gid;
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;
105 inode->i_mode = S_IRUGO | S_IXUGO | S_IWUSR | S_IFDIR;
106 if (tail->link_chain)
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;
115 affs_warning(sb, "read_inode", "inode is LINKDIR");
118 inode->i_mode |= S_IFDIR;
124 affs_warning(sb, "read_inode", "inode is LINKFILE");
127 size = be32_to_cpu(tail->size);
128 inode->i_mode |= S_IFREG;
129 AFFS_I(inode)->mmu_private = inode->i_size = 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) /
136 if (tail->link_chain)
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;
143 inode->i_mode |= S_IFLNK;
144 inode->i_op = &affs_symlink_inode_operations;
145 inode->i_data.a_ops = &affs_symlink_aops;
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;
160 make_bad_inode(inode);
166 affs_write_inode(struct inode *inode, int unused)
168 struct super_block *sb = inode->i_sb;
169 struct buffer_head *bh;
170 struct affs_tail *tail;
174 pr_debug("AFFS: write_inode(%lu)\n",inode->i_ino);
177 // possibly free block
179 bh = affs_bread(sb, inode->i_ino);
181 affs_error(sb,"write_inode","Cannot read block %lu",inode->i_ino);
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);
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)) {
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;
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);
206 affs_fix_checksum(sb, bh);
207 mark_buffer_dirty_inode(bh, inode);
209 affs_free_prealloc(inode);
214 affs_notify_change(struct dentry *dentry, struct iattr *attr)
216 struct inode *inode = dentry->d_inode;
219 pr_debug("AFFS: notify_change(%lu,0x%x)\n",inode->i_ino,attr->ia_valid);
221 error = inode_change_ok(inode,attr);
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))
234 error = inode_setattr(inode, attr);
235 if (!error && (attr->ia_valid & ATTR_MODE))
242 affs_put_inode(struct inode *inode)
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 mutex_lock(&inode->i_mutex);
248 if (inode->i_size != AFFS_I(inode)->mmu_private)
249 affs_truncate(inode);
250 mutex_unlock(&inode->i_mutex);
255 affs_delete_inode(struct inode *inode)
257 pr_debug("AFFS: delete_inode(ino=%lu, nlink=%u)\n", inode->i_ino, inode->i_nlink);
258 truncate_inode_pages(&inode->i_data, 0);
260 if (S_ISREG(inode->i_mode))
261 affs_truncate(inode);
263 affs_free_block(inode->i_sb, inode->i_ino);
267 affs_clear_inode(struct inode *inode)
269 unsigned long cache_page = (unsigned long) AFFS_I(inode)->i_lc;
271 pr_debug("AFFS: clear_inode(ino=%lu, nlink=%u)\n", inode->i_ino, inode->i_nlink);
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);
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;
284 affs_new_inode(struct inode *dir)
286 struct super_block *sb = dir->i_sb;
289 struct buffer_head *bh;
291 if (!(inode = new_inode(sb)))
294 if (!(block = affs_alloc_block(dir, dir->i_ino)))
297 bh = affs_getzeroblk(sb, block);
300 mark_buffer_dirty_inode(bh, inode);
303 inode->i_uid = current->fsuid;
304 inode->i_gid = current->fsgid;
305 inode->i_ino = block;
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;
323 insert_inode_hash(inode);
328 affs_free_block(sb, block);
336 * Add an entry to a directory. Create the header block
337 * and insert it into the hash table.
341 affs_add_entry(struct inode *dir, struct inode *inode, struct dentry *dentry, s32 type)
343 struct super_block *sb = dir->i_sb;
344 struct buffer_head *inode_bh = NULL;
345 struct buffer_head *bh = NULL;
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);
353 bh = affs_bread(sb, inode->i_ino);
357 affs_lock_link(inode);
363 block = affs_alloc_block(dir, dir->i_ino);
367 bh = affs_getzeroblk(sb, block);
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);
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);
390 atomic_inc(&inode->i_count);
392 affs_fix_checksum(sb, bh);
393 mark_buffer_dirty_inode(bh, inode);
394 dentry->d_fsdata = (void *)(long)bh->b_blocknr;
397 retval = affs_insert_hash(dir, bh);
398 mark_buffer_dirty_inode(bh, inode);
399 affs_unlock_dir(dir);
400 affs_unlock_link(inode);
402 d_instantiate(dentry, inode);
404 affs_brelse(inode_bh);
409 affs_free_block(sb, block);
410 affs_unlock_link(inode);