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
;
34 extern struct inode_operations affs_symlink_inode_operations
;
36 #define MIN(a,b) (((a)<(b))?(a):(b))
39 affs_parent_ino(struct inode
*dir
)
41 int root_ino
= (dir
->i_sb
->u
.affs_sb
.s_root_block
);
43 if (!S_ISDIR(dir
->i_mode
)) {
44 affs_error(dir
->i_sb
,"parent_ino","Trying to get parent of non-directory");
47 if (dir
->i_ino
== root_ino
)
49 return dir
->u
.affs_i
.i_parent
;
53 affs_read_inode(struct inode
*inode
)
55 struct buffer_head
*bh
;
56 struct file_front
*file_front
;
57 struct file_end
*file_end
;
63 pr_debug("AFFS: read_inode(%lu)\n",inode
->i_ino
);
66 if (!(bh
= affs_bread(inode
->i_dev
,block
,AFFS_I2BSIZE(inode
)))) {
67 affs_error(inode
->i_sb
,"read_inode","Cannot read block %d",block
);
70 if (affs_checksum_block(AFFS_I2BSIZE(inode
),bh
->b_data
,&ptype
,&stype
) || ptype
!= T_SHORT
) {
71 affs_error(inode
->i_sb
,"read_inode",
72 "Checksum or type (ptype=%d) error on inode %d",ptype
,block
);
77 file_front
= (struct file_front
*)bh
->b_data
;
78 file_end
= GET_END_PTR(struct file_end
, bh
->b_data
,AFFS_I2BSIZE(inode
));
79 prot
= (be32_to_cpu(file_end
->protect
) & ~0x10) ^ FIBF_OWNER
;
81 inode
->u
.affs_i
.i_protect
= prot
;
82 inode
->u
.affs_i
.i_parent
= be32_to_cpu(file_end
->parent
);
83 inode
->u
.affs_i
.i_original
= 0;
84 inode
->u
.affs_i
.i_zone
= 0;
85 inode
->u
.affs_i
.i_hlink
= 0;
86 inode
->u
.affs_i
.i_pa_cnt
= 0;
87 inode
->u
.affs_i
.i_pa_next
= 0;
88 inode
->u
.affs_i
.i_pa_last
= 0;
89 inode
->u
.affs_i
.i_ec
= NULL
;
90 inode
->u
.affs_i
.i_lastblock
= -1;
94 if (inode
->i_sb
->u
.affs_sb
.s_flags
& SF_SETMODE
)
95 inode
->i_mode
= inode
->i_sb
->u
.affs_sb
.s_mode
;
97 inode
->i_mode
= prot_to_mode(prot
);
99 if (inode
->i_sb
->u
.affs_sb
.s_flags
& SF_SETUID
)
100 inode
->i_uid
= inode
->i_sb
->u
.affs_sb
.s_uid
;
101 id
= be16_to_cpu(file_end
->owner_uid
);
102 if (id
== 0 || inode
->i_sb
->u
.affs_sb
.s_flags
& SF_SETUID
)
103 inode
->i_uid
= inode
->i_sb
->u
.affs_sb
.s_uid
;
104 else if (id
== 0xFFFF && inode
->i_sb
->u
.affs_sb
.s_flags
& SF_MUFS
)
109 id
= be16_to_cpu(file_end
->owner_gid
);
110 if (id
== 0 || inode
->i_sb
->u
.affs_sb
.s_flags
& SF_SETGID
)
111 inode
->i_gid
= inode
->i_sb
->u
.affs_sb
.s_gid
;
112 else if (id
== 0xFFFF && inode
->i_sb
->u
.affs_sb
.s_flags
& SF_MUFS
)
117 switch (be32_to_cpu(file_end
->secondary_type
)) {
119 inode
->i_uid
= inode
->i_sb
->u
.affs_sb
.s_uid
;
120 inode
->i_gid
= inode
->i_sb
->u
.affs_sb
.s_gid
;
122 if (be32_to_cpu(file_end
->secondary_type
) == ST_USERDIR
||
123 inode
->i_sb
->u
.affs_sb
.s_flags
& SF_SETMODE
) {
124 if (inode
->i_mode
& S_IRUSR
)
125 inode
->i_mode
|= S_IXUSR
;
126 if (inode
->i_mode
& S_IRGRP
)
127 inode
->i_mode
|= S_IXGRP
;
128 if (inode
->i_mode
& S_IROTH
)
129 inode
->i_mode
|= S_IXOTH
;
130 inode
->i_mode
|= S_IFDIR
;
132 inode
->i_mode
= S_IRUGO
| S_IXUGO
| S_IWUSR
| S_IFDIR
;
136 affs_error(inode
->i_sb
,"read_inode","inode is LINKDIR");
140 affs_error(inode
->i_sb
,"read_inode","inode is LINKFILE");
144 inode
->i_mode
|= S_IFREG
;
145 inode
->i_size
= be32_to_cpu(file_end
->byte_size
);
146 if (inode
->i_sb
->u
.affs_sb
.s_flags
& SF_OFS
)
147 block
= AFFS_I2BSIZE(inode
) - 24;
149 block
= AFFS_I2BSIZE(inode
);
150 inode
->u
.affs_i
.i_lastblock
= ((inode
->i_size
+ block
- 1) / block
) - 1;
153 inode
->i_mode
|= S_IFLNK
;
158 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
159 = (be32_to_cpu(file_end
->created
.ds_Days
) * (24 * 60 * 60) +
160 be32_to_cpu(file_end
->created
.ds_Minute
) * 60 +
161 be32_to_cpu(file_end
->created
.ds_Tick
) / 50 +
162 ((8 * 365 + 2) * 24 * 60 * 60)) +
163 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
;
169 inode
->i_fop
= &affs_file_operations_ofs
;
172 inode
->i_op
= &affs_file_inode_operations
;
173 inode
->i_fop
= &affs_file_operations
;
174 inode
->i_mapping
->a_ops
= &affs_aops
;
175 inode
->u
.affs_i
.mmu_private
= inode
->i_size
;
176 } else if (S_ISDIR(inode
->i_mode
)) {
177 /* Maybe it should be controlled by mount parameter? */
178 inode
->i_mode
|= S_ISVTX
;
179 inode
->i_op
= &affs_dir_inode_operations
;
180 inode
->i_fop
= &affs_dir_operations
;
182 else if (S_ISLNK(inode
->i_mode
)) {
183 inode
->i_op
= &affs_symlink_inode_operations
;
184 inode
->i_data
.a_ops
= &affs_symlink_aops
;
189 affs_write_inode(struct inode
*inode
)
191 struct buffer_head
*bh
;
192 struct file_end
*file_end
;
196 pr_debug("AFFS: write_inode(%lu)\n",inode
->i_ino
);
200 if (!(bh
= bread(inode
->i_dev
,inode
->i_ino
,AFFS_I2BSIZE(inode
)))) {
201 affs_error(inode
->i_sb
,"write_inode","Cannot read block %lu",inode
->i_ino
);
204 file_end
= GET_END_PTR(struct file_end
, bh
->b_data
,AFFS_I2BSIZE(inode
));
205 if (file_end
->secondary_type
== be32_to_cpu(ST_ROOT
)) {
206 secs_to_datestamp(inode
->i_mtime
,&ROOT_END(bh
->b_data
,inode
)->disk_altered
);
208 file_end
->protect
= cpu_to_be32(inode
->u
.affs_i
.i_protect
^ FIBF_OWNER
);
209 file_end
->byte_size
= cpu_to_be32(inode
->i_size
);
210 secs_to_datestamp(inode
->i_mtime
,&file_end
->created
);
211 if (!(inode
->i_ino
== inode
->i_sb
->u
.affs_sb
.s_root_block
)) {
214 if (inode
->i_sb
->u
.affs_sb
.s_flags
& SF_MUFS
) {
215 if (inode
->i_uid
== 0 || inode
->i_uid
== 0xFFFF)
216 uid
= inode
->i_uid
^ ~0;
217 if (inode
->i_gid
== 0 || inode
->i_gid
== 0xFFFF)
218 gid
= inode
->i_gid
^ ~0;
220 if (!(inode
->i_sb
->u
.affs_sb
.s_flags
& SF_SETUID
))
221 file_end
->owner_uid
= cpu_to_be16(uid
);
222 if (!(inode
->i_sb
->u
.affs_sb
.s_flags
& SF_SETGID
))
223 file_end
->owner_gid
= cpu_to_be16(gid
);
226 affs_fix_checksum(AFFS_I2BSIZE(inode
),bh
->b_data
,5);
227 mark_buffer_dirty(bh
,1);
232 affs_notify_change(struct dentry
*dentry
, struct iattr
*attr
)
234 struct inode
*inode
= dentry
->d_inode
;
237 pr_debug("AFFS: notify_change(%lu,0x%x)\n",inode
->i_ino
,attr
->ia_valid
);
239 error
= inode_change_ok(inode
,attr
);
243 if (((attr
->ia_valid
& ATTR_UID
) && (inode
->i_sb
->u
.affs_sb
.s_flags
& SF_SETUID
)) ||
244 ((attr
->ia_valid
& ATTR_GID
) && (inode
->i_sb
->u
.affs_sb
.s_flags
& SF_SETGID
)) ||
245 ((attr
->ia_valid
& ATTR_MODE
) &&
246 (inode
->i_sb
->u
.affs_sb
.s_flags
& (SF_SETMODE
| SF_IMMUTABLE
)))) {
247 if (!(inode
->i_sb
->u
.affs_sb
.s_flags
& SF_QUIET
))
252 if (attr
->ia_valid
& ATTR_MODE
)
253 inode
->u
.affs_i
.i_protect
= mode_to_prot(attr
->ia_mode
);
256 inode_setattr(inode
, attr
);
262 affs_put_inode(struct inode
*inode
)
264 pr_debug("AFFS: put_inode(ino=%lu, nlink=%u)\n",
265 inode
->i_ino
,inode
->i_nlink
);
267 affs_free_prealloc(inode
);
268 if (inode
->i_count
== 1) {
269 unsigned long cache_page
= (unsigned long) inode
->u
.affs_i
.i_ec
;
271 pr_debug("AFFS: freeing ext cache\n");
272 inode
->u
.affs_i
.i_ec
= NULL
;
273 free_page(cache_page
);
279 affs_delete_inode(struct inode
*inode
)
281 pr_debug("AFFS: delete_inode(ino=%lu, nlink=%u)\n",inode
->i_ino
,inode
->i_nlink
);
283 if (S_ISREG(inode
->i_mode
) && !inode
->u
.affs_i
.i_hlink
)
284 affs_truncate(inode
);
285 affs_free_block(inode
->i_sb
,inode
->i_ino
);
290 affs_new_inode(const struct inode
*dir
)
293 struct super_block
*sb
;
296 if (!dir
|| !(inode
= get_empty_inode()))
302 if (!(block
= affs_new_header((struct inode
*)dir
))) {
307 inode
->i_dev
= sb
->s_dev
;
308 inode
->i_uid
= current
->fsuid
;
309 inode
->i_gid
= current
->fsgid
;
310 inode
->i_ino
= block
;
311 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
313 inode
->u
.affs_i
.i_original
= 0;
314 inode
->u
.affs_i
.i_parent
= dir
->i_ino
;
315 inode
->u
.affs_i
.i_zone
= 0;
316 inode
->u
.affs_i
.i_hlink
= 0;
317 inode
->u
.affs_i
.i_pa_cnt
= 0;
318 inode
->u
.affs_i
.i_pa_next
= 0;
319 inode
->u
.affs_i
.i_pa_last
= 0;
320 inode
->u
.affs_i
.i_ec
= NULL
;
321 inode
->u
.affs_i
.i_lastblock
= -1;
323 insert_inode_hash(inode
);
324 mark_inode_dirty(inode
);
330 * Add an entry to a directory. Create the header block
331 * and insert it into the hash table.
335 affs_add_entry(struct inode
*dir
, struct inode
*link
, struct inode
*inode
,
336 struct dentry
*dentry
, int type
)
338 struct buffer_head
*dir_bh
;
339 struct buffer_head
*inode_bh
;
340 struct buffer_head
*link_bh
;
342 const unsigned char *name
= dentry
->d_name
.name
;
343 int len
= dentry
->d_name
.len
;
345 pr_debug("AFFS: add_entry(dir=%lu,inode=%lu,\"%*s\",type=%d)\n",dir
->i_ino
,inode
->i_ino
,
348 if ((retval
= affs_check_name(name
,len
)))
353 dir_bh
= affs_bread(dir
->i_dev
,dir
->i_ino
,AFFS_I2BSIZE(dir
));
354 inode_bh
= affs_bread(inode
->i_dev
,inode
->i_ino
,AFFS_I2BSIZE(inode
));
357 if (!dir_bh
|| !inode_bh
)
360 link_bh
= affs_bread(link
->i_dev
,link
->i_ino
,AFFS_I2BSIZE(link
));
364 ((struct dir_front
*)inode_bh
->b_data
)->primary_type
= cpu_to_be32(T_SHORT
);
365 ((struct dir_front
*)inode_bh
->b_data
)->own_key
= cpu_to_be32(inode
->i_ino
);
366 DIR_END(inode_bh
->b_data
,inode
)->dir_name
[0] = len
;
367 strncpy(DIR_END(inode_bh
->b_data
,inode
)->dir_name
+ 1,name
,len
);
368 DIR_END(inode_bh
->b_data
,inode
)->secondary_type
= cpu_to_be32(type
);
369 DIR_END(inode_bh
->b_data
,inode
)->parent
= cpu_to_be32(dir
->i_ino
);
371 lock_super(inode
->i_sb
);
372 retval
= affs_insert_hash(dir
->i_ino
,inode_bh
,dir
);
375 LINK_END(inode_bh
->b_data
,inode
)->original
= cpu_to_be32(link
->i_ino
);
376 LINK_END(inode_bh
->b_data
,inode
)->link_chain
=
377 FILE_END(link_bh
->b_data
,link
)->link_chain
;
378 FILE_END(link_bh
->b_data
,link
)->link_chain
= cpu_to_be32(inode
->i_ino
);
379 affs_fix_checksum(AFFS_I2BSIZE(link
),link_bh
->b_data
,5);
380 link
->i_version
= ++event
;
381 mark_inode_dirty(link
);
382 mark_buffer_dirty(link_bh
,1);
384 affs_fix_checksum(AFFS_I2BSIZE(inode
),inode_bh
->b_data
,5);
385 affs_fix_checksum(AFFS_I2BSIZE(dir
),dir_bh
->b_data
,5);
386 dir
->i_version
= ++event
;
387 dir
->i_mtime
= dir
->i_atime
= dir
->i_ctime
= CURRENT_TIME
;
388 unlock_super(inode
->i_sb
);
390 mark_inode_dirty(dir
);
391 mark_inode_dirty(inode
);
392 mark_buffer_dirty(dir_bh
,1);
393 mark_buffer_dirty(inode_bh
,1);
397 affs_brelse(inode_bh
);
398 affs_brelse(link_bh
);