1 // SPDX-License-Identifier: GPL-2.0
3 * linux/fs/affs/amigaffs.c
5 * (c) 1996 Hans-Joachim Widmaier - Rewritten
7 * (C) 1993 Ray Burr - Amiga FFS filesystem.
9 * Please send bug reports to: hjw@zvw.de
12 #include <linux/math64.h>
13 #include <linux/iversion.h>
17 * Functions for accessing Amiga-FFS structures.
21 /* Insert a header block bh into the directory dir
22 * caller must hold AFFS_DIR->i_hash_lock!
26 affs_insert_hash(struct inode *dir, struct buffer_head *bh)
28 struct super_block *sb = dir->i_sb;
29 struct buffer_head *dir_bh;
34 offset = affs_hash_name(sb, AFFS_TAIL(sb, bh)->name + 1, AFFS_TAIL(sb, bh)->name[0]);
36 pr_debug("%s(dir=%lu, ino=%d)\n", __func__, dir->i_ino, ino);
38 dir_bh = affs_bread(sb, dir->i_ino);
42 hash_ino = be32_to_cpu(AFFS_HEAD(dir_bh)->table[offset]);
45 dir_bh = affs_bread(sb, hash_ino);
48 hash_ino = be32_to_cpu(AFFS_TAIL(sb, dir_bh)->hash_chain);
50 AFFS_TAIL(sb, bh)->parent = cpu_to_be32(dir->i_ino);
51 AFFS_TAIL(sb, bh)->hash_chain = 0;
52 affs_fix_checksum(sb, bh);
54 if (dir->i_ino == dir_bh->b_blocknr)
55 AFFS_HEAD(dir_bh)->table[offset] = cpu_to_be32(ino);
57 AFFS_TAIL(sb, dir_bh)->hash_chain = cpu_to_be32(ino);
59 affs_adjust_checksum(dir_bh, ino);
60 mark_buffer_dirty_inode(dir_bh, dir);
63 dir->i_mtime = inode_set_ctime_current(dir);
64 inode_inc_iversion(dir);
65 mark_inode_dirty(dir);
70 /* Remove a header block from its directory.
71 * caller must hold AFFS_DIR->i_hash_lock!
75 affs_remove_hash(struct inode *dir, struct buffer_head *rem_bh)
77 struct super_block *sb;
78 struct buffer_head *bh;
79 u32 rem_ino, hash_ino;
84 rem_ino = rem_bh->b_blocknr;
85 offset = affs_hash_name(sb, AFFS_TAIL(sb, rem_bh)->name+1, AFFS_TAIL(sb, rem_bh)->name[0]);
86 pr_debug("%s(dir=%lu, ino=%d, hashval=%d)\n", __func__, dir->i_ino,
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 = inode_set_ctime_current(dir);
118 inode_inc_iversion(dir);
119 mark_inode_dirty(dir);
125 affs_fix_dcache(struct inode *inode, u32 entry_ino)
127 struct dentry *dentry;
128 spin_lock(&inode->i_lock);
129 hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
130 if (entry_ino == (u32)(long)dentry->d_fsdata) {
131 dentry->d_fsdata = (void *)inode->i_ino;
135 spin_unlock(&inode->i_lock);
139 /* Remove header from link chain */
142 affs_remove_link(struct dentry *dentry)
144 struct inode *dir, *inode = d_inode(dentry);
145 struct super_block *sb = inode->i_sb;
146 struct buffer_head *bh, *link_bh = NULL;
150 pr_debug("%s(key=%ld)\n", __func__, inode->i_ino);
152 bh = affs_bread(sb, inode->i_ino);
156 link_ino = (u32)(long)dentry->d_fsdata;
157 if (inode->i_ino == link_ino) {
158 /* we can't remove the head of the link, as its blocknr is still used as ino,
159 * so we remove the block of the first link instead.
161 link_ino = be32_to_cpu(AFFS_TAIL(sb, bh)->link_chain);
162 link_bh = affs_bread(sb, link_ino);
166 dir = affs_iget(sb, be32_to_cpu(AFFS_TAIL(sb, link_bh)->parent));
168 retval = PTR_ERR(dir);
174 * if there's a dentry for that block, make it
175 * refer to inode itself.
177 affs_fix_dcache(inode, link_ino);
178 retval = affs_remove_hash(dir, link_bh);
180 affs_unlock_dir(dir);
183 mark_buffer_dirty_inode(link_bh, inode);
185 memcpy(AFFS_TAIL(sb, bh)->name, AFFS_TAIL(sb, link_bh)->name, 32);
186 retval = affs_insert_hash(dir, bh);
188 affs_unlock_dir(dir);
191 mark_buffer_dirty_inode(bh, inode);
193 affs_unlock_dir(dir);
196 link_bh = affs_bread(sb, link_ino);
201 while ((ino = be32_to_cpu(AFFS_TAIL(sb, bh)->link_chain)) != 0) {
202 if (ino == link_ino) {
203 __be32 ino2 = AFFS_TAIL(sb, link_bh)->link_chain;
204 AFFS_TAIL(sb, bh)->link_chain = ino2;
205 affs_adjust_checksum(bh, be32_to_cpu(ino2) - link_ino);
206 mark_buffer_dirty_inode(bh, inode);
208 /* Fix the link count, if bh is a normal header block without links */
209 switch (be32_to_cpu(AFFS_TAIL(sb, bh)->stype)) {
214 if (!AFFS_TAIL(sb, bh)->link_chain)
217 affs_free_block(sb, link_ino);
221 bh = affs_bread(sb, ino);
227 affs_brelse(link_bh);
234 affs_empty_dir(struct inode *inode)
236 struct super_block *sb = inode->i_sb;
237 struct buffer_head *bh;
241 bh = affs_bread(sb, inode->i_ino);
246 for (size = AFFS_SB(sb)->s_hashsize - 1; size >= 0; size--)
247 if (AFFS_HEAD(bh)->table[size])
257 /* Remove a filesystem object. If the object to be removed has
258 * links to it, one of the links must be changed to inherit
259 * the file or directory. As above, any inode will do.
260 * The buffer will not be freed. If the header is a link, the
261 * block will be marked as free.
262 * This function returns a negative error number in case of
263 * an error, else 0 if the inode is to be deleted or 1 if not.
267 affs_remove_header(struct dentry *dentry)
269 struct super_block *sb;
270 struct inode *inode, *dir;
271 struct buffer_head *bh = NULL;
274 dir = d_inode(dentry->d_parent);
278 inode = d_inode(dentry);
282 pr_debug("%s(key=%ld)\n", __func__, inode->i_ino);
284 bh = affs_bread(sb, (u32)(long)dentry->d_fsdata);
288 affs_lock_link(inode);
290 switch (be32_to_cpu(AFFS_TAIL(sb, bh)->stype)) {
292 /* if we ever want to support links to dirs
293 * i_hash_lock of the inode must only be
294 * taken after some checks
296 affs_lock_dir(inode);
297 retval = affs_empty_dir(inode);
298 affs_unlock_dir(inode);
306 retval = affs_remove_hash(dir, bh);
309 mark_buffer_dirty_inode(bh, inode);
311 affs_unlock_dir(dir);
313 if (inode->i_nlink > 1)
314 retval = affs_remove_link(dentry);
317 affs_unlock_link(inode);
318 inode_set_ctime_current(inode);
319 mark_inode_dirty(inode);
326 affs_unlock_dir(dir);
327 affs_unlock_link(inode);
331 /* Checksum a block, do various consistency checks and optionally return
332 the blocks type number. DATA points to the block. If their pointers
333 are non-null, *PTYPE and *STYPE are set to the primary and secondary
334 block types respectively, *HASHSIZE is set to the size of the hashtable
335 (which lets us calculate the block size).
336 Returns non-zero if the block is not consistent. */
339 affs_checksum_block(struct super_block *sb, struct buffer_head *bh)
341 __be32 *ptr = (__be32 *)bh->b_data;
346 for (bsize = sb->s_blocksize / sizeof(__be32); bsize > 0; bsize--)
347 sum += be32_to_cpu(*ptr++);
352 * Calculate the checksum of a disk block and store it
353 * at the indicated position.
357 affs_fix_checksum(struct super_block *sb, struct buffer_head *bh)
359 int cnt = sb->s_blocksize / sizeof(__be32);
360 __be32 *ptr = (__be32 *)bh->b_data;
364 checksumptr = ptr + 5;
366 for (checksum = 0; cnt > 0; ptr++, cnt--)
367 checksum += be32_to_cpu(*ptr);
368 *checksumptr = cpu_to_be32(-checksum);
372 affs_secs_to_datestamp(time64_t secs, struct affs_date *ds)
378 secs -= sys_tz.tz_minuteswest * 60 + AFFS_EPOCH_DELTA;
381 days = div_s64_rem(secs, 86400, &rem);
385 ds->days = cpu_to_be32(days);
386 ds->mins = cpu_to_be32(minute);
387 ds->ticks = cpu_to_be32(rem * 50);
391 affs_prot_to_mode(u32 prot)
395 if (!(prot & FIBF_NOWRITE))
397 if (!(prot & FIBF_NOREAD))
399 if (!(prot & FIBF_NOEXECUTE))
401 if (prot & FIBF_GRP_WRITE)
403 if (prot & FIBF_GRP_READ)
405 if (prot & FIBF_GRP_EXECUTE)
407 if (prot & FIBF_OTR_WRITE)
409 if (prot & FIBF_OTR_READ)
411 if (prot & FIBF_OTR_EXECUTE)
418 affs_mode_to_prot(struct inode *inode)
420 u32 prot = AFFS_I(inode)->i_protect;
421 umode_t mode = inode->i_mode;
424 * First, clear all RWED bits for owner, group, other.
425 * Then, recalculate them afresh.
427 * We'll always clear the delete-inhibit bit for the owner, as that is
428 * the classic single-user mode AmigaOS protection bit and we need to
429 * stay compatible with all scenarios.
431 * Since multi-user AmigaOS is an extension, we'll only set the
432 * delete-allow bit if any of the other bits in the same user class
433 * (group/other) are used.
435 prot &= ~(FIBF_NOEXECUTE | FIBF_NOREAD
436 | FIBF_NOWRITE | FIBF_NODELETE
437 | FIBF_GRP_EXECUTE | FIBF_GRP_READ
438 | FIBF_GRP_WRITE | FIBF_GRP_DELETE
439 | FIBF_OTR_EXECUTE | FIBF_OTR_READ
440 | FIBF_OTR_WRITE | FIBF_OTR_DELETE);
442 /* Classic single-user AmigaOS flags. These are inverted. */
444 prot |= FIBF_NOEXECUTE;
448 prot |= FIBF_NOWRITE;
450 /* Multi-user extended flags. Not inverted. */
452 prot |= FIBF_GRP_EXECUTE;
454 prot |= FIBF_GRP_READ;
456 prot |= FIBF_GRP_WRITE;
458 prot |= FIBF_GRP_DELETE;
461 prot |= FIBF_OTR_EXECUTE;
463 prot |= FIBF_OTR_READ;
465 prot |= FIBF_OTR_WRITE;
467 prot |= FIBF_OTR_DELETE;
469 AFFS_I(inode)->i_protect = prot;
473 affs_error(struct super_block *sb, const char *function, const char *fmt, ...)
475 struct va_format vaf;
481 pr_crit("error (device %s): %s(): %pV\n", sb->s_id, function, &vaf);
483 pr_warn("Remounting filesystem read-only\n");
484 sb->s_flags |= SB_RDONLY;
489 affs_warning(struct super_block *sb, const char *function, const char *fmt, ...)
491 struct va_format vaf;
497 pr_warn("(device %s): %s(): %pV\n", sb->s_id, function, &vaf);
502 affs_nofilenametruncate(const struct dentry *dentry)
504 return affs_test_opt(AFFS_SB(dentry->d_sb)->s_flags, SF_NO_TRUNCATE);
507 /* Check if the name is valid for a affs object. */
510 affs_check_name(const unsigned char *name, int len, bool notruncate)
514 if (len > AFFSNAMEMAX) {
516 return -ENAMETOOLONG;
519 for (i = 0; i < len; i++) {
520 if (name[i] < ' ' || name[i] == ':'
521 || (name[i] > 0x7e && name[i] < 0xa0))
528 /* This function copies name to bstr, with at most 30
529 * characters length. The bstr will be prepended by
531 * NOTE: The name will must be already checked by
536 affs_copy_name(unsigned char *bstr, struct dentry *dentry)
538 u32 len = min(dentry->d_name.len, AFFSNAMEMAX);
541 memcpy(bstr, dentry->d_name.name, len);