2 * linux/fs/affs/amigaffs.c
4 * (c) 1996 Hans-Joachim Widmaier - Rewritten
6 * (C) 1993 Ray Burr - Amiga FFS filesystem.
14 * Functions for accessing Amiga-FFS structures.
18 /* Insert a header block bh into the directory dir
19 * caller must hold AFFS_DIR->i_hash_lock!
23 affs_insert_hash(struct inode *dir, struct buffer_head *bh)
25 struct super_block *sb = dir->i_sb;
26 struct buffer_head *dir_bh;
31 offset = affs_hash_name(sb, AFFS_TAIL(sb, bh)->name + 1, AFFS_TAIL(sb, bh)->name[0]);
33 pr_debug("%s(dir=%u, ino=%d)\n", __func__, (u32)dir->i_ino, ino);
35 dir_bh = affs_bread(sb, dir->i_ino);
39 hash_ino = be32_to_cpu(AFFS_HEAD(dir_bh)->table[offset]);
42 dir_bh = affs_bread(sb, hash_ino);
45 hash_ino = be32_to_cpu(AFFS_TAIL(sb, dir_bh)->hash_chain);
47 AFFS_TAIL(sb, bh)->parent = cpu_to_be32(dir->i_ino);
48 AFFS_TAIL(sb, bh)->hash_chain = 0;
49 affs_fix_checksum(sb, bh);
51 if (dir->i_ino == dir_bh->b_blocknr)
52 AFFS_HEAD(dir_bh)->table[offset] = cpu_to_be32(ino);
54 AFFS_TAIL(sb, dir_bh)->hash_chain = cpu_to_be32(ino);
56 affs_adjust_checksum(dir_bh, ino);
57 mark_buffer_dirty_inode(dir_bh, dir);
60 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
62 mark_inode_dirty(dir);
67 /* Remove a header block from its directory.
68 * caller must hold AFFS_DIR->i_hash_lock!
72 affs_remove_hash(struct inode *dir, struct buffer_head *rem_bh)
74 struct super_block *sb;
75 struct buffer_head *bh;
76 u32 rem_ino, hash_ino;
81 rem_ino = rem_bh->b_blocknr;
82 offset = affs_hash_name(sb, AFFS_TAIL(sb, rem_bh)->name+1, AFFS_TAIL(sb, rem_bh)->name[0]);
83 pr_debug("%s(dir=%d, ino=%d, hashval=%d)\n",
84 __func__, (u32)dir->i_ino, rem_ino, offset);
86 bh = affs_bread(sb, dir->i_ino);
91 hash_ino = be32_to_cpu(AFFS_HEAD(bh)->table[offset]);
93 if (hash_ino == rem_ino) {
94 ino = AFFS_TAIL(sb, rem_bh)->hash_chain;
95 if (dir->i_ino == bh->b_blocknr)
96 AFFS_HEAD(bh)->table[offset] = ino;
98 AFFS_TAIL(sb, bh)->hash_chain = ino;
99 affs_adjust_checksum(bh, be32_to_cpu(ino) - hash_ino);
100 mark_buffer_dirty_inode(bh, dir);
101 AFFS_TAIL(sb, rem_bh)->parent = 0;
106 bh = affs_bread(sb, hash_ino);
109 hash_ino = be32_to_cpu(AFFS_TAIL(sb, bh)->hash_chain);
114 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
116 mark_inode_dirty(dir);
122 affs_fix_dcache(struct inode *inode, u32 entry_ino)
124 struct dentry *dentry;
125 spin_lock(&inode->i_lock);
126 hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
127 if (entry_ino == (u32)(long)dentry->d_fsdata) {
128 dentry->d_fsdata = (void *)inode->i_ino;
132 spin_unlock(&inode->i_lock);
136 /* Remove header from link chain */
139 affs_remove_link(struct dentry *dentry)
141 struct inode *dir, *inode = dentry->d_inode;
142 struct super_block *sb = inode->i_sb;
143 struct buffer_head *bh = NULL, *link_bh = NULL;
147 pr_debug("%s(key=%ld)\n", __func__, inode->i_ino);
149 bh = affs_bread(sb, inode->i_ino);
153 link_ino = (u32)(long)dentry->d_fsdata;
154 if (inode->i_ino == link_ino) {
155 /* we can't remove the head of the link, as its blocknr is still used as ino,
156 * so we remove the block of the first link instead.
158 link_ino = be32_to_cpu(AFFS_TAIL(sb, bh)->link_chain);
159 link_bh = affs_bread(sb, link_ino);
163 dir = affs_iget(sb, be32_to_cpu(AFFS_TAIL(sb, link_bh)->parent));
165 retval = PTR_ERR(dir);
171 * if there's a dentry for that block, make it
172 * refer to inode itself.
174 affs_fix_dcache(inode, link_ino);
175 retval = affs_remove_hash(dir, link_bh);
177 affs_unlock_dir(dir);
180 mark_buffer_dirty_inode(link_bh, inode);
182 memcpy(AFFS_TAIL(sb, bh)->name, AFFS_TAIL(sb, link_bh)->name, 32);
183 retval = affs_insert_hash(dir, bh);
185 affs_unlock_dir(dir);
188 mark_buffer_dirty_inode(bh, inode);
190 affs_unlock_dir(dir);
193 link_bh = affs_bread(sb, link_ino);
198 while ((ino = be32_to_cpu(AFFS_TAIL(sb, bh)->link_chain)) != 0) {
199 if (ino == link_ino) {
200 __be32 ino2 = AFFS_TAIL(sb, link_bh)->link_chain;
201 AFFS_TAIL(sb, bh)->link_chain = ino2;
202 affs_adjust_checksum(bh, be32_to_cpu(ino2) - link_ino);
203 mark_buffer_dirty_inode(bh, inode);
205 /* Fix the link count, if bh is a normal header block without links */
206 switch (be32_to_cpu(AFFS_TAIL(sb, bh)->stype)) {
211 if (!AFFS_TAIL(sb, bh)->link_chain)
214 affs_free_block(sb, link_ino);
218 bh = affs_bread(sb, ino);
224 affs_brelse(link_bh);
231 affs_empty_dir(struct inode *inode)
233 struct super_block *sb = inode->i_sb;
234 struct buffer_head *bh;
238 bh = affs_bread(sb, inode->i_ino);
243 for (size = AFFS_SB(sb)->s_hashsize - 1; size >= 0; size--)
244 if (AFFS_HEAD(bh)->table[size])
254 /* Remove a filesystem object. If the object to be removed has
255 * links to it, one of the links must be changed to inherit
256 * the file or directory. As above, any inode will do.
257 * The buffer will not be freed. If the header is a link, the
258 * block will be marked as free.
259 * This function returns a negative error number in case of
260 * an error, else 0 if the inode is to be deleted or 1 if not.
264 affs_remove_header(struct dentry *dentry)
266 struct super_block *sb;
267 struct inode *inode, *dir;
268 struct buffer_head *bh = NULL;
271 dir = dentry->d_parent->d_inode;
275 inode = dentry->d_inode;
279 pr_debug("%s(key=%ld)\n", __func__, inode->i_ino);
281 bh = affs_bread(sb, (u32)(long)dentry->d_fsdata);
285 affs_lock_link(inode);
287 switch (be32_to_cpu(AFFS_TAIL(sb, bh)->stype)) {
289 /* if we ever want to support links to dirs
290 * i_hash_lock of the inode must only be
291 * taken after some checks
293 affs_lock_dir(inode);
294 retval = affs_empty_dir(inode);
295 affs_unlock_dir(inode);
303 retval = affs_remove_hash(dir, bh);
306 mark_buffer_dirty_inode(bh, inode);
308 affs_unlock_dir(dir);
310 if (inode->i_nlink > 1)
311 retval = affs_remove_link(dentry);
314 affs_unlock_link(inode);
315 inode->i_ctime = CURRENT_TIME_SEC;
316 mark_inode_dirty(inode);
323 affs_unlock_dir(dir);
324 affs_unlock_link(inode);
328 /* Checksum a block, do various consistency checks and optionally return
329 the blocks type number. DATA points to the block. If their pointers
330 are non-null, *PTYPE and *STYPE are set to the primary and secondary
331 block types respectively, *HASHSIZE is set to the size of the hashtable
332 (which lets us calculate the block size).
333 Returns non-zero if the block is not consistent. */
336 affs_checksum_block(struct super_block *sb, struct buffer_head *bh)
338 __be32 *ptr = (__be32 *)bh->b_data;
343 for (bsize = sb->s_blocksize / sizeof(__be32); bsize > 0; bsize--)
344 sum += be32_to_cpu(*ptr++);
349 * Calculate the checksum of a disk block and store it
350 * at the indicated position.
354 affs_fix_checksum(struct super_block *sb, struct buffer_head *bh)
356 int cnt = sb->s_blocksize / sizeof(__be32);
357 __be32 *ptr = (__be32 *)bh->b_data;
361 checksumptr = ptr + 5;
363 for (checksum = 0; cnt > 0; ptr++, cnt--)
364 checksum += be32_to_cpu(*ptr);
365 *checksumptr = cpu_to_be32(-checksum);
369 secs_to_datestamp(time_t secs, struct affs_date *ds)
374 secs -= sys_tz.tz_minuteswest * 60 + ((8 * 365 + 2) * 24 * 60 * 60);
378 secs -= days * 86400;
382 ds->days = cpu_to_be32(days);
383 ds->mins = cpu_to_be32(minute);
384 ds->ticks = cpu_to_be32(secs * 50);
388 prot_to_mode(u32 prot)
392 if (!(prot & FIBF_NOWRITE))
394 if (!(prot & FIBF_NOREAD))
396 if (!(prot & FIBF_NOEXECUTE))
398 if (prot & FIBF_GRP_WRITE)
400 if (prot & FIBF_GRP_READ)
402 if (prot & FIBF_GRP_EXECUTE)
404 if (prot & FIBF_OTR_WRITE)
406 if (prot & FIBF_OTR_READ)
408 if (prot & FIBF_OTR_EXECUTE)
415 mode_to_prot(struct inode *inode)
417 u32 prot = AFFS_I(inode)->i_protect;
418 umode_t mode = inode->i_mode;
420 if (!(mode & S_IXUSR))
421 prot |= FIBF_NOEXECUTE;
422 if (!(mode & S_IRUSR))
424 if (!(mode & S_IWUSR))
425 prot |= FIBF_NOWRITE;
427 prot |= FIBF_GRP_EXECUTE;
429 prot |= FIBF_GRP_READ;
431 prot |= FIBF_GRP_WRITE;
433 prot |= FIBF_OTR_EXECUTE;
435 prot |= FIBF_OTR_READ;
437 prot |= FIBF_OTR_WRITE;
439 AFFS_I(inode)->i_protect = prot;
443 affs_error(struct super_block *sb, const char *function, const char *fmt, ...)
445 struct va_format vaf;
451 pr_crit("error (device %s): %s(): %pV\n", sb->s_id, function, &vaf);
452 if (!(sb->s_flags & MS_RDONLY))
453 pr_warn("Remounting filesystem read-only\n");
454 sb->s_flags |= MS_RDONLY;
459 affs_warning(struct super_block *sb, const char *function, const char *fmt, ...)
461 struct va_format vaf;
467 pr_warn("(device %s): %s(): %pV\n", sb->s_id, function, &vaf);
472 affs_nofilenametruncate(const struct dentry *dentry)
474 struct inode *inode = dentry->d_inode;
475 return AFFS_SB(inode->i_sb)->s_flags & SF_NO_TRUNCATE;
479 /* Check if the name is valid for a affs object. */
482 affs_check_name(const unsigned char *name, int len, bool notruncate)
488 return -ENAMETOOLONG;
492 for (i = 0; i < len; i++) {
493 if (name[i] < ' ' || name[i] == ':'
494 || (name[i] > 0x7e && name[i] < 0xa0))
501 /* This function copies name to bstr, with at most 30
502 * characters length. The bstr will be prepended by
504 * NOTE: The name will must be already checked by
509 affs_copy_name(unsigned char *bstr, struct dentry *dentry)
511 int len = min(dentry->d_name.len, 30u);
514 memcpy(bstr, dentry->d_name.name, len);