Commit 96385742 authored by Kent Overstreet's avatar Kent Overstreet Committed by Kent Overstreet

bcachefs: Factor out fs-common.c

This refactoring makes the code easier to understand by separating the
bcachefs btree transactional code from the linux VFS code - but more
importantly, it's also to share code with the fuse port.
Signed-off-by: default avatarKent Overstreet <kent.overstreet@linux.dev>
parent 73501ab8
......@@ -27,6 +27,7 @@ bcachefs-y := \
error.o \
extents.o \
fs.o \
fs-common.o \
fs-ioctl.o \
fs-io.o \
fsck.o \
......
......@@ -138,10 +138,10 @@ static struct bkey_i_dirent *dirent_create_key(struct btree_trans *trans,
return dirent;
}
int __bch2_dirent_create(struct btree_trans *trans,
u64 dir_inum, const struct bch_hash_info *hash_info,
u8 type, const struct qstr *name, u64 dst_inum,
int flags)
int bch2_dirent_create(struct btree_trans *trans,
u64 dir_inum, const struct bch_hash_info *hash_info,
u8 type, const struct qstr *name, u64 dst_inum,
int flags)
{
struct bkey_i_dirent *dirent;
int ret;
......@@ -155,16 +155,6 @@ int __bch2_dirent_create(struct btree_trans *trans,
dir_inum, &dirent->k_i, flags);
}
int bch2_dirent_create(struct bch_fs *c, u64 dir_inum,
const struct bch_hash_info *hash_info,
u8 type, const struct qstr *name, u64 dst_inum,
u64 *journal_seq, int flags)
{
return bch2_trans_do(c, journal_seq, flags,
__bch2_dirent_create(&trans, dir_inum, hash_info,
type, name, dst_inum, flags));
}
static void dirent_copy_target(struct bkey_i_dirent *dst,
struct bkey_s_c_dirent src)
{
......@@ -172,23 +162,22 @@ static void dirent_copy_target(struct bkey_i_dirent *dst,
dst->v.d_type = src.v->d_type;
}
static struct bpos bch2_dirent_pos(struct bch_inode_info *inode,
const struct qstr *name)
{
return POS(inode->v.i_ino, bch2_dirent_hash(&inode->ei_str_hash, name));
}
int bch2_dirent_rename(struct btree_trans *trans,
struct bch_inode_info *src_dir, const struct qstr *src_name,
struct bch_inode_info *dst_dir, const struct qstr *dst_name,
enum bch_rename_mode mode)
u64 src_dir, struct bch_hash_info *src_hash,
u64 dst_dir, struct bch_hash_info *dst_hash,
const struct qstr *src_name, u64 *src_inum,
const struct qstr *dst_name, u64 *dst_inum,
enum bch_rename_mode mode)
{
struct btree_iter *src_iter, *dst_iter;
struct bkey_s_c old_src, old_dst;
struct bkey_i_dirent *new_src = NULL, *new_dst = NULL;
struct bpos dst_pos = bch2_dirent_pos(dst_dir, dst_name);
struct bpos dst_pos =
POS(dst_dir, bch2_dirent_hash(dst_hash, dst_name));
int ret;
*src_inum = *dst_inum = 0;
/*
* Lookup dst:
*
......@@ -198,24 +187,25 @@ int bch2_dirent_rename(struct btree_trans *trans,
*/
dst_iter = mode == BCH_RENAME
? bch2_hash_hole(trans, bch2_dirent_hash_desc,
&dst_dir->ei_str_hash,
dst_dir->v.i_ino, dst_name)
dst_hash, dst_dir, dst_name)
: bch2_hash_lookup(trans, bch2_dirent_hash_desc,
&dst_dir->ei_str_hash,
dst_dir->v.i_ino, dst_name,
dst_hash, dst_dir, dst_name,
BTREE_ITER_INTENT);
if (IS_ERR(dst_iter))
return PTR_ERR(dst_iter);
old_dst = bch2_btree_iter_peek_slot(dst_iter);
if (mode != BCH_RENAME)
*dst_inum = le64_to_cpu(bkey_s_c_to_dirent(old_dst).v->d_inum);
/* Lookup src: */
src_iter = bch2_hash_lookup(trans, bch2_dirent_hash_desc,
&src_dir->ei_str_hash,
src_dir->v.i_ino, src_name,
src_hash, src_dir, src_name,
BTREE_ITER_INTENT);
if (IS_ERR(src_iter))
return PTR_ERR(src_iter);
old_src = bch2_btree_iter_peek_slot(src_iter);
*src_inum = le64_to_cpu(bkey_s_c_to_dirent(old_src).v->d_inum);
/* Create new dst key: */
new_dst = dirent_create_key(trans, 0, dst_name, 0);
......@@ -269,8 +259,7 @@ int bch2_dirent_rename(struct btree_trans *trans,
} else {
/* Check if we need a whiteout to delete src: */
ret = bch2_hash_needs_whiteout(trans, bch2_dirent_hash_desc,
&src_dir->ei_str_hash,
src_iter);
src_hash, src_iter);
if (ret < 0)
return ret;
......@@ -284,12 +273,12 @@ int bch2_dirent_rename(struct btree_trans *trans,
return 0;
}
int __bch2_dirent_delete(struct btree_trans *trans, u64 dir_inum,
const struct bch_hash_info *hash_info,
const struct qstr *name)
int bch2_dirent_delete_at(struct btree_trans *trans,
const struct bch_hash_info *hash_info,
struct btree_iter *iter)
{
return bch2_hash_delete(trans, bch2_dirent_hash_desc, hash_info,
dir_inum, name);
return bch2_hash_delete_at(trans, bch2_dirent_hash_desc,
hash_info, iter);
}
int bch2_dirent_delete(struct bch_fs *c, u64 dir_inum,
......@@ -300,7 +289,17 @@ int bch2_dirent_delete(struct bch_fs *c, u64 dir_inum,
return bch2_trans_do(c, journal_seq,
BTREE_INSERT_ATOMIC|
BTREE_INSERT_NOFAIL,
__bch2_dirent_delete(&trans, dir_inum, hash_info, name));
bch2_hash_delete(&trans, bch2_dirent_hash_desc, hash_info,
dir_inum, name));
}
struct btree_iter *
__bch2_dirent_lookup_trans(struct btree_trans *trans, u64 dir_inum,
const struct bch_hash_info *hash_info,
const struct qstr *name)
{
return bch2_hash_lookup(trans, bch2_dirent_hash_desc,
hash_info, dir_inum, name, 0);
}
u64 bch2_dirent_lookup(struct bch_fs *c, u64 dir_inum,
......@@ -314,8 +313,7 @@ u64 bch2_dirent_lookup(struct bch_fs *c, u64 dir_inum,
bch2_trans_init(&trans, c, 0, 0);
iter = bch2_hash_lookup(&trans, bch2_dirent_hash_desc,
hash_info, dir_inum, name, 0);
iter = __bch2_dirent_lookup_trans(&trans, dir_inum, hash_info, name);
if (IS_ERR(iter)) {
BUG_ON(PTR_ERR(iter) == -EINTR);
goto out;
......@@ -349,16 +347,8 @@ int bch2_empty_dir_trans(struct btree_trans *trans, u64 dir_inum)
return ret;
}
int bch2_empty_dir(struct bch_fs *c, u64 dir_inum)
int bch2_readdir(struct bch_fs *c, u64 inum, struct dir_context *ctx)
{
return bch2_trans_do(c, NULL, 0,
bch2_empty_dir_trans(&trans, dir_inum));
}
int bch2_readdir(struct bch_fs *c, struct file *file,
struct dir_context *ctx)
{
struct bch_inode_info *inode = file_bch_inode(file);
struct btree_trans trans;
struct btree_iter *iter;
struct bkey_s_c k;
......@@ -366,22 +356,19 @@ int bch2_readdir(struct bch_fs *c, struct file *file,
unsigned len;
int ret;
if (!dir_emit_dots(file, ctx))
return 0;
bch2_trans_init(&trans, c, 0, 0);
for_each_btree_key(&trans, iter, BTREE_ID_DIRENTS,
POS(inode->v.i_ino, ctx->pos), 0, k, ret) {
POS(inum, ctx->pos), 0, k, ret) {
if (k.k->type != KEY_TYPE_dirent)
continue;
dirent = bkey_s_c_to_dirent(k);
if (bkey_cmp(k.k->p, POS(inode->v.i_ino, ctx->pos)) < 0)
if (bkey_cmp(k.k->p, POS(inum, ctx->pos)) < 0)
continue;
if (k.k->p.inode > inode->v.i_ino)
if (k.k->p.inode > inum)
break;
len = bch2_dirent_name_bytes(dirent);
......
......@@ -29,15 +29,13 @@ static inline unsigned dirent_val_u64s(unsigned len)
sizeof(u64));
}
int __bch2_dirent_create(struct btree_trans *, u64,
const struct bch_hash_info *, u8,
const struct qstr *, u64, int);
int bch2_dirent_create(struct bch_fs *c, u64, const struct bch_hash_info *,
u8, const struct qstr *, u64, u64 *, int);
int __bch2_dirent_delete(struct btree_trans *, u64,
const struct bch_hash_info *,
const struct qstr *);
int bch2_dirent_create(struct btree_trans *, u64,
const struct bch_hash_info *, u8,
const struct qstr *, u64, int);
int bch2_dirent_delete_at(struct btree_trans *,
const struct bch_hash_info *,
struct btree_iter *);
int bch2_dirent_delete(struct bch_fs *, u64, const struct bch_hash_info *,
const struct qstr *, u64 *);
......@@ -48,15 +46,20 @@ enum bch_rename_mode {
};
int bch2_dirent_rename(struct btree_trans *,
struct bch_inode_info *, const struct qstr *,
struct bch_inode_info *, const struct qstr *,
u64, struct bch_hash_info *,
u64, struct bch_hash_info *,
const struct qstr *, u64 *,
const struct qstr *, u64 *,
enum bch_rename_mode);
struct btree_iter *
__bch2_dirent_lookup_trans(struct btree_trans *, u64,
const struct bch_hash_info *,
const struct qstr *);
u64 bch2_dirent_lookup(struct bch_fs *, u64, const struct bch_hash_info *,
const struct qstr *);
int bch2_empty_dir_trans(struct btree_trans *, u64);
int bch2_empty_dir(struct bch_fs *, u64);
int bch2_readdir(struct bch_fs *, struct file *, struct dir_context *);
int bch2_readdir(struct bch_fs *, u64, struct dir_context *);
#endif /* _BCACHEFS_DIRENT_H */
// SPDX-License-Identifier: GPL-2.0
#include "bcachefs.h"
#include "acl.h"
#include "btree_update.h"
#include "dirent.h"
#include "fs-common.h"
#include "inode.h"
#include "xattr.h"
#include <linux/posix_acl.h>
int bch2_create_trans(struct btree_trans *trans, u64 dir_inum,
struct bch_inode_unpacked *dir_u,
struct bch_inode_unpacked *new_inode,
const struct qstr *name,
uid_t uid, gid_t gid, umode_t mode, dev_t rdev,
struct posix_acl *default_acl,
struct posix_acl *acl)
{
struct bch_fs *c = trans->c;
struct btree_iter *dir_iter;
struct bch_hash_info hash = bch2_hash_info_init(c, new_inode);
u64 now = bch2_current_time(trans->c);
int ret;
dir_iter = bch2_inode_peek(trans, dir_u, dir_inum,
name ? BTREE_ITER_INTENT : 0);
if (IS_ERR(dir_iter))
return PTR_ERR(dir_iter);
bch2_inode_init_late(new_inode, now, uid, gid, mode, rdev, dir_u);
if (!name)
new_inode->bi_flags |= BCH_INODE_UNLINKED;
ret = bch2_inode_create(trans, new_inode,
BLOCKDEV_INODE_MAX, 0,
&c->unused_inode_hint);
if (ret)
return ret;
if (default_acl) {
ret = bch2_set_acl_trans(trans, new_inode, &hash,
default_acl, ACL_TYPE_DEFAULT);
if (ret)
return ret;
}
if (acl) {
ret = bch2_set_acl_trans(trans, new_inode, &hash,
acl, ACL_TYPE_ACCESS);
if (ret)
return ret;
}
if (name) {
struct bch_hash_info dir_hash = bch2_hash_info_init(c, dir_u);
dir_u->bi_mtime = dir_u->bi_ctime = now;
if (S_ISDIR(new_inode->bi_mode))
dir_u->bi_nlink++;
ret = bch2_inode_write(trans, dir_iter, dir_u);
if (ret)
return ret;
ret = bch2_dirent_create(trans, dir_inum, &dir_hash,
mode_to_type(new_inode->bi_mode),
name, new_inode->bi_inum,
BCH_HASH_SET_MUST_CREATE);
if (ret)
return ret;
}
return 0;
}
int bch2_link_trans(struct btree_trans *trans,
u64 dir_inum,
u64 inum, struct bch_inode_unpacked *inode_u,
const struct qstr *name)
{
struct btree_iter *dir_iter, *inode_iter;
struct bch_inode_unpacked dir_u;
struct bch_hash_info dir_hash;
u64 now = bch2_current_time(trans->c);
dir_iter = bch2_inode_peek(trans, &dir_u, dir_inum, 0);
if (IS_ERR(dir_iter))
return PTR_ERR(dir_iter);
inode_iter = bch2_inode_peek(trans, inode_u, inum, BTREE_ITER_INTENT);
if (IS_ERR(inode_iter))
return PTR_ERR(inode_iter);
dir_hash = bch2_hash_info_init(trans->c, &dir_u);
inode_u->bi_ctime = now;
bch2_inode_nlink_inc(inode_u);
return bch2_dirent_create(trans, dir_inum, &dir_hash,
mode_to_type(inode_u->bi_mode),
name, inum, BCH_HASH_SET_MUST_CREATE) ?:
bch2_inode_write(trans, inode_iter, inode_u);
}
int bch2_unlink_trans(struct btree_trans *trans,
u64 dir_inum, struct bch_inode_unpacked *dir_u,
struct bch_inode_unpacked *inode_u,
const struct qstr *name)
{
struct btree_iter *dir_iter, *dirent_iter, *inode_iter;
struct bch_hash_info dir_hash;
u64 inum, now = bch2_current_time(trans->c);
struct bkey_s_c k;
dir_iter = bch2_inode_peek(trans, dir_u, dir_inum, BTREE_ITER_INTENT);
if (IS_ERR(dir_iter))
return PTR_ERR(dir_iter);
dir_hash = bch2_hash_info_init(trans->c, dir_u);
dirent_iter = __bch2_dirent_lookup_trans(trans, dir_inum,
&dir_hash, name);
if (IS_ERR(dirent_iter))
return PTR_ERR(dirent_iter);
k = bch2_btree_iter_peek_slot(dirent_iter);
inum = le64_to_cpu(bkey_s_c_to_dirent(k).v->d_inum);
inode_iter = bch2_inode_peek(trans, inode_u, inum, BTREE_ITER_INTENT);
if (IS_ERR(inode_iter))
return PTR_ERR(inode_iter);
dir_u->bi_mtime = dir_u->bi_ctime = inode_u->bi_ctime = now;
dir_u->bi_nlink -= S_ISDIR(inode_u->bi_mode);
bch2_inode_nlink_dec(inode_u);
return (S_ISDIR(inode_u->bi_mode)
? bch2_empty_dir_trans(trans, inum)
: 0) ?:
bch2_dirent_delete_at(trans, &dir_hash, dirent_iter) ?:
bch2_inode_write(trans, dir_iter, dir_u) ?:
bch2_inode_write(trans, inode_iter, inode_u);
}
bool bch2_reinherit_attrs(struct bch_inode_unpacked *dst_u,
struct bch_inode_unpacked *src_u)
{
u64 src, dst;
unsigned id;
bool ret = false;
for (id = 0; id < Inode_opt_nr; id++) {
if (dst_u->bi_fields_set & (1 << id))
continue;
src = bch2_inode_opt_get(src_u, id);
dst = bch2_inode_opt_get(dst_u, id);
if (src == dst)
continue;
bch2_inode_opt_set(dst_u, id, src);
ret = true;
}
return ret;
}
int bch2_rename_trans(struct btree_trans *trans,
u64 src_dir, struct bch_inode_unpacked *src_dir_u,
u64 dst_dir, struct bch_inode_unpacked *dst_dir_u,
struct bch_inode_unpacked *src_inode_u,
struct bch_inode_unpacked *dst_inode_u,
const struct qstr *src_name,
const struct qstr *dst_name,
enum bch_rename_mode mode)
{
struct btree_iter *src_dir_iter, *dst_dir_iter = NULL;
struct btree_iter *src_inode_iter, *dst_inode_iter = NULL;
struct bch_hash_info src_hash, dst_hash;
u64 src_inode, dst_inode, now = bch2_current_time(trans->c);
int ret;
src_dir_iter = bch2_inode_peek(trans, src_dir_u, src_dir,
BTREE_ITER_INTENT);
if (IS_ERR(src_dir_iter))
return PTR_ERR(src_dir_iter);
src_hash = bch2_hash_info_init(trans->c, src_dir_u);
if (dst_dir != src_dir) {
dst_dir_iter = bch2_inode_peek(trans, dst_dir_u, dst_dir,
BTREE_ITER_INTENT);
if (IS_ERR(dst_dir_iter))
return PTR_ERR(dst_dir_iter);
dst_hash = bch2_hash_info_init(trans->c, dst_dir_u);
} else {
dst_dir_u = src_dir_u;
dst_hash = src_hash;
}
ret = bch2_dirent_rename(trans,
src_dir, &src_hash,
dst_dir, &dst_hash,
src_name, &src_inode,
dst_name, &dst_inode,
mode);
if (ret)
return ret;
src_inode_iter = bch2_inode_peek(trans, src_inode_u, src_inode,
BTREE_ITER_INTENT);
if (IS_ERR(src_inode_iter))
return PTR_ERR(src_inode_iter);
if (dst_inode) {
dst_inode_iter = bch2_inode_peek(trans, dst_inode_u, dst_inode,
BTREE_ITER_INTENT);
if (IS_ERR(dst_inode_iter))
return PTR_ERR(dst_inode_iter);
}
if (mode == BCH_RENAME_OVERWRITE) {
if (S_ISDIR(src_inode_u->bi_mode) !=
S_ISDIR(dst_inode_u->bi_mode))
return -ENOTDIR;
if (S_ISDIR(dst_inode_u->bi_mode) &&
bch2_empty_dir_trans(trans, dst_inode))
return -ENOTEMPTY;
}
if (bch2_reinherit_attrs(src_inode_u, dst_dir_u) &&
S_ISDIR(src_inode_u->bi_mode))
return -EXDEV;
if (mode == BCH_RENAME_EXCHANGE &&
bch2_reinherit_attrs(dst_inode_u, src_dir_u) &&
S_ISDIR(dst_inode_u->bi_mode))
return -EXDEV;
if (S_ISDIR(src_inode_u->bi_mode)) {
src_dir_u->bi_nlink--;
dst_dir_u->bi_nlink++;
}
if (dst_inode && S_ISDIR(dst_inode_u->bi_mode)) {
dst_dir_u->bi_nlink--;
src_dir_u->bi_nlink += mode == BCH_RENAME_EXCHANGE;
}
if (mode == BCH_RENAME_OVERWRITE)
bch2_inode_nlink_dec(dst_inode_u);
src_dir_u->bi_mtime = now;
src_dir_u->bi_ctime = now;
if (src_dir != dst_dir) {
dst_dir_u->bi_mtime = now;
dst_dir_u->bi_ctime = now;
}
src_inode_u->bi_ctime = now;
if (dst_inode)
dst_inode_u->bi_ctime = now;
return bch2_inode_write(trans, src_dir_iter, src_dir_u) ?:
(src_dir != dst_dir
? bch2_inode_write(trans, dst_dir_iter, dst_dir_u)
: 0 ) ?:
bch2_inode_write(trans, src_inode_iter, src_inode_u) ?:
(dst_inode
? bch2_inode_write(trans, dst_inode_iter, dst_inode_u)
: 0 );
}
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef _BCACHEFS_FS_COMMON_H
#define _BCACHEFS_FS_COMMON_H
struct posix_acl;
int bch2_create_trans(struct btree_trans *, u64,
struct bch_inode_unpacked *,
struct bch_inode_unpacked *,
const struct qstr *,
uid_t, gid_t, umode_t, dev_t,
struct posix_acl *,
struct posix_acl *);
int bch2_link_trans(struct btree_trans *,
u64,
u64, struct bch_inode_unpacked *,
const struct qstr *);
int bch2_unlink_trans(struct btree_trans *,
u64, struct bch_inode_unpacked *,
struct bch_inode_unpacked *,
const struct qstr *);
int bch2_rename_trans(struct btree_trans *,
u64, struct bch_inode_unpacked *,
u64, struct bch_inode_unpacked *,
struct bch_inode_unpacked *,
struct bch_inode_unpacked *,
const struct qstr *,
const struct qstr *,
enum bch_rename_mode);
bool bch2_reinherit_attrs(struct bch_inode_unpacked *,
struct bch_inode_unpacked *);
#endif /* _BCACHEFS_FS_COMMON_H */
......@@ -5,6 +5,7 @@
#include "chardev.h"
#include "dirent.h"
#include "fs.h"
#include "fs-common.h"
#include "fs-ioctl.h"
#include "quota.h"
......@@ -164,6 +165,15 @@ static int bch2_ioc_fssetxattr(struct bch_fs *c,
return ret;
}
static int bch2_reinherit_attrs_fn(struct bch_inode_info *inode,
struct bch_inode_unpacked *bi,
void *p)
{
struct bch_inode_info *dir = p;
return !bch2_reinherit_attrs(bi, &dir->ei_inode);
}
static int bch2_ioc_reinherit_attrs(struct bch_fs *c,
struct file *file,
struct bch_inode_info *src,
......
This diff is collapsed.
......@@ -103,11 +103,6 @@ static inline struct bch_inode_info *file_bch_inode(struct file *file)
return to_bch_ei(file_inode(file));
}
static inline u8 mode_to_type(umode_t mode)
{
return (mode >> 12) & 15;
}
static inline bool inode_attr_changing(struct bch_inode_info *dir,
struct bch_inode_info *inode,
enum inode_opt_id id)
......@@ -162,17 +157,9 @@ void bch2_inode_update_after_write(struct bch_fs *,
struct bch_inode_info *,
struct bch_inode_unpacked *,
unsigned);
int __must_check bch2_write_inode_trans(struct btree_trans *,
struct bch_inode_info *,
struct bch_inode_unpacked *,
inode_set_fn, void *);
int __must_check bch2_write_inode(struct bch_fs *, struct bch_inode_info *,
inode_set_fn, void *, unsigned);
int bch2_reinherit_attrs_fn(struct bch_inode_info *,
struct bch_inode_unpacked *,
void *);
void bch2_vfs_exit(void);
int bch2_vfs_init(void);
......
......@@ -4,7 +4,7 @@
#include "btree_update.h"
#include "dirent.h"
#include "error.h"
#include "fs.h"
#include "fs-common.h"
#include "fsck.h"
#include "inode.h"
#include "keylist.h"
......@@ -80,9 +80,7 @@ static int reattach_inode(struct bch_fs *c,
struct bch_inode_unpacked *lostfound_inode,
u64 inum)
{
struct bch_hash_info lostfound_hash_info =
bch2_hash_info_init(c, lostfound_inode);
struct bkey_inode_buf packed;
struct bch_inode_unpacked inode_u;
char name_buf[20];
struct qstr name;
int ret;
......@@ -90,30 +88,14 @@ static int reattach_inode(struct bch_fs *c,
snprintf(name_buf, sizeof(name_buf), "%llu", inum);
name = (struct qstr) QSTR(name_buf);
lostfound_inode->bi_nlink++;
bch2_inode_pack(&packed, lostfound_inode);
ret = bch2_btree_insert(c, BTREE_ID_INODES, &packed.inode.k_i,
NULL, NULL,
BTREE_INSERT_NOFAIL|
BTREE_INSERT_LAZY_RW);
if (ret) {
bch_err(c, "error %i reattaching inode %llu while updating lost+found",
ret, inum);
return ret;
}
ret = bch2_trans_do(c, NULL,
BTREE_INSERT_ATOMIC|
BTREE_INSERT_LAZY_RW,
bch2_link_trans(&trans, lostfound_inode->bi_inum,
inum, &inode_u, &name));
if (ret)
bch_err(c, "error %i reattaching inode %llu", ret, inum);
ret = bch2_dirent_create(c, lostfound_inode->bi_inum,
&lostfound_hash_info,
DT_DIR, &name, inum, NULL,
BTREE_INSERT_NOFAIL|
BTREE_INSERT_LAZY_RW);
if (ret) {
bch_err(c, "error %i reattaching inode %llu while creating new dirent",
ret, inum);
return ret;
}
return ret;
}
......@@ -758,7 +740,7 @@ static int check_root(struct bch_fs *c, struct bch_inode_unpacked *root_inode)
fsck_err:
return ret;
create_root:
bch2_inode_init(c, root_inode, 0, 0, S_IFDIR|S_IRWXU|S_IRUGO|S_IXUGO,
bch2_inode_init(c, root_inode, 0, 0, S_IFDIR|0755,
0, NULL);
root_inode->bi_inum = BCACHEFS_ROOT_INO;
......@@ -778,7 +760,6 @@ static int check_lostfound(struct bch_fs *c,
struct qstr lostfound = QSTR("lost+found");
struct bch_hash_info root_hash_info =
bch2_hash_info_init(c, root_inode);
struct bkey_inode_buf packed;
u64 inum;
int ret;
......@@ -806,33 +787,20 @@ static int check_lostfound(struct bch_fs *c,
fsck_err:
return ret;
create_lostfound:
root_inode->bi_nlink++;
bch2_inode_pack(&packed, root_inode);
ret = bch2_btree_insert(c, BTREE_ID_INODES, &packed.inode.k_i,
NULL, NULL,
BTREE_INSERT_NOFAIL|
BTREE_INSERT_LAZY_RW);
if (ret)
return ret;
bch2_inode_init(c, lostfound_inode, 0, 0, S_IFDIR|S_IRWXU|S_IRUGO|S_IXUGO,
0, root_inode);
ret = bch2_inode_create(c, lostfound_inode, BLOCKDEV_INODE_MAX, 0,
&c->unused_inode_hint);
bch2_inode_init_early(c, lostfound_inode);
ret = bch2_trans_do(c, NULL,
BTREE_INSERT_ATOMIC|
BTREE_INSERT_NOFAIL|
BTREE_INSERT_LAZY_RW,
bch2_create_trans(&trans,
BCACHEFS_ROOT_INO, root_inode,
lostfound_inode, &lostfound,
0, 0, S_IFDIR|0755, 0, NULL, NULL));
if (ret)
return ret;
ret = bch2_dirent_create(c, BCACHEFS_ROOT_INO, &root_hash_info, DT_DIR,
&lostfound, lostfound_inode->bi_inum, NULL,
BTREE_INSERT_NOFAIL|
BTREE_INSERT_LAZY_RW);
if (ret)
return ret;
bch_err(c, "error creating lost+found: %i", ret);
return 0;
return ret;
}
struct inode_bitmap {
......
......@@ -297,11 +297,9 @@ void bch2_inode_generation_to_text(struct printbuf *out, struct bch_fs *c,
pr_buf(out, "generation: %u", le32_to_cpu(gen.v->bi_generation));
}
void bch2_inode_init(struct bch_fs *c, struct bch_inode_unpacked *inode_u,
uid_t uid, gid_t gid, umode_t mode, dev_t rdev,
struct bch_inode_unpacked *parent)
void bch2_inode_init_early(struct bch_fs *c,
struct bch_inode_unpacked *inode_u)
{
s64 now = bch2_current_time(c);
enum bch_str_hash_type str_hash =
bch2_str_hash_opt_to_type(c, c->opts.str_hash);
......@@ -311,7 +309,12 @@ void bch2_inode_init(struct bch_fs *c, struct bch_inode_unpacked *inode_u,
inode_u->bi_flags |= str_hash << INODE_STR_HASH_OFFSET;
get_random_bytes(&inode_u->bi_hash_seed,
sizeof(inode_u->bi_hash_seed));
}
void bch2_inode_init_late(struct bch_inode_unpacked *inode_u, u64 now,
uid_t uid, gid_t gid, umode_t mode, dev_t rdev,
struct bch_inode_unpacked *parent)
{
inode_u->bi_mode = mode;
inode_u->bi_uid = uid;
inode_u->bi_gid = gid;
......@@ -321,6 +324,12 @@ void bch2_inode_init(struct bch_fs *c, struct bch_inode_unpacked *inode_u,
inode_u->bi_ctime = now;
inode_u->bi_otime = now;
if (parent && parent->bi_mode & S_ISGID) {
inode_u->bi_gid = parent->bi_gid;
if (S_ISDIR(mode))
inode_u->bi_mode |= S_ISGID;
}
if (parent) {
#define x(_name, ...) inode_u->bi_##_name = parent->bi_##_name;
BCH_INODE_OPTS()
......@@ -328,6 +337,15 @@ void bch2_inode_init(struct bch_fs *c, struct bch_inode_unpacked *inode_u,
}
}
void bch2_inode_init(struct bch_fs *c, struct bch_inode_unpacked *inode_u,
uid_t uid, gid_t gid, umode_t mode, dev_t rdev,
struct bch_inode_unpacked *parent)
{
bch2_inode_init_early(c, inode_u);
bch2_inode_init_late(inode_u, bch2_current_time(c),
uid, gid, mode, rdev, parent);
}
static inline u32 bkey_generation(struct bkey_s_c k)
{
switch (k.k->type) {
......@@ -340,9 +358,9 @@ static inline u32 bkey_generation(struct bkey_s_c k)
}
}
int __bch2_inode_create(struct btree_trans *trans,
struct bch_inode_unpacked *inode_u,
u64 min, u64 max, u64 *hint)
int bch2_inode_create(struct btree_trans *trans,
struct bch_inode_unpacked *inode_u,
u64 min, u64 max, u64 *hint)
{
struct bch_fs *c = trans->c;
struct bkey_inode_buf *inode_p;
......@@ -408,13 +426,6 @@ int __bch2_inode_create(struct btree_trans *trans,
return -ENOSPC;
}
int bch2_inode_create(struct bch_fs *c, struct bch_inode_unpacked *inode_u,
u64 min, u64 max, u64 *hint)
{
return bch2_trans_do(c, NULL, BTREE_INSERT_ATOMIC,
__bch2_inode_create(&trans, inode_u, min, max, hint));
}
int bch2_inode_rm(struct bch_fs *c, u64 inode_nr)
{
struct btree_trans trans;
......
......@@ -51,14 +51,17 @@ struct btree_iter *bch2_inode_peek(struct btree_trans *,
int bch2_inode_write(struct btree_trans *, struct btree_iter *,
struct bch_inode_unpacked *);
void bch2_inode_init_early(struct bch_fs *,
struct bch_inode_unpacked *);
void bch2_inode_init_late(struct bch_inode_unpacked *, u64,
uid_t, gid_t, umode_t, dev_t,
struct bch_inode_unpacked *);
void bch2_inode_init(struct bch_fs *, struct bch_inode_unpacked *,
uid_t, gid_t, umode_t, dev_t,
struct bch_inode_unpacked *);
int __bch2_inode_create(struct btree_trans *,
struct bch_inode_unpacked *,
u64, u64, u64 *);
int bch2_inode_create(struct bch_fs *, struct bch_inode_unpacked *,
int bch2_inode_create(struct btree_trans *,
struct bch_inode_unpacked *,
u64, u64, u64 *);
int bch2_inode_rm(struct bch_fs *, u64);
......@@ -108,6 +111,11 @@ static inline u64 bch2_inode_opt_get(struct bch_inode_unpacked *inode,
}
}
static inline u8 mode_to_type(umode_t mode)
{
return (mode >> 12) & 15;
}
/* i_nlink: */
static inline unsigned nlink_bias(umode_t mode)
......
......@@ -10,6 +10,7 @@
#include "dirent.h"
#include "ec.h"
#include "error.h"
#include "fs-common.h"
#include "fsck.h"
#include "journal_io.h"
#include "journal_reclaim.h"
......@@ -952,7 +953,6 @@ int bch2_fs_initialize(struct bch_fs *c)
{
struct bch_inode_unpacked root_inode, lostfound_inode;
struct bkey_inode_buf packed_inode;
struct bch_hash_info root_hash_info;
struct qstr lostfound = QSTR("lost+found");
const char *err = "cannot allocate memory";
struct bch_dev *ca;
......@@ -997,7 +997,6 @@ int bch2_fs_initialize(struct bch_fs *c)
bch2_inode_init(c, &root_inode, 0, 0,
S_IFDIR|S_IRWXU|S_IRUGO|S_IXUGO, 0, NULL);
root_inode.bi_inum = BCACHEFS_ROOT_INO;
root_inode.bi_nlink++; /* lost+found */
bch2_inode_pack(&packed_inode, &root_inode);
err = "error creating root directory";
......@@ -1007,24 +1006,15 @@ int bch2_fs_initialize(struct bch_fs *c)
if (ret)
goto err;
bch2_inode_init(c, &lostfound_inode, 0, 0,
S_IFDIR|S_IRWXU|S_IRUGO|S_IXUGO, 0,
&root_inode);
lostfound_inode.bi_inum = BCACHEFS_ROOT_INO + 1;
bch2_inode_pack(&packed_inode, &lostfound_inode);
bch2_inode_init_early(c, &lostfound_inode);
err = "error creating lost+found";
ret = bch2_btree_insert(c, BTREE_ID_INODES,
&packed_inode.inode.k_i,
NULL, NULL, 0);
if (ret)
goto err;
root_hash_info = bch2_hash_info_init(c, &root_inode);
ret = bch2_dirent_create(c, BCACHEFS_ROOT_INO, &root_hash_info, DT_DIR,
&lostfound, lostfound_inode.bi_inum, NULL,
BTREE_INSERT_NOFAIL);
ret = bch2_trans_do(c, NULL, BTREE_INSERT_ATOMIC,
bch2_create_trans(&trans, BCACHEFS_ROOT_INO,
&root_inode, &lostfound_inode,
&lostfound,
0, 0, 0755, 0,
NULL, NULL));
if (ret)
goto err;
......
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