<commit_before>/* $Id$ */
/*
 * %GPL_START_LICENSE%
 * ---------------------------------------------------------------------
 * Copyright 2015-2016, Google, Inc.
 * Copyright 2006-2016, Pittsburgh Supercomputing Center
 * All rights reserved.
 *
 * 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 WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License contained in the file
 * `COPYING-GPL' at the top of this distribution or at
 * https://www.gnu.org/licenses/gpl-2.0.html for more details.
 * ---------------------------------------------------------------------
 * %END_LICENSE%
 */

#include "pfl/alloc.h"
#include "pfl/atomic.h"
#include "pfl/ctlsvr.h"
#include "pfl/export.h"
#include "pfl/fs.h"
#include "pfl/hashtbl.h"
#include "pfl/lockedlist.h"
#include "pfl/log.h"
#include "pfl/random.h"
#include "pfl/rpclog.h"
#include "pfl/rsx.h"
#include "pfl/tree.h"
#include "pfl/treeutil.h"
#include "pfl/workthr.h"

#include "bmap.h"
#include "bmap_mds.h"
#include "cache_params.h"
#include "fidc_mds.h"
#include "fidcache.h"
#include "inode.h"
#include "journal_mds.h"
#include "mdsio.h"
#include "mkfn.h"
#include "pathnames.h"
#include "repl_mds.h"
#include "rpc_mds.h"
#include "slashd.h"
#include "slashrpc.h"
#include "slerr.h"
#include "up_sched_res.h"

#include "zfs-fuse/zfs_slashlib.h"

#define	SLM_CBARG_SLOT_CSVC	0

struct pfl_odt		*slm_bia_odt;

int			slm_max_ios = SL_MAX_REPLICAS;

/*
 * Knobs that allow us to turn off some features easily if
 * they turn out to be unstable.
 */
int			slm_ptrunc_enabled = 1;
int			slm_preclaim_enabled = 1;

__static int slm_ptrunc_prepare(struct fidc_membh *, struct srt_stat *, int);

int
mds_bmap_exists(struct fidc_membh *f, sl_bmapno_t n)
{
	sl_bmapno_t nb;
	int locked;

	if (n == 0)
		return (1);

	locked = FCMH_RLOCK(f);
#if 0
	rc = mds_stat_refresh_locked(f);
	if (rc)
		return (rc);
#endif

	nb = fcmh_nvalidbmaps(f);

	/* XXX just read the bmap and check for valid state */

	psclog_debug("f+g="SLPRI_FG" nb=%u fsz=%"PSCPRIdOFFT,
	    SLPRI_FG_ARGS(&f->fcmh_fg), nb, fcmh_2_fsz(f));

	FCMH_URLOCK(f, locked);
	return (n < nb);
}

/*
 * Calculate the number of valid bytes in the bmap.
 */
int64_t
slm_bmap_calc_repltraffic(struct bmap *b)
{
	int i, locked[2], lastslvr, lastsize;
	struct bmap_mds_info *bmi;
	struct fidc_membh *f;
	sl_bmapno_t lastbno;
	int64_t amt = 0;

	f = b->bcm_fcmh;
	locked[0] = FCMH_RLOCK(f);
	locked[1] = BMAP_RLOCK(b);

	lastbno = fcmh_nvalidbmaps(f);
	if (lastbno)
		lastbno--;

	if (fcmh_2_fsz(f)) {
		off_t bmapsize;

		bmapsize = fcmh_2_fsz(f) % SLASH_BMAP_SIZE;
		if (bmapsize == 0)
			bmapsize = SLASH_BMAP_SIZE;

		/* last slvr number within the bmap */
		lastslvr = (bmapsize - 1) / SLASH_SLVR_SIZE;
		lastsize = fcmh_2_fsz(f) % SLASH_SLVR_SIZE;
		if (lastsize == 0)
			lastsize = SLASH_SLVR_SIZE;
	} else {
		/*
		 * XXX can we unlock and return here
		 * without going through the next loop?
		 */ 
		lastslvr = 0;
		lastsize = 0;
	}

	bmi = bmap_2_bmi(b);
	for (i = 0; i < SLASH_SLVRS_PER_BMAP; i++) {
		if (bmi->bmi_crcstates[i] & BMAP_SLVR_DATA) {
			/*
			 * If this is the last sliver of the last bmap,
			 * tally only the portion of data that exists.
			 */
			if (b->bcm_bmapno == lastbno &&
			    i == lastslvr) {
				amt += lastsize;
				break;
			}
			amt += SLASH_SLVR_SIZE;
		}
	}
	BMAP_URLOCK(b, locked[1]);
	FCMH_URLOCK(f, locked[0]);
	return (amt);
}

/*
 * Called when a new read or write lease is added to the bmap.
 * Maintains the DIO status of the bmap based on the numbers of readers
 * and writers present.
 * @b: the locked bmap
 * @rw: read / write op
 * @np: value-result target ION network + process ID.
 * Note: the new bml has yet to be added.
 */
