2017-03-17 06:18:50 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
#ifndef NO_BCACHEFS_FS
|
|
|
|
|
|
|
|
#include "bcachefs.h"
|
|
|
|
#include "acl.h"
|
2020-12-17 20:08:58 +00:00
|
|
|
#include "bkey_buf.h"
|
2017-03-17 06:18:50 +00:00
|
|
|
#include "btree_update.h"
|
|
|
|
#include "buckets.h"
|
|
|
|
#include "chardev.h"
|
|
|
|
#include "dirent.h"
|
2022-07-18 23:42:58 +00:00
|
|
|
#include "errcode.h"
|
2017-03-17 06:18:50 +00:00
|
|
|
#include "extents.h"
|
|
|
|
#include "fs.h"
|
2019-10-02 22:35:36 +00:00
|
|
|
#include "fs-common.h"
|
2017-03-17 06:18:50 +00:00
|
|
|
#include "fs-io.h"
|
|
|
|
#include "fs-ioctl.h"
|
2023-08-03 22:18:21 +00:00
|
|
|
#include "fs-io-buffered.h"
|
|
|
|
#include "fs-io-direct.h"
|
|
|
|
#include "fs-io-pagecache.h"
|
2017-03-17 06:18:50 +00:00
|
|
|
#include "fsck.h"
|
|
|
|
#include "inode.h"
|
2023-09-10 22:05:17 +00:00
|
|
|
#include "io_read.h"
|
2017-03-17 06:18:50 +00:00
|
|
|
#include "journal.h"
|
|
|
|
#include "keylist.h"
|
|
|
|
#include "quota.h"
|
2023-08-16 20:54:33 +00:00
|
|
|
#include "snapshot.h"
|
2017-03-17 06:18:50 +00:00
|
|
|
#include "super.h"
|
|
|
|
#include "xattr.h"
|
2024-05-31 02:35:09 +00:00
|
|
|
#include "trace.h"
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
#include <linux/aio.h>
|
|
|
|
#include <linux/backing-dev.h>
|
|
|
|
#include <linux/exportfs.h>
|
|
|
|
#include <linux/fiemap.h>
|
2024-05-28 04:36:11 +00:00
|
|
|
#include <linux/fs_context.h>
|
2017-03-17 06:18:50 +00:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/pagemap.h>
|
|
|
|
#include <linux/posix_acl.h>
|
|
|
|
#include <linux/random.h>
|
|
|
|
#include <linux/seq_file.h>
|
|
|
|
#include <linux/statfs.h>
|
2021-05-13 20:08:47 +00:00
|
|
|
#include <linux/string.h>
|
2017-03-17 06:18:50 +00:00
|
|
|
#include <linux/xattr.h>
|
|
|
|
|
|
|
|
static struct kmem_cache *bch2_inode_cache;
|
|
|
|
|
2021-11-06 04:03:40 +00:00
|
|
|
static void bch2_vfs_inode_init(struct btree_trans *, subvol_inum,
|
2017-03-17 06:18:50 +00:00
|
|
|
struct bch_inode_info *,
|
2021-10-27 17:05:56 +00:00
|
|
|
struct bch_inode_unpacked *,
|
|
|
|
struct bch_subvolume *);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2021-11-06 04:03:40 +00:00
|
|
|
void bch2_inode_update_after_write(struct btree_trans *trans,
|
2017-03-17 06:18:50 +00:00
|
|
|
struct bch_inode_info *inode,
|
|
|
|
struct bch_inode_unpacked *bi,
|
|
|
|
unsigned fields)
|
|
|
|
{
|
2021-11-06 04:03:40 +00:00
|
|
|
struct bch_fs *c = trans->c;
|
|
|
|
|
|
|
|
BUG_ON(bi->bi_inum != inode->v.i_ino);
|
|
|
|
|
2024-06-08 20:46:58 +00:00
|
|
|
bch2_assert_pos_locked(trans, BTREE_ID_inodes, POS(0, bi->bi_inum));
|
2021-11-06 04:03:40 +00:00
|
|
|
|
2019-09-25 20:19:52 +00:00
|
|
|
set_nlink(&inode->v, bch2_inode_nlink_get(bi));
|
2017-03-17 06:18:50 +00:00
|
|
|
i_uid_write(&inode->v, bi->bi_uid);
|
|
|
|
i_gid_write(&inode->v, bi->bi_gid);
|
|
|
|
inode->v.i_mode = bi->bi_mode;
|
|
|
|
|
|
|
|
if (fields & ATTR_ATIME)
|
2023-10-30 21:09:38 +00:00
|
|
|
inode_set_atime_to_ts(&inode->v, bch2_time_to_timespec(c, bi->bi_atime));
|
2017-03-17 06:18:50 +00:00
|
|
|
if (fields & ATTR_MTIME)
|
2023-10-30 21:09:38 +00:00
|
|
|
inode_set_mtime_to_ts(&inode->v, bch2_time_to_timespec(c, bi->bi_mtime));
|
2017-03-17 06:18:50 +00:00
|
|
|
if (fields & ATTR_CTIME)
|
|
|
|
inode_set_ctime_to_ts(&inode->v, bch2_time_to_timespec(c, bi->bi_ctime));
|
|
|
|
|
|
|
|
inode->ei_inode = *bi;
|
2018-07-17 18:12:42 +00:00
|
|
|
|
|
|
|
bch2_inode_flags_to_vfs(inode);
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
2018-07-17 18:12:42 +00:00
|
|
|
int __must_check bch2_write_inode(struct bch_fs *c,
|
|
|
|
struct bch_inode_info *inode,
|
|
|
|
inode_set_fn set,
|
|
|
|
void *p, unsigned fields)
|
2017-03-17 06:18:50 +00:00
|
|
|
{
|
2023-09-12 21:16:02 +00:00
|
|
|
struct btree_trans *trans = bch2_trans_get(c);
|
2021-08-30 19:18:31 +00:00
|
|
|
struct btree_iter iter = { NULL };
|
2017-03-17 06:18:50 +00:00
|
|
|
struct bch_inode_unpacked inode_u;
|
|
|
|
int ret;
|
|
|
|
retry:
|
2023-09-12 21:16:02 +00:00
|
|
|
bch2_trans_begin(trans);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2023-09-12 21:16:02 +00:00
|
|
|
ret = bch2_inode_peek(trans, &iter, &inode_u, inode_inum(inode),
|
2024-04-07 22:05:34 +00:00
|
|
|
BTREE_ITER_intent) ?:
|
2023-09-12 21:16:02 +00:00
|
|
|
(set ? set(trans, inode, &inode_u, p) : 0) ?:
|
|
|
|
bch2_inode_write(trans, &iter, &inode_u) ?:
|
2023-11-11 21:31:50 +00:00
|
|
|
bch2_trans_commit(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* the btree node lock protects inode->ei_inode, not ei_update_lock;
|
|
|
|
* this is important for inode updates via bchfs_write_index_update
|
|
|
|
*/
|
|
|
|
if (!ret)
|
2023-09-12 21:16:02 +00:00
|
|
|
bch2_inode_update_after_write(trans, inode, &inode_u, fields);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2023-09-12 21:16:02 +00:00
|
|
|
bch2_trans_iter_exit(trans, &iter);
|
2020-04-01 21:28:39 +00:00
|
|
|
|
2022-07-18 03:06:38 +00:00
|
|
|
if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
|
2020-04-01 21:28:39 +00:00
|
|
|
goto retry;
|
|
|
|
|
2023-05-27 23:59:59 +00:00
|
|
|
bch2_fs_fatal_err_on(bch2_err_matches(ret, ENOENT), c,
|
2024-06-09 01:41:01 +00:00
|
|
|
"%s: inode %llu:%llu not found when updating",
|
2024-03-18 01:51:19 +00:00
|
|
|
bch2_err_str(ret),
|
2023-03-15 23:04:05 +00:00
|
|
|
inode_inum(inode).subvol,
|
|
|
|
inode_inum(inode).inum);
|
|
|
|
|
2023-09-12 21:16:02 +00:00
|
|
|
bch2_trans_put(trans);
|
2017-03-17 06:18:50 +00:00
|
|
|
return ret < 0 ? ret : 0;
|
|
|
|
}
|
|
|
|
|
2018-12-17 10:43:00 +00:00
|
|
|
int bch2_fs_quota_transfer(struct bch_fs *c,
|
|
|
|
struct bch_inode_info *inode,
|
|
|
|
struct bch_qid new_qid,
|
|
|
|
unsigned qtypes,
|
|
|
|
enum quota_acct_mode mode)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
qtypes &= enabled_qtypes(c);
|
|
|
|
|
|
|
|
for (i = 0; i < QTYP_NR; i++)
|
|
|
|
if (new_qid.q[i] == inode->ei_qid.q[i])
|
|
|
|
qtypes &= ~(1U << i);
|
|
|
|
|
|
|
|
if (!qtypes)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
mutex_lock(&inode->ei_quota_lock);
|
|
|
|
|
|
|
|
ret = bch2_quota_transfer(c, qtypes, new_qid,
|
|
|
|
inode->ei_qid,
|
|
|
|
inode->v.i_blocks +
|
|
|
|
inode->ei_quota_reserved,
|
|
|
|
mode);
|
|
|
|
if (!ret)
|
|
|
|
for (i = 0; i < QTYP_NR; i++)
|
|
|
|
if (qtypes & (1 << i))
|
|
|
|
inode->ei_qid.q[i] = new_qid.q[i];
|
|
|
|
|
|
|
|
mutex_unlock(&inode->ei_quota_lock);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-06-09 01:41:01 +00:00
|
|
|
static bool subvol_inum_eq(subvol_inum a, subvol_inum b)
|
2021-03-16 05:33:39 +00:00
|
|
|
{
|
2024-06-09 01:41:01 +00:00
|
|
|
return a.subvol == b.subvol && a.inum == b.inum;
|
2021-03-16 05:33:39 +00:00
|
|
|
}
|
|
|
|
|
2024-10-03 01:23:41 +00:00
|
|
|
static u32 bch2_vfs_inode_hash_fn(const void *data, u32 len, u32 seed)
|
|
|
|
{
|
|
|
|
const subvol_inum *inum = data;
|
|
|
|
|
|
|
|
return jhash(&inum->inum, sizeof(inum->inum), seed);
|
|
|
|
}
|
|
|
|
|
|
|
|
static u32 bch2_vfs_inode_obj_hash_fn(const void *data, u32 len, u32 seed)
|
|
|
|
{
|
|
|
|
const struct bch_inode_info *inode = data;
|
|
|
|
|
|
|
|
return bch2_vfs_inode_hash_fn(&inode->ei_inum, sizeof(inode->ei_inum), seed);
|
|
|
|
}
|
|
|
|
|
2024-06-09 01:41:01 +00:00
|
|
|
static int bch2_vfs_inode_cmp_fn(struct rhashtable_compare_arg *arg,
|
|
|
|
const void *obj)
|
2021-03-16 05:33:39 +00:00
|
|
|
{
|
2024-06-09 01:41:01 +00:00
|
|
|
const struct bch_inode_info *inode = obj;
|
|
|
|
const subvol_inum *v = arg->key;
|
2021-03-16 05:33:39 +00:00
|
|
|
|
2024-06-09 01:41:01 +00:00
|
|
|
return !subvol_inum_eq(inode->ei_inum, *v);
|
2021-03-16 05:33:39 +00:00
|
|
|
}
|
|
|
|
|
2024-06-09 01:41:01 +00:00
|
|
|
static const struct rhashtable_params bch2_vfs_inodes_params = {
|
|
|
|
.head_offset = offsetof(struct bch_inode_info, hash),
|
|
|
|
.key_offset = offsetof(struct bch_inode_info, ei_inum),
|
|
|
|
.key_len = sizeof(subvol_inum),
|
2024-10-03 01:23:41 +00:00
|
|
|
.hashfn = bch2_vfs_inode_hash_fn,
|
|
|
|
.obj_hashfn = bch2_vfs_inode_obj_hash_fn,
|
2024-06-09 01:41:01 +00:00
|
|
|
.obj_cmpfn = bch2_vfs_inode_cmp_fn,
|
|
|
|
.automatic_shrinking = true,
|
|
|
|
};
|
|
|
|
|
2024-10-03 01:23:41 +00:00
|
|
|
int bch2_inode_or_descendents_is_open(struct btree_trans *trans, struct bpos p)
|
2021-03-16 05:33:39 +00:00
|
|
|
{
|
2024-10-03 01:23:41 +00:00
|
|
|
struct bch_fs *c = trans->c;
|
|
|
|
struct rhashtable *ht = &c->vfs_inodes_table;
|
|
|
|
subvol_inum inum = (subvol_inum) { .inum = p.offset };
|
|
|
|
DARRAY(u32) subvols;
|
|
|
|
int ret = 0;
|
2021-03-16 05:33:39 +00:00
|
|
|
|
bcachefs: bcachefs_metadata_version_inode_has_child_snapshots
There's an inherent race in taking a snapshot while an unlinked file is
open, and then reattaching it in the child snapshot.
In the interior snapshot node the file will appear unlinked, as though
it should be deleted - it's not referenced by anything in that snapshot
- but we can't delete it, because the file data is referenced by the
child snapshot.
This was being handled incorrectly with
propagate_key_to_snapshot_leaves() - but that doesn't resolve the
fundamental inconsistency of "this file looks like it should be deleted
according to normal rules, but - ".
To fix this, we need to fix the rule for when an inode is deleted. The
previous rule, ignoring snapshots (there was no well-defined rule
for with snapshots) was:
Unlinked, non open files are deleted, either at recovery time or
during online fsck
The new rule is:
Unlinked, non open files, that do not exist in child snapshots, are
deleted.
To make this work transactionally, we add a new inode flag,
BCH_INODE_has_child_snapshot; it overrides BCH_INODE_unlinked when
considering whether to delete an inode, or put it on the deleted list.
For transactional consistency, clearing it handled by the inode trigger:
when deleting an inode we check if there are parent inodes which can now
have the BCH_INODE_has_child_snapshot flag cleared.
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
2024-09-30 02:11:37 +00:00
|
|
|
if (!test_bit(BCH_FS_started, &c->flags))
|
|
|
|
return false;
|
|
|
|
|
2024-10-03 01:23:41 +00:00
|
|
|
darray_init(&subvols);
|
|
|
|
restart_from_top:
|
bcachefs: bcachefs_metadata_version_inode_has_child_snapshots
There's an inherent race in taking a snapshot while an unlinked file is
open, and then reattaching it in the child snapshot.
In the interior snapshot node the file will appear unlinked, as though
it should be deleted - it's not referenced by anything in that snapshot
- but we can't delete it, because the file data is referenced by the
child snapshot.
This was being handled incorrectly with
propagate_key_to_snapshot_leaves() - but that doesn't resolve the
fundamental inconsistency of "this file looks like it should be deleted
according to normal rules, but - ".
To fix this, we need to fix the rule for when an inode is deleted. The
previous rule, ignoring snapshots (there was no well-defined rule
for with snapshots) was:
Unlinked, non open files are deleted, either at recovery time or
during online fsck
The new rule is:
Unlinked, non open files, that do not exist in child snapshots, are
deleted.
To make this work transactionally, we add a new inode flag,
BCH_INODE_has_child_snapshot; it overrides BCH_INODE_unlinked when
considering whether to delete an inode, or put it on the deleted list.
For transactional consistency, clearing it handled by the inode trigger:
when deleting an inode we check if there are parent inodes which can now
have the BCH_INODE_has_child_snapshot flag cleared.
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
2024-09-30 02:11:37 +00:00
|
|
|
|
2024-10-03 01:23:41 +00:00
|
|
|
/*
|
|
|
|
* Tweaked version of __rhashtable_lookup(); we need to get a list of
|
|
|
|
* subvolumes in which the given inode number is open.
|
|
|
|
*
|
|
|
|
* For this to work, we don't include the subvolume ID in the key that
|
|
|
|
* we hash - all inodes with the same inode number regardless of
|
|
|
|
* subvolume will hash to the same slot.
|
|
|
|
*
|
|
|
|
* This will be less than ideal if the same file is ever open
|
|
|
|
* simultaneously in many different snapshots:
|
|
|
|
*/
|
|
|
|
rcu_read_lock();
|
|
|
|
struct rhash_lock_head __rcu *const *bkt;
|
|
|
|
struct rhash_head *he;
|
|
|
|
unsigned int hash;
|
|
|
|
struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht);
|
|
|
|
restart:
|
|
|
|
hash = rht_key_hashfn(ht, tbl, &inum, bch2_vfs_inodes_params);
|
|
|
|
bkt = rht_bucket(tbl, hash);
|
|
|
|
do {
|
|
|
|
struct bch_inode_info *inode;
|
|
|
|
|
|
|
|
rht_for_each_entry_rcu_from(inode, he, rht_ptr_rcu(bkt), tbl, hash, hash) {
|
|
|
|
if (inode->ei_inum.inum == inum.inum) {
|
|
|
|
ret = darray_push_gfp(&subvols, inode->ei_inum.subvol,
|
|
|
|
GFP_NOWAIT|__GFP_NOWARN);
|
|
|
|
if (ret) {
|
|
|
|
rcu_read_unlock();
|
|
|
|
ret = darray_make_room(&subvols, 1);
|
|
|
|
if (ret)
|
|
|
|
goto err;
|
|
|
|
subvols.nr = 0;
|
|
|
|
goto restart_from_top;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* An object might have been moved to a different hash chain,
|
|
|
|
* while we walk along it - better check and retry.
|
|
|
|
*/
|
|
|
|
} while (he != RHT_NULLS_MARKER(bkt));
|
|
|
|
|
|
|
|
/* Ensure we see any new tables. */
|
|
|
|
smp_rmb();
|
|
|
|
|
|
|
|
tbl = rht_dereference_rcu(tbl->future_tbl, ht);
|
|
|
|
if (unlikely(tbl))
|
|
|
|
goto restart;
|
|
|
|
rcu_read_unlock();
|
|
|
|
|
|
|
|
darray_for_each(subvols, i) {
|
|
|
|
u32 snap;
|
|
|
|
ret = bch2_subvolume_get_snapshot(trans, *i, &snap);
|
|
|
|
if (ret)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
ret = bch2_snapshot_is_ancestor(c, snap, p.snapshot);
|
|
|
|
if (ret)
|
|
|
|
break;
|
bcachefs: bcachefs_metadata_version_inode_has_child_snapshots
There's an inherent race in taking a snapshot while an unlinked file is
open, and then reattaching it in the child snapshot.
In the interior snapshot node the file will appear unlinked, as though
it should be deleted - it's not referenced by anything in that snapshot
- but we can't delete it, because the file data is referenced by the
child snapshot.
This was being handled incorrectly with
propagate_key_to_snapshot_leaves() - but that doesn't resolve the
fundamental inconsistency of "this file looks like it should be deleted
according to normal rules, but - ".
To fix this, we need to fix the rule for when an inode is deleted. The
previous rule, ignoring snapshots (there was no well-defined rule
for with snapshots) was:
Unlinked, non open files are deleted, either at recovery time or
during online fsck
The new rule is:
Unlinked, non open files, that do not exist in child snapshots, are
deleted.
To make this work transactionally, we add a new inode flag,
BCH_INODE_has_child_snapshot; it overrides BCH_INODE_unlinked when
considering whether to delete an inode, or put it on the deleted list.
For transactional consistency, clearing it handled by the inode trigger:
when deleting an inode we check if there are parent inodes which can now
have the BCH_INODE_has_child_snapshot flag cleared.
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
2024-09-30 02:11:37 +00:00
|
|
|
}
|
2024-10-03 01:23:41 +00:00
|
|
|
err:
|
|
|
|
darray_exit(&subvols);
|
|
|
|
return ret;
|
|
|
|
}
|
bcachefs: bcachefs_metadata_version_inode_has_child_snapshots
There's an inherent race in taking a snapshot while an unlinked file is
open, and then reattaching it in the child snapshot.
In the interior snapshot node the file will appear unlinked, as though
it should be deleted - it's not referenced by anything in that snapshot
- but we can't delete it, because the file data is referenced by the
child snapshot.
This was being handled incorrectly with
propagate_key_to_snapshot_leaves() - but that doesn't resolve the
fundamental inconsistency of "this file looks like it should be deleted
according to normal rules, but - ".
To fix this, we need to fix the rule for when an inode is deleted. The
previous rule, ignoring snapshots (there was no well-defined rule
for with snapshots) was:
Unlinked, non open files are deleted, either at recovery time or
during online fsck
The new rule is:
Unlinked, non open files, that do not exist in child snapshots, are
deleted.
To make this work transactionally, we add a new inode flag,
BCH_INODE_has_child_snapshot; it overrides BCH_INODE_unlinked when
considering whether to delete an inode, or put it on the deleted list.
For transactional consistency, clearing it handled by the inode trigger:
when deleting an inode we check if there are parent inodes which can now
have the BCH_INODE_has_child_snapshot flag cleared.
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
2024-09-30 02:11:37 +00:00
|
|
|
|
2024-10-03 01:23:41 +00:00
|
|
|
static struct bch_inode_info *__bch2_inode_hash_find(struct bch_fs *c, subvol_inum inum)
|
2021-03-16 05:33:39 +00:00
|
|
|
{
|
2024-10-04 23:44:32 +00:00
|
|
|
return rhashtable_lookup_fast(&c->vfs_inodes_table, &inum, bch2_vfs_inodes_params);
|
2021-03-16 05:33:39 +00:00
|
|
|
}
|
|
|
|
|
2024-10-04 23:44:32 +00:00
|
|
|
static void __wait_on_freeing_inode(struct bch_fs *c,
|
|
|
|
struct bch_inode_info *inode,
|
|
|
|
subvol_inum inum)
|
2024-09-08 05:06:57 +00:00
|
|
|
{
|
2024-10-04 23:44:32 +00:00
|
|
|
wait_queue_head_t *wq;
|
2024-10-09 20:21:00 +00:00
|
|
|
struct wait_bit_queue_entry wait;
|
|
|
|
|
2024-10-04 23:44:32 +00:00
|
|
|
wq = inode_bit_waitqueue(&wait, &inode->v, __I_NEW);
|
|
|
|
prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
|
|
|
|
spin_unlock(&inode->v.i_lock);
|
|
|
|
|
|
|
|
if (__bch2_inode_hash_find(c, inum) == inode)
|
|
|
|
schedule_timeout(HZ * 10);
|
|
|
|
finish_wait(wq, &wait.wq_entry);
|
2024-06-09 01:41:01 +00:00
|
|
|
}
|
|
|
|
|
2024-08-16 16:31:53 +00:00
|
|
|
static struct bch_inode_info *bch2_inode_hash_find(struct bch_fs *c, struct btree_trans *trans,
|
|
|
|
subvol_inum inum)
|
2024-06-09 01:41:01 +00:00
|
|
|
{
|
|
|
|
struct bch_inode_info *inode;
|
|
|
|
repeat:
|
|
|
|
inode = __bch2_inode_hash_find(c, inum);
|
|
|
|
if (inode) {
|
|
|
|
spin_lock(&inode->v.i_lock);
|
|
|
|
if (!test_bit(EI_INODE_HASHED, &inode->ei_flags)) {
|
|
|
|
spin_unlock(&inode->v.i_lock);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if ((inode->v.i_state & (I_FREEING|I_WILL_FREE))) {
|
2024-08-16 16:31:53 +00:00
|
|
|
if (!trans) {
|
2024-10-04 23:44:32 +00:00
|
|
|
__wait_on_freeing_inode(c, inode, inum);
|
2024-08-16 16:31:53 +00:00
|
|
|
} else {
|
|
|
|
bch2_trans_unlock(trans);
|
2024-10-04 23:44:32 +00:00
|
|
|
__wait_on_freeing_inode(c, inode, inum);
|
2024-08-16 16:31:53 +00:00
|
|
|
int ret = bch2_trans_relock(trans);
|
|
|
|
if (ret)
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
}
|
2024-06-09 01:41:01 +00:00
|
|
|
goto repeat;
|
|
|
|
}
|
|
|
|
__iget(&inode->v);
|
|
|
|
spin_unlock(&inode->v.i_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
return inode;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bch2_inode_hash_remove(struct bch_fs *c, struct bch_inode_info *inode)
|
|
|
|
{
|
|
|
|
spin_lock(&inode->v.i_lock);
|
|
|
|
bool remove = test_and_clear_bit(EI_INODE_HASHED, &inode->ei_flags);
|
|
|
|
spin_unlock(&inode->v.i_lock);
|
|
|
|
|
|
|
|
if (remove) {
|
|
|
|
int ret = rhashtable_remove_fast(&c->vfs_inodes_table,
|
|
|
|
&inode->hash, bch2_vfs_inodes_params);
|
|
|
|
BUG_ON(ret);
|
|
|
|
inode->v.i_hash.pprev = NULL;
|
2024-10-04 23:44:32 +00:00
|
|
|
/*
|
|
|
|
* This pairs with the bch2_inode_hash_find() ->
|
|
|
|
* __wait_on_freeing_inode() path
|
|
|
|
*/
|
|
|
|
inode_wake_up_bit(&inode->v, __I_NEW);
|
2024-06-09 01:41:01 +00:00
|
|
|
}
|
2024-09-08 05:06:57 +00:00
|
|
|
}
|
|
|
|
|
2024-08-16 16:31:53 +00:00
|
|
|
static struct bch_inode_info *bch2_inode_hash_insert(struct bch_fs *c,
|
|
|
|
struct btree_trans *trans,
|
|
|
|
struct bch_inode_info *inode)
|
2017-03-17 06:18:50 +00:00
|
|
|
{
|
2024-06-09 01:41:01 +00:00
|
|
|
struct bch_inode_info *old = inode;
|
|
|
|
|
|
|
|
set_bit(EI_INODE_HASHED, &inode->ei_flags);
|
|
|
|
retry:
|
2024-10-03 01:23:41 +00:00
|
|
|
if (unlikely(rhashtable_lookup_insert_key(&c->vfs_inodes_table,
|
|
|
|
&inode->ei_inum,
|
2024-06-09 01:41:01 +00:00
|
|
|
&inode->hash,
|
|
|
|
bch2_vfs_inodes_params))) {
|
2024-08-16 16:31:53 +00:00
|
|
|
old = bch2_inode_hash_find(c, trans, inode->ei_inum);
|
2024-06-09 01:41:01 +00:00
|
|
|
if (!old)
|
|
|
|
goto retry;
|
|
|
|
|
|
|
|
clear_bit(EI_INODE_HASHED, &inode->ei_flags);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2024-06-20 17:20:49 +00:00
|
|
|
/*
|
|
|
|
* bcachefs doesn't use I_NEW; we have no use for it since we
|
|
|
|
* only insert fully created inodes in the inode hash table. But
|
|
|
|
* discard_new_inode() expects it to be set...
|
|
|
|
*/
|
2024-08-16 16:44:49 +00:00
|
|
|
inode->v.i_state |= I_NEW;
|
2024-07-01 20:23:54 +00:00
|
|
|
/*
|
|
|
|
* We don't want bch2_evict_inode() to delete the inode on disk,
|
|
|
|
* we just raced and had another inode in cache. Normally new
|
|
|
|
* inodes don't have nlink == 0 - except tmpfiles do...
|
|
|
|
*/
|
|
|
|
set_nlink(&inode->v, 1);
|
2024-05-23 00:36:42 +00:00
|
|
|
discard_new_inode(&inode->v);
|
2024-08-16 16:31:53 +00:00
|
|
|
return old;
|
2024-01-26 01:25:49 +00:00
|
|
|
} else {
|
2024-06-09 01:41:01 +00:00
|
|
|
inode_fake_hash(&inode->v);
|
|
|
|
|
|
|
|
inode_sb_list_add(&inode->v);
|
|
|
|
|
2024-01-26 01:25:49 +00:00
|
|
|
mutex_lock(&c->vfs_inodes_lock);
|
|
|
|
list_add(&inode->ei_vfs_inode_list, &c->vfs_inodes_list);
|
|
|
|
mutex_unlock(&c->vfs_inodes_lock);
|
2024-08-16 16:31:53 +00:00
|
|
|
return inode;
|
2024-01-26 01:25:49 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-06 04:03:40 +00:00
|
|
|
|
2024-01-26 01:25:49 +00:00
|
|
|
#define memalloc_flags_do(_flags, _do) \
|
|
|
|
({ \
|
|
|
|
unsigned _saved_flags = memalloc_flags_save(_flags); \
|
|
|
|
typeof(_do) _ret = _do; \
|
|
|
|
memalloc_noreclaim_restore(_saved_flags); \
|
|
|
|
_ret; \
|
|
|
|
})
|
|
|
|
|
2024-04-21 02:03:09 +00:00
|
|
|
static struct inode *bch2_alloc_inode(struct super_block *sb)
|
|
|
|
{
|
|
|
|
BUG();
|
|
|
|
}
|
|
|
|
|
2024-09-26 17:11:50 +00:00
|
|
|
static struct bch_inode_info *__bch2_new_inode(struct bch_fs *c, gfp_t gfp)
|
2024-04-21 02:03:09 +00:00
|
|
|
{
|
2024-07-16 02:58:16 +00:00
|
|
|
struct bch_inode_info *inode = alloc_inode_sb(c->vfs_sb,
|
2024-09-26 17:11:50 +00:00
|
|
|
bch2_inode_cache, gfp);
|
2024-04-21 02:03:09 +00:00
|
|
|
if (!inode)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
inode_init_once(&inode->v);
|
|
|
|
mutex_init(&inode->ei_update_lock);
|
|
|
|
two_state_lock_init(&inode->ei_pagecache_lock);
|
|
|
|
INIT_LIST_HEAD(&inode->ei_vfs_inode_list);
|
2024-06-11 00:48:41 +00:00
|
|
|
inode->ei_flags = 0;
|
2024-04-21 02:03:09 +00:00
|
|
|
mutex_init(&inode->ei_quota_lock);
|
2024-06-11 00:48:41 +00:00
|
|
|
memset(&inode->ei_devs_need_flush, 0, sizeof(inode->ei_devs_need_flush));
|
2024-04-21 02:03:09 +00:00
|
|
|
|
2024-09-26 17:11:50 +00:00
|
|
|
if (unlikely(inode_init_always_gfp(c->vfs_sb, &inode->v, gfp))) {
|
2024-04-21 02:03:09 +00:00
|
|
|
kmem_cache_free(bch2_inode_cache, inode);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return inode;
|
|
|
|
}
|
|
|
|
|
2024-01-26 01:25:49 +00:00
|
|
|
/*
|
|
|
|
* Allocate a new inode, dropping/retaking btree locks if necessary:
|
|
|
|
*/
|
|
|
|
static struct bch_inode_info *bch2_new_inode(struct btree_trans *trans)
|
|
|
|
{
|
2024-09-26 17:11:50 +00:00
|
|
|
struct bch_inode_info *inode = __bch2_new_inode(trans->c, GFP_NOWAIT);
|
2021-11-06 04:03:40 +00:00
|
|
|
|
2024-01-26 01:25:49 +00:00
|
|
|
if (unlikely(!inode)) {
|
2024-09-26 17:11:50 +00:00
|
|
|
int ret = drop_locks_do(trans, (inode = __bch2_new_inode(trans->c, GFP_NOFS)) ? 0 : -ENOMEM);
|
2024-04-21 02:26:47 +00:00
|
|
|
if (ret && inode) {
|
|
|
|
__destroy_inode(&inode->v);
|
|
|
|
kmem_cache_free(bch2_inode_cache, inode);
|
|
|
|
}
|
2024-01-26 01:25:49 +00:00
|
|
|
if (ret)
|
|
|
|
return ERR_PTR(ret);
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
2024-01-26 01:25:49 +00:00
|
|
|
return inode;
|
|
|
|
}
|
2023-03-15 15:53:51 +00:00
|
|
|
|
2024-08-16 16:31:53 +00:00
|
|
|
static struct bch_inode_info *bch2_inode_hash_init_insert(struct btree_trans *trans,
|
|
|
|
subvol_inum inum,
|
|
|
|
struct bch_inode_unpacked *bi,
|
|
|
|
struct bch_subvolume *subvol)
|
|
|
|
{
|
|
|
|
struct bch_inode_info *inode = bch2_new_inode(trans);
|
|
|
|
if (IS_ERR(inode))
|
|
|
|
return inode;
|
|
|
|
|
|
|
|
bch2_vfs_inode_init(trans, inum, inode, bi, subvol);
|
|
|
|
|
|
|
|
return bch2_inode_hash_insert(trans->c, trans, inode);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-01-26 01:25:49 +00:00
|
|
|
struct inode *bch2_vfs_inode_get(struct bch_fs *c, subvol_inum inum)
|
|
|
|
{
|
2024-08-16 16:31:53 +00:00
|
|
|
struct bch_inode_info *inode = bch2_inode_hash_find(c, NULL, inum);
|
2024-01-26 01:25:49 +00:00
|
|
|
if (inode)
|
|
|
|
return &inode->v;
|
|
|
|
|
|
|
|
struct btree_trans *trans = bch2_trans_get(c);
|
|
|
|
|
|
|
|
struct bch_inode_unpacked inode_u;
|
|
|
|
struct bch_subvolume subvol;
|
|
|
|
int ret = lockrestart_do(trans,
|
|
|
|
bch2_subvolume_get(trans, inum.subvol, true, 0, &subvol) ?:
|
|
|
|
bch2_inode_find_by_inum_trans(trans, inum, &inode_u)) ?:
|
2024-08-16 16:31:53 +00:00
|
|
|
PTR_ERR_OR_ZERO(inode = bch2_inode_hash_init_insert(trans, inum, &inode_u, &subvol));
|
2024-01-26 01:25:49 +00:00
|
|
|
bch2_trans_put(trans);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2024-01-26 01:25:49 +00:00
|
|
|
return ret ? ERR_PTR(ret) : &inode->v;
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
2021-03-16 04:28:17 +00:00
|
|
|
struct bch_inode_info *
|
2017-03-17 06:18:50 +00:00
|
|
|
__bch2_create(struct mnt_idmap *idmap,
|
|
|
|
struct bch_inode_info *dir, struct dentry *dentry,
|
2021-03-17 03:28:43 +00:00
|
|
|
umode_t mode, dev_t rdev, subvol_inum snapshot_src,
|
|
|
|
unsigned flags)
|
2017-03-17 06:18:50 +00:00
|
|
|
{
|
|
|
|
struct bch_fs *c = dir->v.i_sb->s_fs_info;
|
2023-09-12 21:16:02 +00:00
|
|
|
struct btree_trans *trans;
|
2017-03-17 06:18:50 +00:00
|
|
|
struct bch_inode_unpacked dir_u;
|
2024-01-26 01:25:49 +00:00
|
|
|
struct bch_inode_info *inode;
|
2017-03-17 06:18:50 +00:00
|
|
|
struct bch_inode_unpacked inode_u;
|
|
|
|
struct posix_acl *default_acl = NULL, *acl = NULL;
|
2021-03-16 05:33:39 +00:00
|
|
|
subvol_inum inum;
|
2021-10-27 17:05:56 +00:00
|
|
|
struct bch_subvolume subvol;
|
2018-07-23 09:48:35 +00:00
|
|
|
u64 journal_seq = 0;
|
2024-08-24 01:27:24 +00:00
|
|
|
kuid_t kuid;
|
|
|
|
kgid_t kgid;
|
2017-03-17 06:18:50 +00:00
|
|
|
int ret;
|
|
|
|
|
2019-10-02 22:35:36 +00:00
|
|
|
/*
|
|
|
|
* preallocate acls + vfs inode before btree transaction, so that
|
|
|
|
* nothing can fail after the transaction succeeds:
|
|
|
|
*/
|
2017-03-17 06:18:50 +00:00
|
|
|
#ifdef CONFIG_BCACHEFS_POSIX_ACL
|
2019-10-02 22:35:36 +00:00
|
|
|
ret = posix_acl_create(&dir->v, &mode, &default_acl, &acl);
|
2017-03-17 06:18:50 +00:00
|
|
|
if (ret)
|
2019-10-02 22:35:36 +00:00
|
|
|
return ERR_PTR(ret);
|
2017-03-17 06:18:50 +00:00
|
|
|
#endif
|
2024-09-26 17:11:50 +00:00
|
|
|
inode = __bch2_new_inode(c, GFP_NOFS);
|
2017-03-17 06:18:50 +00:00
|
|
|
if (unlikely(!inode)) {
|
2019-10-02 22:35:36 +00:00
|
|
|
inode = ERR_PTR(-ENOMEM);
|
2017-03-17 06:18:50 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2019-10-02 22:35:36 +00:00
|
|
|
bch2_inode_init_early(c, &inode_u);
|
|
|
|
|
2021-03-16 04:28:17 +00:00
|
|
|
if (!(flags & BCH_CREATE_TMPFILE))
|
2017-03-17 06:18:50 +00:00
|
|
|
mutex_lock(&dir->ei_update_lock);
|
|
|
|
|
2023-09-12 21:16:02 +00:00
|
|
|
trans = bch2_trans_get(c);
|
2017-03-17 06:18:50 +00:00
|
|
|
retry:
|
2023-09-12 21:16:02 +00:00
|
|
|
bch2_trans_begin(trans);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2024-08-24 01:27:24 +00:00
|
|
|
kuid = mapped_fsuid(idmap, i_user_ns(&dir->v));
|
|
|
|
kgid = mapped_fsgid(idmap, i_user_ns(&dir->v));
|
2024-06-09 01:41:01 +00:00
|
|
|
ret = bch2_subvol_is_ro_trans(trans, dir->ei_inum.subvol) ?:
|
2023-12-30 02:16:32 +00:00
|
|
|
bch2_create_trans(trans,
|
2021-03-16 04:28:17 +00:00
|
|
|
inode_inum(dir), &dir_u, &inode_u,
|
|
|
|
!(flags & BCH_CREATE_TMPFILE)
|
|
|
|
? &dentry->d_name : NULL,
|
2024-08-24 01:27:24 +00:00
|
|
|
from_kuid(i_user_ns(&dir->v), kuid),
|
|
|
|
from_kgid(i_user_ns(&dir->v), kgid),
|
2019-10-02 22:35:36 +00:00
|
|
|
mode, rdev,
|
2021-03-17 03:28:43 +00:00
|
|
|
default_acl, acl, snapshot_src, flags) ?:
|
2019-10-02 22:35:36 +00:00
|
|
|
bch2_quota_acct(c, bch_qid(&inode_u), Q_INO, 1,
|
|
|
|
KEY_TYPE_QUOTA_PREALLOC);
|
|
|
|
if (unlikely(ret))
|
|
|
|
goto err_before_quota;
|
|
|
|
|
2024-06-09 01:41:01 +00:00
|
|
|
inum.subvol = inode_u.bi_subvol ?: dir->ei_inum.subvol;
|
2021-10-27 17:05:56 +00:00
|
|
|
inum.inum = inode_u.bi_inum;
|
|
|
|
|
2023-09-12 21:16:02 +00:00
|
|
|
ret = bch2_subvolume_get(trans, inum.subvol, true,
|
2024-04-07 22:05:34 +00:00
|
|
|
BTREE_ITER_with_updates, &subvol) ?:
|
2023-09-12 21:16:02 +00:00
|
|
|
bch2_trans_commit(trans, NULL, &journal_seq, 0);
|
2019-10-02 22:35:36 +00:00
|
|
|
if (unlikely(ret)) {
|
|
|
|
bch2_quota_acct(c, bch_qid(&inode_u), Q_INO, -1,
|
|
|
|
KEY_TYPE_QUOTA_WARN);
|
|
|
|
err_before_quota:
|
2022-07-18 03:06:38 +00:00
|
|
|
if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
|
2019-10-02 22:35:36 +00:00
|
|
|
goto retry;
|
2017-03-17 06:18:50 +00:00
|
|
|
goto err_trans;
|
2019-10-02 22:35:36 +00:00
|
|
|
}
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2021-03-16 04:28:17 +00:00
|
|
|
if (!(flags & BCH_CREATE_TMPFILE)) {
|
2023-09-12 21:16:02 +00:00
|
|
|
bch2_inode_update_after_write(trans, dir, &dir_u,
|
2017-03-17 06:18:50 +00:00
|
|
|
ATTR_MTIME|ATTR_CTIME);
|
|
|
|
mutex_unlock(&dir->ei_update_lock);
|
|
|
|
}
|
|
|
|
|
2023-09-12 21:16:02 +00:00
|
|
|
bch2_vfs_inode_init(trans, inum, inode, &inode_u, &subvol);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
set_cached_acl(&inode->v, ACL_TYPE_ACCESS, acl);
|
|
|
|
set_cached_acl(&inode->v, ACL_TYPE_DEFAULT, default_acl);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* we must insert the new inode into the inode cache before calling
|
|
|
|
* bch2_trans_exit() and dropping locks, else we could race with another
|
|
|
|
* thread pulling the inode in and modifying it:
|
2024-08-16 16:31:53 +00:00
|
|
|
*
|
|
|
|
* also, calling bch2_inode_hash_insert() without passing in the
|
|
|
|
* transaction object is sketchy - if we could ever end up in
|
|
|
|
* __wait_on_freeing_inode(), we'd risk deadlock.
|
|
|
|
*
|
|
|
|
* But that shouldn't be possible, since we still have the inode locked
|
|
|
|
* that we just created, and we _really_ can't take a transaction
|
|
|
|
* restart here.
|
2017-03-17 06:18:50 +00:00
|
|
|
*/
|
2024-08-16 16:31:53 +00:00
|
|
|
inode = bch2_inode_hash_insert(c, NULL, inode);
|
2023-09-12 21:16:02 +00:00
|
|
|
bch2_trans_put(trans);
|
2019-10-02 22:35:36 +00:00
|
|
|
err:
|
2017-03-17 06:18:50 +00:00
|
|
|
posix_acl_release(default_acl);
|
|
|
|
posix_acl_release(acl);
|
|
|
|
return inode;
|
|
|
|
err_trans:
|
2021-03-16 04:28:17 +00:00
|
|
|
if (!(flags & BCH_CREATE_TMPFILE))
|
2018-07-21 02:23:42 +00:00
|
|
|
mutex_unlock(&dir->ei_update_lock);
|
|
|
|
|
2023-09-12 21:16:02 +00:00
|
|
|
bch2_trans_put(trans);
|
2017-03-17 06:18:50 +00:00
|
|
|
make_bad_inode(&inode->v);
|
|
|
|
iput(&inode->v);
|
|
|
|
inode = ERR_PTR(ret);
|
2019-10-02 22:35:36 +00:00
|
|
|
goto err;
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* methods */
|
|
|
|
|
2024-01-25 17:36:37 +00:00
|
|
|
static struct bch_inode_info *bch2_lookup_trans(struct btree_trans *trans,
|
|
|
|
subvol_inum dir, struct bch_hash_info *dir_hash_info,
|
|
|
|
const struct qstr *name)
|
|
|
|
{
|
|
|
|
struct bch_fs *c = trans->c;
|
|
|
|
struct btree_iter dirent_iter = {};
|
|
|
|
subvol_inum inum = {};
|
2024-04-25 23:54:03 +00:00
|
|
|
struct printbuf buf = PRINTBUF;
|
2024-01-25 17:36:37 +00:00
|
|
|
|
2024-04-07 20:16:49 +00:00
|
|
|
struct bkey_s_c k = bch2_hash_lookup(trans, &dirent_iter, bch2_dirent_hash_desc,
|
|
|
|
dir_hash_info, dir, name, 0);
|
|
|
|
int ret = bkey_err(k);
|
2024-01-25 17:36:37 +00:00
|
|
|
if (ret)
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
|
|
|
|
ret = bch2_dirent_read_target(trans, dir, bkey_s_c_to_dirent(k), &inum);
|
|
|
|
if (ret > 0)
|
|
|
|
ret = -ENOENT;
|
|
|
|
if (ret)
|
|
|
|
goto err;
|
|
|
|
|
2024-08-16 16:31:53 +00:00
|
|
|
struct bch_inode_info *inode = bch2_inode_hash_find(c, trans, inum);
|
2024-01-25 17:36:37 +00:00
|
|
|
if (inode)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
struct bch_subvolume subvol;
|
|
|
|
struct bch_inode_unpacked inode_u;
|
|
|
|
ret = bch2_subvolume_get(trans, inum.subvol, true, 0, &subvol) ?:
|
|
|
|
bch2_inode_find_by_inum_nowarn_trans(trans, inum, &inode_u) ?:
|
2024-08-16 16:31:53 +00:00
|
|
|
PTR_ERR_OR_ZERO(inode = bch2_inode_hash_init_insert(trans, inum, &inode_u, &subvol));
|
2024-01-25 17:36:37 +00:00
|
|
|
|
2024-04-25 23:54:03 +00:00
|
|
|
bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT),
|
|
|
|
c, "dirent to missing inode:\n %s",
|
|
|
|
(bch2_bkey_val_to_text(&buf, c, k), buf.buf));
|
2024-01-25 17:36:37 +00:00
|
|
|
if (ret)
|
|
|
|
goto err;
|
|
|
|
|
2024-04-25 23:54:03 +00:00
|
|
|
/* regular files may have hardlinks: */
|
|
|
|
if (bch2_fs_inconsistent_on(bch2_inode_should_have_bp(&inode_u) &&
|
|
|
|
!bkey_eq(k.k->p, POS(inode_u.bi_dir, inode_u.bi_dir_offset)),
|
|
|
|
c,
|
|
|
|
"dirent points to inode that does not point back:\n %s",
|
|
|
|
(bch2_bkey_val_to_text(&buf, c, k),
|
|
|
|
prt_printf(&buf, "\n "),
|
|
|
|
bch2_inode_unpacked_to_text(&buf, &inode_u),
|
|
|
|
buf.buf))) {
|
|
|
|
ret = -ENOENT;
|
|
|
|
goto err;
|
|
|
|
}
|
2024-01-25 17:36:37 +00:00
|
|
|
out:
|
|
|
|
bch2_trans_iter_exit(trans, &dirent_iter);
|
2024-04-25 23:54:03 +00:00
|
|
|
printbuf_exit(&buf);
|
2024-01-25 17:36:37 +00:00
|
|
|
return inode;
|
|
|
|
err:
|
|
|
|
inode = ERR_PTR(ret);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
static struct dentry *bch2_lookup(struct inode *vdir, struct dentry *dentry,
|
|
|
|
unsigned int flags)
|
|
|
|
{
|
|
|
|
struct bch_fs *c = vdir->i_sb->s_fs_info;
|
|
|
|
struct bch_inode_info *dir = to_bch_ei(vdir);
|
2021-03-02 23:35:30 +00:00
|
|
|
struct bch_hash_info hash = bch2_hash_info_init(c, &dir->ei_inode);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2024-01-25 17:36:37 +00:00
|
|
|
struct bch_inode_info *inode;
|
2024-10-14 01:53:26 +00:00
|
|
|
bch2_trans_do(c,
|
2024-01-25 17:36:37 +00:00
|
|
|
PTR_ERR_OR_ZERO(inode = bch2_lookup_trans(trans, inode_inum(dir),
|
|
|
|
&hash, &dentry->d_name)));
|
|
|
|
if (IS_ERR(inode))
|
|
|
|
inode = NULL;
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2024-01-25 17:36:37 +00:00
|
|
|
return d_splice_alias(&inode->v, dentry);
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
2019-10-11 19:14:36 +00:00
|
|
|
static int bch2_mknod(struct mnt_idmap *idmap,
|
|
|
|
struct inode *vdir, struct dentry *dentry,
|
|
|
|
umode_t mode, dev_t rdev)
|
2017-03-17 06:18:50 +00:00
|
|
|
{
|
|
|
|
struct bch_inode_info *inode =
|
2021-03-17 03:28:43 +00:00
|
|
|
__bch2_create(idmap, to_bch_ei(vdir), dentry, mode, rdev,
|
|
|
|
(subvol_inum) { 0 }, 0);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
if (IS_ERR(inode))
|
2022-09-18 19:43:50 +00:00
|
|
|
return bch2_err_class(PTR_ERR(inode));
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
d_instantiate(dentry, &inode->v);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-10-11 19:14:36 +00:00
|
|
|
static int bch2_create(struct mnt_idmap *idmap,
|
|
|
|
struct inode *vdir, struct dentry *dentry,
|
|
|
|
umode_t mode, bool excl)
|
|
|
|
{
|
|
|
|
return bch2_mknod(idmap, vdir, dentry, mode|S_IFREG, 0);
|
|
|
|
}
|
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
static int __bch2_link(struct bch_fs *c,
|
|
|
|
struct bch_inode_info *inode,
|
|
|
|
struct bch_inode_info *dir,
|
|
|
|
struct dentry *dentry)
|
|
|
|
{
|
2019-11-12 04:14:30 +00:00
|
|
|
struct bch_inode_unpacked dir_u, inode_u;
|
2017-03-17 06:18:50 +00:00
|
|
|
int ret;
|
|
|
|
|
2018-07-21 02:23:42 +00:00
|
|
|
mutex_lock(&inode->ei_update_lock);
|
2024-07-04 21:10:29 +00:00
|
|
|
struct btree_trans *trans = bch2_trans_get(c);
|
2019-10-01 20:51:57 +00:00
|
|
|
|
2023-09-12 21:16:02 +00:00
|
|
|
ret = commit_do(trans, NULL, NULL, 0,
|
|
|
|
bch2_link_trans(trans,
|
2021-03-16 04:28:17 +00:00
|
|
|
inode_inum(dir), &dir_u,
|
|
|
|
inode_inum(inode), &inode_u,
|
2021-03-12 22:52:42 +00:00
|
|
|
&dentry->d_name));
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2019-11-12 04:14:30 +00:00
|
|
|
if (likely(!ret)) {
|
2023-09-12 21:16:02 +00:00
|
|
|
bch2_inode_update_after_write(trans, dir, &dir_u,
|
2019-11-12 04:14:30 +00:00
|
|
|
ATTR_MTIME|ATTR_CTIME);
|
2023-09-12 21:16:02 +00:00
|
|
|
bch2_inode_update_after_write(trans, inode, &inode_u, ATTR_CTIME);
|
2019-11-12 04:14:30 +00:00
|
|
|
}
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2023-09-12 21:16:02 +00:00
|
|
|
bch2_trans_put(trans);
|
2018-07-21 02:23:42 +00:00
|
|
|
mutex_unlock(&inode->ei_update_lock);
|
2017-03-17 06:18:50 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bch2_link(struct dentry *old_dentry, 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(old_dentry->d_inode);
|
|
|
|
int ret;
|
2018-07-17 18:03:47 +00:00
|
|
|
|
|
|
|
lockdep_assert_held(&inode->v.i_rwsem);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2024-06-09 01:41:01 +00:00
|
|
|
ret = bch2_subvol_is_ro(c, dir->ei_inum.subvol) ?:
|
|
|
|
bch2_subvol_is_ro(c, inode->ei_inum.subvol) ?:
|
2023-12-30 02:16:32 +00:00
|
|
|
__bch2_link(c, inode, dir, dentry);
|
2017-03-17 06:18:50 +00:00
|
|
|
if (unlikely(ret))
|
2024-02-09 23:34:05 +00:00
|
|
|
return bch2_err_class(ret);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
ihold(&inode->v);
|
|
|
|
d_instantiate(dentry, &inode->v);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-03-17 03:28:43 +00:00
|
|
|
int __bch2_unlink(struct inode *vdir, struct dentry *dentry,
|
2021-10-11 16:03:19 +00:00
|
|
|
bool deleting_snapshot)
|
2017-03-17 06:18:50 +00:00
|
|
|
{
|
|
|
|
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 bch_inode_unpacked dir_u, inode_u;
|
|
|
|
int ret;
|
|
|
|
|
2019-06-24 22:24:38 +00:00
|
|
|
bch2_lock_inodes(INODE_UPDATE_LOCK, dir, inode);
|
2019-10-01 20:51:57 +00:00
|
|
|
|
2024-07-04 21:10:29 +00:00
|
|
|
struct btree_trans *trans = bch2_trans_get(c);
|
|
|
|
|
2023-09-12 21:16:02 +00:00
|
|
|
ret = commit_do(trans, NULL, NULL,
|
2023-11-11 21:31:50 +00:00
|
|
|
BCH_TRANS_COMMIT_no_enospc,
|
2023-09-12 21:16:02 +00:00
|
|
|
bch2_unlink_trans(trans,
|
2023-03-16 15:04:28 +00:00
|
|
|
inode_inum(dir), &dir_u,
|
|
|
|
&inode_u, &dentry->d_name,
|
|
|
|
deleting_snapshot));
|
|
|
|
if (unlikely(ret))
|
|
|
|
goto err;
|
2019-10-02 22:35:36 +00:00
|
|
|
|
2023-09-12 21:16:02 +00:00
|
|
|
bch2_inode_update_after_write(trans, dir, &dir_u,
|
2023-03-16 15:04:28 +00:00
|
|
|
ATTR_MTIME|ATTR_CTIME);
|
2023-09-12 21:16:02 +00:00
|
|
|
bch2_inode_update_after_write(trans, inode, &inode_u,
|
2023-03-16 15:04:28 +00:00
|
|
|
ATTR_MTIME);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2023-03-16 15:04:28 +00:00
|
|
|
if (inode_u.bi_subvol) {
|
|
|
|
/*
|
|
|
|
* Subvolume deletion is asynchronous, but we still want to tell
|
|
|
|
* the VFS that it's been deleted here:
|
|
|
|
*/
|
|
|
|
set_nlink(&inode->v, 0);
|
|
|
|
}
|
|
|
|
err:
|
2023-09-12 21:16:02 +00:00
|
|
|
bch2_trans_put(trans);
|
2024-07-04 21:10:29 +00:00
|
|
|
bch2_unlock_inodes(INODE_UPDATE_LOCK, dir, inode);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-03-17 03:28:43 +00:00
|
|
|
static int bch2_unlink(struct inode *vdir, struct dentry *dentry)
|
|
|
|
{
|
2023-12-30 02:16:32 +00:00
|
|
|
struct bch_inode_info *dir= to_bch_ei(vdir);
|
|
|
|
struct bch_fs *c = dir->v.i_sb->s_fs_info;
|
|
|
|
|
2024-06-09 01:41:01 +00:00
|
|
|
int ret = bch2_subvol_is_ro(c, dir->ei_inum.subvol) ?:
|
2023-12-30 02:16:32 +00:00
|
|
|
__bch2_unlink(vdir, dentry, false);
|
2024-02-09 23:34:05 +00:00
|
|
|
return bch2_err_class(ret);
|
2021-03-17 03:28:43 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
static int bch2_symlink(struct mnt_idmap *idmap,
|
|
|
|
struct inode *vdir, struct dentry *dentry,
|
|
|
|
const char *symname)
|
|
|
|
{
|
|
|
|
struct bch_fs *c = vdir->i_sb->s_fs_info;
|
|
|
|
struct bch_inode_info *dir = to_bch_ei(vdir), *inode;
|
|
|
|
int ret;
|
|
|
|
|
2021-03-16 04:28:17 +00:00
|
|
|
inode = __bch2_create(idmap, dir, dentry, S_IFLNK|S_IRWXUGO, 0,
|
2021-03-17 03:28:43 +00:00
|
|
|
(subvol_inum) { 0 }, BCH_CREATE_TMPFILE);
|
2022-10-19 22:31:33 +00:00
|
|
|
if (IS_ERR(inode))
|
2022-09-18 19:43:50 +00:00
|
|
|
return bch2_err_class(PTR_ERR(inode));
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
inode_lock(&inode->v);
|
|
|
|
ret = page_symlink(&inode->v, symname, strlen(symname) + 1);
|
|
|
|
inode_unlock(&inode->v);
|
|
|
|
|
|
|
|
if (unlikely(ret))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
ret = filemap_write_and_wait_range(inode->v.i_mapping, 0, LLONG_MAX);
|
|
|
|
if (unlikely(ret))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
ret = __bch2_link(c, inode, dir, dentry);
|
|
|
|
if (unlikely(ret))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
d_instantiate(dentry, &inode->v);
|
|
|
|
return 0;
|
|
|
|
err:
|
|
|
|
iput(&inode->v);
|
2024-02-09 23:34:05 +00:00
|
|
|
return bch2_err_class(ret);
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int bch2_mkdir(struct mnt_idmap *idmap,
|
|
|
|
struct inode *vdir, struct dentry *dentry, umode_t mode)
|
|
|
|
{
|
2019-10-11 19:14:36 +00:00
|
|
|
return bch2_mknod(idmap, vdir, dentry, mode|S_IFDIR, 0);
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int bch2_rename2(struct mnt_idmap *idmap,
|
|
|
|
struct inode *src_vdir, struct dentry *src_dentry,
|
|
|
|
struct inode *dst_vdir, struct dentry *dst_dentry,
|
|
|
|
unsigned flags)
|
|
|
|
{
|
|
|
|
struct bch_fs *c = src_vdir->i_sb->s_fs_info;
|
2019-10-02 22:35:36 +00:00
|
|
|
struct bch_inode_info *src_dir = to_bch_ei(src_vdir);
|
|
|
|
struct bch_inode_info *dst_dir = to_bch_ei(dst_vdir);
|
|
|
|
struct bch_inode_info *src_inode = to_bch_ei(src_dentry->d_inode);
|
|
|
|
struct bch_inode_info *dst_inode = to_bch_ei(dst_dentry->d_inode);
|
2017-03-17 06:18:50 +00:00
|
|
|
struct bch_inode_unpacked dst_dir_u, src_dir_u;
|
2024-08-18 17:09:02 +00:00
|
|
|
struct bch_inode_unpacked src_inode_u, dst_inode_u, *whiteout_inode_u;
|
2023-09-12 21:16:02 +00:00
|
|
|
struct btree_trans *trans;
|
2019-10-02 22:35:36 +00:00
|
|
|
enum bch_rename_mode mode = flags & RENAME_EXCHANGE
|
|
|
|
? BCH_RENAME_EXCHANGE
|
|
|
|
: dst_dentry->d_inode
|
|
|
|
? BCH_RENAME_OVERWRITE : BCH_RENAME;
|
2024-08-18 17:09:02 +00:00
|
|
|
bool whiteout = !!(flags & RENAME_WHITEOUT);
|
2017-03-17 06:18:50 +00:00
|
|
|
int ret;
|
|
|
|
|
2024-08-18 17:09:02 +00:00
|
|
|
if (flags & ~(RENAME_NOREPLACE|RENAME_EXCHANGE|RENAME_WHITEOUT))
|
2017-03-17 06:18:50 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
2019-10-02 22:35:36 +00:00
|
|
|
if (mode == BCH_RENAME_OVERWRITE) {
|
|
|
|
ret = filemap_write_and_wait_range(src_inode->v.i_mapping,
|
2017-03-17 06:18:50 +00:00
|
|
|
0, LLONG_MAX);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-06-24 22:24:38 +00:00
|
|
|
bch2_lock_inodes(INODE_UPDATE_LOCK,
|
2019-10-02 22:35:36 +00:00
|
|
|
src_dir,
|
|
|
|
dst_dir,
|
|
|
|
src_inode,
|
|
|
|
dst_inode);
|
|
|
|
|
2024-07-04 21:10:29 +00:00
|
|
|
trans = bch2_trans_get(c);
|
|
|
|
|
2024-06-09 01:41:01 +00:00
|
|
|
ret = bch2_subvol_is_ro_trans(trans, src_dir->ei_inum.subvol) ?:
|
|
|
|
bch2_subvol_is_ro_trans(trans, dst_dir->ei_inum.subvol);
|
2023-12-30 02:16:32 +00:00
|
|
|
if (ret)
|
2024-10-15 02:18:12 +00:00
|
|
|
goto err_tx_restart;
|
2023-12-30 02:16:32 +00:00
|
|
|
|
2019-10-02 22:35:36 +00:00
|
|
|
if (inode_attr_changing(dst_dir, src_inode, Inode_opt_project)) {
|
|
|
|
ret = bch2_fs_quota_transfer(c, src_inode,
|
|
|
|
dst_dir->ei_qid,
|
2018-12-17 10:31:49 +00:00
|
|
|
1 << QTYP_PRJ,
|
|
|
|
KEY_TYPE_QUOTA_PREALLOC);
|
|
|
|
if (ret)
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2019-10-02 22:35:36 +00:00
|
|
|
if (mode == BCH_RENAME_EXCHANGE &&
|
|
|
|
inode_attr_changing(src_dir, dst_inode, Inode_opt_project)) {
|
|
|
|
ret = bch2_fs_quota_transfer(c, dst_inode,
|
|
|
|
src_dir->ei_qid,
|
2018-12-17 10:31:49 +00:00
|
|
|
1 << QTYP_PRJ,
|
|
|
|
KEY_TYPE_QUOTA_PREALLOC);
|
|
|
|
if (ret)
|
|
|
|
goto err;
|
|
|
|
}
|
2024-08-18 17:09:02 +00:00
|
|
|
retry:
|
|
|
|
bch2_trans_begin(trans);
|
2018-12-17 10:31:49 +00:00
|
|
|
|
2024-08-18 17:09:02 +00:00
|
|
|
ret = bch2_rename_trans(trans,
|
|
|
|
inode_inum(src_dir), &src_dir_u,
|
|
|
|
inode_inum(dst_dir), &dst_dir_u,
|
|
|
|
&src_inode_u,
|
|
|
|
&dst_inode_u,
|
|
|
|
&src_dentry->d_name,
|
|
|
|
&dst_dentry->d_name,
|
|
|
|
mode);
|
2017-03-17 06:18:50 +00:00
|
|
|
if (unlikely(ret))
|
2024-08-18 17:09:02 +00:00
|
|
|
goto err_tx_restart;
|
|
|
|
|
|
|
|
if (whiteout) {
|
|
|
|
whiteout_inode_u = bch2_trans_kmalloc_nomemzero(trans, sizeof(*whiteout_inode_u));
|
|
|
|
ret = PTR_ERR_OR_ZERO(whiteout_inode_u);
|
|
|
|
if (unlikely(ret))
|
|
|
|
goto err_tx_restart;
|
|
|
|
bch2_inode_init_early(c, whiteout_inode_u);
|
|
|
|
|
|
|
|
ret = bch2_create_trans(trans,
|
|
|
|
inode_inum(src_dir), &src_dir_u,
|
|
|
|
whiteout_inode_u,
|
|
|
|
&src_dentry->d_name,
|
|
|
|
from_kuid(i_user_ns(&src_dir->v), current_fsuid()),
|
|
|
|
from_kgid(i_user_ns(&src_dir->v), current_fsgid()),
|
|
|
|
S_IFCHR|WHITEOUT_MODE, 0,
|
|
|
|
NULL, NULL, (subvol_inum) { 0 }, 0) ?:
|
|
|
|
bch2_quota_acct(c, bch_qid(whiteout_inode_u), Q_INO, 1,
|
|
|
|
KEY_TYPE_QUOTA_PREALLOC);
|
|
|
|
if (unlikely(ret))
|
|
|
|
goto err_tx_restart;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = bch2_trans_commit(trans, NULL, NULL, 0);
|
|
|
|
if (unlikely(ret)) {
|
|
|
|
err_tx_restart:
|
|
|
|
if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
|
|
|
|
goto retry;
|
2017-03-17 06:18:50 +00:00
|
|
|
goto err;
|
2024-08-18 17:09:02 +00:00
|
|
|
}
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2019-10-02 22:35:36 +00:00
|
|
|
BUG_ON(src_inode->v.i_ino != src_inode_u.bi_inum);
|
|
|
|
BUG_ON(dst_inode &&
|
|
|
|
dst_inode->v.i_ino != dst_inode_u.bi_inum);
|
|
|
|
|
2023-09-12 21:16:02 +00:00
|
|
|
bch2_inode_update_after_write(trans, src_dir, &src_dir_u,
|
2017-03-17 06:18:50 +00:00
|
|
|
ATTR_MTIME|ATTR_CTIME);
|
|
|
|
|
2021-11-05 19:17:13 +00:00
|
|
|
if (src_dir != dst_dir)
|
2023-09-12 21:16:02 +00:00
|
|
|
bch2_inode_update_after_write(trans, dst_dir, &dst_dir_u,
|
2017-03-17 06:18:50 +00:00
|
|
|
ATTR_MTIME|ATTR_CTIME);
|
|
|
|
|
2023-09-12 21:16:02 +00:00
|
|
|
bch2_inode_update_after_write(trans, src_inode, &src_inode_u,
|
2017-03-17 06:18:50 +00:00
|
|
|
ATTR_CTIME);
|
2019-10-02 22:35:36 +00:00
|
|
|
|
2021-11-05 19:17:13 +00:00
|
|
|
if (dst_inode)
|
2023-09-12 21:16:02 +00:00
|
|
|
bch2_inode_update_after_write(trans, dst_inode, &dst_inode_u,
|
2017-03-17 06:18:50 +00:00
|
|
|
ATTR_CTIME);
|
|
|
|
err:
|
2023-09-12 21:16:02 +00:00
|
|
|
bch2_trans_put(trans);
|
2018-12-17 10:31:49 +00:00
|
|
|
|
2019-10-02 22:35:36 +00:00
|
|
|
bch2_fs_quota_transfer(c, src_inode,
|
|
|
|
bch_qid(&src_inode->ei_inode),
|
2018-12-17 10:31:49 +00:00
|
|
|
1 << QTYP_PRJ,
|
|
|
|
KEY_TYPE_QUOTA_NOCHECK);
|
2019-10-02 22:35:36 +00:00
|
|
|
if (dst_inode)
|
|
|
|
bch2_fs_quota_transfer(c, dst_inode,
|
|
|
|
bch_qid(&dst_inode->ei_inode),
|
2018-12-17 10:31:49 +00:00
|
|
|
1 << QTYP_PRJ,
|
|
|
|
KEY_TYPE_QUOTA_NOCHECK);
|
|
|
|
|
2019-06-24 22:24:38 +00:00
|
|
|
bch2_unlock_inodes(INODE_UPDATE_LOCK,
|
2019-10-02 22:35:36 +00:00
|
|
|
src_dir,
|
|
|
|
dst_dir,
|
|
|
|
src_inode,
|
|
|
|
dst_inode);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2024-02-09 23:34:05 +00:00
|
|
|
return bch2_err_class(ret);
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
2019-10-01 20:51:57 +00:00
|
|
|
static void bch2_setattr_copy(struct mnt_idmap *idmap,
|
|
|
|
struct bch_inode_info *inode,
|
|
|
|
struct bch_inode_unpacked *bi,
|
|
|
|
struct iattr *attr)
|
2017-03-17 06:18:50 +00:00
|
|
|
{
|
|
|
|
struct bch_fs *c = inode->v.i_sb->s_fs_info;
|
2019-10-01 20:51:57 +00:00
|
|
|
unsigned int ia_valid = attr->ia_valid;
|
2024-08-24 01:27:24 +00:00
|
|
|
kuid_t kuid;
|
|
|
|
kgid_t kgid;
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2024-08-24 01:27:24 +00:00
|
|
|
if (ia_valid & ATTR_UID) {
|
|
|
|
kuid = from_vfsuid(idmap, i_user_ns(&inode->v), attr->ia_vfsuid);
|
|
|
|
bi->bi_uid = from_kuid(i_user_ns(&inode->v), kuid);
|
|
|
|
}
|
|
|
|
if (ia_valid & ATTR_GID) {
|
|
|
|
kgid = from_vfsgid(idmap, i_user_ns(&inode->v), attr->ia_vfsgid);
|
|
|
|
bi->bi_gid = from_kgid(i_user_ns(&inode->v), kgid);
|
|
|
|
}
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2021-06-15 02:29:54 +00:00
|
|
|
if (ia_valid & ATTR_SIZE)
|
|
|
|
bi->bi_size = attr->ia_size;
|
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
if (ia_valid & ATTR_ATIME)
|
2019-10-01 20:51:57 +00:00
|
|
|
bi->bi_atime = timespec_to_bch2_time(c, attr->ia_atime);
|
2017-03-17 06:18:50 +00:00
|
|
|
if (ia_valid & ATTR_MTIME)
|
2019-10-01 20:51:57 +00:00
|
|
|
bi->bi_mtime = timespec_to_bch2_time(c, attr->ia_mtime);
|
2017-03-17 06:18:50 +00:00
|
|
|
if (ia_valid & ATTR_CTIME)
|
2019-10-01 20:51:57 +00:00
|
|
|
bi->bi_ctime = timespec_to_bch2_time(c, attr->ia_ctime);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
if (ia_valid & ATTR_MODE) {
|
2019-10-01 20:51:57 +00:00
|
|
|
umode_t mode = attr->ia_mode;
|
2017-03-17 06:18:50 +00:00
|
|
|
kgid_t gid = ia_valid & ATTR_GID
|
2024-08-24 01:27:24 +00:00
|
|
|
? kgid
|
2017-03-17 06:18:50 +00:00
|
|
|
: inode->v.i_gid;
|
|
|
|
|
2024-08-24 01:27:24 +00:00
|
|
|
if (!in_group_or_capable(idmap, &inode->v,
|
|
|
|
make_vfsgid(idmap, i_user_ns(&inode->v), gid)))
|
2017-03-17 06:18:50 +00:00
|
|
|
mode &= ~S_ISGID;
|
|
|
|
bi->bi_mode = mode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-15 02:29:54 +00:00
|
|
|
int bch2_setattr_nonsize(struct mnt_idmap *idmap,
|
|
|
|
struct bch_inode_info *inode,
|
|
|
|
struct iattr *attr)
|
2017-03-17 06:18:50 +00:00
|
|
|
{
|
|
|
|
struct bch_fs *c = inode->v.i_sb->s_fs_info;
|
2018-12-17 10:43:00 +00:00
|
|
|
struct bch_qid qid;
|
2023-09-12 21:16:02 +00:00
|
|
|
struct btree_trans *trans;
|
2021-08-30 19:18:31 +00:00
|
|
|
struct btree_iter inode_iter = { NULL };
|
2017-03-17 06:18:50 +00:00
|
|
|
struct bch_inode_unpacked inode_u;
|
|
|
|
struct posix_acl *acl = NULL;
|
2024-08-24 01:27:24 +00:00
|
|
|
kuid_t kuid;
|
|
|
|
kgid_t kgid;
|
2017-03-17 06:18:50 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
mutex_lock(&inode->ei_update_lock);
|
|
|
|
|
2018-12-17 10:43:00 +00:00
|
|
|
qid = inode->ei_qid;
|
|
|
|
|
2024-08-24 01:27:24 +00:00
|
|
|
if (attr->ia_valid & ATTR_UID) {
|
|
|
|
kuid = from_vfsuid(idmap, i_user_ns(&inode->v), attr->ia_vfsuid);
|
|
|
|
qid.q[QTYP_USR] = from_kuid(i_user_ns(&inode->v), kuid);
|
|
|
|
}
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2024-08-24 01:27:24 +00:00
|
|
|
if (attr->ia_valid & ATTR_GID) {
|
|
|
|
kgid = from_vfsgid(idmap, i_user_ns(&inode->v), attr->ia_vfsgid);
|
|
|
|
qid.q[QTYP_GRP] = from_kgid(i_user_ns(&inode->v), kgid);
|
|
|
|
}
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2018-12-17 10:43:00 +00:00
|
|
|
ret = bch2_fs_quota_transfer(c, inode, qid, ~0,
|
|
|
|
KEY_TYPE_QUOTA_PREALLOC);
|
|
|
|
if (ret)
|
|
|
|
goto err;
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2023-09-12 21:16:02 +00:00
|
|
|
trans = bch2_trans_get(c);
|
2017-03-17 06:18:50 +00:00
|
|
|
retry:
|
2023-09-12 21:16:02 +00:00
|
|
|
bch2_trans_begin(trans);
|
2017-03-17 06:18:50 +00:00
|
|
|
kfree(acl);
|
|
|
|
acl = NULL;
|
|
|
|
|
2023-09-12 21:16:02 +00:00
|
|
|
ret = bch2_inode_peek(trans, &inode_iter, &inode_u, inode_inum(inode),
|
2024-04-07 22:05:34 +00:00
|
|
|
BTREE_ITER_intent);
|
2019-10-01 20:51:57 +00:00
|
|
|
if (ret)
|
|
|
|
goto btree_err;
|
|
|
|
|
|
|
|
bch2_setattr_copy(idmap, inode, &inode_u, attr);
|
|
|
|
|
|
|
|
if (attr->ia_valid & ATTR_MODE) {
|
2023-09-12 21:16:02 +00:00
|
|
|
ret = bch2_acl_chmod(trans, inode_inum(inode), &inode_u,
|
2021-03-16 04:28:17 +00:00
|
|
|
inode_u.bi_mode, &acl);
|
2019-10-01 20:51:57 +00:00
|
|
|
if (ret)
|
|
|
|
goto btree_err;
|
|
|
|
}
|
|
|
|
|
2023-09-12 21:16:02 +00:00
|
|
|
ret = bch2_inode_write(trans, &inode_iter, &inode_u) ?:
|
|
|
|
bch2_trans_commit(trans, NULL, NULL,
|
2023-11-11 21:31:50 +00:00
|
|
|
BCH_TRANS_COMMIT_no_enospc);
|
2019-10-01 20:51:57 +00:00
|
|
|
btree_err:
|
2023-09-12 21:16:02 +00:00
|
|
|
bch2_trans_iter_exit(trans, &inode_iter);
|
2021-03-20 00:29:11 +00:00
|
|
|
|
2022-07-18 03:06:38 +00:00
|
|
|
if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
|
2017-03-17 06:18:50 +00:00
|
|
|
goto retry;
|
|
|
|
if (unlikely(ret))
|
|
|
|
goto err_trans;
|
|
|
|
|
2023-09-12 21:16:02 +00:00
|
|
|
bch2_inode_update_after_write(trans, inode, &inode_u, attr->ia_valid);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
if (acl)
|
|
|
|
set_cached_acl(&inode->v, ACL_TYPE_ACCESS, acl);
|
|
|
|
err_trans:
|
2023-09-12 21:16:02 +00:00
|
|
|
bch2_trans_put(trans);
|
2017-03-17 06:18:50 +00:00
|
|
|
err:
|
|
|
|
mutex_unlock(&inode->ei_update_lock);
|
|
|
|
|
2022-09-18 19:43:50 +00:00
|
|
|
return bch2_err_class(ret);
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int bch2_getattr(struct mnt_idmap *idmap,
|
|
|
|
const struct path *path, struct kstat *stat,
|
|
|
|
u32 request_mask, unsigned query_flags)
|
|
|
|
{
|
|
|
|
struct bch_inode_info *inode = to_bch_ei(d_inode(path->dentry));
|
|
|
|
struct bch_fs *c = inode->v.i_sb->s_fs_info;
|
2024-08-24 01:27:24 +00:00
|
|
|
vfsuid_t vfsuid = i_uid_into_vfsuid(idmap, &inode->v);
|
|
|
|
vfsgid_t vfsgid = i_gid_into_vfsgid(idmap, &inode->v);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
stat->dev = inode->v.i_sb->s_dev;
|
|
|
|
stat->ino = inode->v.i_ino;
|
|
|
|
stat->mode = inode->v.i_mode;
|
|
|
|
stat->nlink = inode->v.i_nlink;
|
2024-08-24 01:27:24 +00:00
|
|
|
stat->uid = vfsuid_into_kuid(vfsuid);
|
|
|
|
stat->gid = vfsgid_into_kgid(vfsgid);
|
2017-03-17 06:18:50 +00:00
|
|
|
stat->rdev = inode->v.i_rdev;
|
|
|
|
stat->size = i_size_read(&inode->v);
|
2023-10-30 21:09:38 +00:00
|
|
|
stat->atime = inode_get_atime(&inode->v);
|
|
|
|
stat->mtime = inode_get_mtime(&inode->v);
|
2017-03-17 06:18:50 +00:00
|
|
|
stat->ctime = inode_get_ctime(&inode->v);
|
|
|
|
stat->blksize = block_bytes(c);
|
|
|
|
stat->blocks = inode->v.i_blocks;
|
|
|
|
|
2024-06-09 01:41:01 +00:00
|
|
|
stat->subvol = inode->ei_inum.subvol;
|
2024-03-08 02:29:12 +00:00
|
|
|
stat->result_mask |= STATX_SUBVOL;
|
|
|
|
|
2024-06-20 13:21:12 +00:00
|
|
|
if ((request_mask & STATX_DIOALIGN) && S_ISREG(inode->v.i_mode)) {
|
|
|
|
stat->result_mask |= STATX_DIOALIGN;
|
|
|
|
/*
|
|
|
|
* this is incorrect; we should be tracking this in superblock,
|
|
|
|
* and checking the alignment of open devices
|
|
|
|
*/
|
|
|
|
stat->dio_mem_align = SECTOR_SIZE;
|
|
|
|
stat->dio_offset_align = block_bytes(c);
|
|
|
|
}
|
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
if (request_mask & STATX_BTIME) {
|
|
|
|
stat->result_mask |= STATX_BTIME;
|
|
|
|
stat->btime = bch2_time_to_timespec(c, inode->ei_inode.bi_otime);
|
|
|
|
}
|
|
|
|
|
2023-11-02 15:42:48 +00:00
|
|
|
if (inode->ei_inode.bi_flags & BCH_INODE_immutable)
|
2017-03-17 06:18:50 +00:00
|
|
|
stat->attributes |= STATX_ATTR_IMMUTABLE;
|
2019-11-06 19:29:30 +00:00
|
|
|
stat->attributes_mask |= STATX_ATTR_IMMUTABLE;
|
|
|
|
|
2023-11-02 15:42:48 +00:00
|
|
|
if (inode->ei_inode.bi_flags & BCH_INODE_append)
|
2017-03-17 06:18:50 +00:00
|
|
|
stat->attributes |= STATX_ATTR_APPEND;
|
2019-11-06 19:29:30 +00:00
|
|
|
stat->attributes_mask |= STATX_ATTR_APPEND;
|
|
|
|
|
2023-11-02 15:42:48 +00:00
|
|
|
if (inode->ei_inode.bi_flags & BCH_INODE_nodump)
|
2017-03-17 06:18:50 +00:00
|
|
|
stat->attributes |= STATX_ATTR_NODUMP;
|
2019-11-06 19:29:30 +00:00
|
|
|
stat->attributes_mask |= STATX_ATTR_NODUMP;
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bch2_setattr(struct mnt_idmap *idmap,
|
|
|
|
struct dentry *dentry, struct iattr *iattr)
|
|
|
|
{
|
|
|
|
struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
|
2023-12-30 02:16:32 +00:00
|
|
|
struct bch_fs *c = inode->v.i_sb->s_fs_info;
|
2017-03-17 06:18:50 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
lockdep_assert_held(&inode->v.i_rwsem);
|
|
|
|
|
2024-06-09 01:41:01 +00:00
|
|
|
ret = bch2_subvol_is_ro(c, inode->ei_inum.subvol) ?:
|
2023-12-30 02:16:32 +00:00
|
|
|
setattr_prepare(idmap, dentry, iattr);
|
2017-03-17 06:18:50 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return iattr->ia_valid & ATTR_SIZE
|
2023-09-04 09:38:30 +00:00
|
|
|
? bchfs_truncate(idmap, inode, iattr)
|
2017-03-17 06:18:50 +00:00
|
|
|
: bch2_setattr_nonsize(idmap, inode, iattr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bch2_tmpfile(struct mnt_idmap *idmap,
|
|
|
|
struct inode *vdir, struct file *file, umode_t mode)
|
|
|
|
{
|
|
|
|
struct bch_inode_info *inode =
|
|
|
|
__bch2_create(idmap, to_bch_ei(vdir),
|
2021-03-16 04:28:17 +00:00
|
|
|
file->f_path.dentry, mode, 0,
|
2021-03-17 03:28:43 +00:00
|
|
|
(subvol_inum) { 0 }, BCH_CREATE_TMPFILE);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
if (IS_ERR(inode))
|
2022-09-18 19:43:50 +00:00
|
|
|
return bch2_err_class(PTR_ERR(inode));
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
d_mark_tmpfile(file, &inode->v);
|
|
|
|
d_instantiate(file->f_path.dentry, &inode->v);
|
|
|
|
return finish_open_simple(file, 0);
|
|
|
|
}
|
|
|
|
|
2019-06-29 21:59:21 +00:00
|
|
|
static int bch2_fill_extent(struct bch_fs *c,
|
|
|
|
struct fiemap_extent_info *info,
|
2019-07-25 17:52:14 +00:00
|
|
|
struct bkey_s_c k, unsigned flags)
|
2017-03-17 06:18:50 +00:00
|
|
|
{
|
2020-10-26 21:03:28 +00:00
|
|
|
if (bkey_extent_is_direct_data(k.k)) {
|
2019-07-25 17:52:14 +00:00
|
|
|
struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
|
2018-09-28 01:08:39 +00:00
|
|
|
const union bch_extent_entry *entry;
|
|
|
|
struct extent_ptr_decoded p;
|
2017-03-17 06:18:50 +00:00
|
|
|
int ret;
|
|
|
|
|
2019-08-16 13:59:56 +00:00
|
|
|
if (k.k->type == KEY_TYPE_reflink_v)
|
|
|
|
flags |= FIEMAP_EXTENT_SHARED;
|
|
|
|
|
2019-07-25 17:52:14 +00:00
|
|
|
bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
|
2017-03-17 06:18:50 +00:00
|
|
|
int flags2 = 0;
|
2018-09-28 01:08:39 +00:00
|
|
|
u64 offset = p.ptr.offset;
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2022-11-13 23:59:01 +00:00
|
|
|
if (p.ptr.unwritten)
|
|
|
|
flags2 |= FIEMAP_EXTENT_UNWRITTEN;
|
|
|
|
|
2018-09-28 01:08:39 +00:00
|
|
|
if (p.crc.compression_type)
|
2017-03-17 06:18:50 +00:00
|
|
|
flags2 |= FIEMAP_EXTENT_ENCODED;
|
|
|
|
else
|
2018-09-28 01:08:39 +00:00
|
|
|
offset += p.crc.offset;
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2021-12-14 19:24:41 +00:00
|
|
|
if ((offset & (block_sectors(c) - 1)) ||
|
|
|
|
(k.k->size & (block_sectors(c) - 1)))
|
2017-03-17 06:18:50 +00:00
|
|
|
flags2 |= FIEMAP_EXTENT_NOT_ALIGNED;
|
|
|
|
|
|
|
|
ret = fiemap_fill_next_extent(info,
|
2019-07-25 17:52:14 +00:00
|
|
|
bkey_start_offset(k.k) << 9,
|
2018-09-28 01:08:39 +00:00
|
|
|
offset << 9,
|
2019-07-25 17:52:14 +00:00
|
|
|
k.k->size << 9, flags|flags2);
|
2017-03-17 06:18:50 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2020-10-26 21:03:28 +00:00
|
|
|
} else if (bkey_extent_is_inline_data(k.k)) {
|
|
|
|
return fiemap_fill_next_extent(info,
|
|
|
|
bkey_start_offset(k.k) << 9,
|
|
|
|
0, k.k->size << 9,
|
|
|
|
flags|
|
|
|
|
FIEMAP_EXTENT_DATA_INLINE);
|
2019-07-25 17:52:14 +00:00
|
|
|
} else if (k.k->type == KEY_TYPE_reservation) {
|
2017-03-17 06:18:50 +00:00
|
|
|
return fiemap_fill_next_extent(info,
|
2019-07-25 17:52:14 +00:00
|
|
|
bkey_start_offset(k.k) << 9,
|
|
|
|
0, k.k->size << 9,
|
2017-03-17 06:18:50 +00:00
|
|
|
flags|
|
|
|
|
FIEMAP_EXTENT_DELALLOC|
|
|
|
|
FIEMAP_EXTENT_UNWRITTEN);
|
|
|
|
} else {
|
|
|
|
BUG();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bch2_fiemap(struct inode *vinode, struct fiemap_extent_info *info,
|
|
|
|
u64 start, u64 len)
|
|
|
|
{
|
|
|
|
struct bch_fs *c = vinode->i_sb->s_fs_info;
|
|
|
|
struct bch_inode_info *ei = to_bch_ei(vinode);
|
2023-09-12 21:16:02 +00:00
|
|
|
struct btree_trans *trans;
|
2021-08-30 19:18:31 +00:00
|
|
|
struct btree_iter iter;
|
2017-03-17 06:18:50 +00:00
|
|
|
struct bkey_s_c k;
|
2020-12-17 20:08:58 +00:00
|
|
|
struct bkey_buf cur, prev;
|
2019-08-16 13:59:56 +00:00
|
|
|
unsigned offset_into_extent, sectors;
|
2017-03-17 06:18:50 +00:00
|
|
|
bool have_extent = false;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
ret = fiemap_prep(&ei->v, info, start, &len, FIEMAP_FLAG_SYNC);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2024-05-04 22:12:23 +00:00
|
|
|
struct bpos end = POS(ei->v.i_ino, (start + len) >> 9);
|
2017-03-17 06:18:50 +00:00
|
|
|
if (start + len < start)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2021-03-16 04:28:17 +00:00
|
|
|
start >>= 9;
|
|
|
|
|
2020-12-17 20:08:58 +00:00
|
|
|
bch2_bkey_buf_init(&cur);
|
|
|
|
bch2_bkey_buf_init(&prev);
|
2023-09-12 21:16:02 +00:00
|
|
|
trans = bch2_trans_get(c);
|
2021-03-16 04:28:17 +00:00
|
|
|
|
2023-09-12 21:16:02 +00:00
|
|
|
bch2_trans_iter_init(trans, &iter, BTREE_ID_extents,
|
2024-07-17 15:50:54 +00:00
|
|
|
POS(ei->v.i_ino, start), 0);
|
2021-03-16 04:28:17 +00:00
|
|
|
|
2024-10-15 03:32:23 +00:00
|
|
|
while (!ret || bch2_err_matches(ret, BCH_ERR_transaction_restart)) {
|
2021-03-15 01:30:08 +00:00
|
|
|
enum btree_id data_btree = BTREE_ID_extents;
|
|
|
|
|
2024-07-17 15:50:54 +00:00
|
|
|
bch2_trans_begin(trans);
|
|
|
|
|
|
|
|
u32 snapshot;
|
|
|
|
ret = bch2_subvolume_get_snapshot(trans, ei->ei_inum.subvol, &snapshot);
|
|
|
|
if (ret)
|
2024-10-15 03:32:23 +00:00
|
|
|
continue;
|
2024-07-17 15:50:54 +00:00
|
|
|
|
|
|
|
bch2_btree_iter_set_snapshot(&iter, snapshot);
|
|
|
|
|
|
|
|
k = bch2_btree_iter_peek_upto(&iter, end);
|
|
|
|
ret = bkey_err(k);
|
|
|
|
if (ret)
|
2024-10-15 03:32:23 +00:00
|
|
|
continue;
|
2024-07-17 15:50:54 +00:00
|
|
|
|
|
|
|
if (!k.k)
|
|
|
|
break;
|
|
|
|
|
2019-08-22 20:12:28 +00:00
|
|
|
if (!bkey_extent_is_data(k.k) &&
|
|
|
|
k.k->type != KEY_TYPE_reservation) {
|
2021-08-30 19:18:31 +00:00
|
|
|
bch2_btree_iter_advance(&iter);
|
2019-08-22 20:12:28 +00:00
|
|
|
continue;
|
|
|
|
}
|
2019-07-25 17:52:14 +00:00
|
|
|
|
2021-08-30 19:18:31 +00:00
|
|
|
offset_into_extent = iter.pos.offset -
|
2019-08-16 13:59:56 +00:00
|
|
|
bkey_start_offset(k.k);
|
|
|
|
sectors = k.k->size - offset_into_extent;
|
|
|
|
|
2020-12-17 20:08:58 +00:00
|
|
|
bch2_bkey_buf_reassemble(&cur, c, k);
|
2020-10-25 00:56:47 +00:00
|
|
|
|
2023-09-12 21:16:02 +00:00
|
|
|
ret = bch2_read_indirect_extent(trans, &data_btree,
|
2020-05-22 14:50:05 +00:00
|
|
|
&offset_into_extent, &cur);
|
2019-08-16 13:59:56 +00:00
|
|
|
if (ret)
|
2024-10-15 03:32:23 +00:00
|
|
|
continue;
|
2019-08-16 13:59:56 +00:00
|
|
|
|
2020-10-25 00:56:47 +00:00
|
|
|
k = bkey_i_to_s_c(cur.k);
|
2020-12-17 20:08:58 +00:00
|
|
|
bch2_bkey_buf_realloc(&prev, c, k.k->u64s);
|
2020-10-25 00:56:47 +00:00
|
|
|
|
2019-08-16 13:59:56 +00:00
|
|
|
sectors = min(sectors, k.k->size - offset_into_extent);
|
|
|
|
|
2020-10-26 21:03:28 +00:00
|
|
|
bch2_cut_front(POS(k.k->p.inode,
|
|
|
|
bkey_start_offset(k.k) +
|
|
|
|
offset_into_extent),
|
|
|
|
cur.k);
|
2019-11-09 21:01:15 +00:00
|
|
|
bch2_key_resize(&cur.k->k, sectors);
|
2021-08-30 19:18:31 +00:00
|
|
|
cur.k->k.p = iter.pos;
|
2019-11-09 21:01:15 +00:00
|
|
|
cur.k->k.p.offset += cur.k->k.size;
|
2019-08-16 13:59:56 +00:00
|
|
|
|
2019-08-22 20:12:28 +00:00
|
|
|
if (have_extent) {
|
2023-09-12 21:16:02 +00:00
|
|
|
bch2_trans_unlock(trans);
|
2019-08-22 20:12:28 +00:00
|
|
|
ret = bch2_fill_extent(c, info,
|
2019-11-09 21:01:15 +00:00
|
|
|
bkey_i_to_s_c(prev.k), 0);
|
2019-08-22 20:12:28 +00:00
|
|
|
if (ret)
|
|
|
|
break;
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
2019-08-16 13:59:56 +00:00
|
|
|
|
2019-11-09 21:01:15 +00:00
|
|
|
bkey_copy(prev.k, cur.k);
|
2019-08-22 20:12:28 +00:00
|
|
|
have_extent = true;
|
|
|
|
|
2021-08-30 19:18:31 +00:00
|
|
|
bch2_btree_iter_set_pos(&iter,
|
|
|
|
POS(iter.pos.inode, iter.pos.offset + sectors));
|
2019-07-25 17:52:14 +00:00
|
|
|
}
|
2023-09-12 21:16:02 +00:00
|
|
|
bch2_trans_iter_exit(trans, &iter);
|
2019-08-22 20:12:28 +00:00
|
|
|
|
2023-06-20 01:12:05 +00:00
|
|
|
if (!ret && have_extent) {
|
2023-09-12 21:16:02 +00:00
|
|
|
bch2_trans_unlock(trans);
|
2019-11-09 21:01:15 +00:00
|
|
|
ret = bch2_fill_extent(c, info, bkey_i_to_s_c(prev.k),
|
2019-07-25 17:52:14 +00:00
|
|
|
FIEMAP_EXTENT_LAST);
|
2023-06-20 01:12:05 +00:00
|
|
|
}
|
2019-08-22 20:12:28 +00:00
|
|
|
|
2023-09-12 21:16:02 +00:00
|
|
|
bch2_trans_put(trans);
|
2020-12-17 20:08:58 +00:00
|
|
|
bch2_bkey_buf_exit(&cur, c);
|
|
|
|
bch2_bkey_buf_exit(&prev, c);
|
2017-03-17 06:18:50 +00:00
|
|
|
return ret < 0 ? ret : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct vm_operations_struct bch_vm_ops = {
|
|
|
|
.fault = bch2_page_fault,
|
|
|
|
.map_pages = filemap_map_pages,
|
|
|
|
.page_mkwrite = bch2_page_mkwrite,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int bch2_mmap(struct file *file, struct vm_area_struct *vma)
|
|
|
|
{
|
|
|
|
file_accessed(file);
|
|
|
|
|
|
|
|
vma->vm_ops = &bch_vm_ops;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Directories: */
|
|
|
|
|
|
|
|
static loff_t bch2_dir_llseek(struct file *file, loff_t offset, int whence)
|
|
|
|
{
|
|
|
|
return generic_file_llseek_size(file, offset, whence,
|
|
|
|
S64_MAX, S64_MAX);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bch2_vfs_readdir(struct file *file, struct dir_context *ctx)
|
|
|
|
{
|
2019-10-02 22:35:36 +00:00
|
|
|
struct bch_inode_info *inode = file_bch_inode(file);
|
|
|
|
struct bch_fs *c = inode->v.i_sb->s_fs_info;
|
|
|
|
|
|
|
|
if (!dir_emit_dots(file, ctx))
|
|
|
|
return 0;
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2023-12-17 03:43:41 +00:00
|
|
|
int ret = bch2_readdir(c, inode_inum(inode), ctx);
|
2023-08-06 14:04:05 +00:00
|
|
|
|
2023-12-17 03:43:41 +00:00
|
|
|
bch_err_fn(c, ret);
|
2023-08-06 14:04:05 +00:00
|
|
|
return bch2_err_class(ret);
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
2023-12-30 02:16:32 +00:00
|
|
|
static int bch2_open(struct inode *vinode, struct file *file)
|
|
|
|
{
|
|
|
|
if (file->f_flags & (O_WRONLY|O_RDWR)) {
|
|
|
|
struct bch_inode_info *inode = to_bch_ei(vinode);
|
|
|
|
struct bch_fs *c = inode->v.i_sb->s_fs_info;
|
|
|
|
|
2024-06-09 01:41:01 +00:00
|
|
|
int ret = bch2_subvol_is_ro(c, inode->ei_inum.subvol);
|
2023-12-30 02:16:32 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-05-10 06:10:58 +00:00
|
|
|
file->f_mode |= FMODE_CAN_ODIRECT;
|
|
|
|
|
2023-12-30 02:16:32 +00:00
|
|
|
return generic_file_open(vinode, file);
|
|
|
|
}
|
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
static const struct file_operations bch_file_operations = {
|
2023-12-30 02:16:32 +00:00
|
|
|
.open = bch2_open,
|
2017-03-17 06:18:50 +00:00
|
|
|
.llseek = bch2_llseek,
|
|
|
|
.read_iter = bch2_read_iter,
|
|
|
|
.write_iter = bch2_write_iter,
|
|
|
|
.mmap = bch2_mmap,
|
2024-06-20 01:22:42 +00:00
|
|
|
.get_unmapped_area = thp_get_unmapped_area,
|
2017-03-17 06:18:50 +00:00
|
|
|
.fsync = bch2_fsync,
|
|
|
|
.splice_read = filemap_splice_read,
|
|
|
|
.splice_write = iter_file_splice_write,
|
|
|
|
.fallocate = bch2_fallocate_dispatch,
|
|
|
|
.unlocked_ioctl = bch2_fs_file_ioctl,
|
|
|
|
#ifdef CONFIG_COMPAT
|
|
|
|
.compat_ioctl = bch2_compat_fs_ioctl,
|
|
|
|
#endif
|
2019-08-16 13:59:56 +00:00
|
|
|
.remap_file_range = bch2_remap_file_range,
|
2017-03-17 06:18:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct inode_operations bch_file_inode_operations = {
|
|
|
|
.getattr = bch2_getattr,
|
|
|
|
.setattr = bch2_setattr,
|
|
|
|
.fiemap = bch2_fiemap,
|
|
|
|
.listxattr = bch2_xattr_list,
|
|
|
|
#ifdef CONFIG_BCACHEFS_POSIX_ACL
|
2024-08-07 19:42:23 +00:00
|
|
|
.get_inode_acl = bch2_get_acl,
|
2017-03-17 06:18:50 +00:00
|
|
|
.set_acl = bch2_set_acl,
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct inode_operations bch_dir_inode_operations = {
|
|
|
|
.lookup = bch2_lookup,
|
|
|
|
.create = bch2_create,
|
|
|
|
.link = bch2_link,
|
|
|
|
.unlink = bch2_unlink,
|
|
|
|
.symlink = bch2_symlink,
|
|
|
|
.mkdir = bch2_mkdir,
|
2019-10-11 19:14:36 +00:00
|
|
|
.rmdir = bch2_unlink,
|
2017-03-17 06:18:50 +00:00
|
|
|
.mknod = bch2_mknod,
|
|
|
|
.rename = bch2_rename2,
|
|
|
|
.getattr = bch2_getattr,
|
|
|
|
.setattr = bch2_setattr,
|
|
|
|
.tmpfile = bch2_tmpfile,
|
|
|
|
.listxattr = bch2_xattr_list,
|
|
|
|
#ifdef CONFIG_BCACHEFS_POSIX_ACL
|
2024-08-07 19:42:23 +00:00
|
|
|
.get_inode_acl = bch2_get_acl,
|
2017-03-17 06:18:50 +00:00
|
|
|
.set_acl = bch2_set_acl,
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct file_operations bch_dir_file_operations = {
|
|
|
|
.llseek = bch2_dir_llseek,
|
|
|
|
.read = generic_read_dir,
|
|
|
|
.iterate_shared = bch2_vfs_readdir,
|
|
|
|
.fsync = bch2_fsync,
|
|
|
|
.unlocked_ioctl = bch2_fs_file_ioctl,
|
|
|
|
#ifdef CONFIG_COMPAT
|
|
|
|
.compat_ioctl = bch2_compat_fs_ioctl,
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct inode_operations bch_symlink_inode_operations = {
|
|
|
|
.get_link = page_get_link,
|
|
|
|
.getattr = bch2_getattr,
|
|
|
|
.setattr = bch2_setattr,
|
|
|
|
.listxattr = bch2_xattr_list,
|
|
|
|
#ifdef CONFIG_BCACHEFS_POSIX_ACL
|
2024-08-07 19:42:23 +00:00
|
|
|
.get_inode_acl = bch2_get_acl,
|
2017-03-17 06:18:50 +00:00
|
|
|
.set_acl = bch2_set_acl,
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct inode_operations bch_special_inode_operations = {
|
|
|
|
.getattr = bch2_getattr,
|
|
|
|
.setattr = bch2_setattr,
|
|
|
|
.listxattr = bch2_xattr_list,
|
|
|
|
#ifdef CONFIG_BCACHEFS_POSIX_ACL
|
2024-08-07 19:42:23 +00:00
|
|
|
.get_inode_acl = bch2_get_acl,
|
2017-03-17 06:18:50 +00:00
|
|
|
.set_acl = bch2_set_acl,
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct address_space_operations bch_address_space_operations = {
|
|
|
|
.read_folio = bch2_read_folio,
|
|
|
|
.writepages = bch2_writepages,
|
|
|
|
.readahead = bch2_readahead,
|
2019-07-02 18:59:15 +00:00
|
|
|
.dirty_folio = filemap_dirty_folio,
|
2017-03-17 06:18:50 +00:00
|
|
|
.write_begin = bch2_write_begin,
|
|
|
|
.write_end = bch2_write_end,
|
|
|
|
.invalidate_folio = bch2_invalidate_folio,
|
|
|
|
.release_folio = bch2_release_folio,
|
|
|
|
#ifdef CONFIG_MIGRATION
|
|
|
|
.migrate_folio = filemap_migrate_folio,
|
|
|
|
#endif
|
2023-11-17 16:14:47 +00:00
|
|
|
.error_remove_folio = generic_error_remove_folio,
|
2017-03-17 06:18:50 +00:00
|
|
|
};
|
|
|
|
|
2021-11-14 00:49:14 +00:00
|
|
|
struct bcachefs_fid {
|
|
|
|
u64 inum;
|
|
|
|
u32 subvol;
|
|
|
|
u32 gen;
|
|
|
|
} __packed;
|
|
|
|
|
|
|
|
struct bcachefs_fid_with_parent {
|
|
|
|
struct bcachefs_fid fid;
|
|
|
|
struct bcachefs_fid dir;
|
|
|
|
} __packed;
|
|
|
|
|
|
|
|
static int bcachefs_fid_valid(int fh_len, int fh_type)
|
2017-03-17 06:18:50 +00:00
|
|
|
{
|
2021-11-14 00:49:14 +00:00
|
|
|
switch (fh_type) {
|
|
|
|
case FILEID_BCACHEFS_WITHOUT_PARENT:
|
|
|
|
return fh_len == sizeof(struct bcachefs_fid) / sizeof(u32);
|
|
|
|
case FILEID_BCACHEFS_WITH_PARENT:
|
|
|
|
return fh_len == sizeof(struct bcachefs_fid_with_parent) / sizeof(u32);
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct bcachefs_fid bch2_inode_to_fid(struct bch_inode_info *inode)
|
|
|
|
{
|
|
|
|
return (struct bcachefs_fid) {
|
2024-06-09 01:41:01 +00:00
|
|
|
.inum = inode->ei_inum.inum,
|
|
|
|
.subvol = inode->ei_inum.subvol,
|
2021-11-14 00:49:14 +00:00
|
|
|
.gen = inode->ei_inode.bi_generation,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bch2_encode_fh(struct inode *vinode, u32 *fh, int *len,
|
|
|
|
struct inode *vdir)
|
|
|
|
{
|
|
|
|
struct bch_inode_info *inode = to_bch_ei(vinode);
|
|
|
|
struct bch_inode_info *dir = to_bch_ei(vdir);
|
2023-12-13 16:51:04 +00:00
|
|
|
int min_len;
|
2021-11-14 00:49:14 +00:00
|
|
|
|
|
|
|
if (!S_ISDIR(inode->v.i_mode) && dir) {
|
|
|
|
struct bcachefs_fid_with_parent *fid = (void *) fh;
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2023-12-13 16:51:04 +00:00
|
|
|
min_len = sizeof(*fid) / sizeof(u32);
|
|
|
|
if (*len < min_len) {
|
|
|
|
*len = min_len;
|
|
|
|
return FILEID_INVALID;
|
|
|
|
}
|
|
|
|
|
2021-11-14 00:49:14 +00:00
|
|
|
fid->fid = bch2_inode_to_fid(inode);
|
|
|
|
fid->dir = bch2_inode_to_fid(dir);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2023-12-13 16:51:04 +00:00
|
|
|
*len = min_len;
|
2021-11-14 00:49:14 +00:00
|
|
|
return FILEID_BCACHEFS_WITH_PARENT;
|
|
|
|
} else {
|
|
|
|
struct bcachefs_fid *fid = (void *) fh;
|
|
|
|
|
2023-12-13 16:51:04 +00:00
|
|
|
min_len = sizeof(*fid) / sizeof(u32);
|
|
|
|
if (*len < min_len) {
|
|
|
|
*len = min_len;
|
|
|
|
return FILEID_INVALID;
|
|
|
|
}
|
2021-11-14 00:49:14 +00:00
|
|
|
*fid = bch2_inode_to_fid(inode);
|
|
|
|
|
2023-12-13 16:51:04 +00:00
|
|
|
*len = min_len;
|
2021-11-14 00:49:14 +00:00
|
|
|
return FILEID_BCACHEFS_WITHOUT_PARENT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct inode *bch2_nfs_get_inode(struct super_block *sb,
|
|
|
|
struct bcachefs_fid fid)
|
|
|
|
{
|
|
|
|
struct bch_fs *c = sb->s_fs_info;
|
|
|
|
struct inode *vinode = bch2_vfs_inode_get(c, (subvol_inum) {
|
|
|
|
.subvol = fid.subvol,
|
|
|
|
.inum = fid.inum,
|
|
|
|
});
|
|
|
|
if (!IS_ERR(vinode) && vinode->i_generation != fid.gen) {
|
2017-03-17 06:18:50 +00:00
|
|
|
iput(vinode);
|
2021-11-14 00:49:14 +00:00
|
|
|
vinode = ERR_PTR(-ESTALE);
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
return vinode;
|
|
|
|
}
|
|
|
|
|
2021-11-14 00:49:14 +00:00
|
|
|
static struct dentry *bch2_fh_to_dentry(struct super_block *sb, struct fid *_fid,
|
2017-03-17 06:18:50 +00:00
|
|
|
int fh_len, int fh_type)
|
|
|
|
{
|
2021-11-14 00:49:14 +00:00
|
|
|
struct bcachefs_fid *fid = (void *) _fid;
|
|
|
|
|
|
|
|
if (!bcachefs_fid_valid(fh_len, fh_type))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return d_obtain_alias(bch2_nfs_get_inode(sb, *fid));
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
2021-11-14 00:49:14 +00:00
|
|
|
static struct dentry *bch2_fh_to_parent(struct super_block *sb, struct fid *_fid,
|
2017-03-17 06:18:50 +00:00
|
|
|
int fh_len, int fh_type)
|
|
|
|
{
|
2021-11-14 00:49:14 +00:00
|
|
|
struct bcachefs_fid_with_parent *fid = (void *) _fid;
|
|
|
|
|
|
|
|
if (!bcachefs_fid_valid(fh_len, fh_type) ||
|
|
|
|
fh_type != FILEID_BCACHEFS_WITH_PARENT)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return d_obtain_alias(bch2_nfs_get_inode(sb, fid->dir));
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct dentry *bch2_get_parent(struct dentry *child)
|
|
|
|
{
|
|
|
|
struct bch_inode_info *inode = to_bch_ei(child->d_inode);
|
|
|
|
struct bch_fs *c = inode->v.i_sb->s_fs_info;
|
|
|
|
subvol_inum parent_inum = {
|
|
|
|
.subvol = inode->ei_inode.bi_parent_subvol ?:
|
2024-06-09 01:41:01 +00:00
|
|
|
inode->ei_inum.subvol,
|
2021-11-14 00:49:14 +00:00
|
|
|
.inum = inode->ei_inode.bi_dir,
|
|
|
|
};
|
|
|
|
|
|
|
|
return d_obtain_alias(bch2_vfs_inode_get(c, parent_inum));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bch2_get_name(struct dentry *parent, char *name, struct dentry *child)
|
|
|
|
{
|
|
|
|
struct bch_inode_info *inode = to_bch_ei(child->d_inode);
|
|
|
|
struct bch_inode_info *dir = to_bch_ei(parent->d_inode);
|
|
|
|
struct bch_fs *c = inode->v.i_sb->s_fs_info;
|
2023-09-12 21:16:02 +00:00
|
|
|
struct btree_trans *trans;
|
2021-11-14 00:49:14 +00:00
|
|
|
struct btree_iter iter1;
|
|
|
|
struct btree_iter iter2;
|
|
|
|
struct bkey_s_c k;
|
|
|
|
struct bkey_s_c_dirent d;
|
|
|
|
struct bch_inode_unpacked inode_u;
|
|
|
|
subvol_inum target;
|
|
|
|
u32 snapshot;
|
2023-08-12 21:26:29 +00:00
|
|
|
struct qstr dirent_name;
|
|
|
|
unsigned name_len = 0;
|
2021-11-14 00:49:14 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!S_ISDIR(dir->v.i_mode))
|
|
|
|
return -EINVAL;
|
|
|
|
|
2023-09-12 21:16:02 +00:00
|
|
|
trans = bch2_trans_get(c);
|
2021-11-14 00:49:14 +00:00
|
|
|
|
2023-09-12 21:16:02 +00:00
|
|
|
bch2_trans_iter_init(trans, &iter1, BTREE_ID_dirents,
|
2021-11-14 00:49:14 +00:00
|
|
|
POS(dir->ei_inode.bi_inum, 0), 0);
|
2023-09-12 21:16:02 +00:00
|
|
|
bch2_trans_iter_init(trans, &iter2, BTREE_ID_dirents,
|
2021-11-14 00:49:14 +00:00
|
|
|
POS(dir->ei_inode.bi_inum, 0), 0);
|
|
|
|
retry:
|
2023-09-12 21:16:02 +00:00
|
|
|
bch2_trans_begin(trans);
|
2021-11-14 00:49:14 +00:00
|
|
|
|
2024-06-09 01:41:01 +00:00
|
|
|
ret = bch2_subvolume_get_snapshot(trans, dir->ei_inum.subvol, &snapshot);
|
2021-11-14 00:49:14 +00:00
|
|
|
if (ret)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
bch2_btree_iter_set_snapshot(&iter1, snapshot);
|
|
|
|
bch2_btree_iter_set_snapshot(&iter2, snapshot);
|
|
|
|
|
2023-09-12 21:16:02 +00:00
|
|
|
ret = bch2_inode_find_by_inum_trans(trans, inode_inum(inode), &inode_u);
|
2021-11-14 00:49:14 +00:00
|
|
|
if (ret)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (inode_u.bi_dir == dir->ei_inode.bi_inum) {
|
|
|
|
bch2_btree_iter_set_pos(&iter1, POS(inode_u.bi_dir, inode_u.bi_dir_offset));
|
|
|
|
|
|
|
|
k = bch2_btree_iter_peek_slot(&iter1);
|
|
|
|
ret = bkey_err(k);
|
|
|
|
if (ret)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (k.k->type != KEY_TYPE_dirent) {
|
2023-05-27 23:59:59 +00:00
|
|
|
ret = -BCH_ERR_ENOENT_dirent_doesnt_match_inode;
|
2021-11-14 00:49:14 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
d = bkey_s_c_to_dirent(k);
|
2023-09-12 21:16:02 +00:00
|
|
|
ret = bch2_dirent_read_target(trans, inode_inum(dir), d, &target);
|
2021-11-14 00:49:14 +00:00
|
|
|
if (ret > 0)
|
2023-05-27 23:59:59 +00:00
|
|
|
ret = -BCH_ERR_ENOENT_dirent_doesnt_match_inode;
|
2021-11-14 00:49:14 +00:00
|
|
|
if (ret)
|
|
|
|
goto err;
|
|
|
|
|
2024-06-09 01:41:01 +00:00
|
|
|
if (subvol_inum_eq(target, inode->ei_inum))
|
2021-11-14 00:49:14 +00:00
|
|
|
goto found;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* File with multiple hardlinks and our backref is to the wrong
|
|
|
|
* directory - linear search:
|
|
|
|
*/
|
|
|
|
for_each_btree_key_continue_norestart(iter2, 0, k, ret) {
|
|
|
|
if (k.k->p.inode > dir->ei_inode.bi_inum)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (k.k->type != KEY_TYPE_dirent)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
d = bkey_s_c_to_dirent(k);
|
2023-09-12 21:16:02 +00:00
|
|
|
ret = bch2_dirent_read_target(trans, inode_inum(dir), d, &target);
|
2021-11-14 00:49:14 +00:00
|
|
|
if (ret < 0)
|
|
|
|
break;
|
|
|
|
if (ret)
|
|
|
|
continue;
|
|
|
|
|
2024-06-09 01:41:01 +00:00
|
|
|
if (subvol_inum_eq(target, inode->ei_inum))
|
2021-11-14 00:49:14 +00:00
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = -ENOENT;
|
|
|
|
goto err;
|
|
|
|
found:
|
2023-08-12 21:26:29 +00:00
|
|
|
dirent_name = bch2_dirent_get_name(d);
|
2021-11-14 00:49:14 +00:00
|
|
|
|
2023-08-12 21:26:29 +00:00
|
|
|
name_len = min_t(unsigned, dirent_name.len, NAME_MAX);
|
|
|
|
memcpy(name, dirent_name.name, name_len);
|
2021-11-14 00:49:14 +00:00
|
|
|
name[name_len] = '\0';
|
|
|
|
err:
|
2022-07-18 03:06:38 +00:00
|
|
|
if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
|
2021-11-14 00:49:14 +00:00
|
|
|
goto retry;
|
|
|
|
|
2023-09-12 21:16:02 +00:00
|
|
|
bch2_trans_iter_exit(trans, &iter1);
|
|
|
|
bch2_trans_iter_exit(trans, &iter2);
|
|
|
|
bch2_trans_put(trans);
|
2021-11-14 00:49:14 +00:00
|
|
|
|
|
|
|
return ret;
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct export_operations bch_export_ops = {
|
2021-11-14 00:49:14 +00:00
|
|
|
.encode_fh = bch2_encode_fh,
|
|
|
|
.fh_to_dentry = bch2_fh_to_dentry,
|
|
|
|
.fh_to_parent = bch2_fh_to_parent,
|
|
|
|
.get_parent = bch2_get_parent,
|
|
|
|
.get_name = bch2_get_name,
|
2017-03-17 06:18:50 +00:00
|
|
|
};
|
|
|
|
|
2024-08-16 16:31:53 +00:00
|
|
|
static void bch2_vfs_inode_init(struct btree_trans *trans,
|
|
|
|
subvol_inum inum,
|
2017-03-17 06:18:50 +00:00
|
|
|
struct bch_inode_info *inode,
|
2021-10-27 17:05:56 +00:00
|
|
|
struct bch_inode_unpacked *bi,
|
|
|
|
struct bch_subvolume *subvol)
|
2017-03-17 06:18:50 +00:00
|
|
|
{
|
2024-06-09 01:41:01 +00:00
|
|
|
inode->v.i_ino = inum.inum;
|
|
|
|
inode->ei_inum = inum;
|
|
|
|
inode->ei_inode.bi_inum = inum.inum;
|
2021-11-06 04:03:40 +00:00
|
|
|
bch2_inode_update_after_write(trans, inode, bi, ~0);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
inode->v.i_blocks = bi->bi_sectors;
|
|
|
|
inode->v.i_ino = bi->bi_inum;
|
|
|
|
inode->v.i_rdev = bi->bi_dev;
|
|
|
|
inode->v.i_generation = bi->bi_generation;
|
|
|
|
inode->v.i_size = bi->bi_size;
|
|
|
|
|
2020-12-03 19:27:20 +00:00
|
|
|
inode->ei_flags = 0;
|
2017-03-17 06:18:50 +00:00
|
|
|
inode->ei_quota_reserved = 0;
|
2018-12-17 10:43:00 +00:00
|
|
|
inode->ei_qid = bch_qid(bi);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2024-06-04 08:46:10 +00:00
|
|
|
if (BCH_SUBVOLUME_SNAP(subvol))
|
|
|
|
set_bit(EI_INODE_SNAPSHOT, &inode->ei_flags);
|
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
inode->v.i_mapping->a_ops = &bch_address_space_operations;
|
|
|
|
|
|
|
|
switch (inode->v.i_mode & S_IFMT) {
|
|
|
|
case S_IFREG:
|
|
|
|
inode->v.i_op = &bch_file_inode_operations;
|
|
|
|
inode->v.i_fop = &bch_file_operations;
|
|
|
|
break;
|
|
|
|
case S_IFDIR:
|
|
|
|
inode->v.i_op = &bch_dir_inode_operations;
|
|
|
|
inode->v.i_fop = &bch_dir_file_operations;
|
|
|
|
break;
|
|
|
|
case S_IFLNK:
|
|
|
|
inode_nohighmem(&inode->v);
|
|
|
|
inode->v.i_op = &bch_symlink_inode_operations;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
init_special_inode(&inode->v, inode->v.i_mode, inode->v.i_rdev);
|
|
|
|
inode->v.i_op = &bch_special_inode_operations;
|
|
|
|
break;
|
|
|
|
}
|
2023-03-19 20:47:30 +00:00
|
|
|
|
|
|
|
mapping_set_large_folios(inode->v.i_mapping);
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
2024-04-26 07:29:56 +00:00
|
|
|
static void bch2_free_inode(struct inode *vinode)
|
2017-03-17 06:18:50 +00:00
|
|
|
{
|
2024-04-26 07:29:56 +00:00
|
|
|
kmem_cache_free(bch2_inode_cache, to_bch_ei(vinode));
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
2023-08-12 14:47:45 +00:00
|
|
|
static int inode_update_times_fn(struct btree_trans *trans,
|
|
|
|
struct bch_inode_info *inode,
|
2017-03-17 06:18:50 +00:00
|
|
|
struct bch_inode_unpacked *bi,
|
|
|
|
void *p)
|
|
|
|
{
|
|
|
|
struct bch_fs *c = inode->v.i_sb->s_fs_info;
|
|
|
|
|
2023-10-30 21:09:38 +00:00
|
|
|
bi->bi_atime = timespec_to_bch2_time(c, inode_get_atime(&inode->v));
|
|
|
|
bi->bi_mtime = timespec_to_bch2_time(c, inode_get_mtime(&inode->v));
|
2017-03-17 06:18:50 +00:00
|
|
|
bi->bi_ctime = timespec_to_bch2_time(c, inode_get_ctime(&inode->v));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bch2_vfs_write_inode(struct inode *vinode,
|
|
|
|
struct writeback_control *wbc)
|
|
|
|
{
|
|
|
|
struct bch_fs *c = vinode->i_sb->s_fs_info;
|
|
|
|
struct bch_inode_info *inode = to_bch_ei(vinode);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
mutex_lock(&inode->ei_update_lock);
|
2018-07-17 18:12:42 +00:00
|
|
|
ret = bch2_write_inode(c, inode, inode_update_times_fn, NULL,
|
|
|
|
ATTR_ATIME|ATTR_MTIME|ATTR_CTIME);
|
2017-03-17 06:18:50 +00:00
|
|
|
mutex_unlock(&inode->ei_update_lock);
|
|
|
|
|
2022-09-18 19:43:50 +00:00
|
|
|
return bch2_err_class(ret);
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void bch2_evict_inode(struct inode *vinode)
|
|
|
|
{
|
|
|
|
struct bch_fs *c = vinode->i_sb->s_fs_info;
|
|
|
|
struct bch_inode_info *inode = to_bch_ei(vinode);
|
2024-06-09 01:41:01 +00:00
|
|
|
bool delete = !inode->v.i_nlink && !is_bad_inode(&inode->v);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* evict() has waited for outstanding writeback, we'll do no more IO
|
|
|
|
* through this inode: it's safe to remove from VFS inode hashtable here
|
|
|
|
*
|
|
|
|
* Do that now so that other threads aren't blocked from pulling it back
|
|
|
|
* in, there's no reason for them to be:
|
|
|
|
*/
|
|
|
|
if (!delete)
|
|
|
|
bch2_inode_hash_remove(c, inode);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
truncate_inode_pages_final(&inode->v.i_data);
|
|
|
|
|
|
|
|
clear_inode(&inode->v);
|
|
|
|
|
|
|
|
BUG_ON(!is_bad_inode(&inode->v) && inode->ei_quota_reserved);
|
|
|
|
|
2024-06-09 01:41:01 +00:00
|
|
|
if (delete) {
|
2017-03-17 06:18:50 +00:00
|
|
|
bch2_quota_acct(c, inode->ei_qid, Q_SPC, -((s64) inode->v.i_blocks),
|
2018-11-01 19:10:01 +00:00
|
|
|
KEY_TYPE_QUOTA_WARN);
|
2017-03-17 06:18:50 +00:00
|
|
|
bch2_quota_acct(c, inode->ei_qid, Q_INO, -1,
|
2018-11-01 19:10:01 +00:00
|
|
|
KEY_TYPE_QUOTA_WARN);
|
2022-01-12 07:13:21 +00:00
|
|
|
bch2_inode_rm(c, inode_inum(inode));
|
2024-06-09 01:41:01 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If we are deleting, we need it present in the vfs hash table
|
|
|
|
* so that fsck can check if unlinked inodes are still open:
|
|
|
|
*/
|
|
|
|
bch2_inode_hash_remove(c, inode);
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
2023-03-15 15:53:51 +00:00
|
|
|
|
|
|
|
mutex_lock(&c->vfs_inodes_lock);
|
|
|
|
list_del_init(&inode->ei_vfs_inode_list);
|
|
|
|
mutex_unlock(&c->vfs_inodes_lock);
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
2023-03-15 15:53:51 +00:00
|
|
|
void bch2_evict_subvolume_inodes(struct bch_fs *c, snapshot_id_list *s)
|
2021-10-28 20:24:39 +00:00
|
|
|
{
|
2023-12-17 02:40:26 +00:00
|
|
|
struct bch_inode_info *inode;
|
2023-03-15 15:53:51 +00:00
|
|
|
DARRAY(struct bch_inode_info *) grabbed;
|
|
|
|
bool clean_pass = false, this_pass_clean;
|
2021-10-28 20:24:39 +00:00
|
|
|
|
2023-03-15 15:53:51 +00:00
|
|
|
/*
|
|
|
|
* Initially, we scan for inodes without I_DONTCACHE, then mark them to
|
|
|
|
* be pruned with d_mark_dontcache().
|
|
|
|
*
|
|
|
|
* Once we've had a clean pass where we didn't find any inodes without
|
|
|
|
* I_DONTCACHE, we wait for them to be freed:
|
|
|
|
*/
|
2021-10-28 20:24:39 +00:00
|
|
|
|
2023-03-15 15:53:51 +00:00
|
|
|
darray_init(&grabbed);
|
|
|
|
darray_make_room(&grabbed, 1024);
|
2021-10-28 20:24:39 +00:00
|
|
|
again:
|
|
|
|
cond_resched();
|
2023-03-15 15:53:51 +00:00
|
|
|
this_pass_clean = true;
|
|
|
|
|
|
|
|
mutex_lock(&c->vfs_inodes_lock);
|
|
|
|
list_for_each_entry(inode, &c->vfs_inodes_list, ei_vfs_inode_list) {
|
2024-06-09 01:41:01 +00:00
|
|
|
if (!snapshot_list_has_id(s, inode->ei_inum.subvol))
|
2021-10-28 20:24:39 +00:00
|
|
|
continue;
|
|
|
|
|
2023-03-15 15:53:51 +00:00
|
|
|
if (!(inode->v.i_state & I_DONTCACHE) &&
|
2023-06-09 19:41:41 +00:00
|
|
|
!(inode->v.i_state & I_FREEING) &&
|
|
|
|
igrab(&inode->v)) {
|
2023-03-15 15:53:51 +00:00
|
|
|
this_pass_clean = false;
|
|
|
|
|
2023-06-09 19:41:41 +00:00
|
|
|
if (darray_push_gfp(&grabbed, inode, GFP_ATOMIC|__GFP_NOWARN)) {
|
|
|
|
iput(&inode->v);
|
2023-03-15 15:53:51 +00:00
|
|
|
break;
|
2023-06-09 19:41:41 +00:00
|
|
|
}
|
2023-03-15 15:53:51 +00:00
|
|
|
} else if (clean_pass && this_pass_clean) {
|
2024-08-23 12:47:38 +00:00
|
|
|
struct wait_bit_queue_entry wqe;
|
|
|
|
struct wait_queue_head *wq_head;
|
2021-10-28 20:24:39 +00:00
|
|
|
|
2024-08-23 12:47:38 +00:00
|
|
|
wq_head = inode_bit_waitqueue(&wqe, &inode->v, __I_NEW);
|
|
|
|
prepare_to_wait_event(wq_head, &wqe.wq_entry,
|
|
|
|
TASK_UNINTERRUPTIBLE);
|
2023-03-15 15:53:51 +00:00
|
|
|
mutex_unlock(&c->vfs_inodes_lock);
|
|
|
|
|
2021-10-28 20:24:39 +00:00
|
|
|
schedule();
|
2024-08-23 12:47:38 +00:00
|
|
|
finish_wait(wq_head, &wqe.wq_entry);
|
2021-10-28 20:24:39 +00:00
|
|
|
goto again;
|
|
|
|
}
|
2023-03-15 15:53:51 +00:00
|
|
|
}
|
|
|
|
mutex_unlock(&c->vfs_inodes_lock);
|
2021-10-28 20:24:39 +00:00
|
|
|
|
2023-06-09 19:41:41 +00:00
|
|
|
darray_for_each(grabbed, i) {
|
|
|
|
inode = *i;
|
|
|
|
d_mark_dontcache(&inode->v);
|
|
|
|
d_prune_aliases(&inode->v);
|
|
|
|
iput(&inode->v);
|
|
|
|
}
|
2023-03-15 15:53:51 +00:00
|
|
|
grabbed.nr = 0;
|
|
|
|
|
|
|
|
if (!clean_pass || !this_pass_clean) {
|
|
|
|
clean_pass = this_pass_clean;
|
|
|
|
goto again;
|
2021-10-28 20:24:39 +00:00
|
|
|
}
|
2023-03-15 15:53:51 +00:00
|
|
|
|
|
|
|
darray_exit(&grabbed);
|
2021-10-28 20:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
static int bch2_statfs(struct dentry *dentry, struct kstatfs *buf)
|
|
|
|
{
|
|
|
|
struct super_block *sb = dentry->d_sb;
|
|
|
|
struct bch_fs *c = sb->s_fs_info;
|
2018-11-27 13:23:22 +00:00
|
|
|
struct bch_fs_usage_short usage = bch2_fs_usage_read_short(c);
|
2018-07-24 18:54:39 +00:00
|
|
|
unsigned shift = sb->s_blocksize_bits - 9;
|
2020-11-03 00:49:23 +00:00
|
|
|
/*
|
|
|
|
* this assumes inodes take up 64 bytes, which is a decent average
|
|
|
|
* number:
|
|
|
|
*/
|
|
|
|
u64 avail_inodes = ((usage.capacity - usage.used) << 3);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
buf->f_type = BCACHEFS_STATFS_MAGIC;
|
|
|
|
buf->f_bsize = sb->s_blocksize;
|
2018-11-27 13:23:22 +00:00
|
|
|
buf->f_blocks = usage.capacity >> shift;
|
2023-09-11 03:35:02 +00:00
|
|
|
buf->f_bfree = usage.free >> shift;
|
|
|
|
buf->f_bavail = avail_factor(usage.free) >> shift;
|
2020-11-03 00:49:23 +00:00
|
|
|
|
|
|
|
buf->f_files = usage.nr_inodes + avail_inodes;
|
|
|
|
buf->f_ffree = avail_inodes;
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2024-02-03 20:05:17 +00:00
|
|
|
buf->f_fsid = uuid_to_fsid(c->sb.user_uuid.b);
|
2017-03-17 06:18:50 +00:00
|
|
|
buf->f_namelen = BCH_NAME_MAX;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bch2_sync_fs(struct super_block *sb, int wait)
|
|
|
|
{
|
|
|
|
struct bch_fs *c = sb->s_fs_info;
|
2022-09-18 19:43:50 +00:00
|
|
|
int ret;
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2024-05-31 02:35:09 +00:00
|
|
|
trace_bch2_sync_fs(sb, wait);
|
|
|
|
|
2019-08-22 20:30:55 +00:00
|
|
|
if (c->opts.journal_flush_disabled)
|
|
|
|
return 0;
|
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
if (!wait) {
|
|
|
|
bch2_journal_flush_async(&c->journal, NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-09-18 19:43:50 +00:00
|
|
|
ret = bch2_journal_flush(&c->journal);
|
|
|
|
return bch2_err_class(ret);
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct bch_fs *bch2_path_to_fs(const char *path)
|
|
|
|
{
|
|
|
|
struct bch_fs *c;
|
|
|
|
dev_t dev;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = lookup_bdev(path, &dev);
|
|
|
|
if (ret)
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
|
|
|
|
c = bch2_dev_to_fs(dev);
|
2020-09-08 22:30:32 +00:00
|
|
|
if (c)
|
2017-03-17 06:18:50 +00:00
|
|
|
closure_put(&c->cl);
|
2020-09-08 22:30:32 +00:00
|
|
|
return c ?: ERR_PTR(-ENOENT);
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
2024-05-28 04:36:11 +00:00
|
|
|
static int bch2_remount(struct super_block *sb, int *flags,
|
|
|
|
struct bch_opts opts)
|
2017-03-17 06:18:50 +00:00
|
|
|
{
|
|
|
|
struct bch_fs *c = sb->s_fs_info;
|
2024-05-28 04:36:11 +00:00
|
|
|
int ret = 0;
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2023-12-23 22:50:29 +00:00
|
|
|
opt_set(opts, read_only, (*flags & SB_RDONLY) != 0);
|
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
if (opts.read_only != c->opts.read_only) {
|
2020-06-15 18:58:47 +00:00
|
|
|
down_write(&c->state_lock);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
if (opts.read_only) {
|
|
|
|
bch2_fs_read_only(c);
|
|
|
|
|
|
|
|
sb->s_flags |= SB_RDONLY;
|
|
|
|
} else {
|
2019-03-22 02:19:57 +00:00
|
|
|
ret = bch2_fs_read_write(c);
|
|
|
|
if (ret) {
|
|
|
|
bch_err(c, "error going rw: %i", ret);
|
2020-06-15 18:58:47 +00:00
|
|
|
up_write(&c->state_lock);
|
2022-09-18 19:43:50 +00:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto err;
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sb->s_flags &= ~SB_RDONLY;
|
|
|
|
}
|
|
|
|
|
|
|
|
c->opts.read_only = opts.read_only;
|
|
|
|
|
2020-06-15 18:58:47 +00:00
|
|
|
up_write(&c->state_lock);
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
2023-09-12 22:41:22 +00:00
|
|
|
if (opt_defined(opts, errors))
|
2017-03-17 06:18:50 +00:00
|
|
|
c->opts.errors = opts.errors;
|
2022-09-18 19:43:50 +00:00
|
|
|
err:
|
|
|
|
return bch2_err_class(ret);
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
2020-09-07 02:58:28 +00:00
|
|
|
static int bch2_show_devname(struct seq_file *seq, struct dentry *root)
|
|
|
|
{
|
|
|
|
struct bch_fs *c = root->d_sb->s_fs_info;
|
|
|
|
bool first = true;
|
|
|
|
|
2023-12-17 04:47:29 +00:00
|
|
|
for_each_online_member(c, ca) {
|
2020-09-07 02:58:28 +00:00
|
|
|
if (!first)
|
|
|
|
seq_putc(seq, ':');
|
|
|
|
first = false;
|
2023-11-16 17:13:43 +00:00
|
|
|
seq_puts(seq, ca->disk_sb.sb_name);
|
2020-09-07 02:58:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
static int bch2_show_options(struct seq_file *seq, struct dentry *root)
|
|
|
|
{
|
|
|
|
struct bch_fs *c = root->d_sb->s_fs_info;
|
2022-02-25 18:18:19 +00:00
|
|
|
struct printbuf buf = PRINTBUF;
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2024-09-08 00:27:23 +00:00
|
|
|
bch2_opts_to_text(&buf, c->opts, c, c->disk_sb.sb,
|
|
|
|
OPT_MOUNT, OPT_HIDDEN, OPT_SHOW_MOUNT_STYLE);
|
|
|
|
printbuf_nul_terminate(&buf);
|
2024-09-26 02:00:01 +00:00
|
|
|
seq_printf(seq, ",%s", buf.buf);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2024-09-08 00:27:23 +00:00
|
|
|
int ret = buf.allocation_failure ? -ENOMEM : 0;
|
2022-02-25 18:18:19 +00:00
|
|
|
printbuf_exit(&buf);
|
|
|
|
return ret;
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
2020-09-08 22:30:32 +00:00
|
|
|
static void bch2_put_super(struct super_block *sb)
|
|
|
|
{
|
|
|
|
struct bch_fs *c = sb->s_fs_info;
|
|
|
|
|
|
|
|
__bch2_fs_stop(c);
|
|
|
|
}
|
|
|
|
|
bcachefs: initial freeze/unfreeze support
Initial support for the vfs superblock freeze and unfreeze
operations. Superblock freeze occurs in stages, where the vfs
attempts to quiesce high level write operations, page faults, fs
internal operations, and then finally calls into the filesystem for
any last stage steps (i.e. log flushing, etc.) before marking the
superblock frozen.
The majority of write paths are covered by freeze protection (i.e.
sb_start_write() and friends) in higher level common code, with the
exception of the fs-internal SB_FREEZE_FS stage (i.e.
sb_start_intwrite()). This typically maps to active filesystem
transactions in a manner that allows the vfs to implement a barrier
of internal fs operations during the freeze sequence. This is not a
viable model for bcachefs, however, because it utilizes transactions
both to populate the journal as well as to perform journal reclaim.
This means that mapping intwrite protection to transaction lifecycle
or transaction commit is likely to deadlock freeze, as quiescing the
journal requires transactional operations blocked by the final stage
of freeze.
The flipside of this is that bcachefs does already maintain its own
internal sets of write references for similar purposes, currently
utilized for transitions from read-write to read-only mode. Since
this largely mirrors the high level sequence involved with freeze,
we can simply invoke this mechanism in the freeze callback to fully
quiesce the filesystem in the final stage. This means that while the
SB_FREEZE_FS stage is essentially a no-op, the ->freeze_fs()
callback that immediately follows begins by performing effectively
the same step by quiescing all internal write references.
One caveat to this approach is that without integration of internal
freeze protection, write operations gated on internal write refs
will fail with an internal -EROFS error rather than block on
acquiring freeze protection. IOW, this is roughly equivalent to only
having support for sb_start_intwrite_trylock(), and not the blocking
variant. Many of these paths already use non-blocking internal write
refs and so would map into an sb_start_intwrite_trylock() anyways.
The only instance of this I've been able to uncover that doesn't
explicitly rely on a higher level non-blocking write ref is the
bch2_rbio_narrow_crcs() path, which updates crcs in certain read
cases, and Kent has pointed out isn't critical if it happens to fail
due to read-only status.
Given that, implement basic freeze support as described above and
leave tighter integration with internal freeze protection as a
possible future enhancement. There are multiple potential ideas
worth exploring here. For example, we could implement a multi-stage
freeze callback that might allow bcachefs to quiesce its internal
write references without deadlocks, we could integrate intwrite
protection with bcachefs' internal write references somehow or
another, or perhaps consider implementing blocking support for
internal write refs to be used specifically for freeze, etc. In the
meantime, this enables functional freeze support and the associated
test coverage that comes with it.
Signed-off-by: Brian Foster <bfoster@redhat.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
2023-09-15 12:51:54 +00:00
|
|
|
/*
|
|
|
|
* bcachefs doesn't currently integrate intwrite freeze protection but the
|
|
|
|
* internal write references serve the same purpose. Therefore reuse the
|
|
|
|
* read-only transition code to perform the quiesce. The caveat is that we don't
|
|
|
|
* currently have the ability to block tasks that want a write reference while
|
|
|
|
* the superblock is frozen. This is fine for now, but we should either add
|
|
|
|
* blocking support or find a way to integrate sb_start_intwrite() and friends.
|
|
|
|
*/
|
|
|
|
static int bch2_freeze(struct super_block *sb)
|
|
|
|
{
|
|
|
|
struct bch_fs *c = sb->s_fs_info;
|
|
|
|
|
|
|
|
down_write(&c->state_lock);
|
|
|
|
bch2_fs_read_only(c);
|
|
|
|
up_write(&c->state_lock);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bch2_unfreeze(struct super_block *sb)
|
|
|
|
{
|
|
|
|
struct bch_fs *c = sb->s_fs_info;
|
|
|
|
int ret;
|
|
|
|
|
2023-11-26 22:05:02 +00:00
|
|
|
if (test_bit(BCH_FS_emergency_ro, &c->flags))
|
2023-12-05 13:24:38 +00:00
|
|
|
return 0;
|
|
|
|
|
bcachefs: initial freeze/unfreeze support
Initial support for the vfs superblock freeze and unfreeze
operations. Superblock freeze occurs in stages, where the vfs
attempts to quiesce high level write operations, page faults, fs
internal operations, and then finally calls into the filesystem for
any last stage steps (i.e. log flushing, etc.) before marking the
superblock frozen.
The majority of write paths are covered by freeze protection (i.e.
sb_start_write() and friends) in higher level common code, with the
exception of the fs-internal SB_FREEZE_FS stage (i.e.
sb_start_intwrite()). This typically maps to active filesystem
transactions in a manner that allows the vfs to implement a barrier
of internal fs operations during the freeze sequence. This is not a
viable model for bcachefs, however, because it utilizes transactions
both to populate the journal as well as to perform journal reclaim.
This means that mapping intwrite protection to transaction lifecycle
or transaction commit is likely to deadlock freeze, as quiescing the
journal requires transactional operations blocked by the final stage
of freeze.
The flipside of this is that bcachefs does already maintain its own
internal sets of write references for similar purposes, currently
utilized for transitions from read-write to read-only mode. Since
this largely mirrors the high level sequence involved with freeze,
we can simply invoke this mechanism in the freeze callback to fully
quiesce the filesystem in the final stage. This means that while the
SB_FREEZE_FS stage is essentially a no-op, the ->freeze_fs()
callback that immediately follows begins by performing effectively
the same step by quiescing all internal write references.
One caveat to this approach is that without integration of internal
freeze protection, write operations gated on internal write refs
will fail with an internal -EROFS error rather than block on
acquiring freeze protection. IOW, this is roughly equivalent to only
having support for sb_start_intwrite_trylock(), and not the blocking
variant. Many of these paths already use non-blocking internal write
refs and so would map into an sb_start_intwrite_trylock() anyways.
The only instance of this I've been able to uncover that doesn't
explicitly rely on a higher level non-blocking write ref is the
bch2_rbio_narrow_crcs() path, which updates crcs in certain read
cases, and Kent has pointed out isn't critical if it happens to fail
due to read-only status.
Given that, implement basic freeze support as described above and
leave tighter integration with internal freeze protection as a
possible future enhancement. There are multiple potential ideas
worth exploring here. For example, we could implement a multi-stage
freeze callback that might allow bcachefs to quiesce its internal
write references without deadlocks, we could integrate intwrite
protection with bcachefs' internal write references somehow or
another, or perhaps consider implementing blocking support for
internal write refs to be used specifically for freeze, etc. In the
meantime, this enables functional freeze support and the associated
test coverage that comes with it.
Signed-off-by: Brian Foster <bfoster@redhat.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
2023-09-15 12:51:54 +00:00
|
|
|
down_write(&c->state_lock);
|
|
|
|
ret = bch2_fs_read_write(c);
|
|
|
|
up_write(&c->state_lock);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
static const struct super_operations bch_super_operations = {
|
|
|
|
.alloc_inode = bch2_alloc_inode,
|
2024-04-26 07:29:56 +00:00
|
|
|
.free_inode = bch2_free_inode,
|
2017-03-17 06:18:50 +00:00
|
|
|
.write_inode = bch2_vfs_write_inode,
|
|
|
|
.evict_inode = bch2_evict_inode,
|
|
|
|
.sync_fs = bch2_sync_fs,
|
|
|
|
.statfs = bch2_statfs,
|
2020-09-07 02:58:28 +00:00
|
|
|
.show_devname = bch2_show_devname,
|
2017-03-17 06:18:50 +00:00
|
|
|
.show_options = bch2_show_options,
|
|
|
|
.put_super = bch2_put_super,
|
|
|
|
.freeze_fs = bch2_freeze,
|
|
|
|
.unfreeze_fs = bch2_unfreeze,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int bch2_set_super(struct super_block *s, void *data)
|
|
|
|
{
|
|
|
|
s->s_fs_info = data;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-09-08 22:30:32 +00:00
|
|
|
static int bch2_noset_super(struct super_block *s, void *data)
|
|
|
|
{
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
2023-12-23 02:10:32 +00:00
|
|
|
typedef DARRAY(struct bch_fs *) darray_fs;
|
|
|
|
|
2020-09-08 22:30:32 +00:00
|
|
|
static int bch2_test_super(struct super_block *s, void *data)
|
|
|
|
{
|
|
|
|
struct bch_fs *c = s->s_fs_info;
|
2023-12-23 02:10:32 +00:00
|
|
|
darray_fs *d = data;
|
2020-09-08 22:30:32 +00:00
|
|
|
|
|
|
|
if (!c)
|
|
|
|
return false;
|
|
|
|
|
2023-12-23 02:10:32 +00:00
|
|
|
darray_for_each(*d, i)
|
|
|
|
if (c != *i)
|
2020-09-08 22:30:32 +00:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-05-29 17:38:06 +00:00
|
|
|
static int bch2_fs_get_tree(struct fs_context *fc)
|
2017-03-17 06:18:50 +00:00
|
|
|
{
|
|
|
|
struct bch_fs *c;
|
|
|
|
struct super_block *sb;
|
|
|
|
struct inode *vinode;
|
2024-05-29 17:38:06 +00:00
|
|
|
struct bch2_opts_parse *opts_parse = fc->fs_private;
|
|
|
|
struct bch_opts opts = opts_parse->opts;
|
2024-05-29 17:55:49 +00:00
|
|
|
darray_str devs;
|
|
|
|
darray_fs devs_to_fs = {};
|
2017-03-17 06:18:50 +00:00
|
|
|
int ret;
|
|
|
|
|
2024-05-29 17:38:06 +00:00
|
|
|
opt_set(opts, read_only, (fc->sb_flags & SB_RDONLY) != 0);
|
2024-05-29 17:55:49 +00:00
|
|
|
opt_set(opts, nostart, true);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2024-05-29 17:38:06 +00:00
|
|
|
if (!fc->source || strlen(fc->source) == 0)
|
|
|
|
return -EINVAL;
|
2021-06-10 11:52:42 +00:00
|
|
|
|
2024-05-29 17:38:06 +00:00
|
|
|
ret = bch2_split_devs(fc->source, &devs);
|
2023-12-23 02:10:32 +00:00
|
|
|
if (ret)
|
2024-05-29 17:38:06 +00:00
|
|
|
return ret;
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2023-12-23 02:10:32 +00:00
|
|
|
darray_for_each(devs, i) {
|
|
|
|
ret = darray_push(&devs_to_fs, bch2_path_to_fs(*i));
|
2024-05-29 17:55:49 +00:00
|
|
|
if (ret)
|
|
|
|
goto err;
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
2024-05-29 17:38:06 +00:00
|
|
|
sb = sget(fc->fs_type, bch2_test_super, bch2_noset_super, fc->sb_flags|SB_NOSEC, &devs_to_fs);
|
2020-09-08 22:30:32 +00:00
|
|
|
if (!IS_ERR(sb))
|
|
|
|
goto got_sb;
|
|
|
|
|
2023-12-23 02:10:32 +00:00
|
|
|
c = bch2_fs_open(devs.data, devs.nr, opts);
|
2024-05-29 17:55:49 +00:00
|
|
|
ret = PTR_ERR_OR_ZERO(c);
|
|
|
|
if (ret)
|
|
|
|
goto err;
|
2020-10-24 01:07:17 +00:00
|
|
|
|
|
|
|
/* Some options can't be parsed until after the fs is started: */
|
2024-05-29 17:55:49 +00:00
|
|
|
opts = bch2_opts_empty();
|
2024-05-29 17:38:06 +00:00
|
|
|
ret = bch2_parse_mount_opts(c, &opts, NULL, opts_parse->parse_later.buf);
|
2024-05-29 17:55:49 +00:00
|
|
|
if (ret)
|
|
|
|
goto err_stop_fs;
|
2020-10-24 01:07:17 +00:00
|
|
|
|
|
|
|
bch2_opts_apply(&c->opts, opts);
|
|
|
|
|
2024-05-29 17:55:49 +00:00
|
|
|
ret = bch2_fs_start(c);
|
|
|
|
if (ret)
|
|
|
|
goto err_stop_fs;
|
2020-09-08 22:30:32 +00:00
|
|
|
|
2024-05-29 17:38:06 +00:00
|
|
|
sb = sget(fc->fs_type, NULL, bch2_set_super, fc->sb_flags|SB_NOSEC, c);
|
2024-05-29 17:55:49 +00:00
|
|
|
ret = PTR_ERR_OR_ZERO(sb);
|
|
|
|
if (ret)
|
|
|
|
goto err_stop_fs;
|
2020-09-08 22:30:32 +00:00
|
|
|
got_sb:
|
|
|
|
c = sb->s_fs_info;
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2020-09-08 22:30:32 +00:00
|
|
|
if (sb->s_root) {
|
2024-05-29 17:38:06 +00:00
|
|
|
if ((fc->sb_flags ^ sb->s_flags) & SB_RDONLY) {
|
2017-03-17 06:18:50 +00:00
|
|
|
ret = -EBUSY;
|
|
|
|
goto err_put_super;
|
|
|
|
}
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2019-06-29 21:59:21 +00:00
|
|
|
sb->s_blocksize = block_bytes(c);
|
|
|
|
sb->s_blocksize_bits = ilog2(block_bytes(c));
|
2017-03-17 06:18:50 +00:00
|
|
|
sb->s_maxbytes = MAX_LFS_FILESIZE;
|
|
|
|
sb->s_op = &bch_super_operations;
|
|
|
|
sb->s_export_op = &bch_export_ops;
|
|
|
|
#ifdef CONFIG_BCACHEFS_QUOTA
|
|
|
|
sb->s_qcop = &bch2_quotactl_operations;
|
|
|
|
sb->s_quota_types = QTYPE_MASK_USR|QTYPE_MASK_GRP|QTYPE_MASK_PRJ;
|
|
|
|
#endif
|
|
|
|
sb->s_xattr = bch2_xattr_handlers;
|
|
|
|
sb->s_magic = BCACHEFS_STATFS_MAGIC;
|
2021-04-29 02:51:42 +00:00
|
|
|
sb->s_time_gran = c->sb.nsec_per_time_unit;
|
|
|
|
sb->s_time_min = div_s64(S64_MIN, c->sb.time_units_per_sec) + 1;
|
|
|
|
sb->s_time_max = div_s64(S64_MAX, c->sb.time_units_per_sec);
|
2024-02-03 20:23:07 +00:00
|
|
|
sb->s_uuid = c->sb.user_uuid;
|
2024-06-06 23:30:41 +00:00
|
|
|
sb->s_shrink->seeks = 0;
|
2017-03-17 06:18:50 +00:00
|
|
|
c->vfs_sb = sb;
|
2022-10-19 22:31:33 +00:00
|
|
|
strscpy(sb->s_id, c->name, sizeof(sb->s_id));
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
ret = super_setup_bdi(sb);
|
|
|
|
if (ret)
|
|
|
|
goto err_put_super;
|
|
|
|
|
|
|
|
sb->s_bdi->ra_pages = VM_READAHEAD_PAGES;
|
|
|
|
|
2023-12-17 04:47:29 +00:00
|
|
|
for_each_online_member(c, ca) {
|
2017-03-17 06:18:50 +00:00
|
|
|
struct block_device *bdev = ca->disk_sb.bdev;
|
|
|
|
|
|
|
|
/* XXX: create an anonymous device for multi device filesystems */
|
|
|
|
sb->s_bdev = bdev;
|
|
|
|
sb->s_dev = bdev->bd_dev;
|
|
|
|
percpu_ref_put(&ca->io_ref);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-05-27 23:15:44 +00:00
|
|
|
c->dev = sb->s_dev;
|
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
#ifdef CONFIG_BCACHEFS_POSIX_ACL
|
|
|
|
if (c->opts.acl)
|
|
|
|
sb->s_flags |= SB_POSIXACL;
|
|
|
|
#endif
|
|
|
|
|
Many singleton patches against the MM code. The patch series which are
included in this merge do the following:
- Kemeng Shi has contributed some compation maintenance work in the
series "Fixes and cleanups to compaction".
- Joel Fernandes has a patchset ("Optimize mremap during mutual
alignment within PMD") which fixes an obscure issue with mremap()'s
pagetable handling during a subsequent exec(), based upon an
implementation which Linus suggested.
- More DAMON/DAMOS maintenance and feature work from SeongJae Park i the
following patch series:
mm/damon: misc fixups for documents, comments and its tracepoint
mm/damon: add a tracepoint for damos apply target regions
mm/damon: provide pseudo-moving sum based access rate
mm/damon: implement DAMOS apply intervals
mm/damon/core-test: Fix memory leaks in core-test
mm/damon/sysfs-schemes: Do DAMOS tried regions update for only one apply interval
- In the series "Do not try to access unaccepted memory" Adrian Hunter
provides some fixups for the recently-added "unaccepted memory' feature.
To increase the feature's checking coverage. "Plug a few gaps where
RAM is exposed without checking if it is unaccepted memory".
- In the series "cleanups for lockless slab shrink" Qi Zheng has done
some maintenance work which is preparation for the lockless slab
shrinking code.
- Qi Zheng has redone the earlier (and reverted) attempt to make slab
shrinking lockless in the series "use refcount+RCU method to implement
lockless slab shrink".
- David Hildenbrand contributes some maintenance work for the rmap code
in the series "Anon rmap cleanups".
- Kefeng Wang does more folio conversions and some maintenance work in
the migration code. Series "mm: migrate: more folio conversion and
unification".
- Matthew Wilcox has fixed an issue in the buffer_head code which was
causing long stalls under some heavy memory/IO loads. Some cleanups
were added on the way. Series "Add and use bdev_getblk()".
- In the series "Use nth_page() in place of direct struct page
manipulation" Zi Yan has fixed a potential issue with the direct
manipulation of hugetlb page frames.
- In the series "mm: hugetlb: Skip initialization of gigantic tail
struct pages if freed by HVO" has improved our handling of gigantic
pages in the hugetlb vmmemmep optimizaton code. This provides
significant boot time improvements when significant amounts of gigantic
pages are in use.
- Matthew Wilcox has sent the series "Small hugetlb cleanups" - code
rationalization and folio conversions in the hugetlb code.
- Yin Fengwei has improved mlock()'s handling of large folios in the
series "support large folio for mlock"
- In the series "Expose swapcache stat for memcg v1" Liu Shixin has
added statistics for memcg v1 users which are available (and useful)
under memcg v2.
- Florent Revest has enhanced the MDWE (Memory-Deny-Write-Executable)
prctl so that userspace may direct the kernel to not automatically
propagate the denial to child processes. The series is named "MDWE
without inheritance".
- Kefeng Wang has provided the series "mm: convert numa balancing
functions to use a folio" which does what it says.
- In the series "mm/ksm: add fork-exec support for prctl" Stefan Roesch
makes is possible for a process to propagate KSM treatment across
exec().
- Huang Ying has enhanced memory tiering's calculation of memory
distances. This is used to permit the dax/kmem driver to use "high
bandwidth memory" in addition to Optane Data Center Persistent Memory
Modules (DCPMM). The series is named "memory tiering: calculate
abstract distance based on ACPI HMAT"
- In the series "Smart scanning mode for KSM" Stefan Roesch has
optimized KSM by teaching it to retain and use some historical
information from previous scans.
- Yosry Ahmed has fixed some inconsistencies in memcg statistics in the
series "mm: memcg: fix tracking of pending stats updates values".
- In the series "Implement IOCTL to get and optionally clear info about
PTEs" Peter Xu has added an ioctl to /proc/<pid>/pagemap which permits
us to atomically read-then-clear page softdirty state. This is mainly
used by CRIU.
- Hugh Dickins contributed the series "shmem,tmpfs: general maintenance"
- a bunch of relatively minor maintenance tweaks to this code.
- Matthew Wilcox has increased the use of the VMA lock over file-backed
page faults in the series "Handle more faults under the VMA lock". Some
rationalizations of the fault path became possible as a result.
- In the series "mm/rmap: convert page_move_anon_rmap() to
folio_move_anon_rmap()" David Hildenbrand has implemented some cleanups
and folio conversions.
- In the series "various improvements to the GUP interface" Lorenzo
Stoakes has simplified and improved the GUP interface with an eye to
providing groundwork for future improvements.
- Andrey Konovalov has sent along the series "kasan: assorted fixes and
improvements" which does those things.
- Some page allocator maintenance work from Kemeng Shi in the series
"Two minor cleanups to break_down_buddy_pages".
- In thes series "New selftest for mm" Breno Leitao has developed
another MM self test which tickles a race we had between madvise() and
page faults.
- In the series "Add folio_end_read" Matthew Wilcox provides cleanups
and an optimization to the core pagecache code.
- Nhat Pham has added memcg accounting for hugetlb memory in the series
"hugetlb memcg accounting".
- Cleanups and rationalizations to the pagemap code from Lorenzo
Stoakes, in the series "Abstract vma_merge() and split_vma()".
- Audra Mitchell has fixed issues in the procfs page_owner code's new
timestamping feature which was causing some misbehaviours. In the
series "Fix page_owner's use of free timestamps".
- Lorenzo Stoakes has fixed the handling of new mappings of sealed files
in the series "permit write-sealed memfd read-only shared mappings".
- Mike Kravetz has optimized the hugetlb vmemmap optimization in the
series "Batch hugetlb vmemmap modification operations".
- Some buffer_head folio conversions and cleanups from Matthew Wilcox in
the series "Finish the create_empty_buffers() transition".
- As a page allocator performance optimization Huang Ying has added
automatic tuning to the allocator's per-cpu-pages feature, in the series
"mm: PCP high auto-tuning".
- Roman Gushchin has contributed the patchset "mm: improve performance
of accounted kernel memory allocations" which improves their performance
by ~30% as measured by a micro-benchmark.
- folio conversions from Kefeng Wang in the series "mm: convert page
cpupid functions to folios".
- Some kmemleak fixups in Liu Shixin's series "Some bugfix about
kmemleak".
- Qi Zheng has improved our handling of memoryless nodes by keeping them
off the allocation fallback list. This is done in the series "handle
memoryless nodes more appropriately".
- khugepaged conversions from Vishal Moola in the series "Some
khugepaged folio conversions".
-----BEGIN PGP SIGNATURE-----
iHUEABYIAB0WIQTTMBEPP41GrTpTJgfdBJ7gKXxAjgUCZULEMwAKCRDdBJ7gKXxA
jhQHAQCYpD3g849x69DmHnHWHm/EHQLvQmRMDeYZI+nx/sCJOwEAw4AKg0Oemv9y
FgeUPAD1oasg6CP+INZvCj34waNxwAc=
=E+Y4
-----END PGP SIGNATURE-----
Merge tag 'mm-stable-2023-11-01-14-33' of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Pull MM updates from Andrew Morton:
"Many singleton patches against the MM code. The patch series which are
included in this merge do the following:
- Kemeng Shi has contributed some compation maintenance work in the
series 'Fixes and cleanups to compaction'
- Joel Fernandes has a patchset ('Optimize mremap during mutual
alignment within PMD') which fixes an obscure issue with mremap()'s
pagetable handling during a subsequent exec(), based upon an
implementation which Linus suggested
- More DAMON/DAMOS maintenance and feature work from SeongJae Park i
the following patch series:
mm/damon: misc fixups for documents, comments and its tracepoint
mm/damon: add a tracepoint for damos apply target regions
mm/damon: provide pseudo-moving sum based access rate
mm/damon: implement DAMOS apply intervals
mm/damon/core-test: Fix memory leaks in core-test
mm/damon/sysfs-schemes: Do DAMOS tried regions update for only one apply interval
- In the series 'Do not try to access unaccepted memory' Adrian
Hunter provides some fixups for the recently-added 'unaccepted
memory' feature. To increase the feature's checking coverage. 'Plug
a few gaps where RAM is exposed without checking if it is
unaccepted memory'
- In the series 'cleanups for lockless slab shrink' Qi Zheng has done
some maintenance work which is preparation for the lockless slab
shrinking code
- Qi Zheng has redone the earlier (and reverted) attempt to make slab
shrinking lockless in the series 'use refcount+RCU method to
implement lockless slab shrink'
- David Hildenbrand contributes some maintenance work for the rmap
code in the series 'Anon rmap cleanups'
- Kefeng Wang does more folio conversions and some maintenance work
in the migration code. Series 'mm: migrate: more folio conversion
and unification'
- Matthew Wilcox has fixed an issue in the buffer_head code which was
causing long stalls under some heavy memory/IO loads. Some cleanups
were added on the way. Series 'Add and use bdev_getblk()'
- In the series 'Use nth_page() in place of direct struct page
manipulation' Zi Yan has fixed a potential issue with the direct
manipulation of hugetlb page frames
- In the series 'mm: hugetlb: Skip initialization of gigantic tail
struct pages if freed by HVO' has improved our handling of gigantic
pages in the hugetlb vmmemmep optimizaton code. This provides
significant boot time improvements when significant amounts of
gigantic pages are in use
- Matthew Wilcox has sent the series 'Small hugetlb cleanups' - code
rationalization and folio conversions in the hugetlb code
- Yin Fengwei has improved mlock()'s handling of large folios in the
series 'support large folio for mlock'
- In the series 'Expose swapcache stat for memcg v1' Liu Shixin has
added statistics for memcg v1 users which are available (and
useful) under memcg v2
- Florent Revest has enhanced the MDWE (Memory-Deny-Write-Executable)
prctl so that userspace may direct the kernel to not automatically
propagate the denial to child processes. The series is named 'MDWE
without inheritance'
- Kefeng Wang has provided the series 'mm: convert numa balancing
functions to use a folio' which does what it says
- In the series 'mm/ksm: add fork-exec support for prctl' Stefan
Roesch makes is possible for a process to propagate KSM treatment
across exec()
- Huang Ying has enhanced memory tiering's calculation of memory
distances. This is used to permit the dax/kmem driver to use 'high
bandwidth memory' in addition to Optane Data Center Persistent
Memory Modules (DCPMM). The series is named 'memory tiering:
calculate abstract distance based on ACPI HMAT'
- In the series 'Smart scanning mode for KSM' Stefan Roesch has
optimized KSM by teaching it to retain and use some historical
information from previous scans
- Yosry Ahmed has fixed some inconsistencies in memcg statistics in
the series 'mm: memcg: fix tracking of pending stats updates
values'
- In the series 'Implement IOCTL to get and optionally clear info
about PTEs' Peter Xu has added an ioctl to /proc/<pid>/pagemap
which permits us to atomically read-then-clear page softdirty
state. This is mainly used by CRIU
- Hugh Dickins contributed the series 'shmem,tmpfs: general
maintenance', a bunch of relatively minor maintenance tweaks to
this code
- Matthew Wilcox has increased the use of the VMA lock over
file-backed page faults in the series 'Handle more faults under the
VMA lock'. Some rationalizations of the fault path became possible
as a result
- In the series 'mm/rmap: convert page_move_anon_rmap() to
folio_move_anon_rmap()' David Hildenbrand has implemented some
cleanups and folio conversions
- In the series 'various improvements to the GUP interface' Lorenzo
Stoakes has simplified and improved the GUP interface with an eye
to providing groundwork for future improvements
- Andrey Konovalov has sent along the series 'kasan: assorted fixes
and improvements' which does those things
- Some page allocator maintenance work from Kemeng Shi in the series
'Two minor cleanups to break_down_buddy_pages'
- In thes series 'New selftest for mm' Breno Leitao has developed
another MM self test which tickles a race we had between madvise()
and page faults
- In the series 'Add folio_end_read' Matthew Wilcox provides cleanups
and an optimization to the core pagecache code
- Nhat Pham has added memcg accounting for hugetlb memory in the
series 'hugetlb memcg accounting'
- Cleanups and rationalizations to the pagemap code from Lorenzo
Stoakes, in the series 'Abstract vma_merge() and split_vma()'
- Audra Mitchell has fixed issues in the procfs page_owner code's new
timestamping feature which was causing some misbehaviours. In the
series 'Fix page_owner's use of free timestamps'
- Lorenzo Stoakes has fixed the handling of new mappings of sealed
files in the series 'permit write-sealed memfd read-only shared
mappings'
- Mike Kravetz has optimized the hugetlb vmemmap optimization in the
series 'Batch hugetlb vmemmap modification operations'
- Some buffer_head folio conversions and cleanups from Matthew Wilcox
in the series 'Finish the create_empty_buffers() transition'
- As a page allocator performance optimization Huang Ying has added
automatic tuning to the allocator's per-cpu-pages feature, in the
series 'mm: PCP high auto-tuning'
- Roman Gushchin has contributed the patchset 'mm: improve
performance of accounted kernel memory allocations' which improves
their performance by ~30% as measured by a micro-benchmark
- folio conversions from Kefeng Wang in the series 'mm: convert page
cpupid functions to folios'
- Some kmemleak fixups in Liu Shixin's series 'Some bugfix about
kmemleak'
- Qi Zheng has improved our handling of memoryless nodes by keeping
them off the allocation fallback list. This is done in the series
'handle memoryless nodes more appropriately'
- khugepaged conversions from Vishal Moola in the series 'Some
khugepaged folio conversions'"
[ bcachefs conflicts with the dynamically allocated shrinkers have been
resolved as per Stephen Rothwell in
https://lore.kernel.org/all/20230913093553.4290421e@canb.auug.org.au/
with help from Qi Zheng.
The clone3 test filtering conflict was half-arsed by yours truly ]
* tag 'mm-stable-2023-11-01-14-33' of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm: (406 commits)
mm/damon/sysfs: update monitoring target regions for online input commit
mm/damon/sysfs: remove requested targets when online-commit inputs
selftests: add a sanity check for zswap
Documentation: maple_tree: fix word spelling error
mm/vmalloc: fix the unchecked dereference warning in vread_iter()
zswap: export compression failure stats
Documentation: ubsan: drop "the" from article title
mempolicy: migration attempt to match interleave nodes
mempolicy: mmap_lock is not needed while migrating folios
mempolicy: alloc_pages_mpol() for NUMA policy without vma
mm: add page_rmappable_folio() wrapper
mempolicy: remove confusing MPOL_MF_LAZY dead code
mempolicy: mpol_shared_policy_init() without pseudo-vma
mempolicy trivia: use pgoff_t in shared mempolicy tree
mempolicy trivia: slightly more consistent naming
mempolicy trivia: delete those ancient pr_debug()s
mempolicy: fix migrate_pages(2) syscall return nr_failed
kernfs: drop shared NUMA mempolicy hooks
hugetlbfs: drop shared NUMA mempolicy pretence
mm/damon/sysfs-test: add a unit test for damon_sysfs_set_targets()
...
2023-11-03 05:38:47 +00:00
|
|
|
sb->s_shrink->seeks = 0;
|
2021-11-13 18:36:26 +00:00
|
|
|
|
2021-03-16 05:33:39 +00:00
|
|
|
vinode = bch2_vfs_inode_get(c, BCACHEFS_ROOT_SUBVOL_INUM);
|
2022-07-18 23:42:58 +00:00
|
|
|
ret = PTR_ERR_OR_ZERO(vinode);
|
2023-12-17 03:43:41 +00:00
|
|
|
bch_err_msg(c, ret, "mounting: error getting root inode");
|
|
|
|
if (ret)
|
2017-03-17 06:18:50 +00:00
|
|
|
goto err_put_super;
|
|
|
|
|
|
|
|
sb->s_root = d_make_root(vinode);
|
|
|
|
if (!sb->s_root) {
|
2019-04-17 22:21:19 +00:00
|
|
|
bch_err(c, "error mounting: error allocating root dentry");
|
2017-03-17 06:18:50 +00:00
|
|
|
ret = -ENOMEM;
|
|
|
|
goto err_put_super;
|
|
|
|
}
|
|
|
|
|
|
|
|
sb->s_flags |= SB_ACTIVE;
|
|
|
|
out:
|
2024-05-29 17:38:06 +00:00
|
|
|
fc->root = dget(sb->s_root);
|
2024-05-28 23:21:59 +00:00
|
|
|
err:
|
2024-05-29 17:55:49 +00:00
|
|
|
darray_exit(&devs_to_fs);
|
|
|
|
bch2_darray_str_exit(&devs);
|
2024-07-03 16:58:34 +00:00
|
|
|
if (ret)
|
|
|
|
pr_err("error: %s", bch2_err_str(ret));
|
2024-05-28 23:21:59 +00:00
|
|
|
/*
|
|
|
|
* On an inconsistency error in recovery we might see an -EROFS derived
|
|
|
|
* errorcode (from the journal), but we don't want to return that to
|
|
|
|
* userspace as that causes util-linux to retry the mount RO - which is
|
|
|
|
* confusing:
|
|
|
|
*/
|
|
|
|
if (bch2_err_matches(ret, EROFS) && ret != -EROFS)
|
|
|
|
ret = -EIO;
|
2024-05-29 17:38:06 +00:00
|
|
|
return bch2_err_class(ret);
|
2024-05-29 17:55:49 +00:00
|
|
|
|
|
|
|
err_stop_fs:
|
|
|
|
bch2_fs_stop(c);
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
err_put_super:
|
|
|
|
__bch2_fs_stop(c);
|
|
|
|
deactivate_locked_super(sb);
|
|
|
|
goto err;
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void bch2_kill_sb(struct super_block *sb)
|
|
|
|
{
|
|
|
|
struct bch_fs *c = sb->s_fs_info;
|
|
|
|
|
|
|
|
generic_shutdown_super(sb);
|
2023-11-12 19:15:35 +00:00
|
|
|
bch2_fs_free(c);
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
2024-05-28 04:36:11 +00:00
|
|
|
static void bch2_fs_context_free(struct fs_context *fc)
|
|
|
|
{
|
|
|
|
struct bch2_opts_parse *opts = fc->fs_private;
|
|
|
|
|
|
|
|
if (opts) {
|
|
|
|
printbuf_exit(&opts->parse_later);
|
|
|
|
kfree(opts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bch2_fs_parse_param(struct fs_context *fc,
|
|
|
|
struct fs_parameter *param)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* the "source" param, i.e., the name of the device(s) to mount,
|
|
|
|
* is handled by the VFS layer.
|
|
|
|
*/
|
|
|
|
if (!strcmp(param->key, "source"))
|
|
|
|
return -ENOPARAM;
|
|
|
|
|
|
|
|
struct bch2_opts_parse *opts = fc->fs_private;
|
|
|
|
struct bch_fs *c = NULL;
|
|
|
|
|
|
|
|
/* for reconfigure, we already have a struct bch_fs */
|
|
|
|
if (fc->root)
|
|
|
|
c = fc->root->d_sb->s_fs_info;
|
|
|
|
|
|
|
|
int ret = bch2_parse_one_mount_opt(c, &opts->opts,
|
|
|
|
&opts->parse_later, param->key,
|
|
|
|
param->string);
|
|
|
|
|
|
|
|
return bch2_err_class(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bch2_fs_reconfigure(struct fs_context *fc)
|
|
|
|
{
|
|
|
|
struct super_block *sb = fc->root->d_sb;
|
|
|
|
struct bch2_opts_parse *opts = fc->fs_private;
|
|
|
|
|
|
|
|
return bch2_remount(sb, &fc->sb_flags, opts->opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct fs_context_operations bch2_context_ops = {
|
|
|
|
.free = bch2_fs_context_free,
|
|
|
|
.parse_param = bch2_fs_parse_param,
|
|
|
|
.get_tree = bch2_fs_get_tree,
|
|
|
|
.reconfigure = bch2_fs_reconfigure,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int bch2_init_fs_context(struct fs_context *fc)
|
|
|
|
{
|
|
|
|
struct bch2_opts_parse *opts = kzalloc(sizeof(*opts), GFP_KERNEL);
|
|
|
|
|
|
|
|
if (!opts)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
opts->parse_later = PRINTBUF;
|
|
|
|
|
|
|
|
fc->ops = &bch2_context_ops;
|
|
|
|
fc->fs_private = opts;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-06-09 01:41:01 +00:00
|
|
|
void bch2_fs_vfs_exit(struct bch_fs *c)
|
|
|
|
{
|
|
|
|
if (c->vfs_inodes_table.tbl)
|
|
|
|
rhashtable_destroy(&c->vfs_inodes_table);
|
|
|
|
}
|
|
|
|
|
|
|
|
int bch2_fs_vfs_init(struct bch_fs *c)
|
|
|
|
{
|
|
|
|
return rhashtable_init(&c->vfs_inodes_table, &bch2_vfs_inodes_params);
|
|
|
|
}
|
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
static struct file_system_type bcache_fs_type = {
|
2024-05-28 04:36:11 +00:00
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.name = "bcachefs",
|
|
|
|
.init_fs_context = bch2_init_fs_context,
|
|
|
|
.kill_sb = bch2_kill_sb,
|
2024-08-24 01:27:24 +00:00
|
|
|
.fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
|
2017-03-17 06:18:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
MODULE_ALIAS_FS("bcachefs");
|
|
|
|
|
|
|
|
void bch2_vfs_exit(void)
|
|
|
|
{
|
|
|
|
unregister_filesystem(&bcache_fs_type);
|
2022-10-19 22:31:33 +00:00
|
|
|
kmem_cache_destroy(bch2_inode_cache);
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int __init bch2_vfs_init(void)
|
|
|
|
{
|
|
|
|
int ret = -ENOMEM;
|
|
|
|
|
2024-07-03 07:09:55 +00:00
|
|
|
bch2_inode_cache = KMEM_CACHE(bch_inode_info, SLAB_RECLAIM_ACCOUNT |
|
|
|
|
SLAB_ACCOUNT);
|
2017-03-17 06:18:50 +00:00
|
|
|
if (!bch2_inode_cache)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
ret = register_filesystem(&bcache_fs_type);
|
|
|
|
if (ret)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
err:
|
|
|
|
bch2_vfs_exit();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* NO_BCACHEFS_FS */
|