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
12 #include <linux/sched.h>
13 #include <linux/gfp.h>
16 extern const struct inode_operations affs_symlink_inode_operations;
17 extern struct timezone sys_tz;
19 struct inode *affs_iget(struct super_block *sb, unsigned long ino)
21 struct affs_sb_info *sbi = AFFS_SB(sb);
22 struct buffer_head *bh;
23 struct affs_head *head;
24 struct affs_tail *tail;
31 inode = iget_locked(sb, ino);
33 return ERR_PTR(-ENOMEM);
34 if (!(inode->i_state & I_NEW))
37 pr_debug("AFFS: affs_iget(%lu)\n", inode->i_ino);
40 bh = affs_bread(sb, block);
42 affs_warning(sb, "read_inode", "Cannot read block %d", block);
45 if (affs_checksum_block(sb, bh) || be32_to_cpu(AFFS_HEAD(bh)->ptype) != T_SHORT) {
46 affs_warning(sb,"read_inode",
47 "Checksum or type (ptype=%d) error on inode %d",
48 AFFS_HEAD(bh)->ptype, block);
53 tail = AFFS_TAIL(sb, bh);
54 prot = be32_to_cpu(tail->protect);
59 AFFS_I(inode)->i_extcnt = 1;
60 AFFS_I(inode)->i_ext_last = ~1;
61 AFFS_I(inode)->i_protect = prot;
62 atomic_set(&AFFS_I(inode)->i_opencnt, 0);
63 AFFS_I(inode)->i_blkcnt = 0;
64 AFFS_I(inode)->i_lc = NULL;
65 AFFS_I(inode)->i_lc_size = 0;
66 AFFS_I(inode)->i_lc_shift = 0;
67 AFFS_I(inode)->i_lc_mask = 0;
68 AFFS_I(inode)->i_ac = NULL;
69 AFFS_I(inode)->i_ext_bh = NULL;
70 AFFS_I(inode)->mmu_private = 0;
71 AFFS_I(inode)->i_lastalloc = 0;
72 AFFS_I(inode)->i_pa_cnt = 0;
74 if (sbi->s_flags & SF_SETMODE)
75 inode->i_mode = sbi->s_mode;
77 inode->i_mode = prot_to_mode(prot);
79 id = be16_to_cpu(tail->uid);
80 if (id == 0 || sbi->s_flags & SF_SETUID)
81 inode->i_uid = sbi->s_uid;
82 else if (id == 0xFFFF && sbi->s_flags & SF_MUFS)
87 id = be16_to_cpu(tail->gid);
88 if (id == 0 || sbi->s_flags & SF_SETGID)
89 inode->i_gid = sbi->s_gid;
90 else if (id == 0xFFFF && sbi->s_flags & SF_MUFS)
95 switch (be32_to_cpu(tail->stype)) {
97 inode->i_uid = sbi->s_uid;
98 inode->i_gid = sbi->s_gid;
101 if (be32_to_cpu(tail->stype) == ST_USERDIR ||
102 sbi->s_flags & SF_SETMODE) {
103 if (inode->i_mode & S_IRUSR)
104 inode->i_mode |= S_IXUSR;
105 if (inode->i_mode & S_IRGRP)
106 inode->i_mode |= S_IXGRP;
107 if (inode->i_mode & S_IROTH)
108 inode->i_mode |= S_IXOTH;
109 inode->i_mode |= S_IFDIR;
111 inode->i_mode = S_IRUGO | S_IXUGO | S_IWUSR | S_IFDIR;
112 /* Maybe it should be controlled by mount parameter? */
113 //inode->i_mode |= S_ISVTX;
114 inode->i_op = &affs_dir_inode_operations;
115 inode->i_fop = &affs_dir_operations;
119 affs_warning(sb, "read_inode", "inode is LINKDIR");
122 inode->i_mode |= S_IFDIR;
123 /* ... and leave ->i_op and ->i_fop pointing to empty */
127 affs_warning(sb, "read_inode", "inode is LINKFILE");
130 size = be32_to_cpu(tail->size);
131 inode->i_mode |= S_IFREG;
132 AFFS_I(inode)->mmu_private = inode->i_size = size;
134 AFFS_I(inode)->i_blkcnt = (size - 1) /
135 sbi->s_data_blksize + 1;
136 AFFS_I(inode)->i_extcnt = (AFFS_I(inode)->i_blkcnt - 1) /
139 if (tail->link_chain)
141 inode->i_mapping->a_ops = (sbi->s_flags & SF_OFS) ? &affs_aops_ofs : &affs_aops;
142 inode->i_op = &affs_file_inode_operations;
143 inode->i_fop = &affs_file_operations;
146 inode->i_mode |= S_IFLNK;
147 inode->i_op = &affs_symlink_inode_operations;
148 inode->i_data.a_ops = &affs_symlink_aops;
152 inode->i_mtime.tv_sec = inode->i_atime.tv_sec = inode->i_ctime.tv_sec
153 = (be32_to_cpu(tail->change.days) * (24 * 60 * 60) +
154 be32_to_cpu(tail->change.mins) * 60 +
155 be32_to_cpu(tail->change.ticks) / 50 +
156 ((8 * 365 + 2) * 24 * 60 * 60)) +
157 sys_tz.tz_minuteswest * 60;
158 inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec = inode->i_atime.tv_nsec = 0;
160 unlock_new_inode(inode);
166 return ERR_PTR(-EIO);
170 affs_write_inode(struct inode *inode, struct writeback_control *wbc)
172 struct super_block *sb = inode->i_sb;
173 struct buffer_head *bh;
174 struct affs_tail *tail;
178 pr_debug("AFFS: write_inode(%lu)\n",inode->i_ino);
181 // possibly free block
183 bh = affs_bread(sb, inode->i_ino);
185 affs_error(sb,"write_inode","Cannot read block %lu",inode->i_ino);
188 tail = AFFS_TAIL(sb, bh);
189 if (tail->stype == cpu_to_be32(ST_ROOT)) {
190 secs_to_datestamp(inode->i_mtime.tv_sec,&AFFS_ROOT_TAIL(sb, bh)->root_change);
192 tail->protect = cpu_to_be32(AFFS_I(inode)->i_protect);
193 tail->size = cpu_to_be32(inode->i_size);
194 secs_to_datestamp(inode->i_mtime.tv_sec,&tail->change);
195 if (!(inode->i_ino == AFFS_SB(sb)->s_root_block)) {
198 if (AFFS_SB(sb)->s_flags & SF_MUFS) {
199 if (inode->i_uid == 0 || inode->i_uid == 0xFFFF)
200 uid = inode->i_uid ^ ~0;
201 if (inode->i_gid == 0 || inode->i_gid == 0xFFFF)
202 gid = inode->i_gid ^ ~0;
204 if (!(AFFS_SB(sb)->s_flags & SF_SETUID))
205 tail->uid = cpu_to_be16(uid);
206 if (!(AFFS_SB(sb)->s_flags & SF_SETGID))
207 tail->gid = cpu_to_be16(gid);
210 affs_fix_checksum(sb, bh);
211 mark_buffer_dirty_inode(bh, inode);
213 affs_free_prealloc(inode);
218 affs_notify_change(struct dentry *dentry, struct iattr *attr)
220 struct inode *inode = dentry->d_inode;
223 pr_debug("AFFS: notify_change(%lu,0x%x)\n",inode->i_ino,attr->ia_valid);
225 error = inode_change_ok(inode,attr);
229 if (((attr->ia_valid & ATTR_UID) && (AFFS_SB(inode->i_sb)->s_flags & SF_SETUID)) ||
230 ((attr->ia_valid & ATTR_GID) && (AFFS_SB(inode->i_sb)->s_flags & SF_SETGID)) ||
231 ((attr->ia_valid & ATTR_MODE) &&
232 (AFFS_SB(inode->i_sb)->s_flags & (SF_SETMODE | SF_IMMUTABLE)))) {
233 if (!(AFFS_SB(inode->i_sb)->s_flags & SF_QUIET))
238 if ((attr->ia_valid & ATTR_SIZE) &&
239 attr->ia_size != i_size_read(inode)) {
240 error = vmtruncate(inode, attr->ia_size);
245 setattr_copy(inode, attr);
246 mark_inode_dirty(inode);
248 if (attr->ia_valid & ATTR_MODE)
255 affs_evict_inode(struct inode *inode)
257 unsigned long cache_page;
258 pr_debug("AFFS: evict_inode(ino=%lu, nlink=%u)\n", inode->i_ino, inode->i_nlink);
259 truncate_inode_pages(&inode->i_data, 0);
261 if (!inode->i_nlink) {
263 affs_truncate(inode);
266 invalidate_inode_buffers(inode);
268 affs_free_prealloc(inode);
269 cache_page = (unsigned long)AFFS_I(inode)->i_lc;
271 pr_debug("AFFS: freeing ext cache\n");
272 AFFS_I(inode)->i_lc = NULL;
273 AFFS_I(inode)->i_ac = NULL;
274 free_page(cache_page);
276 affs_brelse(AFFS_I(inode)->i_ext_bh);
277 AFFS_I(inode)->i_ext_last = ~1;
278 AFFS_I(inode)->i_ext_bh = NULL;
281 affs_free_block(inode->i_sb, inode->i_ino);
285 affs_new_inode(struct inode *dir)
287 struct super_block *sb = dir->i_sb;
290 struct buffer_head *bh;
292 if (!(inode = new_inode(sb)))
295 if (!(block = affs_alloc_block(dir, dir->i_ino)))
298 bh = affs_getzeroblk(sb, block);
301 mark_buffer_dirty_inode(bh, inode);
304 inode->i_uid = current_fsuid();
305 inode->i_gid = current_fsgid();
306 inode->i_ino = block;
308 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
309 atomic_set(&AFFS_I(inode)->i_opencnt, 0);
310 AFFS_I(inode)->i_blkcnt = 0;
311 AFFS_I(inode)->i_lc = NULL;
312 AFFS_I(inode)->i_lc_size = 0;
313 AFFS_I(inode)->i_lc_shift = 0;
314 AFFS_I(inode)->i_lc_mask = 0;
315 AFFS_I(inode)->i_ac = NULL;
316 AFFS_I(inode)->i_ext_bh = NULL;
317 AFFS_I(inode)->mmu_private = 0;
318 AFFS_I(inode)->i_protect = 0;
319 AFFS_I(inode)->i_lastalloc = 0;
320 AFFS_I(inode)->i_pa_cnt = 0;
321 AFFS_I(inode)->i_extcnt = 1;
322 AFFS_I(inode)->i_ext_last = ~1;
324 insert_inode_hash(inode);
329 affs_free_block(sb, block);
337 * Add an entry to a directory. Create the header block
338 * and insert it into the hash table.
342 affs_add_entry(struct inode *dir, struct inode *inode, struct dentry *dentry, s32 type)
344 struct super_block *sb = dir->i_sb;
345 struct buffer_head *inode_bh = NULL;
346 struct buffer_head *bh = NULL;
350 pr_debug("AFFS: add_entry(dir=%u, inode=%u, \"%*s\", type=%d)\n", (u32)dir->i_ino,
351 (u32)inode->i_ino, (int)dentry->d_name.len, dentry->d_name.name, type);
354 bh = affs_bread(sb, inode->i_ino);
358 affs_lock_link(inode);
363 block = affs_alloc_block(dir, dir->i_ino);
368 bh = affs_getzeroblk(sb, block);
376 AFFS_HEAD(bh)->ptype = cpu_to_be32(T_SHORT);
377 AFFS_HEAD(bh)->key = cpu_to_be32(bh->b_blocknr);
378 affs_copy_name(AFFS_TAIL(sb, bh)->name, dentry);
379 AFFS_TAIL(sb, bh)->stype = cpu_to_be32(type);
380 AFFS_TAIL(sb, bh)->parent = cpu_to_be32(dir->i_ino);
384 chain = AFFS_TAIL(sb, inode_bh)->link_chain;
385 AFFS_TAIL(sb, bh)->original = cpu_to_be32(inode->i_ino);
386 AFFS_TAIL(sb, bh)->link_chain = chain;
387 AFFS_TAIL(sb, inode_bh)->link_chain = cpu_to_be32(block);
388 affs_adjust_checksum(inode_bh, block - be32_to_cpu(chain));
389 mark_buffer_dirty_inode(inode_bh, inode);
393 affs_fix_checksum(sb, bh);
394 mark_buffer_dirty_inode(bh, inode);
395 dentry->d_fsdata = (void *)(long)bh->b_blocknr;
398 retval = affs_insert_hash(dir, bh);
399 mark_buffer_dirty_inode(bh, inode);
400 affs_unlock_dir(dir);
401 affs_unlock_link(inode);
403 d_instantiate(dentry, inode);
405 affs_brelse(inode_bh);
410 affs_free_block(sb, block);
411 affs_unlock_link(inode);