<commit_before>/*
 * 2007+ Copyright (c) <NAME> <<EMAIL>>
 * 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 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.
 */

#include <linux/module.h>
#include <linux/crypto.h>
#include <linux/fs.h>
#include <linux/jhash.h>
#include <linux/hash.h>
#include <linux/ktime.h>
#include <linux/mempool.h>
#include <linux/mm.h>
#include <linux/mount.h>
#include <linux/pagemap.h>
#include <linux/parser.h>
#include <linux/poll.h>
#include <linux/swap.h>
#include <linux/slab.h>
#include <linux/statfs.h>
#include <linux/writeback.h>

#include "netfs.h"

static struct kmem_cache *netfs_trans_dst;
static mempool_t *netfs_trans_dst_pool;

static void netfs_trans_init_static(struct netfs_trans *t, int num, int size)
{
	t->page_num = num;
	t->total_size = size;
	atomic_set(&t->refcnt, 1);

	spin_lock_init(&t->dst_lock);
	INIT_LIST_HEAD(&t->dst_list);
}

static int netfs_trans_send_pages(struct netfs_trans *t, struct netfs_state *st)
{
	int err = 0;
	unsigned int i, attached_pages = t->attached_pages, ci;
	struct msghdr msg;
	struct page **pages = (t->eng)?t->eng->pages:t->pages;
	struct page *p;
	unsigned int size;

	msg.msg_name = NULL;
	msg.msg_namelen = 0;
	msg.msg_control = NULL;
	msg.msg_controllen = 0;
	msg.msg_flags = MSG_WAITALL | MSG_MORE;

	ci = 0;
	for (i=0; i<t->page_num; ++i) {
		struct page *page = pages[ci];
		struct netfs_cmd cmd;
		struct iovec io;

		p = t->pages[i];

		if (!p)
			continue;

		size = page_private(p);

		io.iov_base = &cmd;
		io.iov_len = sizeof(struct netfs_cmd);

		cmd.cmd = NETFS_WRITE_PAGE;
		cmd.ext = 0;
		cmd.id = 0;
		cmd.size = size;
		cmd.start = p->index;
		cmd.start <<= PAGE_CACHE_SHIFT;
		cmd.csize = 0;
		cmd.cpad = 0;
		cmd.iv = pohmelfs_gen_iv(t);

		netfs_convert_cmd(&cmd);

		msg.msg_iov = &io;
		msg.msg_iovlen = 1;
		msg.msg_flags = MSG_WAITALL | MSG_MORE;

		err = kernel_sendmsg(st->socket, &msg, (struct kvec *)msg.msg_iov, 1, sizeof(struct netfs_cmd));
		if (err <= 0) {
			printk("%s: %d/%d failed to send transaction header: t: %p, gen: %u, err: %d.\n",
					__func__, i, t->page_num, t, t->gen, err);
			if (err == 0)
				err = -ECONNRESET;
			goto err_out;
		}

		msg.msg_flags = MSG_WAITALL | (attached_pages == 1 ? 0 :
				MSG_MORE);

		err = kernel_sendpage(st->socket, page, 0, size, msg.msg_flags);
		if (err <= 0) {
			printk("%s: %d/%d failed to send transaction page: t: %p, gen: %u, size: %u, err: %d.\n",
					__func__, i, t->page_num, t, t->gen, size, err);
			if (err == 0)
				err = -ECONNRESET;
			goto err_out;
		}

		dprintk("%s: %d/%d sent t: %p, gen: %u, page: %p/%p, size: %u.\n",
			__func__, i, t->page_num, t, t->gen, page, p, size);

		err = 0;
		attached_pages--;
		if (!attached_pages)
			break;
		ci++;

		continue;

err_out:
		printk("%s: t: %p, gen: %u, err: %d.\n", __func__, t, t->gen, err);
		netfs_state_exit(st);
		break;
	}

	return err;
}

