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(&dcache_lock);
132 spin_lock(&dcache_inode_lock);
133 head = &inode->i_dentry;
135 while (next != head) {
136 dentry = list_entry(next, struct dentry, d_alias);
137 if (entry_ino == (u32)(long)dentry->d_fsdata) {
138 dentry->d_fsdata = data;
143 spin_unlock(&dcache_inode_lock);
144 spin_unlock(&dcache_lock);
148 /* Remove header from link chain */
151 affs_remove_link(struct dentry *dentry)
153 struct inode *dir, *inode = dentry->d_inode;
154 struct super_block *sb = inode->i_sb;
155 struct buffer_head *bh = NULL, *link_bh = NULL;
159 pr_debug("AFFS: remove_link(key=%ld)\n", inode->i_ino);
161 bh = affs_bread(sb, inode->i_ino);
165 link_ino = (u32)(long)dentry->d_fsdata;
166 if (inode->i_ino == link_ino) {
167 /* we can't remove the head of the link, as its blocknr is still used as ino,
168 * so we remove the block of the first link instead.
170 link_ino = be32_to_cpu(AFFS_TAIL(sb, bh)->link_chain);
171 link_bh = affs_bread(sb, link_ino);
175 dir = affs_iget(sb, be32_to_cpu(AFFS_TAIL(sb, link_bh)->parent));
177 retval = PTR_ERR(dir);
182 affs_fix_dcache(dentry, link_ino);
183 retval = affs_remove_hash(dir, link_bh);
185 affs_unlock_dir(dir);
188 mark_buffer_dirty_inode(link_bh, inode);
190 memcpy(AFFS_TAIL(sb, bh)->name, AFFS_TAIL(sb, link_bh)->name, 32);
191 retval = affs_insert_hash(dir, bh);
193 affs_unlock_dir(dir);
196 mark_buffer_dirty_inode(bh, inode);
198 affs_unlock_dir(dir);
201 link_bh = affs_bread(sb, link_ino);
206 while ((ino = be32_to_cpu(AFFS_TAIL(sb, bh)->link_chain)) != 0) {
207 if (ino == link_ino) {
208 __be32 ino2 = AFFS_TAIL(sb, link_bh)->link_chain;
209 AFFS_TAIL(sb, bh)->link_chain = ino2;
210 affs_adjust_checksum(bh, be32_to_cpu(ino2) - link_ino);
211 mark_buffer_dirty_inode(bh, inode);
213 /* Fix the link count, if bh is a normal header block without links */
214 switch (be32_to_cpu(AFFS_TAIL(sb, bh)->stype)) {
219 if (!AFFS_TAIL(sb, bh)->link_chain)
222 affs_free_block(sb, link_ino);
226 bh = affs_bread(sb, ino);
232 affs_brelse(link_bh);
239 affs_empty_dir(struct inode *inode)
241 struct super_block *sb = inode->i_sb;
242 struct buffer_head *bh;
246 bh = affs_bread(sb, inode->i_ino);
251 for (size = AFFS_SB(sb)->s_hashsize - 1; size >= 0; size--)
252 if (AFFS_HEAD(bh)->table[size])
262 /* Remove a filesystem object. If the object to be removed has
263 * links to it, one of the links must be changed to inherit
264 * the file or directory. As above, any inode will do.
265 * The buffer will not be freed. If the header is a link, the
266 * block will be marked as free.
267 * This function returns a negative error number in case of
268 * an error, else 0 if the inode is to be deleted or 1 if not.
272 affs_remove_header(struct dentry *dentry)
274 struct super_block *sb;
275 struct inode *inode, *dir;
276 struct buffer_head *bh = NULL;
279 dir = dentry->d_parent->d_inode;
283 inode = dentry->d_inode;
287 pr_debug("AFFS: remove_header(key=%ld)\n", inode->i_ino);
289 bh = affs_bread(sb, (u32)(long)dentry->d_fsdata);
293 affs_lock_link(inode);
295 switch (be32_to_cpu(AFFS_TAIL(sb, bh)->stype)) {
297 /* if we ever want to support links to dirs
298 * i_hash_lock of the inode must only be
299 * taken after some checks
301 affs_lock_dir(inode);
302 retval = affs_empty_dir(inode);
303 affs_unlock_dir(inode);
311 retval = affs_remove_hash(dir, bh);
314 mark_buffer_dirty_inode(bh, inode);
316 affs_unlock_dir(dir);
318 if (inode->i_nlink > 1)
319 retval = affs_remove_link(dentry);
322 affs_unlock_link(inode);
323 inode->i_ctime = CURRENT_TIME_SEC;
324 mark_inode_dirty(inode);
331 affs_unlock_dir(dir);
332 affs_unlock_link(inode);
336 /* Checksum a block, do various consistency checks and optionally return
337 the blocks type number. DATA points to the block. If their pointers
338 are non-null, *PTYPE and *STYPE are set to the primary and secondary
339 block types respectively, *HASHSIZE is set to the size of the hashtable
340 (which lets us calculate the block size).
341 Returns non-zero if the block is not consistent. */
344 affs_checksum_block(struct super_block *sb, struct buffer_head *bh)
346 __be32 *ptr = (__be32 *)bh->b_data;
351 for (bsize = sb->s_blocksize / sizeof(__be32); bsize > 0; bsize--)
352 sum += be32_to_cpu(*ptr++);
357 * Calculate the checksum of a disk block and store it
358 * at the indicated position.
362 affs_fix_checksum(struct super_block *sb, struct buffer_head *bh)
364 int cnt = sb->s_blocksize / sizeof(__be32);
365 __be32 *ptr = (__be32 *)bh->b_data;
369 checksumptr = ptr + 5;
371 for (checksum = 0; cnt > 0; ptr++, cnt--)
372 checksum += be32_to_cpu(*ptr);
373 *checksumptr = cpu_to_be32(-checksum);
377 secs_to_datestamp(time_t secs, struct affs_date *ds)
382 secs -= sys_tz.tz_minuteswest * 60 + ((8 * 365 + 2) * 24 * 60 * 60);
386 secs -= days * 86400;
390 ds->days = cpu_to_be32(days);
391 ds->mins = cpu_to_be32(minute);
392 ds->ticks = cpu_to_be32(secs * 50);
396 prot_to_mode(u32 prot)
400 if (!(prot & FIBF_NOWRITE))
402 if (!(prot & FIBF_NOREAD))
404 if (!(prot & FIBF_NOEXECUTE))
406 if (prot & FIBF_GRP_WRITE)
408 if (prot & FIBF_GRP_READ)
410 if (prot & FIBF_GRP_EXECUTE)
412 if (prot & FIBF_OTR_WRITE)
414 if (prot & FIBF_OTR_READ)
416 if (prot & FIBF_OTR_EXECUTE)
423 mode_to_prot(struct inode *inode)
425 u32 prot = AFFS_I(inode)->i_protect;
426 mode_t mode = inode->i_mode;
428 if (!(mode & S_IXUSR))
429 prot |= FIBF_NOEXECUTE;
430 if (!(mode & S_IRUSR))
432 if (!(mode & S_IWUSR))
433 prot |= FIBF_NOWRITE;
435 prot |= FIBF_GRP_EXECUTE;
437 prot |= FIBF_GRP_READ;
439 prot |= FIBF_GRP_WRITE;
441 prot |= FIBF_OTR_EXECUTE;
443 prot |= FIBF_OTR_READ;
445 prot |= FIBF_OTR_WRITE;
447 AFFS_I(inode)->i_protect = prot;
451 affs_error(struct super_block *sb, const char *function, const char *fmt, ...)
456 vsnprintf(ErrorBuffer,sizeof(ErrorBuffer),fmt,args);
459 printk(KERN_CRIT "AFFS error (device %s): %s(): %s\n", sb->s_id,
460 function,ErrorBuffer);
461 if (!(sb->s_flags & MS_RDONLY))
462 printk(KERN_WARNING "AFFS: Remounting filesystem read-only\n");
463 sb->s_flags |= MS_RDONLY;
467 affs_warning(struct super_block *sb, const char *function, const char *fmt, ...)
472 vsnprintf(ErrorBuffer,sizeof(ErrorBuffer),fmt,args);
475 printk(KERN_WARNING "AFFS warning (device %s): %s(): %s\n", sb->s_id,
476 function,ErrorBuffer);
479 /* Check if the name is valid for a affs object. */
482 affs_check_name(const unsigned char *name, int len)
487 #ifdef AFFS_NO_TRUNCATE
488 return -ENAMETOOLONG;
493 for (i = 0; i < len; i++) {
494 if (name[i] < ' ' || name[i] == ':'
495 || (name[i] > 0x7e && name[i] < 0xa0))
502 /* This function copies name to bstr, with at most 30
503 * characters length. The bstr will be prepended by
505 * NOTE: The name will must be already checked by
510 affs_copy_name(unsigned char *bstr, struct dentry *dentry)
512 int len = min(dentry->d_name.len, 30u);
515 memcpy(bstr, dentry->d_name.name, len);