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>
15 extern const 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
);
249 affs_drop_inode(struct inode
*inode
)
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
);
256 generic_drop_inode(inode
);
260 affs_delete_inode(struct inode
*inode
)
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);
265 if (S_ISREG(inode
->i_mode
))
266 affs_truncate(inode
);
268 affs_free_block(inode
->i_sb
, inode
->i_ino
);
272 affs_clear_inode(struct inode
*inode
)
274 unsigned long cache_page
= (unsigned long) AFFS_I(inode
)->i_lc
;
276 pr_debug("AFFS: clear_inode(ino=%lu, nlink=%u)\n", inode
->i_ino
, inode
->i_nlink
);
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
);
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
;
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_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;
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
= NULL
;
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
);
358 bh
= affs_bread(sb
, inode
->i_ino
);
362 affs_lock_link(inode
);
368 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
);
395 atomic_inc(&inode
->i_count
);
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
);