int netfs_trans_send(struct netfs_trans *t, struct netfs_state *st)
{
	int err;
	struct msghdr msg;

	BUG_ON(!t->iovec.iov_len);
	BUG_ON(t->iovec.iov_len > 1024*1024*1024);

	netfs_state_lock_send(st);
	if (!st->socket) {
		err = netfs_state_init(st);
		if (err)
			goto err_out_unlock_return;
	}

	msg.msg_iov = &t->iovec;
	msg.msg_iovlen = 1;
	msg.msg_name = NULL;
	msg.msg_namelen = 0;
	msg.msg_control = NULL;
	msg.msg_controllen = 0;
	msg.msg_flags = MSG_WAITALL;

	if (t->attached_pages)
		msg.msg_flags |= MSG_MORE;

	err = kernel_sendmsg(st->socket, &msg, (struct kvec *)msg.msg_iov, 1, t->iovec.iov_len);
	if (err <= 0) {
		printk("%s: failed to send contig transaction: t: %p, gen: %u, size: %zu, err: %d.\n",
				__func__, t, t->gen, t->iovec.iov_len, err);
		if (err == 0)
			err = -ECONNRESET;
		goto err_out_unlock_return;
	}

	dprintk("%s: sent %s transaction: t: %p, gen: %u, size: %zu, page_num: %u.\n",
			__func__, (t->page_num)?"partial":"full",
			t, t->gen, t->iovec.iov_len, t->page_num);

	err = 0;
	if (t->attached_pages)
		err = netfs_trans_send_pages(t, st);

err_out_unlock_return:

	if (st->need_reset)
		netfs_state_exit(st);

	netfs_state_unlock_send(st);

	dprintk("%s: t: %p, gen: %u, err: %d.\n",
		__func__, t, t->gen, err);

	t->result = err;
	return err;
}

static inline int netfs_trans_cmp(unsigned int gen, unsigned int new)
{
	if (gen < new)
		return 1;
	if (gen > new)
		return -1;
	return 0;
}
<commit_msg>Staging: pohmelfs: Coding style fixes in trans.c<commit_after>/*
 * 2007+ Copyright (c) <NAME> <<EMAIL>>
 * 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 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.
 */

#include <linux/module.h>
#include <linux/crypto.h>
#include <linux/fs.h>
#include <linux/jhash.h>
#include <linux/hash.h>
#include <linux/ktime.h>
#include <linux/mempool.h>
#include <linux/mm.h>
#include <linux/mount.h>
#include <linux/pagemap.h>
#include <linux/parser.h>
#include <linux/poll.h>
#include <linux/swap.h>
#include <linux/slab.h>
#include <linux/statfs.h>
#include <linux/writeback.h>

#include "netfs.h"

static struct kmem_cache *netfs_trans_dst;
static mempool_t *netfs_trans_dst_pool;

static void netfs_trans_init_static(struct netfs_trans *t, int num, int size)
{
	t->page_num = num;
	t->total_size = size;
	atomic_set(&t->refcnt, 1);

	spin_lock_init(&t->dst_lock);
	INIT_LIST_HEAD(&t->dst_list);
}

