Commit 53070406 authored by Linus Torvalds's avatar Linus Torvalds

Merge branch 'work.adfs' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs

Pull adfs updates from Al Viro:
 "adfs stuff for this cycle"

* 'work.adfs' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs: (42 commits)
  fs/adfs: bigdir: Fix an error code in adfs_fplus_read()
  Documentation: update adfs filesystem documentation
  fs/adfs: mostly divorse inode number from indirect disc address
  fs/adfs: super: add support for E and E+ floppy image formats
  fs/adfs: super: extract filesystem block probe
  fs/adfs: dir: remove debug in adfs_dir_update()
  fs/adfs: super: fix inode dropping
  fs/adfs: bigdir: implement directory update support
  fs/adfs: bigdir: calculate and validate directory checkbyte
  fs/adfs: bigdir: directory validation strengthening
  fs/adfs: bigdir: extract directory validation
  fs/adfs: bigdir: factor out directory entry offset calculation
  fs/adfs: newdir: split out directory commit from update
  fs/adfs: newdir: clean up adfs_f_update()
  fs/adfs: newdir: merge adfs_dir_read() into adfs_f_read()
  fs/adfs: newdir: improve directory validation
  fs/adfs: newdir: factor out directory format validation
  fs/adfs: dir: use pointers to access directory head/tails
  fs/adfs: dir: add more efficient iterate() per-format method
  fs/adfs: dir: switch to iterate_shared method
  ...
