<commit_before>/*
 *  linux/net/sunrpc/clnt.c
 *
 *  This file contains the high-level RPC interface.
 *  It is modeled as a finite state machine to support both synchronous
 *  and asynchronous requests.
 *
 *  -	RPC header generation and argument serialization.
 *  -	Credential refresh.
 *  -	TCP connect handling.
 *  -	Retry of operation when it is suspected the operation failed because
 *	of uid squashing on the server, or when the credentials were stale
 *	and need to be refreshed, or when a packet was damaged in transit.
 *	This may be have to be moved to the VFS layer.
 *
 *  Copyright (C) 1992,1993 <NAME> <<EMAIL>>
 *  Copyright (C) 1995,1996 <NAME> <<EMAIL>>
 */


#include <linux/module.h>
#include <linux/types.h>
#include <linux/kallsyms.h>
#include <linux/mm.h>
#include <linux/namei.h>
#include <linux/mount.h>
#include <linux/slab.h>
#include <linux/utsname.h>
#include <linux/workqueue.h>
#include <linux/in.h>
#include <linux/in6.h>
#include <linux/un.h>
#include <linux/rcupdate.h>

#include <linux/sunrpc/clnt.h>
#include <linux/sunrpc/rpc_pipe_fs.h>
#include <linux/sunrpc/metrics.h>
#include <linux/sunrpc/bc_xprt.h>
#include <trace/events/sunrpc.h>

#include "sunrpc.h"
#include "netns.h"

#ifdef RPC_DEBUG
# define RPCDBG_FACILITY	RPCDBG_CALL
#endif

#define dprint_status(t)					\
	dprintk("RPC: %5u %s (status %d)\n", t->tk_pid,		\
			__func__, t->tk_status)

/*
 * All RPC clients are linked into this list
 */

static DECLARE_WAIT_QUEUE_HEAD(destroy_wait);


static void	call_start(struct rpc_task *task);
static void	call_reserve(struct rpc_task *task);
static void	call_reserveresult(struct rpc_task *task);
static void	call_allocate(struct rpc_task *task);
static void	call_decode(struct rpc_task *task);
static void	call_bind(struct rpc_task *task);
static void	call_bind_status(struct rpc_task *task);
static void	call_transmit(struct rpc_task *task);
#if defined(CONFIG_SUNRPC_BACKCHANNEL)
static void	call_bc_transmit(struct rpc_task *task);
#endif /* CONFIG_SUNRPC_BACKCHANNEL */
static void	call_status(struct rpc_task *task);
static void	call_transmit_status(struct rpc_task *task);
static void	call_refresh(struct rpc_task *task);
static void	call_refreshresult(struct rpc_task *task);
static void	call_timeout(struct rpc_task *task);
static void	call_connect(struct rpc_task *task);
static void	call_connect_status(struct rpc_task *task);

static __be32	*rpc_encode_header(struct rpc_task *task);
static __be32	*rpc_verify_header(struct rpc_task *task);
static int	rpc_ping(struct rpc_clnt *clnt);

static void rpc_register_client(struct rpc_clnt *clnt)
{
	struct net *net = rpc_net_ns(clnt);
	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);

	spin_lock(&sn->rpc_client_lock);
	list_add(&clnt->cl_clients, &sn->all_clients);
	spin_unlock(&sn->rpc_client_lock);
}

static void rpc_unregister_client(struct rpc_clnt *clnt)
{
	struct net *net = rpc_net_ns(clnt);
	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);

	spin_lock(&sn->rpc_client_lock);
	list_del(&clnt->cl_clients);
	spin_unlock(&sn->rpc_client_lock);
}

static void __rpc_clnt_remove_pipedir(struct rpc_clnt *clnt)
{
	if (clnt->cl_dentry) {
		if (clnt->cl_auth && clnt->cl_auth->au_ops->pipes_destroy)
			clnt->cl_auth->au_ops->pipes_destroy(clnt->cl_auth);
		rpc_remove_client_dir(clnt->cl_dentry);
	}
	clnt->cl_dentry = NULL;
}

