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 struct inode *affs_iget(struct super_block *sb, unsigned long ino)
18 struct affs_sb_info *sbi = AFFS_SB(sb);
19 struct buffer_head *bh;
20 struct affs_tail *tail;
27 inode = iget_locked(sb, ino);
29 return ERR_PTR(-ENOMEM);
30 if (!(inode->i_state & I_NEW))
33 pr_debug("affs_iget(%lu)\n", inode->i_ino);
36 bh = affs_bread(sb, block);
38 affs_warning(sb, "read_inode", "Cannot read block %d", block);
41 if (affs_checksum_block(sb, bh) || be32_to_cpu(AFFS_HEAD(bh)->ptype) != T_SHORT) {
42 affs_warning(sb,"read_inode",
43 "Checksum or type (ptype=%d) error on inode %d",
44 AFFS_HEAD(bh)->ptype, block);
48 tail = AFFS_TAIL(sb, bh);
49 prot = be32_to_cpu(tail->protect);
54 AFFS_I(inode)->i_extcnt = 1;
55 AFFS_I(inode)->i_ext_last = ~1;
56 AFFS_I(inode)->i_protect = prot;
57 atomic_set(&AFFS_I(inode)->i_opencnt, 0);
58 AFFS_I(inode)->i_blkcnt = 0;
59 AFFS_I(inode)->i_lc = NULL;
60 AFFS_I(inode)->i_lc_size = 0;
61 AFFS_I(inode)->i_lc_shift = 0;
62 AFFS_I(inode)->i_lc_mask = 0;
63 AFFS_I(inode)->i_ac = NULL;
64 AFFS_I(inode)->i_ext_bh = NULL;
65 AFFS_I(inode)->mmu_private = 0;
66 AFFS_I(inode)->i_lastalloc = 0;
67 AFFS_I(inode)->i_pa_cnt = 0;
69 if (affs_test_opt(sbi->s_flags, SF_SETMODE))
70 inode->i_mode = sbi->s_mode;
72 inode->i_mode = prot_to_mode(prot);
74 id = be16_to_cpu(tail->uid);
75 if (id == 0 || affs_test_opt(sbi->s_flags, SF_SETUID))
76 inode->i_uid = sbi->s_uid;
77 else if (id == 0xFFFF && affs_test_opt(sbi->s_flags, SF_MUFS))
78 i_uid_write(inode, 0);
80 i_uid_write(inode, id);
82 id = be16_to_cpu(tail->gid);
83 if (id == 0 || affs_test_opt(sbi->s_flags, SF_SETGID))
84 inode->i_gid = sbi->s_gid;
85 else if (id == 0xFFFF && affs_test_opt(sbi->s_flags, SF_MUFS))
86 i_gid_write(inode, 0);
88 i_gid_write(inode, id);
90 switch (be32_to_cpu(tail->stype)) {
92 inode->i_uid = sbi->s_uid;
93 inode->i_gid = sbi->s_gid;
96 if (be32_to_cpu(tail->stype) == ST_USERDIR ||
97 affs_test_opt(sbi->s_flags, SF_SETMODE)) {
98 if (inode->i_mode & S_IRUSR)
99 inode->i_mode |= S_IXUSR;
100 if (inode->i_mode & S_IRGRP)
101 inode->i_mode |= S_IXGRP;
102 if (inode->i_mode & S_IROTH)
103 inode->i_mode |= S_IXOTH;
104 inode->i_mode |= S_IFDIR;
106 inode->i_mode = S_IRUGO | S_IXUGO | S_IWUSR | S_IFDIR;
107 /* Maybe it should be controlled by mount parameter? */
108 //inode->i_mode |= S_ISVTX;
109 inode->i_op = &affs_dir_inode_operations;
110 inode->i_fop = &affs_dir_operations;
114 affs_warning(sb, "read_inode", "inode is LINKDIR");
117 inode->i_mode |= S_IFDIR;
118 /* ... and leave ->i_op and ->i_fop pointing to empty */
122 affs_warning(sb, "read_inode", "inode is LINKFILE");
125 size = be32_to_cpu(tail->size);
126 inode->i_mode |= S_IFREG;
127 AFFS_I(inode)->mmu_private = inode->i_size = size;
129 AFFS_I(inode)->i_blkcnt = (size - 1) /
130 sbi->s_data_blksize + 1;
131 AFFS_I(inode)->i_extcnt = (AFFS_I(inode)->i_blkcnt - 1) /
134 if (tail->link_chain)
136 inode->i_mapping->a_ops = affs_test_opt(sbi->s_flags, SF_OFS) ?
137 &affs_aops_ofs : &affs_aops;
138 inode->i_op = &affs_file_inode_operations;
139 inode->i_fop = &affs_file_operations;
142 inode->i_mode |= S_IFLNK;
143 inode_nohighmem(inode);
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;
157 unlock_new_inode(inode);
163 return ERR_PTR(-EIO);
167 affs_write_inode(struct inode *inode, struct writeback_control *wbc)
169 struct super_block *sb = inode->i_sb;
170 struct buffer_head *bh;
171 struct affs_tail *tail;
175 pr_debug("write_inode(%lu)\n", inode->i_ino);
178 // possibly free block
180 bh = affs_bread(sb, inode->i_ino);
182 affs_error(sb,"write_inode","Cannot read block %lu",inode->i_ino);
185 tail = AFFS_TAIL(sb, bh);
186 if (tail->stype == cpu_to_be32(ST_ROOT)) {
187 secs_to_datestamp(inode->i_mtime.tv_sec,&AFFS_ROOT_TAIL(sb, bh)->root_change);
189 tail->protect = cpu_to_be32(AFFS_I(inode)->i_protect);
190 tail->size = cpu_to_be32(inode->i_size);
191 secs_to_datestamp(inode->i_mtime.tv_sec,&tail->change);
192 if (!(inode->i_ino == AFFS_SB(sb)->s_root_block)) {
193 uid = i_uid_read(inode);
194 gid = i_gid_read(inode);
195 if (affs_test_opt(AFFS_SB(sb)->s_flags, SF_MUFS)) {
196 if (uid == 0 || uid == 0xFFFF)
198 if (gid == 0 || gid == 0xFFFF)
201 if (!affs_test_opt(AFFS_SB(sb)->s_flags, SF_SETUID))
202 tail->uid = cpu_to_be16(uid);
203 if (!affs_test_opt(AFFS_SB(sb)->s_flags, SF_SETGID))
204 tail->gid = cpu_to_be16(gid);
207 affs_fix_checksum(sb, bh);
208 mark_buffer_dirty_inode(bh, inode);
210 affs_free_prealloc(inode);
215 affs_notify_change(struct dentry *dentry, struct iattr *attr)
217 struct inode *inode = d_inode(dentry);
220 pr_debug("notify_change(%lu,0x%x)\n", inode->i_ino, attr->ia_valid);
222 error = setattr_prepare(dentry, attr);
226 if (((attr->ia_valid & ATTR_UID) &&
227 affs_test_opt(AFFS_SB(inode->i_sb)->s_flags, SF_SETUID)) ||
228 ((attr->ia_valid & ATTR_GID) &&
229 affs_test_opt(AFFS_SB(inode->i_sb)->s_flags, SF_SETGID)) ||
230 ((attr->ia_valid & ATTR_MODE) &&
231 (AFFS_SB(inode->i_sb)->s_flags &
232 (AFFS_MOUNT_SF_SETMODE | AFFS_MOUNT_SF_IMMUTABLE)))) {
233 if (!affs_test_opt(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 = inode_newsize_ok(inode, attr->ia_size);
244 truncate_setsize(inode, attr->ia_size);
245 affs_truncate(inode);
248 setattr_copy(inode, attr);
249 mark_inode_dirty(inode);
251 if (attr->ia_valid & ATTR_MODE)
258 affs_evict_inode(struct inode *inode)
260 unsigned long cache_page;
261 pr_debug("evict_inode(ino=%lu, nlink=%u)\n",
262 inode->i_ino, inode->i_nlink);
263 truncate_inode_pages_final(&inode->i_data);
265 if (!inode->i_nlink) {
267 affs_truncate(inode);
270 invalidate_inode_buffers(inode);
272 affs_free_prealloc(inode);
273 cache_page = (unsigned long)AFFS_I(inode)->i_lc;
275 pr_debug("freeing ext cache\n");
276 AFFS_I(inode)->i_lc = NULL;
277 AFFS_I(inode)->i_ac = NULL;
278 free_page(cache_page);
280 affs_brelse(AFFS_I(inode)->i_ext_bh);
281 AFFS_I(inode)->i_ext_last = ~1;
282 AFFS_I(inode)->i_ext_bh = NULL;
285 affs_free_block(inode->i_sb, inode->i_ino);
289 affs_new_inode(struct inode *dir)
291 struct super_block *sb = dir->i_sb;
294 struct buffer_head *bh;
296 if (!(inode = new_inode(sb)))
299 if (!(block = affs_alloc_block(dir, dir->i_ino)))
302 bh = affs_getzeroblk(sb, block);
305 mark_buffer_dirty_inode(bh, inode);
308 inode->i_uid = current_fsuid();
309 inode->i_gid = current_fsgid();
310 inode->i_ino = block;
312 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
313 atomic_set(&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;
328 insert_inode_hash(inode);
333 affs_free_block(sb, block);
341 * Add an entry to a directory. Create the header block
342 * and insert it into the hash table.
346 affs_add_entry(struct inode *dir, struct inode *inode, struct dentry *dentry, s32 type)
348 struct super_block *sb = dir->i_sb;
349 struct buffer_head *inode_bh = NULL;
350 struct buffer_head *bh;
354 pr_debug("%s(dir=%lu, inode=%lu, \"%pd\", type=%d)\n", __func__,
355 dir->i_ino, inode->i_ino, dentry, type);
358 bh = affs_bread(sb, inode->i_ino);
362 affs_lock_link(inode);
367 block = affs_alloc_block(dir, dir->i_ino);
372 bh = affs_getzeroblk(sb, block);
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);
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);
397 affs_fix_checksum(sb, bh);
398 mark_buffer_dirty_inode(bh, inode);
399 dentry->d_fsdata = (void *)(long)bh->b_blocknr;
402 retval = affs_insert_hash(dir, bh);
403 mark_buffer_dirty_inode(bh, inode);
404 affs_unlock_dir(dir);
405 affs_unlock_link(inode);
407 d_instantiate(dentry, inode);
409 affs_brelse(inode_bh);
414 affs_free_block(sb, block);
415 affs_unlock_link(inode);