linux/drivers/staging/pohmelfs/net.c
<<
>>
Prefs
   1/*
   2 * 2007+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
   3 * All rights reserved.
   4 *
   5 * This program is free software; you can redistribute it and/or modify
   6 * it under the terms of the GNU General Public License as published by
   7 * the Free Software Foundation; either version 2 of the License, or
   8 * (at your option) any later version.
   9 *
  10 * This program is distributed in the hope that it will be useful,
  11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13 * GNU General Public License for more details.
  14 */
  15
  16#include <linux/fsnotify.h>
  17#include <linux/jhash.h>
  18#include <linux/in.h>
  19#include <linux/in6.h>
  20#include <linux/kthread.h>
  21#include <linux/pagemap.h>
  22#include <linux/poll.h>
  23#include <linux/swap.h>
  24#include <linux/syscalls.h>
  25#include <linux/vmalloc.h>
  26
  27#include "netfs.h"
  28
  29/*
  30 * Async machinery lives here.
  31 * All commands being sent to server do _not_ require sync reply,
  32 * instead, if it is really needed, like readdir or readpage, caller
  33 * sleeps waiting for data, which will be placed into provided buffer
  34 * and caller will be awakened.
  35 *
  36 * Every command response can come without some listener. For example
  37 * readdir response will add new objects into cache without appropriate
  38 * request from userspace. This is used in cache coherency.
  39 *
  40 * If object is not found for given data, it is discarded.
  41 *
  42 * All requests are received by dedicated kernel thread.
  43 */
  44
  45/*
  46 * Basic network sending/receiving functions.
  47 * Blocked mode is used.
  48 */
  49static int netfs_data_recv(struct netfs_state *st, void *buf, u64 size)
  50{
  51        struct msghdr msg;
  52        struct kvec iov;
  53        int err;
  54
  55        BUG_ON(!size);
  56
  57        iov.iov_base = buf;
  58        iov.iov_len = size;
  59
  60        msg.msg_iov = (struct iovec *)&iov;
  61        msg.msg_iovlen = 1;
  62        msg.msg_name = NULL;
  63        msg.msg_namelen = 0;
  64        msg.msg_control = NULL;
  65        msg.msg_controllen = 0;
  66        msg.msg_flags = MSG_DONTWAIT;
  67
  68        err = kernel_recvmsg(st->socket, &msg, &iov, 1, iov.iov_len,
  69                        msg.msg_flags);
  70        if (err <= 0) {
  71                printk("%s: failed to recv data: size: %llu, err: %d.\n", __func__, size, err);
  72                if (err == 0)
  73                        err = -ECONNRESET;
  74        }
  75
  76        return err;
  77}
  78
  79static int pohmelfs_data_recv(struct netfs_state *st, void *data, unsigned int size)
  80{
  81        unsigned int revents = 0;
  82        unsigned int err_mask = POLLERR | POLLHUP | POLLRDHUP;
  83        unsigned int mask = err_mask | POLLIN;
  84        int err = 0;
  85
  86        while (size && !err) {
  87                revents = netfs_state_poll(st);
  88
  89                if (!(revents & mask)) {
  90                        DEFINE_WAIT(wait);
  91
  92                        for (;;) {
  93                                prepare_to_wait(&st->thread_wait, &wait, TASK_INTERRUPTIBLE);
  94                                if (kthread_should_stop())
  95                                        break;
  96
  97                                revents = netfs_state_poll(st);
  98
  99                                if (revents & mask)
 100                                        break;
 101
 102                                if (signal_pending(current))
 103                                        break;
 104
 105                                schedule();
 106                                continue;
 107                        }
 108                        finish_wait(&st->thread_wait, &wait);
 109                }
 110
 111                err = 0;
 112                netfs_state_lock(st);
 113                if (st->socket && (st->read_socket == st->socket) && (revents & POLLIN)) {
 114                        err = netfs_data_recv(st, data, size);
 115                        if (err > 0) {
 116                                data += err;
 117                                size -= err;
 118                                err = 0;
 119                        } else if (err == 0)
 120                                err = -ECONNRESET;
 121                }
 122
 123                if (revents & err_mask) {
 124                        printk("%s: revents: %x, socket: %p, size: %u, err: %d.\n",
 125                                        __func__, revents, st->socket, size, err);
 126                        err = -ECONNRESET;
 127                }
 128                netfs_state_unlock(st);
 129
 130                if (err < 0) {
 131                        if (netfs_state_trylock_send(st)) {
 132                                netfs_state_exit(st);
 133                                err = netfs_state_init(st);
 134                                if (!err)
 135                                        err = -EAGAIN;
 136                                netfs_state_unlock_send(st);
 137                        } else {
 138                                st->need_reset = 1;
 139                        }
 140                }
 141
 142                if (kthread_should_stop())
 143                        err = -ENODEV;
 144
 145                if (err)
 146                        printk("%s: socket: %p, read_socket: %p, revents: %x, rev_error: %d, "
 147                                        "should_stop: %d, size: %u, err: %d.\n",
 148                                __func__, st->socket, st->read_socket,
 149                                revents, revents & err_mask, kthread_should_stop(), size, err);
 150        }
 151
 152        return err;
 153}
 154
 155int pohmelfs_data_recv_and_check(struct netfs_state *st, void *data, unsigned int size)
 156{
 157        struct netfs_cmd *cmd = &st->cmd;
 158        int err;
 159
 160        err = pohmelfs_data_recv(st, data, size);
 161        if (err)
 162                return err;
 163
 164        return pohmelfs_crypto_process_input_data(&st->eng, cmd->iv, data, NULL, size);
 165}
 166
 167/*
 168 * Polling machinery.
 169 */
 170
 171struct netfs_poll_helper {
 172        poll_table              pt;
 173        struct netfs_state      *st;
 174};
 175
 176static int netfs_queue_wake(wait_queue_t *wait, unsigned mode, int sync, void *key)
 177{
 178        struct netfs_state *st = container_of(wait, struct netfs_state, wait);
 179
 180        wake_up(&st->thread_wait);
 181        return 1;
 182}
 183
 184static void netfs_queue_func(struct file *file, wait_queue_head_t *whead,
 185                                 poll_table *pt)
 186{
 187        struct netfs_state *st = container_of(pt, struct netfs_poll_helper, pt)->st;
 188
 189        st->whead = whead;
 190        init_waitqueue_func_entry(&st->wait, netfs_queue_wake);
 191        add_wait_queue(whead, &st->wait);
 192}
 193
 194static void netfs_poll_exit(struct netfs_state *st)
 195{
 196        if (st->whead) {
 197                remove_wait_queue(st->whead, &st->wait);
 198                st->whead = NULL;
 199        }
 200}
 201
 202static int netfs_poll_init(struct netfs_state *st)
 203{
 204        struct netfs_poll_helper ph;
 205
 206        ph.st = st;
 207        init_poll_funcptr(&ph.pt, &netfs_queue_func);
 208
 209        st->socket->ops->poll(NULL, st->socket, &ph.pt);
 210        return 0;
 211}
 212
 213/*
 214 * Get response for readpage command. We search inode and page in its mapping
 215 * and copy data into. If it was async request, then we queue page into shared
 216 * data and wakeup listener, who will copy it to userspace.
 217 *
 218 * There is a work in progress of allowing to call copy_to_user() directly from
 219 * async receiving kernel thread.
 220 */
 221static int pohmelfs_read_page_response(struct netfs_state *st)
 222{
 223        struct pohmelfs_sb *psb = st->psb;
 224        struct netfs_cmd *cmd = &st->cmd;
 225        struct inode *inode;
 226        struct page *page;
 227        int err = 0;
 228
 229        if (cmd->size > PAGE_CACHE_SIZE) {
 230                err = -EINVAL;
 231                goto err_out_exit;
 232        }
 233
 234        inode = ilookup(st->psb->sb, cmd->id);
 235        if (!inode) {
 236                printk("%s: failed to find inode: id: %llu.\n", __func__, cmd->id);
 237                err = -ENOENT;
 238                goto err_out_exit;
 239        }
 240
 241        page = find_get_page(inode->i_mapping, cmd->start >> PAGE_CACHE_SHIFT);
 242        if (!page || !PageLocked(page)) {
 243                printk("%s: failed to find/lock page: page: %p, id: %llu, start: %llu, index: %llu.\n",
 244                                __func__, page, cmd->id, cmd->start, cmd->start >> PAGE_CACHE_SHIFT);
 245
 246                while (cmd->size) {
 247                        unsigned int sz = min(cmd->size, st->size);
 248
 249                        err = pohmelfs_data_recv(st, st->data, sz);
 250                        if (err)
 251                                break;
 252
 253                        cmd->size -= sz;
 254                }
 255
 256                err = -ENODEV;
 257                if (page)
 258                        goto err_out_page_put;
 259                goto err_out_put;
 260        }
 261
 262        if (cmd->size) {
 263                void *addr;
 264
 265                addr = kmap(page);
 266                err = pohmelfs_data_recv(st, addr, cmd->size);
 267                kunmap(page);
 268
 269                if (err)
 270                        goto err_out_page_unlock;
 271        }
 272
 273        dprintk("%s: page: %p, start: %llu, size: %u, locked: %d.\n",
 274                __func__, page, cmd->start, cmd->size, PageLocked(page));
 275
 276        SetPageChecked(page);
 277        if ((psb->hash_string || psb->cipher_string) && psb->perform_crypto && cmd->size) {
 278                err = pohmelfs_crypto_process_input_page(&st->eng, page, cmd->size, cmd->iv);
 279                if (err < 0)
 280                        goto err_out_page_unlock;
 281        } else {
 282                SetPageUptodate(page);
 283                unlock_page(page);
 284                page_cache_release(page);
 285        }
 286
 287        pohmelfs_put_inode(POHMELFS_I(inode));
 288        wake_up(&st->psb->wait);
 289
 290        return 0;
 291
 292err_out_page_unlock:
 293        SetPageError(page);
 294        unlock_page(page);
 295err_out_page_put:
 296        page_cache_release(page);
 297err_out_put:
 298        pohmelfs_put_inode(POHMELFS_I(inode));
 299err_out_exit:
 300        wake_up(&st->psb->wait);
 301        return err;
 302}
 303
 304static int pohmelfs_check_name(struct pohmelfs_inode *parent, struct qstr *str,
 305                struct netfs_inode_info *info)
 306{
 307        struct inode *inode;
 308        struct pohmelfs_name *n;
 309        int err = 0;
 310        u64 ino = 0;
 311
 312        mutex_lock(&parent->offset_lock);
 313        n = pohmelfs_search_hash(parent, str->hash);
 314        if (n)
 315                ino = n->ino;
 316        mutex_unlock(&parent->offset_lock);
 317
 318        if (!ino)
 319                goto out;
 320
 321        inode = ilookup(parent->vfs_inode.i_sb, ino);
 322        if (!inode)
 323                goto out;
 324
 325        dprintk("%s: parent: %llu, inode: %llu.\n", __func__, parent->ino, ino);
 326
 327        pohmelfs_fill_inode(inode, info);
 328        pohmelfs_put_inode(POHMELFS_I(inode));
 329        err = -EEXIST;
 330out:
 331        return err;
 332}
 333
 334/*
 335 * Readdir response from server. If special field is set, we wakeup
 336 * listener (readdir() call), which will copy data to userspace.
 337 */
 338static int pohmelfs_readdir_response(struct netfs_state *st)
 339{
 340        struct inode *inode;
 341        struct netfs_cmd *cmd = &st->cmd;
 342        struct netfs_inode_info *info;
 343        struct pohmelfs_inode *parent = NULL, *npi;
 344        int err = 0, last = cmd->ext;
 345        struct qstr str;
 346
 347        if (cmd->size > st->size)
 348                return -EINVAL;
 349
 350        inode = ilookup(st->psb->sb, cmd->id);
 351        if (!inode) {
 352                printk("%s: failed to find inode: id: %llu.\n", __func__, cmd->id);
 353                return -ENOENT;
 354        }
 355        parent = POHMELFS_I(inode);
 356
 357        if (!cmd->size && cmd->start) {
 358                err = -cmd->start;
 359                goto out;
 360        }
 361
 362        if (cmd->size) {
 363                char *name;
 364
 365                err = pohmelfs_data_recv_and_check(st, st->data, cmd->size);
 366                if (err)
 367                        goto err_out_put;
 368
 369                info = (struct netfs_inode_info *)(st->data);
 370
 371                name = (char *)(info + 1);
 372                str.len = cmd->size - sizeof(struct netfs_inode_info) - 1 - cmd->cpad;
 373                name[str.len] = 0;
 374                str.name = name;
 375                str.hash = jhash(str.name, str.len, 0);
 376
 377                netfs_convert_inode_info(info);
 378
 379                if (parent) {
 380                        err = pohmelfs_check_name(parent, &str, info);
 381                        if (err) {
 382                                if (err == -EEXIST)
 383                                        err = 0;
 384                                goto out;
 385                        }
 386                }
 387
 388                info->ino = cmd->start;
 389                if (!info->ino)
 390                        info->ino = pohmelfs_new_ino(st->psb);
 391
 392                dprintk("%s: parent: %llu, ino: %llu, name: '%s', hash: %x, len: %u, mode: %o.\n",
 393                                __func__, parent->ino, info->ino, str.name, str.hash, str.len,
 394                                info->mode);
 395
 396                npi = pohmelfs_new_inode(st->psb, parent, &str, info, 0);
 397                if (IS_ERR(npi)) {
 398                        err = PTR_ERR(npi);
 399
 400                        if (err != -EEXIST)
 401                                goto err_out_put;
 402                } else {
 403                        struct dentry *dentry, *alias, *pd;
 404
 405                        set_bit(NETFS_INODE_REMOTE_SYNCED, &npi->state);
 406                        clear_bit(NETFS_INODE_OWNED, &npi->state);
 407
 408                        pd = d_find_alias(&parent->vfs_inode);
 409                        if (pd) {
 410                                str.hash = full_name_hash(str.name, str.len);
 411                                dentry = d_alloc(pd, &str);
 412                                if (dentry) {
 413                                        alias = d_materialise_unique(dentry, &npi->vfs_inode);
 414                                        if (alias)
 415                                                dput(dentry);
 416                                }
 417
 418                                dput(dentry);
 419                                dput(pd);
 420                        }
 421                }
 422        }
 423out:
 424        if (last) {
 425                set_bit(NETFS_INODE_REMOTE_DIR_SYNCED, &parent->state);
 426                set_bit(NETFS_INODE_REMOTE_SYNCED, &parent->state);
 427                wake_up(&st->psb->wait);
 428        }
 429        pohmelfs_put_inode(parent);
 430
 431        return err;
 432
 433err_out_put:
 434        clear_bit(NETFS_INODE_REMOTE_DIR_SYNCED, &parent->state);
 435        printk("%s: parent: %llu, ino: %llu, cmd_id: %llu.\n", __func__, parent->ino, cmd->start, cmd->id);
 436        pohmelfs_put_inode(parent);
 437        wake_up(&st->psb->wait);
 438        return err;
 439}
 440
 441/*
 442 * Lookup command response.
 443 * It searches for inode to be looked at (if it exists) and substitutes
 444 * its inode information (size, permission, mode and so on), if inode does
 445 * not exist, new one will be created and inserted into caches.
 446 */
 447static int pohmelfs_lookup_response(struct netfs_state *st)
 448{
 449        struct inode *inode = NULL;
 450        struct netfs_cmd *cmd = &st->cmd;
 451        struct netfs_inode_info *info;
 452        struct pohmelfs_inode *parent = NULL, *npi;
 453        int err = -EINVAL;
 454        char *name;
 455
 456        inode = ilookup(st->psb->sb, cmd->id);
 457        if (!inode) {
 458                printk("%s: lookup response: id: %llu, start: %llu, size: %u.\n",
 459                                __func__, cmd->id, cmd->start, cmd->size);
 460                err = -ENOENT;
 461                goto err_out_exit;
 462        }
 463        parent = POHMELFS_I(inode);
 464
 465        if (!cmd->size) {
 466                err = -cmd->start;
 467                goto err_out_put;
 468        }
 469
 470        if (cmd->size < sizeof(struct netfs_inode_info)) {
 471                printk("%s: broken lookup response: id: %llu, start: %llu, size: %u.\n",
 472                                __func__, cmd->id, cmd->start, cmd->size);
 473                err = -EINVAL;
 474                goto err_out_put;
 475        }
 476
 477        err = pohmelfs_data_recv_and_check(st, st->data, cmd->size);
 478        if (err)
 479                goto err_out_put;
 480
 481        info = (struct netfs_inode_info *)(st->data);
 482        name = (char *)(info + 1);
 483
 484        netfs_convert_inode_info(info);
 485
 486        info->ino = cmd->start;
 487        if (!info->ino)
 488                info->ino = pohmelfs_new_ino(st->psb);
 489
 490        dprintk("%s: parent: %llu, ino: %llu, name: '%s', start: %llu.\n",
 491                        __func__, parent->ino, info->ino, name, cmd->start);
 492
 493        if (cmd->start)
 494                npi = pohmelfs_new_inode(st->psb, parent, NULL, info, 0);
 495        else {
 496                struct qstr str;
 497
 498                str.name = name;
 499                str.len = cmd->size - sizeof(struct netfs_inode_info) - 1 - cmd->cpad;
 500                str.hash = jhash(name, str.len, 0);
 501
 502                npi = pohmelfs_new_inode(st->psb, parent, &str, info, 0);
 503        }
 504        if (IS_ERR(npi)) {
 505                err = PTR_ERR(npi);
 506
 507                if (err != -EEXIST)
 508                        goto err_out_put;
 509        } else {
 510                set_bit(NETFS_INODE_REMOTE_SYNCED, &npi->state);
 511                clear_bit(NETFS_INODE_OWNED, &npi->state);
 512        }
 513
 514        clear_bit(NETFS_COMMAND_PENDING, &parent->state);
 515        pohmelfs_put_inode(parent);
 516
 517        wake_up(&st->psb->wait);
 518
 519        return 0;
 520
 521err_out_put:
 522        pohmelfs_put_inode(parent);
 523err_out_exit:
 524        clear_bit(NETFS_COMMAND_PENDING, &parent->state);
 525        wake_up(&st->psb->wait);
 526        printk("%s: inode: %p, id: %llu, start: %llu, size: %u, err: %d.\n",
 527                        __func__, inode, cmd->id, cmd->start, cmd->size, err);
 528        return err;
 529}
 530
 531/*
 532 * Create response, just marks local inode as 'created', so that writeback
 533 * for any of its children (or own) would not try to sync it again.
 534 */
 535static int pohmelfs_create_response(struct netfs_state *st)
 536{
 537        struct inode *inode;
 538        struct netfs_cmd *cmd = &st->cmd;
 539        struct pohmelfs_inode *pi;
 540
 541        inode = ilookup(st->psb->sb, cmd->id);
 542        if (!inode) {
 543                printk("%s: failed to find inode: id: %llu, start: %llu.\n",
 544                                __func__, cmd->id, cmd->start);
 545                goto err_out_exit;
 546        }
 547
 548        pi = POHMELFS_I(inode);
 549
 550        /*
 551         * To lock or not to lock?
 552         * We actually do not care if it races...
 553         */
 554        if (cmd->start)
 555                make_bad_inode(inode);
 556        set_bit(NETFS_INODE_REMOTE_SYNCED, &pi->state);
 557
 558        pohmelfs_put_inode(pi);
 559
 560        wake_up(&st->psb->wait);
 561        return 0;
 562
 563err_out_exit:
 564        wake_up(&st->psb->wait);
 565        return -ENOENT;
 566}
 567
 568/*
 569 * Object remove response. Just says that remove request has been received.
 570 * Used in cache coherency protocol.
 571 */
 572static int pohmelfs_remove_response(struct netfs_state *st)
 573{
 574        struct netfs_cmd *cmd = &st->cmd;
 575        int err;
 576
 577        err = pohmelfs_data_recv_and_check(st, st->data, cmd->size);
 578        if (err)
 579                return err;
 580
 581        dprintk("%s: parent: %llu, path: '%s'.\n", __func__, cmd->id, (char *)st->data);
 582
 583        return 0;
 584}
 585
 586/*
 587 * Transaction reply processing.
 588 *
 589 * Find transaction based on its generation number, bump its reference counter,
 590 * so that none could free it under us, drop from the trees and lists and
 591 * drop reference counter. When it hits zero (when all destinations replied
 592 * and all timeout handled by async scanning code), completion will be called
 593 * and transaction will be freed.
 594 */
 595static int pohmelfs_transaction_response(struct netfs_state *st)
 596{
 597        struct netfs_trans_dst *dst;
 598        struct netfs_trans *t = NULL;
 599        struct netfs_cmd *cmd = &st->cmd;
 600        short err = (signed)cmd->ext;
 601
 602        mutex_lock(&st->trans_lock);
 603        dst = netfs_trans_search(st, cmd->start);
 604        if (dst) {
 605                netfs_trans_remove_nolock(dst, st);
 606                t = dst->trans;
 607        }
 608        mutex_unlock(&st->trans_lock);
 609
 610        if (!t) {
 611                printk("%s: failed to find transaction: start: %llu: id: %llu, size: %u, ext: %u.\n",
 612                                __func__, cmd->start, cmd->id, cmd->size, cmd->ext);
 613                err = -EINVAL;
 614                goto out;
 615        }
 616
 617        t->result = err;
 618        netfs_trans_drop_dst_nostate(dst);
 619
 620out:
 621        wake_up(&st->psb->wait);
 622        return err;
 623}
 624
 625/*
 626 * Inode metadata cache coherency message.
 627 */
 628static int pohmelfs_page_cache_response(struct netfs_state *st)
 629{
 630        struct netfs_cmd *cmd = &st->cmd;
 631        struct inode *inode;
 632
 633        dprintk("%s: st: %p, id: %llu, start: %llu, size: %u.\n", __func__, st, cmd->id, cmd->start, cmd->size);
 634
 635        inode = ilookup(st->psb->sb, cmd->id);
 636        if (!inode) {
 637                printk("%s: failed to find inode: id: %llu.\n", __func__, cmd->id);
 638                return -ENOENT;
 639        }
 640
 641        set_bit(NETFS_INODE_NEED_FLUSH, &POHMELFS_I(inode)->state);
 642        pohmelfs_put_inode(POHMELFS_I(inode));
 643
 644        return 0;
 645}
 646
 647/*
 648 * Root capabilities response: export statistics
 649 * like used and available size, number of files and dirs,
 650 * permissions.
 651 */
 652static int pohmelfs_root_cap_response(struct netfs_state *st)
 653{
 654        struct netfs_cmd *cmd = &st->cmd;
 655        struct netfs_root_capabilities *cap;
 656        struct pohmelfs_sb *psb = st->psb;
 657
 658        if (cmd->size != sizeof(struct netfs_root_capabilities)) {
 659                psb->flags = EPROTO;
 660                wake_up(&psb->wait);
 661                return -EPROTO;
 662        }
 663
 664        cap = st->data;
 665
 666        netfs_convert_root_capabilities(cap);
 667
 668        if (psb->total_size < cap->used + cap->avail)
 669                psb->total_size = cap->used + cap->avail;
 670        if (cap->avail)
 671                psb->avail_size = cap->avail;
 672        psb->state_flags = cap->flags;
 673
 674        if (psb->state_flags & POHMELFS_FLAGS_RO) {
 675                psb->sb->s_flags |= MS_RDONLY;
 676                printk(KERN_INFO "Mounting POHMELFS (%d) read-only.\n", psb->idx);
 677        }
 678
 679        if (psb->state_flags & POHMELFS_FLAGS_XATTR)
 680                printk(KERN_INFO "Mounting POHMELFS (%d) "
 681                        "with extended attributes support.\n", psb->idx);
 682
 683        if (atomic_long_read(&psb->total_inodes) <= 1)
 684                atomic_long_set(&psb->total_inodes, cap->nr_files);
 685
 686        dprintk("%s: total: %llu, avail: %llu, flags: %llx, inodes: %llu.\n",
 687                __func__, psb->total_size, psb->avail_size, psb->state_flags, cap->nr_files);
 688
 689        psb->flags = 0;
 690        wake_up(&psb->wait);
 691        return 0;
 692}
 693
 694/*
 695 * Crypto capabilities of the server, where it says that
 696 * it supports or does not requested hash/cipher algorithms.
 697 */
 698static int pohmelfs_crypto_cap_response(struct netfs_state *st)
 699{
 700        struct netfs_cmd *cmd = &st->cmd;
 701        struct netfs_crypto_capabilities *cap;
 702        struct pohmelfs_sb *psb = st->psb;
 703        int err = 0;
 704
 705        if (cmd->size != sizeof(struct netfs_crypto_capabilities)) {
 706                psb->flags = EPROTO;
 707                wake_up(&psb->wait);
 708                return -EPROTO;
 709        }
 710
 711        cap = st->data;
 712
 713        dprintk("%s: cipher '%s': %s, hash: '%s': %s.\n",
 714                        __func__,
 715                        psb->cipher_string, (cap->cipher_strlen)?"SUPPORTED":"NOT SUPPORTED",
 716                        psb->hash_string, (cap->hash_strlen)?"SUPPORTED":"NOT SUPPORTED");
 717
 718        if (!cap->hash_strlen) {
 719                if (psb->hash_strlen && psb->crypto_fail_unsupported)
 720                        err = -ENOTSUPP;
 721                psb->hash_strlen = 0;
 722                kfree(psb->hash_string);
 723                psb->hash_string = NULL;
 724        }
 725
 726        if (!cap->cipher_strlen) {
 727                if (psb->cipher_strlen && psb->crypto_fail_unsupported)
 728                        err = -ENOTSUPP;
 729                psb->cipher_strlen = 0;
 730                kfree(psb->cipher_string);
 731                psb->cipher_string = NULL;
 732        }
 733
 734        return err;
 735}
 736
 737/*
 738 * Capabilities handshake response.
 739 */
 740static int pohmelfs_capabilities_response(struct netfs_state *st)
 741{
 742        struct netfs_cmd *cmd = &st->cmd;
 743        int err = 0;
 744
 745        err = pohmelfs_data_recv(st, st->data, cmd->size);
 746        if (err)
 747                return err;
 748
 749        switch (cmd->id) {
 750                case POHMELFS_CRYPTO_CAPABILITIES:
 751                        return pohmelfs_crypto_cap_response(st);
 752                case POHMELFS_ROOT_CAPABILITIES:
 753                        return pohmelfs_root_cap_response(st);
 754                default:
 755                        break;
 756        }
 757        return -EINVAL;
 758}
 759
 760/*
 761 * Receiving extended attribute.
 762 * Does not work properly if received size is more than requested one,
 763 * it should not happen with current request/reply model though.
 764 */
 765static int pohmelfs_getxattr_response(struct netfs_state *st)
 766{
 767        struct pohmelfs_sb *psb = st->psb;
 768        struct netfs_cmd *cmd = &st->cmd;
 769        struct pohmelfs_mcache *m;
 770        short error = (signed short)cmd->ext, err;
 771        unsigned int sz, total_size;
 772
 773        m = pohmelfs_mcache_search(psb, cmd->id);
 774
 775        dprintk("%s: id: %llu, gen: %llu, err: %d.\n",
 776                __func__, cmd->id, (m)?m->gen:0, error);
 777
 778        if (!m) {
 779                printk("%s: failed to find getxattr cache entry: id: %llu.\n", __func__, cmd->id);
 780                return -ENOENT;
 781        }
 782
 783        if (cmd->size) {
 784                sz = min_t(unsigned int, cmd->size, m->size);
 785                err = pohmelfs_data_recv_and_check(st, m->data, sz);
 786                if (err) {
 787                        error = err;
 788                        goto out;
 789                }
 790
 791                m->size = sz;
 792                total_size = cmd->size - sz;
 793
 794                while (total_size) {
 795                        sz = min(total_size, st->size);
 796
 797                        err = pohmelfs_data_recv_and_check(st, st->data, sz);
 798                        if (err) {
 799                                error = err;
 800                                break;
 801                        }
 802
 803                        total_size -= sz;
 804                }
 805        }
 806
 807out:
 808        m->err = error;
 809        complete(&m->complete);
 810        pohmelfs_mcache_put(psb, m);
 811
 812        return error;
 813}
 814
 815int pohmelfs_data_lock_response(struct netfs_state *st)
 816{
 817        struct pohmelfs_sb *psb = st->psb;
 818        struct netfs_cmd *cmd = &st->cmd;
 819        struct pohmelfs_mcache *m;
 820        short err = (signed short)cmd->ext;
 821        u64 id = cmd->id;
 822
 823        m = pohmelfs_mcache_search(psb, id);
 824
 825        dprintk("%s: id: %llu, gen: %llu, err: %d.\n",
 826                __func__, cmd->id, (m)?m->gen:0, err);
 827
 828        if (!m) {
 829                pohmelfs_data_recv(st, st->data, cmd->size);
 830                printk("%s: failed to find data lock response: id: %llu.\n", __func__, cmd->id);
 831                return -ENOENT;
 832        }
 833
 834        if (cmd->size)
 835                err = pohmelfs_data_recv_and_check(st, &m->info, cmd->size);
 836
 837        m->err = err;
 838        complete(&m->complete);
 839        pohmelfs_mcache_put(psb, m);
 840
 841        return err;
 842}
 843
 844static void __inline__ netfs_state_reset(struct netfs_state *st)
 845{
 846        netfs_state_lock_send(st);
 847        netfs_state_exit(st);
 848        netfs_state_init(st);
 849        netfs_state_unlock_send(st);
 850}
 851
 852/*
 853 * Main receiving function, called from dedicated kernel thread.
 854 */
 855static int pohmelfs_recv(void *data)
 856{
 857        int err = -EINTR;
 858        struct netfs_state *st = data;
 859        struct netfs_cmd *cmd = &st->cmd;
 860
 861        while (!kthread_should_stop()) {
 862                /*
 863                 * If socket will be reset after this statement, then
 864                 * pohmelfs_data_recv() will just fail and loop will
 865                 * start again, so it can be done without any locks.
 866                 *
 867                 * st->read_socket is needed to prevents state machine
 868                 * breaking between this data reading and subsequent one
 869                 * in protocol specific functions during connection reset.
 870                 * In case of reset we have to read next command and do
 871                 * not expect data for old command to magically appear in
 872                 * new connection.
 873                 */
 874                st->read_socket = st->socket;
 875                err = pohmelfs_data_recv(st, cmd, sizeof(struct netfs_cmd));
 876                if (err) {
 877                        msleep(1000);
 878                        continue;
 879                }
 880
 881                netfs_convert_cmd(cmd);
 882
 883                dprintk("%s: cmd: %u, id: %llu, start: %llu, size: %u, "
 884                                "ext: %u, csize: %u, cpad: %u.\n",
 885                                __func__, cmd->cmd, cmd->id, cmd->start,
 886                                cmd->size, cmd->ext, cmd->csize, cmd->cpad);
 887
 888                if (cmd->csize) {
 889                        struct pohmelfs_crypto_engine *e = &st->eng;
 890
 891                        if (unlikely(cmd->csize > e->size/2)) {
 892                                netfs_state_reset(st);
 893                                continue;
 894                        }
 895
 896                        if (e->hash && unlikely(cmd->csize != st->psb->crypto_attached_size)) {
 897                                dprintk("%s: cmd: cmd: %u, id: %llu, start: %llu, size: %u, "
 898                                                "csize: %u != digest size %u.\n",
 899                                                __func__, cmd->cmd, cmd->id, cmd->start, cmd->size,
 900                                                cmd->csize, st->psb->crypto_attached_size);
 901                                netfs_state_reset(st);
 902                                continue;
 903                        }
 904
 905                        err = pohmelfs_data_recv(st, e->data, cmd->csize);
 906                        if (err) {
 907                                netfs_state_reset(st);
 908                                continue;
 909                        }
 910
 911#ifdef CONFIG_POHMELFS_DEBUG
 912                        {
 913                                unsigned int i;
 914                                unsigned char *hash = e->data;
 915
 916                                dprintk("%s: received hash: ", __func__);
 917                                for (i=0; i<cmd->csize; ++i)
 918                                        printk("%02x ", hash[i]);
 919
 920                                printk("\n");
 921                        }
 922#endif
 923                        cmd->size -= cmd->csize;
 924                }
 925
 926                /*
 927                 * This should catch protocol breakage and random garbage instead of commands.
 928                 */
 929                if (unlikely((cmd->size > st->size) && (cmd->cmd != NETFS_XATTR_GET))) {
 930                        netfs_state_reset(st);
 931                        continue;
 932                }
 933
 934                switch (cmd->cmd) {
 935                        case NETFS_READ_PAGE:
 936                                err = pohmelfs_read_page_response(st);
 937                                break;
 938                        case NETFS_READDIR:
 939                                err = pohmelfs_readdir_response(st);
 940                                break;
 941                        case NETFS_LOOKUP:
 942                                err = pohmelfs_lookup_response(st);
 943                                break;
 944                        case NETFS_CREATE:
 945                                err = pohmelfs_create_response(st);
 946                                break;
 947                        case NETFS_REMOVE:
 948                                err = pohmelfs_remove_response(st);
 949                                break;
 950                        case NETFS_TRANS:
 951                                err = pohmelfs_transaction_response(st);
 952                                break;
 953                        case NETFS_PAGE_CACHE:
 954                                err = pohmelfs_page_cache_response(st);
 955                                break;
 956                        case NETFS_CAPABILITIES:
 957                                err = pohmelfs_capabilities_response(st);
 958                                break;
 959                        case NETFS_LOCK:
 960                                err = pohmelfs_data_lock_response(st);
 961                                break;
 962                        case NETFS_XATTR_GET:
 963                                err = pohmelfs_getxattr_response(st);
 964                                break;
 965                        default:
 966                                printk("%s: wrong cmd: %u, id: %llu, start: %llu, size: %u, ext: %u.\n",
 967                                        __func__, cmd->cmd, cmd->id, cmd->start, cmd->size, cmd->ext);
 968                                netfs_state_reset(st);
 969                                break;
 970                }
 971        }
 972
 973        while (!kthread_should_stop())
 974                schedule_timeout_uninterruptible(msecs_to_jiffies(10));
 975
 976        return err;
 977}
 978
 979int netfs_state_init(struct netfs_state *st)
 980{
 981        int err;
 982        struct pohmelfs_ctl *ctl = &st->ctl;
 983
 984        err = sock_create(ctl->addr.sa_family, ctl->type, ctl->proto, &st->socket);
 985        if (err) {
 986                printk("%s: failed to create a socket: family: %d, type: %d, proto: %d, err: %d.\n",
 987                                __func__, ctl->addr.sa_family, ctl->type, ctl->proto, err);
 988                goto err_out_exit;
 989        }
 990
 991        st->socket->sk->sk_allocation = GFP_NOIO;
 992        st->socket->sk->sk_sndtimeo = st->socket->sk->sk_rcvtimeo = msecs_to_jiffies(60000);
 993
 994        err = kernel_connect(st->socket, (struct sockaddr *)&ctl->addr, ctl->addrlen, 0);
 995        if (err) {
 996                printk("%s: failed to connect to server: idx: %u, err: %d.\n",
 997                                __func__, st->psb->idx, err);
 998                goto err_out_release;
 999        }
1000        st->socket->sk->sk_sndtimeo = st->socket->sk->sk_rcvtimeo = msecs_to_jiffies(60000);
1001
1002        err = netfs_poll_init(st);
1003        if (err)
1004                goto err_out_release;
1005
1006        if (st->socket->ops->family == AF_INET) {
1007                struct sockaddr_in *sin = (struct sockaddr_in *)&ctl->addr;
1008                printk(KERN_INFO "%s: (re)connected to peer %pi4:%d.\n", __func__,
1009                        &sin->sin_addr.s_addr, ntohs(sin->sin_port));
1010        } else if (st->socket->ops->family == AF_INET6) {
1011                struct sockaddr_in6 *sin = (struct sockaddr_in6 *)&ctl->addr;
1012                printk(KERN_INFO "%s: (re)connected to peer %pi6:%d", __func__,
1013                                &sin->sin6_addr, ntohs(sin->sin6_port));
1014        }
1015
1016        return 0;
1017
1018err_out_release:
1019        sock_release(st->socket);
1020err_out_exit:
1021        st->socket = NULL;
1022        return err;
1023}
1024
1025void netfs_state_exit(struct netfs_state *st)
1026{
1027        if (st->socket) {
1028                netfs_poll_exit(st);
1029                st->socket->ops->shutdown(st->socket, 2);
1030
1031                if (st->socket->ops->family == AF_INET) {
1032                        struct sockaddr_in *sin = (struct sockaddr_in *)&st->ctl.addr;
1033                        printk(KERN_INFO "%s: disconnected from peer %pi4:%d.\n", __func__,
1034                                &sin->sin_addr.s_addr, ntohs(sin->sin_port));
1035                } else if (st->socket->ops->family == AF_INET6) {
1036                        struct sockaddr_in6 *sin = (struct sockaddr_in6 *)&st->ctl.addr;
1037                        printk(KERN_INFO "%s: disconnected from peer %pi6:%d", __func__,
1038                                &sin->sin6_addr, ntohs(sin->sin6_port));
1039                }
1040
1041                sock_release(st->socket);
1042                st->socket = NULL;
1043                st->read_socket = NULL;
1044                st->need_reset = 0;
1045        }
1046}
1047
1048int pohmelfs_state_init_one(struct pohmelfs_sb *psb, struct pohmelfs_config *conf)
1049{
1050        struct netfs_state *st = &conf->state;
1051        int err = -ENOMEM;
1052
1053        mutex_init(&st->__state_lock);
1054        mutex_init(&st->__state_send_lock);
1055        init_waitqueue_head(&st->thread_wait);
1056
1057        st->psb = psb;
1058        st->trans_root = RB_ROOT;
1059        mutex_init(&st->trans_lock);
1060
1061        st->size = psb->trans_data_size;
1062        st->data = kmalloc(st->size, GFP_KERNEL);
1063        if (!st->data)
1064                goto err_out_exit;
1065
1066        if (psb->perform_crypto) {
1067                err = pohmelfs_crypto_engine_init(&st->eng, psb);
1068                if (err)
1069                        goto err_out_free_data;
1070        }
1071
1072        err = netfs_state_init(st);
1073        if (err)
1074                goto err_out_free_engine;
1075
1076        st->thread = kthread_run(pohmelfs_recv, st, "pohmelfs/%u", psb->idx);
1077        if (IS_ERR(st->thread)) {
1078                err = PTR_ERR(st->thread);
1079                goto err_out_netfs_exit;
1080        }
1081
1082        if (!psb->active_state)
1083                psb->active_state = conf;
1084
1085        dprintk("%s: conf: %p, st: %p, socket: %p.\n",
1086                        __func__, conf, st, st->socket);
1087        return 0;
1088
1089err_out_netfs_exit:
1090        netfs_state_exit(st);
1091err_out_free_engine:
1092        pohmelfs_crypto_engine_exit(&st->eng);
1093err_out_free_data:
1094        kfree(st->data);
1095err_out_exit:
1096        return err;
1097
1098}
1099
1100void pohmelfs_state_flush_transactions(struct netfs_state *st)
1101{
1102        struct rb_node *rb_node;
1103        struct netfs_trans_dst *dst;
1104
1105        mutex_lock(&st->trans_lock);
1106        for (rb_node = rb_first(&st->trans_root); rb_node; ) {
1107                dst = rb_entry(rb_node, struct netfs_trans_dst, state_entry);
1108                rb_node = rb_next(rb_node);
1109
1110                dst->trans->result = -EINVAL;
1111                netfs_trans_remove_nolock(dst, st);
1112                netfs_trans_drop_dst_nostate(dst);
1113        }
1114        mutex_unlock(&st->trans_lock);
1115}
1116
1117static void pohmelfs_state_exit_one(struct pohmelfs_config *c)
1118{
1119        struct netfs_state *st = &c->state;
1120
1121        dprintk("%s: exiting, st: %p.\n", __func__, st);
1122        if (st->thread) {
1123                kthread_stop(st->thread);
1124                st->thread = NULL;
1125        }
1126
1127        netfs_state_lock_send(st);
1128        netfs_state_exit(st);
1129        netfs_state_unlock_send(st);
1130
1131        pohmelfs_state_flush_transactions(st);
1132
1133        pohmelfs_crypto_engine_exit(&st->eng);
1134        kfree(st->data);
1135
1136        kfree(c);
1137}
1138
1139/*
1140 * Initialize network stack. It searches for given ID in global
1141 * configuration table, this contains information of the remote server
1142 * (address (any supported by socket interface) and port, protocol and so on).
1143 */
1144int pohmelfs_state_init(struct pohmelfs_sb *psb)
1145{
1146        int err = -ENOMEM;
1147
1148        err = pohmelfs_copy_config(psb);
1149        if (err) {
1150                pohmelfs_state_exit(psb);
1151                return err;
1152        }
1153
1154        return 0;
1155}
1156
1157void pohmelfs_state_exit(struct pohmelfs_sb *psb)
1158{
1159        struct pohmelfs_config *c, *tmp;
1160
1161        list_for_each_entry_safe(c, tmp, &psb->state_list, config_entry) {
1162                list_del(&c->config_entry);
1163                pohmelfs_state_exit_one(c);
1164        }
1165}
1166
1167void pohmelfs_switch_active(struct pohmelfs_sb *psb)
1168{
1169        struct pohmelfs_config *c = psb->active_state;
1170
1171        if (!list_empty(&psb->state_list)) {
1172                if (c->config_entry.next != &psb->state_list) {
1173                        psb->active_state = list_entry(c->config_entry.next,
1174                                struct pohmelfs_config, config_entry);
1175                } else {
1176                        psb->active_state = list_entry(psb->state_list.next,
1177                                struct pohmelfs_config, config_entry);
1178                }
1179
1180                dprintk("%s: empty: %d, active %p -> %p.\n",
1181                        __func__, list_empty(&psb->state_list), c,
1182                        psb->active_state);
1183        } else
1184                psb->active_state = NULL;
1185}
1186
1187void pohmelfs_check_states(struct pohmelfs_sb *psb)
1188{
1189        struct pohmelfs_config *c, *tmp;
1190        LIST_HEAD(delete_list);
1191
1192        mutex_lock(&psb->state_lock);
1193        list_for_each_entry_safe(c, tmp, &psb->state_list, config_entry) {
1194                if (pohmelfs_config_check(c, psb->idx)) {
1195
1196                        if (psb->active_state == c)
1197                                pohmelfs_switch_active(psb);
1198                        list_move(&c->config_entry, &delete_list);
1199                }
1200        }
1201        pohmelfs_copy_config(psb);
1202        mutex_unlock(&psb->state_lock);
1203
1204        list_for_each_entry_safe(c, tmp, &delete_list, config_entry) {
1205                list_del(&c->config_entry);
1206                pohmelfs_state_exit_one(c);
1207        }
1208}
1209