static void rpc_clnt_remove_pipedir(struct rpc_clnt *clnt)
{
	struct net *net = rpc_net_ns(clnt);
	struct super_block *pipefs_sb;

	pipefs_sb = rpc_get_sb_net(net);
	if (pipefs_sb) {
		__rpc_clnt_remove_pipedir(clnt);
		rpc_put_sb_net(net);
	}
}

static struct dentry *rpc_setup_pipedir_sb(struct super_block *sb,
				    struct rpc_clnt *clnt,
				    const char *dir_name)
{
	static uint32_t clntid;
	char name[15];
	struct qstr q = { .name = name };
	struct dentry *dir, *dentry;
	int error;

	dir = rpc_d_lookup_sb(sb, dir_name);
	if (dir == NULL)
		return dir;
	for (;;) {
		q.len = snprintf(name, sizeof(name), "clnt%x", (unsigned int)clntid++);
		name[sizeof(name) - 1] = '\0';
		q.hash = full_name_hash(q.name, q.len);
		dentry = rpc_create_client_dir(dir, &q, clnt);
		if (!IS_ERR(dentry))
			break;
		error = PTR_ERR(dentry);
		if (error != -EEXIST) {
			printk(KERN_INFO "RPC: Couldn't create pipefs entry"
					" %s/%s, error %d\n",
					dir_name, name, error);
			break;
		}
	}
	dput(dir);
	return dentry;
}

static int
rpc_setup_pipedir(struct rpc_clnt *clnt, const char *dir_name)
{
	struct net *net = rpc_net_ns(clnt);
	struct super_block *pipefs_sb;
	struct dentry *dentry;

	clnt->cl_dentry = NULL;
	if (dir_name == NULL)
		return 0;
	pipefs_sb = rpc_get_sb_net(net);
	if (!pipefs_sb)
		return 0;
	dentry = rpc_setup_pipedir_sb(pipefs_sb, clnt, dir_name);
	rpc_put_sb_net(net);
	if (IS_ERR(dentry))
		return PTR_ERR(dentry);
	clnt->cl_dentry = dentry;
	return 0;
}

static inline int rpc_clnt_skip_event(struct rpc_clnt *clnt, unsigned long event)
{
	if (((event == RPC_PIPEFS_MOUNT) && clnt->cl_dentry) ||
	    ((event == RPC_PIPEFS_UMOUNT) && !clnt->cl_dentry))
		return 1;
	return 0;
}

static int __rpc_clnt_handle_event(struct rpc_clnt *clnt, unsigned long event,
				   struct super_block *sb)
{
	struct dentry *dentry;
	int err = 0;

	switch (event) {
	case RPC_PIPEFS_MOUNT:
		dentry = rpc_setup_pipedir_sb(sb, clnt,
					      clnt->cl_program->pipe_dir_name);
		BUG_ON(dentry == NULL);
		if (IS_ERR(dentry))
			return PTR_ERR(dentry);
		clnt->cl_dentry = dentry;
		if (clnt->cl_auth->au_ops->pipes_create) {
			err = clnt->cl_auth->au_ops->pipes_create(clnt->cl_auth);
			if (err)
				__rpc_clnt_remove_pipedir(clnt);
		}
		break;
	case RPC_PIPEFS_UMOUNT:
		__rpc_clnt_remove_pipedir(clnt);
		break;
	default:
		printk(KERN_ERR "%s: unknown event: %ld\n", __func__, event);
		return -ENOTSUPP;
	}
	return err;
}

static int __rpc_pipefs_event(struct rpc_clnt *clnt, unsigned long event,
				struct super_block *sb)
{
	int error = 0;