__static int
mds_bmap_directio(struct bmap *b, enum rw rw, int want_dio,
    lnet_process_id_t *np)
{
	struct bmap_mds_info *bmi = bmap_2_bmi(b);
	struct bmap_mds_lease *bml, *tmp;
	int rc = 0, force_dio = 0;

	BMAP_LOCK_ENSURE(b);<commit_msg>We don't really use bmap state information to optimizae replication or I/O request for a client. So let go for simplicity.<commit_after>/* $Id$ */
/*
 * %GPL_START_LICENSE%
 * ---------------------------------------------------------------------
 * Copyright 2015-2016, Google, Inc.
 * Copyright 2006-2016, Pittsburgh Supercomputing Center
 * All rights reserved.
 *
 * 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 WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License contained in the file
 * `COPYING-GPL' at the top of this distribution or at
 * https://www.gnu.org/licenses/gpl-2.0.html for more details.
 * ---------------------------------------------------------------------
 * %END_LICENSE%
 */

#include "pfl/alloc.h"
#include "pfl/atomic.h"
#include "pfl/ctlsvr.h"
#include "pfl/export.h"
#include "pfl/fs.h"
#include "pfl/hashtbl.h"
#include "pfl/lockedlist.h"
#include "pfl/log.h"
#include "pfl/random.h"
#include "pfl/rpclog.h"
#include "pfl/rsx.h"
#include "pfl/tree.h"
#include "pfl/treeutil.h"
#include "pfl/workthr.h"

#include "bmap.h"
#include "bmap_mds.h"
#include "cache_params.h"
#include "fidc_mds.h"
#include "fidcache.h"
#include "inode.h"
#include "journal_mds.h"
#include "mdsio.h"
#include "mkfn.h"
#include "pathnames.h"
#include "repl_mds.h"
#include "rpc_mds.h"
#include "slashd.h"
#include "slashrpc.h"
#include "slerr.h"
#include "up_sched_res.h"

#include "zfs-fuse/zfs_slashlib.h"

#define	SLM_CBARG_SLOT_CSVC	0

struct pfl_odt		*slm_bia_odt;

int			slm_max_ios = SL_MAX_REPLICAS;

/*
 * Knobs that allow us to turn off some features easily if
 * they turn out to be unstable.
 */
int			slm_ptrunc_enabled = 1;
int			slm_preclaim_enabled = 1;

__static int slm_ptrunc_prepare(struct fidc_membh *, struct srt_stat *, int);

int
mds_bmap_exists(struct fidc_membh *f, sl_bmapno_t n)
{
	sl_bmapno_t nb;
	int locked;

	if (n == 0)
		return (1);

	locked = FCMH_RLOCK(f);
#if 0
	rc = mds_stat_refresh_locked(f);
	if (rc)
		return (rc);
#endif

	nb = fcmh_nvalidbmaps(f);

	/* XXX just read the bmap and check for valid state */

	psclog_debug("f+g="SLPRI_FG" nb=%u fsz=%"PSCPRIdOFFT,
	    SLPRI_FG_ARGS(&f->fcmh_fg), nb, fcmh_2_fsz(f));

	FCMH_URLOCK(f, locked);
	return (n < nb);
}

/*
 * Calculate the number of valid bytes in the bmap.
 */
int64_t
slm_bmap_calc_repltraffic(struct bmap *b)
{
	struct fidc_membh *f;
	sl_bmapno_t lastbno;
	int64_t amt;

	f = b->bcm_fcmh;
	lastbno = fcmh_nvalidbmaps(f);
	if (lastbno)
		lastbno--;

	if (b->bcm_bmapno == lastbno)
		amt = fcmh_2_fsz(f) % SLASH_BMAP_SIZE;
	else
		amt = SLASH_BMAP_SIZE;

	return (amt);
}

/*
 * Called when a new read or write lease is added to the bmap.
 * Maintains the DIO status of the bmap based on the numbers of readers
 * and writers present.
 * @b: the locked bmap
 * @rw: read / write op
 * @np: value-result target ION network + process ID.
 * Note: the new bml has yet to be added.
 */
__static int
mds_bmap_directio(struct bmap *b, enum rw rw, int want_dio,
    lnet_process_id_t *np)
{
	struct bmap_mds_info *bmi = bmap_2_bmi(b);
	struct bmap_mds_lease *bml, *tmp;
	int rc = 0, force_dio = 0;

	BMAP_LOCK_ENSURE(b);
{'fraction_non_alphanumeric': 0.15310880829015544, 'fraction_numerical': 0.010751295336787564, 'mean_word_length': 3.318232662192394, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 10, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '172012', 'n_tokens_mistral': 3221, 'n_tokens_neox': 3078, 'n_words': 848}