mirror of
https://github.com/torvalds/linux.git
synced 2026-10-28 17:55:02 +01:00
Please consider pulling these changes from the signed vfs-7.3-rc1.misc tag.
Thanks!
Christian
-----BEGIN PGP SIGNATURE-----
iHUEABYKAB0WIQRAhzRXHqcMeLMyaSiRxhvAZXjcogUCan7RJQAKCRCRxhvAZXjc
otI6AQCIwshv02UI9fmbTC9pFr8b+v/9IWQwdVULaH61HXgICQD7BE/CktL8nfRs
zCzGS6kBdWgiOA6bdVpgGGLqiMRnQgo=
=Kc+G
-----END PGP SIGNATURE-----
Merge tag 'vfs-7.3-rc1.misc' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs
Pull misc vfs updates from Christian Brauner:
"Bigger cleanups:
- The lockref dead-count handling is tidied up.
The open-coded check for a count below zero as the dead marker
relies on information the caller should not have.
- make put_mnt_ns() leave mounts connected. Destroying a mount
namespace disconnected its mounts from their mount points. So a
file descriptor still open on the parent of a mount point could be
used to peek under it.
Locked mounts were already kept connected to prevent exactly that.
But a mount is only locked when its tree is copied across a user
namespace boundary. So a mount namespace set up by a privileged
component had no locked mounts and its mounts were disconnected.
Passing UMOUNT_CONNECTED keeps every mount connected and prevents
that bug.
- vfs_prepare_mode() passes S_IFDIR for directories. I meant to fix
that ago but didn't get to it. So now someone finally did it.
This kills the exception where the mode could be 0 when a directory
was created whereas every other creation operation passed it
explicitly already.
- move long delayed work for ufs, jffs2, hfsplus, hfs and affs from
the per-cpu system_long_wq to the new unbound system_dfl_long_wq.
None of that work relies on per-cpu state and the work item is
enqueued with queue_delayed_work() whose timer is global anyway. So
it may as well benefit from scheduler task placement.
Smaller fixes and cleanups:
- unlock_buffer() and journal_end_buffer_io_sync() use
clear_and_wake_up_bit()
- the pipe page pools are unified into a single per-pipe pool and the
extra wake_up(rd_wait) is limited to EPOLLET consumers
- eventpoll now computes its timer slack lazily in ep_poll()
- shrink_dcache_for_umount() keeps making progress on busy roots
- excess xarray nodes are freed in clear_inode()
- romfs detects hard link cycles
- the user path of nested backing files is fixed
- pidfd holds exec_update_lock around the namespace ioctl
- non-memcg-aware nr_cached_objects is skipped during memcg slab
shrink
- iomap_write_iter() always returns status
- mangle_path() is renamed to seq_mangle_path()
- inode timestamp accessors are annotated
- new regression test for pipe->poll_usage.
- a few documentation, kernel-doc and selftest fixes"
* tag 'vfs-7.3-rc1.misc' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs: (67 commits)
selftests/namespaces: Fix racy pipe handshake in timens and pidns_separate
selftests/epoll: add a regression test for pipe->poll_usage
pipe: only enable the extra wake_up(rd_wait) for EPOLLET consumers
pidfd: hold exec_update_lock around namespace ioctl
fs: fix user path of nested backing files
fs: remove stale inode_insert5() kernel-doc parameter
fs: fix switch/case indentation in sysfs() syscall
fs: document semantics of kstat::{uid,gid} fields
dcache: keep shrink_dcache_for_umount() making progress on busy roots
seq_file: rename mangle_path to seq_mangle_path
nstree: add/fix struct ns_id_req kernel-doc member fields
dcache: use lockref routines for dead count checks
lockref: tidy up dead count handling
initramfs: fix typo in reserve_initrd_mem comment
fs/pipe: unify the page pools into a single per-pipe pool
fs: annotate inode timestamp accessors
eventpoll: compute timer slack lazily in ep_poll()
selftests/filesystems: add mntns cleanup test
put_mnt_ns(): leave mounts connected
affs: Move long delayed work on system_dfl_long_wq
...
462 lines
9.8 KiB
C
462 lines
9.8 KiB
C
// SPDX-License-Identifier: GPL-2.0-only
|
|
/*
|
|
* OMFS (as used by RIO Karma) directory operations.
|
|
* Copyright (C) 2005 Bob Copeland <me@bobcopeland.com>
|
|
*/
|
|
|
|
#include <linux/fs.h>
|
|
#include <linux/ctype.h>
|
|
#include <linux/buffer_head.h>
|
|
#include "omfs.h"
|
|
|
|
static int omfs_hash(const char *name, int namelen, int mod)
|
|
{
|
|
int i, hash = 0;
|
|
for (i = 0; i < namelen; i++)
|
|
hash ^= tolower(name[i]) << (i % 24);
|
|
return hash % mod;
|
|
}
|
|
|
|
/*
|
|
* Finds the bucket for a given name and reads the containing block;
|
|
* *ofs is set to the offset of the first list entry.
|
|
*/
|
|
static struct buffer_head *omfs_get_bucket(struct inode *dir,
|
|
const char *name, int namelen, int *ofs)
|
|
{
|
|
int nbuckets = (dir->i_size - OMFS_DIR_START)/8;
|
|
int bucket = omfs_hash(name, namelen, nbuckets);
|
|
|
|
*ofs = OMFS_DIR_START + bucket * 8;
|
|
return omfs_bread(dir->i_sb, dir->i_ino);
|
|
}
|
|
|
|
static struct buffer_head *omfs_scan_list(struct inode *dir, u64 block,
|
|
const char *name, int namelen,
|
|
u64 *prev_block)
|
|
{
|
|
struct buffer_head *bh;
|
|
struct omfs_inode *oi;
|
|
int err = -ENOENT;
|
|
*prev_block = ~0;
|
|
|
|
while (block != ~0) {
|
|
bh = omfs_bread(dir->i_sb, block);
|
|
if (!bh) {
|
|
err = -EIO;
|
|
goto err;
|
|
}
|
|
|
|
oi = (struct omfs_inode *) bh->b_data;
|
|
if (omfs_is_bad(OMFS_SB(dir->i_sb), &oi->i_head, block)) {
|
|
brelse(bh);
|
|
goto err;
|
|
}
|
|
|
|
if (strncmp(oi->i_name, name, namelen) == 0)
|
|
return bh;
|
|
|
|
*prev_block = block;
|
|
block = be64_to_cpu(oi->i_sibling);
|
|
brelse(bh);
|
|
}
|
|
err:
|
|
return ERR_PTR(err);
|
|
}
|
|
|
|
static struct buffer_head *omfs_find_entry(struct inode *dir,
|
|
const char *name, int namelen)
|
|
{
|
|
struct buffer_head *bh;
|
|
int ofs;
|
|
u64 block, dummy;
|
|
|
|
bh = omfs_get_bucket(dir, name, namelen, &ofs);
|
|
if (!bh)
|
|
return ERR_PTR(-EIO);
|
|
|
|
block = be64_to_cpu(*((__be64 *) &bh->b_data[ofs]));
|
|
brelse(bh);
|
|
|
|
return omfs_scan_list(dir, block, name, namelen, &dummy);
|
|
}
|
|
|
|
int omfs_make_empty(struct inode *inode, struct super_block *sb)
|
|
{
|
|
struct omfs_sb_info *sbi = OMFS_SB(sb);
|
|
struct buffer_head *bh;
|
|
struct omfs_inode *oi;
|
|
|
|
bh = omfs_bread(sb, inode->i_ino);
|
|
if (!bh)
|
|
return -ENOMEM;
|
|
|
|
memset(bh->b_data, 0, sizeof(struct omfs_inode));
|
|
|
|
if (S_ISDIR(inode->i_mode)) {
|
|
memset(&bh->b_data[OMFS_DIR_START], 0xff,
|
|
sbi->s_sys_blocksize - OMFS_DIR_START);
|
|
} else
|
|
omfs_make_empty_table(bh, OMFS_EXTENT_START);
|
|
|
|
oi = (struct omfs_inode *) bh->b_data;
|
|
oi->i_head.h_self = cpu_to_be64(inode->i_ino);
|
|
oi->i_sibling = ~cpu_to_be64(0ULL);
|
|
|
|
mark_buffer_dirty(bh);
|
|
brelse(bh);
|
|
return 0;
|
|
}
|
|
|
|
static int omfs_add_link(struct dentry *dentry, struct inode *inode)
|
|
{
|
|
struct inode *dir = d_inode(dentry->d_parent);
|
|
const char *name = dentry->d_name.name;
|
|
int namelen = dentry->d_name.len;
|
|
struct omfs_inode *oi;
|
|
struct buffer_head *bh;
|
|
u64 block;
|
|
__be64 *entry;
|
|
int ofs;
|
|
|
|
/* just prepend to head of queue in proper bucket */
|
|
bh = omfs_get_bucket(dir, name, namelen, &ofs);
|
|
if (!bh)
|
|
goto out;
|
|
|
|
entry = (__be64 *) &bh->b_data[ofs];
|
|
block = be64_to_cpu(*entry);
|
|
*entry = cpu_to_be64(inode->i_ino);
|
|
mark_buffer_dirty(bh);
|
|
brelse(bh);
|
|
|
|
/* now set the sibling and parent pointers on the new inode */
|
|
bh = omfs_bread(dir->i_sb, inode->i_ino);
|
|
if (!bh)
|
|
goto out;
|
|
|
|
oi = (struct omfs_inode *) bh->b_data;
|
|
memcpy(oi->i_name, name, namelen);
|
|
memset(oi->i_name + namelen, 0, OMFS_NAMELEN - namelen);
|
|
oi->i_sibling = cpu_to_be64(block);
|
|
oi->i_parent = cpu_to_be64(dir->i_ino);
|
|
mark_buffer_dirty(bh);
|
|
brelse(bh);
|
|
|
|
inode_set_ctime_current(dir);
|
|
|
|
/* mark affected inodes dirty to rebuild checksums */
|
|
mark_inode_dirty(dir);
|
|
mark_inode_dirty(inode);
|
|
return 0;
|
|
out:
|
|
return -ENOMEM;
|
|
}
|
|
|
|
static int omfs_delete_entry(struct dentry *dentry)
|
|
{
|
|
struct inode *dir = d_inode(dentry->d_parent);
|
|
struct inode *dirty;
|
|
const char *name = dentry->d_name.name;
|
|
int namelen = dentry->d_name.len;
|
|
struct omfs_inode *oi;
|
|
struct buffer_head *bh, *bh2;
|
|
__be64 *entry, next;
|
|
u64 block, prev;
|
|
int ofs;
|
|
int err = -ENOMEM;
|
|
|
|
/* delete the proper node in the bucket's linked list */
|
|
bh = omfs_get_bucket(dir, name, namelen, &ofs);
|
|
if (!bh)
|
|
goto out;
|
|
|
|
entry = (__be64 *) &bh->b_data[ofs];
|
|
block = be64_to_cpu(*entry);
|
|
|
|
bh2 = omfs_scan_list(dir, block, name, namelen, &prev);
|
|
if (IS_ERR(bh2)) {
|
|
err = PTR_ERR(bh2);
|
|
goto out_free_bh;
|
|
}
|
|
|
|
oi = (struct omfs_inode *) bh2->b_data;
|
|
next = oi->i_sibling;
|
|
brelse(bh2);
|
|
|
|
if (prev != ~0) {
|
|
/* found in middle of list, get list ptr */
|
|
brelse(bh);
|
|
bh = omfs_bread(dir->i_sb, prev);
|
|
if (!bh)
|
|
goto out;
|
|
|
|
oi = (struct omfs_inode *) bh->b_data;
|
|
entry = &oi->i_sibling;
|
|
}
|
|
|
|
*entry = next;
|
|
mark_buffer_dirty(bh);
|
|
|
|
if (prev != ~0) {
|
|
dirty = omfs_iget(dir->i_sb, prev);
|
|
if (!IS_ERR(dirty)) {
|
|
mark_inode_dirty(dirty);
|
|
iput(dirty);
|
|
}
|
|
}
|
|
|
|
err = 0;
|
|
out_free_bh:
|
|
brelse(bh);
|
|
out:
|
|
return err;
|
|
}
|
|
|
|
static int omfs_dir_is_empty(struct inode *inode)
|
|
{
|
|
int nbuckets = (inode->i_size - OMFS_DIR_START) / 8;
|
|
struct buffer_head *bh;
|
|
u64 *ptr;
|
|
int i;
|
|
|
|
bh = omfs_bread(inode->i_sb, inode->i_ino);
|
|
|
|
if (!bh)
|
|
return 0;
|
|
|
|
ptr = (u64 *) &bh->b_data[OMFS_DIR_START];
|
|
|
|
for (i = 0; i < nbuckets; i++, ptr++)
|
|
if (*ptr != ~0)
|
|
break;
|
|
|
|
brelse(bh);
|
|
return *ptr != ~0;
|
|
}
|
|
|
|
static int omfs_remove(struct inode *dir, struct dentry *dentry)
|
|
{
|
|
struct inode *inode = d_inode(dentry);
|
|
int ret;
|
|
|
|
|
|
if (S_ISDIR(inode->i_mode) &&
|
|
!omfs_dir_is_empty(inode))
|
|
return -ENOTEMPTY;
|
|
|
|
ret = omfs_delete_entry(dentry);
|
|
if (ret)
|
|
return ret;
|
|
|
|
clear_nlink(inode);
|
|
mark_inode_dirty(inode);
|
|
mark_inode_dirty(dir);
|
|
return 0;
|
|
}
|
|
|
|
static int omfs_add_node(struct inode *dir, struct dentry *dentry, umode_t mode)
|
|
{
|
|
int err;
|
|
struct inode *inode = omfs_new_inode(dir, mode);
|
|
|
|
if (IS_ERR(inode))
|
|
return PTR_ERR(inode);
|
|
|
|
err = omfs_make_empty(inode, dir->i_sb);
|
|
if (err)
|
|
goto out_free_inode;
|
|
|
|
err = omfs_add_link(dentry, inode);
|
|
if (err)
|
|
goto out_free_inode;
|
|
|
|
d_instantiate(dentry, inode);
|
|
return 0;
|
|
|
|
out_free_inode:
|
|
iput(inode);
|
|
return err;
|
|
}
|
|
|
|
static struct dentry *omfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
|
|
struct dentry *dentry, umode_t mode)
|
|
{
|
|
return ERR_PTR(omfs_add_node(dir, dentry, mode));
|
|
}
|
|
|
|
static int omfs_create(struct mnt_idmap *idmap, struct inode *dir,
|
|
struct dentry *dentry, umode_t mode)
|
|
{
|
|
return omfs_add_node(dir, dentry, mode | S_IFREG);
|
|
}
|
|
|
|
static struct dentry *omfs_lookup(struct inode *dir, struct dentry *dentry,
|
|
unsigned int flags)
|
|
{
|
|
struct buffer_head *bh;
|
|
struct inode *inode = NULL;
|
|
|
|
if (dentry->d_name.len > OMFS_NAMELEN)
|
|
return ERR_PTR(-ENAMETOOLONG);
|
|
|
|
bh = omfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len);
|
|
if (!IS_ERR(bh)) {
|
|
struct omfs_inode *oi = (struct omfs_inode *)bh->b_data;
|
|
ino_t ino = be64_to_cpu(oi->i_head.h_self);
|
|
brelse(bh);
|
|
inode = omfs_iget(dir->i_sb, ino);
|
|
} else if (bh != ERR_PTR(-ENOENT)) {
|
|
inode = ERR_CAST(bh);
|
|
}
|
|
return d_splice_alias(inode, dentry);
|
|
}
|
|
|
|
/* sanity check block's self pointer */
|
|
int omfs_is_bad(struct omfs_sb_info *sbi, struct omfs_header *header,
|
|
u64 fsblock)
|
|
{
|
|
int is_bad;
|
|
u64 ino = be64_to_cpu(header->h_self);
|
|
is_bad = ((ino != fsblock) || (ino < sbi->s_root_ino) ||
|
|
(ino > sbi->s_num_blocks));
|
|
|
|
if (is_bad)
|
|
printk(KERN_WARNING "omfs: bad hash chain detected\n");
|
|
|
|
return is_bad;
|
|
}
|
|
|
|
static bool omfs_fill_chain(struct inode *dir, struct dir_context *ctx,
|
|
u64 fsblock, int hindex)
|
|
{
|
|
/* follow chain in this bucket */
|
|
while (fsblock != ~0) {
|
|
struct buffer_head *bh = omfs_bread(dir->i_sb, fsblock);
|
|
struct omfs_inode *oi;
|
|
u64 self;
|
|
unsigned char d_type;
|
|
|
|
if (!bh)
|
|
return true;
|
|
|
|
oi = (struct omfs_inode *) bh->b_data;
|
|
if (omfs_is_bad(OMFS_SB(dir->i_sb), &oi->i_head, fsblock)) {
|
|
brelse(bh);
|
|
return true;
|
|
}
|
|
|
|
self = fsblock;
|
|
fsblock = be64_to_cpu(oi->i_sibling);
|
|
|
|
/* skip visited nodes */
|
|
if (hindex) {
|
|
hindex--;
|
|
brelse(bh);
|
|
continue;
|
|
}
|
|
|
|
d_type = (oi->i_type == OMFS_DIR) ? DT_DIR : DT_REG;
|
|
|
|
if (!dir_emit(ctx, oi->i_name,
|
|
strnlen(oi->i_name, OMFS_NAMELEN),
|
|
self, d_type)) {
|
|
brelse(bh);
|
|
return false;
|
|
}
|
|
brelse(bh);
|
|
ctx->pos++;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
static int omfs_rename(struct mnt_idmap *idmap, struct inode *old_dir,
|
|
struct dentry *old_dentry, struct inode *new_dir,
|
|
struct dentry *new_dentry, unsigned int flags)
|
|
{
|
|
struct inode *new_inode = d_inode(new_dentry);
|
|
struct inode *old_inode = d_inode(old_dentry);
|
|
int err;
|
|
|
|
if (flags & ~RENAME_NOREPLACE)
|
|
return -EINVAL;
|
|
|
|
if (new_inode) {
|
|
/* overwriting existing file/dir */
|
|
err = omfs_remove(new_dir, new_dentry);
|
|
if (err)
|
|
goto out;
|
|
}
|
|
|
|
/* since omfs locates files by name, we need to unlink _before_
|
|
* adding the new link or we won't find the old one */
|
|
err = omfs_delete_entry(old_dentry);
|
|
if (err)
|
|
goto out;
|
|
|
|
mark_inode_dirty(old_dir);
|
|
err = omfs_add_link(new_dentry, old_inode);
|
|
if (err)
|
|
goto out;
|
|
|
|
inode_set_ctime_current(old_inode);
|
|
mark_inode_dirty(old_inode);
|
|
out:
|
|
return err;
|
|
}
|
|
|
|
static int omfs_readdir(struct file *file, struct dir_context *ctx)
|
|
{
|
|
struct inode *dir = file_inode(file);
|
|
struct buffer_head *bh;
|
|
__be64 *p;
|
|
unsigned int hchain, hindex;
|
|
int nbuckets;
|
|
|
|
if (ctx->pos >> 32)
|
|
return -EINVAL;
|
|
|
|
if (ctx->pos < 1 << 20) {
|
|
if (!dir_emit_dots(file, ctx))
|
|
return 0;
|
|
ctx->pos = 1 << 20;
|
|
}
|
|
|
|
nbuckets = (dir->i_size - OMFS_DIR_START) / 8;
|
|
|
|
/* high 12 bits store bucket + 1 and low 20 bits store hash index */
|
|
hchain = (ctx->pos >> 20) - 1;
|
|
hindex = ctx->pos & 0xfffff;
|
|
|
|
bh = omfs_bread(dir->i_sb, dir->i_ino);
|
|
if (!bh)
|
|
return -EINVAL;
|
|
|
|
p = (__be64 *)(bh->b_data + OMFS_DIR_START) + hchain;
|
|
|
|
for (; hchain < nbuckets; hchain++) {
|
|
__u64 fsblock = be64_to_cpu(*p++);
|
|
if (!omfs_fill_chain(dir, ctx, fsblock, hindex))
|
|
break;
|
|
hindex = 0;
|
|
ctx->pos = (hchain+2) << 20;
|
|
}
|
|
brelse(bh);
|
|
return 0;
|
|
}
|
|
|
|
const struct inode_operations omfs_dir_inops = {
|
|
.lookup = omfs_lookup,
|
|
.mkdir = omfs_mkdir,
|
|
.rename = omfs_rename,
|
|
.create = omfs_create,
|
|
.unlink = omfs_remove,
|
|
.rmdir = omfs_remove,
|
|
};
|
|
|
|
const struct file_operations omfs_dir_operations = {
|
|
.read = generic_read_dir,
|
|
.iterate_shared = omfs_readdir,
|
|
.llseek = generic_file_llseek,
|
|
};
|