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 static char ErrorBuffer[256];
16 * Functions for accessing Amiga-FFS structures.
20 /* Insert a header block bh into the directory dir
21 * caller must hold AFFS_DIR->i_hash_lock!
25 affs_insert_hash(struct inode *dir, struct buffer_head *bh)
27 struct super_block *sb = dir->i_sb;
28 struct buffer_head *dir_bh;
33 offset = affs_hash_name(sb, AFFS_TAIL(sb, bh)->name + 1, AFFS_TAIL(sb, bh)->name[0]);
35 pr_debug("%s(dir=%u, ino=%d)\n", __func__, (u32)dir->i_ino, ino);
37 dir_bh = affs_bread(sb, dir->i_ino);
41 hash_ino = be32_to_cpu(AFFS_HEAD(dir_bh)->table[offset]);
44 dir_bh = affs_bread(sb, hash_ino);
47 hash_ino = be32_to_cpu(AFFS_TAIL(sb, dir_bh)->hash_chain);
49 AFFS_TAIL(sb, bh)->parent = cpu_to_be32(dir->i_ino);
50 AFFS_TAIL(sb, bh)->hash_chain = 0;
51 affs_fix_checksum(sb, bh);
53 if (dir->i_ino == dir_bh->b_blocknr)
54 AFFS_HEAD(dir_bh)->table[offset] = cpu_to_be32(ino);
56 AFFS_TAIL(sb, dir_bh)->hash_chain = cpu_to_be32(ino);
58 affs_adjust_checksum(dir_bh, ino);
59 mark_buffer_dirty_inode(dir_bh, dir);
62 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
64 mark_inode_dirty(dir);
69 /* Remove a header block from its directory.
70 * caller must hold AFFS_DIR->i_hash_lock!
74 affs_remove_hash(struct inode *dir, struct buffer_head *rem_bh)
76 struct super_block *sb;
77 struct buffer_head *bh;
78 u32 rem_ino, hash_ino;
83 rem_ino = rem_bh->b_blocknr;
84 offset = affs_hash_name(sb, AFFS_TAIL(sb, rem_bh)->name+1, AFFS_TAIL(sb, rem_bh)->name[0]);
85 pr_debug("%s(dir=%d, ino=%d, hashval=%d)\n",
86 __func__, (u32)dir->i_ino, rem_ino, offset);
88 bh = affs_bread(sb, dir->i_ino);
93 hash_ino = be32_to_cpu(AFFS_HEAD(bh)->table[offset]);
95 if (hash_ino == rem_ino) {
96 ino = AFFS_TAIL(sb, rem_bh)->hash_chain;
97 if (dir->i_ino == bh->b_blocknr)
98 AFFS_HEAD(bh)->table[offset] = ino;
100 AFFS_TAIL(sb, bh)->hash_chain = ino;
101 affs_adjust_checksum(bh, be32_to_cpu(ino) - hash_ino);
102 mark_buffer_dirty_inode(bh, dir);
103 AFFS_TAIL(sb, rem_bh)->parent = 0;
108 bh = affs_bread(sb, hash_ino);
111 hash_ino = be32_to_cpu(AFFS_TAIL(sb, bh)->hash_chain);
116 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
118 mark_inode_dirty(dir);
124 affs_fix_dcache(struct inode *inode, u32 entry_ino)
126 struct dentry *dentry;
127 spin_lock(&inode->i_lock);
128 hlist_for_each_entry(dentry, &inode->i_dentry, d_alias) {
129 if (entry_ino == (u32)(long)dentry->d_fsdata) {
130 dentry->d_fsdata = (void *)inode->i_ino;
134 spin_unlock(&inode->i_lock);
138 /* Remove header from link chain */
141 affs_remove_link(struct dentry *dentry)
143 struct inode *dir, *inode = dentry->d_inode;
144 struct super_block *sb = inode->i_sb;
145 struct buffer_head *bh = NULL, *link_bh = NULL;
149 pr_debug("%s(key=%ld)\n", __func__, inode->i_ino);
151 bh = affs_bread(sb, inode->i_ino);
155 link_ino = (u32)(long)dentry->d_fsdata;
156 if (inode->i_ino == link_ino) {
157 /* we can't remove the head of the link, as its blocknr is still used as ino,
158 * so we remove the block of the first link instead.
160 link_ino = be32_to_cpu(AFFS_TAIL(sb, bh)->link_chain);
161 link_bh = affs_bread(sb, link_ino);
165 dir = affs_iget(sb, be32_to_cpu(AFFS_TAIL(sb, link_bh)->parent));
167 retval = PTR_ERR(dir);
173 * if there's a dentry for that block, make it
174 * refer to inode itself.
176 affs_fix_dcache(inode, link_ino);
177 retval = affs_remove_hash(dir, link_bh);
179 affs_unlock_dir(dir);
182 mark_buffer_dirty_inode(link_bh, inode);
184 memcpy(AFFS_TAIL(sb, bh)->name, AFFS_TAIL(sb, link_bh)->name, 32);
185 retval = affs_insert_hash(dir, bh);
187 affs_unlock_dir(dir);
190 mark_buffer_dirty_inode(bh, inode);
192 affs_unlock_dir(dir);
195 link_bh = affs_bread(sb, link_ino);
200 while ((ino = be32_to_cpu(AFFS_TAIL(sb, bh)->link_chain)) != 0) {
201 if (ino == link_ino) {
202 __be32 ino2 = AFFS_TAIL(sb, link_bh)->link_chain;
203 AFFS_TAIL(sb, bh)->link_chain = ino2;
204 affs_adjust_checksum(bh, be32_to_cpu(ino2) - link_ino);
205 mark_buffer_dirty_inode(bh, inode);
207 /* Fix the link count, if bh is a normal header block without links */
208 switch (be32_to_cpu(AFFS_TAIL(sb, bh)->stype)) {
213 if (!AFFS_TAIL(sb, bh)->link_chain)
216 affs_free_block(sb, link_ino);
220 bh = affs_bread(sb, ino);
226 affs_brelse(link_bh);
233 affs_empty_dir(struct inode *inode)
235 struct super_block *sb = inode->i_sb;
236 struct buffer_head *bh;
240 bh = affs_bread(sb, inode->i_ino);
245 for (size = AFFS_SB(sb)->s_hashsize - 1; size >= 0; size--)
246 if (AFFS_HEAD(bh)->table[size])
256 /* Remove a filesystem object. If the object to be removed has
257 * links to it, one of the links must be changed to inherit
258 * the file or directory. As above, any inode will do.
259 * The buffer will not be freed. If the header is a link, the
260 * block will be marked as free.
261 * This function returns a negative error number in case of
262 * an error, else 0 if the inode is to be deleted or 1 if not.
266 affs_remove_header(struct dentry *dentry)
268 struct super_block *sb;
269 struct inode *inode, *dir;
270 struct buffer_head *bh = NULL;
273 dir = dentry->d_parent->d_inode;
277 inode = dentry->d_inode;
281 pr_debug("%s(key=%ld)\n", __func__, inode->i_ino);
283 bh = affs_bread(sb, (u32)(long)dentry->d_fsdata);
287 affs_lock_link(inode);
289 switch (be32_to_cpu(AFFS_TAIL(sb, bh)->stype)) {
291 /* if we ever want to support links to dirs
292 * i_hash_lock of the inode must only be
293 * taken after some checks
295 affs_lock_dir(inode);
296 retval = affs_empty_dir(inode);
297 affs_unlock_dir(inode);
305 retval = affs_remove_hash(dir, bh);
308 mark_buffer_dirty_inode(bh, inode);
310 affs_unlock_dir(dir);
312 if (inode->i_nlink > 1)
313 retval = affs_remove_link(dentry);
316 affs_unlock_link(inode);
317 inode->i_ctime = CURRENT_TIME_SEC;
318 mark_inode_dirty(inode);
325 affs_unlock_dir(dir);
326 affs_unlock_link(inode);
330 /* Checksum a block, do various consistency checks and optionally return
331 the blocks type number. DATA points to the block. If their pointers
332 are non-null, *PTYPE and *STYPE are set to the primary and secondary
333 block types respectively, *HASHSIZE is set to the size of the hashtable
334 (which lets us calculate the block size).
335 Returns non-zero if the block is not consistent. */
338 affs_checksum_block(struct super_block *sb, struct buffer_head *bh)
340 __be32 *ptr = (__be32 *)bh->b_data;
345 for (bsize = sb->s_blocksize / sizeof(__be32); bsize > 0; bsize--)
346 sum += be32_to_cpu(*ptr++);
351 * Calculate the checksum of a disk block and store it
352 * at the indicated position.
356 affs_fix_checksum(struct super_block *sb, struct buffer_head *bh)
358 int cnt = sb->s_blocksize / sizeof(__be32);
359 __be32 *ptr = (__be32 *)bh->b_data;
363 checksumptr = ptr + 5;
365 for (checksum = 0; cnt > 0; ptr++, cnt--)
366 checksum += be32_to_cpu(*ptr);
367 *checksumptr = cpu_to_be32(-checksum);
371 secs_to_datestamp(time_t secs, struct affs_date *ds)
376 secs -= sys_tz.tz_minuteswest * 60 + ((8 * 365 + 2) * 24 * 60 * 60);
380 secs -= days * 86400;
384 ds->days = cpu_to_be32(days);
385 ds->mins = cpu_to_be32(minute);
386 ds->ticks = cpu_to_be32(secs * 50);
390 prot_to_mode(u32 prot)
394 if (!(prot & FIBF_NOWRITE))
396 if (!(prot & FIBF_NOREAD))
398 if (!(prot & FIBF_NOEXECUTE))
400 if (prot & FIBF_GRP_WRITE)
402 if (prot & FIBF_GRP_READ)
404 if (prot & FIBF_GRP_EXECUTE)
406 if (prot & FIBF_OTR_WRITE)
408 if (prot & FIBF_OTR_READ)
410 if (prot & FIBF_OTR_EXECUTE)
417 mode_to_prot(struct inode *inode)
419 u32 prot = AFFS_I(inode)->i_protect;
420 umode_t mode = inode->i_mode;
422 if (!(mode & S_IXUSR))
423 prot |= FIBF_NOEXECUTE;
424 if (!(mode & S_IRUSR))
426 if (!(mode & S_IWUSR))
427 prot |= FIBF_NOWRITE;
429 prot |= FIBF_GRP_EXECUTE;
431 prot |= FIBF_GRP_READ;
433 prot |= FIBF_GRP_WRITE;
435 prot |= FIBF_OTR_EXECUTE;
437 prot |= FIBF_OTR_READ;
439 prot |= FIBF_OTR_WRITE;
441 AFFS_I(inode)->i_protect = prot;
445 affs_error(struct super_block *sb, const char *function, const char *fmt, ...)
450 vsnprintf(ErrorBuffer,sizeof(ErrorBuffer),fmt,args);
453 pr_crit("error (device %s): %s(): %s\n", sb->s_id,
454 function,ErrorBuffer);
455 if (!(sb->s_flags & MS_RDONLY))
456 pr_warn("Remounting filesystem read-only\n");
457 sb->s_flags |= MS_RDONLY;
461 affs_warning(struct super_block *sb, const char *function, const char *fmt, ...)
466 vsnprintf(ErrorBuffer,sizeof(ErrorBuffer),fmt,args);
469 pr_warn("(device %s): %s(): %s\n", sb->s_id,
470 function,ErrorBuffer);
474 affs_nofilenametruncate(const struct dentry *dentry)
476 struct inode *inode = dentry->d_inode;
477 return AFFS_SB(inode->i_sb)->s_flags & SF_NO_TRUNCATE;
481 /* Check if the name is valid for a affs object. */
484 affs_check_name(const unsigned char *name, int len, bool notruncate)
490 return -ENAMETOOLONG;
494 for (i = 0; i < len; i++) {
495 if (name[i] < ' ' || name[i] == ':'
496 || (name[i] > 0x7e && name[i] < 0xa0))
503 /* This function copies name to bstr, with at most 30
504 * characters length. The bstr will be prepended by
506 * NOTE: The name will must be already checked by
511 affs_copy_name(unsigned char *bstr, struct dentry *dentry)
513 int len = min(dentry->d_name.len, 30u);
516 memcpy(bstr, dentry->d_name.name, len);