Skip to content

Commit

Permalink
Merge branch 'work.adfs' of git://git.kernel.org/pub/scm/linux/kernel…
Browse files Browse the repository at this point in the history
…/git/viro/vfs

Pull adfs updates from Al Viro:
 "More ADFS patches from Russell King"

* 'work.adfs' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs:
  fs/adfs: add time stamp and file type helpers
  fs/adfs: super: limit idlen according to directory type
  fs/adfs: super: fix use-after-free bug
  fs/adfs: super: safely update options on remount
  fs/adfs: super: correct superblock flags
  fs/adfs: clean up indirect disc addresses and fragment IDs
  fs/adfs: clean up error message printing
  fs/adfs: use %pV for error messages
  fs/adfs: use format_version from disc_record
  fs/adfs: add helper to get filesystem size
  fs/adfs: add helper to get discrecord from map
  fs/adfs: correct disc record structure
  • Loading branch information
torvalds committed Jul 19, 2019
2 parents 933a90b + b4ed8f7 commit d2fbf4b
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 144 deletions.
70 changes: 45 additions & 25 deletions fs/adfs/adfs.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* SPDX-License-Identifier: GPL-2.0 */
#include <linux/buffer_head.h>
#include <linux/fs.h>
#include <linux/adfs_fs.h>

Expand All @@ -8,6 +9,15 @@
#define ADFS_BAD_FRAG 1
#define ADFS_ROOT_FRAG 2

#define ADFS_FILETYPE_NONE ((u16)~0)

/* RISC OS 12-bit filetype is stored in load_address[19:8] */
static inline u16 adfs_filetype(u32 loadaddr)
{
return (loadaddr & 0xfff00000) == 0xfff00000 ?
(loadaddr >> 8) & 0xfff : ADFS_FILETYPE_NONE;
}

#define ADFS_NDA_OWNER_READ (1 << 0)
#define ADFS_NDA_OWNER_WRITE (1 << 1)
#define ADFS_NDA_LOCKED (1 << 2)
Expand All @@ -18,22 +28,28 @@

#include "dir_f.h"

struct buffer_head;

/*
* adfs file system inode data in memory
*/
struct adfs_inode_info {
loff_t mmu_private;
unsigned long parent_id; /* object id of parent */
__u32 parent_id; /* parent indirect disc address */
__u32 loadaddr; /* RISC OS load address */
__u32 execaddr; /* RISC OS exec address */
unsigned int filetype; /* RISC OS file type */
unsigned int attr; /* RISC OS permissions */
unsigned int stamped:1; /* RISC OS file has date/time */
struct inode vfs_inode;
};

static inline struct adfs_inode_info *ADFS_I(struct inode *inode)
{
return container_of(inode, struct adfs_inode_info, vfs_inode);
}

static inline bool adfs_inode_is_stamped(struct inode *inode)
{
return (ADFS_I(inode)->loadaddr & 0xfff00000) == 0xfff00000;
}

/*
* Forward-declare this
*/
Expand All @@ -59,10 +75,8 @@ struct adfs_sb_info {
__u32 s_ids_per_zone; /* max. no ids in one zone */
__u32 s_idlen; /* length of ID in map */
__u32 s_map_size; /* sector size of a map */
unsigned long s_size; /* total size (in blocks) of this fs */
signed int s_map2blk; /* shift left by this for map->sector*/
unsigned int s_log2sharesize;/* log2 share size */
__le32 s_version; /* disc format version */
unsigned int s_namelen; /* maximum number of characters in name */
};

Expand All @@ -71,11 +85,6 @@ static inline struct adfs_sb_info *ADFS_SB(struct super_block *sb)
return sb->s_fs_info;
}

static inline struct adfs_inode_info *ADFS_I(struct inode *inode)
{
return container_of(inode, struct adfs_inode_info, vfs_inode);
}

