linux/fs/adfs/super.c

495 lines
11 KiB
C
Raw Normal View History

// SPDX-License-Identifier: GPL-2.0-only
/*
* linux/fs/adfs/super.c
*
* Copyright (C) 1997-1999 Russell King
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/parser.h>
#include <linux/mount.h>
#include <linux/seq_file.h>
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h percpu.h is included by sched.h and module.h and thus ends up being included when building most .c files. percpu.h includes slab.h which in turn includes gfp.h making everything defined by the two files universally available and complicating inclusion dependencies. percpu.h -> slab.h dependency is about to be removed. Prepare for this change by updating users of gfp and slab facilities include those headers directly instead of assuming availability. As this conversion needs to touch large number of source files, the following script is used as the basis of conversion. http://userweb.kernel.org/~tj/misc/slabh-sweep.py The script does the followings. * Scan files for gfp and slab usages and update includes such that only the necessary includes are there. ie. if only gfp is used, gfp.h, if slab is used, slab.h. * When the script inserts a new include, it looks at the include blocks and try to put the new include such that its order conforms to its surrounding. It's put in the include block which contains core kernel includes, in the same order that the rest are ordered - alphabetical, Christmas tree, rev-Xmas-tree or at the end if there doesn't seem to be any matching order. * If the script can't find a place to put a new include (mostly because the file doesn't have fitting include block), it prints out an error message indicating which .h file needs to be added to the file. The conversion was done in the following steps. 1. The initial automatic conversion of all .c files updated slightly over 4000 files, deleting around 700 includes and adding ~480 gfp.h and ~3000 slab.h inclusions. The script emitted errors for ~400 files. 2. Each error was manually checked. Some didn't need the inclusion, some needed manual addition while adding it to implementation .h or embedding .c file was more appropriate for others. This step added inclusions to around 150 files. 3. The script was run again and the output was compared to the edits from #2 to make sure no file was left behind. 4. Several build tests were done and a couple of problems were fixed. e.g. lib/decompress_*.c used malloc/free() wrappers around slab APIs requiring slab.h to be added manually. 5. The script was run on all .h files but without automatically editing them as sprinkling gfp.h and slab.h inclusions around .h files could easily lead to inclusion dependency hell. Most gfp.h inclusion directives were ignored as stuff from gfp.h was usually wildly available and often used in preprocessor macros. Each slab.h inclusion directive was examined and added manually as necessary. 6. percpu.h was updated not to include slab.h. 7. Build test were done on the following configurations and failures were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my distributed build env didn't work with gcov compiles) and a few more options had to be turned off depending on archs to make things build (like ipr on powerpc/64 which failed due to missing writeq). * x86 and x86_64 UP and SMP allmodconfig and a custom test config. * powerpc and powerpc64 SMP allmodconfig * sparc and sparc64 SMP allmodconfig * ia64 SMP allmodconfig * s390 SMP allmodconfig * alpha SMP allmodconfig * um on x86_64 SMP allmodconfig 8. percpu.h modifications were reverted so that it could be applied as a separate patch and serve as bisection point. Given the fact that I had only a couple of failures from tests on step 6, I'm fairly confident about the coverage of this conversion patch. If there is a breakage, it's likely to be something in one of the arch headers which should be easily discoverable easily on most builds of the specific arch. Signed-off-by: Tejun Heo <tj@kernel.org> Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org> Cc: Ingo Molnar <mingo@redhat.com> Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 08:04:11 +00:00
#include <linux/slab.h>
#include <linux/statfs.h>
#include <linux/user_namespace.h>
#include <linux/blkdev.h>
#include "adfs.h"
#include "dir_f.h"
#include "dir_fplus.h"
#define ADFS_SB_FLAGS SB_NOATIME
#define ADFS_DEFAULT_OWNER_MASK S_IRWXU
#define ADFS_DEFAULT_OTHER_MASK (S_IRWXG | S_IRWXO)
void __adfs_error(struct super_block *sb, const char *function, const char *fmt, ...)
{
struct va_format vaf;
va_list args;
va_start(args, fmt);
vaf.fmt = fmt;
vaf.va = &args;
printk(KERN_CRIT "ADFS-fs error (device %s)%s%s: %pV\n",
sb->s_id, function ? ": " : "",
function ? function : "", &vaf);
va_end(args);
}
void adfs_msg(struct super_block *sb, const char *pfx, const char *fmt, ...)
{
struct va_format vaf;
va_list args;
va_start(args, fmt);
vaf.fmt = fmt;
vaf.va = &args;
printk("%sADFS-fs (%s): %pV\n", pfx, sb->s_id, &vaf);
va_end(args);
}
static int adfs_checkdiscrecord(struct adfs_discrecord *dr)
{
unsigned int max_idlen;
int i;
/* sector size must be 256, 512 or 1024 bytes */
if (dr->log2secsize != 8 &&
dr->log2secsize != 9 &&
dr->log2secsize != 10)
return 1;
/* idlen must be at least log2secsize + 3 */
if (dr->idlen < dr->log2secsize + 3)
return 1;
/* we cannot have such a large disc that we
* are unable to represent sector offsets in
* 32 bits. This works out at 2.0 TB.
*/
if (le32_to_cpu(dr->disc_size_high) >> dr->log2secsize)
return 1;
/*
* Maximum idlen is limited to 16 bits for new directories by
* the three-byte storage of an indirect disc address. For
* big directories, idlen must be no greater than 19 v2 [1.0]
*/
max_idlen = dr->format_version ? 19 : 16;
if (dr->idlen > max_idlen)
return 1;
/* reserved bytes should be zero */
for (i = 0; i < sizeof(dr->unused52); i++)
if (dr->unused52[i] != 0)
return 1;
return 0;
}
static void adfs_put_super(struct super_block *sb)
{
struct adfs_sb_info *asb = ADFS_SB(sb);
adfs_free_map(sb);
kfree_rcu(asb, rcu);
}
static int adfs_show_options(struct seq_file *seq, struct dentry *root)
{
struct adfs_sb_info *asb = ADFS_SB(root->d_sb);
if (!uid_eq(asb->s_uid, GLOBAL_ROOT_UID))
seq_printf(seq, ",uid=%u", from_kuid_munged(&init_user_ns, asb->s_uid));
if (!gid_eq(asb->s_gid, GLOBAL_ROOT_GID))
seq_printf(seq, ",gid=%u", from_kgid_munged(&init_user_ns, asb->s_gid));
if (asb->s_owner_mask != ADFS_DEFAULT_OWNER_MASK)
seq_printf(seq, ",ownmask=%o", asb->s_owner_mask);
if (asb->s_other_mask != ADFS_DEFAULT_OTHER_MASK)
seq_printf(seq, ",othmask=%o", asb->s_other_mask);
if (asb->s_ftsuffix != 0)
seq_printf(seq, ",ftsuffix=%u", asb->s_ftsuffix);
return 0;
}
enum {Opt_uid, Opt_gid, Opt_ownmask, Opt_othmask, Opt_ftsuffix, Opt_err};
static const match_table_t tokens = {
{Opt_uid, "uid=%u"},
{Opt_gid, "gid=%u"},
{Opt_ownmask, "ownmask=%o"},
{Opt_othmask, "othmask=%o"},
{Opt_ftsuffix, "ftsuffix=%u"},
{Opt_err, NULL}
};
static int parse_options(struct super_block *sb, struct adfs_sb_info *asb,
char *options)
{
char *p;
int option;
if (!options)
return 0;
while ((p = strsep(&options, ",")) != NULL) {
substring_t args[MAX_OPT_ARGS];
int token;
if (!*p)
continue;
token = match_token(p, tokens, args);
switch (token) {
case Opt_uid:
if (match_int(args, &option))
return -EINVAL;
asb->s_uid = make_kuid(current_user_ns(), option);
if (!uid_valid(asb->s_uid))
return -EINVAL;
break;
case Opt_gid:
if (match_int(args, &option))
return -EINVAL;
asb->s_gid = make_kgid(current_user_ns(), option);
if (!gid_valid(asb->s_gid))
return -EINVAL;
break;
case Opt_ownmask:
if (match_octal(args, &option))
return -EINVAL;
asb->s_owner_mask = option;
break;
case Opt_othmask:
if (match_octal(args, &option))
return -EINVAL;
asb->s_other_mask = option;
break;
case Opt_ftsuffix:
if (match_int(args, &option))
return -EINVAL;
asb->s_ftsuffix = option;
break;
default:
adfs_msg(sb, KERN_ERR,
"unrecognised mount option \"%s\" or missing value",
p);
return -EINVAL;
}
}
return 0;
}
static int adfs_remount(struct super_block *sb, int *flags, char *data)
{
struct adfs_sb_info temp_asb;
int ret;
fs: push sync_filesystem() down to the file system's remount_fs() Previously, the no-op "mount -o mount /dev/xxx" operation when the file system is already mounted read-write causes an implied, unconditional syncfs(). This seems pretty stupid, and it's certainly documented or guaraunteed to do this, nor is it particularly useful, except in the case where the file system was mounted rw and is getting remounted read-only. However, it's possible that there might be some file systems that are actually depending on this behavior. In most file systems, it's probably fine to only call sync_filesystem() when transitioning from read-write to read-only, and there are some file systems where this is not needed at all (for example, for a pseudo-filesystem or something like romfs). Signed-off-by: "Theodore Ts'o" <tytso@mit.edu> Cc: linux-fsdevel@vger.kernel.org Cc: Christoph Hellwig <hch@infradead.org> Cc: Artem Bityutskiy <dedekind1@gmail.com> Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: Evgeniy Dushistov <dushistov@mail.ru> Cc: Jan Kara <jack@suse.cz> Cc: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp> Cc: Anders Larsen <al@alarsen.net> Cc: Phillip Lougher <phillip@squashfs.org.uk> Cc: Kees Cook <keescook@chromium.org> Cc: Mikulas Patocka <mikulas@artax.karlin.mff.cuni.cz> Cc: Petr Vandrovec <petr@vandrovec.name> Cc: xfs@oss.sgi.com Cc: linux-btrfs@vger.kernel.org Cc: linux-cifs@vger.kernel.org Cc: samba-technical@lists.samba.org Cc: codalist@coda.cs.cmu.edu Cc: linux-ext4@vger.kernel.org Cc: linux-f2fs-devel@lists.sourceforge.net Cc: fuse-devel@lists.sourceforge.net Cc: cluster-devel@redhat.com Cc: linux-mtd@lists.infradead.org Cc: jfs-discussion@lists.sourceforge.net Cc: linux-nfs@vger.kernel.org Cc: linux-nilfs@vger.kernel.org Cc: linux-ntfs-dev@lists.sourceforge.net Cc: ocfs2-devel@oss.oracle.com Cc: reiserfs-devel@vger.kernel.org
2014-03-13 14:14:33 +00:00
sync_filesystem(sb);
*flags |= ADFS_SB_FLAGS;
temp_asb = *ADFS_SB(sb);
ret = parse_options(sb, &temp_asb, data);
if (ret == 0)
*ADFS_SB(sb) = temp_asb;
return ret;
}
static int adfs_statfs(struct dentry *dentry, struct kstatfs *buf)
{
struct super_block *sb = dentry->d_sb;
struct adfs_sb_info *sbi = ADFS_SB(sb);
u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
adfs_map_statfs(sb, buf);
buf->f_type = ADFS_SUPER_MAGIC;
buf->f_namelen = sbi->s_namelen;
buf->f_bsize = sb->s_blocksize;
buf->f_ffree = (long)(buf->f_bfree * buf->f_files) / (long)buf->f_blocks;
buf->f_fsid = u64_to_fsid(id);
return 0;
}
static struct kmem_cache *adfs_inode_cachep;
static struct inode *adfs_alloc_inode(struct super_block *sb)
{
struct adfs_inode_info *ei;
ei = kmem_cache_alloc(adfs_inode_cachep, GFP_KERNEL);
if (!ei)
return NULL;
return &ei->vfs_inode;
}
static void adfs_free_inode(struct inode *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)
{
struct adfs_inode_info *ei = (struct adfs_inode_info *) foo;
inode_init_once(&ei->vfs_inode);
}
static int __init init_inodecache(void)
{
adfs_inode_cachep = kmem_cache_create("adfs_inode_cache",
sizeof(struct adfs_inode_info),
0, (SLAB_RECLAIM_ACCOUNT|
2016-01-14 23:18:21 +00:00
SLAB_MEM_SPREAD|SLAB_ACCOUNT),
init_once);
if (adfs_inode_cachep == NULL)
return -ENOMEM;
return 0;
}
static void destroy_inodecache(void)
{
/*
* Make sure all delayed rcu free inodes are flushed before we
* destroy cache.
*/
rcu_barrier();
kmem_cache_destroy(adfs_inode_cachep);
}
static const struct super_operations adfs_sops = {
.alloc_inode = adfs_alloc_inode,
.free_inode = adfs_free_inode,
.drop_inode = adfs_drop_inode,
.write_inode = adfs_write_inode,
.put_super = adfs_put_super,
.statfs = adfs_statfs,
.remount_fs = adfs_remount,
.show_options = adfs_show_options,
};
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_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;
}
/* read the buffer */
bh = sb_bread(sb, offset >> sb->s_blocksize_bits);
if (!bh) {
adfs_msg(sb, KERN_ERR,
"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;
}
/* 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);
}
brelse(bh);
}
return -EIO;
}
static int adfs_validate_bblk(struct super_block *sb, struct buffer_head *bh,
struct adfs_discrecord **drp)
{
struct adfs_discrecord *dr;
unsigned char *b_data;
b_data = bh->b_data + (ADFS_DISCRECORD % sb->s_blocksize);
if (adfs_checkbblk(b_data))
return -EILSEQ;
/* Do some sanity checks on the ADFS disc record */
dr = (struct adfs_discrecord *)(b_data + ADFS_DR_OFFSET);
if (adfs_checkdiscrecord(dr))
return -EILSEQ;
*drp = dr;
return 0;
}
static int adfs_validate_dr0(struct super_block *sb, struct buffer_head *bh,
struct adfs_discrecord **drp)
{
struct adfs_discrecord *dr;
/* Do some sanity checks on the ADFS disc record */
dr = (struct adfs_discrecord *)(bh->b_data + 4);
if (adfs_checkdiscrecord(dr) || dr->nzones_high || dr->nzones != 1)
return -EILSEQ;
*drp = dr;
return 0;
}
static int adfs_fill_super(struct super_block *sb, void *data, int silent)
{
struct adfs_discrecord *dr;
struct object_info root_obj;
struct adfs_sb_info *asb;
struct inode *root;
int ret = -EINVAL;
sb->s_flags |= ADFS_SB_FLAGS;
asb = kzalloc(sizeof(*asb), GFP_KERNEL);
if (!asb)
return -ENOMEM;
sb->s_fs_info = asb;
sb->s_magic = ADFS_SUPER_MAGIC;
sb->s_time_gran = 10000000;
/* set default options */
asb->s_uid = GLOBAL_ROOT_UID;
asb->s_gid = GLOBAL_ROOT_GID;
asb->s_owner_mask = ADFS_DEFAULT_OWNER_MASK;
asb->s_other_mask = ADFS_DEFAULT_OTHER_MASK;
asb->s_ftsuffix = 0;
if (parse_options(sb, asb, data))
goto error;
/* Try to probe the filesystem boot block */
ret = adfs_probe(sb, ADFS_DISCRECORD, 1, adfs_validate_bblk);
if (ret == -EILSEQ)
ret = adfs_probe(sb, 0, silent, adfs_validate_dr0);
if (ret == -EILSEQ) {
if (!silent)
adfs_msg(sb, KERN_ERR,
"error: can't find an ADFS filesystem on dev %s.",
sb->s_id);
ret = -EINVAL;
}
if (ret)
goto error;
/* set up enough so that we can read an inode */
sb->s_op = &adfs_sops;
dr = adfs_map_discrecord(asb->s_map);
root_obj.parent_id = root_obj.indaddr = le32_to_cpu(dr->root);
root_obj.name_len = 0;
/* Set root object date as 01 Jan 1987 00:00:00 */
root_obj.loadaddr = 0xfff0003f;
root_obj.execaddr = 0xec22c000;
root_obj.size = ADFS_NEWDIR_SIZE;
root_obj.attr = ADFS_NDA_DIRECTORY | ADFS_NDA_OWNER_READ |
ADFS_NDA_OWNER_WRITE | ADFS_NDA_PUBLIC_READ;
/*
* If this is a F+ disk with variable length directories,
* get the root_size from the disc record.
*/
if (dr->format_version) {
root_obj.size = le32_to_cpu(dr->root_size);
asb->s_dir = &adfs_fplus_dir_ops;
asb->s_namelen = ADFS_FPLUS_NAME_LEN;
} else {
asb->s_dir = &adfs_f_dir_ops;
asb->s_namelen = ADFS_F_NAME_LEN;
}
/*
* ,xyz hex filetype suffix may be added by driver
* to files that have valid RISC OS filetype
*/
if (asb->s_ftsuffix)
asb->s_namelen += 4;
sb->s_d_op = &adfs_dentry_operations;
root = adfs_iget(sb, &root_obj);
sb->s_root = d_make_root(root);
if (!sb->s_root) {
adfs_free_map(sb);
adfs_error(sb, "get root inode failed\n");
ret = -EIO;
goto error;
}
return 0;
error:
sb->s_fs_info = NULL;
kfree(asb);
return ret;
}
static struct dentry *adfs_mount(struct file_system_type *fs_type,
int flags, const char *dev_name, void *data)
{
return mount_bdev(fs_type, flags, dev_name, data, adfs_fill_super);
}
static struct file_system_type adfs_fs_type = {
.owner = THIS_MODULE,
.name = "adfs",
.mount = adfs_mount,
.kill_sb = kill_block_super,
.fs_flags = FS_REQUIRES_DEV,
};
fs: Limit sys_mount to only request filesystem modules. Modify the request_module to prefix the file system type with "fs-" and add aliases to all of the filesystems that can be built as modules to match. A common practice is to build all of the kernel code and leave code that is not commonly needed as modules, with the result that many users are exposed to any bug anywhere in the kernel. Looking for filesystems with a fs- prefix limits the pool of possible modules that can be loaded by mount to just filesystems trivially making things safer with no real cost. Using aliases means user space can control the policy of which filesystem modules are auto-loaded by editing /etc/modprobe.d/*.conf with blacklist and alias directives. Allowing simple, safe, well understood work-arounds to known problematic software. This also addresses a rare but unfortunate problem where the filesystem name is not the same as it's module name and module auto-loading would not work. While writing this patch I saw a handful of such cases. The most significant being autofs that lives in the module autofs4. This is relevant to user namespaces because we can reach the request module in get_fs_type() without having any special permissions, and people get uncomfortable when a user specified string (in this case the filesystem type) goes all of the way to request_module. After having looked at this issue I don't think there is any particular reason to perform any filtering or permission checks beyond making it clear in the module request that we want a filesystem module. The common pattern in the kernel is to call request_module() without regards to the users permissions. In general all a filesystem module does once loaded is call register_filesystem() and go to sleep. Which means there is not much attack surface exposed by loading a filesytem module unless the filesystem is mounted. In a user namespace filesystems are not mounted unless .fs_flags = FS_USERNS_MOUNT, which most filesystems do not set today. Acked-by: Serge Hallyn <serge.hallyn@canonical.com> Acked-by: Kees Cook <keescook@chromium.org> Reported-by: Kees Cook <keescook@google.com> Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
2013-03-03 03:39:14 +00:00
MODULE_ALIAS_FS("adfs");
static int __init init_adfs_fs(void)
{
int err = init_inodecache();
if (err)
goto out1;
err = register_filesystem(&adfs_fs_type);
if (err)
goto out;
return 0;
out:
destroy_inodecache();
out1:
return err;
}
static void __exit exit_adfs_fs(void)
{
unregister_filesystem(&adfs_fs_type);
destroy_inodecache();
}
module_init(init_adfs_fs)
module_exit(exit_adfs_fs)
MODULE_LICENSE("GPL");