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 <asm/div64.h>
15 #include <linux/errno.h>
17 #include <linux/malloc.h>
18 #include <linux/stat.h>
19 #include <linux/sched.h>
20 #include <linux/affs_fs.h>
21 #include <linux/kernel.h>
23 #include <linux/string.h>
24 #include <linux/locks.h>
25 #include <linux/genhd.h>
26 #include <linux/amigaffs.h>
27 #include <linux/major.h>
28 #include <linux/blkdev.h>
29 #include <linux/init.h>
30 #include <linux/smp_lock.h>
31 #include <asm/system.h>
32 #include <asm/uaccess.h>
34 extern int *blk_size
[];
35 extern struct timezone sys_tz
;
36 extern struct inode_operations affs_symlink_inode_operations
;
38 #define MIN(a,b) (((a)<(b))?(a):(b))
41 affs_parent_ino(struct inode
*dir
)
43 int root_ino
= (dir
->i_sb
->u
.affs_sb
.s_root_block
);
45 if (!S_ISDIR(dir
->i_mode
)) {
46 affs_error(dir
->i_sb
,"parent_ino","Trying to get parent of non-directory");
49 if (dir
->i_ino
== root_ino
)
51 return dir
->u
.affs_i
.i_parent
;
55 affs_read_inode(struct inode
*inode
)
57 struct buffer_head
*bh
;
58 struct file_front
*file_front
;
59 struct file_end
*file_end
;
66 pr_debug("AFFS: read_inode(%lu)\n",inode
->i_ino
);
69 if (!(bh
= affs_bread(inode
->i_dev
,block
,AFFS_I2BSIZE(inode
)))) {
70 affs_error(inode
->i_sb
,"read_inode","Cannot read block %d",block
);
73 if (affs_checksum_block(AFFS_I2BSIZE(inode
),bh
->b_data
,&ptype
,&stype
) || ptype
!= T_SHORT
) {
74 affs_error(inode
->i_sb
,"read_inode",
75 "Checksum or type (ptype=%d) error on inode %d",ptype
,block
);
80 file_front
= (struct file_front
*)bh
->b_data
;
81 file_end
= GET_END_PTR(struct file_end
, bh
->b_data
,AFFS_I2BSIZE(inode
));
82 prot
= (be32_to_cpu(file_end
->protect
) & ~0x10) ^ FIBF_OWNER
;
84 inode
->u
.affs_i
.i_protect
= prot
;
85 inode
->u
.affs_i
.i_parent
= be32_to_cpu(file_end
->parent
);
86 inode
->u
.affs_i
.i_original
= 0;
87 inode
->u
.affs_i
.i_zone
= 0;
88 inode
->u
.affs_i
.i_hlink
= 0;
89 inode
->u
.affs_i
.i_pa_cnt
= 0;
90 inode
->u
.affs_i
.i_pa_next
= 0;
91 inode
->u
.affs_i
.i_pa_last
= 0;
92 inode
->u
.affs_i
.i_ec
= NULL
;
93 inode
->u
.affs_i
.i_lastblock
= -1;
97 if (inode
->i_sb
->u
.affs_sb
.s_flags
& SF_SETMODE
)
98 inode
->i_mode
= inode
->i_sb
->u
.affs_sb
.s_mode
;
100 inode
->i_mode
= prot_to_mode(prot
);
102 if (inode
->i_sb
->u
.affs_sb
.s_flags
& SF_SETUID
)
103 inode
->i_uid
= inode
->i_sb
->u
.affs_sb
.s_uid
;
104 id
= be16_to_cpu(file_end
->owner_uid
);
105 if (id
== 0 || inode
->i_sb
->u
.affs_sb
.s_flags
& SF_SETUID
)
106 inode
->i_uid
= inode
->i_sb
->u
.affs_sb
.s_uid
;
107 else if (id
== 0xFFFF && inode
->i_sb
->u
.affs_sb
.s_flags
& SF_MUFS
)
112 id
= be16_to_cpu(file_end
->owner_gid
);
113 if (id
== 0 || inode
->i_sb
->u
.affs_sb
.s_flags
& SF_SETGID
)
114 inode
->i_gid
= inode
->i_sb
->u
.affs_sb
.s_gid
;
115 else if (id
== 0xFFFF && inode
->i_sb
->u
.affs_sb
.s_flags
& SF_MUFS
)
120 switch (be32_to_cpu(file_end
->secondary_type
)) {
122 inode
->i_uid
= inode
->i_sb
->u
.affs_sb
.s_uid
;
123 inode
->i_gid
= inode
->i_sb
->u
.affs_sb
.s_gid
;
125 if (be32_to_cpu(file_end
->secondary_type
) == ST_USERDIR
||
126 inode
->i_sb
->u
.affs_sb
.s_flags
& SF_SETMODE
) {
127 if (inode
->i_mode
& S_IRUSR
)
128 inode
->i_mode
|= S_IXUSR
;
129 if (inode
->i_mode
& S_IRGRP
)
130 inode
->i_mode
|= S_IXGRP
;
131 if (inode
->i_mode
& S_IROTH
)
132 inode
->i_mode
|= S_IXOTH
;
133 inode
->i_mode
|= S_IFDIR
;
135 inode
->i_mode
= S_IRUGO
| S_IXUGO
| S_IWUSR
| S_IFDIR
;
139 affs_error(inode
->i_sb
,"read_inode","inode is LINKDIR");
143 affs_error(inode
->i_sb
,"read_inode","inode is LINKFILE");
147 inode
->i_mode
|= S_IFREG
;
148 inode
->i_size
= be32_to_cpu(file_end
->byte_size
);
149 if (inode
->i_sb
->u
.affs_sb
.s_flags
& SF_OFS
)
150 block
= AFFS_I2BSIZE(inode
) - 24;
152 block
= AFFS_I2BSIZE(inode
);
153 tmp
= inode
->i_size
+ block
-1;
156 inode
->u
.affs_i
.i_lastblock
= tmp
;
159 inode
->i_mode
|= S_IFLNK
;
164 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
165 = (be32_to_cpu(file_end
->created
.ds_Days
) * (24 * 60 * 60) +
166 be32_to_cpu(file_end
->created
.ds_Minute
) * 60 +
167 be32_to_cpu(file_end
->created
.ds_Tick
) / 50 +
168 ((8 * 365 + 2) * 24 * 60 * 60)) +
169 sys_tz
.tz_minuteswest
* 60;
172 if (S_ISREG(inode
->i_mode
)) {
173 if (inode
->i_sb
->u
.affs_sb
.s_flags
& SF_OFS
) {
174 inode
->i_op
= &affs_file_inode_operations
;
175 inode
->i_fop
= &affs_file_operations_ofs
;
178 inode
->i_op
= &affs_file_inode_operations
;
179 inode
->i_fop
= &affs_file_operations
;
180 inode
->i_mapping
->a_ops
= &affs_aops
;
181 inode
->u
.affs_i
.mmu_private
= inode
->i_size
;
182 } else if (S_ISDIR(inode
->i_mode
)) {
183 /* Maybe it should be controlled by mount parameter? */
184 inode
->i_mode
|= S_ISVTX
;
185 inode
->i_op
= &affs_dir_inode_operations
;
186 inode
->i_fop
= &affs_dir_operations
;
188 else if (S_ISLNK(inode
->i_mode
)) {
189 inode
->i_op
= &affs_symlink_inode_operations
;
190 inode
->i_data
.a_ops
= &affs_symlink_aops
;
195 affs_write_inode(struct inode
*inode
, int unused
)
197 struct buffer_head
*bh
;
198 struct file_end
*file_end
;
202 pr_debug("AFFS: write_inode(%lu)\n",inode
->i_ino
);
207 if (!(bh
= bread(inode
->i_dev
,inode
->i_ino
,AFFS_I2BSIZE(inode
)))) {
208 affs_error(inode
->i_sb
,"write_inode","Cannot read block %lu",inode
->i_ino
);
212 file_end
= GET_END_PTR(struct file_end
, bh
->b_data
,AFFS_I2BSIZE(inode
));
213 if (file_end
->secondary_type
== be32_to_cpu(ST_ROOT
)) {
214 secs_to_datestamp(inode
->i_mtime
,&ROOT_END(bh
->b_data
,inode
)->disk_altered
);
216 file_end
->protect
= cpu_to_be32(inode
->u
.affs_i
.i_protect
^ FIBF_OWNER
);
217 file_end
->byte_size
= cpu_to_be32(inode
->i_size
);
218 secs_to_datestamp(inode
->i_mtime
,&file_end
->created
);
219 if (!(inode
->i_ino
== inode
->i_sb
->u
.affs_sb
.s_root_block
)) {
222 if (inode
->i_sb
->u
.affs_sb
.s_flags
& SF_MUFS
) {
223 if (inode
->i_uid
== 0 || inode
->i_uid
== 0xFFFF)
224 uid
= inode
->i_uid
^ ~0;
225 if (inode
->i_gid
== 0 || inode
->i_gid
== 0xFFFF)
226 gid
= inode
->i_gid
^ ~0;
228 if (!(inode
->i_sb
->u
.affs_sb
.s_flags
& SF_SETUID
))
229 file_end
->owner_uid
= cpu_to_be16(uid
);
230 if (!(inode
->i_sb
->u
.affs_sb
.s_flags
& SF_SETGID
))
231 file_end
->owner_gid
= cpu_to_be16(gid
);
234 affs_fix_checksum(AFFS_I2BSIZE(inode
),bh
->b_data
,5);
235 mark_buffer_dirty(bh
);
241 affs_notify_change(struct dentry
*dentry
, struct iattr
*attr
)
243 struct inode
*inode
= dentry
->d_inode
;
246 pr_debug("AFFS: notify_change(%lu,0x%x)\n",inode
->i_ino
,attr
->ia_valid
);
248 error
= inode_change_ok(inode
,attr
);
252 if (((attr
->ia_valid
& ATTR_UID
) && (inode
->i_sb
->u
.affs_sb
.s_flags
& SF_SETUID
)) ||
253 ((attr
->ia_valid
& ATTR_GID
) && (inode
->i_sb
->u
.affs_sb
.s_flags
& SF_SETGID
)) ||
254 ((attr
->ia_valid
& ATTR_MODE
) &&
255 (inode
->i_sb
->u
.affs_sb
.s_flags
& (SF_SETMODE
| SF_IMMUTABLE
)))) {
256 if (!(inode
->i_sb
->u
.affs_sb
.s_flags
& SF_QUIET
))
261 if (attr
->ia_valid
& ATTR_MODE
)
262 inode
->u
.affs_i
.i_protect
= mode_to_prot(attr
->ia_mode
);
265 inode_setattr(inode
, attr
);
271 affs_put_inode(struct inode
*inode
)
273 pr_debug("AFFS: put_inode(ino=%lu, nlink=%u)\n",
274 inode
->i_ino
,inode
->i_nlink
);
277 affs_free_prealloc(inode
);
278 if (atomic_read(&inode
->i_count
) == 1) {
279 unsigned long cache_page
= (unsigned long) inode
->u
.affs_i
.i_ec
;
281 pr_debug("AFFS: freeing ext cache\n");
282 inode
->u
.affs_i
.i_ec
= NULL
;
283 free_page(cache_page
);
290 affs_delete_inode(struct inode
*inode
)
292 pr_debug("AFFS: delete_inode(ino=%lu, nlink=%u)\n",inode
->i_ino
,inode
->i_nlink
);
295 if (S_ISREG(inode
->i_mode
) && !inode
->u
.affs_i
.i_hlink
)
296 affs_truncate(inode
);
297 affs_free_block(inode
->i_sb
,inode
->i_ino
);
303 affs_new_inode(const struct inode
*dir
)
306 struct super_block
*sb
;
313 inode
= new_inode(sb
);
317 if (!(block
= affs_new_header((struct inode
*)dir
))) {
322 inode
->i_uid
= current
->fsuid
;
323 inode
->i_gid
= current
->fsgid
;
324 inode
->i_ino
= block
;
325 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
327 inode
->u
.affs_i
.i_original
= 0;
328 inode
->u
.affs_i
.i_parent
= dir
->i_ino
;
329 inode
->u
.affs_i
.i_zone
= 0;
330 inode
->u
.affs_i
.i_hlink
= 0;
331 inode
->u
.affs_i
.i_pa_cnt
= 0;
332 inode
->u
.affs_i
.i_pa_next
= 0;
333 inode
->u
.affs_i
.i_pa_last
= 0;
334 inode
->u
.affs_i
.i_ec
= NULL
;
335 inode
->u
.affs_i
.i_lastblock
= -1;
337 insert_inode_hash(inode
);
338 mark_inode_dirty(inode
);
344 * Add an entry to a directory. Create the header block
345 * and insert it into the hash table.
349 affs_add_entry(struct inode
*dir
, struct inode
*link
, struct inode
*inode
,
350 struct dentry
*dentry
, int type
)
352 struct buffer_head
*dir_bh
;
353 struct buffer_head
*inode_bh
;
354 struct buffer_head
*link_bh
;
356 const unsigned char *name
= dentry
->d_name
.name
;
357 int len
= dentry
->d_name
.len
;
359 pr_debug("AFFS: add_entry(dir=%lu,inode=%lu,\"%*s\",type=%d)\n",dir
->i_ino
,inode
->i_ino
,
362 if ((retval
= affs_check_name(name
,len
)))
367 dir_bh
= affs_bread(dir
->i_dev
,dir
->i_ino
,AFFS_I2BSIZE(dir
));
368 inode_bh
= affs_bread(inode
->i_dev
,inode
->i_ino
,AFFS_I2BSIZE(inode
));
371 if (!dir_bh
|| !inode_bh
)
374 link_bh
= affs_bread(link
->i_dev
,link
->i_ino
,AFFS_I2BSIZE(link
));
378 ((struct dir_front
*)inode_bh
->b_data
)->primary_type
= cpu_to_be32(T_SHORT
);
379 ((struct dir_front
*)inode_bh
->b_data
)->own_key
= cpu_to_be32(inode
->i_ino
);
380 DIR_END(inode_bh
->b_data
,inode
)->dir_name
[0] = len
;
381 strncpy(DIR_END(inode_bh
->b_data
,inode
)->dir_name
+ 1,name
,len
);
382 DIR_END(inode_bh
->b_data
,inode
)->secondary_type
= cpu_to_be32(type
);
383 DIR_END(inode_bh
->b_data
,inode
)->parent
= cpu_to_be32(dir
->i_ino
);
385 lock_super(inode
->i_sb
);
386 retval
= affs_insert_hash(dir
->i_ino
,inode_bh
,dir
);
389 LINK_END(inode_bh
->b_data
,inode
)->original
= cpu_to_be32(link
->i_ino
);
390 LINK_END(inode_bh
->b_data
,inode
)->link_chain
=
391 FILE_END(link_bh
->b_data
,link
)->link_chain
;
392 FILE_END(link_bh
->b_data
,link
)->link_chain
= cpu_to_be32(inode
->i_ino
);
393 affs_fix_checksum(AFFS_I2BSIZE(link
),link_bh
->b_data
,5);
394 link
->i_version
= ++event
;
395 mark_inode_dirty(link
);
396 mark_buffer_dirty(link_bh
);
398 affs_fix_checksum(AFFS_I2BSIZE(inode
),inode_bh
->b_data
,5);
399 affs_fix_checksum(AFFS_I2BSIZE(dir
),dir_bh
->b_data
,5);
400 dir
->i_version
= ++event
;
401 dir
->i_mtime
= dir
->i_atime
= dir
->i_ctime
= CURRENT_TIME
;
402 unlock_super(inode
->i_sb
);
404 mark_inode_dirty(dir
);
405 mark_inode_dirty(inode
);
406 mark_buffer_dirty(dir_bh
);
407 mark_buffer_dirty(inode_bh
);
411 affs_brelse(inode_bh
);
412 affs_brelse(link_bh
);