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
14 #include <linux/errno.h>
16 #include <linux/malloc.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 <asm/system.h>
30 #include <asm/uaccess.h>
32 extern int *blk_size
[];
33 extern struct timezone sys_tz
;
35 #define MIN(a,b) (((a)<(b))?(a):(b))
38 affs_parent_ino(struct inode
*dir
)
40 int root_ino
= (dir
->i_sb
->u
.affs_sb
.s_root_block
);
42 if (!S_ISDIR(dir
->i_mode
)) {
43 affs_error(dir
->i_sb
,"parent_ino","Trying to get parent of non-directory");
46 if (dir
->i_ino
== root_ino
)
48 return dir
->u
.affs_i
.i_parent
;
52 affs_read_inode(struct inode
*inode
)
54 struct buffer_head
*bh
;
55 struct file_front
*file_front
;
56 struct file_end
*file_end
;
62 pr_debug("AFFS: read_inode(%lu)\n",inode
->i_ino
);
65 if (!(bh
= affs_bread(inode
->i_dev
,block
,AFFS_I2BSIZE(inode
)))) {
66 affs_error(inode
->i_sb
,"read_inode","Cannot read block %d",block
);
69 if (affs_checksum_block(AFFS_I2BSIZE(inode
),bh
->b_data
,&ptype
,&stype
) || ptype
!= T_SHORT
) {
70 affs_error(inode
->i_sb
,"read_inode",
71 "Checksum or type (ptype=%d) error on inode %d",ptype
,block
);
76 file_front
= (struct file_front
*)bh
->b_data
;
77 file_end
= GET_END_PTR(struct file_end
, bh
->b_data
,AFFS_I2BSIZE(inode
));
78 prot
= (be32_to_cpu(file_end
->protect
) & ~0x10) ^ FIBF_OWNER
;
80 inode
->u
.affs_i
.i_protect
= prot
;
81 inode
->u
.affs_i
.i_parent
= be32_to_cpu(file_end
->parent
);
82 inode
->u
.affs_i
.i_original
= 0;
83 inode
->u
.affs_i
.i_zone
= 0;
84 inode
->u
.affs_i
.i_hlink
= 0;
85 inode
->u
.affs_i
.i_pa_cnt
= 0;
86 inode
->u
.affs_i
.i_pa_next
= 0;
87 inode
->u
.affs_i
.i_pa_last
= 0;
88 inode
->u
.affs_i
.i_ec
= NULL
;
89 inode
->u
.affs_i
.i_lastblock
= -1;
93 if (inode
->i_sb
->u
.affs_sb
.s_flags
& SF_SETMODE
)
94 inode
->i_mode
= inode
->i_sb
->u
.affs_sb
.s_mode
;
96 inode
->i_mode
= prot_to_mode(prot
);
98 if (inode
->i_sb
->u
.affs_sb
.s_flags
& SF_SETUID
)
99 inode
->i_uid
= inode
->i_sb
->u
.affs_sb
.s_uid
;
100 id
= be16_to_cpu(file_end
->owner_uid
);
101 if (id
== 0 || inode
->i_sb
->u
.affs_sb
.s_flags
& SF_SETUID
)
102 inode
->i_uid
= inode
->i_sb
->u
.affs_sb
.s_uid
;
103 else if (id
== 0xFFFF && inode
->i_sb
->u
.affs_sb
.s_flags
& SF_MUFS
)
108 id
= be16_to_cpu(file_end
->owner_gid
);
109 if (id
== 0 || inode
->i_sb
->u
.affs_sb
.s_flags
& SF_SETGID
)
110 inode
->i_gid
= inode
->i_sb
->u
.affs_sb
.s_gid
;
111 else if (id
== 0xFFFF && inode
->i_sb
->u
.affs_sb
.s_flags
& SF_MUFS
)
116 switch (be32_to_cpu(file_end
->secondary_type
)) {
118 inode
->i_uid
= inode
->i_sb
->u
.affs_sb
.s_uid
;
119 inode
->i_gid
= inode
->i_sb
->u
.affs_sb
.s_gid
;
121 if (be32_to_cpu(file_end
->secondary_type
) == ST_USERDIR
||
122 inode
->i_sb
->u
.affs_sb
.s_flags
& SF_SETMODE
) {
123 if (inode
->i_mode
& S_IRUSR
)
124 inode
->i_mode
|= S_IXUSR
;
125 if (inode
->i_mode
& S_IRGRP
)
126 inode
->i_mode
|= S_IXGRP
;
127 if (inode
->i_mode
& S_IROTH
)
128 inode
->i_mode
|= S_IXOTH
;
129 inode
->i_mode
|= S_IFDIR
;
131 inode
->i_mode
= S_IRUGO
| S_IXUGO
| S_IWUSR
| S_IFDIR
;
135 affs_error(inode
->i_sb
,"read_inode","inode is LINKDIR");
139 affs_error(inode
->i_sb
,"read_inode","inode is LINKFILE");
143 inode
->i_mode
|= S_IFREG
;
144 inode
->i_size
= be32_to_cpu(file_end
->byte_size
);
145 if (inode
->i_sb
->u
.affs_sb
.s_flags
& SF_OFS
)
146 block
= AFFS_I2BSIZE(inode
) - 24;
148 block
= AFFS_I2BSIZE(inode
);
149 inode
->u
.affs_i
.i_lastblock
= ((inode
->i_size
+ block
- 1) / block
) - 1;
152 inode
->i_mode
|= S_IFLNK
;
157 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
158 = (be32_to_cpu(file_end
->created
.ds_Days
) * (24 * 60 * 60) +
159 be32_to_cpu(file_end
->created
.ds_Minute
) * 60 +
160 be32_to_cpu(file_end
->created
.ds_Tick
) / 50 +
161 ((8 * 365 + 2) * 24 * 60 * 60)) +
162 sys_tz
.tz_minuteswest
* 60;
166 if (S_ISREG(inode
->i_mode
)) {
167 if (inode
->i_sb
->u
.affs_sb
.s_flags
& SF_OFS
) {
168 inode
->i_op
= &affs_file_inode_operations_ofs
;
170 inode
->i_op
= &affs_file_inode_operations
;
172 } else if (S_ISDIR(inode
->i_mode
)) {
173 /* Maybe it should be controlled by mount parameter? */
174 inode
->i_mode
|= S_ISVTX
;
175 inode
->i_op
= &affs_dir_inode_operations
;
177 else if (S_ISLNK(inode
->i_mode
))
178 inode
->i_op
= &affs_symlink_inode_operations
;
182 affs_write_inode(struct inode
*inode
)
184 struct buffer_head
*bh
;
185 struct file_end
*file_end
;
189 pr_debug("AFFS: write_inode(%lu)\n",inode
->i_ino
);
193 if (!(bh
= bread(inode
->i_dev
,inode
->i_ino
,AFFS_I2BSIZE(inode
)))) {
194 affs_error(inode
->i_sb
,"write_inode","Cannot read block %lu",inode
->i_ino
);
197 file_end
= GET_END_PTR(struct file_end
, bh
->b_data
,AFFS_I2BSIZE(inode
));
198 if (file_end
->secondary_type
== be32_to_cpu(ST_ROOT
)) {
199 secs_to_datestamp(inode
->i_mtime
,&ROOT_END(bh
->b_data
,inode
)->disk_altered
);
201 file_end
->protect
= cpu_to_be32(inode
->u
.affs_i
.i_protect
^ FIBF_OWNER
);
202 file_end
->byte_size
= cpu_to_be32(inode
->i_size
);
203 secs_to_datestamp(inode
->i_mtime
,&file_end
->created
);
204 if (!(inode
->i_ino
== inode
->i_sb
->u
.affs_sb
.s_root_block
)) {
207 if (inode
->i_sb
->u
.affs_sb
.s_flags
& SF_MUFS
) {
208 if (inode
->i_uid
== 0 || inode
->i_uid
== 0xFFFF)
209 uid
= inode
->i_uid
^ ~0;
210 if (inode
->i_gid
== 0 || inode
->i_gid
== 0xFFFF)
211 gid
= inode
->i_gid
^ ~0;
213 if (!(inode
->i_sb
->u
.affs_sb
.s_flags
& SF_SETUID
))
214 file_end
->owner_uid
= cpu_to_be16(uid
);
215 if (!(inode
->i_sb
->u
.affs_sb
.s_flags
& SF_SETGID
))
216 file_end
->owner_gid
= cpu_to_be16(gid
);
219 affs_fix_checksum(AFFS_I2BSIZE(inode
),bh
->b_data
,5);
220 mark_buffer_dirty(bh
,1);
225 affs_notify_change(struct dentry
*dentry
, struct iattr
*attr
)
227 struct inode
*inode
= dentry
->d_inode
;
230 pr_debug("AFFS: notify_change(%lu,0x%x)\n",inode
->i_ino
,attr
->ia_valid
);
232 error
= inode_change_ok(inode
,attr
);
236 if (((attr
->ia_valid
& ATTR_UID
) && (inode
->i_sb
->u
.affs_sb
.s_flags
& SF_SETUID
)) ||
237 ((attr
->ia_valid
& ATTR_GID
) && (inode
->i_sb
->u
.affs_sb
.s_flags
& SF_SETGID
)) ||
238 ((attr
->ia_valid
& ATTR_MODE
) &&
239 (inode
->i_sb
->u
.affs_sb
.s_flags
& (SF_SETMODE
| SF_IMMUTABLE
)))) {
240 if (!(inode
->i_sb
->u
.affs_sb
.s_flags
& SF_QUIET
))
245 if (attr
->ia_valid
& ATTR_MODE
)
246 inode
->u
.affs_i
.i_protect
= mode_to_prot(attr
->ia_mode
);
248 inode_setattr(inode
, attr
);
249 mark_inode_dirty(inode
);
256 affs_put_inode(struct inode
*inode
)
258 pr_debug("AFFS: put_inode(ino=%lu, nlink=%u)\n",
259 inode
->i_ino
,inode
->i_nlink
);
261 affs_free_prealloc(inode
);
262 if (inode
->i_count
== 1) {
263 unsigned long cache_page
= (unsigned long) inode
->u
.affs_i
.i_ec
;
265 pr_debug("AFFS: freeing ext cache\n");
266 inode
->u
.affs_i
.i_ec
= NULL
;
267 free_page(cache_page
);
273 affs_delete_inode(struct inode
*inode
)
275 pr_debug("AFFS: delete_inode(ino=%lu, nlink=%u)\n",inode
->i_ino
,inode
->i_nlink
);
277 if (S_ISREG(inode
->i_mode
) && !inode
->u
.affs_i
.i_hlink
)
278 affs_truncate(inode
);
279 affs_free_block(inode
->i_sb
,inode
->i_ino
);
284 affs_new_inode(const struct inode
*dir
)
287 struct super_block
*sb
;
290 if (!dir
|| !(inode
= get_empty_inode()))
297 if (!(block
= affs_new_header((struct inode
*)dir
))) {
304 inode
->i_dev
= sb
->s_dev
;
305 inode
->i_uid
= current
->fsuid
;
306 inode
->i_gid
= current
->fsgid
;
307 inode
->i_ino
= block
;
312 inode
->i_blksize
= 0;
313 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
315 inode
->u
.affs_i
.i_original
= 0;
316 inode
->u
.affs_i
.i_parent
= dir
->i_ino
;
317 inode
->u
.affs_i
.i_zone
= 0;
318 inode
->u
.affs_i
.i_hlink
= 0;
319 inode
->u
.affs_i
.i_pa_cnt
= 0;
320 inode
->u
.affs_i
.i_pa_next
= 0;
321 inode
->u
.affs_i
.i_pa_last
= 0;
322 inode
->u
.affs_i
.i_ec
= NULL
;
323 inode
->u
.affs_i
.i_lastblock
= -1;
325 insert_inode_hash(inode
);
326 mark_inode_dirty(inode
);
332 * Add an entry to a directory. Create the header block
333 * and insert it into the hash table.
337 affs_add_entry(struct inode
*dir
, struct inode
*link
, struct inode
*inode
,
338 struct dentry
*dentry
, int type
)
340 struct buffer_head
*dir_bh
;
341 struct buffer_head
*inode_bh
;
342 struct buffer_head
*link_bh
;
344 const unsigned char *name
= dentry
->d_name
.name
;
345 int len
= dentry
->d_name
.len
;
347 pr_debug("AFFS: add_entry(dir=%lu,inode=%lu,\"%*s\",type=%d)\n",dir
->i_ino
,inode
->i_ino
,
350 if ((retval
= affs_check_name(name
,len
)))
355 dir_bh
= affs_bread(dir
->i_dev
,dir
->i_ino
,AFFS_I2BSIZE(dir
));
356 inode_bh
= affs_bread(inode
->i_dev
,inode
->i_ino
,AFFS_I2BSIZE(inode
));
359 if (!dir_bh
|| !inode_bh
)
362 link_bh
= affs_bread(link
->i_dev
,link
->i_ino
,AFFS_I2BSIZE(link
));
366 ((struct dir_front
*)inode_bh
->b_data
)->primary_type
= cpu_to_be32(T_SHORT
);
367 ((struct dir_front
*)inode_bh
->b_data
)->own_key
= cpu_to_be32(inode
->i_ino
);
368 DIR_END(inode_bh
->b_data
,inode
)->dir_name
[0] = len
;
369 strncpy(DIR_END(inode_bh
->b_data
,inode
)->dir_name
+ 1,name
,len
);
370 DIR_END(inode_bh
->b_data
,inode
)->secondary_type
= cpu_to_be32(type
);
371 DIR_END(inode_bh
->b_data
,inode
)->parent
= cpu_to_be32(dir
->i_ino
);
373 lock_super(inode
->i_sb
);
374 retval
= affs_insert_hash(dir
->i_ino
,inode_bh
,dir
);
377 LINK_END(inode_bh
->b_data
,inode
)->original
= cpu_to_be32(link
->i_ino
);
378 LINK_END(inode_bh
->b_data
,inode
)->link_chain
=
379 FILE_END(link_bh
->b_data
,link
)->link_chain
;
380 FILE_END(link_bh
->b_data
,link
)->link_chain
= cpu_to_be32(inode
->i_ino
);
381 affs_fix_checksum(AFFS_I2BSIZE(link
),link_bh
->b_data
,5);
382 link
->i_version
= ++event
;
383 mark_inode_dirty(link
);
384 mark_buffer_dirty(link_bh
,1);
386 affs_fix_checksum(AFFS_I2BSIZE(inode
),inode_bh
->b_data
,5);
387 affs_fix_checksum(AFFS_I2BSIZE(dir
),dir_bh
->b_data
,5);
388 dir
->i_version
= ++event
;
389 dir
->i_mtime
= dir
->i_atime
= dir
->i_ctime
= CURRENT_TIME
;
390 unlock_super(inode
->i_sb
);
392 mark_inode_dirty(dir
);
393 mark_inode_dirty(inode
);
394 mark_buffer_dirty(dir_bh
,1);
395 mark_buffer_dirty(inode_bh
,1);
399 affs_brelse(inode_bh
);
400 affs_brelse(link_bh
);