	for (;; clnt = clnt->cl_parent) {<commit_msg>SUNRPC: remove BUG_ON in __rpc_clnt_handle_event<commit_after>/*
 *  linux/net/sunrpc/clnt.c
 *
 *  This file contains the high-level RPC interface.
 *  It is modeled as a finite state machine to support both synchronous
 *  and asynchronous requests.
 *
 *  -	RPC header generation and argument serialization.
 *  -	Credential refresh.
 *  -	TCP connect handling.
 *  -	Retry of operation when it is suspected the operation failed because
 *	of uid squashing on the server, or when the credentials were stale
 *	and need to be refreshed, or when a packet was damaged in transit.
 *	This may be have to be moved to the VFS layer.
 *
 *  Copyright (C) 1992,1993 <NAME> <<EMAIL>>
 *  Copyright (C) 1995,1996 <NAME> <<EMAIL>>
 */


#include <linux/module.h>
#include <linux/types.h>
#include <linux/kallsyms.h>
#include <linux/mm.h>
#include <linux/namei.h>
#include <linux/mount.h>
#include <linux/slab.h>
#include <linux/utsname.h>
#include <linux/workqueue.h>
#include <linux/in.h>
#include <linux/in6.h>
#include <linux/un.h>
#include <linux/rcupdate.h>

#include <linux/sunrpc/clnt.h>
#include <linux/sunrpc/rpc_pipe_fs.h>
#include <linux/sunrpc/metrics.h>
#include <linux/sunrpc/bc_xprt.h>
#include <trace/events/sunrpc.h>

#include "sunrpc.h"
#include "netns.h"

#ifdef RPC_DEBUG
# define RPCDBG_FACILITY	RPCDBG_CALL
#endif

#define dprint_status(t)					\
	dprintk("RPC: %5u %s (status %d)\n", t->tk_pid,		\
			__func__, t->tk_status)

/*
 * All RPC clients are linked into this list
 */

static DECLARE_WAIT_QUEUE_HEAD(destroy_wait);


static void	call_start(struct rpc_task *task);
static void	call_reserve(struct rpc_task *task);
static void	call_reserveresult(struct rpc_task *task);
static void	call_allocate(struct rpc_task *task);
static void	call_decode(struct rpc_task *task);
static void	call_bind(struct rpc_task *task);
static void	call_bind_status(struct rpc_task *task);
static void	call_transmit(struct rpc_task *task);
#if defined(CONFIG_SUNRPC_BACKCHANNEL)
static void	call_bc_transmit(struct rpc_task *task);
#endif /* CONFIG_SUNRPC_BACKCHANNEL */
static void	call_status(struct rpc_task *task);
static void	call_transmit_status(struct rpc_task *task);
static void	call_refresh(struct rpc_task *task);
static void	call_refreshresult(struct rpc_task *task);
static void	call_timeout(struct rpc_task *task);
static void	call_connect(struct rpc_task *task);
static void	call_connect_status(struct rpc_task *task);

static __be32	*rpc_encode_header(struct rpc_task *task);
static __be32	*rpc_verify_header(struct rpc_task *task);
static int	rpc_ping(struct rpc_clnt *clnt);

static void rpc_register_client(struct rpc_clnt *clnt)
{
	struct net *net = rpc_net_ns(clnt);
	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);

	spin_lock(&sn->rpc_client_lock);
	list_add(&clnt->cl_clients, &sn->all_clients);
	spin_unlock(&sn->rpc_client_lock);
}

static void rpc_unregister_client(struct rpc_clnt *clnt)
{
	struct net *net = rpc_net_ns(clnt);
	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);

	spin_lock(&sn->rpc_client_lock);
	list_del(&clnt->cl_clients);
	spin_unlock(&sn->rpc_client_lock);
}

static void __rpc_clnt_remove_pipedir(struct rpc_clnt *clnt)
{
	if (clnt->cl_dentry) {
		if (clnt->cl_auth && clnt->cl_auth->au_ops->pipes_destroy)
			clnt->cl_auth->au_ops->pipes_destroy(clnt->cl_auth);
		rpc_remove_client_dir(clnt->cl_dentry);
	}
	clnt->cl_dentry = NULL;
}

