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
13 #include <asm/div64.h>
14 #include <linux/errno.h>
16 #include <linux/slab.h>
17 #include <linux/stat.h>
18 #include <linux/sched.h>
19 #include <linux/affs_fs.h>
20 #include <linux/kernel.h>
22 #include <linux/string.h>
23 #include <linux/locks.h>
24 #include <linux/genhd.h>
25 #include <linux/amigaffs.h>
26 #include <linux/major.h>
27 #include <linux/blkdev.h>
28 #include <linux/init.h>
29 #include <linux/smp_lock.h>
30 #include <asm/system.h>
31 #include <asm/uaccess.h>
32 #include <linux/module.h>
34 extern struct inode_operations affs_symlink_inode_operations;
35 extern struct timezone sys_tz;
38 affs_read_inode(struct inode *inode)
40 struct super_block *sb = inode->i_sb;
41 struct buffer_head *bh;
42 struct affs_head *head;
43 struct affs_tail *tail;
49 pr_debug("AFFS: read_inode(%lu)\n",inode->i_ino);
52 bh = affs_bread(sb, block);
54 affs_warning(sb, "read_inode", "Cannot read block %d", block);
57 if (affs_checksum_block(sb, bh) || be32_to_cpu(AFFS_HEAD(bh)->ptype) != T_SHORT) {
58 affs_warning(sb,"read_inode",
59 "Checksum or type (ptype=%d) error on inode %d",
60 AFFS_HEAD(bh)->ptype, block);
65 tail = AFFS_TAIL(sb, bh);
66 prot = be32_to_cpu(tail->protect);
71 memset(AFFS_INODE, 0, sizeof(*AFFS_INODE));
72 init_MUTEX(&AFFS_INODE->i_link_lock);
73 init_MUTEX(&AFFS_INODE->i_ext_lock);
74 AFFS_INODE->i_extcnt = 1;
75 AFFS_INODE->i_ext_last = ~1;
76 AFFS_INODE->i_protect = prot;
78 if (AFFS_SB->s_flags & SF_SETMODE)
79 inode->i_mode = AFFS_SB->s_mode;
81 inode->i_mode = prot_to_mode(prot);
83 id = be16_to_cpu(tail->uid);
84 if (id == 0 || AFFS_SB->s_flags & SF_SETUID)
85 inode->i_uid = AFFS_SB->s_uid;
86 else if (id == 0xFFFF && AFFS_SB->s_flags & SF_MUFS)
91 id = be16_to_cpu(tail->gid);
92 if (id == 0 || AFFS_SB->s_flags & SF_SETGID)
93 inode->i_gid = AFFS_SB->s_gid;
94 else if (id == 0xFFFF && AFFS_SB->s_flags & SF_MUFS)
99 switch (be32_to_cpu(tail->stype)) {
101 inode->i_uid = AFFS_SB->s_uid;
102 inode->i_gid = AFFS_SB->s_gid;
105 if (be32_to_cpu(tail->stype) == ST_USERDIR ||
106 AFFS_SB->s_flags & SF_SETMODE) {
107 if (inode->i_mode & S_IRUSR)
108 inode->i_mode |= S_IXUSR;
109 if (inode->i_mode & S_IRGRP)
110 inode->i_mode |= S_IXGRP;
111 if (inode->i_mode & S_IROTH)
112 inode->i_mode |= S_IXOTH;
113 inode->i_mode |= S_IFDIR;
115 inode->i_mode = S_IRUGO | S_IXUGO | S_IWUSR | S_IFDIR;
116 if (tail->link_chain)
118 /* Maybe it should be controlled by mount parameter? */
119 //inode->i_mode |= S_ISVTX;
120 inode->i_op = &affs_dir_inode_operations;
121 inode->i_fop = &affs_dir_operations;
125 affs_warning(sb, "read_inode", "inode is LINKDIR");
128 inode->i_mode |= S_IFDIR;
134 affs_warning(sb, "read_inode", "inode is LINKFILE");
137 size = be32_to_cpu(tail->size);
138 inode->i_mode |= S_IFREG;
139 AFFS_INODE->mmu_private = inode->i_size = size;
141 AFFS_INODE->i_blkcnt = (size - 1) /
142 AFFS_SB->s_data_blksize + 1;
143 AFFS_INODE->i_extcnt = (AFFS_INODE->i_blkcnt - 1) /
144 AFFS_SB->s_hashsize + 1;
146 if (tail->link_chain)
148 inode->i_mapping->a_ops = (AFFS_SB->s_flags & SF_OFS) ? &affs_aops_ofs : &affs_aops;
149 inode->i_op = &affs_file_inode_operations;
150 inode->i_fop = &affs_file_operations;
153 inode->i_mode |= S_IFLNK;
154 inode->i_op = &affs_symlink_inode_operations;
155 inode->i_data.a_ops = &affs_symlink_aops;
159 inode->i_mtime = inode->i_atime = inode->i_ctime
160 = (be32_to_cpu(tail->change.days) * (24 * 60 * 60) +
161 be32_to_cpu(tail->change.mins) * 60 +
162 be32_to_cpu(tail->change.ticks) / 50 +
163 ((8 * 365 + 2) * 24 * 60 * 60)) +
164 sys_tz.tz_minuteswest * 60;
169 make_bad_inode(inode);
175 affs_write_inode(struct inode *inode, int unused)
177 struct super_block *sb = inode->i_sb;
178 struct buffer_head *bh;
179 struct affs_tail *tail;
183 pr_debug("AFFS: write_inode(%lu)\n",inode->i_ino);
186 // possibly free block
189 bh = affs_bread(sb, inode->i_ino);
191 affs_error(sb,"write_inode","Cannot read block %lu",inode->i_ino);
195 tail = AFFS_TAIL(sb, bh);
196 if (tail->stype == be32_to_cpu(ST_ROOT)) {
197 secs_to_datestamp(inode->i_mtime,&AFFS_ROOT_TAIL(sb, bh)->root_change);
199 tail->protect = cpu_to_be32(AFFS_INODE->i_protect);
200 tail->size = cpu_to_be32(inode->i_size);
201 secs_to_datestamp(inode->i_mtime,&tail->change);
202 if (!(inode->i_ino == AFFS_SB->s_root_block)) {
205 if (sb->u.affs_sb.s_flags & SF_MUFS) {
206 if (inode->i_uid == 0 || inode->i_uid == 0xFFFF)
207 uid = inode->i_uid ^ ~0;
208 if (inode->i_gid == 0 || inode->i_gid == 0xFFFF)
209 gid = inode->i_gid ^ ~0;
211 if (!(sb->u.affs_sb.s_flags & SF_SETUID))
212 tail->uid = cpu_to_be16(uid);
213 if (!(sb->u.affs_sb.s_flags & SF_SETGID))
214 tail->gid = cpu_to_be16(gid);
217 affs_fix_checksum(sb, bh);
218 mark_buffer_dirty_inode(bh, inode);
224 affs_notify_change(struct dentry *dentry, struct iattr *attr)
226 struct inode *inode = dentry->d_inode;
229 pr_debug("AFFS: notify_change(%lu,0x%x)\n",inode->i_ino,attr->ia_valid);
231 error = inode_change_ok(inode,attr);
235 if (((attr->ia_valid & ATTR_UID) && (inode->i_sb->u.affs_sb.s_flags & SF_SETUID)) ||
236 ((attr->ia_valid & ATTR_GID) && (inode->i_sb->u.affs_sb.s_flags & SF_SETGID)) ||
237 ((attr->ia_valid & ATTR_MODE) &&
238 (inode->i_sb->u.affs_sb.s_flags & (SF_SETMODE | SF_IMMUTABLE)))) {
239 if (!(inode->i_sb->u.affs_sb.s_flags & SF_QUIET))
244 inode_setattr(inode, attr);
245 if (!error && (attr->ia_valid & ATTR_MODE))
252 affs_put_inode(struct inode *inode)
254 pr_debug("AFFS: put_inode(ino=%lu, nlink=%u)\n", inode->i_ino, inode->i_nlink);
256 affs_free_prealloc(inode);
257 if (atomic_read(&inode->i_count) == 1) {
258 if (inode->i_size != AFFS_INODE->mmu_private)
259 affs_truncate(inode);
260 //if (inode->i_nlink)
261 // affs_clear_inode(inode);
267 affs_delete_inode(struct inode *inode)
269 pr_debug("AFFS: delete_inode(ino=%lu, nlink=%u)\n", inode->i_ino, inode->i_nlink);
272 if (S_ISREG(inode->i_mode))
273 affs_truncate(inode);
275 affs_free_block(inode->i_sb, inode->i_ino);
280 affs_clear_inode(struct inode *inode)
282 unsigned long cache_page = (unsigned long) inode->u.affs_i.i_lc;
284 pr_debug("AFFS: clear_inode(ino=%lu, nlink=%u)\n", inode->i_ino, inode->i_nlink);
286 pr_debug("AFFS: freeing ext cache\n");
287 inode->u.affs_i.i_lc = NULL;
288 inode->u.affs_i.i_ac = NULL;
289 free_page(cache_page);
291 affs_brelse(AFFS_INODE->i_ext_bh);
292 AFFS_INODE->i_ext_last = ~1;
293 AFFS_INODE->i_ext_bh = NULL;
297 affs_new_inode(struct inode *dir)
299 struct super_block *sb = dir->i_sb;
302 struct buffer_head *bh;
304 if (!(inode = new_inode(sb)))
307 if (!(block = affs_alloc_block(dir, dir->i_ino)))
310 bh = affs_getzeroblk(sb, block);
313 mark_buffer_dirty_inode(bh, inode);
316 inode->i_uid = current->fsuid;
317 inode->i_gid = current->fsgid;
318 inode->i_ino = block;
320 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
321 memset(AFFS_INODE, 0, sizeof(*AFFS_INODE));
322 AFFS_INODE->i_extcnt = 1;
323 AFFS_INODE->i_ext_last = ~1;
324 init_MUTEX(&AFFS_INODE->i_link_lock);
325 init_MUTEX(&AFFS_INODE->i_ext_lock);
327 insert_inode_hash(inode);
332 affs_free_block(sb, block);
340 * Add an entry to a directory. Create the header block
341 * and insert it into the hash table.
345 affs_add_entry(struct inode *dir, struct inode *inode, struct dentry *dentry, s32 type)
347 struct super_block *sb = dir->i_sb;
348 struct buffer_head *inode_bh = NULL;
349 struct buffer_head *bh = NULL;
353 pr_debug("AFFS: add_entry(dir=%u, inode=%u, \"%*s\", type=%d)\n", (u32)dir->i_ino,
354 (u32)inode->i_ino, (int)dentry->d_name.len, dentry->d_name.name, type);
357 bh = affs_bread(sb, inode->i_ino);
361 affs_lock_link(inode);
367 block = affs_alloc_block(dir, dir->i_ino);
371 bh = affs_getzeroblk(sb, block);
379 AFFS_HEAD(bh)->ptype = cpu_to_be32(T_SHORT);
380 AFFS_HEAD(bh)->key = cpu_to_be32(bh->b_blocknr);
381 affs_copy_name(AFFS_TAIL(sb, bh)->name, dentry);
382 AFFS_TAIL(sb, bh)->stype = cpu_to_be32(type);
383 AFFS_TAIL(sb, bh)->parent = cpu_to_be32(dir->i_ino);
387 chain = AFFS_TAIL(sb, inode_bh)->link_chain;
388 AFFS_TAIL(sb, bh)->original = cpu_to_be32(inode->i_ino);
389 AFFS_TAIL(sb, bh)->link_chain = chain;
390 AFFS_TAIL(sb, inode_bh)->link_chain = cpu_to_be32(block);
391 affs_adjust_checksum(inode_bh, block - be32_to_cpu(chain));
392 mark_buffer_dirty_inode(inode_bh, inode);
394 atomic_inc(&inode->i_count);
396 affs_fix_checksum(sb, bh);
397 mark_buffer_dirty_inode(bh, inode);
398 dentry->d_fsdata = (void *)bh->b_blocknr;
401 retval = affs_insert_hash(dir, bh);
402 mark_buffer_dirty_inode(bh, inode);
403 affs_unlock_dir(dir);
404 affs_unlock_link(inode);
406 d_instantiate(dentry, inode);
408 affs_brelse(inode_bh);
413 affs_free_block(sb, block);
414 affs_unlock_link(inode);
417 MODULE_LICENSE("GPL");