parents 6aee4bad 587065dc
Filesystems supported by ADFS
-----------------------------
The ADFS module supports the following Filecore formats which have:
- new maps
- new directories or big directories
In terms of the named formats, this means we support:
- E and E+, with or without boot block
- F and F+
We fully support reading files from these filesystems, and writing to
existing files within their existing allocation. Essentially, we do
not support changing any of the filesystem metadata.
This is intended to support loopback mounted Linux native filesystems
on a RISC OS Filecore filesystem, but will allow the data within files
to be changed.
If write support (ADFS_FS_RW) is configured, we allow rudimentary
directory updates, specifically updating the access mode and timestamp.
Mount options for ADFS Mount options for ADFS
---------------------- ----------------------
......
...@@ -26,14 +26,13 @@ static inline u16 adfs_filetype(u32 loadaddr) ...@@ -26,14 +26,13 @@ static inline u16 adfs_filetype(u32 loadaddr)
#define ADFS_NDA_PUBLIC_READ (1 << 5) #define ADFS_NDA_PUBLIC_READ (1 << 5)
#define ADFS_NDA_PUBLIC_WRITE (1 << 6) #define ADFS_NDA_PUBLIC_WRITE (1 << 6)
#include "dir_f.h"
/* /*
* adfs file system inode data in memory * adfs file system inode data in memory
*/ */
struct adfs_inode_info { struct adfs_inode_info {
loff_t mmu_private; loff_t mmu_private;
__u32 parent_id; /* parent indirect disc address */ __u32 parent_id; /* parent indirect disc address */
__u32 indaddr; /* object indirect disc address */
__u32 loadaddr; /* RISC OS load address */ __u32 loadaddr; /* RISC OS load address */
__u32 execaddr; /* RISC OS exec address */ __u32 execaddr; /* RISC OS exec address */
unsigned int attr; /* RISC OS permissions */ unsigned int attr; /* RISC OS permissions */
...@@ -93,15 +92,19 @@ struct adfs_dir { ...@@ -93,15 +92,19 @@ struct adfs_dir {
int nr_buffers; int nr_buffers;
struct buffer_head *bh[4]; struct buffer_head *bh[4];
struct buffer_head **bhs;
/* big directories need allocated buffers */
struct buffer_head **bh_fplus;
unsigned int pos; unsigned int pos;
__u32 parent_id; __u32 parent_id;
struct adfs_dirheader dirhead; union {
union adfs_dirtail dirtail; struct adfs_dirheader *dirhead;
struct adfs_bigdirheader *bighead;
};
union {
struct adfs_newdirtail *newtail;
struct adfs_bigdirtail *bigtail;
};
}; };
/* /*
...@@ -122,13 +125,13 @@ struct object_info { ...@@ -122,13 +125,13 @@ struct object_info {
struct adfs_dir_ops { struct adfs_dir_ops {
int (*read)(struct super_block *sb, unsigned int indaddr, int (*read)(struct super_block *sb, unsigned int indaddr,
unsigned int size, struct adfs_dir *dir); unsigned int size, struct adfs_dir *dir);
int (*iterate)(struct adfs_dir *dir, struct dir_context *ctx);
int (*setpos)(struct adfs_dir *dir, unsigned int fpos); int (*setpos)(struct adfs_dir *dir, unsigned int fpos);
int (*getnext)(struct adfs_dir *dir, struct object_info *obj); int (*getnext)(struct adfs_dir *dir, struct object_info *obj);
int (*update)(struct adfs_dir *dir, struct object_info *obj); int (*update)(struct adfs_dir *dir, struct object_info *obj);
int (*create)(struct adfs_dir *dir, struct object_info *obj); int (*create)(struct adfs_dir *dir, struct object_info *obj);
int (*remove)(struct adfs_dir *dir, struct object_info *obj); int (*remove)(struct adfs_dir *dir, struct object_info *obj);
int (*sync)(struct adfs_dir *dir); int (*commit)(struct adfs_dir *dir);
void (*free)(struct adfs_dir *dir);
}; };
struct adfs_discmap { struct adfs_discmap {
...@@ -145,7 +148,9 @@ int adfs_notify_change(struct dentry *dentry, struct iattr *attr); ...@@ -145,7 +148,9 @@ int adfs_notify_change(struct dentry *dentry, struct iattr *attr);
/* map.c */ /* map.c */
int adfs_map_lookup(struct super_block *sb, u32 frag_id, unsigned int offset); int adfs_map_lookup(struct super_block *sb, u32 frag_id, unsigned int offset);
extern unsigned int adfs_map_free(struct super_block *sb); void adfs_map_statfs(struct super_block *sb, struct kstatfs *buf);
struct adfs_discmap *adfs_read_map(struct super_block *sb, struct adfs_discrecord *dr);
void adfs_free_map(struct super_block *sb);
/* Misc */ /* Misc */
__printf(3, 4) __printf(3, 4)
...@@ -167,6 +172,13 @@ extern const struct dentry_operations adfs_dentry_operations; ...@@ -167,6 +172,13 @@ extern const struct dentry_operations adfs_dentry_operations;
extern const struct adfs_dir_ops adfs_f_dir_ops; extern const struct adfs_dir_ops adfs_f_dir_ops;
extern const struct adfs_dir_ops adfs_fplus_dir_ops; extern const struct adfs_dir_ops adfs_fplus_dir_ops;
int adfs_dir_copyfrom(void *dst, struct adfs_dir *dir, unsigned int offset,
size_t len);
int adfs_dir_copyto(struct adfs_dir *dir, unsigned int offset, const void *src,
size_t len);
void adfs_dir_relse(struct adfs_dir *dir);
int adfs_dir_read_buffers(struct super_block *sb, u32 indaddr,
unsigned int size, struct adfs_dir *dir);
void adfs_object_fixup(struct adfs_dir *dir, struct object_info *obj); void adfs_object_fixup(struct adfs_dir *dir, struct object_info *obj);
extern int adfs_dir_update(struct super_block *sb, struct object_info *obj, extern int adfs_dir_update(struct super_block *sb, struct object_info *obj,
int wait); int wait);
......
...@@ -6,12 +6,196 @@ ...@@ -6,12 +6,196 @@
* *
* Common directory handling for ADFS * Common directory handling for ADFS
*/ */
#include <linux/slab.h>
#include "adfs.h" #include "adfs.h"
/* /*
* For future. This should probably be per-directory. * For future. This should probably be per-directory.
*/ */
static DEFINE_RWLOCK(adfs_dir_lock); static DECLARE_RWSEM(adfs_dir_rwsem);
int adfs_dir_copyfrom(void *dst, struct adfs_dir *dir, unsigned int offset,
size_t len)
{
struct super_block *sb = dir->sb;
unsigned int index, remain;
index = offset >> sb->s_blocksize_bits;
offset &= sb->s_blocksize - 1;
remain = sb->s_blocksize - offset;
if (index + (remain < len) >= dir->nr_buffers)
return -EINVAL;
if (remain < len) {
memcpy(dst, dir->bhs[index]->b_data + offset, remain);
dst += remain;
len -= remain;
index += 1;
offset = 0;
}
memcpy(dst, dir->bhs[index]->b_data + offset, len);
return 0;
}
int adfs_dir_copyto(struct adfs_dir *dir, unsigned int offset, const void *src,
size_t len)
{
struct super_block *sb = dir->sb;
unsigned int index, remain;
index = offset >> sb->s_blocksize_bits;
offset &= sb->s_blocksize - 1;
remain = sb->s_blocksize - offset;
if (index + (remain < len) >= dir->nr_buffers)
return -EINVAL;
if (remain < len) {
memcpy(dir->bhs[index]->b_data + offset, src, remain);
src += remain;
len -= remain;
index += 1;
offset = 0;
}
memcpy(dir->bhs[index]->b_data + offset, src, len);
return 0;
}
static void __adfs_dir_cleanup(struct adfs_dir *dir)
{
dir->nr_buffers = 0;
if (dir->bhs != dir->bh)
kfree(dir->bhs);
dir->bhs = NULL;
dir->sb = NULL;
}
void adfs_dir_relse(struct adfs_dir *dir)
{
unsigned int i;
for (i = 0; i < dir->nr_buffers; i++)
brelse(dir->bhs[i]);
__adfs_dir_cleanup(dir);
}
static void adfs_dir_forget(struct adfs_dir *dir)
{
unsigned int i;
for (i = 0; i < dir->nr_buffers; i++)
bforget(dir->bhs[i]);
__adfs_dir_cleanup(dir);
}
int adfs_dir_read_buffers(struct super_block *sb, u32 indaddr,
unsigned int size, struct adfs_dir *dir)
{
struct buffer_head **bhs;
unsigned int i, num;
int block;
num = ALIGN(size, sb->s_blocksize) >> sb->s_blocksize_bits;
if (num > ARRAY_SIZE(dir->bh)) {
/* We only allow one extension */
if (dir->bhs != dir->bh)
return -EINVAL;
bhs = kcalloc(num, sizeof(*bhs), GFP_KERNEL);
if (!bhs)
return -ENOMEM;
if (dir->nr_buffers)
memcpy(bhs, dir->bhs, dir->nr_buffers * sizeof(*bhs));
dir->bhs = bhs;
}
for (i = dir->nr_buffers; i < num; i++) {
block = __adfs_block_map(sb, indaddr, i);
if (!block) {
adfs_error(sb, "dir %06x has a hole at offset %u",
indaddr, i);
goto error;
}
dir->bhs[i] = sb_bread(sb, block);
if (!dir->bhs[i]) {
adfs_error(sb,
"dir %06x failed read at offset %u, mapped block 0x%08x",
indaddr, i, block);
goto error;
}
dir->nr_buffers++;
}
return 0;
error:
adfs_dir_relse(dir);
return -EIO;
}
static int adfs_dir_read(struct super_block *sb, u32 indaddr,
unsigned int size, struct adfs_dir *dir)
{
dir->sb = sb;
dir->bhs = dir->bh;
dir->nr_buffers = 0;
return ADFS_SB(sb)->s_dir->read(sb, indaddr, size, dir);
}
static int adfs_dir_read_inode(struct super_block *sb, struct inode *inode,
struct adfs_dir *dir)
{
int ret;
ret = adfs_dir_read(sb, ADFS_I(inode)->indaddr, inode->i_size, dir);
if (ret)
return ret;
if (ADFS_I(inode)->parent_id != dir->parent_id) {
adfs_error(sb,
"parent directory id changed under me! (%06x but got %06x)\n",
ADFS_I(inode)->parent_id, dir->parent_id);
adfs_dir_relse(dir);
ret = -EIO;
}
return ret;
}
static void adfs_dir_mark_dirty(struct adfs_dir *dir)
{
unsigned int i;
/* Mark the buffers dirty */
for (i = 0; i < dir->nr_buffers; i++)
mark_buffer_dirty(dir->bhs[i]);
}
static int adfs_dir_sync(struct adfs_dir *dir)
{
int err = 0;
int i;
for (i = dir->nr_buffers - 1; i >= 0; i--) {
struct buffer_head *bh = dir->bhs[i];
sync_dirty_buffer(bh);
if (buffer_req(bh) && !buffer_uptodate(bh))
err = -EIO;
}
return err;
}
void adfs_object_fixup(struct adfs_dir *dir, struct object_info *obj) void adfs_object_fixup(struct adfs_dir *dir, struct object_info *obj)
{ {
...@@ -51,87 +235,90 @@ void adfs_object_fixup(struct adfs_dir *dir, struct object_info *obj) ...@@ -51,87 +235,90 @@ void adfs_object_fixup(struct adfs_dir *dir, struct object_info *obj)
} }
} }
static int static int adfs_iterate(struct file *file, struct dir_context *ctx)
adfs_readdir(struct file *file, struct dir_context *ctx)
{ {
struct inode *inode = file_inode(file); struct inode *inode = file_inode(file);
struct super_block *sb = inode->i_sb; struct super_block *sb = inode->i_sb;
const struct adfs_dir_ops *ops = ADFS_SB(sb)->s_dir; const struct adfs_dir_ops *ops = ADFS_SB(sb)->s_dir;
struct object_info obj;
struct adfs_dir dir; struct adfs_dir dir;
int ret = 0; int ret;
if (ctx->pos >> 32)
return 0;
ret = ops->read(sb, inode->i_ino, inode->i_size, &dir); down_read(&adfs_dir_rwsem);
ret = adfs_dir_read_inode(sb, inode, &dir);
if (ret) if (ret)
return ret; goto unlock;
if (ctx->pos == 0) { if (ctx->pos == 0) {
if (!dir_emit_dot(file, ctx)) if (!dir_emit_dot(file, ctx))
goto free_out; goto unlock_relse;
ctx->pos = 1; ctx->pos = 1;
} }
if (ctx->pos == 1) { if (ctx->pos == 1) {
if (!dir_emit(ctx, "..", 2, dir.parent_id, DT_DIR)) if (!dir_emit(ctx, "..", 2, dir.parent_id, DT_DIR))
goto free_out; goto unlock_relse;
ctx->pos = 2; ctx->pos = 2;
} }
read_lock(&adfs_dir_lock); ret = ops->iterate(&dir, ctx);
ret = ops->setpos(&dir, ctx->pos - 2); unlock_relse:
if (ret) up_read(&adfs_dir_rwsem);
goto unlock_out; adfs_dir_relse(&dir);
while (ops->getnext(&dir, &obj) == 0) { return ret;
if (!dir_emit(ctx, obj.name, obj.name_len,
obj.indaddr, DT_UNKNOWN))
break;
ctx->pos++;
}
unlock_out:
read_unlock(&adfs_dir_lock);
free_out: unlock:
ops->free(&dir); up_read(&adfs_dir_rwsem);
return ret; return ret;
} }
int int
adfs_dir_update(struct super_block *sb, struct object_info *obj, int wait) adfs_dir_update(struct super_block *sb, struct object_info *obj, int wait)
{ {
int ret = -EINVAL;
#ifdef CONFIG_ADFS_FS_RW
const struct adfs_dir_ops *ops = ADFS_SB(sb)->s_dir; const struct adfs_dir_ops *ops = ADFS_SB(sb)->s_dir;
struct adfs_dir dir; struct adfs_dir dir;
int ret;
printk(KERN_INFO "adfs_dir_update: object %06x in dir %06x\n", if (!IS_ENABLED(CONFIG_ADFS_FS_RW))
obj->indaddr, obj->parent_id); return -EINVAL;
if (!ops->update) { if (!ops->update)
ret = -EINVAL; return -EINVAL;
goto out;
}
ret = ops->read(sb, obj->parent_id, 0, &dir); down_write(&adfs_dir_rwsem);
ret = adfs_dir_read(sb, obj->parent_id, 0, &dir);
if (ret) if (ret)
goto out; goto unlock;
write_lock(&adfs_dir_lock);
ret = ops->update(&dir, obj); ret = ops->update(&dir, obj);
write_unlock(&adfs_dir_lock); if (ret)
goto forget;
if (wait) { ret = ops->commit(&dir);
int err = ops->sync(&dir); if (ret)
if (!ret) goto forget;
ret = err; up_write(&adfs_dir_rwsem);
}
adfs_dir_mark_dirty(&dir);
if (wait)
ret = adfs_dir_sync(&dir);
adfs_dir_relse(&dir);
return ret;
/*
* If the updated failed because the entry wasn't found, we can
* just release the buffers. If it was any other error, forget
* the dirtied buffers so they aren't written back to the media.
*/
forget:
if (ret == -ENOENT)
adfs_dir_relse(&dir);
else
adfs_dir_forget(&dir);
unlock:
up_write(&adfs_dir_rwsem);
ops->free(&dir);
out:
#endif
return ret; return ret;
} }
...@@ -167,25 +354,14 @@ static int adfs_dir_lookup_byname(struct inode *inode, const struct qstr *qstr, ...@@ -167,25 +354,14 @@ static int adfs_dir_lookup_byname(struct inode *inode, const struct qstr *qstr,
u32 name_len; u32 name_len;
int ret; int ret;
ret = ops->read(sb, inode->i_ino, inode->i_size, &dir); down_read(&adfs_dir_rwsem);
ret = adfs_dir_read_inode(sb, inode, &dir);
if (ret) if (ret)
goto out; goto unlock;
if (ADFS_I(inode)->parent_id != dir.parent_id) {
adfs_error(sb,
"parent directory changed under me! (%06x but got %06x)\n",
ADFS_I(inode)->parent_id, dir.parent_id);
ret = -EIO;
goto free_out;
}
obj->parent_id = inode->i_ino;
read_lock(&adfs_dir_lock);
ret = ops->setpos(&dir, 0); ret = ops->setpos(&dir, 0);
if (ret) if (ret)
goto unlock_out; goto unlock_relse;
ret = -ENOENT; ret = -ENOENT;
name = qstr->name; name = qstr->name;
...@@ -196,20 +372,22 @@ static int adfs_dir_lookup_byname(struct inode *inode, const struct qstr *qstr, ...@@ -196,20 +372,22 @@ static int adfs_dir_lookup_byname(struct inode *inode, const struct qstr *qstr,
break; break;
} }
} }
obj->parent_id = ADFS_I(inode)->indaddr;
unlock_out: unlock_relse:
read_unlock(&adfs_dir_lock); up_read(&adfs_dir_rwsem);
adfs_dir_relse(&dir);
return ret;
free_out: unlock:
ops->free(&dir); up_read(&adfs_dir_rwsem);
out:
return ret; return ret;
} }
const struct file_operations adfs_dir_operations = { const struct file_operations adfs_dir_operations = {
.read = generic_read_dir, .read = generic_read_dir,
.llseek = generic_file_llseek, .llseek = generic_file_llseek,
.iterate = adfs_readdir, .iterate_shared = adfs_iterate,
.fsync = generic_file_fsync, .fsync = generic_file_fsync,
}; };
......
...@@ -9,8 +9,6 @@ ...@@ -9,8 +9,6 @@
#include "adfs.h" #include "adfs.h"
#include "dir_f.h" #include "dir_f.h"
static void adfs_f_free(struct adfs_dir *dir);
/* /*
* Read an (unaligned) value of length 1..4 bytes * Read an (unaligned) value of length 1..4 bytes
*/ */
...@@ -60,7 +58,7 @@ static inline void adfs_writeval(unsigned char *p, int len, unsigned int val) ...@@ -60,7 +58,7 @@ static inline void adfs_writeval(unsigned char *p, int len, unsigned int val)
#define bufoff(_bh,_idx) \ #define bufoff(_bh,_idx) \
({ int _buf = _idx >> blocksize_bits; \ ({ int _buf = _idx >> blocksize_bits; \
int _off = _idx - (_buf << blocksize_bits);\ int _off = _idx - (_buf << blocksize_bits);\
(u8 *)(_bh[_buf]->b_data + _off); \ (void *)(_bh[_buf]->b_data + _off); \
}) })
/* /*
...@@ -123,65 +121,49 @@ adfs_dir_checkbyte(const struct adfs_dir *dir) ...@@ -123,65 +121,49 @@ adfs_dir_checkbyte(const struct adfs_dir *dir)
return (dircheck ^ (dircheck >> 8) ^ (dircheck >> 16) ^ (dircheck >> 24)) & 0xff; return (dircheck ^ (dircheck >> 8) ^ (dircheck >> 16) ^ (dircheck >> 24)) & 0xff;
} }
/* Read and check that a directory is valid */ static int adfs_f_validate(struct adfs_dir *dir)
static int adfs_dir_read(struct super_block *sb, u32 indaddr,
unsigned int size, struct adfs_dir *dir)
{ {
const unsigned int blocksize_bits = sb->s_blocksize_bits; struct adfs_dirheader *head = dir->dirhead;
int blk = 0; struct adfs_newdirtail *tail = dir->newtail;
/* if (head->startmasseq != tail->endmasseq ||
* Directories which are not a multiple of 2048 bytes tail->dirlastmask || tail->reserved[0] || tail->reserved[1] ||
* are considered bad v2 [3.6] (memcmp(&head->startname, "Nick", 4) &&
*/ memcmp(&head->startname, "Hugo", 4)) ||
if (size & 2047) memcmp(&head->startname, &tail->endname, 4) ||
goto bad_dir; adfs_dir_checkbyte(dir) != tail->dircheckbyte)
return -EIO;
size >>= blocksize_bits;
dir->nr_buffers = 0;
dir->sb = sb;
for (blk = 0; blk < size; blk++) {
int phys;
phys = __adfs_block_map(sb, indaddr, blk); return 0;
if (!phys) { }
adfs_error(sb, "dir %06x has a hole at offset %d",
indaddr, blk);
goto release_buffers;
}
dir->bh[blk] = sb_bread(sb, phys); /* Read and check that a directory is valid */
if (!dir->bh[blk]) static int adfs_f_read(struct super_block *sb, u32 indaddr, unsigned int size,
goto release_buffers; struct adfs_dir *dir)
} {
const unsigned int blocksize_bits = sb->s_blocksize_bits;
int ret;
memcpy(&dir->dirhead, bufoff(dir->bh, 0), sizeof(dir->dirhead)); if (size && size != ADFS_NEWDIR_SIZE)
memcpy(&dir->dirtail, bufoff(dir->bh, 2007), sizeof(dir->dirtail)); return -EIO;
if (dir->dirhead.startmasseq != dir->dirtail.new.endmasseq || ret = adfs_dir_read_buffers(sb, indaddr, ADFS_NEWDIR_SIZE, dir);
memcmp(&dir->dirhead.startname, &dir->dirtail.new.endname, 4)) if (ret)
goto bad_dir; return ret;
if (memcmp(&dir->dirhead.startname, "Nick", 4) && dir->dirhead = bufoff(dir->bh, 0);
memcmp(&dir->dirhead.startname, "Hugo", 4)) dir->newtail = bufoff(dir->bh, 2007);
goto bad_dir;
if (adfs_dir_checkbyte(dir) != dir->dirtail.new.dircheckbyte) if (adfs_f_validate(dir))
goto bad_dir; goto bad_dir;
dir->nr_buffers = blk; dir->parent_id = adfs_readval(dir->newtail->dirparent, 3);
return 0; return 0;
bad_dir: bad_dir:
adfs_error(sb, "dir %06x is corrupted", indaddr); adfs_error(sb, "dir %06x is corrupted", indaddr);
release_buffers: adfs_dir_relse(dir);
for (blk -= 1; blk >= 0; blk -= 1)
brelse(dir->bh[blk]);
dir->sb = NULL;
return -EIO; return -EIO;
} }
...@@ -232,24 +214,12 @@ adfs_obj2dir(struct adfs_direntry *de, struct object_info *obj) ...@@ -232,24 +214,12 @@ adfs_obj2dir(struct adfs_direntry *de, struct object_info *obj)
static int static int
__adfs_dir_get(struct adfs_dir *dir, int pos, struct object_info *obj) __adfs_dir_get(struct adfs_dir *dir, int pos, struct object_info *obj)
{ {
struct super_block *sb = dir->sb;
struct adfs_direntry de; struct adfs_direntry de;
int thissize, buffer, offset; int ret;
buffer = pos >> sb->s_blocksize_bits;
if (buffer > dir->nr_buffers)
return -EINVAL;
offset = pos & (sb->s_blocksize - 1);
thissize = sb->s_blocksize - offset;
if (thissize > 26)
thissize = 26;
memcpy(&de, dir->bh[buffer]->b_data + offset, thissize); ret = adfs_dir_copyfrom(&de, dir, pos, 26);
if (thissize != 26) if (ret)
memcpy(((char *)&de) + thissize, dir->bh[buffer + 1]->b_data, return ret;
26 - thissize);
if (!de.dirobname[0]) if (!de.dirobname[0])
return -ENOENT; return -ENOENT;
...@@ -259,89 +229,6 @@ __adfs_dir_get(struct adfs_dir *dir, int pos, struct object_info *obj) ...@@ -259,89 +229,6 @@ __adfs_dir_get(struct adfs_dir *dir, int pos, struct object_info *obj)
return 0; return 0;
} }
static int
__adfs_dir_put(struct adfs_dir *dir, int pos, struct object_info *obj)
{
struct super_block *sb = dir->sb;
struct adfs_direntry de;
int thissize, buffer, offset;
buffer = pos >> sb->s_blocksize_bits;
if (buffer > dir->nr_buffers)
return -EINVAL;
offset = pos & (sb->s_blocksize - 1);
thissize = sb->s_blocksize - offset;
if (thissize > 26)
thissize = 26;
/*
* Get the entry in total
*/
memcpy(&de, dir->bh[buffer]->b_data + offset, thissize);
if (thissize != 26)
memcpy(((char *)&de) + thissize, dir->bh[buffer + 1]->b_data,
26 - thissize);
/*
* update it
*/
adfs_obj2dir(&de, obj);
/*
* Put the new entry back
*/
memcpy(dir->bh[buffer]->b_data + offset, &de, thissize);
if (thissize != 26)
memcpy(dir->bh[buffer + 1]->b_data, ((char *)&de) + thissize,
26 - thissize);
return 0;
}
/*
* the caller is responsible for holding the necessary
* locks.
*/
static int adfs_dir_find_entry(struct adfs_dir *dir, u32 indaddr)
{
int pos, ret;
ret = -ENOENT;
for (pos = 5; pos < ADFS_NUM_DIR_ENTRIES * 26 + 5; pos += 26) {
struct object_info obj;
if (!__adfs_dir_get(dir, pos, &obj))
break;
if (obj.indaddr == indaddr) {
ret = pos;
break;
}
}
return ret;
}
static int adfs_f_read(struct super_block *sb, u32 indaddr, unsigned int size,
struct adfs_dir *dir)
{
int ret;
if (size != ADFS_NEWDIR_SIZE)
return -EIO;
ret = adfs_dir_read(sb, indaddr, size, dir);
if (ret)
adfs_error(sb, "unable to read directory");
else
dir->parent_id = adfs_readval(dir->dirtail.new.dirparent, 3);
return ret;
}
static int static int
adfs_f_setpos(struct adfs_dir *dir, unsigned int fpos) adfs_f_setpos(struct adfs_dir *dir, unsigned int fpos)
{ {
...@@ -364,99 +251,74 @@ adfs_f_getnext(struct adfs_dir *dir, struct object_info *obj) ...@@ -364,99 +251,74 @@ adfs_f_getnext(struct adfs_dir *dir, struct object_info *obj)
return ret; return ret;
} }
static int static int adfs_f_iterate(struct adfs_dir *dir, struct dir_context *ctx)
adfs_f_update(struct adfs_dir *dir, struct object_info *obj)
{ {
struct super_block *sb = dir->sb; struct object_info obj;
int ret, i; int pos = 5 + (ctx->pos - 2) * 26;
ret = adfs_dir_find_entry(dir, obj->indaddr); while (ctx->pos < 2 + ADFS_NUM_DIR_ENTRIES) {
if (ret < 0) { if (__adfs_dir_get(dir, pos, &obj))
adfs_error(dir->sb, "unable to locate entry to update"); break;
goto out; if (!dir_emit(ctx, obj.name, obj.name_len,
obj.indaddr, DT_UNKNOWN))
break;
pos += 26;
ctx->pos++;
} }
return 0;
}
__adfs_dir_put(dir, ret, obj); static int adfs_f_update(struct adfs_dir *dir, struct object_info *obj)
{
/* struct adfs_direntry de;
* Increment directory sequence number int offset, ret;
*/
dir->bh[0]->b_data[0] += 1;
dir->bh[dir->nr_buffers - 1]->b_data[sb->s_blocksize - 6] += 1;
ret = adfs_dir_checkbyte(dir);
/*
* Update directory check byte
*/
dir->bh[dir->nr_buffers - 1]->b_data[sb->s_blocksize - 1] = ret;
#if 1
{
const unsigned int blocksize_bits = sb->s_blocksize_bits;
memcpy(&dir->dirhead, bufoff(dir->bh, 0), sizeof(dir->dirhead));
memcpy(&dir->dirtail, bufoff(dir->bh, 2007), sizeof(dir->dirtail));
if (dir->dirhead.startmasseq != dir->dirtail.new.endmasseq || offset = 5 - (int)sizeof(de);
memcmp(&dir->dirhead.startname, &dir->dirtail.new.endname, 4))
goto bad_dir;
if (memcmp(&dir->dirhead.startname, "Nick", 4) && do {
memcmp(&dir->dirhead.startname, "Hugo", 4)) offset += sizeof(de);
goto bad_dir; ret = adfs_dir_copyfrom(&de, dir, offset, sizeof(de));
if (ret) {
adfs_error(dir->sb, "error reading directory entry");
return -ENOENT;
}
if (!de.dirobname[0]) {
adfs_error(dir->sb, "unable to locate entry to update");
return -ENOENT;
}
} while (adfs_readval(de.dirinddiscadd, 3) != obj->indaddr);
if (adfs_dir_checkbyte(dir) != dir->dirtail.new.dircheckbyte) /* Update the directory entry with the new object state */
goto bad_dir; adfs_obj2dir(&de, obj);
}
#endif
for (i = dir->nr_buffers - 1; i >= 0; i--)
mark_buffer_dirty(dir->bh[i]);
ret = 0; /* Write the directory entry back to the directory */
out: return adfs_dir_copyto(dir, offset, &de, 26);
return ret;
#if 1
bad_dir:
adfs_error(dir->sb, "whoops! I broke a directory!");
return -EIO;
#endif
} }
static int static int adfs_f_commit(struct adfs_dir *dir)
adfs_f_sync(struct adfs_dir *dir)
{ {
int err = 0; int ret;
int i;
for (i = dir->nr_buffers - 1; i >= 0; i--) {
struct buffer_head *bh = dir->bh[i];
sync_dirty_buffer(bh);
if (buffer_req(bh) && !buffer_uptodate(bh))
err = -EIO;
}
return err; /* Increment directory sequence number */
} dir->dirhead->startmasseq += 1;
dir->newtail->endmasseq += 1;
static void /* Update directory check byte */
adfs_f_free(struct adfs_dir *dir) dir->newtail->dircheckbyte = adfs_dir_checkbyte(dir);
{
int i;
for (i = dir->nr_buffers - 1; i >= 0; i--) { /* Make sure the directory still validates correctly */
brelse(dir->bh[i]); ret = adfs_f_validate(dir);
dir->bh[i] = NULL; if (ret)
} adfs_msg(dir->sb, KERN_ERR, "error: update broke directory");
dir->nr_buffers = 0; return ret;
dir->sb = NULL;
} }
const struct adfs_dir_ops adfs_f_dir_ops = { const struct adfs_dir_ops adfs_f_dir_ops = {
.read = adfs_f_read, .read = adfs_f_read,
.iterate = adfs_f_iterate,
.setpos = adfs_f_setpos, .setpos = adfs_f_setpos,
.getnext = adfs_f_getnext, .getnext = adfs_f_getnext,
.update = adfs_f_update, .update = adfs_f_update,
.sync = adfs_f_sync, .commit = adfs_f_commit,
.free = adfs_f_free
}; };
...@@ -13,9 +13,9 @@ ...@@ -13,9 +13,9 @@
* Directory header * Directory header
*/ */
struct adfs_dirheader { struct adfs_dirheader {
unsigned char startmasseq; __u8 startmasseq;
unsigned char startname[4]; __u8 startname[4];
}; } __attribute__((packed));
#define ADFS_NEWDIR_SIZE 2048 #define ADFS_NEWDIR_SIZE 2048
#define ADFS_NUM_DIR_ENTRIES 77 #define ADFS_NUM_DIR_ENTRIES 77
...@@ -31,32 +31,36 @@ struct adfs_direntry { ...@@ -31,32 +31,36 @@ struct adfs_direntry {
__u8 dirlen[4]; __u8 dirlen[4];
__u8 dirinddiscadd[3]; __u8 dirinddiscadd[3];
__u8 newdiratts; __u8 newdiratts;
}; } __attribute__((packed));
/* /*
* Directory tail * Directory tail
*/ */
struct adfs_olddirtail {
__u8 dirlastmask;
char dirname[10];
__u8 dirparent[3];
char dirtitle[19];
__u8 reserved[14];
__u8 endmasseq;
__u8 endname[4];
__u8 dircheckbyte;
} __attribute__((packed));
struct adfs_newdirtail {
__u8 dirlastmask;
__u8 reserved[2];
__u8 dirparent[3];
char dirtitle[19];
char dirname[10];
__u8 endmasseq;
__u8 endname[4];
__u8 dircheckbyte;
} __attribute__((packed));
union adfs_dirtail { union adfs_dirtail {
struct { struct adfs_olddirtail old;
unsigned char dirlastmask; struct adfs_newdirtail new;
char dirname[10];
unsigned char dirparent[3];
char dirtitle[19];
unsigned char reserved[14];
unsigned char endmasseq;
unsigned char endname[4];
unsigned char dircheckbyte;
} old;
struct {
unsigned char dirlastmask;
unsigned char reserved[2];
unsigned char dirparent[3];
char dirtitle[19];
char dirname[10];
unsigned char endmasseq;
unsigned char endname[4];
unsigned char dircheckbyte;
} new;
}; };
#endif #endif
...@@ -4,123 +4,163 @@ ...@@ -4,123 +4,163 @@
* *
* Copyright (C) 1997-1999 Russell King * Copyright (C) 1997-1999 Russell King
*/ */
#include <linux/slab.h>
#include "adfs.h" #include "adfs.h"
#include "dir_fplus.h" #include "dir_fplus.h"
static int /* Return the byte offset to directory entry pos */
adfs_fplus_read(struct super_block *sb, unsigned int id, unsigned int sz, struct adfs_dir *dir) static unsigned int adfs_fplus_offset(const struct adfs_bigdirheader *h,
unsigned int pos)
{ {
struct adfs_bigdirheader *h; return offsetof(struct adfs_bigdirheader, bigdirname) +
struct adfs_bigdirtail *t; ALIGN(le32_to_cpu(h->bigdirnamelen), 4) +
unsigned long block; pos * sizeof(struct adfs_bigdirentry);
unsigned int blk, size; }
int i, ret = -EIO;
dir->nr_buffers = 0; static int adfs_fplus_validate_header(const struct adfs_bigdirheader *h)
{
unsigned int size = le32_to_cpu(h->bigdirsize);
unsigned int len;
/* start off using fixed bh set - only alloc for big dirs */ if (h->bigdirversion[0] != 0 || h->bigdirversion[1] != 0 ||
dir->bh_fplus = &dir->bh[0]; h->bigdirversion[2] != 0 ||
h->bigdirstartname != cpu_to_le32(BIGDIRSTARTNAME) ||
!size || size & 2047 || size > SZ_4M)
return -EIO;
block = __adfs_block_map(sb, id, 0); size -= sizeof(struct adfs_bigdirtail) +
if (!block) { offsetof(struct adfs_bigdirheader, bigdirname);
adfs_error(sb, "dir object %X has a hole at offset 0", id);
goto out;
}
dir->bh_fplus[0] = sb_bread(sb, block); /* Check that bigdirnamelen fits within the directory */
if (!dir->bh_fplus[0]) len = ALIGN(le32_to_cpu(h->bigdirnamelen), 4);
goto out; if (len > size)
dir->nr_buffers += 1; return -EIO;
h = (struct adfs_bigdirheader *)dir->bh_fplus[0]->b_data; size -= len;
size = le32_to_cpu(h->bigdirsize);
if (size != sz) { /* Check that bigdirnamesize fits within the directory */
adfs_msg(sb, KERN_WARNING, len = le32_to_cpu(h->bigdirnamesize);
"directory header size %X does not match directory size %X", if (len > size)
size, sz); return -EIO;
size -= len;
/*
* Avoid division, we know that absolute maximum number of entries
* can not be so large to cause overflow of the multiplication below.
*/
len = le32_to_cpu(h->bigdirentries);
if (len > SZ_4M / sizeof(struct adfs_bigdirentry) ||
len * sizeof(struct adfs_bigdirentry) > size)
return -EIO;
return 0;
}
static int adfs_fplus_validate_tail(const struct adfs_bigdirheader *h,
const struct adfs_bigdirtail *t)
{
if (t->bigdirendname != cpu_to_le32(BIGDIRENDNAME) ||
t->bigdirendmasseq != h->startmasseq ||
t->reserved[0] != 0 || t->reserved[1] != 0)
return -EIO;
return 0;
}
static u8 adfs_fplus_checkbyte(struct adfs_dir *dir)
{
struct adfs_bigdirheader *h = dir->bighead;
struct adfs_bigdirtail *t = dir->bigtail;
unsigned int end, bs, bi, i;
__le32 *bp;
u32 dircheck;
end = adfs_fplus_offset(h, le32_to_cpu(h->bigdirentries)) +
le32_to_cpu(h->bigdirnamesize);
/* Accumulate the contents of the header, entries and names */
for (dircheck = 0, bi = 0; end; bi++) {
bp = (void *)dir->bhs[bi]->b_data;
bs = dir->bhs[bi]->b_size;
if (bs > end)
bs = end;
for (i = 0; i < bs; i += sizeof(u32))
dircheck = ror32(dircheck, 13) ^ le32_to_cpup(bp++);
end -= bs;
} }
if (h->bigdirversion[0] != 0 || h->bigdirversion[1] != 0 || /* Accumulate the contents of the tail except for the check byte */
h->bigdirversion[2] != 0 || size & 2047 || dircheck = ror32(dircheck, 13) ^ le32_to_cpu(t->bigdirendname);
h->bigdirstartname != cpu_to_le32(BIGDIRSTARTNAME)) { dircheck = ror32(dircheck, 13) ^ t->bigdirendmasseq;
adfs_error(sb, "dir %06x has malformed header", id); dircheck = ror32(dircheck, 13) ^ t->reserved[0];
dircheck = ror32(dircheck, 13) ^ t->reserved[1];
return dircheck ^ dircheck >> 8 ^ dircheck >> 16 ^ dircheck >> 24;
}
static int adfs_fplus_read(struct super_block *sb, u32 indaddr,
unsigned int size, struct adfs_dir *dir)
{
struct adfs_bigdirheader *h;
struct adfs_bigdirtail *t;
unsigned int dirsize;
int ret;
/* Read first buffer */
ret = adfs_dir_read_buffers(sb, indaddr, sb->s_blocksize, dir);
if (ret)
return ret;
dir->bighead = h = (void *)dir->bhs[0]->b_data;
ret = adfs_fplus_validate_header(h);
if (ret) {
adfs_error(sb, "dir %06x has malformed header", indaddr);
goto out; goto out;
} }
size >>= sb->s_blocksize_bits; dirsize = le32_to_cpu(h->bigdirsize);
if (size > ARRAY_SIZE(dir->bh)) { if (size && dirsize != size) {
/* this directory is too big for fixed bh set, must allocate */ adfs_msg(sb, KERN_WARNING,
struct buffer_head **bh_fplus = "dir %06x header size %X does not match directory size %X",
kcalloc(size, sizeof(struct buffer_head *), indaddr, dirsize, size);
GFP_KERNEL);
if (!bh_fplus) {
adfs_msg(sb, KERN_ERR,
"not enough memory for dir object %X (%d blocks)",
id, size);
ret = -ENOMEM;
goto out;
}
dir->bh_fplus = bh_fplus;
/* copy over the pointer to the block that we've already read */
dir->bh_fplus[0] = dir->bh[0];
} }
for (blk = 1; blk < size; blk++) { /* Read remaining buffers */
block = __adfs_block_map(sb, id, blk); ret = adfs_dir_read_buffers(sb, indaddr, dirsize, dir);
if (!block) { if (ret)
adfs_error(sb, "dir object %X has a hole at offset %d", id, blk); return ret;
goto out;
}
dir->bh_fplus[blk] = sb_bread(sb, block); dir->bigtail = t = (struct adfs_bigdirtail *)
if (!dir->bh_fplus[blk]) { (dir->bhs[dir->nr_buffers - 1]->b_data + (sb->s_blocksize - 8));
adfs_error(sb, "dir object %x failed read for offset %d, mapped block %lX",
id, blk, block);
goto out;
}
dir->nr_buffers += 1; ret = adfs_fplus_validate_tail(h, t);
if (ret) {
adfs_error(sb, "dir %06x has malformed tail", indaddr);
goto out;
} }
t = (struct adfs_bigdirtail *) if (adfs_fplus_checkbyte(dir) != t->bigdircheckbyte) {
(dir->bh_fplus[size - 1]->b_data + (sb->s_blocksize - 8)); adfs_error(sb, "dir %06x checkbyte mismatch\n", indaddr);
if (t->bigdirendname != cpu_to_le32(BIGDIRENDNAME) ||
t->bigdirendmasseq != h->startmasseq ||
t->reserved[0] != 0 || t->reserved[1] != 0) {
adfs_error(sb, "dir %06x has malformed tail", id);
goto out; goto out;
} }
dir->parent_id = le32_to_cpu(h->bigdirparent); dir->parent_id = le32_to_cpu(h->bigdirparent);
dir->sb = sb;
return 0; return 0;
out: out:
if (dir->bh_fplus) { adfs_dir_relse(dir);
for (i = 0; i < dir->nr_buffers; i++)
brelse(dir->bh_fplus[i]);
if (&dir->bh[0] != dir->bh_fplus)
kfree(dir->bh_fplus);
dir->bh_fplus = NULL;
}
dir->nr_buffers = 0;
dir->sb = NULL;
return ret; return ret;
} }
static int static int
adfs_fplus_setpos(struct adfs_dir *dir, unsigned int fpos) adfs_fplus_setpos(struct adfs_dir *dir, unsigned int fpos)
{ {
struct adfs_bigdirheader *h =
(struct adfs_bigdirheader *) dir->bh_fplus[0]->b_data;
int ret = -ENOENT; int ret = -ENOENT;
if (fpos <= le32_to_cpu(h->bigdirentries)) { if (fpos <= le32_to_cpu(dir->bighead->bigdirentries)) {
dir->pos = fpos; dir->pos = fpos;
ret = 0; ret = 0;
} }
...@@ -128,51 +168,23 @@ adfs_fplus_setpos(struct adfs_dir *dir, unsigned int fpos) ...@@ -128,51 +168,23 @@ adfs_fplus_setpos(struct adfs_dir *dir, unsigned int fpos)
return ret; return ret;
} }
static void
dir_memcpy(struct adfs_dir *dir, unsigned int offset, void *to, int len)
{
struct super_block *sb = dir->sb;
unsigned int buffer, partial, remainder;
buffer = offset >> sb->s_blocksize_bits;
offset &= sb->s_blocksize - 1;
partial = sb->s_blocksize - offset;
if (partial >= len)
memcpy(to, dir->bh_fplus[buffer]->b_data + offset, len);
else {
char *c = (char *)to;
remainder = len - partial;
memcpy(c,
dir->bh_fplus[buffer]->b_data + offset,
partial);
memcpy(c + partial,
dir->bh_fplus[buffer + 1]->b_data,
remainder);
}
}
static int static int
adfs_fplus_getnext(struct adfs_dir *dir, struct object_info *obj) adfs_fplus_getnext(struct adfs_dir *dir, struct object_info *obj)
{ {
struct adfs_bigdirheader *h = struct adfs_bigdirheader *h = dir->bighead;
(struct adfs_bigdirheader *) dir->bh_fplus[0]->b_data;
struct adfs_bigdirentry bde; struct adfs_bigdirentry bde;
unsigned int offset; unsigned int offset;
int ret = -ENOENT; int ret;
if (dir->pos >= le32_to_cpu(h->bigdirentries)) if (dir->pos >= le32_to_cpu(h->bigdirentries))
goto out; return -ENOENT;
offset = offsetof(struct adfs_bigdirheader, bigdirname); offset = adfs_fplus_offset(h, dir->pos);
offset += ((le32_to_cpu(h->bigdirnamelen) + 4) & ~3);
offset += dir->pos * sizeof(struct adfs_bigdirentry);
dir_memcpy(dir, offset, &bde, sizeof(struct adfs_bigdirentry)); ret = adfs_dir_copyfrom(&bde, dir, offset,
sizeof(struct adfs_bigdirentry));
if (ret)
return ret;
obj->loadaddr = le32_to_cpu(bde.bigdirload); obj->loadaddr = le32_to_cpu(bde.bigdirload);
obj->execaddr = le32_to_cpu(bde.bigdirexec); obj->execaddr = le32_to_cpu(bde.bigdirexec);
...@@ -181,59 +193,95 @@ adfs_fplus_getnext(struct adfs_dir *dir, struct object_info *obj) ...@@ -181,59 +193,95 @@ adfs_fplus_getnext(struct adfs_dir *dir, struct object_info *obj)
obj->attr = le32_to_cpu(bde.bigdirattr); obj->attr = le32_to_cpu(bde.bigdirattr);
obj->name_len = le32_to_cpu(bde.bigdirobnamelen); obj->name_len = le32_to_cpu(bde.bigdirobnamelen);
offset = offsetof(struct adfs_bigdirheader, bigdirname); offset = adfs_fplus_offset(h, le32_to_cpu(h->bigdirentries));
offset += ((le32_to_cpu(h->bigdirnamelen) + 4) & ~3);
offset += le32_to_cpu(h->bigdirentries) * sizeof(struct adfs_bigdirentry);
offset += le32_to_cpu(bde.bigdirobnameptr); offset += le32_to_cpu(bde.bigdirobnameptr);
dir_memcpy(dir, offset, obj->name, obj->name_len); ret = adfs_dir_copyfrom(obj->name, dir, offset, obj->name_len);
if (ret)
return ret;
adfs_object_fixup(dir, obj); adfs_object_fixup(dir, obj);
dir->pos += 1; dir->pos += 1;
ret = 0;
out: return 0;
return ret;
} }
static int static int adfs_fplus_iterate(struct adfs_dir *dir, struct dir_context *ctx)
adfs_fplus_sync(struct adfs_dir *dir)
{ {
int err = 0; struct object_info obj;
int i;
if ((ctx->pos - 2) >> 32)
for (i = dir->nr_buffers - 1; i >= 0; i--) { return 0;
struct buffer_head *bh = dir->bh_fplus[i];
sync_dirty_buffer(bh); if (adfs_fplus_setpos(dir, ctx->pos - 2))
if (buffer_req(bh) && !buffer_uptodate(bh)) return 0;
err = -EIO;
while (!adfs_fplus_getnext(dir, &obj)) {
if (!dir_emit(ctx, obj.name, obj.name_len,
obj.indaddr, DT_UNKNOWN))
break;
ctx->pos++;
} }
return err; return 0;
} }
static void static int adfs_fplus_update(struct adfs_dir *dir, struct object_info *obj)
adfs_fplus_free(struct adfs_dir *dir)
{ {
int i; struct adfs_bigdirheader *h = dir->bighead;
struct adfs_bigdirentry bde;
int offset, end, ret;
if (dir->bh_fplus) { offset = adfs_fplus_offset(h, 0) - sizeof(bde);
for (i = 0; i < dir->nr_buffers; i++) end = adfs_fplus_offset(h, le32_to_cpu(h->bigdirentries));
brelse(dir->bh_fplus[i]);
if (&dir->bh[0] != dir->bh_fplus) do {
kfree(dir->bh_fplus); offset += sizeof(bde);
if (offset >= end) {
adfs_error(dir->sb, "unable to locate entry to update");
return -ENOENT;
}
ret = adfs_dir_copyfrom(&bde, dir, offset, sizeof(bde));
if (ret) {
adfs_error(dir->sb, "error reading directory entry");
return -ENOENT;
}
} while (le32_to_cpu(bde.bigdirindaddr) != obj->indaddr);
dir->bh_fplus = NULL; bde.bigdirload = cpu_to_le32(obj->loadaddr);
} bde.bigdirexec = cpu_to_le32(obj->execaddr);
bde.bigdirlen = cpu_to_le32(obj->size);
bde.bigdirindaddr = cpu_to_le32(obj->indaddr);
bde.bigdirattr = cpu_to_le32(obj->attr);
return adfs_dir_copyto(dir, offset, &bde, sizeof(bde));
}
static int adfs_fplus_commit(struct adfs_dir *dir)
{
int ret;
dir->nr_buffers = 0; /* Increment directory sequence number */
dir->sb = NULL; dir->bighead->startmasseq += 1;
dir->bigtail->bigdirendmasseq += 1;
/* Update directory check byte */
dir->bigtail->bigdircheckbyte = adfs_fplus_checkbyte(dir);
/* Make sure the directory still validates correctly */
ret = adfs_fplus_validate_header(dir->bighead);
if (ret == 0)
ret = adfs_fplus_validate_tail(dir->bighead, dir->bigtail);
return ret;
} }
const struct adfs_dir_ops adfs_fplus_dir_ops = { const struct adfs_dir_ops adfs_fplus_dir_ops = {
.read = adfs_fplus_read, .read = adfs_fplus_read,
.iterate = adfs_fplus_iterate,
.setpos = adfs_fplus_setpos, .setpos = adfs_fplus_setpos,
.getnext = adfs_fplus_getnext, .getnext = adfs_fplus_getnext,
.sync = adfs_fplus_sync, .update = adfs_fplus_update,
.free = adfs_fplus_free .commit = adfs_fplus_commit,
}; };
...@@ -22,7 +22,7 @@ struct adfs_bigdirheader { ...@@ -22,7 +22,7 @@ struct adfs_bigdirheader {
__le32 bigdirnamesize; __le32 bigdirnamesize;
__le32 bigdirparent; __le32 bigdirparent;
char bigdirname[1]; char bigdirname[1];
}; } __attribute__((packed, aligned(4)));
struct adfs_bigdirentry { struct adfs_bigdirentry {
__le32 bigdirload; __le32 bigdirload;
...@@ -32,11 +32,11 @@ struct adfs_bigdirentry { ...@@ -32,11 +32,11 @@ struct adfs_bigdirentry {
__le32 bigdirattr; __le32 bigdirattr;
__le32 bigdirobnamelen; __le32 bigdirobnamelen;
__le32 bigdirobnameptr; __le32 bigdirobnameptr;
}; } __attribute__((packed, aligned(4)));
struct adfs_bigdirtail { struct adfs_bigdirtail {
__le32 bigdirendname; __le32 bigdirendname;
__u8 bigdirendmasseq; __u8 bigdirendmasseq;
__u8 reserved[2]; __u8 reserved[2];
__u8 bigdircheckbyte; __u8 bigdircheckbyte;
}; } __attribute__((packed, aligned(4)));
...@@ -20,7 +20,8 @@ adfs_get_block(struct inode *inode, sector_t block, struct buffer_head *bh, ...@@ -20,7 +20,8 @@ adfs_get_block(struct inode *inode, sector_t block, struct buffer_head *bh,
if (block >= inode->i_blocks) if (block >= inode->i_blocks)
goto abort_toobig; goto abort_toobig;
block = __adfs_block_map(inode->i_sb, inode->i_ino, block); block = __adfs_block_map(inode->i_sb, ADFS_I(inode)->indaddr,
block);
if (block) if (block)
map_bh(bh, inode->i_sb, block); map_bh(bh, inode->i_sb, block);
return 0; return 0;
...@@ -126,29 +127,29 @@ adfs_atts2mode(struct super_block *sb, struct inode *inode) ...@@ -126,29 +127,29 @@ adfs_atts2mode(struct super_block *sb, struct inode *inode)
* Convert Linux permission to ADFS attribute. We try to do the reverse * Convert Linux permission to ADFS attribute. We try to do the reverse
* of atts2mode, but there is not a 1:1 translation. * of atts2mode, but there is not a 1:1 translation.
*/ */
static int static int adfs_mode2atts(struct super_block *sb, struct inode *inode,
adfs_mode2atts(struct super_block *sb, struct inode *inode) umode_t ia_mode)
{ {
struct adfs_sb_info *asb = ADFS_SB(sb);
umode_t mode; umode_t mode;
int attr; int attr;
struct adfs_sb_info *asb = ADFS_SB(sb);
/* FIXME: should we be able to alter a link? */ /* FIXME: should we be able to alter a link? */
if (S_ISLNK(inode->i_mode)) if (S_ISLNK(inode->i_mode))
return ADFS_I(inode)->attr; return ADFS_I(inode)->attr;
/* Directories do not have read/write permissions on the media */
if (S_ISDIR(inode->i_mode)) if (S_ISDIR(inode->i_mode))
attr = ADFS_NDA_DIRECTORY; return ADFS_NDA_DIRECTORY;
else
attr = 0;
mode = inode->i_mode & asb->s_owner_mask; attr = 0;
mode = ia_mode & asb->s_owner_mask;
if (mode & S_IRUGO) if (mode & S_IRUGO)
attr |= ADFS_NDA_OWNER_READ; attr |= ADFS_NDA_OWNER_READ;
if (mode & S_IWUGO) if (mode & S_IWUGO)
attr |= ADFS_NDA_OWNER_WRITE; attr |= ADFS_NDA_OWNER_WRITE;
mode = inode->i_mode & asb->s_other_mask; mode = ia_mode & asb->s_other_mask;
mode &= ~asb->s_owner_mask; mode &= ~asb->s_owner_mask;
if (mode & S_IRUGO) if (mode & S_IRUGO)
attr |= ADFS_NDA_PUBLIC_READ; attr |= ADFS_NDA_PUBLIC_READ;
...@@ -158,6 +159,8 @@ adfs_mode2atts(struct super_block *sb, struct inode *inode) ...@@ -158,6 +159,8 @@ adfs_mode2atts(struct super_block *sb, struct inode *inode)
return attr; return attr;
} }
static const s64 nsec_unix_epoch_diff_risc_os_epoch = 2208988800000000000LL;
/* /*
* Convert an ADFS time to Unix time. ADFS has a 40-bit centi-second time * Convert an ADFS time to Unix time. ADFS has a 40-bit centi-second time
* referenced to 1 Jan 1900 (til 2248) so we need to discard 2208988800 seconds * referenced to 1 Jan 1900 (til 2248) so we need to discard 2208988800 seconds
...@@ -170,8 +173,6 @@ adfs_adfs2unix_time(struct timespec64 *tv, struct inode *inode) ...@@ -170,8 +173,6 @@ adfs_adfs2unix_time(struct timespec64 *tv, struct inode *inode)
/* 01 Jan 1970 00:00:00 (Unix epoch) as nanoseconds since /* 01 Jan 1970 00:00:00 (Unix epoch) as nanoseconds since
* 01 Jan 1900 00:00:00 (RISC OS epoch) * 01 Jan 1900 00:00:00 (RISC OS epoch)
*/ */
static const s64 nsec_unix_epoch_diff_risc_os_epoch =
2208988800000000000LL;
s64 nsec; s64 nsec;
if (!adfs_inode_is_stamped(inode)) if (!adfs_inode_is_stamped(inode))
...@@ -204,24 +205,23 @@ adfs_adfs2unix_time(struct timespec64 *tv, struct inode *inode) ...@@ -204,24 +205,23 @@ adfs_adfs2unix_time(struct timespec64 *tv, struct inode *inode)
return; return;
} }
/* /* Convert an Unix time to ADFS time for an entry that is already stamped. */
* Convert an Unix time to ADFS time. We only do this if the entry has a static void adfs_unix2adfs_time(struct inode *inode,
* time/date stamp already. const struct timespec64 *ts)
*/
static void
adfs_unix2adfs_time(struct inode *inode, unsigned int secs)
{ {
unsigned int high, low; s64 cs, nsec = timespec64_to_ns(ts);
if (adfs_inode_is_stamped(inode)) { /* convert from Unix to RISC OS epoch */
/* convert 32-bit seconds to 40-bit centi-seconds */ nsec += nsec_unix_epoch_diff_risc_os_epoch;
low = (secs & 255) * 100;
high = (secs / 256) * 100 + (low >> 8) + 0x336e996a;
ADFS_I(inode)->loadaddr = (high >> 24) | /* convert from nanoseconds to centiseconds */
(ADFS_I(inode)->loadaddr & ~0xff); cs = div_s64(nsec, 10000000);
ADFS_I(inode)->execaddr = (low & 255) | (high << 8);
} cs = clamp_t(s64, cs, 0, 0xffffffffff);
ADFS_I(inode)->loadaddr &= ~0xff;
ADFS_I(inode)->loadaddr |= (cs >> 32) & 0xff;
ADFS_I(inode)->execaddr = cs;
} }
/* /*
...@@ -260,6 +260,7 @@ adfs_iget(struct super_block *sb, struct object_info *obj) ...@@ -260,6 +260,7 @@ adfs_iget(struct super_block *sb, struct object_info *obj)
* for cross-directory renames. * for cross-directory renames.
*/ */
ADFS_I(inode)->parent_id = obj->parent_id; ADFS_I(inode)->parent_id = obj->parent_id;
ADFS_I(inode)->indaddr = obj->indaddr;
ADFS_I(inode)->loadaddr = obj->loadaddr; ADFS_I(inode)->loadaddr = obj->loadaddr;
ADFS_I(inode)->execaddr = obj->execaddr; ADFS_I(inode)->execaddr = obj->execaddr;
ADFS_I(inode)->attr = obj->attr; ADFS_I(inode)->attr = obj->attr;
...@@ -315,10 +316,11 @@ adfs_notify_change(struct dentry *dentry, struct iattr *attr) ...@@ -315,10 +316,11 @@ adfs_notify_change(struct dentry *dentry, struct iattr *attr)
if (ia_valid & ATTR_SIZE) if (ia_valid & ATTR_SIZE)
truncate_setsize(inode, attr->ia_size); truncate_setsize(inode, attr->ia_size);
if (ia_valid & ATTR_MTIME) { if (ia_valid & ATTR_MTIME && adfs_inode_is_stamped(inode)) {
inode->i_mtime = attr->ia_mtime; adfs_unix2adfs_time(inode, &attr->ia_mtime);
adfs_unix2adfs_time(inode, attr->ia_mtime.tv_sec); adfs_adfs2unix_time(&inode->i_mtime, inode);
} }
/* /*
* FIXME: should we make these == to i_mtime since we don't * FIXME: should we make these == to i_mtime since we don't
* have the ability to represent them in our filesystem? * have the ability to represent them in our filesystem?
...@@ -328,7 +330,7 @@ adfs_notify_change(struct dentry *dentry, struct iattr *attr) ...@@ -328,7 +330,7 @@ adfs_notify_change(struct dentry *dentry, struct iattr *attr)
if (ia_valid & ATTR_CTIME) if (ia_valid & ATTR_CTIME)
inode->i_ctime = attr->ia_ctime; inode->i_ctime = attr->ia_ctime;
if (ia_valid & ATTR_MODE) { if (ia_valid & ATTR_MODE) {
ADFS_I(inode)->attr = adfs_mode2atts(sb, inode); ADFS_I(inode)->attr = adfs_mode2atts(sb, inode, attr->ia_mode);
inode->i_mode = adfs_atts2mode(sb, inode); inode->i_mode = adfs_atts2mode(sb, inode);
} }
...@@ -353,7 +355,7 @@ int adfs_write_inode(struct inode *inode, struct writeback_control *wbc) ...@@ -353,7 +355,7 @@ int adfs_write_inode(struct inode *inode, struct writeback_control *wbc)
struct object_info obj; struct object_info obj;
int ret; int ret;
obj.indaddr = inode->i_ino; obj.indaddr = ADFS_I(inode)->indaddr;
obj.name_len = 0; obj.name_len = 0;
obj.parent_id = ADFS_I(inode)->parent_id; obj.parent_id = ADFS_I(inode)->parent_id;
obj.loadaddr = ADFS_I(inode)->loadaddr; obj.loadaddr = ADFS_I(inode)->loadaddr;
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
* *
* Copyright (C) 1997-2002 Russell King * Copyright (C) 1997-2002 Russell King
*/ */
#include <linux/slab.h>
#include <linux/statfs.h>
#include <asm/unaligned.h> #include <asm/unaligned.h>
#include "adfs.h" #include "adfs.h"
...@@ -66,54 +68,41 @@ static DEFINE_RWLOCK(adfs_map_lock); ...@@ -66,54 +68,41 @@ static DEFINE_RWLOCK(adfs_map_lock);
static int lookup_zone(const struct adfs_discmap *dm, const unsigned int idlen, static int lookup_zone(const struct adfs_discmap *dm, const unsigned int idlen,
const u32 frag_id, unsigned int *offset) const u32 frag_id, unsigned int *offset)
{ {
const unsigned int mapsize = dm->dm_endbit; const unsigned int endbit = dm->dm_endbit;
const u32 idmask = (1 << idlen) - 1; const u32 idmask = (1 << idlen) - 1;
unsigned char *map = dm->dm_bh->b_data + 4; unsigned char *map = dm->dm_bh->b_data;
unsigned int start = dm->dm_startbit; unsigned int start = dm->dm_startbit;
unsigned int mapptr; unsigned int freelink, fragend;
u32 frag; u32 frag;
frag = GET_FRAG_ID(map, 8, idmask & 0x7fff);
freelink = frag ? 8 + frag : 0;
do { do {
frag = GET_FRAG_ID(map, start, idmask); frag = GET_FRAG_ID(map, start, idmask);
mapptr = start + idlen;
fragend = find_next_bit_le(map, endbit, start + idlen);
/* if (fragend >= endbit)
* find end of fragment goto error;
*/
{ if (start == freelink) {
__le32 *_map = (__le32 *)map; freelink += frag & 0x7fff;
u32 v = le32_to_cpu(_map[mapptr >> 5]) >> (mapptr & 31); } else if (frag == frag_id) {
while (v == 0) { unsigned int length = fragend + 1 - start;
mapptr = (mapptr & ~31) + 32;
if (mapptr >= mapsize) if (*offset < length)
goto error; return start + *offset;
v = le32_to_cpu(_map[mapptr >> 5]); *offset -= length;
}
mapptr += 1 + ffz(~v);
} }
if (frag == frag_id) start = fragend + 1;
goto found; } while (start < endbit);
again:
start = mapptr;
} while (mapptr < mapsize);
return -1; return -1;
error: error:
printk(KERN_ERR "adfs: oversized fragment 0x%x at 0x%x-0x%x\n", printk(KERN_ERR "adfs: oversized fragment 0x%x at 0x%x-0x%x\n",
frag, start, mapptr); frag, start, fragend);
return -1; return -1;
found:
{
int length = mapptr - start;
if (*offset >= length) {
*offset -= length;
goto again;
}
}
return start + *offset;
} }
/* /*
...@@ -125,12 +114,12 @@ static int lookup_zone(const struct adfs_discmap *dm, const unsigned int idlen, ...@@ -125,12 +114,12 @@ static int lookup_zone(const struct adfs_discmap *dm, const unsigned int idlen,
static unsigned int static unsigned int
scan_free_map(struct adfs_sb_info *asb, struct adfs_discmap *dm) scan_free_map(struct adfs_sb_info *asb, struct adfs_discmap *dm)
{ {
const unsigned int mapsize = dm->dm_endbit + 32; const unsigned int endbit = dm->dm_endbit;
const unsigned int idlen = asb->s_idlen; const unsigned int idlen = asb->s_idlen;
const unsigned int frag_idlen = idlen <= 15 ? idlen : 15; const unsigned int frag_idlen = idlen <= 15 ? idlen : 15;
const u32 idmask = (1 << frag_idlen) - 1; const u32 idmask = (1 << frag_idlen) - 1;
unsigned char *map = dm->dm_bh->b_data; unsigned char *map = dm->dm_bh->b_data;
unsigned int start = 8, mapptr; unsigned int start = 8, fragend;
u32 frag; u32 frag;
unsigned long total = 0; unsigned long total = 0;
...@@ -149,29 +138,13 @@ scan_free_map(struct adfs_sb_info *asb, struct adfs_discmap *dm) ...@@ -149,29 +138,13 @@ scan_free_map(struct adfs_sb_info *asb, struct adfs_discmap *dm)
do { do {
start += frag; start += frag;
/*
* get fragment id
*/
frag = GET_FRAG_ID(map, start, idmask); frag = GET_FRAG_ID(map, start, idmask);
mapptr = start + idlen;
/*
* find end of fragment
*/
{
__le32 *_map = (__le32 *)map;
u32 v = le32_to_cpu(_map[mapptr >> 5]) >> (mapptr & 31);
while (v == 0) {
mapptr = (mapptr & ~31) + 32;
if (mapptr >= mapsize)
goto error;
v = le32_to_cpu(_map[mapptr >> 5]);
}
mapptr += 1 + ffz(~v);
}
total += mapptr - start; fragend = find_next_bit_le(map, endbit, start + idlen);
if (fragend >= endbit)
goto error;
total += fragend + 1 - start;
} while (frag >= idlen + 1); } while (frag >= idlen + 1);
if (frag != 0) if (frag != 0)
...@@ -220,10 +193,10 @@ static int scan_map(struct adfs_sb_info *asb, unsigned int zone, ...@@ -220,10 +193,10 @@ static int scan_map(struct adfs_sb_info *asb, unsigned int zone,
* total_free = E(free_in_zone_n) * total_free = E(free_in_zone_n)
* nzones * nzones
*/ */
unsigned int void adfs_map_statfs(struct super_block *sb, struct kstatfs *buf)
adfs_map_free(struct super_block *sb)
{ {
struct adfs_sb_info *asb = ADFS_SB(sb); struct adfs_sb_info *asb = ADFS_SB(sb);
struct adfs_discrecord *dr = adfs_map_discrecord(asb->s_map);
struct adfs_discmap *dm; struct adfs_discmap *dm;
unsigned int total = 0; unsigned int total = 0;
unsigned int zone; unsigned int zone;
...@@ -235,7 +208,10 @@ adfs_map_free(struct super_block *sb) ...@@ -235,7 +208,10 @@ adfs_map_free(struct super_block *sb)
total += scan_free_map(asb, dm++); total += scan_free_map(asb, dm++);
} while (--zone > 0); } while (--zone > 0);
return signed_asl(total, asb->s_map2blk); buf->f_blocks = adfs_disc_size(dr) >> sb->s_blocksize_bits;
buf->f_files = asb->s_ids_per_zone * asb->s_map_size;
buf->f_bavail =
buf->f_bfree = signed_asl(total, asb->s_map2blk);
} }
int adfs_map_lookup(struct super_block *sb, u32 frag_id, unsigned int offset) int adfs_map_lookup(struct super_block *sb, u32 frag_id, unsigned int offset)
...@@ -280,3 +256,152 @@ int adfs_map_lookup(struct super_block *sb, u32 frag_id, unsigned int offset) ...@@ -280,3 +256,152 @@ int adfs_map_lookup(struct super_block *sb, u32 frag_id, unsigned int offset)
frag_id, zone, asb->s_map_size); frag_id, zone, asb->s_map_size);
return 0; return 0;
} }
static unsigned char adfs_calczonecheck(struct super_block *sb, unsigned char *map)
{
unsigned int v0, v1, v2, v3;
int i;
v0 = v1 = v2 = v3 = 0;
for (i = sb->s_blocksize - 4; i; i -= 4) {
v0 += map[i] + (v3 >> 8);
v3 &= 0xff;
v1 += map[i + 1] + (v0 >> 8);
v0 &= 0xff;
v2 += map[i + 2] + (v1 >> 8);
v1 &= 0xff;
v3 += map[i + 3] + (v2 >> 8);
v2 &= 0xff;
}
v0 += v3 >> 8;
v1 += map[1] + (v0 >> 8);
v2 += map[2] + (v1 >> 8);
v3 += map[3] + (v2 >> 8);
return v0 ^ v1 ^ v2 ^ v3;
}
static int adfs_checkmap(struct super_block *sb, struct adfs_discmap *dm)
{
unsigned char crosscheck = 0, zonecheck = 1;
int i;
for (i = 0; i < ADFS_SB(sb)->s_map_size; i++) {
unsigned char *map;
map = dm[i].dm_bh->b_data;
if (adfs_calczonecheck(sb, map) != map[0]) {
adfs_error(sb, "zone %d fails zonecheck", i);
zonecheck = 0;
}
crosscheck ^= map[3];
}
if (crosscheck != 0xff)
adfs_error(sb, "crosscheck != 0xff");
return crosscheck == 0xff && zonecheck;
}
/*
* Layout the map - the first zone contains a copy of the disc record,
* and the last zone must be limited to the size of the filesystem.
*/
static void adfs_map_layout(struct adfs_discmap *dm, unsigned int nzones,
struct adfs_discrecord *dr)
{
unsigned int zone, zone_size;
u64 size;
zone_size = (8 << dr->log2secsize) - le16_to_cpu(dr->zone_spare);
dm[0].dm_bh = NULL;
dm[0].dm_startblk = 0;
dm[0].dm_startbit = 32 + ADFS_DR_SIZE_BITS;
dm[0].dm_endbit = 32 + zone_size;
for (zone = 1; zone < nzones; zone++) {
dm[zone].dm_bh = NULL;
dm[zone].dm_startblk = zone * zone_size - ADFS_DR_SIZE_BITS;
dm[zone].dm_startbit = 32;
dm[zone].dm_endbit = 32 + zone_size;
}
size = adfs_disc_size(dr) >> dr->log2bpmb;
size -= (nzones - 1) * zone_size - ADFS_DR_SIZE_BITS;
dm[nzones - 1].dm_endbit = 32 + size;
}
static int adfs_map_read(struct adfs_discmap *dm, struct super_block *sb,
unsigned int map_addr, unsigned int nzones)
{
unsigned int zone;
for (zone = 0; zone < nzones; zone++) {
dm[zone].dm_bh = sb_bread(sb, map_addr + zone);
if (!dm[zone].dm_bh)
return -EIO;
}
return 0;
}
static void adfs_map_relse(struct adfs_discmap *dm, unsigned int nzones)
{
unsigned int zone;
for (zone = 0; zone < nzones; zone++)
brelse(dm[zone].dm_bh);
}
struct adfs_discmap *adfs_read_map(struct super_block *sb, struct adfs_discrecord *dr)
{
struct adfs_sb_info *asb = ADFS_SB(sb);
struct adfs_discmap *dm;
unsigned int map_addr, zone_size, nzones;
int ret;
nzones = dr->nzones | dr->nzones_high << 8;
zone_size = (8 << dr->log2secsize) - le16_to_cpu(dr->zone_spare);
asb->s_idlen = dr->idlen;
asb->s_map_size = nzones;
asb->s_map2blk = dr->log2bpmb - dr->log2secsize;
asb->s_log2sharesize = dr->log2sharesize;
asb->s_ids_per_zone = zone_size / (asb->s_idlen + 1);
map_addr = (nzones >> 1) * zone_size -
((nzones > 1) ? ADFS_DR_SIZE_BITS : 0);
map_addr = signed_asl(map_addr, asb->s_map2blk);
dm = kmalloc_array(nzones, sizeof(*dm), GFP_KERNEL);
if (dm == NULL) {
adfs_error(sb, "not enough memory");
return ERR_PTR(-ENOMEM);
}
adfs_map_layout(dm, nzones, dr);
ret = adfs_map_read(dm, sb, map_addr, nzones);
if (ret) {
adfs_error(sb, "unable to read map");
goto error_free;
}
if (adfs_checkmap(sb, dm))
return dm;
adfs_error(sb, "map corrupted");
error_free:
adfs_map_relse(dm, nzones);
kfree(dm);
return ERR_PTR(-EIO);
}
void adfs_free_map(struct super_block *sb)
{
struct adfs_sb_info *asb = ADFS_SB(sb);
adfs_map_relse(asb->s_map, asb->s_map_size);
kfree(asb->s_map);
}
...@@ -88,59 +88,11 @@ static int adfs_checkdiscrecord(struct adfs_discrecord *dr) ...@@ -88,59 +88,11 @@ static int adfs_checkdiscrecord(struct adfs_discrecord *dr)
return 0; return 0;
} }
static unsigned char adfs_calczonecheck(struct super_block *sb, unsigned char *map)
{
unsigned int v0, v1, v2, v3;
int i;
v0 = v1 = v2 = v3 = 0;
for (i = sb->s_blocksize - 4; i; i -= 4) {
v0 += map[i] + (v3 >> 8);
v3 &= 0xff;
v1 += map[i + 1] + (v0 >> 8);
v0 &= 0xff;
v2 += map[i + 2] + (v1 >> 8);
v1 &= 0xff;
v3 += map[i + 3] + (v2 >> 8);
v2 &= 0xff;
}
v0 += v3 >> 8;
v1 += map[1] + (v0 >> 8);
v2 += map[2] + (v1 >> 8);
v3 += map[3] + (v2 >> 8);
return v0 ^ v1 ^ v2 ^ v3;
}
static int adfs_checkmap(struct super_block *sb, struct adfs_discmap *dm)
{
unsigned char crosscheck = 0, zonecheck = 1;
int i;
for (i = 0; i < ADFS_SB(sb)->s_map_size; i++) {
unsigned char *map;
map = dm[i].dm_bh->b_data;
if (adfs_calczonecheck(sb, map) != map[0]) {
adfs_error(sb, "zone %d fails zonecheck", i);
zonecheck = 0;
}
crosscheck ^= map[3];
}
if (crosscheck != 0xff)
adfs_error(sb, "crosscheck != 0xff");
return crosscheck == 0xff && zonecheck;
}
static void adfs_put_super(struct super_block *sb) static void adfs_put_super(struct super_block *sb)
{ {
int i;
struct adfs_sb_info *asb = ADFS_SB(sb); struct adfs_sb_info *asb = ADFS_SB(sb);
for (i = 0; i < asb->s_map_size; i++) adfs_free_map(sb);
brelse(asb->s_map[i].dm_bh);
kfree(asb->s_map);
kfree_rcu(asb, rcu); kfree_rcu(asb, rcu);
} }
...@@ -249,16 +201,13 @@ static int adfs_statfs(struct dentry *dentry, struct kstatfs *buf) ...@@ -249,16 +201,13 @@ static int adfs_statfs(struct dentry *dentry, struct kstatfs *buf)
{ {
struct super_block *sb = dentry->d_sb; struct super_block *sb = dentry->d_sb;
struct adfs_sb_info *sbi = ADFS_SB(sb); struct adfs_sb_info *sbi = ADFS_SB(sb);
struct adfs_discrecord *dr = adfs_map_discrecord(sbi->s_map);
u64 id = huge_encode_dev(sb->s_bdev->bd_dev); u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
adfs_map_statfs(sb, buf);
buf->f_type = ADFS_SUPER_MAGIC; buf->f_type = ADFS_SUPER_MAGIC;
buf->f_namelen = sbi->s_namelen; buf->f_namelen = sbi->s_namelen;
buf->f_bsize = sb->s_blocksize; buf->f_bsize = sb->s_blocksize;
buf->f_blocks = adfs_disc_size(dr) >> sb->s_blocksize_bits;
buf->f_files = sbi->s_ids_per_zone * sbi->s_map_size;
buf->f_bavail =
buf->f_bfree = adfs_map_free(sb);
buf->f_ffree = (long)(buf->f_bfree * buf->f_files) / (long)buf->f_blocks; buf->f_ffree = (long)(buf->f_bfree * buf->f_files) / (long)buf->f_blocks;
buf->f_fsid.val[0] = (u32)id; buf->f_fsid.val[0] = (u32)id;
buf->f_fsid.val[1] = (u32)(id >> 32); buf->f_fsid.val[1] = (u32)(id >> 32);
...@@ -282,6 +231,12 @@ static void adfs_free_inode(struct inode *inode) ...@@ -282,6 +231,12 @@ static void adfs_free_inode(struct inode *inode)
kmem_cache_free(adfs_inode_cachep, ADFS_I(inode)); kmem_cache_free(adfs_inode_cachep, ADFS_I(inode));
} }
static int adfs_drop_inode(struct inode *inode)
{
/* always drop inodes if we are read-only */
return !IS_ENABLED(CONFIG_ADFS_FS_RW) || IS_RDONLY(inode);
}
static void init_once(void *foo) static void init_once(void *foo)
{ {
struct adfs_inode_info *ei = (struct adfs_inode_info *) foo; struct adfs_inode_info *ei = (struct adfs_inode_info *) foo;
...@@ -314,7 +269,7 @@ static void destroy_inodecache(void) ...@@ -314,7 +269,7 @@ static void destroy_inodecache(void)
static const struct super_operations adfs_sops = { static const struct super_operations adfs_sops = {
.alloc_inode = adfs_alloc_inode, .alloc_inode = adfs_alloc_inode,
.free_inode = adfs_free_inode, .free_inode = adfs_free_inode,
.drop_inode = generic_delete_inode, .drop_inode = adfs_drop_inode,
.write_inode = adfs_write_inode, .write_inode = adfs_write_inode,
.put_super = adfs_put_super, .put_super = adfs_put_super,
.statfs = adfs_statfs, .statfs = adfs_statfs,
...@@ -322,66 +277,94 @@ static const struct super_operations adfs_sops = { ...@@ -322,66 +277,94 @@ static const struct super_operations adfs_sops = {
.show_options = adfs_show_options, .show_options = adfs_show_options,
}; };
static struct adfs_discmap *adfs_read_map(struct super_block *sb, struct adfs_discrecord *dr) static int adfs_probe(struct super_block *sb, unsigned int offset, int silent,
int (*validate)(struct super_block *sb,
struct buffer_head *bh,
struct adfs_discrecord **bhp))
{ {
struct adfs_discmap *dm;
unsigned int map_addr, zone_size, nzones;
int i, zone;
struct adfs_sb_info *asb = ADFS_SB(sb); struct adfs_sb_info *asb = ADFS_SB(sb);
struct adfs_discrecord *dr;
struct buffer_head *bh;
unsigned int blocksize = BLOCK_SIZE;
int ret, try;
for (try = 0; try < 2; try++) {
/* try to set the requested block size */
if (sb->s_blocksize != blocksize &&
!sb_set_blocksize(sb, blocksize)) {
if (!silent)
adfs_msg(sb, KERN_ERR,
"error: unsupported blocksize");
return -EINVAL;
}
nzones = asb->s_map_size; /* read the buffer */
zone_size = (8 << dr->log2secsize) - le16_to_cpu(dr->zone_spare); bh = sb_bread(sb, offset >> sb->s_blocksize_bits);
map_addr = (nzones >> 1) * zone_size - if (!bh) {
((nzones > 1) ? ADFS_DR_SIZE_BITS : 0); adfs_msg(sb, KERN_ERR,
map_addr = signed_asl(map_addr, asb->s_map2blk); "error: unable to read block %u, try %d",
offset >> sb->s_blocksize_bits, try);
return -EIO;
}
/* validate it */
ret = validate(sb, bh, &dr);
if (ret) {
brelse(bh);
return ret;
}
asb->s_ids_per_zone = zone_size / (asb->s_idlen + 1); /* does the block size match the filesystem block size? */
blocksize = 1 << dr->log2secsize;
if (sb->s_blocksize == blocksize) {
asb->s_map = adfs_read_map(sb, dr);
brelse(bh);
return PTR_ERR_OR_ZERO(asb->s_map);
}
dm = kmalloc_array(nzones, sizeof(*dm), GFP_KERNEL); brelse(bh);
if (dm == NULL) {
adfs_error(sb, "not enough memory");
return ERR_PTR(-ENOMEM);
} }
for (zone = 0; zone < nzones; zone++, map_addr++) { return -EIO;
dm[zone].dm_startbit = 0; }
dm[zone].dm_endbit = zone_size;
dm[zone].dm_startblk = zone * zone_size - ADFS_DR_SIZE_BITS;
dm[zone].dm_bh = sb_bread(sb, map_addr);
if (!dm[zone].dm_bh) { static int adfs_validate_bblk(struct super_block *sb, struct buffer_head *bh,
adfs_error(sb, "unable to read map"); struct adfs_discrecord **drp)
goto error_free; {
} struct adfs_discrecord *dr;
} unsigned char *b_data;
/* adjust the limits for the first and last map zones */ b_data = bh->b_data + (ADFS_DISCRECORD % sb->s_blocksize);
i = zone - 1; if (adfs_checkbblk(b_data))
dm[0].dm_startblk = 0; return -EILSEQ;
dm[0].dm_startbit = ADFS_DR_SIZE_BITS;
dm[i].dm_endbit = (adfs_disc_size(dr) >> dr->log2bpmb) +
(ADFS_DR_SIZE_BITS - i * zone_size);
if (adfs_checkmap(sb, dm)) /* Do some sanity checks on the ADFS disc record */
return dm; dr = (struct adfs_discrecord *)(b_data + ADFS_DR_OFFSET);
if (adfs_checkdiscrecord(dr))
return -EILSEQ;
*drp = dr;
return 0;
}
adfs_error(sb, "map corrupted"); static int adfs_validate_dr0(struct super_block *sb, struct buffer_head *bh,
struct adfs_discrecord **drp)
{
struct adfs_discrecord *dr;
error_free: /* Do some sanity checks on the ADFS disc record */
while (--zone >= 0) dr = (struct adfs_discrecord *)(bh->b_data + 4);
brelse(dm[zone].dm_bh); if (adfs_checkdiscrecord(dr) || dr->nzones_high || dr->nzones != 1)
return -EILSEQ;
kfree(dm); *drp = dr;
return ERR_PTR(-EIO); return 0;
} }
static int adfs_fill_super(struct super_block *sb, void *data, int silent) static int adfs_fill_super(struct super_block *sb, void *data, int silent)
{ {
struct adfs_discrecord *dr; struct adfs_discrecord *dr;
struct buffer_head *bh;
struct object_info root_obj; struct object_info root_obj;
unsigned char *b_data;
unsigned int blocksize;
struct adfs_sb_info *asb; struct adfs_sb_info *asb;
struct inode *root; struct inode *root;
int ret = -EINVAL; int ret = -EINVAL;
...@@ -391,7 +374,10 @@ static int adfs_fill_super(struct super_block *sb, void *data, int silent) ...@@ -391,7 +374,10 @@ static int adfs_fill_super(struct super_block *sb, void *data, int silent)
asb = kzalloc(sizeof(*asb), GFP_KERNEL); asb = kzalloc(sizeof(*asb), GFP_KERNEL);
if (!asb) if (!asb)
return -ENOMEM; return -ENOMEM;
sb->s_fs_info = asb; sb->s_fs_info = asb;
sb->s_magic = ADFS_SUPER_MAGIC;
sb->s_time_gran = 10000000;
/* set default options */ /* set default options */
asb->s_uid = GLOBAL_ROOT_UID; asb->s_uid = GLOBAL_ROOT_UID;
...@@ -403,78 +389,21 @@ static int adfs_fill_super(struct super_block *sb, void *data, int silent) ...@@ -403,78 +389,21 @@ static int adfs_fill_super(struct super_block *sb, void *data, int silent)
if (parse_options(sb, asb, data)) if (parse_options(sb, asb, data))
goto error; goto error;
sb_set_blocksize(sb, BLOCK_SIZE); /* Try to probe the filesystem boot block */
if (!(bh = sb_bread(sb, ADFS_DISCRECORD / BLOCK_SIZE))) { ret = adfs_probe(sb, ADFS_DISCRECORD, 1, adfs_validate_bblk);
adfs_msg(sb, KERN_ERR, "error: unable to read superblock"); if (ret == -EILSEQ)
ret = -EIO; ret = adfs_probe(sb, 0, silent, adfs_validate_dr0);
goto error; if (ret == -EILSEQ) {
}
b_data = bh->b_data + (ADFS_DISCRECORD % BLOCK_SIZE);
if (adfs_checkbblk(b_data)) {
ret = -EINVAL;
goto error_badfs;
}
dr = (struct adfs_discrecord *)(b_data + ADFS_DR_OFFSET);
/*
* Do some sanity checks on the ADFS disc record
*/
if (adfs_checkdiscrecord(dr)) {
ret = -EINVAL;
goto error_badfs;
}
blocksize = 1 << dr->log2secsize;
brelse(bh);
if (sb_set_blocksize(sb, blocksize)) {
bh = sb_bread(sb, ADFS_DISCRECORD / sb->s_blocksize);
if (!bh) {
adfs_msg(sb, KERN_ERR,
"error: couldn't read superblock on 2nd try.");
ret = -EIO;
goto error;
}
b_data = bh->b_data + (ADFS_DISCRECORD % sb->s_blocksize);
if (adfs_checkbblk(b_data)) {
adfs_msg(sb, KERN_ERR,
"error: disc record mismatch, very weird!");
ret = -EINVAL;
goto error_free_bh;
}
dr = (struct adfs_discrecord *)(b_data + ADFS_DR_OFFSET);
} else {
if (!silent) if (!silent)
adfs_msg(sb, KERN_ERR, adfs_msg(sb, KERN_ERR,
"error: unsupported blocksize"); "error: can't find an ADFS filesystem on dev %s.",
sb->s_id);
ret = -EINVAL; ret = -EINVAL;
goto error;
} }
if (ret)
goto error;
/* /* set up enough so that we can read an inode */
* blocksize on this device should now be set to the ADFS log2secsize
*/
sb->s_magic = ADFS_SUPER_MAGIC;
asb->s_idlen = dr->idlen;
asb->s_map_size = dr->nzones | (dr->nzones_high << 8);
asb->s_map2blk = dr->log2bpmb - dr->log2secsize;
asb->s_log2sharesize = dr->log2sharesize;
asb->s_map = adfs_read_map(sb, dr);
if (IS_ERR(asb->s_map)) {
ret = PTR_ERR(asb->s_map);
goto error_free_bh;
}
brelse(bh);
/*
* set up enough so that we can read an inode
*/
sb->s_op = &adfs_sops; sb->s_op = &adfs_sops;
dr = adfs_map_discrecord(asb->s_map); dr = adfs_map_discrecord(asb->s_map);
...@@ -511,23 +440,13 @@ static int adfs_fill_super(struct super_block *sb, void *data, int silent) ...@@ -511,23 +440,13 @@ static int adfs_fill_super(struct super_block *sb, void *data, int silent)
root = adfs_iget(sb, &root_obj); root = adfs_iget(sb, &root_obj);
sb->s_root = d_make_root(root); sb->s_root = d_make_root(root);
if (!sb->s_root) { if (!sb->s_root) {
int i; adfs_free_map(sb);
for (i = 0; i < asb->s_map_size; i++)
brelse(asb->s_map[i].dm_bh);
kfree(asb->s_map);
adfs_error(sb, "get root inode failed\n"); adfs_error(sb, "get root inode failed\n");
ret = -EIO; ret = -EIO;
goto error; goto error;
} }
return 0; return 0;
error_badfs:
if (!silent)
adfs_msg(sb, KERN_ERR,
"error: can't find an ADFS filesystem on dev %s.",
sb->s_id);
error_free_bh:
brelse(bh);
error: error:
sb->s_fs_info = NULL; sb->s_fs_info = NULL;
kfree(asb); kfree(asb);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment