Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 | /*
* 2007+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
* 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;
}
struct netfs_trans_dst *netfs_trans_search(struct netfs_state *st, unsigned int gen)
{
struct rb_root *root = &st->trans_root;
struct rb_node *n = root->rb_node;
struct netfs_trans_dst *tmp, *ret = NULL;
struct netfs_trans *t;
int cmp;
while (n) {
tmp = rb_entry(n, struct netfs_trans_dst, state_entry);
t = tmp->trans;
cmp = netfs_trans_cmp(t->gen, gen);
if (cmp < 0)
n = n->rb_left;
else if (cmp > 0)
n = n->rb_right;
else {
ret = tmp;
break;
}
}
return ret;
}
static int netfs_trans_insert(struct netfs_trans_dst *ndst, struct netfs_state *st)
{
struct rb_root *root = &st->trans_root;
struct rb_node **n = &root->rb_node, *parent = NULL;
struct netfs_trans_dst *ret = NULL, *tmp;
struct netfs_trans *t = NULL, *new = ndst->trans;
int cmp;
while (*n) {
parent = *n;
tmp = rb_entry(parent, struct netfs_trans_dst, state_entry);
t = tmp->trans;
cmp = netfs_trans_cmp(t->gen, new->gen);
if (cmp < 0)
n = &parent->rb_left;
else if (cmp > 0)
n = &parent->rb_right;
else {
ret = tmp;
break;
}
}
if (ret) {
printk("%s: exist: old: gen: %u, flags: %x, send_time: %lu, "
"new: gen: %u, flags: %x, send_time: %lu.\n",
__func__, t->gen, t->flags, ret->send_time,
new->gen, new->flags, ndst->send_time);
return -EEXIST;
}
rb_link_node(&ndst->state_entry, parent, n);
rb_insert_color(&ndst->state_entry, root);
ndst->send_time = jiffies;
return 0;
}
int netfs_trans_remove_nolock(struct netfs_trans_dst *dst, struct netfs_state *st)
{
if (dst && dst->state_entry.rb_parent_color) {
rb_erase(&dst->state_entry, &st->trans_root);
dst->state_entry.rb_parent_color = 0;
return 1;
}
return 0;
}
static int netfs_trans_remove_state(struct netfs_trans_dst *dst)
{
int ret;
struct netfs_state *st = dst->state;
mutex_lock(&st->trans_lock);
ret = netfs_trans_remove_nolock(dst, st);
mutex_unlock(&st->trans_lock);
return ret;
}
/*
* Create new destination for given transaction associated with given network state.
* Transaction's reference counter is bumped and will be dropped when either
* reply is received or when async timeout detection task will fail resending
* and drop transaction.
*/
static int netfs_trans_push_dst(struct netfs_trans *t, struct netfs_state *st)
{
struct netfs_trans_dst *dst;
int err;
dst = mempool_alloc(netfs_trans_dst_pool, GFP_KERNEL);
if (!dst)
return -ENOMEM;
dst->retries = 0;
dst->send_time = 0;
dst->state = st;
dst->trans = t;
netfs_trans_get(t);
mutex_lock(&st->trans_lock);
err = netfs_trans_insert(dst, st);
mutex_unlock(&st->trans_lock);
if (err)
goto err_out_free;
spin_lock(&t->dst_lock);
list_add_tail(&dst->trans_entry, &t->dst_list);
spin_unlock(&t->dst_lock);
return 0;
err_out_free:
t->result = err;
netfs_trans_put(t);
mempool_free(dst, netfs_trans_dst_pool);
return err;
}
static void netfs_trans_free_dst(struct netfs_trans_dst *dst)
{
netfs_trans_put(dst->trans);
mempool_free(dst, netfs_trans_dst_pool);
}
static void netfs_trans_remove_dst(struct netfs_trans_dst *dst)
{
if (netfs_trans_remove_state(dst))
netfs_trans_free_dst(dst);
}
/*
* Drop destination transaction entry when we know it.
*/
void netfs_trans_drop_dst(struct netfs_trans_dst *dst)
{
struct netfs_trans *t = dst->trans;
spin_lock(&t->dst_lock);
list_del_init(&dst->trans_entry);
spin_unlock(&t->dst_lock);
netfs_trans_remove_dst(dst);
}
/*
* Drop destination transaction entry when we know it and when we
* already removed dst from state tree.
*/
void netfs_trans_drop_dst_nostate(struct netfs_trans_dst *dst)
{
struct netfs_trans *t = dst->trans;
spin_lock(&t->dst_lock);
list_del_init(&dst->trans_entry);
spin_unlock(&t->dst_lock);
netfs_trans_free_dst(dst);
}
/*
* This drops destination transaction entry from appropriate network state
* tree and drops related reference counter. It is possible that transaction
* will be freed here if its reference counter hits zero.
* Destination transaction entry will be freed.
*/
void netfs_trans_drop_trans(struct netfs_trans *t, struct netfs_state *st)
{
struct netfs_trans_dst *dst, *tmp, *ret = NULL;
spin_lock(&t->dst_lock);
list_for_each_entry_safe(dst, tmp, &t->dst_list, trans_entry) {
if (dst->state == st) {
ret = dst;
list_del(&dst->trans_entry);
break;
}
}
spin_unlock(&t->dst_lock);
if (ret)
netfs_trans_remove_dst(ret);
}
/*
* This drops destination transaction entry from appropriate network state
* tree and drops related reference counter. It is possible that transaction
* will be freed here if its reference counter hits zero.
* Destination transaction entry will be freed.
*/
void netfs_trans_drop_last(struct netfs_trans *t, struct netfs_state *st)
{
struct netfs_trans_dst *dst, *tmp, *ret;
spin_lock(&t->dst_lock);
ret = list_entry(t->dst_list.prev, struct netfs_trans_dst, trans_entry);
if (ret->state != st) {
ret = NULL;
list_for_each_entry_safe(dst, tmp, &t->dst_list, trans_entry) {
if (dst->state == st) {
ret = dst;
list_del_init(&dst->trans_entry);
break;
}
}
} else {
list_del(&ret->trans_entry);
}
spin_unlock(&t->dst_lock);
if (ret)
netfs_trans_remove_dst(ret);
}
static int netfs_trans_push(struct netfs_trans *t, struct netfs_state *st)
{
int err;
err = netfs_trans_push_dst(t, st);
if (err)
return err;
err = netfs_trans_send(t, st);
if (err)
goto err_out_free;
if (t->flags & NETFS_TRANS_SINGLE_DST)
pohmelfs_switch_active(st->psb);
return 0;
err_out_free:
t->result = err;
netfs_trans_drop_last(t, st);
return err;
}
int netfs_trans_finish_send(struct netfs_trans *t, struct pohmelfs_sb *psb)
{
struct pohmelfs_config *c;
int err = -ENODEV;
struct netfs_state *st;
#if 0
dprintk("%s: t: %p, gen: %u, size: %u, page_num: %u, active: %p.\n",
__func__, t, t->gen, t->iovec.iov_len, t->page_num, psb->active_state);
#endif
mutex_lock(&psb->state_lock);
list_for_each_entry(c, &psb->state_list, config_entry) {
st = &c->state;
if (t->flags & NETFS_TRANS_SINGLE_DST) {
if (!(st->ctl.perm & POHMELFS_IO_PERM_READ))
continue;
} else {
if (!(st->ctl.perm & POHMELFS_IO_PERM_WRITE))
continue;
}
if (psb->active_state && (psb->active_state->state.ctl.prio >= st->ctl.prio) &&
(t->flags & NETFS_TRANS_SINGLE_DST))
st = &psb->active_state->state;
err = netfs_trans_push(t, st);
if (!err && (t->flags & NETFS_TRANS_SINGLE_DST))
break;
}
mutex_unlock(&psb->state_lock);
#if 0
dprintk("%s: fully sent t: %p, gen: %u, size: %u, page_num: %u, err: %d.\n",
__func__, t, t->gen, t->iovec.iov_len, t->page_num, err);
#endif
if (err)
t->result = err;
return err;
}
int netfs_trans_finish(struct netfs_trans *t, struct pohmelfs_sb *psb)
{
int err;
struct netfs_cmd *cmd = t->iovec.iov_base;
t->gen = atomic_inc_return(&psb->trans_gen);
cmd->size = t->iovec.iov_len - sizeof(struct netfs_cmd) +
t->attached_size + t->attached_pages * sizeof(struct netfs_cmd);
cmd->cmd = NETFS_TRANS;
cmd->start = t->gen;
cmd->id = 0;
if (psb->perform_crypto) {
cmd->ext = psb->crypto_attached_size;
cmd->csize = psb->crypto_attached_size;
}
dprintk("%s: t: %u, size: %u, iov_len: %zu, attached_size: %u, attached_pages: %u.\n",
__func__, t->gen, cmd->size, t->iovec.iov_len, t->attached_size, t->attached_pages);
err = pohmelfs_trans_crypt(t, psb);
if (err) {
t->result = err;
netfs_convert_cmd(cmd);
dprintk("%s: trans: %llu, crypto_attached_size: %u, attached_size: %u, attached_pages: %d, trans_size: %u, err: %d.\n",
__func__, cmd->start, psb->crypto_attached_size, t->attached_size, t->attached_pages, cmd->size, err);
}
netfs_trans_put(t);
return err;
}
/*
* Resend transaction to remote server(s).
* If new servers were added into superblock, we can try to send data
* to them too.
*
* It is called under superblock's state_lock, so we can safely
* dereference psb->state_list. Also, transaction's reference counter is
* bumped, so it can not go away under us, thus we can safely access all
* its members. State is locked.
*
* This function returns 0 if transaction was successfully sent to at
* least one destination target.
*/
int netfs_trans_resend(struct netfs_trans *t, struct pohmelfs_sb *psb)
{
struct netfs_trans_dst *dst;
struct netfs_state *st;
struct pohmelfs_config *c;
int err, exist, error = -ENODEV;
list_for_each_entry(c, &psb->state_list, config_entry) {
st = &c->state;
exist = 0;
spin_lock(&t->dst_lock);
list_for_each_entry(dst, &t->dst_list, trans_entry) {
if (st == dst->state) {
exist = 1;
break;
}
}
spin_unlock(&t->dst_lock);
if (exist) {
if (!(t->flags & NETFS_TRANS_SINGLE_DST) ||
(c->config_entry.next == &psb->state_list)) {
dprintk("%s: resending st: %p, t: %p, gen: %u.\n",
__func__, st, t, t->gen);
err = netfs_trans_send(t, st);
if (!err)
error = 0;
}
continue;
}
dprintk("%s: pushing/resending st: %p, t: %p, gen: %u.\n",
__func__, st, t, t->gen);
err = netfs_trans_push(t, st);
if (err)
continue;
error = 0;
if (t->flags & NETFS_TRANS_SINGLE_DST)
break;
}
t->result = error;
return error;
}
void *netfs_trans_add(struct netfs_trans *t, unsigned int size)
{
struct iovec *io = &t->iovec;
void *ptr;
if (size > t->total_size) {
ptr = ERR_PTR(-EINVAL);
goto out;
}
if (io->iov_len + size > t->total_size) {
dprintk("%s: too big size t: %p, gen: %u, iov_len: %zu, size: %u, total: %u.\n",
__func__, t, t->gen, io->iov_len, size, t->total_size);
ptr = ERR_PTR(-E2BIG);
goto out;
}
ptr = io->iov_base + io->iov_len;
io->iov_len += size;
out:
dprintk("%s: t: %p, gen: %u, size: %u, total: %zu.\n",
__func__, t, t->gen, size, io->iov_len);
return ptr;
}
void netfs_trans_free(struct netfs_trans *t)
{
if (t->eng)
pohmelfs_crypto_thread_make_ready(t->eng->thread);
kfree(t);
}
struct netfs_trans *netfs_trans_alloc(struct pohmelfs_sb *psb, unsigned int size,
unsigned int flags, unsigned int nr)
{
struct netfs_trans *t;
unsigned int num, cont, pad, size_no_trans;
unsigned int crypto_added = 0;
struct netfs_cmd *cmd;
if (psb->perform_crypto)
crypto_added = psb->crypto_attached_size;
/*
* |sizeof(struct netfs_trans)|
* |sizeof(struct netfs_cmd)| - transaction header
* |size| - buffer with requested size
* |padding| - crypto padding, zero bytes
* |nr * sizeof(struct page *)| - array of page pointers
*
* Overall size should be less than PAGE_SIZE for guaranteed allocation.
*/
cont = size;
size = ALIGN(size, psb->crypto_align_size);
pad = size - cont;
size_no_trans = size + sizeof(struct netfs_cmd) * 2 + crypto_added;
cont = sizeof(struct netfs_trans) + size_no_trans;
num = (PAGE_SIZE - cont)/sizeof(struct page *);
if (nr > num)
nr = num;
t = kzalloc(cont + nr*sizeof(struct page *), GFP_NOIO);
if (!t)
goto err_out_exit;
t->iovec.iov_base = (void *)(t + 1);
t->pages = (struct page **)(t->iovec.iov_base + size_no_trans);
/*
* Reserving space for transaction header.
*/
t->iovec.iov_len = sizeof(struct netfs_cmd) + crypto_added;
netfs_trans_init_static(t, nr, size_no_trans);
t->flags = flags;
t->psb = psb;
cmd = (struct netfs_cmd *)t->iovec.iov_base;
cmd->size = size;
cmd->cpad = pad;
cmd->csize = crypto_added;
dprintk("%s: t: %p, gen: %u, size: %u, padding: %u, align_size: %u, flags: %x, "
"page_num: %u, base: %p, pages: %p.\n",
__func__, t, t->gen, size, pad, psb->crypto_align_size, flags, nr,
t->iovec.iov_base, t->pages);
return t;
err_out_exit:
return NULL;
}
int netfs_trans_init(void)
{
int err = -ENOMEM;
netfs_trans_dst = kmem_cache_create("netfs_trans_dst", sizeof(struct netfs_trans_dst),
0, 0, NULL);
if (!netfs_trans_dst)
goto err_out_exit;
netfs_trans_dst_pool = mempool_create_slab_pool(256, netfs_trans_dst);
if (!netfs_trans_dst_pool)
goto err_out_free;
return 0;
err_out_free:
kmem_cache_destroy(netfs_trans_dst);
err_out_exit:
return err;
}
void netfs_trans_exit(void)
{
mempool_destroy(netfs_trans_dst_pool);
kmem_cache_destroy(netfs_trans_dst);
}
|