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
11 #include <linux/math64.h>
15 * Functions for accessing Amiga-FFS structures.
19 /* Insert a header block bh into the directory dir
20 * caller must hold AFFS_DIR->i_hash_lock!
24 affs_insert_hash(struct inode *dir, struct buffer_head *bh)
26 struct super_block *sb = dir->i_sb;
27 struct buffer_head *dir_bh;
32 offset = affs_hash_name(sb, AFFS_TAIL(sb, bh)->name + 1, AFFS_TAIL(sb, bh)->name[0]);
34 pr_debug("%s(dir=%lu, ino=%d)\n", __func__, dir->i_ino, ino);
36 dir_bh = affs_bread(sb, dir->i_ino);
40 hash_ino = be32_to_cpu(AFFS_HEAD(dir_bh)->table[offset]);
43 dir_bh = affs_bread(sb, hash_ino);
46 hash_ino = be32_to_cpu(AFFS_TAIL(sb, dir_bh)->hash_chain);
48 AFFS_TAIL(sb, bh)->parent = cpu_to_be32(dir->i_ino);
49 AFFS_TAIL(sb, bh)->hash_chain = 0;
50 affs_fix_checksum(sb, bh);
52 if (dir->i_ino == dir_bh->b_blocknr)
53 AFFS_HEAD(dir_bh)->table[offset] = cpu_to_be32(ino);
55 AFFS_TAIL(sb, dir_bh)->hash_chain = cpu_to_be32(ino);
57 affs_adjust_checksum(dir_bh, ino);
58 mark_buffer_dirty_inode(dir_bh, dir);
61 dir->i_mtime = dir->i_ctime = current_time(dir);
63 mark_inode_dirty(dir);
68 /* Remove a header block from its directory.
69 * caller must hold AFFS_DIR->i_hash_lock!
73 affs_remove_hash(struct inode *dir, struct buffer_head *rem_bh)
75 struct super_block *sb;
76 struct buffer_head *bh;
77 u32 rem_ino, hash_ino;
82 rem_ino = rem_bh->b_blocknr;
83 offset = affs_hash_name(sb, AFFS_TAIL(sb, rem_bh)->name+1, AFFS_TAIL(sb, rem_bh)->name[0]);
84 pr_debug("%s(dir=%lu, ino=%d, hashval=%d)\n", __func__, dir->i_ino,
87 bh = affs_bread(sb, dir->i_ino);
92 hash_ino = be32_to_cpu(AFFS_HEAD(bh)->table[offset]);
94 if (hash_ino == rem_ino) {
95 ino = AFFS_TAIL(sb, rem_bh)->hash_chain;
96 if (dir->i_ino == bh->b_blocknr)
97 AFFS_HEAD(bh)->table[offset] = ino;
99 AFFS_TAIL(sb, bh)->hash_chain = ino;
100 affs_adjust_checksum(bh, be32_to_cpu(ino) - hash_ino);
101 mark_buffer_dirty_inode(bh, dir);
102 AFFS_TAIL(sb, rem_bh)->parent = 0;
107 bh = affs_bread(sb, hash_ino);
110 hash_ino = be32_to_cpu(AFFS_TAIL(sb, bh)->hash_chain);
115 dir->i_mtime = dir->i_ctime = current_time(dir);
117 mark_inode_dirty(dir);
123 affs_fix_dcache(struct inode *inode, u32 entry_ino)
125 struct dentry *dentry;
126 spin_lock(&inode->i_lock);
127 hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
128 if (entry_ino == (u32)(long)dentry->d_fsdata) {
129 dentry->d_fsdata = (void *)inode->i_ino;
133 spin_unlock(&inode->i_lock);
137 /* Remove header from link chain */
140 affs_remove_link(struct dentry *dentry)
142 struct inode *dir, *inode = d_inode(dentry);
143 struct super_block *sb = inode->i_sb;
144 struct buffer_head *bh, *link_bh = NULL;
148 pr_debug("%s(key=%ld)\n", __func__, inode->i_ino);
150 bh = affs_bread(sb, inode->i_ino);
154 link_ino = (u32)(long)dentry->d_fsdata;
155 if (inode->i_ino == link_ino) {
156 /* we can't remove the head of the link, as its blocknr is still used as ino,
157 * so we remove the block of the first link instead.
159 link_ino = be32_to_cpu(AFFS_TAIL(sb, bh)->link_chain);
160 link_bh = affs_bread(sb, link_ino);
164 dir = affs_iget(sb, be32_to_cpu(AFFS_TAIL(sb, link_bh)->parent));
166 retval = PTR_ERR(dir);
172 * if there's a dentry for that block, make it
173 * refer to inode itself.
175 affs_fix_dcache(inode, link_ino);
176 retval = affs_remove_hash(dir, link_bh);
178 affs_unlock_dir(dir);
181 mark_buffer_dirty_inode(link_bh, inode);
183 memcpy(AFFS_TAIL(sb, bh)->name, AFFS_TAIL(sb, link_bh)->name, 32);
184 retval = affs_insert_hash(dir, bh);
186 affs_unlock_dir(dir);
189 mark_buffer_dirty_inode(bh, inode);
191 affs_unlock_dir(dir);
194 link_bh = affs_bread(sb, link_ino);
199 while ((ino = be32_to_cpu(AFFS_TAIL(sb, bh)->link_chain)) != 0) {
200 if (ino == link_ino) {
201 __be32 ino2 = AFFS_TAIL(sb, link_bh)->link_chain;
202 AFFS_TAIL(sb, bh)->link_chain = ino2;
203 affs_adjust_checksum(bh, be32_to_cpu(ino2) - link_ino);
204 mark_buffer_dirty_inode(bh, inode);
206 /* Fix the link count, if bh is a normal header block without links */
207 switch (be32_to_cpu(AFFS_TAIL(sb, bh)->stype)) {
212 if (!AFFS_TAIL(sb, bh)->link_chain)
215 affs_free_block(sb, link_ino);
219 bh = affs_bread(sb, ino);
225 affs_brelse(link_bh);
232 affs_empty_dir(struct inode *inode)
234 struct super_block *sb = inode->i_sb;
235 struct buffer_head *bh;
239 bh = affs_bread(sb, inode->i_ino);
244 for (size = AFFS_SB(sb)->s_hashsize - 1; size >= 0; size--)
245 if (AFFS_HEAD(bh)->table[size])
255 /* Remove a filesystem object. If the object to be removed has
256 * links to it, one of the links must be changed to inherit
257 * the file or directory. As above, any inode will do.
258 * The buffer will not be freed. If the header is a link, the
259 * block will be marked as free.
260 * This function returns a negative error number in case of
261 * an error, else 0 if the inode is to be deleted or 1 if not.
265 affs_remove_header(struct dentry *dentry)
267 struct super_block *sb;
268 struct inode *inode, *dir;
269 struct buffer_head *bh = NULL;
272 dir = d_inode(dentry->d_parent);
276 inode = d_inode(dentry);
280 pr_debug("%s(key=%ld)\n", __func__, inode->i_ino);
282 bh = affs_bread(sb, (u32)(long)dentry->d_fsdata);
286 affs_lock_link(inode);
288 switch (be32_to_cpu(AFFS_TAIL(sb, bh)->stype)) {
290 /* if we ever want to support links to dirs
291 * i_hash_lock of the inode must only be
292 * taken after some checks
294 affs_lock_dir(inode);
295 retval = affs_empty_dir(inode);
296 affs_unlock_dir(inode);
304 retval = affs_remove_hash(dir, bh);
307 mark_buffer_dirty_inode(bh, inode);
309 affs_unlock_dir(dir);
311 if (inode->i_nlink > 1)
312 retval = affs_remove_link(dentry);
315 affs_unlock_link(inode);
316 inode->i_ctime = current_time(inode);
317 mark_inode_dirty(inode);
324 affs_unlock_dir(dir);
325 affs_unlock_link(inode);
329 /* Checksum a block, do various consistency checks and optionally return
330 the blocks type number. DATA points to the block. If their pointers
331 are non-null, *PTYPE and *STYPE are set to the primary and secondary
332 block types respectively, *HASHSIZE is set to the size of the hashtable
333 (which lets us calculate the block size).
334 Returns non-zero if the block is not consistent. */
337 affs_checksum_block(struct super_block *sb, struct buffer_head *bh)
339 __be32 *ptr = (__be32 *)bh->b_data;
344 for (bsize = sb->s_blocksize / sizeof(__be32); bsize > 0; bsize--)
345 sum += be32_to_cpu(*ptr++);
350 * Calculate the checksum of a disk block and store it
351 * at the indicated position.
355 affs_fix_checksum(struct super_block *sb, struct buffer_head *bh)
357 int cnt = sb->s_blocksize / sizeof(__be32);
358 __be32 *ptr = (__be32 *)bh->b_data;
362 checksumptr = ptr + 5;
364 for (checksum = 0; cnt > 0; ptr++, cnt--)
365 checksum += be32_to_cpu(*ptr);
366 *checksumptr = cpu_to_be32(-checksum);
370 affs_secs_to_datestamp(time64_t secs, struct affs_date *ds)
376 secs -= sys_tz.tz_minuteswest * 60 + ((8 * 365 + 2) * 24 * 60 * 60);
379 days = div_s64_rem(secs, 86400, &rem);
383 ds->days = cpu_to_be32(days);
384 ds->mins = cpu_to_be32(minute);
385 ds->ticks = cpu_to_be32(rem * 50);
389 affs_prot_to_mode(u32 prot)
393 if (!(prot & FIBF_NOWRITE))
395 if (!(prot & FIBF_NOREAD))
397 if (!(prot & FIBF_NOEXECUTE))
399 if (prot & FIBF_GRP_WRITE)
401 if (prot & FIBF_GRP_READ)
403 if (prot & FIBF_GRP_EXECUTE)
405 if (prot & FIBF_OTR_WRITE)
407 if (prot & FIBF_OTR_READ)
409 if (prot & FIBF_OTR_EXECUTE)
416 affs_mode_to_prot(struct inode *inode)
418 u32 prot = AFFS_I(inode)->i_protect;
419 umode_t mode = inode->i_mode;
422 prot |= FIBF_NOEXECUTE;
426 prot |= FIBF_NOWRITE;
428 prot |= FIBF_GRP_EXECUTE;
430 prot |= FIBF_GRP_READ;
432 prot |= FIBF_GRP_WRITE;
434 prot |= FIBF_OTR_EXECUTE;
436 prot |= FIBF_OTR_READ;
438 prot |= FIBF_OTR_WRITE;
440 AFFS_I(inode)->i_protect = prot;
444 affs_error(struct super_block *sb, const char *function, const char *fmt, ...)
446 struct va_format vaf;
452 pr_crit("error (device %s): %s(): %pV\n", sb->s_id, function, &vaf);
453 if (!(sb->s_flags & MS_RDONLY))
454 pr_warn("Remounting filesystem read-only\n");
455 sb->s_flags |= MS_RDONLY;
460 affs_warning(struct super_block *sb, const char *function, const char *fmt, ...)
462 struct va_format vaf;
468 pr_warn("(device %s): %s(): %pV\n", sb->s_id, function, &vaf);
473 affs_nofilenametruncate(const struct dentry *dentry)
475 return affs_test_opt(AFFS_SB(dentry->d_sb)->s_flags, SF_NO_TRUNCATE);
478 /* Check if the name is valid for a affs object. */
481 affs_check_name(const unsigned char *name, int len, bool notruncate)
485 if (len > AFFSNAMEMAX) {
487 return -ENAMETOOLONG;
490 for (i = 0; i < len; i++) {
491 if (name[i] < ' ' || name[i] == ':'
492 || (name[i] > 0x7e && name[i] < 0xa0))
499 /* This function copies name to bstr, with at most 30
500 * characters length. The bstr will be prepended by
502 * NOTE: The name will must be already checked by
507 affs_copy_name(unsigned char *bstr, struct dentry *dentry)
509 u32 len = min(dentry->d_name.len, AFFSNAMEMAX);
512 memcpy(bstr, dentry->d_name.name, len);