static int netfs_trans_send_pages(struct netfs_trans *t, struct netfs_state *st)
{
	int err = 0;
	unsigned int i, attached_pages = t->attached_pages, ci;
	struct msghdr msg;
	struct page **pages = (t->eng) ? t->eng->pages : t->pages;
	struct page *p;
	unsigned int size;

	msg.msg_name = NULL;
	msg.msg_namelen = 0;
	msg.msg_control = NULL;
	msg.msg_controllen = 0;
	msg.msg_flags = MSG_WAITALL | MSG_MORE;

	ci = 0;
	for (i = 0; i < t->page_num; ++i) {
		struct page *page = pages[ci];
		struct netfs_cmd cmd;
		struct iovec io;

		p = t->pages[i];

		if (!p)
			continue;

		size = page_private(p);

		io.iov_base = &cmd;
		io.iov_len = sizeof(struct netfs_cmd);

		cmd.cmd = NETFS_WRITE_PAGE;
		cmd.ext = 0;
		cmd.id = 0;
		cmd.size = size;
		cmd.start = p->index;
		cmd.start <<= PAGE_CACHE_SHIFT;
		cmd.csize = 0;
		cmd.cpad = 0;
		cmd.iv = pohmelfs_gen_iv(t);

		netfs_convert_cmd(&cmd);

		msg.msg_iov = &io;
		msg.msg_iovlen = 1;
		msg.msg_flags = MSG_WAITALL | MSG_MORE;

		err = kernel_sendmsg(st->socket, &msg, (struct kvec *)msg.msg_iov, 1, sizeof(struct netfs_cmd));
		if (err <= 0) {
			printk("%s: %d/%d failed to send transaction header: t: %p, gen: %u, err: %d.\n",
					__func__, i, t->page_num, t, t->gen, err);
			if (err == 0)
				err = -ECONNRESET;
			goto err_out;
		}

		msg.msg_flags = MSG_WAITALL | (attached_pages == 1 ? 0 :
				MSG_MORE);

		err = kernel_sendpage(st->socket, page, 0, size, msg.msg_flags);
		if (err <= 0) {
			printk("%s: %d/%d failed to send transaction page: t: %p, gen: %u, size: %u, err: %d.\n",
					__func__, i, t->page_num, t, t->gen, size, err);
			if (err == 0)
				err = -ECONNRESET;
			goto err_out;
		}

		dprintk("%s: %d/%d sent t: %p, gen: %u, page: %p/%p, size: %u.\n",
			__func__, i, t->page_num, t, t->gen, page, p, size);

		err = 0;
		attached_pages--;
		if (!attached_pages)
			break;
		ci++;

		continue;

err_out:
		printk("%s: t: %p, gen: %u, err: %d.\n", __func__, t, t->gen, err);
		netfs_state_exit(st);
		break;
	}

	return err;
}

int netfs_trans_send(struct netfs_trans *t, struct netfs_state *st)
{
	int err;
	struct msghdr msg;

	BUG_ON(!t->iovec.iov_len);
	BUG_ON(t->iovec.iov_len > 1024*1024*1024);

	netfs_state_lock_send(st);
	if (!st->socket) {
		err = netfs_state_init(st);
		if (err)
			goto err_out_unlock_return;
	}

	msg.msg_iov = &t->iovec;
	msg.msg_iovlen = 1;
	msg.msg_name = NULL;
	msg.msg_namelen = 0;
	msg.msg_control = NULL;
	msg.msg_controllen = 0;
	msg.msg_flags = MSG_WAITALL;

	if (t->attached_pages)
		msg.msg_flags |= MSG_MORE;

	err = kernel_sendmsg(st->socket, &msg, (struct kvec *)msg.msg_iov, 1, t->iovec.iov_len);
	if (err <= 0) {
		printk("%s: failed to send contig transaction: t: %p, gen: %u, size: %zu, err: %d.\n",
				__func__, t, t->gen, t->iovec.iov_len, err);
		if (err == 0)
			err = -ECONNRESET;
		goto err_out_unlock_return;
	}

	dprintk("%s: sent %s transaction: t: %p, gen: %u, size: %zu, page_num: %u.\n",
			__func__, (t->page_num) ? "partial" : "full",
			t, t->gen, t->iovec.iov_len, t->page_num);

	err = 0;
	if (t->attached_pages)
		err = netfs_trans_send_pages(t, st);

err_out_unlock_return:

	if (st->need_reset)
		netfs_state_exit(st);

	netfs_state_unlock_send(st);

	dprintk("%s: t: %p, gen: %u, err: %d.\n",
		__func__, t, t->gen, err);

	t->result = err;
	return err;
}

static inline int netfs_trans_cmp(unsigned int gen, unsigned int new)
{
	if (gen < new)
		return 1;
	if (gen > new)
		return -1;
	return 0;
}

{'fraction_non_alphanumeric': 0.15975342179500573, 'fraction_numerical': 0.009821335283669418, 'mean_word_length': 3.715270935960591, 'pattern_counts': {'":': 1, '<': 55, '<?xml version=': 0, '>': 123, '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': '104135', 'n_tokens_mistral': 4253, 'n_tokens_neox': 3946, 'n_words': 1034}