/*
* Directory handling
*/
Expand All @@ -89,7 +98,7 @@ struct adfs_dir {
struct buffer_head **bh_fplus;

unsigned int pos;
unsigned int parent_id;
__u32 parent_id;

struct adfs_dirheader dirhead;
union adfs_dirtail dirtail;
Expand All @@ -101,20 +110,18 @@ struct adfs_dir {
#define ADFS_MAX_NAME_LEN (256 + 4) /* +4 for ,xyz hex filetype suffix */
struct object_info {
__u32 parent_id; /* parent object id */
__u32 file_id; /* object id */
__u32 indaddr; /* indirect disc addr */
__u32 loadaddr; /* load address */
__u32 execaddr; /* execution address */
__u32 size; /* size */
__u8 attr; /* RISC OS attributes */
unsigned int name_len; /* name length */
char name[ADFS_MAX_NAME_LEN];/* file name */

/* RISC OS file type (12-bit: derived from loadaddr) */
__u16 filetype;
};

struct adfs_dir_ops {
int (*read)(struct super_block *sb, unsigned int id, unsigned int sz, struct adfs_dir *dir);
int (*read)(struct super_block *sb, unsigned int indaddr,
unsigned int size, struct adfs_dir *dir);
int (*setpos)(struct adfs_dir *dir, unsigned int fpos);
int (*getnext)(struct adfs_dir *dir, struct object_info *obj);
int (*update)(struct adfs_dir *dir, struct object_info *obj);
Expand All @@ -137,14 +144,15 @@ int adfs_write_inode(struct inode *inode, struct writeback_control *wbc);
int adfs_notify_change(struct dentry *dentry, struct iattr *attr);

/* map.c */
extern int adfs_map_lookup(struct super_block *sb, unsigned int 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);

/* Misc */
__printf(3, 4)
void __adfs_error(struct super_block *sb, const char *function,
const char *fmt, ...);
#define adfs_error(sb, fmt...) __adfs_error(sb, __func__, fmt)
void adfs_msg(struct super_block *sb, const char *pfx, const char *fmt, ...);

/* super.c */

Expand Down Expand Up @@ -182,16 +190,28 @@ static inline __u32 signed_asl(__u32 val, signed int shift)
*
* The root directory ID should always be looked up in the map [3.4]
*/
static inline int
__adfs_block_map(struct super_block *sb, unsigned int object_id,
unsigned int block)
static inline int __adfs_block_map(struct super_block *sb, u32 indaddr,
unsigned int block)
{
if (object_id & 255) {
if (indaddr & 255) {
unsigned int off;

off = (object_id & 255) - 1;
off = (indaddr & 255) - 1;
block += off << ADFS_SB(sb)->s_log2sharesize;
}

return adfs_map_lookup(sb, object_id >> 8, block);
return adfs_map_lookup(sb, indaddr >> 8, block);
}

/* Return the disc record from the map */
static inline
struct adfs_discrecord *adfs_map_discrecord(struct adfs_discmap *dm)
{
return (void *)(dm[0].dm_bh->b_data + 4);
}

static inline u64 adfs_disc_size(const struct adfs_discrecord *dr)
{
return (u64)le32_to_cpu(dr->disc_size_high) << 32 |
le32_to_cpu(dr->disc_size);
}
25 changes: 10 additions & 15 deletions fs/adfs/dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,14 @@ void adfs_object_fixup(struct adfs_dir *dir, struct object_info *obj)
if (obj->name_len <= 2 && dots == obj->name_len)
obj->name[0] = '^';

obj->filetype = -1;

/*
* object is a file and is filetyped and timestamped?
* RISC OS 12-bit filetype is stored in load_address[19:8]
* If the object is a file, and the user requested the ,xyz hex
* filetype suffix to the name, check the filetype and append.
*/
if ((0 == (obj->attr & ADFS_NDA_DIRECTORY)) &&
(0xfff00000 == (0xfff00000 & obj->loadaddr))) {
obj->filetype = (__u16) ((0x000fff00 & obj->loadaddr) >> 8);

/* optionally append the ,xyz hex filetype suffix */
if (ADFS_SB(dir->sb)->s_ftsuffix) {
__u16 filetype = obj->filetype;
if (!(obj->attr & ADFS_NDA_DIRECTORY) && ADFS_SB(dir->sb)->s_ftsuffix) {
u16 filetype = adfs_filetype(obj->loadaddr);

if (filetype != ADFS_FILETYPE_NONE) {
obj->name[obj->name_len++] = ',';
obj->name[obj->name_len++] = hex_asc_lo(filetype >> 8);
obj->name[obj->name_len++] = hex_asc_lo(filetype >> 4);
Expand Down Expand Up @@ -92,7 +86,7 @@ adfs_readdir(struct file *file, struct dir_context *ctx)
goto unlock_out;
while (ops->getnext(&dir, &obj) == 0) {
if (!dir_emit(ctx, obj.name, obj.name_len,
obj.file_id, DT_UNKNOWN))
obj.indaddr, DT_UNKNOWN))
break;
ctx->pos++;
}
Expand All @@ -113,8 +107,8 @@ adfs_dir_update(struct super_block *sb, struct object_info *obj, int wait)
const struct adfs_dir_ops *ops = ADFS_SB(sb)->s_dir;
struct adfs_dir dir;

printk(KERN_INFO "adfs_dir_update: object %06X in dir %06X\n",
obj->file_id, obj->parent_id);
printk(KERN_INFO "adfs_dir_update: object %06x in dir %06x\n",
obj->indaddr, obj->parent_id);

if (!ops->update) {
ret = -EINVAL;
Expand Down Expand Up @@ -178,7 +172,8 @@ static int adfs_dir_lookup_byname(struct inode *inode, const struct qstr *qstr,
goto out;

if (ADFS_I(inode)->parent_id != dir.parent_id) {
adfs_error(sb, "parent directory changed under me! (%lx but got %x)\n",
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;
Expand Down
38 changes: 16 additions & 22 deletions fs/adfs/dir_f.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*
* E and F format directory handling
*/
#include <linux/buffer_head.h>
#include "adfs.h"
#include "dir_f.h"

Expand Down Expand Up @@ -124,12 +123,9 @@ adfs_dir_checkbyte(const struct adfs_dir *dir)
return (dircheck ^ (dircheck >> 8) ^ (dircheck >> 16) ^ (dircheck >> 24)) & 0xff;
}

/*
* Read and check that a directory is valid
*/
static int
adfs_dir_read(struct super_block *sb, unsigned long object_id,
unsigned int size, struct adfs_dir *dir)
/* Read and check that a directory is valid */
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;
int blk = 0;
Expand All @@ -149,10 +145,10 @@ adfs_dir_read(struct super_block *sb, unsigned long object_id,
for (blk = 0; blk < size; blk++) {
int phys;

phys = __adfs_block_map(sb, object_id, blk);
phys = __adfs_block_map(sb, indaddr, blk);
if (!phys) {
adfs_error(sb, "dir object %lX has a hole at offset %d",
object_id, blk);
adfs_error(sb, "dir %06x has a hole at offset %d",
indaddr, blk);
goto release_buffers;
}

Expand Down Expand Up @@ -180,8 +176,7 @@ adfs_dir_read(struct super_block *sb, unsigned long object_id,
return 0;

bad_dir:
adfs_error(sb, "corrupted directory fragment %lX",
object_id);
adfs_error(sb, "dir %06x is corrupted", indaddr);
release_buffers:
for (blk -= 1; blk >= 0; blk -= 1)
brelse(dir->bh[blk]);
Expand All @@ -208,7 +203,7 @@ adfs_dir2obj(struct adfs_dir *dir, struct object_info *obj,
}

obj->name_len = name_len;
obj->file_id = adfs_readval(de->dirinddiscadd, 3);
obj->indaddr = adfs_readval(de->dirinddiscadd, 3);
obj->loadaddr = adfs_readval(de->dirload, 4);
obj->execaddr = adfs_readval(de->direxec, 4);
obj->size = adfs_readval(de->dirlen, 4);
Expand All @@ -223,7 +218,7 @@ adfs_dir2obj(struct adfs_dir *dir, struct object_info *obj,
static inline void
adfs_obj2dir(struct adfs_direntry *de, struct object_info *obj)
{
adfs_writeval(de->dirinddiscadd, 3, obj->file_id);
adfs_writeval(de->dirinddiscadd, 3, obj->indaddr);
adfs_writeval(de->dirload, 4, obj->loadaddr);
adfs_writeval(de->direxec, 4, obj->execaddr);
adfs_writeval(de->dirlen, 4, obj->size);
Expand Down Expand Up @@ -309,8 +304,7 @@ __adfs_dir_put(struct adfs_dir *dir, int pos, struct object_info *obj)
* the caller is responsible for holding the necessary
* locks.
*/
static int
adfs_dir_find_entry(struct adfs_dir *dir, unsigned long object_id)
static int adfs_dir_find_entry(struct adfs_dir *dir, u32 indaddr)
{
int pos, ret;

Expand All @@ -322,7 +316,7 @@ adfs_dir_find_entry(struct adfs_dir *dir, unsigned long object_id)
if (!__adfs_dir_get(dir, pos, &obj))
break;

if (obj.file_id == object_id) {
if (obj.indaddr == indaddr) {
ret = pos;
break;
}
Expand All @@ -331,15 +325,15 @@ adfs_dir_find_entry(struct adfs_dir *dir, unsigned long object_id)
return ret;
}

static int
adfs_f_read(struct super_block *sb, unsigned int id, unsigned int sz, struct adfs_dir *dir)
static int adfs_f_read(struct super_block *sb, u32 indaddr, unsigned int size,
struct adfs_dir *dir)
{
int ret;

if (sz != ADFS_NEWDIR_SIZE)
if (size != ADFS_NEWDIR_SIZE)
return -EIO;

ret = adfs_dir_read(sb, id, sz, dir);
ret = adfs_dir_read(sb, indaddr, size, dir);
if (ret)
adfs_error(sb, "unable to read directory");
else
Expand Down Expand Up @@ -376,7 +370,7 @@ adfs_f_update(struct adfs_dir *dir, struct object_info *obj)
struct super_block *sb = dir->sb;
int ret, i;

ret = adfs_dir_find_entry(dir, obj->file_id);
ret = adfs_dir_find_entry(dir, obj->indaddr);
if (ret < 0) {
adfs_error(dir->sb, "unable to locate entry to update");
goto out;
Expand Down
21 changes: 9 additions & 12 deletions fs/adfs/dir_fplus.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*
* Copyright (C) 1997-1999 Russell King
*/
#include <linux/buffer_head.h>
#include <linux/slab.h>
#include "adfs.h"
#include "dir_fplus.h"
Expand Down Expand Up @@ -37,17 +36,15 @@ adfs_fplus_read(struct super_block *sb, unsigned int id, unsigned int sz, struct
h = (struct adfs_bigdirheader *)dir->bh_fplus[0]->b_data;
size = le32_to_cpu(h->bigdirsize);
if (size != sz) {
printk(KERN_WARNING "adfs: adfs_fplus_read:"
" directory header size %X\n"
" does not match directory size %X\n",
size, sz);
adfs_msg(sb, KERN_WARNING,
"directory header size %X does not match directory size %X",
size, sz);
}

if (h->bigdirversion[0] != 0 || h->bigdirversion[1] != 0 ||
h->bigdirversion[2] != 0 || size & 2047 ||
h->bigdirstartname != cpu_to_le32(BIGDIRSTARTNAME)) {
printk(KERN_WARNING "adfs: dir object %X has"
" malformed dir header\n", id);
adfs_error(sb, "dir %06x has malformed header", id);
goto out;
}

Expand All @@ -58,9 +55,10 @@ adfs_fplus_read(struct super_block *sb, unsigned int id, unsigned int sz, struct
kcalloc(size, sizeof(struct buffer_head *),
GFP_KERNEL);
if (!bh_fplus) {
adfs_msg(sb, KERN_ERR,
"not enough memory for dir object %X (%d blocks)",
id, size);
ret = -ENOMEM;
adfs_error(sb, "not enough memory for"
" dir object %X (%d blocks)", id, size);
goto out;
}
dir->bh_fplus = bh_fplus;
Expand Down Expand Up @@ -91,8 +89,7 @@ adfs_fplus_read(struct super_block *sb, unsigned int id, unsigned int sz, struct
if (t->bigdirendname != cpu_to_le32(BIGDIRENDNAME) ||
t->bigdirendmasseq != h->startmasseq ||
t->reserved[0] != 0 || t->reserved[1] != 0) {
printk(KERN_WARNING "adfs: dir object %X has "
"malformed dir end\n", id);
adfs_error(sb, "dir %06x has malformed tail", id);
goto out;
}

Expand Down Expand Up @@ -180,7 +177,7 @@ adfs_fplus_getnext(struct adfs_dir *dir, struct object_info *obj)
obj->loadaddr = le32_to_cpu(bde.bigdirload);
obj->execaddr = le32_to_cpu(bde.bigdirexec);
obj->size = le32_to_cpu(bde.bigdirlen);
obj->file_id = le32_to_cpu(bde.bigdirindaddr);
obj->indaddr = le32_to_cpu(bde.bigdirindaddr);
obj->attr = le32_to_cpu(bde.bigdirattr);
obj->name_len = le32_to_cpu(bde.bigdirobnamelen);

Expand Down
Loading

0 comments on commit d2fbf4b

Please sign in to comment.