static void rpc_clnt_remove_pipedir(struct rpc_clnt *clnt)
{
	struct net *net = rpc_net_ns(clnt);
	struct super_block *pipefs_sb;

	pipefs_sb = rpc_get_sb_net(net);
	if (pipefs_sb) {
		__rpc_clnt_remove_pipedir(clnt);
		rpc_put_sb_net(net);
	}
}

static struct dentry *rpc_setup_pipedir_sb(struct super_block *sb,
				    struct rpc_clnt *clnt,
				    const char *dir_name)
{
	static uint32_t clntid;
	char name[15];
	struct qstr q = { .name = name };
	struct dentry *dir, *dentry;
	int error;

	dir = rpc_d_lookup_sb(sb, dir_name);
	if (dir == NULL) {
		pr_info("RPC: pipefs directory doesn't exist: %s\n", dir_name);
		return dir;
	}
	for (;;) {
		q.len = snprintf(name, sizeof(name), "clnt%x", (unsigned int)clntid++);
		name[sizeof(name) - 1] = '\0';
		q.hash = full_name_hash(q.name, q.len);
		dentry = rpc_create_client_dir(dir, &q, clnt);
		if (!IS_ERR(dentry))
			break;
		error = PTR_ERR(dentry);
		if (error != -EEXIST) {
			printk(KERN_INFO "RPC: Couldn't create pipefs entry"
					" %s/%s, error %d\n",
					dir_name, name, error);
			break;
		}
	}
	dput(dir);
	return dentry;
}

static int
rpc_setup_pipedir(struct rpc_clnt *clnt, const char *dir_name)
{
	struct net *net = rpc_net_ns(clnt);
	struct super_block *pipefs_sb;
	struct dentry *dentry;

	clnt->cl_dentry = NULL;
	if (dir_name == NULL)
		return 0;
	pipefs_sb = rpc_get_sb_net(net);
	if (!pipefs_sb)
		return 0;
	dentry = rpc_setup_pipedir_sb(pipefs_sb, clnt, dir_name);
	rpc_put_sb_net(net);
	if (IS_ERR(dentry))
		return PTR_ERR(dentry);
	clnt->cl_dentry = dentry;
	return 0;
}

static inline int rpc_clnt_skip_event(struct rpc_clnt *clnt, unsigned long event)
{
	if (((event == RPC_PIPEFS_MOUNT) && clnt->cl_dentry) ||
	    ((event == RPC_PIPEFS_UMOUNT) && !clnt->cl_dentry))
		return 1;
	return 0;
}

static int __rpc_clnt_handle_event(struct rpc_clnt *clnt, unsigned long event,
				   struct super_block *sb)
{
	struct dentry *dentry;
	int err = 0;

	switch (event) {
	case RPC_PIPEFS_MOUNT:
		dentry = rpc_setup_pipedir_sb(sb, clnt,
					      clnt->cl_program->pipe_dir_name);
		if (!dentry)
			return -ENOENT;
		if (IS_ERR(dentry))
			return PTR_ERR(dentry);
		clnt->cl_dentry = dentry;
		if (clnt->cl_auth->au_ops->pipes_create) {
			err = clnt->cl_auth->au_ops->pipes_create(clnt->cl_auth);
			if (err)
				__rpc_clnt_remove_pipedir(clnt);
		}
		break;
	case RPC_PIPEFS_UMOUNT:
		__rpc_clnt_remove_pipedir(clnt);
		break;
	default:
		printk(KERN_ERR "%s: unknown event: %ld\n", __func__, event);
		return -ENOTSUPP;
	}
	return err;
}

static int __rpc_pipefs_event(struct rpc_clnt *clnt, unsigned long event,
				struct super_block *sb)
{
	int error = 0;

	for (;; clnt = clnt->cl_parent) {
{'fraction_non_alphanumeric': 0.12441860465116279, 'fraction_numerical': 0.005813953488372093, 'mean_word_length': 4.916953316953317, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 121, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3051653', 'n_tokens_mistral': 5048, 'n_tokens_neox': 4821, 'n_words': 1091}