2014-11-21 10:51:34 +00:00
|
|
|
/*
|
|
|
|
* MARS Long Distance Replication Software
|
|
|
|
*
|
|
|
|
* This file is part of MARS project: http://schoebel.github.io/mars/
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010-2014 Thomas Schoebel-Theuer
|
|
|
|
* Copyright (C) 2011-2014 1&1 Internet AG
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2013-01-23 11:21:22 +00:00
|
|
|
|
|
|
|
#include "lib_mapfree.h"
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/list.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/spinlock.h>
|
|
|
|
#include <linux/wait.h>
|
|
|
|
#include <linux/file.h>
|
2022-07-21 08:16:42 +00:00
|
|
|
#include <linux/pagemap.h>
|
2013-01-23 11:21:22 +00:00
|
|
|
|
2020-09-08 07:15:45 +00:00
|
|
|
/* needed for symlink checking */
|
|
|
|
#include "sy_old/strategy.h"
|
|
|
|
|
2013-09-16 09:51:03 +00:00
|
|
|
// time to wait between background mapfree operations
|
2013-01-23 11:21:22 +00:00
|
|
|
int mapfree_period_sec = 10;
|
|
|
|
EXPORT_SYMBOL_GPL(mapfree_period_sec);
|
|
|
|
|
2013-09-16 09:51:03 +00:00
|
|
|
// some grace space where no regular cleanup should occur
|
|
|
|
int mapfree_grace_keep_mb = 16;
|
|
|
|
EXPORT_SYMBOL_GPL(mapfree_grace_keep_mb);
|
|
|
|
|
2021-11-02 09:42:47 +00:00
|
|
|
/* Internal reaction time.
|
|
|
|
* Not critical, except when waiting for rmmod ;)
|
|
|
|
*/
|
|
|
|
#define MAPFREE_SLEEP_JIFFIES (HZ / 10 + 1)
|
|
|
|
|
|
|
|
int mapfree_sleep_jiffies = MAPFREE_SLEEP_JIFFIES;
|
|
|
|
|
|
|
|
static DECLARE_WAIT_QUEUE_HEAD(mapfree_event);
|
|
|
|
|
2020-09-08 08:13:17 +00:00
|
|
|
struct mf_hash_anchor {
|
|
|
|
struct rw_semaphore hash_mutex;
|
|
|
|
struct list_head hash_anchor;
|
|
|
|
struct task_struct *hash_thread;
|
|
|
|
};
|
|
|
|
|
2021-07-12 09:52:42 +00:00
|
|
|
#define MAPFREE_HASH 128
|
2020-09-08 08:13:17 +00:00
|
|
|
|
|
|
|
static inline
|
|
|
|
unsigned int mf_hash(const char *name)
|
|
|
|
{
|
|
|
|
unsigned char digest[MARS_DIGEST_SIZE] = {};
|
|
|
|
unsigned int res = 0;
|
|
|
|
|
|
|
|
mars_digest(MREF_CHKSUM_CRC32C | MREF_CHKSUM_MD5_OLD,
|
|
|
|
NULL,
|
|
|
|
digest,
|
|
|
|
name, strlen(name));
|
|
|
|
|
|
|
|
res = *(unsigned int *)&digest % MAPFREE_HASH;
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
2013-01-23 11:21:22 +00:00
|
|
|
|
|
|
|
static
|
2020-09-08 08:13:17 +00:00
|
|
|
struct mf_hash_anchor mf_table[MAPFREE_HASH];
|
2013-01-23 11:21:22 +00:00
|
|
|
|
2013-09-16 09:51:03 +00:00
|
|
|
void mapfree_pages(struct mapfree_info *mf, int grace_keep)
|
2013-01-23 11:21:22 +00:00
|
|
|
{
|
|
|
|
struct address_space *mapping;
|
|
|
|
pgoff_t start;
|
|
|
|
pgoff_t end;
|
|
|
|
|
2015-02-11 16:51:51 +00:00
|
|
|
if (unlikely(!mf))
|
|
|
|
goto done;
|
2013-01-23 11:21:22 +00:00
|
|
|
if (unlikely(!mf->mf_filp || !(mapping = mf->mf_filp->f_mapping)))
|
|
|
|
goto done;
|
|
|
|
|
2013-09-16 09:51:03 +00:00
|
|
|
if (grace_keep < 0) { // force full flush
|
2013-01-23 11:21:22 +00:00
|
|
|
start = 0;
|
|
|
|
end = -1;
|
|
|
|
} else {
|
|
|
|
loff_t tmp;
|
|
|
|
loff_t min;
|
|
|
|
|
2017-12-09 10:16:50 +00:00
|
|
|
down_write(&mf->mf_mutex);
|
2013-01-23 11:21:22 +00:00
|
|
|
|
|
|
|
min = tmp = mf->mf_min[0];
|
|
|
|
if (likely(mf->mf_min[1] < min))
|
|
|
|
min = mf->mf_min[1];
|
|
|
|
if (tmp) {
|
|
|
|
mf->mf_min[1] = tmp;
|
|
|
|
mf->mf_min[0] = 0;
|
|
|
|
}
|
|
|
|
|
2017-12-09 10:16:50 +00:00
|
|
|
up_write(&mf->mf_mutex);
|
2013-01-23 11:21:22 +00:00
|
|
|
|
2013-09-16 09:51:03 +00:00
|
|
|
min -= (loff_t)grace_keep * (1024 * 1024); // megabytes
|
|
|
|
end = 0;
|
|
|
|
|
|
|
|
if (min > 0 || mf->mf_last) {
|
2013-01-23 11:21:22 +00:00
|
|
|
start = mf->mf_last / PAGE_SIZE;
|
2013-07-03 05:52:08 +00:00
|
|
|
// add some grace overlapping
|
|
|
|
if (likely(start > 0))
|
|
|
|
start--;
|
2013-01-23 11:21:22 +00:00
|
|
|
mf->mf_last = min;
|
|
|
|
end = min / PAGE_SIZE;
|
|
|
|
} else { // there was no progress for at least 2 rounds
|
|
|
|
start = 0;
|
2013-09-16 09:51:03 +00:00
|
|
|
if (!grace_keep) // also flush thoroughly
|
|
|
|
end = -1;
|
2013-01-23 11:21:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MARS_DBG("file = '%s' start = %lu end = %lu\n", SAFE_STR(mf->mf_name), start, end);
|
|
|
|
}
|
|
|
|
|
2013-09-16 09:51:03 +00:00
|
|
|
if (end > start || end == -1) {
|
2013-01-23 11:21:22 +00:00
|
|
|
invalidate_mapping_pages(mapping, start, end);
|
|
|
|
}
|
|
|
|
|
|
|
|
done:;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void _mapfree_put(struct mapfree_info *mf)
|
|
|
|
{
|
|
|
|
if (atomic_dec_and_test(&mf->mf_count)) {
|
|
|
|
MARS_DBG("closing file '%s' filp = %p\n", mf->mf_name, mf->mf_filp);
|
|
|
|
list_del_init(&mf->mf_head);
|
2014-03-30 06:18:47 +00:00
|
|
|
CHECK_HEAD_EMPTY(&mf->mf_dirty_anchor);
|
2013-01-23 11:21:22 +00:00
|
|
|
if (likely(mf->mf_filp)) {
|
2013-09-16 09:51:03 +00:00
|
|
|
mapfree_pages(mf, -1);
|
2013-01-23 11:21:22 +00:00
|
|
|
filp_close(mf->mf_filp, NULL);
|
|
|
|
}
|
|
|
|
brick_string_free(mf->mf_name);
|
|
|
|
brick_mem_free(mf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void mapfree_put(struct mapfree_info *mf)
|
|
|
|
{
|
2021-11-03 13:44:17 +00:00
|
|
|
unsigned int hash;
|
2020-09-15 00:49:22 +00:00
|
|
|
|
2021-11-03 13:44:17 +00:00
|
|
|
if (unlikely(!mf))
|
|
|
|
return;
|
|
|
|
|
|
|
|
hash = mf->mf_hash;
|
|
|
|
if (unlikely(hash >= MAPFREE_HASH)) {
|
|
|
|
MARS_ERR("Bad mapfree hash %d\n", hash);
|
|
|
|
return;
|
2015-02-11 16:51:51 +00:00
|
|
|
}
|
2021-11-03 13:44:17 +00:00
|
|
|
|
|
|
|
down_write(&mf_table[hash].hash_mutex);
|
|
|
|
_mapfree_put(mf);
|
|
|
|
up_write(&mf_table[hash].hash_mutex);
|
2013-01-23 11:21:22 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(mapfree_put);
|
|
|
|
|
2020-11-05 06:43:22 +00:00
|
|
|
loff_t mapfree_real_size(struct mapfree_info *mf)
|
|
|
|
{
|
|
|
|
loff_t length = 0;
|
|
|
|
if (likely(mf && mf->mf_filp && mf->mf_filp->f_mapping)) {
|
|
|
|
struct inode *inode = mf->mf_filp->f_mapping->host;
|
|
|
|
|
|
|
|
length = i_size_read(inode);
|
|
|
|
}
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2020-09-30 08:34:14 +00:00
|
|
|
struct mapfree_info *mapfree_get(const char *name, int flags, int *error)
|
2013-01-23 11:21:22 +00:00
|
|
|
{
|
2021-07-12 09:30:20 +00:00
|
|
|
struct kstat check_stat = {};
|
2013-01-23 11:21:22 +00:00
|
|
|
struct mapfree_info *mf = NULL;
|
|
|
|
struct list_head *tmp;
|
2020-09-08 08:13:17 +00:00
|
|
|
unsigned int hash = mf_hash(name);
|
2013-01-23 11:21:22 +00:00
|
|
|
|
2021-02-16 06:00:43 +00:00
|
|
|
flags |= O_CLOEXEC;
|
2021-07-12 09:30:20 +00:00
|
|
|
if (!(flags & O_DIRECT) &&
|
|
|
|
mars_stat(name, &check_stat, false) >= 0 &&
|
|
|
|
(S_ISBLK(check_stat.mode))) {
|
2020-09-08 08:13:17 +00:00
|
|
|
down_read(&mf_table[hash].hash_mutex);
|
|
|
|
for (tmp = mf_table[hash].hash_anchor.next;
|
|
|
|
tmp != &mf_table[hash].hash_anchor;
|
|
|
|
tmp = tmp->next) {
|
2013-01-23 11:21:22 +00:00
|
|
|
struct mapfree_info *_mf = container_of(tmp, struct mapfree_info, mf_head);
|
|
|
|
if (_mf->mf_flags == flags && !strcmp(_mf->mf_name, name)) {
|
|
|
|
mf = _mf;
|
|
|
|
atomic_inc(&mf->mf_count);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-09-08 08:13:17 +00:00
|
|
|
up_read(&mf_table[hash].hash_mutex);
|
2013-01-23 11:21:22 +00:00
|
|
|
|
2020-04-04 08:42:01 +00:00
|
|
|
if (mf) {
|
2020-11-05 06:43:22 +00:00
|
|
|
loff_t length = mapfree_real_size(mf);
|
2020-04-04 08:42:01 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* In some cases like truncated logfiles,
|
|
|
|
* account for any shortenings.
|
|
|
|
*/
|
|
|
|
mf->mf_max = length;
|
|
|
|
for (i = 0; i < DIRTY_MAX; i++)
|
|
|
|
mf_dirty_reduce(mf, i, length);
|
|
|
|
|
2013-01-23 11:21:22 +00:00
|
|
|
goto done;
|
2020-04-04 08:42:01 +00:00
|
|
|
}
|
2013-01-23 11:21:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
struct address_space *mapping;
|
|
|
|
struct inode *inode;
|
2017-12-10 09:57:53 +00:00
|
|
|
loff_t length;
|
|
|
|
int i;
|
2013-01-23 11:21:22 +00:00
|
|
|
int ra = 1;
|
|
|
|
int prot = 0600;
|
|
|
|
mm_segment_t oldfs;
|
|
|
|
|
|
|
|
mf = brick_zmem_alloc(sizeof(struct mapfree_info));
|
|
|
|
if (unlikely(!mf)) {
|
|
|
|
MARS_ERR("no mem, name = '%s'\n", name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-09-08 08:13:17 +00:00
|
|
|
mf->mf_hash = hash;
|
2013-01-23 11:21:22 +00:00
|
|
|
mf->mf_name = brick_strdup(name);
|
|
|
|
if (unlikely(!mf->mf_name)) {
|
|
|
|
MARS_ERR("no mem, name = '%s'\n", name);
|
|
|
|
brick_mem_free(mf);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-09-08 07:15:45 +00:00
|
|
|
/* allow replacement of a .deleted symlink */
|
|
|
|
if (flags & O_CREAT) {
|
2020-09-21 12:11:03 +00:00
|
|
|
const char *check = mars_readlink(name, NULL);
|
2020-09-08 07:15:45 +00:00
|
|
|
|
2020-09-21 12:11:03 +00:00
|
|
|
if (check && !strcmp(check, MARS_DELETED_STR))
|
2020-09-08 07:15:45 +00:00
|
|
|
mars_unlink(name);
|
|
|
|
|
|
|
|
brick_string_free(check);
|
|
|
|
}
|
|
|
|
|
2013-01-23 11:21:22 +00:00
|
|
|
mf->mf_flags = flags;
|
|
|
|
INIT_LIST_HEAD(&mf->mf_head);
|
2014-03-30 06:18:47 +00:00
|
|
|
INIT_LIST_HEAD(&mf->mf_dirty_anchor);
|
2013-01-23 11:21:22 +00:00
|
|
|
atomic_set(&mf->mf_count, 1);
|
2017-12-09 10:16:50 +00:00
|
|
|
init_rwsem(&mf->mf_mutex);
|
2013-04-16 07:37:54 +00:00
|
|
|
mf->mf_max = -1;
|
2013-01-23 11:21:22 +00:00
|
|
|
|
|
|
|
oldfs = get_fs();
|
2021-01-29 21:35:46 +00:00
|
|
|
set_fs(KERNEL_DS);
|
2013-01-23 11:21:22 +00:00
|
|
|
mf->mf_filp = filp_open(name, flags, prot);
|
|
|
|
set_fs(oldfs);
|
|
|
|
|
|
|
|
MARS_DBG("file '%s' flags = %d prot = %d filp = %p\n", name, flags, prot, mf->mf_filp);
|
|
|
|
|
|
|
|
if (unlikely(!mf->mf_filp || IS_ERR(mf->mf_filp))) {
|
|
|
|
int err = PTR_ERR(mf->mf_filp);
|
2020-09-30 08:34:14 +00:00
|
|
|
|
|
|
|
if (error)
|
|
|
|
*error = err;
|
2013-01-23 11:21:22 +00:00
|
|
|
MARS_ERR("can't open file '%s' status=%d\n", name, err);
|
|
|
|
mf->mf_filp = NULL;
|
|
|
|
_mapfree_put(mf);
|
|
|
|
mf = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unlikely(!(mapping = mf->mf_filp->f_mapping) ||
|
|
|
|
!(inode = mapping->host))) {
|
|
|
|
MARS_ERR("file '%s' has no mapping\n", name);
|
|
|
|
mf->mf_filp = NULL;
|
|
|
|
_mapfree_put(mf);
|
|
|
|
mf = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
mapping_set_gfp_mask(mapping, mapping_gfp_mask(mapping) & ~(__GFP_IO | __GFP_FS));
|
|
|
|
|
2017-12-10 09:57:53 +00:00
|
|
|
length = i_size_read(inode);
|
|
|
|
mf->mf_max = length;
|
|
|
|
for (i = 0; i < DIRTY_MAX; i++) {
|
|
|
|
rwlock_init(&mf->mf_length[i].dl_lock);
|
|
|
|
mf->mf_length[i].dl_length = length;
|
|
|
|
}
|
2013-04-25 12:19:32 +00:00
|
|
|
|
2013-01-23 11:21:22 +00:00
|
|
|
if (S_ISBLK(inode->i_mode)) {
|
2019-12-01 18:46:31 +00:00
|
|
|
#ifdef MARS_HAS_BDI_GET
|
|
|
|
struct backing_dev_info *bdi =
|
|
|
|
I_BDEV(inode)->bd_bdi;
|
|
|
|
MARS_INF("changing blkdev readahead from %lu to %d\n",
|
|
|
|
bdi->ra_pages, ra);
|
|
|
|
bdi->ra_pages = ra;
|
|
|
|
#else /* deprecated old code */
|
2013-01-23 11:21:22 +00:00
|
|
|
MARS_INF("changing blkdev readahead from %lu to %d\n", inode->i_bdev->bd_disk->queue->backing_dev_info.ra_pages, ra);
|
|
|
|
inode->i_bdev->bd_disk->queue->backing_dev_info.ra_pages = ra;
|
2019-12-01 18:46:31 +00:00
|
|
|
#endif
|
2013-01-23 11:21:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & O_DIRECT) { // never share them
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// maintain global list of all open files
|
2020-09-08 08:13:17 +00:00
|
|
|
down_write(&mf_table[hash].hash_mutex);
|
|
|
|
for (tmp = mf_table[hash].hash_anchor.next;
|
|
|
|
tmp != &mf_table[hash].hash_anchor;
|
|
|
|
tmp = tmp->next) {
|
2013-01-23 11:21:22 +00:00
|
|
|
struct mapfree_info *_mf = container_of(tmp, struct mapfree_info, mf_head);
|
|
|
|
if (unlikely(_mf->mf_flags == flags && !strcmp(_mf->mf_name, name))) {
|
|
|
|
MARS_WRN("race on creation of '%s' detected\n", name);
|
|
|
|
_mapfree_put(mf);
|
|
|
|
mf = _mf;
|
2013-07-09 16:50:18 +00:00
|
|
|
atomic_inc(&mf->mf_count);
|
2013-01-23 11:21:22 +00:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
}
|
2020-09-08 08:13:17 +00:00
|
|
|
list_add_tail(&mf->mf_head, &mf_table[hash].hash_anchor);
|
2013-01-23 11:21:22 +00:00
|
|
|
leave:
|
2020-09-08 08:13:17 +00:00
|
|
|
up_write(&mf_table[hash].hash_mutex);
|
2013-01-23 11:21:22 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
done:
|
|
|
|
return mf;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(mapfree_get);
|
|
|
|
|
2013-04-16 07:37:54 +00:00
|
|
|
void mapfree_set(struct mapfree_info *mf, loff_t min, loff_t max)
|
2013-01-23 11:21:22 +00:00
|
|
|
{
|
2021-11-03 13:44:17 +00:00
|
|
|
struct mf_hash_anchor *mha;
|
|
|
|
unsigned int hash;
|
|
|
|
|
|
|
|
if (unlikely(!mf))
|
|
|
|
return;
|
|
|
|
|
|
|
|
hash = mf->mf_hash;
|
|
|
|
if (unlikely(hash >= MAPFREE_HASH)) {
|
|
|
|
MARS_ERR("Bad mapfree hash %d\n", hash);
|
|
|
|
return;
|
2015-02-11 16:51:51 +00:00
|
|
|
}
|
2021-11-03 13:44:17 +00:00
|
|
|
|
|
|
|
mha = &mf_table[hash];
|
|
|
|
|
|
|
|
down_write(&mha->hash_mutex);
|
|
|
|
if (!mf->mf_min[0] || mf->mf_min[0] > min)
|
|
|
|
mf->mf_min[0] = min;
|
|
|
|
if (max >= 0 && mf->mf_max < max)
|
|
|
|
mf->mf_max = max;
|
|
|
|
up_write(&mha->hash_mutex);
|
2013-01-23 11:21:22 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(mapfree_set);
|
|
|
|
|
|
|
|
static
|
|
|
|
int mapfree_thread(void *data)
|
|
|
|
{
|
2020-09-08 08:13:17 +00:00
|
|
|
struct mf_hash_anchor *mha = data;
|
|
|
|
|
2021-11-02 09:42:47 +00:00
|
|
|
for (;;) {
|
2013-01-23 11:21:22 +00:00
|
|
|
struct mapfree_info *mf = NULL;
|
|
|
|
struct list_head *tmp;
|
|
|
|
long long eldest = 0;
|
2021-11-02 09:42:47 +00:00
|
|
|
bool is_empty = true;
|
2013-01-23 11:21:22 +00:00
|
|
|
|
2021-11-02 09:42:47 +00:00
|
|
|
wait_event_interruptible_timeout(mapfree_event,
|
|
|
|
!mapfree_period_sec,
|
|
|
|
mapfree_sleep_jiffies);
|
2013-01-23 11:21:22 +00:00
|
|
|
|
2020-09-08 08:13:17 +00:00
|
|
|
down_read(&mha->hash_mutex);
|
2013-01-23 11:21:22 +00:00
|
|
|
|
2020-09-08 08:13:17 +00:00
|
|
|
for (tmp = mha->hash_anchor.next;
|
|
|
|
tmp != &mha->hash_anchor;
|
|
|
|
tmp = tmp->next) {
|
2013-01-23 11:21:22 +00:00
|
|
|
struct mapfree_info *_mf = container_of(tmp, struct mapfree_info, mf_head);
|
2021-11-02 09:42:47 +00:00
|
|
|
|
|
|
|
if (_mf->mf_finished)
|
|
|
|
continue;
|
|
|
|
is_empty = false;
|
2013-01-23 11:21:22 +00:00
|
|
|
if (unlikely(!_mf->mf_jiffies)) {
|
|
|
|
_mf->mf_jiffies = jiffies;
|
2021-11-02 09:42:47 +00:00
|
|
|
if (mapfree_period_sec > 0)
|
|
|
|
continue;
|
2013-01-23 11:21:22 +00:00
|
|
|
}
|
|
|
|
if ((long long)jiffies - _mf->mf_jiffies > mapfree_period_sec * HZ &&
|
|
|
|
(!mf || _mf->mf_jiffies < eldest)) {
|
|
|
|
mf = _mf;
|
|
|
|
eldest = _mf->mf_jiffies;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (mf)
|
|
|
|
atomic_inc(&mf->mf_count);
|
|
|
|
|
2020-09-08 08:13:17 +00:00
|
|
|
up_read(&mha->hash_mutex);
|
2013-01-23 11:21:22 +00:00
|
|
|
|
|
|
|
if (!mf) {
|
2021-11-02 09:42:47 +00:00
|
|
|
if (!is_empty)
|
|
|
|
continue;
|
|
|
|
if (brick_thread_should_stop())
|
|
|
|
break;
|
|
|
|
brick_msleep(1);
|
2013-01-23 11:21:22 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-09-16 09:51:03 +00:00
|
|
|
mapfree_pages(mf, mapfree_grace_keep_mb);
|
2013-01-23 11:21:22 +00:00
|
|
|
|
2021-11-02 09:42:47 +00:00
|
|
|
if (brick_thread_should_stop())
|
|
|
|
mf->mf_finished = true;
|
2013-01-23 11:21:22 +00:00
|
|
|
mf->mf_jiffies = jiffies;
|
|
|
|
mapfree_put(mf);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-12-10 09:57:53 +00:00
|
|
|
////////////////// dirty IOs in append mode //////////////////
|
|
|
|
|
|
|
|
static
|
|
|
|
struct dirty_length *_get_dl(struct mapfree_info *mf, enum dirty_stage stage)
|
|
|
|
{
|
|
|
|
#ifdef MARS_DEBUGGING
|
|
|
|
if (unlikely(stage < 0)) {
|
|
|
|
MARS_ERR("bad stage=%d\n", stage);
|
|
|
|
stage = 0;
|
|
|
|
}
|
|
|
|
if (unlikely(stage >= DIRTY_MAX)) {
|
|
|
|
MARS_ERR("bad stage=%d\n", stage);
|
|
|
|
stage = DIRTY_MAX - 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return &mf->mf_length[stage];
|
|
|
|
}
|
|
|
|
|
|
|
|
void mf_dirty_append(struct mapfree_info *mf, enum dirty_stage stage, loff_t newlen)
|
|
|
|
{
|
|
|
|
struct dirty_length *dl = _get_dl(mf, stage);
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
traced_writelock(&dl->dl_lock, flags);
|
2020-11-05 07:30:05 +00:00
|
|
|
dl->dl_appends++;
|
2017-12-10 09:57:53 +00:00
|
|
|
if (dl->dl_length < newlen)
|
|
|
|
dl->dl_length = newlen;
|
|
|
|
traced_writeunlock(&dl->dl_lock, flags);
|
|
|
|
}
|
|
|
|
|
2020-04-04 08:42:01 +00:00
|
|
|
void mf_dirty_reduce(struct mapfree_info *mf, enum dirty_stage stage, loff_t newlen)
|
|
|
|
{
|
|
|
|
struct dirty_length *dl = _get_dl(mf, stage);
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
traced_writelock(&dl->dl_lock, flags);
|
|
|
|
if (dl->dl_length > newlen)
|
|
|
|
dl->dl_length = newlen;
|
|
|
|
traced_writeunlock(&dl->dl_lock, flags);
|
|
|
|
}
|
|
|
|
|
2017-12-10 09:57:53 +00:00
|
|
|
loff_t mf_dirty_length(struct mapfree_info *mf, enum dirty_stage stage)
|
|
|
|
{
|
2022-06-21 12:30:05 +00:00
|
|
|
struct dirty_length *dl;
|
|
|
|
loff_t res = 0;
|
|
|
|
|
|
|
|
CHECK_PTR_NULL(mf, done);
|
|
|
|
dl = _get_dl(mf, stage);
|
|
|
|
CHECK_PTR_NULL(dl, done);
|
2017-12-10 09:57:53 +00:00
|
|
|
|
|
|
|
#ifdef CONFIG_64BIT
|
2020-11-05 07:30:05 +00:00
|
|
|
/* Use the real length when no writes are flying.
|
|
|
|
*/
|
|
|
|
if (stage > 0) {
|
|
|
|
struct dirty_length *d0 = _get_dl(mf, 0);
|
2021-01-25 10:56:46 +00:00
|
|
|
u64 nr1 = READ_ONCE(dl->dl_appends);
|
|
|
|
u64 nr0 = READ_ONCE(d0->dl_appends);
|
2020-11-05 07:30:05 +00:00
|
|
|
|
|
|
|
if (nr0 <= nr1) {
|
|
|
|
loff_t real_size = mapfree_real_size(mf);
|
|
|
|
|
|
|
|
/* check for races once again */
|
2021-01-25 10:56:46 +00:00
|
|
|
nr1 = READ_ONCE(dl->dl_appends);
|
|
|
|
nr0 = READ_ONCE(d0->dl_appends);
|
2020-11-05 07:30:05 +00:00
|
|
|
if (nr0 <= nr1)
|
|
|
|
return real_size;
|
|
|
|
}
|
|
|
|
}
|
2022-06-21 12:30:05 +00:00
|
|
|
res = READ_ONCE(dl->dl_length);
|
2017-12-10 09:57:53 +00:00
|
|
|
#else /* cannot rely on atomic read of two 32bit values */
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
traced_readlock(&dl->dl_lock, flags);
|
|
|
|
res = dl->dl_length;
|
|
|
|
traced_readunlock(&dl->dl_lock, flags);
|
|
|
|
#endif
|
2022-06-21 12:30:05 +00:00
|
|
|
done:
|
|
|
|
return res;
|
2017-12-10 09:57:53 +00:00
|
|
|
}
|
|
|
|
|
2014-03-30 06:18:47 +00:00
|
|
|
////////////////// dirty IOs on the fly //////////////////
|
|
|
|
|
2017-12-10 10:30:21 +00:00
|
|
|
loff_t mf_get_any_dirty(const char *filename, int stage)
|
2014-03-30 06:18:47 +00:00
|
|
|
{
|
2020-09-08 08:13:17 +00:00
|
|
|
unsigned int hash = mf_hash(filename);
|
2017-12-10 10:30:21 +00:00
|
|
|
loff_t res = -1;
|
2014-03-30 06:18:47 +00:00
|
|
|
struct list_head *tmp;
|
|
|
|
|
2020-09-08 08:13:17 +00:00
|
|
|
down_read(&mf_table[hash].hash_mutex);
|
|
|
|
for (tmp = mf_table[hash].hash_anchor.next;
|
|
|
|
tmp != &mf_table[hash].hash_anchor;
|
|
|
|
tmp = tmp->next) {
|
2014-03-30 06:18:47 +00:00
|
|
|
struct mapfree_info *mf = container_of(tmp, struct mapfree_info, mf_head);
|
|
|
|
if (!strcmp(mf->mf_name, filename)) {
|
2021-07-12 09:46:06 +00:00
|
|
|
loff_t len = mf_dirty_length(mf, stage);
|
|
|
|
|
|
|
|
if (len > res)
|
|
|
|
res = len;
|
2014-03-30 06:18:47 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-08 08:13:17 +00:00
|
|
|
up_read(&mf_table[hash].hash_mutex);
|
2017-12-10 10:30:21 +00:00
|
|
|
return res;
|
2014-03-30 06:18:47 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(mf_get_any_dirty);
|
|
|
|
|
2013-01-23 11:21:22 +00:00
|
|
|
////////////////// module init stuff /////////////////////////
|
|
|
|
|
|
|
|
int __init init_mars_mapfree(void)
|
|
|
|
{
|
2020-09-08 08:13:17 +00:00
|
|
|
int i;
|
|
|
|
|
2013-01-23 11:21:22 +00:00
|
|
|
MARS_DBG("init_mapfree()\n");
|
2021-11-02 09:42:47 +00:00
|
|
|
mapfree_sleep_jiffies = MAPFREE_SLEEP_JIFFIES;
|
2020-09-08 08:13:17 +00:00
|
|
|
for (i = 0; i < MAPFREE_HASH; i++) {
|
|
|
|
init_rwsem(&mf_table[i].hash_mutex);
|
|
|
|
INIT_LIST_HEAD(&mf_table[i].hash_anchor);
|
2021-11-03 13:04:22 +00:00
|
|
|
mf_table[i].hash_thread = NULL;
|
|
|
|
}
|
|
|
|
for (i = 0; i < MAPFREE_HASH; i++) {
|
|
|
|
struct task_struct *thread;
|
|
|
|
|
2020-09-08 08:13:17 +00:00
|
|
|
thread = brick_thread_create(mapfree_thread,
|
|
|
|
&mf_table[i],
|
|
|
|
"mars_mf%d", i);
|
|
|
|
if (unlikely(!thread)) {
|
|
|
|
MARS_ERR("could not create mapfree thread %d\n", i);
|
2021-11-03 13:04:22 +00:00
|
|
|
exit_mars_mapfree();
|
2020-09-08 08:13:17 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
mf_table[i].hash_thread = thread;
|
2013-01-23 11:21:22 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-23 11:16:26 +00:00
|
|
|
void exit_mars_mapfree(void)
|
2013-01-23 11:21:22 +00:00
|
|
|
{
|
2020-09-08 08:13:17 +00:00
|
|
|
int i;
|
|
|
|
|
2013-01-23 11:21:22 +00:00
|
|
|
MARS_DBG("exit_mapfree()\n");
|
2021-11-02 09:42:47 +00:00
|
|
|
mapfree_period_sec = -1;
|
|
|
|
mapfree_sleep_jiffies = 1;
|
|
|
|
wake_up_interruptible_all(&mapfree_event);
|
|
|
|
|
2020-09-08 08:13:17 +00:00
|
|
|
for (i = 0; i < MAPFREE_HASH; i++) {
|
2021-11-02 09:42:47 +00:00
|
|
|
struct task_struct *thread = mf_table[i].hash_thread;
|
|
|
|
|
|
|
|
if (thread) {
|
2020-09-08 08:13:17 +00:00
|
|
|
mf_table[i].hash_thread = NULL;
|
2021-11-02 09:42:47 +00:00
|
|
|
brick_thread_stop(thread);
|
2020-09-08 08:13:17 +00:00
|
|
|
}
|
|
|
|
CHECK_HEAD_EMPTY(&mf_table[i].hash_anchor);
|
2013-01-23 11:21:22 +00:00
|
|
|
}
|
|
|
|
}
|