2 * linux/fs/affs/amigaffs.c
4 * (c) 1996 Hans-Joachim Widmaier - Rewritten
6 * (C) 1993 Ray Burr - Amiga FFS filesystem.
8 * Please send bug reports to: hjw@zvw.de
13 extern struct timezone sys_tz
;
15 static char ErrorBuffer
[256];
18 * Functions for accessing Amiga-FFS structures.
22 /* Insert a header block bh into the directory dir
23 * caller must hold AFFS_DIR->i_hash_lock!
27 affs_insert_hash(struct inode
*dir
, struct buffer_head
*bh
)
29 struct super_block
*sb
= dir
->i_sb
;
30 struct buffer_head
*dir_bh
;
35 offset
= affs_hash_name(sb
, AFFS_TAIL(sb
, bh
)->name
+ 1, AFFS_TAIL(sb
, bh
)->name
[0]);
37 pr_debug("AFFS: insert_hash(dir=%u, ino=%d)\n", (u32
)dir
->i_ino
, ino
);
39 dir_bh
= affs_bread(sb
, dir
->i_ino
);
43 hash_ino
= be32_to_cpu(AFFS_HEAD(dir_bh
)->table
[offset
]);
46 dir_bh
= affs_bread(sb
, hash_ino
);
49 hash_ino
= be32_to_cpu(AFFS_TAIL(sb
, dir_bh
)->hash_chain
);
51 AFFS_TAIL(sb
, bh
)->parent
= cpu_to_be32(dir
->i_ino
);
52 AFFS_TAIL(sb
, bh
)->hash_chain
= 0;
53 affs_fix_checksum(sb
, bh
);
55 if (dir
->i_ino
== dir_bh
->b_blocknr
)
56 AFFS_HEAD(dir_bh
)->table
[offset
] = cpu_to_be32(ino
);
58 AFFS_TAIL(sb
, dir_bh
)->hash_chain
= cpu_to_be32(ino
);
60 affs_adjust_checksum(dir_bh
, ino
);
61 mark_buffer_dirty_inode(dir_bh
, dir
);
64 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME_SEC
;
66 mark_inode_dirty(dir
);
71 /* Remove a header block from its directory.
72 * caller must hold AFFS_DIR->i_hash_lock!
76 affs_remove_hash(struct inode
*dir
, struct buffer_head
*rem_bh
)
78 struct super_block
*sb
;
79 struct buffer_head
*bh
;
80 u32 rem_ino
, hash_ino
;
85 rem_ino
= rem_bh
->b_blocknr
;
86 offset
= affs_hash_name(sb
, AFFS_TAIL(sb
, rem_bh
)->name
+1, AFFS_TAIL(sb
, rem_bh
)->name
[0]);
87 pr_debug("AFFS: remove_hash(dir=%d, ino=%d, hashval=%d)\n", (u32
)dir
->i_ino
, rem_ino
, offset
);
89 bh
= affs_bread(sb
, dir
->i_ino
);
94 hash_ino
= be32_to_cpu(AFFS_HEAD(bh
)->table
[offset
]);
96 if (hash_ino
== rem_ino
) {
97 ino
= AFFS_TAIL(sb
, rem_bh
)->hash_chain
;
98 if (dir
->i_ino
== bh
->b_blocknr
)
99 AFFS_HEAD(bh
)->table
[offset
] = ino
;
101 AFFS_TAIL(sb
, bh
)->hash_chain
= ino
;
102 affs_adjust_checksum(bh
, be32_to_cpu(ino
) - hash_ino
);
103 mark_buffer_dirty_inode(bh
, dir
);
104 AFFS_TAIL(sb
, rem_bh
)->parent
= 0;
109 bh
= affs_bread(sb
, hash_ino
);
112 hash_ino
= be32_to_cpu(AFFS_TAIL(sb
, bh
)->hash_chain
);
117 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME_SEC
;
119 mark_inode_dirty(dir
);
125 affs_fix_dcache(struct dentry
*dentry
, u32 entry_ino
)
127 struct inode
*inode
= dentry
->d_inode
;
128 void *data
= dentry
->d_fsdata
;
129 struct list_head
*head
, *next
;
131 spin_lock(&inode
->i_lock
);
132 head
= &inode
->i_dentry
;
134 while (next
!= head
) {
135 dentry
= list_entry(next
, struct dentry
, d_alias
);
136 if (entry_ino
== (u32
)(long)dentry
->d_fsdata
) {
137 dentry
->d_fsdata
= data
;
142 spin_unlock(&inode
->i_lock
);
146 /* Remove header from link chain */
149 affs_remove_link(struct dentry
*dentry
)
151 struct inode
*dir
, *inode
= dentry
->d_inode
;
152 struct super_block
*sb
= inode
->i_sb
;
153 struct buffer_head
*bh
= NULL
, *link_bh
= NULL
;
157 pr_debug("AFFS: remove_link(key=%ld)\n", inode
->i_ino
);
159 bh
= affs_bread(sb
, inode
->i_ino
);
163 link_ino
= (u32
)(long)dentry
->d_fsdata
;
164 if (inode
->i_ino
== link_ino
) {
165 /* we can't remove the head of the link, as its blocknr is still used as ino,
166 * so we remove the block of the first link instead.
168 link_ino
= be32_to_cpu(AFFS_TAIL(sb
, bh
)->link_chain
);
169 link_bh
= affs_bread(sb
, link_ino
);
173 dir
= affs_iget(sb
, be32_to_cpu(AFFS_TAIL(sb
, link_bh
)->parent
));
175 retval
= PTR_ERR(dir
);
180 affs_fix_dcache(dentry
, link_ino
);
181 retval
= affs_remove_hash(dir
, link_bh
);
183 affs_unlock_dir(dir
);
186 mark_buffer_dirty_inode(link_bh
, inode
);
188 memcpy(AFFS_TAIL(sb
, bh
)->name
, AFFS_TAIL(sb
, link_bh
)->name
, 32);
189 retval
= affs_insert_hash(dir
, bh
);
191 affs_unlock_dir(dir
);
194 mark_buffer_dirty_inode(bh
, inode
);
196 affs_unlock_dir(dir
);
199 link_bh
= affs_bread(sb
, link_ino
);
204 while ((ino
= be32_to_cpu(AFFS_TAIL(sb
, bh
)->link_chain
)) != 0) {
205 if (ino
== link_ino
) {
206 __be32 ino2
= AFFS_TAIL(sb
, link_bh
)->link_chain
;
207 AFFS_TAIL(sb
, bh
)->link_chain
= ino2
;
208 affs_adjust_checksum(bh
, be32_to_cpu(ino2
) - link_ino
);
209 mark_buffer_dirty_inode(bh
, inode
);
211 /* Fix the link count, if bh is a normal header block without links */
212 switch (be32_to_cpu(AFFS_TAIL(sb
, bh
)->stype
)) {
217 if (!AFFS_TAIL(sb
, bh
)->link_chain
)
220 affs_free_block(sb
, link_ino
);
224 bh
= affs_bread(sb
, ino
);
230 affs_brelse(link_bh
);
237 affs_empty_dir(struct inode
*inode
)
239 struct super_block
*sb
= inode
->i_sb
;
240 struct buffer_head
*bh
;
244 bh
= affs_bread(sb
, inode
->i_ino
);
249 for (size
= AFFS_SB(sb
)->s_hashsize
- 1; size
>= 0; size
--)
250 if (AFFS_HEAD(bh
)->table
[size
])
260 /* Remove a filesystem object. If the object to be removed has
261 * links to it, one of the links must be changed to inherit
262 * the file or directory. As above, any inode will do.
263 * The buffer will not be freed. If the header is a link, the
264 * block will be marked as free.
265 * This function returns a negative error number in case of
266 * an error, else 0 if the inode is to be deleted or 1 if not.
270 affs_remove_header(struct dentry
*dentry
)
272 struct super_block
*sb
;
273 struct inode
*inode
, *dir
;
274 struct buffer_head
*bh
= NULL
;
277 dir
= dentry
->d_parent
->d_inode
;
281 inode
= dentry
->d_inode
;
285 pr_debug("AFFS: remove_header(key=%ld)\n", inode
->i_ino
);
287 bh
= affs_bread(sb
, (u32
)(long)dentry
->d_fsdata
);
291 affs_lock_link(inode
);
293 switch (be32_to_cpu(AFFS_TAIL(sb
, bh
)->stype
)) {
295 /* if we ever want to support links to dirs
296 * i_hash_lock of the inode must only be
297 * taken after some checks
299 affs_lock_dir(inode
);
300 retval
= affs_empty_dir(inode
);
301 affs_unlock_dir(inode
);
309 retval
= affs_remove_hash(dir
, bh
);
312 mark_buffer_dirty_inode(bh
, inode
);
314 affs_unlock_dir(dir
);
316 if (inode
->i_nlink
> 1)
317 retval
= affs_remove_link(dentry
);
320 affs_unlock_link(inode
);
321 inode
->i_ctime
= CURRENT_TIME_SEC
;
322 mark_inode_dirty(inode
);
329 affs_unlock_dir(dir
);
330 affs_unlock_link(inode
);
334 /* Checksum a block, do various consistency checks and optionally return
335 the blocks type number. DATA points to the block. If their pointers
336 are non-null, *PTYPE and *STYPE are set to the primary and secondary
337 block types respectively, *HASHSIZE is set to the size of the hashtable
338 (which lets us calculate the block size).
339 Returns non-zero if the block is not consistent. */
342 affs_checksum_block(struct super_block
*sb
, struct buffer_head
*bh
)
344 __be32
*ptr
= (__be32
*)bh
->b_data
;
349 for (bsize
= sb
->s_blocksize
/ sizeof(__be32
); bsize
> 0; bsize
--)
350 sum
+= be32_to_cpu(*ptr
++);
355 * Calculate the checksum of a disk block and store it
356 * at the indicated position.
360 affs_fix_checksum(struct super_block
*sb
, struct buffer_head
*bh
)
362 int cnt
= sb
->s_blocksize
/ sizeof(__be32
);
363 __be32
*ptr
= (__be32
*)bh
->b_data
;
367 checksumptr
= ptr
+ 5;
369 for (checksum
= 0; cnt
> 0; ptr
++, cnt
--)
370 checksum
+= be32_to_cpu(*ptr
);
371 *checksumptr
= cpu_to_be32(-checksum
);
375 secs_to_datestamp(time_t secs
, struct affs_date
*ds
)
380 secs
-= sys_tz
.tz_minuteswest
* 60 + ((8 * 365 + 2) * 24 * 60 * 60);
384 secs
-= days
* 86400;
388 ds
->days
= cpu_to_be32(days
);
389 ds
->mins
= cpu_to_be32(minute
);
390 ds
->ticks
= cpu_to_be32(secs
* 50);
394 prot_to_mode(u32 prot
)
398 if (!(prot
& FIBF_NOWRITE
))
400 if (!(prot
& FIBF_NOREAD
))
402 if (!(prot
& FIBF_NOEXECUTE
))
404 if (prot
& FIBF_GRP_WRITE
)
406 if (prot
& FIBF_GRP_READ
)
408 if (prot
& FIBF_GRP_EXECUTE
)
410 if (prot
& FIBF_OTR_WRITE
)
412 if (prot
& FIBF_OTR_READ
)
414 if (prot
& FIBF_OTR_EXECUTE
)
421 mode_to_prot(struct inode
*inode
)
423 u32 prot
= AFFS_I(inode
)->i_protect
;
424 mode_t mode
= inode
->i_mode
;
426 if (!(mode
& S_IXUSR
))
427 prot
|= FIBF_NOEXECUTE
;
428 if (!(mode
& S_IRUSR
))
430 if (!(mode
& S_IWUSR
))
431 prot
|= FIBF_NOWRITE
;
433 prot
|= FIBF_GRP_EXECUTE
;
435 prot
|= FIBF_GRP_READ
;
437 prot
|= FIBF_GRP_WRITE
;
439 prot
|= FIBF_OTR_EXECUTE
;
441 prot
|= FIBF_OTR_READ
;
443 prot
|= FIBF_OTR_WRITE
;
445 AFFS_I(inode
)->i_protect
= prot
;
449 affs_error(struct super_block
*sb
, const char *function
, const char *fmt
, ...)
454 vsnprintf(ErrorBuffer
,sizeof(ErrorBuffer
),fmt
,args
);
457 printk(KERN_CRIT
"AFFS error (device %s): %s(): %s\n", sb
->s_id
,
458 function
,ErrorBuffer
);
459 if (!(sb
->s_flags
& MS_RDONLY
))
460 printk(KERN_WARNING
"AFFS: Remounting filesystem read-only\n");
461 sb
->s_flags
|= MS_RDONLY
;
465 affs_warning(struct super_block
*sb
, const char *function
, const char *fmt
, ...)
470 vsnprintf(ErrorBuffer
,sizeof(ErrorBuffer
),fmt
,args
);
473 printk(KERN_WARNING
"AFFS warning (device %s): %s(): %s\n", sb
->s_id
,
474 function
,ErrorBuffer
);
477 /* Check if the name is valid for a affs object. */
480 affs_check_name(const unsigned char *name
, int len
)
485 #ifdef AFFS_NO_TRUNCATE
486 return -ENAMETOOLONG
;
491 for (i
= 0; i
< len
; i
++) {
492 if (name
[i
] < ' ' || name
[i
] == ':'
493 || (name
[i
] > 0x7e && name
[i
] < 0xa0))
500 /* This function copies name to bstr, with at most 30
501 * characters length. The bstr will be prepended by
503 * NOTE: The name will must be already checked by
508 affs_copy_name(unsigned char *bstr
, struct dentry
*dentry
)
510 int len
= min(dentry
->d_name
.len
, 30u);
513 memcpy(bstr
, dentry
->d_name
.name
, len
);