bcachefs: bch2_inode_peek()/bch2_inode_write()

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
This commit is contained in:
Kent Overstreet 2019-10-01 16:51:57 -04:00 committed by Kent Overstreet
parent 618b9e575b
commit 58677a1d40
5 changed files with 198 additions and 192 deletions

View File

@ -281,51 +281,54 @@ int bch2_set_acl_trans(struct btree_trans *trans,
return ret == -ENOENT ? 0 : ret;
}
static int inode_update_for_set_acl_fn(struct bch_inode_info *inode,
struct bch_inode_unpacked *bi,
void *p)
{
struct bch_fs *c = inode->v.i_sb->s_fs_info;
umode_t mode = (unsigned long) p;
bi->bi_ctime = bch2_current_time(c);
bi->bi_mode = mode;
return 0;
}
int bch2_set_acl(struct mnt_idmap *idmap,
struct dentry *dentry,
struct posix_acl *acl, int type)
struct posix_acl *_acl, int type)
{
struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
struct bch_fs *c = inode->v.i_sb->s_fs_info;
struct btree_trans trans;
struct btree_iter *inode_iter;
struct bch_inode_unpacked inode_u;
umode_t mode = inode->v.i_mode;
struct posix_acl *acl;
umode_t mode;
int ret;
mutex_lock(&inode->ei_update_lock);
bch2_trans_init(&trans, c, 0, 0);
retry:
bch2_trans_begin(&trans);
acl = _acl;
if (type == ACL_TYPE_ACCESS && acl) {
inode_iter = bch2_inode_peek(&trans, &inode_u, inode->v.i_ino,
BTREE_ITER_INTENT);
ret = PTR_ERR_OR_ZERO(inode_iter);
if (ret)
goto btree_err;
mode = inode_u.bi_mode;
if (type == ACL_TYPE_ACCESS) {
ret = posix_acl_update_mode(idmap, &inode->v, &mode, &acl);
if (ret)
goto err;
}
retry:
bch2_trans_begin(&trans);
ret = bch2_set_acl_trans(&trans,
&inode->ei_inode,
&inode->ei_str_hash,
acl, type) ?:
bch2_write_inode_trans(&trans, inode, &inode_u,
inode_update_for_set_acl_fn,
(void *)(unsigned long) mode) ?:
ret = bch2_set_acl_trans(&trans, &inode_u,
&inode->ei_str_hash,
acl, type);
if (ret)
goto btree_err;
inode_u.bi_ctime = bch2_current_time(c);
inode_u.bi_mode = mode;
ret = bch2_inode_write(&trans, inode_iter, &inode_u) ?:
bch2_trans_commit(&trans, NULL,
&inode->ei_journal_seq,
BTREE_INSERT_ATOMIC|
BTREE_INSERT_NOUNLOCK);
btree_err:
if (ret == -EINTR)
goto retry;
if (unlikely(ret))

View File

@ -241,11 +241,13 @@ static void i_sectors_acct(struct bch_fs *c, struct bch_inode_info *inode,
static int sum_sector_overwrites(struct btree_trans *trans,
struct btree_iter *extent_iter,
struct bkey_i *new, bool *allocating,
struct bkey_i *new,
bool may_allocate,
s64 *delta)
{
struct btree_iter *iter;
struct bkey_s_c old;
int ret = 0;
*delta = 0;
@ -253,21 +255,13 @@ static int sum_sector_overwrites(struct btree_trans *trans,
if (IS_ERR(iter))
return PTR_ERR(iter);
old = bch2_btree_iter_peek_slot(iter);
while (1) {
/*
* should not be possible to get an error here, since we're
* carefully not advancing past @new and thus whatever leaf node
* @_iter currently points to:
*/
BUG_ON(bkey_err(old));
if (allocating &&
!*allocating &&
for_each_btree_key_continue(iter, BTREE_ITER_SLOTS, old, ret) {
if (!may_allocate &&
bch2_bkey_nr_ptrs_allocated(old) <
bch2_bkey_nr_dirty_ptrs(bkey_i_to_s_c(new)))
*allocating = true;
bch2_bkey_nr_dirty_ptrs(bkey_i_to_s_c(new))) {
ret = -ENOSPC;
break;
}
*delta += (min(new->k.p.offset,
old.k->p.offset) -
@ -278,12 +272,10 @@ static int sum_sector_overwrites(struct btree_trans *trans,
if (bkey_cmp(old.k->p, new->k.p) >= 0)
break;
old = bch2_btree_iter_next_slot(iter);
}
bch2_trans_iter_put(trans, iter);
return 0;
return ret;
}
int bch2_extent_update(struct btree_trans *trans,
@ -301,9 +293,7 @@ int bch2_extent_update(struct btree_trans *trans,
struct btree_iter *inode_iter = NULL;
struct bch_inode_unpacked inode_u;
struct bkey_inode_buf inode_p;
bool allocating = false;
bool extended = false;
bool inode_locked = false;
s64 i_sectors_delta;
int ret;
@ -315,15 +305,11 @@ int bch2_extent_update(struct btree_trans *trans,
if (ret)
return ret;
ret = sum_sector_overwrites(trans, extent_iter,
k, &allocating,
&i_sectors_delta);
ret = sum_sector_overwrites(trans, extent_iter, k,
may_allocate, &i_sectors_delta);
if (ret)
return ret;
if (!may_allocate && allocating)
return -ENOSPC;
bch2_trans_update(trans, extent_iter, k);
new_i_size = min(k->k.p.offset << 9, new_i_size);
@ -331,29 +317,28 @@ int bch2_extent_update(struct btree_trans *trans,
/* XXX: inode->i_size locking */
if (i_sectors_delta ||
new_i_size > inode->ei_inode.bi_size) {
inode_iter = bch2_trans_get_iter(trans,
BTREE_ID_INODES,
POS(k->k.p.inode, 0),
BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
inode_iter = bch2_inode_peek(trans, &inode_u,
k->k.p.inode, BTREE_ITER_INTENT);
if (IS_ERR(inode_iter))
return PTR_ERR(inode_iter);
ret = bch2_btree_iter_traverse(inode_iter);
if (ret)
goto err;
inode_u = inode->ei_inode;
inode_u.bi_sectors += i_sectors_delta;
/* XXX: this is slightly suspect */
/*
* XXX: can BCH_INODE_I_SIZE_DIRTY be true here? i.e. can we
* race with truncate?
*/
if (!(inode_u.bi_flags & BCH_INODE_I_SIZE_DIRTY) &&
new_i_size > inode_u.bi_size) {
inode_u.bi_size = new_i_size;
extended = true;
}
bch2_inode_pack(&inode_p, &inode_u);
bch2_trans_update(trans, inode_iter, &inode_p.inode.k_i);
if (i_sectors_delta || extended) {
bch2_inode_pack(&inode_p, &inode_u);
bch2_trans_update(trans, inode_iter,
&inode_p.inode.k_i);
}
}
ret = bch2_trans_commit(trans, disk_res,
@ -365,33 +350,25 @@ int bch2_extent_update(struct btree_trans *trans,
if (ret)
goto err;
inode->ei_inode.bi_sectors += i_sectors_delta;
EBUG_ON(i_sectors_delta &&
inode->ei_inode.bi_sectors != inode_u.bi_sectors);
if (extended) {
inode->ei_inode.bi_size = new_i_size;
if (direct) {
spin_lock(&inode->v.i_lock);
if (new_i_size > inode->v.i_size)
i_size_write(&inode->v, new_i_size);
spin_unlock(&inode->v.i_lock);
}
if (i_sectors_delta || extended) {
inode->ei_inode.bi_sectors = inode_u.bi_sectors;
inode->ei_inode.bi_size = inode_u.bi_size;
}
if (direct)
i_sectors_acct(c, inode, quota_res, i_sectors_delta);
if (direct && extended) {
spin_lock(&inode->v.i_lock);
if (new_i_size > inode->v.i_size)
i_size_write(&inode->v, new_i_size);
spin_unlock(&inode->v.i_lock);
}
if (total_delta)
*total_delta += i_sectors_delta;
err:
if (!IS_ERR_OR_NULL(inode_iter))
bch2_trans_iter_put(trans, inode_iter);
if (inode_locked)
mutex_unlock(&inode->ei_update_lock);
return ret;
}

View File

@ -155,41 +155,19 @@ int __must_check bch2_write_inode_trans(struct btree_trans *trans,
void *p)
{
struct btree_iter *iter = NULL;
struct bkey_inode_buf *inode_p;
struct bkey_s_c k;
int ret = 0;
iter = bch2_trans_get_iter(trans, BTREE_ID_INODES,
POS(inode->v.i_ino, 0),
BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
if (IS_ERR(iter))
return PTR_ERR(iter);
k = bch2_btree_iter_peek_slot(iter);
ret = bkey_err(k);
iter = bch2_inode_peek(trans, inode_u, inode->v.i_ino,
BTREE_ITER_INTENT);
ret = PTR_ERR_OR_ZERO(iter);
if (ret)
return ret;
if (k.k->type != KEY_TYPE_inode)
return -EIO;
ret = bch2_inode_unpack(bkey_s_c_to_inode(k), inode_u);
ret = set ? set(inode, inode_u, p) : 0;
if (ret)
return ret;
if (set) {
ret = set(inode, inode_u, p);
if (ret)
return ret;
}
inode_p = bch2_trans_kmalloc(trans, sizeof(*inode_p));
if (IS_ERR(inode_p))
return PTR_ERR(inode_p);
bch2_inode_pack(inode_p, inode_u);
bch2_trans_update(trans, iter, &inode_p->inode.k_i);
return 0;
return bch2_inode_write(trans, iter, inode_u);
}
int __must_check bch2_write_inode(struct bch_fs *c,
@ -531,23 +509,13 @@ static int bch2_create(struct mnt_idmap *idmap,
return 0;
}
static int inode_update_for_link_fn(struct bch_inode_info *inode,
struct bch_inode_unpacked *bi,
void *p)
{
struct bch_fs *c = inode->v.i_sb->s_fs_info;
bi->bi_ctime = bch2_current_time(c);
bch2_inode_nlink_inc(bi);
return 0;
}
static int __bch2_link(struct bch_fs *c,
struct bch_inode_info *inode,
struct bch_inode_info *dir,
struct dentry *dentry)
{
struct btree_trans trans;
struct btree_iter *inode_iter;
struct bch_inode_unpacked inode_u;
int ret;
@ -555,21 +523,30 @@ static int __bch2_link(struct bch_fs *c,
bch2_trans_init(&trans, c, 4, 1024);
retry:
bch2_trans_begin(&trans);
ret = __bch2_dirent_create(&trans, dir->v.i_ino,
&dir->ei_str_hash,
mode_to_type(inode->v.i_mode),
&dentry->d_name,
inode->v.i_ino,
BCH_HASH_SET_MUST_CREATE) ?:
bch2_write_inode_trans(&trans, inode, &inode_u,
inode_update_for_link_fn,
NULL) ?:
bch2_trans_commit(&trans, NULL,
&inode->ei_journal_seq,
BTREE_INSERT_ATOMIC|
BTREE_INSERT_NOUNLOCK);
BCH_HASH_SET_MUST_CREATE);
if (ret)
goto err;
inode_iter = bch2_inode_peek(&trans, &inode_u, inode->v.i_ino,
BTREE_ITER_INTENT);
ret = PTR_ERR_OR_ZERO(inode_iter);
if (ret)
goto err;
inode_u.bi_ctime = bch2_current_time(c);
bch2_inode_nlink_inc(&inode_u);
ret = bch2_inode_write(&trans, inode_iter, &inode_u) ?:
bch2_trans_commit(&trans, NULL,
&inode->ei_journal_seq,
BTREE_INSERT_ATOMIC|
BTREE_INSERT_NOUNLOCK);
err:
if (ret == -EINTR)
goto retry;
@ -600,36 +577,12 @@ static int bch2_link(struct dentry *old_dentry, struct inode *vdir,
return 0;
}
static int inode_update_dir_for_unlink_fn(struct bch_inode_info *inode,
struct bch_inode_unpacked *bi,
void *p)
{
struct bch_fs *c = inode->v.i_sb->s_fs_info;
struct bch_inode_info *unlink_inode = p;
bi->bi_mtime = bi->bi_ctime = bch2_current_time(c);
bi->bi_nlink -= S_ISDIR(unlink_inode->v.i_mode);
return 0;
}
static int inode_update_for_unlink_fn(struct bch_inode_info *inode,
struct bch_inode_unpacked *bi,
void *p)
{
struct bch_fs *c = inode->v.i_sb->s_fs_info;
bi->bi_ctime = bch2_current_time(c);
bch2_inode_nlink_dec(bi);
return 0;
}
static int bch2_unlink(struct inode *vdir, struct dentry *dentry)
{
struct bch_fs *c = vdir->i_sb->s_fs_info;
struct bch_inode_info *dir = to_bch_ei(vdir);
struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
struct btree_iter *dir_iter, *inode_iter;
struct bch_inode_unpacked dir_u, inode_u;
struct btree_trans trans;
int ret;
@ -641,25 +594,42 @@ retry:
ret = __bch2_dirent_delete(&trans, dir->v.i_ino,
&dir->ei_str_hash,
&dentry->d_name) ?:
bch2_write_inode_trans(&trans, dir, &dir_u,
inode_update_dir_for_unlink_fn,
inode) ?:
bch2_write_inode_trans(&trans, inode, &inode_u,
inode_update_for_unlink_fn,
NULL) ?:
&dentry->d_name);
if (ret)
goto btree_err;
dir_iter = bch2_inode_peek(&trans, &dir_u, dir->v.i_ino,
BTREE_ITER_INTENT);
ret = PTR_ERR_OR_ZERO(dir_iter);
if (ret)
goto btree_err;
inode_iter = bch2_inode_peek(&trans, &inode_u, inode->v.i_ino,
BTREE_ITER_INTENT);
ret = PTR_ERR_OR_ZERO(inode_iter);
if (ret)
goto btree_err;
dir_u.bi_mtime = dir_u.bi_ctime = inode_u.bi_ctime =
bch2_current_time(c);
dir_u.bi_nlink -= S_ISDIR(inode_u.bi_mode);
bch2_inode_nlink_dec(&inode_u);
ret = bch2_inode_write(&trans, dir_iter, &dir_u) ?:
bch2_inode_write(&trans, inode_iter, &inode_u) ?:
bch2_trans_commit(&trans, NULL,
&dir->ei_journal_seq,
BTREE_INSERT_ATOMIC|
BTREE_INSERT_NOUNLOCK|
BTREE_INSERT_NOFAIL);
btree_err:
if (ret == -EINTR)
goto retry;
if (ret)
goto err;
if (dir->ei_journal_seq > inode->ei_journal_seq)
inode->ei_journal_seq = dir->ei_journal_seq;
journal_seq_copy(inode, dir->ei_journal_seq);
bch2_inode_update_after_write(c, dir, &dir_u,
ATTR_MTIME|ATTR_CTIME);
@ -953,67 +923,60 @@ err:
return ret;
}
struct inode_write_setattr {
struct iattr *attr;
struct mnt_idmap *idmap;
};
static int inode_update_for_setattr_fn(struct bch_inode_info *inode,
struct bch_inode_unpacked *bi,
void *p)
static void bch2_setattr_copy(struct mnt_idmap *idmap,
struct bch_inode_info *inode,
struct bch_inode_unpacked *bi,
struct iattr *attr)
{
struct bch_fs *c = inode->v.i_sb->s_fs_info;
struct inode_write_setattr *s = p;
unsigned int ia_valid = s->attr->ia_valid;
unsigned int ia_valid = attr->ia_valid;
if (ia_valid & ATTR_UID)
bi->bi_uid = from_kuid(i_user_ns(&inode->v), s->attr->ia_uid);
bi->bi_uid = from_kuid(i_user_ns(&inode->v), attr->ia_uid);
if (ia_valid & ATTR_GID)
bi->bi_gid = from_kgid(i_user_ns(&inode->v), s->attr->ia_gid);
bi->bi_gid = from_kgid(i_user_ns(&inode->v), attr->ia_gid);
if (ia_valid & ATTR_ATIME)
bi->bi_atime = timespec_to_bch2_time(c, s->attr->ia_atime);
bi->bi_atime = timespec_to_bch2_time(c, attr->ia_atime);
if (ia_valid & ATTR_MTIME)
bi->bi_mtime = timespec_to_bch2_time(c, s->attr->ia_mtime);
bi->bi_mtime = timespec_to_bch2_time(c, attr->ia_mtime);
if (ia_valid & ATTR_CTIME)
bi->bi_ctime = timespec_to_bch2_time(c, s->attr->ia_ctime);
bi->bi_ctime = timespec_to_bch2_time(c, attr->ia_ctime);
if (ia_valid & ATTR_MODE) {
umode_t mode = s->attr->ia_mode;
umode_t mode = attr->ia_mode;
kgid_t gid = ia_valid & ATTR_GID
? s->attr->ia_gid
? attr->ia_gid
: inode->v.i_gid;
if (!in_group_p(gid) &&
!capable_wrt_inode_uidgid(s->idmap, &inode->v, CAP_FSETID))
!capable_wrt_inode_uidgid(idmap, &inode->v, CAP_FSETID))
mode &= ~S_ISGID;
bi->bi_mode = mode;
}
return 0;
}
static int bch2_setattr_nonsize(struct mnt_idmap *idmap,
struct bch_inode_info *inode,
struct iattr *iattr)
struct iattr *attr)
{
struct bch_fs *c = inode->v.i_sb->s_fs_info;
struct bch_qid qid;
struct btree_trans trans;
struct btree_iter *inode_iter;
struct bch_inode_unpacked inode_u;
struct posix_acl *acl = NULL;
struct inode_write_setattr s = { iattr, idmap };
int ret;
mutex_lock(&inode->ei_update_lock);
qid = inode->ei_qid;
if (iattr->ia_valid & ATTR_UID)
qid.q[QTYP_USR] = from_kuid(i_user_ns(&inode->v), iattr->ia_uid);
if (attr->ia_valid & ATTR_UID)
qid.q[QTYP_USR] = from_kuid(i_user_ns(&inode->v), attr->ia_uid);
if (iattr->ia_valid & ATTR_GID)
qid.q[QTYP_GRP] = from_kgid(i_user_ns(&inode->v), iattr->ia_gid);
if (attr->ia_valid & ATTR_GID)
qid.q[QTYP_GRP] = from_kgid(i_user_ns(&inode->v), attr->ia_gid);
ret = bch2_fs_quota_transfer(c, inode, qid, ~0,
KEY_TYPE_QUOTA_PREALLOC);
@ -1026,22 +989,33 @@ retry:
kfree(acl);
acl = NULL;
ret = bch2_write_inode_trans(&trans, inode, &inode_u,
inode_update_for_setattr_fn, &s) ?:
(iattr->ia_valid & ATTR_MODE
? bch2_acl_chmod(&trans, inode, iattr->ia_mode, &acl)
: 0) ?:
inode_iter = bch2_inode_peek(&trans, &inode_u, inode->v.i_ino,
BTREE_ITER_INTENT);
ret = PTR_ERR_OR_ZERO(inode_iter);
if (ret)
goto btree_err;
bch2_setattr_copy(idmap, inode, &inode_u, attr);
if (attr->ia_valid & ATTR_MODE) {
ret = bch2_acl_chmod(&trans, inode, inode_u.bi_mode, &acl);
if (ret)
goto btree_err;
}
ret = bch2_inode_write(&trans, inode_iter, &inode_u) ?:
bch2_trans_commit(&trans, NULL,
&inode->ei_journal_seq,
BTREE_INSERT_ATOMIC|
BTREE_INSERT_NOUNLOCK|
BTREE_INSERT_NOFAIL);
btree_err:
if (ret == -EINTR)
goto retry;
if (unlikely(ret))
goto err_trans;
bch2_inode_update_after_write(c, inode, &inode_u, iattr->ia_valid);
bch2_inode_update_after_write(c, inode, &inode_u, attr->ia_valid);
if (acl)
set_cached_acl(&inode->v, ACL_TYPE_ACCESS, acl);

View File

@ -181,6 +181,53 @@ int bch2_inode_unpack(struct bkey_s_c_inode inode,
return 0;
}
struct btree_iter *bch2_inode_peek(struct btree_trans *trans,
struct bch_inode_unpacked *inode,
u64 inum, unsigned flags)
{
struct btree_iter *iter;
struct bkey_s_c k;
int ret;
iter = bch2_trans_get_iter(trans, BTREE_ID_INODES, POS(inum, 0),
BTREE_ITER_SLOTS|flags);
if (IS_ERR(iter))
return iter;
k = bch2_btree_iter_peek_slot(iter);
ret = bkey_err(k);
if (ret)
goto err;
ret = k.k->type == KEY_TYPE_inode ? 0 : -EIO;
if (ret)
goto err;
ret = bch2_inode_unpack(bkey_s_c_to_inode(k), inode);
if (ret)
goto err;
return iter;
err:
bch2_trans_iter_put(trans, iter);
return ERR_PTR(ret);
}
int bch2_inode_write(struct btree_trans *trans,
struct btree_iter *iter,
struct bch_inode_unpacked *inode)
{
struct bkey_inode_buf *inode_p;
inode_p = bch2_trans_kmalloc(trans, sizeof(*inode_p));
if (IS_ERR(inode_p))
return PTR_ERR(inode_p);
bch2_inode_pack(inode_p, inode);
bch2_trans_update(trans, iter, &inode_p->inode.k_i);
return 0;
}
const char *bch2_inode_invalid(const struct bch_fs *c, struct bkey_s_c k)
{
struct bkey_s_c_inode inode = bkey_s_c_to_inode(k);

View File

@ -46,6 +46,11 @@ struct bkey_inode_buf {
void bch2_inode_pack(struct bkey_inode_buf *, const struct bch_inode_unpacked *);
int bch2_inode_unpack(struct bkey_s_c_inode, struct bch_inode_unpacked *);
struct btree_iter *bch2_inode_peek(struct btree_trans *,
struct bch_inode_unpacked *, u64, unsigned);
int bch2_inode_write(struct btree_trans *, struct btree_iter *,
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 *);